Search in sources :

Example 6 with ExprHasName

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

the class VizTree method convertValueToText.

/**
 * {@inheritDoc}
 */
@Override
public String convertValueToText(Object val, boolean selected, boolean expanded, boolean leaf, int row, boolean focus) {
    String c = ">";
    if (onWindows)
        c = selected ? " style=\"color:#ffffff;\">" : " style=\"color:#000000;\">";
    if (val instanceof A4Solution)
        return "<html> <b" + c + encode(title == null ? "" : title) + "</b></html>";
    if (val instanceof Sig) {
        String label = ((Sig) val).label;
        if (label.startsWith("this/"))
            label = label.substring(5);
        return "<html> <b" + c + "sig " + encode(label) + "</b></html>";
    }
    if (val instanceof ExprVar)
        return "<html> <b" + c + "set " + encode(((ExprVar) val).label) + "</b></html>";
    if (val instanceof String)
        return "<html> <span" + c + encode((String) val) + "</span></html>";
    if (val instanceof Pair)
        return "<html> <b" + c + "field " + encode(((ExprHasName) (((Pair<?, ?>) val).b)).label) + "</b></html>";
    if (val instanceof A4Tuple) {
        StringBuilder sb = new StringBuilder("<html> <span" + c);
        A4Tuple tp = (A4Tuple) val;
        for (int i = 1; i < tp.arity(); i++) {
            if (i > 1)
                sb.append(" -> ");
            sb.append(encode(tp.atom(i)));
        }
        sb.append("</span></html>");
        return sb.toString();
    }
    return "";
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) A4Solution(edu.mit.csail.sdg.translator.A4Solution) Pair(edu.mit.csail.sdg.alloy4.Pair)

Example 7 with ExprHasName

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

the class CompModule method addFunc.

/**
 * Add a FUN or PRED declaration.
 */
void addFunc(Pos p, Pos isPrivate, String n, Expr f, List<Decl> decls, Expr t, Expr v) throws Err {
    if (decls == null)
        decls = new ArrayList<Decl>();
    else
        decls = new ArrayList<Decl>(decls);
    if (f != null)
        decls.add(0, new Decl(null, null, null, Util.asList(ExprVar.make(f.span(), "this")), f));
    for (Decl d : decls) {
        if (d.isPrivate != null) {
            ExprHasName name = d.names.get(0);
            throw new ErrorSyntax(d.isPrivate.merge(name.pos), "Function parameter \"" + name.label + "\" is always private already.");
        }
        if (d.disjoint2 != null) {
            ExprHasName name = d.names.get(d.names.size() - 1);
            throw new ErrorSyntax(d.disjoint2.merge(name.pos), "Function parameter \"" + name.label + "\" cannot be bound to a 'disjoint' expression.");
        }
    }
    status = 3;
    dup(p, n, false);
    ExprHasName dup = Decl.findDuplicateName(decls);
    if (dup != null)
        throw new ErrorSyntax(dup.span(), "The parameter name \"" + dup.label + "\" cannot appear more than once.");
    Func ans = new Func(p, isPrivate, n, decls, t, v);
    ArrayList<Func> list = funcs.get(n);
    if (list == null) {
        list = new ArrayList<Func>();
        funcs.put(n, list);
    }
    list.add(ans);
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Func(edu.mit.csail.sdg.ast.Func) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) ArrayList(java.util.ArrayList) Decl(edu.mit.csail.sdg.ast.Decl)

Example 8 with ExprHasName

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

the class CompModule method resolveFuncDecls.

/**
 * Each FunAST will now point to a bodyless Func object.
 */
