Search in sources :

Example 6 with ErrorWarning

use of edu.mit.csail.sdg.alloy4.ErrorWarning in project org.alloytools.alloy by AlloyTools.

the class ExprITE method resolve.

/**
 * {@inheritDoc}
 */
@Override
public Expr resolve(Type p, Collection<ErrorWarning> warns) {
    if (errors.size() > 0)
        return this;
    Type a = left.type, b = right.type;
    if (p.size() > 0) {
        a = a.intersect(p);
        b = b.intersect(p);
        // if (p.is_int()) { a=Type.makeInt(a); b=Type.makeInt(b); }
        if (p.is_bool) {
            a = Type.makeBool(a);
            b = Type.makeBool(b);
        }
        if (warns != null && left.type.hasTuple() && !a.hasTuple())
            warns.add(new ErrorWarning(left.span(), "This subexpression is redundant."));
        if (warns != null && right.type.hasTuple() && !b.hasTuple())
            warns.add(new ErrorWarning(right.span(), "This subexpression is redundant."));
    } else {
        a = p;
        b = p;
    }
    Expr cond = this.cond.resolve(Type.FORMULA, warns);
    Expr left = this.left.resolve(a, warns);
    Expr right = this.right.resolve(b, warns);
    return (cond == this.cond && left == this.left && right == this.right) ? this : make(pos, cond, left, right);
}
Also used : ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning)

Example 7 with ErrorWarning

use of edu.mit.csail.sdg.alloy4.ErrorWarning in project org.alloytools.alloy by AlloyTools.

the class ExampleUsingTheCompiler method main.

/*
     * Execute every command in every file. This method parses every file, then
     * execute every command. If there are syntax or type errors, it may throw a
     * ErrorSyntax or ErrorType or ErrorAPI or ErrorFatal exception. You should
     * catch them and display them, and they may contain filename/line/column
     * information.
     */
public static void main(String[] args) throws Err {
    // The visualizer (We will initialize it to nonnull when we visualize an
    // Alloy solution)
    VizGUI viz = null;
    // Alloy4 sends diagnostic messages and progress reports to the
    // A4Reporter.
    // By default, the A4Reporter ignores all these events (but you can
    // extend the A4Reporter to display the event for the user)
    A4Reporter rep = new A4Reporter() {

        // For example, here we choose to display each "warning" by printing
        // it to System.out
        @Override
        public void warning(ErrorWarning msg) {
            System.out.print("Relevance Warning:\n" + (msg.toString().trim()) + "\n\n");
            System.out.flush();
        }
    };
    for (String filename : args) {
        // Parse+typecheck the model
        System.out.println("=========== Parsing+Typechecking " + filename + " =============");
        Module world = CompUtil.parseEverything_fromFile(rep, null, filename);
        // Choose some default options for how you want to execute the
        // commands
        A4Options options = new A4Options();
        options.solver = A4Options.SatSolver.SAT4J;
        for (Command command : world.getAllCommands()) {
            // Execute the command
            System.out.println("============ Command " + command + ": ============");
            A4Solution ans = TranslateAlloyToKodkod.execute_command(rep, world.getAllReachableSigs(), command, options);
            // Print the outcome
            System.out.println(ans);
            // If satisfiable...
            if (ans.satisfiable()) {
                // You can query "ans" to find out the values of each set or
                // type.
                // This can be useful for debugging.
                // 
                // You can also write the outcome to an XML file
                ans.writeXML("alloy_example_output.xml");
                // You can then visualize the XML file by calling this:
                if (viz == null) {
                    viz = new VizGUI(false, "alloy_example_output.xml", null);
                } else {
                    viz.loadXML("alloy_example_output.xml", true);
                }
            }
        }
    }
}
Also used : VizGUI(edu.mit.csail.sdg.alloy4viz.VizGUI) Command(edu.mit.csail.sdg.ast.Command) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) A4Options(edu.mit.csail.sdg.translator.A4Options) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning) Module(edu.mit.csail.sdg.ast.Module) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Example 8 with ErrorWarning

use of edu.mit.csail.sdg.alloy4.ErrorWarning 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 9 with ErrorWarning

use of edu.mit.csail.sdg.alloy4.ErrorWarning 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 10 with ErrorWarning

use of edu.mit.csail.sdg.alloy4.ErrorWarning in project org.alloytools.alloy by AlloyTools.

the class Macro method instantiate.

/**
 * Instantiate it.
 *
 * @param warnings - the list that will receive any warning we generate; can be
 *            null if we wish to ignore warnings
 */
Expr instantiate(Context cx, List<ErrorWarning> warnings) throws Err {
    if (cx.unrolls <= 0) {
        Pos p = span();
        return new ExprBad(p, toString(), new ErrorType(p, "Macro substitution too deep; possibly indicating an infinite recursion."));
    }
    if (params.size() != args.size())
        return this;
    Context cx2 = new Context(realModule, warnings, cx.unrolls - 1);
    for (int n = params.size(), i = 0; i < n; i++) {
        Expr tmp = args.get(i);
        if (!(tmp instanceof Macro))
            tmp = tmp.resolve(tmp.type(), warnings);
        cx2.put(params.get(i).label, tmp);
    }
    return cx2.check(body);
}
Also used : Context(edu.mit.csail.sdg.parser.CompModule.Context) ExprBad(edu.mit.csail.sdg.ast.ExprBad) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos)

Aggregations

ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)8 ErrorWarning (edu.mit.csail.sdg.alloy4.ErrorWarning)7 Expr (edu.mit.csail.sdg.ast.Expr)5 Decl (edu.mit.csail.sdg.ast.Decl)4 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)4 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)3 Err (edu.mit.csail.sdg.alloy4.Err)3 Func (edu.mit.csail.sdg.ast.Func)3 ArrayList (java.util.ArrayList)3 JoinableList (edu.mit.csail.sdg.alloy4.JoinableList)2 Command (edu.mit.csail.sdg.ast.Command)2 Module (edu.mit.csail.sdg.ast.Module)2 Sig (edu.mit.csail.sdg.ast.Sig)2 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)2 ProductType (edu.mit.csail.sdg.ast.Type.ProductType)2 A4Options (edu.mit.csail.sdg.translator.A4Options)2 A4Solution (edu.mit.csail.sdg.translator.A4Solution)2 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)1 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)1 Pos (edu.mit.csail.sdg.alloy4.Pos)1