Search in sources :

Example 26 with Expr

use of edu.mit.csail.sdg.ast.Expr 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 27 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(ExprUnary x) throws Err {
    if (x.op == ExprUnary.Op.NOOP) {
        return visitThis(x.sub);
    }
    if (x.op == ExprUnary.Op.NOT) {
        Expr s = x.sub.deNOP();
        if (s instanceof ExprBinary && ((ExprBinary) s).op == ExprBinary.Op.OR) {
            Expr a = visitThis(((ExprBinary) s).left.not());
            Expr b = visitThis(((ExprBinary) s).right.not());
            return a.and(b);
        }
    }
    return x;
}
Also used : ExprBinary(edu.mit.csail.sdg.ast.ExprBinary) Expr(edu.mit.csail.sdg.ast.Expr)

Example 28 with Expr

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

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

the class CompModule method getSubnodes.

/**
 * {@inheritDoc}
 */
@Override
public List<? extends Browsable> getSubnodes() {
    ArrayList<Browsable> ans = new ArrayList<Browsable>();
    ArrayList<Browsable> x;
    if (opens.size() > 0) {
        x = new ArrayList<Browsable>(opens.size());
        for (Open e : opens.values()) if (!x.contains(e.realModule))
            x.add(e.realModule);
        ans.add(make("<b>" + x.size() + (x.size() > 1 ? " opens</b>" : " open</b>"), x));
    }
    if (sigs.size() > 0) {
        x = new ArrayList<Browsable>(sigs.values());
        ans.add(make("<b>" + x.size() + (x.size() > 1 ? " sigs</b>" : " sig</b>"), x));
    }
    if (funcs.size() > 0) {
        ArrayList<Browsable> x2 = new ArrayList<Browsable>(funcs.size());
        x = new ArrayList<Browsable>(funcs.size());
        for (ArrayList<Func> e : funcs.values()) for (Func f : e) if (f.isPred)
            x.add(f);
        else
            x2.add(f);
        if (x.size() > 0)
            ans.add(make("<b>" + x.size() + (x.size() > 1 ? " preds</b>" : " pred</b>"), x));
        if (x2.size() > 0)
            ans.add(make("<b>" + x2.size() + (x2.size() > 1 ? " funs</b>" : " fun</b>"), x2));
    }
    if (commands.size() > 0) {
        ArrayList<Browsable> x2 = new ArrayList<Browsable>(commands.size());
        x = new ArrayList<Browsable>(commands.size());
        for (Command e : commands) if (e.check)
            x.add(e);
        else
            x2.add(e);
        if (x.size() > 0)
            ans.add(make("<b>" + x.size() + (x.size() > 1 ? " checks</b>" : " check</b>"), x));
        if (x2.size() > 0)
            ans.add(make("<b>" + x2.size() + (x2.size() > 1 ? " runs</b>" : " run</b>"), x2));
    }
    if (facts.size() > 0) {
        x = new ArrayList<Browsable>(facts.size());
        for (Pair<String, Expr> e : facts) x.add(make(e.b.span(), e.b.span(), "<b>fact " + e.a + "</b>", e.b));
        ans.add(make("<b>" + x.size() + (x.size() > 1 ? " facts</b>" : " fact</b>"), x));
    }
    if (asserts.size() > 0) {
        x = new ArrayList<Browsable>(asserts.size());
        for (Map.Entry<String, Expr> e : asserts.entrySet()) {
            Pos sp = e.getValue().span();
            x.add(make(sp, sp, "<b>assert</b> " + e.getKey(), e.getValue()));
        }
        ans.add(make("<b>" + x.size() + (x.size() > 1 ? " asserts</b>" : " assert</b>"), x));
    }
    return ans;
}
Also used : Browsable(edu.mit.csail.sdg.ast.Browsable) Func(edu.mit.csail.sdg.ast.Func) ArrayList(java.util.ArrayList) Expr(edu.mit.csail.sdg.ast.Expr) Command(edu.mit.csail.sdg.ast.Command) Pos(edu.mit.csail.sdg.alloy4.Pos) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with Expr

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

the class CompModule method populate.

/**
 * Resolve the name based on the current context and this module.
 */
