use of edu.mit.csail.sdg.ast.ExprHasName in project org.alloytools.alloy by AlloyTools.
the class VizTree method convertValueToText.
/**
* {@inheritDoc}
*/
@Override
public String convertValueToText(Object val, boolean selected, boolean expanded, boolean leaf, int row, boolean focus) {
String c = ">";
if (onWindows)
c = selected ? " style=\"color:#ffffff;\">" : " style=\"color:#000000;\">";
if (val instanceof A4Solution)
return "<html> <b" + c + encode(title == null ? "" : title) + "</b></html>";
if (val instanceof Sig) {
String label = ((Sig) val).label;
if (label.startsWith("this/"))
label = label.substring(5);
return "<html> <b" + c + "sig " + encode(label) + "</b></html>";
}
if (val instanceof ExprVar)
return "<html> <b" + c + "set " + encode(((ExprVar) val).label) + "</b></html>";
if (val instanceof String)
return "<html> <span" + c + encode((String) val) + "</span></html>";
if (val instanceof Pair)
return "<html> <b" + c + "field " + encode(((ExprHasName) (((Pair<?, ?>) val).b)).label) + "</b></html>";
if (val instanceof A4Tuple) {
StringBuilder sb = new StringBuilder("<html> <span" + c);
A4Tuple tp = (A4Tuple) val;
for (int i = 1; i < tp.arity(); i++) {
if (i > 1)
sb.append(" -> ");
sb.append(encode(tp.atom(i)));
}
sb.append("</span></html>");
return sb.toString();
}
return "";
}
use of edu.mit.csail.sdg.ast.ExprHasName 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.ast.ExprHasName in project org.alloytools.alloy by AlloyTools.
the class CompModule method resolveFuncDecls.
/**
* Each FunAST will now point to a bodyless Func object.
*/
private JoinableList<Err> resolveFuncDecls(A4Reporter rep, JoinableList<Err> errors, List<ErrorWarning> warns) throws Err {
for (ArrayList<Func> list : funcs.values()) {
for (int listi = 0; listi < list.size(); listi++) {
Func f = list.get(listi);
String fullname = (path.length() == 0 ? "this/" : (path + "/")) + f.label;
// Each PARAMETER can refer to earlier parameter in the same
// function, and any SIG or FIELD visible from here.
// Each RETURNTYPE can refer to the parameters of the same
// function, and any SIG or FIELD visible from here.
Context cx = new Context(this, warns);
cx.rootfunparam = true;
TempList<Decl> tmpdecls = new TempList<Decl>();
boolean err = false;
for (Decl d : f.decls) {
TempList<ExprVar> tmpvars = new TempList<ExprVar>();
Expr val = cx.check(d.expr).resolve_as_set(warns);
if (!val.errors.isEmpty()) {
err = true;
errors = errors.make(val.errors);
}
for (ExprHasName n : d.names) {
ExprVar v = ExprVar.make(n.span(), n.label, val.type());
cx.put(n.label, v);
tmpvars.add(v);
rep.typecheck((f.isPred ? "pred " : "fun ") + fullname + ", Param " + n.label + ": " + v.type() + "\n");
}
tmpdecls.add(new Decl(d.isPrivate, d.disjoint, d.disjoint2, tmpvars.makeConst(), val));
}
Expr ret = null;
if (!f.isPred) {
ret = cx.check(f.returnDecl).resolve_as_set(warns);
if (!ret.errors.isEmpty()) {
err = true;
errors = errors.make(ret.errors);
}
}
if (err)
continue;
try {
f = new Func(f.pos, f.isPrivate, fullname, tmpdecls.makeConst(), ret, f.getBody());
list.set(listi, f);
rep.typecheck("" + f + ", RETURN: " + f.returnDecl.type() + "\n");
} catch (Err ex) {
errors = errors.make(ex);
}
}
}
return errors;
}
use of edu.mit.csail.sdg.ast.ExprHasName in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method visit_qt.
/**
* Helper method that translates the quantification expression "op vars | sub"
*/
private Object visit_qt(final ExprQt.Op op, final ConstList<Decl> xvars, final Expr sub) throws Err {
if (op == ExprQt.Op.NO) {
return visit_qt(ExprQt.Op.ALL, xvars, sub.not());
}
if (op == ExprQt.Op.ONE || op == ExprQt.Op.LONE) {
boolean ok = true;
for (int i = 0; i < xvars.size(); i++) {
Expr v = addOne(xvars.get(i).expr).deNOP();
if (v.type().arity() != 1 || v.mult() != ExprUnary.Op.ONEOF) {
ok = false;
break;
}
}
if (op == ExprQt.Op.ONE && ok)
return ((Expression) visit_qt(ExprQt.Op.COMPREHENSION, xvars, sub)).one();
if (op == ExprQt.Op.LONE && ok)
return ((Expression) visit_qt(ExprQt.Op.COMPREHENSION, xvars, sub)).lone();
}
if (op == ExprQt.Op.ONE) {
Formula f1 = (Formula) visit_qt(ExprQt.Op.LONE, xvars, sub);
Formula f2 = (Formula) visit_qt(ExprQt.Op.SOME, xvars, sub);
return f1.and(f2);
}
if (op == ExprQt.Op.LONE) {
QuantifiedFormula p1 = (QuantifiedFormula) visit_qt(ExprQt.Op.ALL, xvars, sub);
QuantifiedFormula p2 = (QuantifiedFormula) visit_qt(ExprQt.Op.ALL, xvars, sub);
Decls s1 = p1.decls(), s2 = p2.decls(), decls = null;
Formula f1 = p1.formula(), f2 = p2.formula();
Formula[] conjuncts = new Formula[s1.size()];
for (int i = 0; i < conjuncts.length; i++) {
kodkod.ast.Decl d1 = s1.get(i), d2 = s2.get(i);
conjuncts[i] = d1.variable().eq(d2.variable());
if (decls == null)
decls = d1.and(d2);
else
decls = decls.and(d1).and(d2);
}
return f1.and(f2).implies(Formula.and(conjuncts)).forAll(decls);
}
Decls dd = null;
List<Formula> guards = new ArrayList<Formula>();
for (Decl dep : xvars) {
final Expr dexexpr = addOne(dep.expr);
final Expression dv = cset(dexexpr);
for (ExprHasName dex : dep.names) {
final Variable v = Variable.nary(skolem(dex.label), dex.type().arity());
final kodkod.ast.Decl newd;
env.put((ExprVar) dex, v);
if (dex.type().arity() != 1) {
guards.add(isIn(v, dexexpr));
newd = v.setOf(dv);
} else
switch(dexexpr.mult()) {
case SETOF:
newd = v.setOf(dv);
break;
case SOMEOF:
newd = v.someOf(dv);
break;
case LONEOF:
newd = v.loneOf(dv);
break;
default:
newd = v.oneOf(dv);
}
if (frame != null)
frame.kv2typepos(v, dex.type(), dex.pos);
if (dd == null)
dd = newd;
else
dd = dd.and(newd);
}
}
final Formula ans = (op == ExprQt.Op.SUM) ? null : cform(sub);
final IntExpression ians = (op != ExprQt.Op.SUM) ? null : cint(sub);
for (Decl d : xvars) for (ExprHasName v : d.names) env.remove((ExprVar) v);
if (op == ExprQt.Op.COMPREHENSION)
// guards.size()==0, since each var
return ans.comprehension(dd);
// has to be unary
if (op == ExprQt.Op.SUM)
// guards.size()==0, since each var has to be
return ians.sum(dd);
// unary
if (op == ExprQt.Op.SOME) {
if (guards.size() == 0)
return ans.forSome(dd);
guards.add(ans);
return Formula.and(guards).forSome(dd);
} else {
if (guards.size() == 0)
return ans.forAll(dd);
return Formula.and(guards).implies(ans).forAll(dd);
}
}
use of edu.mit.csail.sdg.ast.ExprHasName in project org.alloytools.alloy by AlloyTools.
the class DemoFileSystem method acyclic.
/*
* These corresponds to the helper predicates/functions provided in util/*.als
*/
static Expr acyclic(Expr r) throws Err {
// x is a variable over the domain
Decl d = r.join(Sig.UNIV).oneOf("x");
// of r
ExprHasName x = d.get();
// (x !in x.^r) for
return x.in(x.join(r.closure())).not().forAll(d);
// all x
}
Aggregations