Package edu.mit.csail.sdg.alloy4compiler.ast

Examples of edu.mit.csail.sdg.alloy4compiler.ast.Type


            for(ExprVar sk:frame.skolems) un.seen(sk.label);
            // Store up the skolems
            List<Object> skolems = new ArrayList<Object>();
            for(Map.Entry<Relation,Type> e: frame.rel2type.entrySet()) {
               Relation r = e.getKey(); if (!frame.eval.instance().contains(r)) continue;
               Type t = e.getValue();   if (t.arity() > r.arity()) continue; // Something is wrong; let's skip it
               while (t.arity() < r.arity()) t = UNIV.type().product(t);
               String n = Util.tail(r.name());
               while(n.length()>0 && n.charAt(0)=='$') n = n.substring(1);
               skolems.add(n);
               skolems.add(t);
               skolems.add(r);
            }
            // Find all suitable "next" or "prev" relations
            nexts = new LinkedHashMap<Sig,List<Tuple>>();
            for(Sig sig:frame.sigs) for(Field f: sig.getFields()) if (f.label.compareToIgnoreCase("next")==0) {
               List<List<PrimSig>> fold = f.type().fold();
               if (fold.size()==1) {
                  List<PrimSig> t = fold.get(0);
                  if (t.size()==3 && t.get(0).isOne!=null && t.get(1)==t.get(2) && !nexts.containsKey(t.get(1))) {
                     TupleSet set = frame.eval.evaluate(frame.a2k(t.get(1)));
                     if (set.size()<=1) continue;
                     TupleSet next = frame.eval.evaluate(frame.a2k(t.get(0)).join(frame.a2k(f)));
                     List<Tuple> test = isOrder(next, set);
                     if (test!=null) nexts.put(t.get(1), test);
                  } else if (t.size()==2 && t.get(0)==t.get(1) && !nexts.containsKey(t.get(0))) {
                     TupleSet set = frame.eval.evaluate(frame.a2k(t.get(0)));
                     if (set.size()<=1) continue;
                     TupleSet next = frame.eval.evaluate(frame.a2k(f));
                     List<Tuple> test = isOrder(next, set);
                     if (test!=null) nexts.put(t.get(1), test);
                  }
               }
            }
            for(Sig sig:frame.sigs) for(Field f: sig.getFields()) if (f.label.compareToIgnoreCase("prev")==0) {
               List<List<PrimSig>> fold = f.type().fold();
               if (fold.size()==1) {
                  List<PrimSig> t = fold.get(0);
                  if (t.size()==3 && t.get(0).isOne!=null && t.get(1)==t.get(2) && !nexts.containsKey(t.get(1))) {
                     TupleSet set = frame.eval.evaluate(frame.a2k(t.get(1)));
                     if (set.size()<=1) continue;
                     TupleSet next = frame.eval.evaluate(frame.a2k(t.get(0)).join(frame.a2k(f)).transpose());
                     List<Tuple> test = isOrder(next, set);
                     if (test!=null) nexts.put(t.get(1), test);
                  } else if (t.size()==2 && t.get(0)==t.get(1) && !nexts.containsKey(t.get(0))) {
                     TupleSet set = frame.eval.evaluate(frame.a2k(t.get(0)));
                     if (set.size()<=1) continue;
                     TupleSet next = frame.eval.evaluate(frame.a2k(f).transpose());
                     List<Tuple> test = isOrder(next, set);
                     if (test!=null) nexts.put(t.get(1), test);
                  }
               }
            }
            // Assign atom->name and atom->MostSignificantSig
            for(Tuple t:frame.eval.evaluate(Relation.INTS)) { frame.atom2sig.put(t.atom(0), SIGINT); }
            for(Tuple t:frame.eval.evaluate(KK_SEQIDX))     { frame.atom2sig.put(t.atom(0), SEQIDX); }
            for(Tuple t:frame.eval.evaluate(KK_STRING))     { frame.atom2sig.put(t.atom(0), STRING); }
            for(Sig sig:frame.sigs) if (sig instanceof PrimSig && !sig.builtin && ((PrimSig)sig).isTopLevel()) rename(frame, (PrimSig)sig, nexts, un);
            // These are redundant atoms that were not chosen to be in the final instance
            int unused=0;
            for(Tuple tuple:frame.eval.evaluate(Relation.UNIV)) {
               Object atom = tuple.atom(0);
               if (!frame.atom2sig.containsKey(atom)) { frame.atom2name.put(atom, "unused"+unused); unused++; }
            }
            // Add the skolems
            for(int num=skolems.size(), i=0; i<num-2; i=i+3) {
                String n = (String) skolems.get(i);
                while(n.length()>0 && n.charAt(0)=='$') n=n.substring(1);
                Type t = (Type) skolems.get(i+1);
                Relation r = (Relation) skolems.get(i+2);
                frame.addSkolem(un.make("$"+n), t, r);
            }
            return;
        }
        for(PrimSig c: s.children()) rename(frame, c, nexts, un);
        String signame = un.make(s.label.startsWith("this/") ? s.label.substring(5) : s.label);
        List<Tuple> list = new ArrayList<Tuple>();
        for(Tuple t: frame.eval.evaluate(frame.a2k(s))) list.add(t);
        List<Tuple> order = nexts.get(s);
        if (order!=null && order.size()==list.size() && order.containsAll(list)) { list=order; }
        int i = 0;
        for(Tuple t: list) {
           if (frame.atom2sig.containsKey(t.atom(0))) continue; // This means one of the subsig has already claimed this atom.
           String x = signame + "$" + i;
           i++;
           frame.atom2sig.put(t.atom(0), s);
           frame.atom2name.put(t.atom(0), x);
           ExprVar v = ExprVar.make(null, x, s.type());
           TupleSet ts = t.universe().factory().range(t, t);
           Relation r = Relation.unary(x);
           frame.eval.instance().add(r, ts);
           frame.a2k.put(v, r);
           frame.atoms.add(v);
        }