private Expr populate(TempList<Expr> ch, TempList<String> re, Decl rootfield, Sig rootsig, boolean rootfunparam, Func rootfunbody, Pos pos, String fullname, Expr THIS) {
    // Return object can be Func(with > 0 arguments) or Expr
    final String name = (fullname.charAt(0) == '@') ? fullname.substring(1) : fullname;
    boolean fun = (rootsig != null && (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF)) || (rootsig == null && !rootfunparam);
    if (name.equals("univ"))
        return ExprUnary.Op.NOOP.make(pos, UNIV);
    if (name.equals("Int"))
        return ExprUnary.Op.NOOP.make(pos, SIGINT);
    if (name.equals("seq/Int"))
        return ExprUnary.Op.NOOP.make(pos, SEQIDX);
    if (name.equals("String"))
        return ExprUnary.Op.NOOP.make(pos, STRING);
    if (name.equals("none"))
        return ExprUnary.Op.NOOP.make(pos, NONE);
    if (name.equals("iden"))
        return ExprConstant.Op.IDEN.make(pos, 0);
    if (name.equals("sig$") || name.equals("field$"))
        if (world != null) {
            Sig s = world.sigs.get(name);
            if (s != null)
                return ExprUnary.Op.NOOP.make(pos, s);
        }
    final List<Object> ans = name.indexOf('/') >= 0 ? getRawQS(fun ? 5 : 1, name) : getRawNQS(this, fun ? 5 : 1, name);
    final Sig param = params.get(name);
    if (param != null && !ans.contains(param))
        ans.add(param);
    for (Object x : ans) {
        if (x instanceof Sig) {
            Sig y = (Sig) x;
            ch.add(ExprUnary.Op.NOOP.make(pos, y, null, 0));
            re.add("sig " + y.label);
        } else if (x instanceof Func) {
            Func f = (Func) x;
            int fn = f.count();
            int penalty = 0;
            if (resolution == 1 && fn > 0 && rootsig != null && THIS != null && THIS.type().hasArity(1) && f.get(0).type().intersects(THIS.type())) {
                // If we're inside a sig, and there is a unary variable
                // bound to "this",
                // we should consider it as a possible FIRST ARGUMENT of a
                // fun/pred call
                ConstList<Expr> t = Util.asList(THIS);
                // penalty
                ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 1 + penalty) : ExprBadCall.make(pos, null, f, t, 1 + penalty));
                // of
                // 1
                re.add((f.isPred ? "pred this." : "fun this.") + f.label);
            }
            if (resolution == 1) {
                ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, penalty) : ExprBadCall.make(pos, null, f, null, penalty));
                re.add((f.isPred ? "pred " : "fun ") + f.label);
            }
            if (resolution == 2 && f != rootfunbody && THIS != null && fullname.charAt(0) != '@' && fn > 0 && f.get(0).type().intersects(THIS.type())) {
                // If there is some value bound to "this", we should
                // consider it as a possible FIRST ARGUMENT of a fun/pred
                // call
                ConstList<Expr> t = Util.asList(THIS);
                ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 0) : ExprBadCall.make(pos, null, f, t, 0));
                re.add((f.isPred ? "pred this." : "fun this.") + f.label);
            }
            if (resolution != 1) {
                ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, 0) : ExprBadCall.make(pos, null, f, null, 0));
                re.add((f.isPred ? "pred " : "fun ") + f.label);
            }
        }
    }
    // All else: we can call, and can refer to anything visible.
    for (CompModule m : getAllNameableModules()) for (Sig s : m.sigs.values()) if (m == this || s.isPrivate == null)
        for (Field f : s.getFields()) if (f.isMeta == null && (m == this || f.isPrivate == null) && f.label.equals(name))
            if (resolution == 1) {
                Expr x = null;
                if (rootsig == null) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                } else if (rootsig.isSameOrDescendentOf(f.sig)) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                    if (fullname.charAt(0) != '@')
                        x = THIS.join(x);
                } else if (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 1);
                }
                // penalty of 1
                if (x != null) {
                    ch.add(x);
                    re.add("field " + f.sig.label + " <: " + f.label);
                }
            } else if (rootfield == null || rootsig.isSameOrDescendentOf(f.sig)) {
                Expr x0 = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                if (resolution == 2 && THIS != null && fullname.charAt(0) != '@' && f.type().firstColumnOverlaps(THIS.type())) {
                    ch.add(THIS.join(x0));
                    re.add("field " + f.sig.label + " <: this." + f.label);
                    if (rootsig != null)
                        continue;
                }
                ch.add(x0);
                re.add("field " + f.sig.label + " <: " + f.label);
            }
    if (metaSig() != null && (rootsig == null || rootfield == null)) {
        SafeList<PrimSig> children = null;
        try {
            children = metaSig().children();
        } catch (Err err) {
            return null;
        }
        // exception NOT possible
        for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
            Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
            ch.add(x);
            re.add("field " + f.sig.label + " <: " + f.label);
        }
    }
    if (metaField() != null && (rootsig == null || rootfield == null)) {
        SafeList<PrimSig> children = null;
        try {
            children = metaField().children();
        } catch (Err err) {
            return null;
        }
        // exception NOT possible
        for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
            Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
            ch.add(x);
            re.add("field " + f.sig.label + " <: " + f.label);
        }
    }
    return null;
}
Also used : Err(edu.mit.csail.sdg.alloy4.Err) Func(edu.mit.csail.sdg.ast.Func) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Field(edu.mit.csail.sdg.ast.Sig.Field) Expr(edu.mit.csail.sdg.ast.Expr) ConstList(edu.mit.csail.sdg.alloy4.ConstList) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

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