Search in sources :

Example 6 with ErrorType

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

the class ExprCall method make.

// ============================================================================================================//
/**
 * Constructs an ExprCall node with the given predicate/function "fun" and the
 * list of arguments "args".
 */
public static Expr make(Pos pos, Pos closingBracket, Func fun, List<Expr> args, long extraPenalty) {
    if (extraPenalty < 0)
        extraPenalty = 0;
    if (args == null)
        args = ConstList.make();
    long weight = extraPenalty;
    boolean ambiguous = false;
    JoinableList<Err> errs = emptyListOfErrors;
    TempList<Expr> newargs = new TempList<Expr>(args.size());
    if (args.size() != fun.count()) {
        errs = errs.make(new ErrorSyntax(pos, "" + fun + " has " + fun.count() + " parameters but is called with " + args.size() + " arguments."));
    }
    for (int i = 0; i < args.size(); i++) {
        final int a = (i < fun.count()) ? fun.get(i).type.arity() : 0;
        final Expr x = args.get(i).typecheck_as_set();
        ambiguous = ambiguous || x.ambiguous;
        errs = errs.make(x.errors);
        weight = weight + x.weight;
        if (x.mult != 0)
            errs = errs.make(new ErrorSyntax(x.span(), "Multiplicity expression not allowed here."));
        if (a > 0 && x.errors.isEmpty() && !x.type.hasArity(a))
            errs = errs.make(new ErrorType(x.span(), "This should have arity " + a + " but instead its possible type(s) are " + x.type));
        newargs.add(x);
    }
    Type t = Type.FORMULA;
    if (!fun.isPred && errs.size() == 0) {
        final Type tt = fun.returnDecl.type;
        try {
            // This provides a limited form of polymorphic function,
            // by using actual arguments at each call site to derive a
            // tighter bound on the return value.
            DeduceType d = new DeduceType();
            for (int i = 0; i < args.size(); i++) {
                ExprVar param = fun.get(i);
                d.env.put(param, newargs.get(i).type.extract(param.type.arity()));
            }
            t = fun.returnDecl.accept(d);
            if (t == null || t.is_int() || t.is_bool || t.arity() != tt.arity())
                // Just in case an error occurred...
                t = tt;
        } catch (Throwable ex) {
            // Just in case an error occurred...
            t = tt;
        }
    }
    return new ExprCall(pos, closingBracket, ambiguous, t, fun, newargs.makeConst(), extraPenalty, weight, errs);
}
Also used : Err(edu.mit.csail.sdg.alloy4.Err) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType)

Example 7 with ErrorType

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

the class ExprChoice method make.

// ============================================================================================================//
/**
 * Construct an ExprChoice node.
 */
public static Expr make(boolean ignoreIntFuns, Pos pos, ConstList<Expr> choices, ConstList<String> reasons) {
    if (choices.size() == 0)
        return new ExprBad(pos, "", new ErrorType(pos, "This expression failed to be typechecked."));
    if (choices.size() == 1 && choices.get(0).errors.isEmpty())
        // Shortcut
        return choices.get(0);
    if (ignoreIntFuns) {
        int n = choices.size();
        TempList<Expr> ch = new TempList<Expr>(n);
        TempList<String> rs = new TempList<String>(n);
        for (int i = 0; i < n; i++) {
            Expr e = choices.get(i);
            if (!((e instanceof ExprCall) && e.toString().startsWith("integer/"))) {
                ch.add(e);
                rs.add(reasons.get(i));
            }
        }
        return make(false, pos, ch.makeConst(), rs.makeConst());
    }
    Type type = EMPTY;
    boolean first = true;
    long weight = 0;
    for (Expr x : choices) {
        type = x.type.merge(type);
        if (first || weight > x.weight)
            if (x.type != EMPTY) {
                weight = x.weight;
                first = false;
            }
    }
    return new ExprChoice(pos, choices, reasons, type, weight);
}
Also used : ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList)

Example 8 with ErrorType

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

the class ExprChoice method resolveHelper.

// ============================================================================================================//
/**
 * Resolve the list of choices, or return an ExprBad object containing the list
 * of unresolvable ambiguities.
 */
