Search in sources :

Example 6 with Decl

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

the class SimpleCLI method main.

public static void main(String[] args) throws Exception {
    final boolean sat4j = "yes".equals(System.getProperty("sat4j"));
    final boolean minisat = "yes".equals(System.getProperty("minisat"));
    SatSolver solver = A4Options.SatSolver.make("mem", "mem", "/zweb/sat/mem");
    final SimpleReporter rep = new SimpleReporter();
    final StringBuilder sb = rep.sb;
    for (String filename : args) {
        try {
            // Parse+Typecheck
            rep.sb.append("\n\nMain file = " + filename + "\n");
            if (db)
                db("Parsing+Typechecking...");
            Module world = CompUtil.parseEverything_fromFile(rep, null, filename);
            if (db)
                db(" ok\n");
            List<Command> cmds = world.getAllCommands();
            for (ErrorWarning msg : rep.warnings) rep.sb.append("Relevance Warning:\n" + (msg.toString().trim()) + "\n\n");
            rep.warnings.clear();
            // Do a detailed dump if we will not be executing the commands
            if (args.length != 1) {
                for (Module m : world.getAllReachableModules()) {
                    for (Sig x : m.getAllSigs()) {
                        sb.append("\nSig ").append(x.label).append(" at position ").append(x.pos).append("\n");
                        for (Decl d : x.getFieldDecls()) for (ExprHasName f : d.names) {
                            sb.append("\nField ").append(f.label).append(" with type ").append(f.type()).append("\n");
                            d.expr.toString(sb, 2);
                        }
                        rep.flush();
                    }
                    for (Func x : m.getAllFunc()) {
                        sb.append("\nFun/pred ").append(x.label).append(" at position ").append(x.pos).append("\n");
                        for (Decl d : x.decls) for (ExprHasName v : d.names) {
                            v.toString(sb, 2);
                            d.expr.toString(sb, 4);
                        }
                        x.returnDecl.toString(sb, 2);
                        x.getBody().toString(sb, 4);
                        rep.flush();
                    }
                    for (Pair<String, Expr> x : m.getAllFacts()) {
                        sb.append("\nFact ").append(x.a).append("\n");
                        x.b.toString(sb, 4);
                        rep.flush();
                    }
                    for (Pair<String, Expr> x : m.getAllAssertions()) {
                        sb.append("\nAssertion ").append(x.a).append("\n");
                        x.b.toString(sb, 4);
                        rep.flush();
                    }
                    if (m == world)
                        for (Command x : m.getAllCommands()) {
                            sb.append("\nCommand ").append(x.label).append("\n");
                            x.formula.toString(sb, 4);
                            rep.flush();
                        }
                }
                continue;
            }
            // Validate the metamodel generation code
            StringWriter metasb = new StringWriter();
            PrintWriter meta = new PrintWriter(metasb);
            Util.encodeXMLs(meta, "\n<alloy builddate=\"", Version.buildDate(), "\">\n\n");
            A4SolutionWriter.writeMetamodel(world.getAllReachableSigs(), filename, meta);
            Util.encodeXMLs(meta, "\n</alloy>");
            meta.flush();
            metasb.flush();
            String metaxml = metasb.toString();
            A4SolutionReader.read(new ArrayList<Sig>(), new XMLNode(new StringReader(metaxml)));
            StaticInstanceReader.parseInstance(new StringReader(metaxml));
            // Okay, now solve the commands
            A4Options options = new A4Options();
            options.originalFilename = filename;
            options.solverDirectory = "/zweb/zweb/tmp/alloy4/x86-freebsd";
            options.solver = sat4j ? A4Options.SatSolver.SAT4J : (minisat ? A4Options.SatSolver.MiniSatJNI : solver);
            for (int i = 0; i < cmds.size(); i++) {
                Command c = cmds.get(i);
                if (db) {
                    String cc = c.toString();
                    if (cc.length() > 60)
                        cc = cc.substring(0, 55);
                    db("Executing " + cc + "...\n");
                }
                rep.sb.append("Executing \"" + c + "\"\n");
                options.skolemDepth = 0;
                A4Solution s = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), c, options);
                if (s.satisfiable()) {
                    validate(s);
                    if (s.isIncremental()) {
                        s = s.next();
                        if (s.satisfiable())
                            validate(s);
                    }
                }
                options.skolemDepth = 2;
                s = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), c, options);
                if (s.satisfiable()) {
                    validate(s);
                    if (s.isIncremental()) {
                        s = s.next();
                        if (s.satisfiable())
                            validate(s);
                    }
                }
            }
        } catch (Throwable ex) {
            rep.sb.append("\n\nException: " + ex);
        }
        if (db) {
            if (args.length != 1)
                db(" ERROR!\n");
            else
                db("\n\n");
        }
    }
    rep.close();
}
Also used : XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Func(edu.mit.csail.sdg.ast.Func) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning) Decl(edu.mit.csail.sdg.ast.Decl) Sig(edu.mit.csail.sdg.ast.Sig) Expr(edu.mit.csail.sdg.ast.Expr) StringWriter(java.io.StringWriter) Command(edu.mit.csail.sdg.ast.Command) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) A4Options(edu.mit.csail.sdg.translator.A4Options) StringReader(java.io.StringReader) Module(edu.mit.csail.sdg.ast.Module) SatSolver(edu.mit.csail.sdg.translator.A4Options.SatSolver) PrintWriter(java.io.PrintWriter) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Example 7 with Decl

use of edu.mit.csail.sdg.ast.Decl 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 Decl

use of edu.mit.csail.sdg.ast.Decl 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 Decl

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

the class CompModule method addDefaultCommand.

public void addDefaultCommand() {
    if (commands.isEmpty()) {
        addFunc(Pos.UNKNOWN, Pos.UNKNOWN, "$$Default", null, new ArrayList<Decl>(), null, ExprConstant.TRUE);
        commands.add(new Command(Pos.UNKNOWN, ExprConstant.TRUE, "Default", false, 4, 4, 4, 0, null, null, ExprVar.make(null, "$$Default"), null));
    }
}
Also used : Command(edu.mit.csail.sdg.ast.Command) Decl(edu.mit.csail.sdg.ast.Decl)

Example 10 with Decl

use of edu.mit.csail.sdg.ast.Decl 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)

Aggregations

Decl (edu.mit.csail.sdg.ast.Decl)12 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)9 Expr (edu.mit.csail.sdg.ast.Expr)8 Func (edu.mit.csail.sdg.ast.Func)5 Sig (edu.mit.csail.sdg.ast.Sig)4 Err (edu.mit.csail.sdg.alloy4.Err)3 Command (edu.mit.csail.sdg.ast.Command)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 ExprVar (edu.mit.csail.sdg.ast.ExprVar)2 Module (edu.mit.csail.sdg.ast.Module)2 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)2 A4Options (edu.mit.csail.sdg.translator.A4Options)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