use of edu.mit.csail.sdg.ast.Decl in project org.alloytools.alloy by AlloyTools.
the class SimpleCLI method main.
public static void main(String[] args) throws Exception {
final boolean sat4j = "yes".equals(System.getProperty("sat4j"));
final boolean minisat = "yes".equals(System.getProperty("minisat"));
SatSolver solver = A4Options.SatSolver.make("mem", "mem", "/zweb/sat/mem");
final SimpleReporter rep = new SimpleReporter();
final StringBuilder sb = rep.sb;
for (String filename : args) {
try {
// Parse+Typecheck
rep.sb.append("\n\nMain file = " + filename + "\n");
if (db)
db("Parsing+Typechecking...");
Module world = CompUtil.parseEverything_fromFile(rep, null, filename);
if (db)
db(" ok\n");
List<Command> cmds = world.getAllCommands();
for (ErrorWarning msg : rep.warnings) rep.sb.append("Relevance Warning:\n" + (msg.toString().trim()) + "\n\n");
rep.warnings.clear();
// Do a detailed dump if we will not be executing the commands
if (args.length != 1) {
for (Module m : world.getAllReachableModules()) {
for (Sig x : m.getAllSigs()) {
sb.append("\nSig ").append(x.label).append(" at position ").append(x.pos).append("\n");
for (Decl d : x.getFieldDecls()) for (ExprHasName f : d.names) {
sb.append("\nField ").append(f.label).append(" with type ").append(f.type()).append("\n");
d.expr.toString(sb, 2);
}
rep.flush();
}
for (Func x : m.getAllFunc()) {
sb.append("\nFun/pred ").append(x.label).append(" at position ").append(x.pos).append("\n");
for (Decl d : x.decls) for (ExprHasName v : d.names) {
v.toString(sb, 2);
d.expr.toString(sb, 4);
}
x.returnDecl.toString(sb, 2);
x.getBody().toString(sb, 4);
rep.flush();
}
for (Pair<String, Expr> x : m.getAllFacts()) {
sb.append("\nFact ").append(x.a).append("\n");
x.b.toString(sb, 4);
rep.flush();
}
for (Pair<String, Expr> x : m.getAllAssertions()) {
sb.append("\nAssertion ").append(x.a).append("\n");
x.b.toString(sb, 4);
rep.flush();
}
if (m == world)
for (Command x : m.getAllCommands()) {
sb.append("\nCommand ").append(x.label).append("\n");
x.formula.toString(sb, 4);
rep.flush();
}
}
continue;
}
// Validate the metamodel generation code
StringWriter metasb = new StringWriter();
PrintWriter meta = new PrintWriter(metasb);
Util.encodeXMLs(meta, "\n<alloy builddate=\"", Version.buildDate(), "\">\n\n");
A4SolutionWriter.writeMetamodel(world.getAllReachableSigs(), filename, meta);
Util.encodeXMLs(meta, "\n</alloy>");
meta.flush();
metasb.flush();
String metaxml = metasb.toString();
A4SolutionReader.read(new ArrayList<Sig>(), new XMLNode(new StringReader(metaxml)));
StaticInstanceReader.parseInstance(new StringReader(metaxml));
// Okay, now solve the commands
A4Options options = new A4Options();
options.originalFilename = filename;
options.solverDirectory = "/zweb/zweb/tmp/alloy4/x86-freebsd";
options.solver = sat4j ? A4Options.SatSolver.SAT4J : (minisat ? A4Options.SatSolver.MiniSatJNI : solver);
for (int i = 0; i < cmds.size(); i++) {
Command c = cmds.get(i);
if (db) {
String cc = c.toString();
if (cc.length() > 60)
cc = cc.substring(0, 55);
db("Executing " + cc + "...\n");
}
rep.sb.append("Executing \"" + c + "\"\n");
options.skolemDepth = 0;
A4Solution s = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), c, options);
if (s.satisfiable()) {
validate(s);
if (s.isIncremental()) {
s = s.next();
if (s.satisfiable())
validate(s);
}
}
options.skolemDepth = 2;
s = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), c, options);
if (s.satisfiable()) {
validate(s);
if (s.isIncremental()) {
s = s.next();
if (s.satisfiable())
validate(s);
}
}
}
} catch (Throwable ex) {
rep.sb.append("\n\nException: " + ex);
}
if (db) {
if (args.length != 1)
db(" ERROR!\n");
else
db("\n\n");
}
}
rep.close();
}
use of edu.mit.csail.sdg.ast.Decl 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.Decl 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.Decl in project org.alloytools.alloy by AlloyTools.
the class CompModule method addDefaultCommand.
public void addDefaultCommand() {
if (commands.isEmpty()) {
addFunc(Pos.UNKNOWN, Pos.UNKNOWN, "$$Default", null, new ArrayList<Decl>(), null, ExprConstant.TRUE);
commands.add(new Command(Pos.UNKNOWN, ExprConstant.TRUE, "Default", false, 4, 4, 4, 0, null, null, ExprVar.make(null, "$$Default"), null));
}
}
use of edu.mit.csail.sdg.ast.Decl 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);
}
}
Aggregations