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);
}
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()");
}
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();
}
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.");
}
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.");
}
Aggregations