Search in sources :

Example 41 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class TranslateAlloyToKodkod method visit.

/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprList x) throws Err {
    if (x.op == ExprList.Op.AND || x.op == ExprList.Op.OR) {
        if (x.args.size() == 0)
            return (x.op == ExprList.Op.AND) ? Formula.TRUE : Formula.FALSE;
        Formula answer = getSingleFormula(x.op == ExprList.Op.AND, 1, x.args);
        return k2pos(answer, x);
    }
    if (x.op == ExprList.Op.TOTALORDER) {
        Expression elem = cset(x.args.get(0)), first = cset(x.args.get(1)), next = cset(x.args.get(2));
        if (elem instanceof Relation && first instanceof Relation && next instanceof Relation) {
            Relation lst = frame.addRel("", null, frame.query(true, elem, false));
            totalOrderPredicates.add((Relation) elem);
            totalOrderPredicates.add((Relation) first);
            totalOrderPredicates.add(lst);
            totalOrderPredicates.add((Relation) next);
            return k2pos(((Relation) next).totalOrder((Relation) elem, (Relation) first, lst), x);
        }
        // every
        Formula f1 = elem.in(first.join(next.reflexiveClosure()));
        // element
        // is in
        // the
        // total
        // order
        // first element has no
        Formula f2 = next.join(first).no();
        // predecessor
        Variable e = Variable.unary("v" + Integer.toString(cnt++));
        // each element
        Formula f3 = e.eq(first).or(next.join(e).one());
        // (except the
        // first) has
        // one
        // predecessor
        // each
        Formula f4 = e.eq(elem.difference(next.join(elem))).or(e.join(next).one());
        // element
        // (except
        // the
        // last)
        // has
        // one
        // successor
        // there are no
        Formula f5 = e.in(e.join(next.closure())).not();
        // cycles
        return k2pos(f3.and(f4).and(f5).forAll(e.oneOf(elem)).and(f1).and(f2), x);
    }
    // This says no(a&b) and no((a+b)&c) and no((a+b+c)&d)...
    // Empirically this seems to be more efficient than "no(a&b) and no(a&c)
    // and no(b&c)"
    Formula answer = null;
    Expression a = null;
    for (Expr arg : x.args) {
        Expression b = cset(arg);
        if (a == null) {
            a = b;
            continue;
        }
        if (answer == null)
            answer = a.intersection(b).no();
        else
            answer = a.intersection(b).no().and(answer);
        a = a.union(b);
    }
    if (answer != null)
        return k2pos(answer, x);
    else
        return Formula.TRUE;
}
Also used : QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Variable(kodkod.ast.Variable) Expr(edu.mit.csail.sdg.ast.Expr) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) IntExpression(kodkod.ast.IntExpression)

Example 42 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class TranslateAlloyToKodkod method visit.

/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprCall x) throws Err {
    final Func f = x.fun;
    final Object candidate = f.count() == 0 ? cacheForConstants.get(f) : null;
    if (candidate != null)
        return candidate;
    final Expr body = f.getBody();
    if (body.type().arity() < 0 || body.type().arity() != f.returnDecl.type().arity())
        throw new ErrorType(body.span(), "Function return value not fully resolved.");
    final int n = f.count();
    int maxRecursion = unrolls;
    for (Func ff : current_function) if (ff == f) {
        if (maxRecursion < 0) {
            throw new ErrorSyntax(x.span(), "" + f + " cannot call itself recursively!");
        }
        if (maxRecursion == 0) {
            Type t = f.returnDecl.type();
            if (t.is_bool)
                return Formula.FALSE;
            if (t.is_int())
                return IntConstant.constant(0);
            int i = t.arity();
            Expression ans = Expression.NONE;
            while (i > 1) {
                ans = ans.product(Expression.NONE);
                i--;
            }
            return ans;
        }
        maxRecursion--;
    }
    Env<ExprVar, Object> newenv = new Env<ExprVar, Object>();
    for (int i = 0; i < n; i++) newenv.put(f.get(i), cset(x.args.get(i)));
    Env<ExprVar, Object> oldenv = env;
    env = newenv;
    current_function.add(f);
    Object ans = visitThis(body);
    env = oldenv;
    current_function.remove(current_function.size() - 1);
    if (ans instanceof Formula)
        k2pos((Formula) ans, x);
    if (f.count() == 0)
        cacheForConstants.put(f, ans);
    return ans;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Type(edu.mit.csail.sdg.ast.Type) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Expr(edu.mit.csail.sdg.ast.Expr) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Func(edu.mit.csail.sdg.ast.Func) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) IntExpression(kodkod.ast.IntExpression) Env(edu.mit.csail.sdg.alloy4.Env)