private Expr resolveHelper(boolean firstPass, final Type t, List<Expr> choices, List<String> reasons, Collection<ErrorWarning> warns) {
    List<Expr> ch = new ArrayList<Expr>(choices.size());
    List<String> re = new ArrayList<String>(choices.size());
    // We first prefer exact matches
    for (int i = 0; i < choices.size(); i++) {
        Type tt = choices.get(i).type;
        if (/* [AM](t.is_int() && tt.is_int()) || */
        (t.is_bool && tt.is_bool) || t.intersects(tt)) {
            ch.add(choices.get(i));
            re.add(reasons.get(i));
        }
    }
    // If none, we try any legal matches
    if (ch.size() == 0) {
        for (int i = 0; i < choices.size(); i++) if (choices.get(i).type.hasCommonArity(t)) {
            ch.add(choices.get(i));
            re.add(reasons.get(i));
        }
    }
    // If too many, then keep the choices with the smallest weight
    if (ch.size() > 1) {
        List<Expr> ch2 = new ArrayList<Expr>(ch.size());
        List<String> re2 = new ArrayList<String>(ch.size());
        long w = 0;
        for (int i = 0; i < ch.size(); i++) {
            Expr c = ch.get(i);
            String r = re.get(i);
            if (ch2.size() > 0 && c.weight > w)
                continue;
            else if (ch2.size() == 0 || c.weight < w) {
                ch2.clear();
                re2.clear();
                w = c.weight;
            }
            ch2.add(c);
            re2.add(r);
        }
        ch = ch2;
        re = re2;
        // resolve them all and try again
        if (firstPass && ch.size() > 1) {
            ch2 = new ArrayList<Expr>(ch.size());
            for (Expr c : ch) ch2.add(c.resolve(t, null));
            return resolveHelper(false, t, ch2, re, warns);
        }
    }
    // If we are down to exactly 1 match, return it
    if (ch.size() == 1)
        return ch.get(0).resolve(t, warns);
    // emptyset-of-the-same-arity, then just return emptyset
    none: while (ch.size() > 1) {
        int arity = -1;
        for (Expr c : ch) {
            if (c.type.is_bool || c.type.is_int() || c.type.hasTuple())
                break none;
            int a = c.type.arity();
            if (a < 1)
                break none;
            if (arity < 0)
                arity = a;
            else if (arity != a)
                break none;
        }
        Expr ans = Sig.NONE;
        while (arity > 1) {
            ans = ans.product(Sig.NONE);
            arity--;
        }
        return ExprUnary.Op.NOOP.make(span(), ans);
    }
    // Otherwise, complain!
    String txt;
    if (ch.size() > 1) {
        txt = "\nThis name is ambiguous due to multiple matches:";
    } else {
        txt = "\nThis name cannot be resolved; its relevant type does not intersect with any of the following candidates:";
        re = reasons;
    }
    StringBuilder msg = new StringBuilder(txt);
    for (String r : re) msg.append('\n').append(r);
    return new ExprBad(pos, toString(), new ErrorType(pos, msg.toString()));
}
Also used : ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) ArrayList(java.util.ArrayList)

Example 9 with ErrorType

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

the class ExprList method make.

/**
 * Generates a call to a builtin predicate
 */
public static ExprList make(Pos pos, Pos closingBracket, Op op, List<? extends Expr> args) {
    boolean ambiguous = false;
    JoinableList<Err> errs = emptyListOfErrors;
    TempList<Expr> newargs = new TempList<Expr>(args.size());
    long weight = 0;
    Type commonArity = null;
    for (int i = 0; i < args.size(); i++) {
        Expr a = (op == Op.AND || op == Op.OR) ? args.get(i).typecheck_as_formula() : args.get(i).typecheck_as_set();
        ambiguous = ambiguous || a.ambiguous;
        weight = weight + a.weight;
        if (a.mult != 0)
            errs = errs.make(new ErrorSyntax(a.span(), "Multiplicity expression not allowed here."));
        if (!a.errors.isEmpty())
            errs = errs.make(a.errors);
        else if (commonArity == null)
            commonArity = a.type;
        else
            commonArity = commonArity.pickCommonArity(a.type);
        if (op == Op.AND)
            addAND(newargs, a);
        else if (op == Op.OR)
            addOR(newargs, a);
        else
            newargs.add(a);
    }
    if (op == Op.TOTALORDER) {
        if (newargs.size() != 3) {
            errs = errs.make(new ErrorSyntax(pos, "The builtin pred/totalOrder[] predicate must be called with exactly three arguments."));
        } else if (errs.isEmpty()) {
            if (!newargs.get(0).type.hasArity(1))
                errs = errs.make(new ErrorType(pos, "The first argument to pred/totalOrder must be unary."));
            if (!newargs.get(1).type.hasArity(1))
                errs = errs.make(new ErrorType(pos, "The second argument to pred/totalOrder must be unary."));
            if (!newargs.get(2).type.hasArity(2))
                errs = errs.make(new ErrorType(pos, "The third argument to pred/totalOrder must be binary."));
        }
    }
    if (op == Op.DISJOINT) {
        if (newargs.size() < 2)
            errs = errs.make(new ErrorSyntax(pos, "The builtin disjoint[] predicate must be called with at least two arguments."));
        if (commonArity == EMPTY)
            errs = errs.make(new ErrorType(pos, "The builtin predicate disjoint[] cannot be used among expressions of different arities."));
    }
    return new ExprList(pos, closingBracket, op, ambiguous, newargs.makeConst(), weight, errs);
}
Also used : ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Err(edu.mit.csail.sdg.alloy4.Err) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType)

Example 10 with ErrorType

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

Aggregations

ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)18 Err (edu.mit.csail.sdg.alloy4.Err)6 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)6 Pos (edu.mit.csail.sdg.alloy4.Pos)5 ArrayList (java.util.ArrayList)5 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)4 CapacityExceededException (kodkod.engine.CapacityExceededException)4 HigherOrderDeclException (kodkod.engine.fol2sat.HigherOrderDeclException)4 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)3 Expr (edu.mit.csail.sdg.ast.Expr)3 Sig (edu.mit.csail.sdg.ast.Sig)3 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)2 Env (edu.mit.csail.sdg.alloy4.Env)2 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)2 Command (edu.mit.csail.sdg.ast.Command)2 ExprVar (edu.mit.csail.sdg.ast.ExprVar)2 Func (edu.mit.csail.sdg.ast.Func)2 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 LinkedHashMap (java.util.LinkedHashMap)2