Search in sources :

Example 56 with Err

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

Example 57 with Err

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

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprQt x) throws Err {
    Expr xx = x.desugar();
    if (xx instanceof ExprQt)
        x = (ExprQt) xx;
    else
        return visitThis(xx);
    if (x.op == ExprQt.Op.COMPREHENSION) {
        TempList<SimTuple> ans = new TempList<SimTuple>();
        enumerate(ans, 0, x, x.sub, 0);
        return SimTupleset.make(ans.makeConst());
    }
    if (x.op == ExprQt.Op.ALL)
        return enumerate(null, 0, x, x.sub.not(), 0) == 0;
    if (x.op == ExprQt.Op.NO)
        return enumerate(null, 0, x, x.sub, 0) == 0;
    if (x.op == ExprQt.Op.SOME)
        return enumerate(null, 0, x, x.sub, 0) >= 1;
    if (x.op == ExprQt.Op.LONE)
        return enumerate(null, 0, x, x.sub, 0) <= 1;
    if (x.op == ExprQt.Op.ONE)
        return enumerate(null, 0, x, x.sub, 0) == 1;
    if (x.op == ExprQt.Op.SUM)
        return trunc(enumerate(null, 0, x, x.sub, 0));
    throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprQt.accept()");
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) ExprQt(edu.mit.csail.sdg.ast.ExprQt)

Example 58 with Err

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

the class SimInstance method init.

/**
 * Initializes the given var to be associated with the given unary value; should
 * only be called at the beginning.
 * <p>
 * The resulting instance may or may not satisfy all facts, and should be
 * checked for consistency.
 */
public void init(ExprVar var, SimTupleset value) throws Err {
    if (value == null) {
        sfs.remove(var);
        return;
    }
    if (!value.empty() && value.arity() != var.type().arity())
        throw new ErrorType("Evaluator encountered an error: skolem " + var.label + " arity must not be " + value.arity());
    sfs.put(var, value);
    cacheUNIV = null;
    cacheSTRING = null;
    cacheForConstants.clear();
}
Also used : ErrorType(edu.mit.csail.sdg.alloy4.ErrorType)

Example 59 with Err

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

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public SimTupleset visit(Field x) throws Err {
    if (x.defined) {
        final ExprVar v = (ExprVar) (x.sig.decl.get());
        final Expr b = x.decl().expr;
        final Env<ExprVar, Object> oldenv = env;
        env = new Env<ExprVar, Object>();
        if (!b.hasVar(v)) {
            SimTupleset ans = cset(x.sig).product(cset(b));
            env = oldenv;
            return ans;
        }
        SimTupleset ans = SimTupleset.EMPTY;
        for (SimTuple a : visit(x.sig)) {
            SimTupleset left = SimTupleset.make(a);
            env.put(v, left);
            SimTupleset right = cset(b);
            env.remove(v);
            ans = left.product(right).union(ans);
        }
        env = oldenv;
        return ans;
    }
    Object ans = sfs.get(x);
    if (ans instanceof SimTupleset)
        return (SimTupleset) ans;
    else
        throw new ErrorFatal("Unknown field " + x + " encountered during evaluation.");
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr)

Example 60 with Err

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

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public SimTupleset visit(Sig x) throws Err {
    if (x.isSame(Sig.NONE))
        return SimTupleset.EMPTY;
    if (x.isSame(Sig.SEQIDX))
        return SimTupleset.make(0, maxseq - 1);
    if (x.isSame(Sig.SIGINT))
        return SimTupleset.make(min, max);
    if (x.isSame(Sig.STRING)) {
        if (cacheSTRING == null) {
            cacheSTRING = SimTupleset.EMPTY;
            for (Map.Entry<Expr, SimTupleset> e : sfs.entrySet()) if (e.getKey() instanceof Field || e.getKey() instanceof ExprVar) {
                for (SimTuple t : e.getValue()) for (int i = t.arity() - 1; i >= 0; i--) {
                    String a = t.get(i).toString();
                    if (a.length() > 0 && a.charAt(0) == '"')
                        cacheSTRING = cacheSTRING.union(SimTuple.make(t.get(i)));
                }
            }
        }
        return cacheSTRING;
    }
    if (x == Sig.UNIV) {
        if (cacheUNIV == null) {
            cacheUNIV = SimTupleset.make(min, max);
            for (Map.Entry<Expr, SimTupleset> e : sfs.entrySet()) if (e.getKey() instanceof PrimSig && ((PrimSig) (e.getKey())).isTopLevel())
                cacheUNIV = cacheUNIV.union(e.getValue());
            cacheUNIV = cacheUNIV.union(visit(Sig.STRING));
        }
        return cacheUNIV;
    }
    Object ans = sfs.get(x);
    if (ans instanceof SimTupleset)
        return (SimTupleset) ans;
    else
        throw new ErrorFatal("Unknown sig " + x + " encountered during evaluation.");
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Field(edu.mit.csail.sdg.ast.Sig.Field) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Aggregations

ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)32 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)28 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)27 Err (edu.mit.csail.sdg.alloy4.Err)25 Expr (edu.mit.csail.sdg.ast.Expr)25 Sig (edu.mit.csail.sdg.ast.Sig)24 Pos (edu.mit.csail.sdg.alloy4.Pos)16 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)15 ExprVar (edu.mit.csail.sdg.ast.ExprVar)15 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)15 ArrayList (java.util.ArrayList)15 Field (edu.mit.csail.sdg.ast.Sig.Field)12 LinkedHashMap (java.util.LinkedHashMap)10 IOException (java.io.IOException)9 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)8 Func (edu.mit.csail.sdg.ast.Func)8 TupleSet (kodkod.instance.TupleSet)8 Command (edu.mit.csail.sdg.ast.Command)7 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)5