Example 43 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class CUP$CompParser$actions method syntax_error.

@Override
public void syntax_error(Symbol x) throws Err {
    Map<Integer, String> ch = new LinkedHashMap<Integer, String>();
    ch.put(CompSym.ARROW, "->");
    ch.put(CompSym.ANY_ARROW_SOME, "->");
    ch.put(CompSym.ANY_ARROW_ONE, "->");
    ch.put(CompSym.ANY_ARROW_LONE, "->");
    ch.put(CompSym.SOME_ARROW_ANY, "some");
    ch.put(CompSym.SOME_ARROW_SOME, "some");
    ch.put(CompSym.SOME_ARROW_ONE, "some");
    ch.put(CompSym.SOME_ARROW_LONE, "some");
    ch.put(CompSym.ONE_ARROW_ANY, "one");
    ch.put(CompSym.ONE_ARROW_SOME, "one");
    ch.put(CompSym.ONE_ARROW_ONE, "one");
    ch.put(CompSym.ONE_ARROW_LONE, "one");
    ch.put(CompSym.LONE_ARROW_ANY, "lone");
    ch.put(CompSym.LONE_ARROW_SOME, "lone");
    ch.put(CompSym.LONE_ARROW_ONE, "lone");
    ch.put(CompSym.LONE_ARROW_LONE, "lone");
    ch.put(CompSym.INTADD, "fun");
    ch.put(CompSym.INTSUB, "fun");
    ch.put(CompSym.INTMUL, "fun");
    ch.put(CompSym.INTDIV, "fun");
    ch.put(CompSym.INTREM, "fun");
    ch.put(CompSym.INTMIN, "fun");
    ch.put(CompSym.INTMAX, "fun");
    ch.put(CompSym.INTNEXT, "fun");
    ch.put(CompSym.TOTALORDER, "pred");
    ch.put(CompSym.ABSTRACT, "abstract");
    ch.put(CompSym.ALL, "all");
    ch.put(CompSym.ALL2, "all");
    ch.put(CompSym.AMPERSAND, "&");
    ch.put(CompSym.AND, "&&");
    ch.put(CompSym.AS, "as");
    ch.put(CompSym.ASSERT, "assert");
    ch.put(CompSym.AT, "@");
    ch.put(CompSym.BAR, "|");
    ch.put(CompSym.BUT, "but");
    ch.put(CompSym.CARET, "^");
    ch.put(CompSym.CHECK, "check");
    ch.put(CompSym.COLON, ":");
    ch.put(CompSym.COMMA, ", ");
    ch.put(CompSym.DISJ, "disj");
    ch.put(CompSym.DOMAIN, "<:");
    ch.put(CompSym.DOT, ".");
    ch.put(CompSym.ELSE, "else");
    ch.put(CompSym.ENUM, "enum");
    ch.put(CompSym.EQUALS, "=");
    ch.put(CompSym.EXACTLY, "exactly");
    ch.put(CompSym.EXH, "exh");
    ch.put(CompSym.EXPECT, "expect");
    ch.put(CompSym.EXTENDS, "extends");
    ch.put(CompSym.FACT, "fact");
    ch.put(CompSym.FOR, "for");
    ch.put(CompSym.FUN, "fun");
    ch.put(CompSym.GT, ">");
    ch.put(CompSym.GTE, ">=");
    ch.put(CompSym.HASH, "#");
    ch.put(CompSym.IDEN, "iden");
    ch.put(CompSym.IFF, "iff");
    ch.put(CompSym.IMPLIES, "=>");
    ch.put(CompSym.IN, "in");
    ch.put(CompSym.INT, "int");
    ch.put(CompSym.LBRACE, "{");
    ch.put(CompSym.LBRACKET, "[");
    ch.put(CompSym.LET, "let");
    ch.put(CompSym.LONE2, "lone");
    ch.put(CompSym.LONE, "lone");
    ch.put(CompSym.LPAREN, "(");
    ch.put(CompSym.LT, "<");
    ch.put(CompSym.LTE, "<=");
    ch.put(CompSym.MINUS, "-");
    ch.put(CompSym.MODULE, "module");
    ch.put(CompSym.NO2, "no");
    ch.put(CompSym.NO, "no");
    ch.put(CompSym.NONE, "none");
    ch.put(CompSym.NOT, "!");
    ch.put(CompSym.NOTEQUALS, "!");
    ch.put(CompSym.NOTGT, "!");
    ch.put(CompSym.NOTGTE, "!");
    ch.put(CompSym.NOTIN, "!");
    ch.put(CompSym.NOTLT, "!");
    ch.put(CompSym.NOTLTE, "!");
    ch.put(CompSym.ONE2, "one");
    ch.put(CompSym.ONE, "one");
    ch.put(CompSym.OPEN, "open");
    ch.put(CompSym.OR, "||");
    ch.put(CompSym.PART, "part");
    ch.put(CompSym.PLUS, "+");
    ch.put(CompSym.PLUSPLUS, "++");
    ch.put(CompSym.PRED, "pred");
    ch.put(CompSym.PRIVATE, "private");
    ch.put(CompSym.RANGE, ":>");
    ch.put(CompSym.RBRACE, "}");
    ch.put(CompSym.RBRACKET, "]");
    ch.put(CompSym.RPAREN, ")");
    ch.put(CompSym.RUN, "run");
    ch.put(CompSym.SEQ, "seq");
    ch.put(CompSym.SET, "set");
    ch.put(CompSym.SHL, "<<");
    ch.put(CompSym.SHR, ">>>");
    ch.put(CompSym.SHA, ">>");
    ch.put(CompSym.SIG, "sig");
    ch.put(CompSym.SIGINT, "Int");
    ch.put(CompSym.SLASH, "/");
    ch.put(CompSym.SOME2, "some");
    ch.put(CompSym.SOME, "some");
    ch.put(CompSym.STAR, "*");
    ch.put(CompSym.STRING, "String");
    ch.put(CompSym.SUM2, "sum");
    ch.put(CompSym.SUM, "sum");
    ch.put(CompSym.THIS, "this");
    ch.put(CompSym.TILDE, "~");
    ch.put(CompSym.UNIV, "univ");
    ch.put(CompSym.ID, "NAME");
    ch.put(CompSym.NUMBER, "NUMBER");
    ch.put(CompSym.STR, "STRING");
    TreeSet<String> list = new TreeSet<String>();
    Pos p = Pos.UNKNOWN;
    if (x != null && x.value instanceof Pos)
        p = (Pos) (x.value);
    else if (x != null && x.value instanceof Expr)
        p = ((Expr) (x.value)).pos;
    else if (x != null)
        p = x.pos;
    if (!stack.empty())
        for (Map.Entry<Integer, String> e : ch.entrySet()) {
            int key = e.getKey(), act = get_action(((Symbol) stack.peek()).parse_state, key);
            if (act == 0)
                continue;
            try {
                if (act > 0 || alloy_confirm(key))
                    list.add(e.getValue());
            } catch (Throwable ex) {
            // If the parser is really really confused, alloy_confirm() could fail with array out-of-bound exception, etc.
            }
        }
    String result = "There are " + list.size() + " possible tokens that can appear here:\n";
    for (String item : list) result = result + item + " ";
    throw new ErrorSyntax(p, (list.size() != 0) ? result : "");
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos) TreeSet(java.util.TreeSet) Symbol(java_cup.runtime.Symbol) LinkedHashMap(java.util.LinkedHashMap)