private JoinableList<Err> resolveFuncDecls(A4Reporter rep, JoinableList<Err> errors, List<ErrorWarning> warns) throws Err {
    for (ArrayList<Func> list : funcs.values()) {
        for (int listi = 0; listi < list.size(); listi++) {
            Func f = list.get(listi);
            String fullname = (path.length() == 0 ? "this/" : (path + "/")) + f.label;
            // Each PARAMETER can refer to earlier parameter in the same
            // function, and any SIG or FIELD visible from here.
            // Each RETURNTYPE can refer to the parameters of the same
            // function, and any SIG or FIELD visible from here.
            Context cx = new Context(this, warns);
            cx.rootfunparam = true;
            TempList<Decl> tmpdecls = new TempList<Decl>();
            boolean err = false;
            for (Decl d : f.decls) {
                TempList<ExprVar> tmpvars = new TempList<ExprVar>();
                Expr val = cx.check(d.expr).resolve_as_set(warns);
                if (!val.errors.isEmpty()) {
                    err = true;
                    errors = errors.make(val.errors);
                }
                for (ExprHasName n : d.names) {
                    ExprVar v = ExprVar.make(n.span(), n.label, val.type());
                    cx.put(n.label, v);
                    tmpvars.add(v);
                    rep.typecheck((f.isPred ? "pred " : "fun ") + fullname + ", Param " + n.label + ": " + v.type() + "\n");
                }
                tmpdecls.add(new Decl(d.isPrivate, d.disjoint, d.disjoint2, tmpvars.makeConst(), val));
            }
            Expr ret = null;
            if (!f.isPred) {
                ret = cx.check(f.returnDecl).resolve_as_set(warns);
                if (!ret.errors.isEmpty()) {
                    err = true;
                    errors = errors.make(ret.errors);
                }
            }
            if (err)
                continue;
            try {
                f = new Func(f.pos, f.isPrivate, fullname, tmpdecls.makeConst(), ret, f.getBody());
                list.set(listi, f);
                rep.typecheck("" + f + ", RETURN: " + f.returnDecl.type() + "\n");
            } catch (Err ex) {
                errors = errors.make(ex);
            }
        }
    }
    return errors;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Err(edu.mit.csail.sdg.alloy4.Err) Func(edu.mit.csail.sdg.ast.Func) Decl(edu.mit.csail.sdg.ast.Decl) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) Expr(edu.mit.csail.sdg.ast.Expr) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName)

Example 9 with ExprHasName

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

the class TranslateAlloyToKodkod method visit_qt.

/**
 * Helper method that translates the quantification expression "op vars | sub"
 */
