use of edu.mit.csail.sdg.alloy4.ErrorSyntax 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.ErrorSyntax 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);
}
use of edu.mit.csail.sdg.alloy4.ErrorSyntax 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);
}
}
}
}
}
use of edu.mit.csail.sdg.alloy4.ErrorSyntax in project org.alloytools.alloy by AlloyTools.
the class CompModule method addFunc.
/**
* Add a FUN or PRED declaration.
*/
void addFunc(Pos p, Pos isPrivate, String n, Expr f, List<Decl> decls, Expr t, Expr v) throws Err {
if (decls == null)
decls = new ArrayList<Decl>();
else
decls = new ArrayList<Decl>(decls);
if (f != null)
decls.add(0, new Decl(null, null, null, Util.asList(ExprVar.make(f.span(), "this")), f));
for (Decl d : decls) {
if (d.isPrivate != null) {
ExprHasName name = d.names.get(0);
throw new ErrorSyntax(d.isPrivate.merge(name.pos), "Function parameter \"" + name.label + "\" is always private already.");
}
if (d.disjoint2 != null) {
ExprHasName name = d.names.get(d.names.size() - 1);
throw new ErrorSyntax(d.disjoint2.merge(name.pos), "Function parameter \"" + name.label + "\" cannot be bound to a 'disjoint' expression.");
}
}
status = 3;
dup(p, n, false);
ExprHasName dup = Decl.findDuplicateName(decls);
if (dup != null)
throw new ErrorSyntax(dup.span(), "The parameter name \"" + dup.label + "\" cannot appear more than once.");
Func ans = new Func(p, isPrivate, n, decls, t, v);
ArrayList<Func> list = funcs.get(n);
if (list == null) {
list = new ArrayList<Func>();
funcs.put(n, list);
}
list.add(ans);
}
use of edu.mit.csail.sdg.alloy4.ErrorSyntax in project org.alloytools.alloy by AlloyTools.
the class CompModule method resolveParams.
/**
* Every param in every module will now point to a nonnull Sig.
*/
private static void resolveParams(A4Reporter rep, List<CompModule> modules) throws Err {
while (true) {
boolean chg = false;
Open missing = null;
String missingName = "";
for (CompModule mod : modules) for (Open open : mod.opens.values()) {
CompModule sub = open.realModule;
if (open.args.size() != sub.params.size())
throw new ErrorSyntax(open.pos, "You supplied " + open.args.size() + " arguments to the open statement, but the imported module requires " + sub.params.size() + " arguments.");
int i = 0;
for (Map.Entry<String, Sig> p : sub.params.entrySet()) {
Sig old = p.getValue();
String kn = p.getKey(), vn = open.args.get(i);
i++;
Sig vv = mod.getRawSIG(open.pos, vn);
if (vv == null) {
if (old == null) {
missing = open;
missingName = vn;
}
continue;
}
if (old == vv)
continue;
if (old != null)
throw new ErrorFatal(open.pos, "Internal error (module re-instantiated with different arguments)");
if (vv == NONE)
throw new ErrorSyntax(open.pos, "You cannot use \"none\" as an instantiating argument.");
chg = true;
p.setValue(vv);
rep.parse("RESOLVE: " + (sub.path.length() == 0 ? "this/" : sub.path) + "/" + kn + " := " + vv + "\n");
}
}
if (!chg && missing == null)
return;
if (!chg)
throw new ErrorSyntax(missing.pos, "The signature name \"" + missingName + "\" cannot be found.");
}
}
Aggregations