Example 44 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class A4SolutionReader method parseSig.

/**
 * Parse sig/set.
 */
private Sig parseSig(String id, int depth) throws IOException, Err {
    Sig ans = id2sig.get(id);
    if (ans != null)
        return ans;
    XMLNode node = nmap.get(id);
    if (node == null)
        throw new IOException("Unknown SigID " + id + " encountered.");
    if (!node.is("sig"))
        throw new IOException("ID " + id + " is not a sig.");
    String label = label(node);
    Attr isAbstract = yes(node, "abstract") ? Attr.ABSTRACT : null;
    Attr isOne = yes(node, "one") ? Attr.ONE : null;
    Attr isLone = yes(node, "lone") ? Attr.LONE : null;
    Attr isSome = yes(node, "some") ? Attr.SOME : null;
    Attr isPrivate = yes(node, "private") ? Attr.PRIVATE : null;
    Attr isMeta = yes(node, "meta") ? Attr.META : null;
    Attr isEnum = yes(node, "enum") ? Attr.ENUM : null;
    Attr isExact = yes(node, "exact") ? Attr.EXACT : null;
    if (yes(node, "builtin")) {
        if (label.equals(UNIV.label)) {
            id2sig.put(id, UNIV);
            return UNIV;
        }
        if (label.equals(SIGINT.label)) {
            id2sig.put(id, SIGINT);
            return SIGINT;
        }
        if (label.equals(SEQIDX.label)) {
            id2sig.put(id, SEQIDX);
            return SEQIDX;
        }
        if (label.equals(STRING.label)) {
            id2sig.put(id, STRING);
            return STRING;
        }
        throw new IOException("Unknown builtin sig: " + label + " (id=" + id + ")");
    }
    if (depth > nmap.size())
        throw new IOException("Sig " + label + " (id=" + id + ") is in a cyclic inheritance relationship.");
    List<Sig> parents = null;
    TupleSet ts = factory.noneOf(1);
    for (XMLNode sub : node) {
        if (sub.is("atom")) {
            ts.add(factory.tuple(sub.getAttribute("label")));
            continue;
        }
        if (!sub.is("type"))
            continue;
        Sig parent = parseSig(sub.getAttribute("ID"), depth + 1);
        if (parents == null)
            parents = new ArrayList<Sig>();
        parents.add(parent);
    }
    if (parents == null) {
        String parentID = node.getAttribute("parentID");
        Sig parent = parseSig(parentID, depth + 1);
        if (!(parent instanceof PrimSig))
            throw new IOException("Parent of sig " + label + " (id=" + id + ") must not be a subset sig.");
        for (Expr choice : choices) if (choice instanceof PrimSig && parent == ((PrimSig) choice).parent && ((Sig) choice).label.equals(label)) {
            ans = (Sig) choice;
            choices.remove(choice);
            break;
        }
        if (ans == null) {
            ans = new PrimSig(label, (PrimSig) parent, isAbstract, isLone, isOne, isSome, isPrivate, isMeta, isEnum);
            allsigs.add(ans);
        }
    } else {
        for (Expr choice : choices) if (choice instanceof SubsetSig && ((Sig) choice).label.equals(label) && sameset(parents, ((SubsetSig) choice).parents)) {
            ans = (Sig) choice;
            choices.remove(choice);
            break;
        }
        if (ans == null) {
            ans = new SubsetSig(label, parents, isExact, isLone, isOne, isSome, isPrivate, isMeta);
            allsigs.add(ans);
        }
    }
    id2sig.put(id, ans);
    expr2ts.put(ans, ts);
    if (ans instanceof PrimSig) {
        // Add the atoms in this SIG into all parent sigs
        for (PrimSig ans2 = ((PrimSig) ans).parent; ans2 != null && !ans2.builtin; ans2 = ans2.parent) {
            TupleSet ts2 = expr2ts.get(ans2);
            if (ts2 == null)
                ts2 = ts.clone();
            else {
                ts2 = ts2.clone();
                ts2.addAll(ts);
            }
            expr2ts.put(ans2, ts2);
        }
    }
    return ans;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) TupleSet(kodkod.instance.TupleSet) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Expr(edu.mit.csail.sdg.ast.Expr) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Attr(edu.mit.csail.sdg.ast.Attr) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 45 with Expr