private Object visit_qt(final ExprQt.Op op, final ConstList<Decl> xvars, final Expr sub) throws Err {
    if (op == ExprQt.Op.NO) {
        return visit_qt(ExprQt.Op.ALL, xvars, sub.not());
    }
    if (op == ExprQt.Op.ONE || op == ExprQt.Op.LONE) {
        boolean ok = true;
        for (int i = 0; i < xvars.size(); i++) {
            Expr v = addOne(xvars.get(i).expr).deNOP();
            if (v.type().arity() != 1 || v.mult() != ExprUnary.Op.ONEOF) {
                ok = false;
                break;
            }
        }
        if (op == ExprQt.Op.ONE && ok)
            return ((Expression) visit_qt(ExprQt.Op.COMPREHENSION, xvars, sub)).one();
        if (op == ExprQt.Op.LONE && ok)
            return ((Expression) visit_qt(ExprQt.Op.COMPREHENSION, xvars, sub)).lone();
    }
    if (op == ExprQt.Op.ONE) {
        Formula f1 = (Formula) visit_qt(ExprQt.Op.LONE, xvars, sub);
        Formula f2 = (Formula) visit_qt(ExprQt.Op.SOME, xvars, sub);
        return f1.and(f2);
    }
    if (op == ExprQt.Op.LONE) {
        QuantifiedFormula p1 = (QuantifiedFormula) visit_qt(ExprQt.Op.ALL, xvars, sub);
        QuantifiedFormula p2 = (QuantifiedFormula) visit_qt(ExprQt.Op.ALL, xvars, sub);
        Decls s1 = p1.decls(), s2 = p2.decls(), decls = null;
        Formula f1 = p1.formula(), f2 = p2.formula();
        Formula[] conjuncts = new Formula[s1.size()];
        for (int i = 0; i < conjuncts.length; i++) {
            kodkod.ast.Decl d1 = s1.get(i), d2 = s2.get(i);
            conjuncts[i] = d1.variable().eq(d2.variable());
            if (decls == null)
                decls = d1.and(d2);
            else
                decls = decls.and(d1).and(d2);
        }
        return f1.and(f2).implies(Formula.and(conjuncts)).forAll(decls);
    }
    Decls dd = null;
    List<Formula> guards = new ArrayList<Formula>();
    for (Decl dep : xvars) {
        final Expr dexexpr = addOne(dep.expr);
        final Expression dv = cset(dexexpr);
        for (ExprHasName dex : dep.names) {
            final Variable v = Variable.nary(skolem(dex.label), dex.type().arity());
            final kodkod.ast.Decl newd;
            env.put((ExprVar) dex, v);
            if (dex.type().arity() != 1) {
                guards.add(isIn(v, dexexpr));
                newd = v.setOf(dv);
            } else
                switch(dexexpr.mult()) {
                    case SETOF:
                        newd = v.setOf(dv);
                        break;
                    case SOMEOF:
                        newd = v.someOf(dv);
                        break;
                    case LONEOF:
                        newd = v.loneOf(dv);
                        break;
                    default:
                        newd = v.oneOf(dv);
                }
            if (frame != null)
                frame.kv2typepos(v, dex.type(), dex.pos);
            if (dd == null)
                dd = newd;
            else
                dd = dd.and(newd);
        }
    }
    final Formula ans = (op == ExprQt.Op.SUM) ? null : cform(sub);
    final IntExpression ians = (op != ExprQt.Op.SUM) ? null : cint(sub);
    for (Decl d : xvars) for (ExprHasName v : d.names) env.remove((ExprVar) v);
    if (op == ExprQt.Op.COMPREHENSION)
        // guards.size()==0, since each var
        return ans.comprehension(dd);
    // has to be unary
    if (op == ExprQt.Op.SUM)
        // guards.size()==0, since each var has to be
        return ians.sum(dd);
    // unary
    if (op == ExprQt.Op.SOME) {
        if (guards.size() == 0)
            return ans.forSome(dd);
        guards.add(ans);
        return Formula.and(guards).forSome(dd);
    } else {
        if (guards.size() == 0)
            return ans.forAll(dd);
        return Formula.and(guards).implies(ans).forAll(dd);
    }
}
Also used : ExprToIntCast(kodkod.ast.ExprToIntCast) IntToExprCast(kodkod.ast.IntToExprCast) Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) IntExpression(kodkod.ast.IntExpression) ArrayList(java.util.ArrayList) Decl(edu.mit.csail.sdg.ast.Decl) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) Expr(edu.mit.csail.sdg.ast.Expr) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) IntExpression(kodkod.ast.IntExpression) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) QuantifiedFormula(kodkod.ast.QuantifiedFormula)

Example 10 with ExprHasName

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

the class DemoFileSystem method acyclic.

/*
     * These corresponds to the helper predicates/functions provided in util/*.als
     */
static Expr acyclic(Expr r) throws Err {
    // x is a variable over the domain
    Decl d = r.join(Sig.UNIV).oneOf("x");
    // of r
    ExprHasName x = d.get();
    // (x !in x.^r) for
    return x.in(x.join(r.closure())).not().forAll(d);
// all x
}
Also used : ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) Decl(edu.mit.csail.sdg.ast.Decl)

Aggregations

ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)10 Decl (edu.mit.csail.sdg.ast.Decl)9 Expr (edu.mit.csail.sdg.ast.Expr)7 Func (edu.mit.csail.sdg.ast.Func)4 Sig (edu.mit.csail.sdg.ast.Sig)4 Err (edu.mit.csail.sdg.alloy4.Err)3 ExprVar (edu.mit.csail.sdg.ast.ExprVar)3 Field (edu.mit.csail.sdg.ast.Sig.Field)3 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)2 ErrorWarning (edu.mit.csail.sdg.alloy4.ErrorWarning)2 Pair (edu.mit.csail.sdg.alloy4.Pair)2 Module (edu.mit.csail.sdg.ast.Module)2 A4Solution (edu.mit.csail.sdg.translator.A4Solution)2 ArrayList (java.util.ArrayList)2 BinaryExpression (kodkod.ast.BinaryExpression)2 Expression (kodkod.ast.Expression)2 IntExpression (kodkod.ast.IntExpression)2 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)1 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)1 Command (edu.mit.csail.sdg.ast.Command)1