use of edu.mit.csail.sdg.alloy4.ConstList.TempList in project org.alloytools.alloy by AlloyTools.
the class CompModule method resolveCommand.
/**
* Resolve a particular command.
*/
private Command resolveCommand(Command cmd, ConstList<Sig> exactSigs, Expr globalFacts) throws Err {
Command parent = cmd.parent == null ? null : resolveCommand(cmd.parent, exactSigs, globalFacts);
String cname = ((ExprVar) (cmd.formula)).label;
Expr e;
Clause declaringClause = null;
if (cmd.check) {
// We prefer assertion in the
List<Object> m = getRawQS(2, cname);
// topmost module
if (m.size() == 0 && cname.indexOf('/') < 0)
m = getRawNQS(this, 2, cname);
if (m.size() > 1)
unique(cmd.pos, cname, m);
if (m.size() < 1)
throw new ErrorSyntax(cmd.pos, "The assertion \"" + cname + "\" cannot be found.");
Expr expr = (Expr) (m.get(0));
e = expr.not();
} else {
// We prefer fun/pred in the
List<Object> m = getRawQS(4, cname);
// topmost module
if (m.size() == 0 && cname.indexOf('/') < 0)
m = getRawNQS(this, 4, cname);
if (m.size() > 1)
unique(cmd.pos, cname, m);
if (m.size() < 1)
throw new ErrorSyntax(cmd.pos, "The predicate/function \"" + cname + "\" cannot be found.");
Func f = (Func) (m.get(0));
declaringClause = f;
e = f.getBody();
if (!f.isPred)
e = e.in(f.returnDecl);
if (f.decls.size() > 0)
e = ExprQt.Op.SOME.make(null, null, f.decls, e);
}
if (e == null)
e = ExprConstant.TRUE;
TempList<CommandScope> sc = new TempList<CommandScope>(cmd.scope.size());
for (CommandScope et : cmd.scope) {
Sig s = getRawSIG(et.sig.pos, et.sig.label);
if (s == null)
throw new ErrorSyntax(et.sig.pos, "The sig \"" + et.sig.label + "\" cannot be found.");
sc.add(new CommandScope(null, s, et.isExact, et.startingScope, et.endingScope, et.increment));
}
if (cmd.nameExpr != null) {
cmd.nameExpr.setReferenced(declaringClause);
}
return new Command(cmd.pos, cmd.nameExpr, cmd.label, cmd.check, cmd.overall, cmd.bitwidth, cmd.maxseq, cmd.expects, sc.makeConst(), exactSigs, globalFacts.and(e), parent);
}
use of edu.mit.csail.sdg.alloy4.ConstList.TempList in project org.alloytools.alloy by AlloyTools.
the class CompModule method addOpen.
/**
* Add an OPEN declaration.
*/
void addOpen(Pos pos, Pos isPrivate, ExprVar name, List<ExprVar> args, ExprVar alias) throws Err {
if (status > 2)
throw new ErrorSyntax(pos, "The \"open\" declaration must occur before any\n" + "sig/pred/fun/fact/assert/check/run command.");
String as = (alias == null ? "" : alias.label);
if (name.label.length() == 0)
throw new ErrorSyntax(name.span(), "The filename cannot be empty.");
if (as.indexOf('$') >= 0)
throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'$\' character");
if (as.indexOf('@') >= 0)
throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'@\' character");
if (as.indexOf('/') >= 0)
throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'/\' character");
if (as.length() == 0) {
as = "open$" + (1 + opens.size());
if (args == null || args.size() == 0) {
for (int i = 0; ; i++) {
if (i >= name.label.length()) {
as = name.label;
break;
}
char c = name.label.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
continue;
if (i == 0)
break;
if (!(c >= '0' && c <= '9') && c != '_' && c != '\'' && c != '\"')
break;
}
}
}
final TempList<String> newlist = new TempList<String>(args == null ? 0 : args.size());
if (args != null)
for (int i = 0; i < args.size(); i++) {
ExprVar arg = args.get(i);
if (arg.label.length() == 0)
throw new ErrorSyntax(arg.span(), "Argument cannot be empty.");
if (arg.label.indexOf('@') >= 0)
throw new ErrorSyntax(arg.span(), "Argument cannot contain the \'@\' chracter.");
newlist.add(arg.label);
}
Open x = opens.get(as);
if (x != null) {
// we allow this, especially because of util/sequniv
if (x.args.equals(newlist.makeConst()) && x.filename.equals(name.label))
return;
throw new ErrorSyntax(pos, "You cannot import two different modules\n" + "using the same alias.");
}
List<Expr> expressions = new ArrayList<>(args == null ? Collections.emptySet() : args);
expressions.add(0, name);
expressions.add(alias);
x = new Open(pos, isPrivate != null, as, newlist.makeConst(), name.label, expressions);
opens.put(as, x);
}
use of edu.mit.csail.sdg.alloy4.ConstList.TempList 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);
}
use of edu.mit.csail.sdg.alloy4.ConstList.TempList 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);
}
use of edu.mit.csail.sdg.alloy4.ConstList.TempList 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);
}
Aggregations