use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.

the class ConvToConjunction method visit.

/**
 * {@inheritDoc}
 */
@Override
public Expr visit(ExprQt x) throws Err {
    if (x.op == ExprQt.Op.ALL) {
        Expr s = x.sub.deNOP();
        if (s instanceof ExprBinary && ((ExprBinary) s).op == ExprBinary.Op.AND) {
            Expr a = visitThis(x.op.make(Pos.UNKNOWN, Pos.UNKNOWN, x.decls, ((ExprBinary) s).left));
            Expr b = visitThis(x.op.make(Pos.UNKNOWN, Pos.UNKNOWN, x.decls, ((ExprBinary) s).right));
            return a.and(b);
        }
    }
    return x;
}
Also used : ExprBinary(edu.mit.csail.sdg.ast.ExprBinary) Expr(edu.mit.csail.sdg.ast.Expr)

Aggregations

Expr (edu.mit.csail.sdg.ast.Expr)46 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)17 Sig (edu.mit.csail.sdg.ast.Sig)15 Func (edu.mit.csail.sdg.ast.Func)12 ExprVar (edu.mit.csail.sdg.ast.ExprVar)11 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)11 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)9 Decl (edu.mit.csail.sdg.ast.Decl)8 Field (edu.mit.csail.sdg.ast.Sig.Field)8 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)6 Pos (edu.mit.csail.sdg.alloy4.Pos)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)6 Command (edu.mit.csail.sdg.ast.Command)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 BinaryExpression (kodkod.ast.BinaryExpression)5