View Full Code Here


        final Reporter oldReporter = solver.options().reporter();
        final boolean solved[] = new boolean[]{true};
        solver.options().setReporter(new AbstractReporter() { // Set up a reporter to catch the type+pos of skolems
            @Override public void skolemizing(Decl decl, Relation skolem, List<Decl> predecl) {
                try {
                    Type t=kv2typepos(decl.variable()).a;
                    if (t==Type.EMPTY) return;
                    for(int i=(predecl==null ? -1 : predecl.size()-1); i>=0; i--) {
                        Type pp=kv2typepos(predecl.get(i).variable()).a;
                        if (pp==Type.EMPTY) return;
                        t=pp.product(t);
                    }
                    kr2type(skolem, t);
                } catch(Throwable ex) { } // Exception here is not fatal
            }
            @Override public void solvingCNF(int primaryVars, int vars, int clauses) {
View Full Code Here

       if (x!=Sig.UNIV) return x.children(); else return toplevels;
    }

    /** Write the given Expr and its Type. */
    private boolean writeExpr(String prefix, Expr expr) throws Err {
       Type type = expr.type();
       if (!type.hasTuple()) return false;
       if (sol!=null) {
          // Check to see if the tupleset is *really* fully contained inside "type".
          // If not, then grow "type" until the tupleset is fully contained inside "type"
          Expr sum = type.toExpr();
          int lastSize = (-1);
          while(true) {
             A4TupleSet ts = (A4TupleSet)(sol.eval(expr.minus(sum)));
             int n = ts.size();
             if (n<=0) break;
             if (lastSize>0 && lastSize<=n) throw new ErrorFatal("An internal error occurred in the evaluator.");
             lastSize=n;
             Type extra = ts.iterator().next().type();
             type = type.merge(extra);
             sum = sum.plus(extra.toExpr());
          }
          // Now, write out the tupleset
          A4TupleSet ts = (A4TupleSet)(sol.eval(expr));
          for(A4Tuple t: ts) {
             if (prefix.length()>0) { out.print(prefix); prefix=""; }
View Full Code Here

    /** Returns the arity. */
    public int arity() { return tuple.arity(); }

    /** Returns the type constructed by taking the product for each sig in this tuple. */
    public Type type() {
        Type ans = null;
        for(int i=0; i<tuple.arity(); i++) if (ans==null) ans=sig(0).type(); else ans=ans.product(sig(i).type());
        return ans;
    }
View Full Code Here

       */
      private static boolean applicable(Func f, List<Expr> args) {
         if (f.count() > args.size()) return false;
         int i=0;
         for(ExprVar d: f.params()) {
            Type param=d.type(), arg=args.get(i).type();
            i++;
            // The reason we don't directly compare "arg.arity()" with "param.arity()"
            // is because the arguments may not be fully resolved yet.
            if (!arg.hasCommonArity(param)) return false;
            if (arg.hasTuple() && param.hasTuple() && !arg.intersects(param)) return false;
         }
         return true;
      }
View Full Code Here

        for(Func ff:current_function) if (ff==f) {
            if (maxRecursion<0) {
                throw new ErrorSyntax(x.span(), ""+f+" cannot call itself recursively!");
            }
            if (maxRecursion==0) {
                Type t = f.returnDecl.type();
                if (t.is_bool) return Formula.FALSE;
                if (t.is_int) return IntConstant.constant(0);
                int i = t.arity();
                Expression ans = Expression.NONE;
                while(i>1) { ans = ans.product(Expression.NONE); i--; }
                return ans;
            }
            maxRecursion--;
View Full Code Here

                    rep.bound("Field "+s.label+"."+f.label+" defined to be "+sim+"\n");
                    sol.addField(f, sol.a2k(s).product(sim));
                    continue;
                 }
              }
              Type t = isOne ? Sig.UNIV.type().join(f.type()) : f.type();
              TupleSet ub = factory.noneOf(t.arity());
              for(List<PrimSig> p:t.fold()) {
                 TupleSet upper=null;
                 for(PrimSig b:p) {
                    TupleSet tmp = sol.query(true, sol.a2k(b), false);
                    if (upper==null) upper=tmp; else upper=upper.product(tmp);
                 }
View Full Code Here

TOP

Related Classes of edu.mit.csail.sdg.alloy4compiler.ast.Type

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.