use of edu.mit.csail.sdg.alloy4.Pos 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.Pos 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()));
}
use of edu.mit.csail.sdg.alloy4.Pos in project org.alloytools.alloy by AlloyTools.
the class ExprITE method resolve.
/**
* {@inheritDoc}
*/
@Override
public Expr resolve(Type p, Collection<ErrorWarning> warns) {
if (errors.size() > 0)
return this;
Type a = left.type, b = right.type;
if (p.size() > 0) {
a = a.intersect(p);
b = b.intersect(p);
// if (p.is_int()) { a=Type.makeInt(a); b=Type.makeInt(b); }
if (p.is_bool) {
a = Type.makeBool(a);
b = Type.makeBool(b);
}
if (warns != null && left.type.hasTuple() && !a.hasTuple())
warns.add(new ErrorWarning(left.span(), "This subexpression is redundant."));
if (warns != null && right.type.hasTuple() && !b.hasTuple())
warns.add(new ErrorWarning(right.span(), "This subexpression is redundant."));
} else {
a = p;
b = p;
}
Expr cond = this.cond.resolve(Type.FORMULA, warns);
Expr left = this.left.resolve(a, warns);
Expr right = this.right.resolve(b, warns);
return (cond == this.cond && left == this.left && right == this.right) ? this : make(pos, cond, left, right);
}
use of edu.mit.csail.sdg.alloy4.Pos 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.Pos in project org.alloytools.alloy by AlloyTools.
the class SimpleGUI method doVisualize.
/**
* This method displays a particular instance or message.
*/
@SuppressWarnings("unchecked")
Runner doVisualize(String arg) {
if (wrap)
return wrapMe(arg);
text.clearShade();
if (arg.startsWith("MSG: ")) {
// MSG: message
OurDialog.showtext("Detailed Message", arg.substring(5));
}
if (arg.startsWith("CORE: ")) {
// CORE: filename
String filename = Util.canon(arg.substring(6));
Pair<Set<Pos>, Set<Pos>> hCore;
// Set<Pos> lCore;
InputStream is = null;
ObjectInputStream ois = null;
try {
is = new FileInputStream(filename);
ois = new ObjectInputStream(is);
hCore = (Pair<Set<Pos>, Set<Pos>>) ois.readObject();
// lCore = (Set<Pos>) ois.readObject();
} catch (Throwable ex) {
log.logRed("Error reading or parsing the core \"" + filename + "\"\n");
return null;
} finally {
Util.close(ois);
Util.close(is);
}
text.clearShade();
text.shade(hCore.b, subCoreColor, false);
text.shade(hCore.a, coreColor, false);
// shade again, because if not all files were open, some shadings
// will have no effect
text.shade(hCore.b, subCoreColor, false);
text.shade(hCore.a, coreColor, false);
}
if (arg.startsWith("POS: ")) {
// POS: x1 y1 x2 y2 filename
Scanner s = new Scanner(arg.substring(5));
int x1 = s.nextInt(), y1 = s.nextInt(), x2 = s.nextInt(), y2 = s.nextInt();
String f = s.nextLine();
if (f.length() > 0 && f.charAt(0) == ' ')
// Get rid of the space after Y2
f = f.substring(1);
Pos p = new Pos(Util.canon(f), x1, y1, x2, y2);
text.shade(p);
}
if (arg.startsWith("CNF: ")) {
// CNF: filename
String filename = Util.canon(arg.substring(5));
try {
String text = Util.readAll(filename);
OurDialog.showtext("Text Viewer", text);
} catch (IOException ex) {
log.logRed("Error reading the file \"" + filename + "\"\n");
}
}
if (arg.startsWith("XML: ")) {
// XML: filename
viz.loadXML(Util.canon(arg.substring(5)), false);
}
return null;
}
Aggregations