Search in sources :

Example 1 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.

the class CompModule method addModelName.

/**
 * Add the "MODULE" declaration.
 */
void addModelName(Pos pos, String moduleName, List<ExprVar> list) throws Err {
    if (status > 0)
        throw new ErrorSyntax(pos, "The \"module\" declaration must occur at the top,\n" + "and can occur at most once.");
    this.moduleName = moduleName;
    this.modulePos = pos;
    boolean nextIsExact = false;
    if (list != null)
        for (ExprVar expr : list) {
            if (expr == null) {
                nextIsExact = true;
                continue;
            }
            String name = expr.label;
            dup(expr.span(), name, true);
            if (path.length() == 0) {
                Sig newSig = addSig(name, null, null, null, null, WHERE.make(expr.span()));
                if (nextIsExact)
                    exactSigs.add(newSig);
            } else {
                params.put(name, null);
                if (nextIsExact)
                    exactParams.add(name);
            }
            nextIsExact = false;
        }
    // This line must be at the end, since "addSig" will
    this.status = 1;
// otherwise bump the status value to 3
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Example 2 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar 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);
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Func(edu.mit.csail.sdg.ast.Func) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) Command(edu.mit.csail.sdg.ast.Command) Clause(edu.mit.csail.sdg.ast.Clause) CommandScope(edu.mit.csail.sdg.ast.CommandScope)

Example 3 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.

the class SimInstance method enumerate.

/**
 * Helper method for enumerating all possibilties for a
 * quantification-expression.
 */
private int enumerate(final TempList<SimTuple> store, int sum, final ExprQt x, final Expr body, final int i) throws Err {
    // if op is ALL NO SOME ONE LONE then it always returns
    // 0 1 2
    final int n = x.count();
    final ExprVar v = x.get(i);
    final Expr bound = x.getBound(i);
    final SimTupleset e = cset(bound);
    final Iterator<SimTupleset> it;
    switch(bound.mult()) {
        case LONEOF:
            it = e.loneOf();
            break;
        case ONEOF:
            it = e.oneOf();
            break;
        case SOMEOF:
            it = e.someOf();
            break;
        default:
            it = e.setOf();
    }
    while (it.hasNext()) {
        final SimTupleset binding = it.next();
        if (bound.mult == 2 && !isIn(binding, bound))
            continue;
        env.put(v, binding);
        if (i < n - 1)
            sum = enumerate(store, sum, x, body, i + 1);
        else if (x.op == ExprQt.Op.SUM)
            sum += cint(body);
        else if (x.op != ExprQt.Op.COMPREHENSION)
            sum += cform(body) ? 1 : 0;
        else if (cform(body)) {
            SimTuple a = null, b;
            for (int j = 0; j < n; j++) {
                b = ((SimTupleset) (env.get(x.get(j)))).getTuple();
                if (a == null)
                    a = b;
                else
                    a = a.product(b);
            }
            store.add(a);
        }
        env.remove(v);
        if (sum >= 2 && x.op != ExprQt.Op.COMPREHENSION && x.op != ExprQt.Op.SUM)
            // no need to enumerate further
            return 2;
    }
    return sum;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Expr(edu.mit.csail.sdg.ast.Expr)

Example 4 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprCall x) throws Err {
    final Func f = x.fun;
    final int n = f.count();
    final Object candidate = n == 0 ? cacheForConstants.get(f) : null;
    if (candidate != null)
        return candidate;
    final Expr body = f.getBody();
    if (body.type().arity() < 0 || body.type().arity() != f.returnDecl.type().arity())
        throw new ErrorType(body.span(), "Function return value not fully resolved.");
    for (Func ff : current_function) if (ff == f)
        throw new ErrorSyntax(x.span(), "" + f + " cannot call itself recursively!");
    Env<ExprVar, Object> newenv = new Env<ExprVar, Object>();
    List<SimTupleset> list = new ArrayList<SimTupleset>(x.args.size());
    for (int i = 0; i < n; i++) {
        SimTupleset ts = cset(x.args.get(i));
        newenv.put(f.get(i), ts);
        list.add(ts);
    }
    final SimCallback cb = callbacks.get(f);
    if (cb != null) {
        try {
            Object answer = cb.compute(f, list);
            if (answer != null) {
                if (x.args.size() == 0)
                    cacheForConstants.put(f, answer);
                return answer;
            }
        } catch (Exception ex) {
        // if the callback failed, we can just continue with our
        // original attempt to evaluate this call
        }
    }
    Env<ExprVar, Object> oldenv = env;
    env = newenv;
    current_function.add(f);
    Object ans = visitThis(body);
    env = oldenv;
    current_function.remove(current_function.size() - 1);
    if (f.count() == 0)
        cacheForConstants.put(f, ans);
    return ans;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Func(edu.mit.csail.sdg.ast.Func) ArrayList(java.util.ArrayList) Env(edu.mit.csail.sdg.alloy4.Env) IOException(java.io.IOException) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType)

Example 5 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.

the class A4Solution method addSkolem.

/**
 * Add a new skolem to this solution and associate it with the given expression.
 * <br>
 * The expression must contain only constant Relations or Relations that are
 * already bound in this solution.
 */
private ExprVar addSkolem(String label, Type type, Expression expr) throws Err {
    if (solved)
        throw new ErrorFatal("Cannot add an additional skolem since solve() has completed.");
    int a = type.arity();
    if (a < 1)
        throw new ErrorFatal("Skolem " + label + " must be associated with a relational value.");
    if (a != expr.arity())
        throw new ErrorFatal("Skolem " + label + " must be associated with an " + a + "-ary relational value.");
    ExprVar v = ExprVar.make(Pos.UNKNOWN, label, type);
    a2k.put(v, expr);
    skolems.add(v);
    return v;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal)

Aggregations

ExprVar (edu.mit.csail.sdg.ast.ExprVar)22 Expr (edu.mit.csail.sdg.ast.Expr)11 Sig (edu.mit.csail.sdg.ast.Sig)11 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)8 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)7 Field (edu.mit.csail.sdg.ast.Sig.Field)7 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)5 Func (edu.mit.csail.sdg.ast.Func)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)3 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)3 SafeList (edu.mit.csail.sdg.alloy4.SafeList)3 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)3 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)3 A4Solution (edu.mit.csail.sdg.translator.A4Solution)3 IOException (java.io.IOException)3 IdentityHashMap (java.util.IdentityHashMap)3 Map (java.util.Map)3