Add updates...
[repair.git] / Repair / RepairCompiler / MCC / IR / Termination.java
index 830e07551e74c93ceb39d068260dbc1457811dd3..5b741c1e26a52a1aeae1b24c9e1e256e814cdbf4 100755 (executable)
@@ -26,6 +26,7 @@ public class Termination {
     ComputeMaxSize maxsize;
     State state;
     AbstractInterferes abstractinterferes;
+    ConcreteInterferes concreteinterferes;
     ConstraintDependence constraintdependence;
     ExactSize exactsize;
     ArrayAnalysis arrayanalysis;
@@ -62,6 +63,7 @@ public class Termination {
        arrayanalysis=new ArrayAnalysis(state,this);
 
        abstractinterferes=new AbstractInterferes(this);
+       concreteinterferes=new ConcreteInterferes(this);
        generateconjunctionnodes();
        constraintdependence=new ConstraintDependence(state,this);
 
@@ -86,6 +88,9 @@ public class Termination {
            e.printStackTrace();
            System.exit(-1);
        }
+
+       generatedebuggraphs();
+
        for(Iterator it=updatenodes.iterator();it.hasNext();) {
            GraphNode gn=(GraphNode)it.next();
            TermNode tn=(TermNode)gn.getOwner();
@@ -99,7 +104,6 @@ public class Termination {
            System.out.println("Can't generate terminating repair algorithm!");
            System.exit(-1);
        }
-       constraintdependence.traversedependences(this);
 
        System.out.println("Removing:");
        for(Iterator it=removedset.iterator();it.hasNext();) {
@@ -117,7 +121,30 @@ public class Termination {
            e.printStackTrace();
            System.exit(-1);
        }
+       constraintdependence.traversedependences(this);
+    }
 
+    void generatedebuggraphs() {
+       for (int i=0;i<Compiler.debuggraphs.size();i++) {
+           DebugItem di=(DebugItem) Compiler.debuggraphs.get(i);
+           HashSet superset=new HashSet();
+           Constraint constr=(Constraint)state.vConstraints.get(di.constraintnum);
+           
+           if (di.conjunctionnum==-1) {
+               superset.addAll((Set)conjunctionmap.get(constr));
+           } else {
+               DNFConstraint dnf=constr.dnfconstraint;
+               superset.add(conjtonodemap.get(dnf.get(di.conjunctionnum)));
+           }
+           
+           GraphNode.boundedcomputeclosure(superset,null,di.depth);
+           try {
+               GraphNode.DOTVisitor.visit(new FileOutputStream("graph"+di.constraintnum+"-"+di.depth+(di.conjunctionnum==-1?"":"-"+di.conjunctionnum)+".dot"),superset);
+           } catch (Exception e) {
+               e.printStackTrace();
+               System.exit(-1);
+           }
+       }
     }
 
 
@@ -142,8 +169,6 @@ public class Termination {
                    conjunctionmap.put(c,new HashSet());
                ((Set)conjunctionmap.get(c)).add(gn);
                conjtonodemap.put(dnf.get(j),gn);
-
-
            }
            // Construct quantifier "conjunction" nodes
            for(int j=0;j<c.numQuantifiers();j++) {
@@ -155,6 +180,7 @@ public class Termination {
                    DNFConstraint dconst=new DNFConstraint(ip);
                    dconst=dconst.not();
                    TermNode tn=new TermNode(c,dconst.get(0));
+                   tn.setquantifiernode();
                    GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
                                               "Conj ("+i+","+j+") "+dconst.get(0).name()
                                               ,tn);
@@ -171,6 +197,7 @@ public class Termination {
                    DNFConstraint dconst=new DNFConstraint(ip);
                    dconst=dconst.not();
                    TermNode tn=new TermNode(c,dconst.get(0));
+                   tn.setquantifiernode();
                    GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
                                               "Conj ("+i+","+j+") "+dconst.get(0).name()
                                               ,tn);
@@ -179,7 +206,6 @@ public class Termination {
                        conjunctionmap.put(c,new HashSet());
                    ((Set)conjunctionmap.get(c)).add(gn);
                    conjtonodemap.put(dconst.get(0),gn);
-
                }
            }
        }
@@ -223,16 +249,15 @@ public class Termination {
                    }
                }
            }
-
            /* Cycle through the rules to look for possible conflicts */
            for(int i=0;i<state.vRules.size();i++) {
                Rule r=(Rule) state.vRules.get(i);  
-               if (ConcreteInterferes.interferes(mun,r,true)) {
+               if (concreteinterferes.interferes(mun,r,true)) {
                    GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
                    GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
                    gn.addEdge(e);
                }
-               if (ConcreteInterferes.interferes(mun,r,false)) {
+               if (concreteinterferes.interferes(mun,r,false)) {
                    GraphNode scopenode=(GraphNode)scopefalsify.get(r);
                    GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
                    gn.addEdge(e);
@@ -263,7 +288,6 @@ public class Termination {
                    }
                }
            }
-
            for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
                GraphNode gn2=(GraphNode)scopeiterator.next();
                TermNode tn2=(TermNode)gn2.getOwner();
@@ -362,7 +386,7 @@ public class Termination {
                int[] array=dp.getPredicate().getRepairs(dp.isNegated(),this);
                Descriptor d=dp.getPredicate().getDescriptor();
                for(int j=0;j<array.length;j++) {
-                   AbstractRepair ar=new AbstractRepair(dp,array[j],d);
+                   AbstractRepair ar=new AbstractRepair(dp,array[j],d,sources);
                    TermNode tn2=new TermNode(ar);
                    GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),gn.getTextLabel()+" #"+i+" "+ar.type(),tn2);
                    GraphNode.Edge e=new GraphNode.Edge("abstract",gn2);
@@ -382,7 +406,7 @@ public class Termination {
            VarExpr ve=new VarExpr("DUMMY");
            InclusionPredicate ip=new InclusionPredicate(ve,new SetExpr(sd));
            DNFPredicate tp=new DNFPredicate(false,ip);
-           AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTOSET, sd);
+           AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTOSET, sd,sources);
            TermNode tn=new TermNode(ar);
            GraphNode gn=new GraphNode("AbstractAddSetRule"+i,tn);
            if (!predtoabstractmap.containsKey(tp))
@@ -393,7 +417,7 @@ public class Termination {
            abstractadd.put(sd,gn);
            
            DNFPredicate tp2=new DNFPredicate(true,ip);
-           AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMSET, sd);
+           AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMSET, sd,sources);
            TermNode tn2=new TermNode(ar2);
            GraphNode gn2=new GraphNode("AbstractRemSetRule"+i,tn2);
            if (!predtoabstractmap.containsKey(tp2))
@@ -413,7 +437,7 @@ public class Termination {
            InclusionPredicate ip=new InclusionPredicate(ve2,new ImageSetExpr(vd1, rd));
            
            DNFPredicate tp=new DNFPredicate(false,ip);
-           AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTORELATION, rd);
+           AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTORELATION, rd,sources);
            TermNode tn=new TermNode(ar);
            GraphNode gn=new GraphNode("AbstractAddRelRule"+i,tn);
            if (!predtoabstractmap.containsKey(tp))
@@ -424,7 +448,7 @@ public class Termination {
            abstractadd.put(rd,gn);
            
            DNFPredicate tp2=new DNFPredicate(true,ip);
-           AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMRELATION, rd);
+           AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMRELATION, rd,sources);
            TermNode tn2=new TermNode(ar2);
            GraphNode gn2=new GraphNode("AbstractRemRelRule"+i,tn2);
            if (!predtoabstractmap.containsKey(tp2))
@@ -486,7 +510,7 @@ public class Termination {
                } else {
                    /* Negate conjunction */
                    int c=j-r.numQuantifiers();
-                   if (!processconjunction(un,r.getDNFNegGuardExpr().get(c))) {
+                   if (!processconjunction(un,r.getDNFNegGuardExpr().get(c),null)) {
                        continue;
                    }
                }
@@ -505,6 +529,7 @@ public class Termination {
     void generatedatastructureupdatenodes() {
        for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
            GraphNode gn=(GraphNode)absiterator.next();
+           System.out.println("Analysing: "+gn.getTextLabel());
            TermNode tn=(TermNode) gn.getOwner();
            AbstractRepair ar=tn.getAbstract();
            if (ar.getType()==AbstractRepair.ADDTOSET) {
@@ -590,7 +615,7 @@ public class Termination {
                    } else {goodflag=false;break;}
                } else {
                    int c=count[i]-r.numQuantifiers();
-                   if (!processconjunction(un,r.getDNFNegGuardExpr().get(c))) {
+                   if (!processconjunction(un,r.getDNFNegGuardExpr().get(c),null)) {
                        goodflag=false;break;
                    }
                }
@@ -675,13 +700,13 @@ public class Termination {
                UpdateNode un=new UpdateNode(r);
                
                int c=count[i];
-               if (!processconjunction(un,r.getDNFGuardExpr().get(c))) {
+               if (!processconjunction(un,r.getDNFGuardExpr().get(c),null)) {
                    goodflag=false;break;
                }
                RelationInclusion ri=(RelationInclusion)r.getInclusion();
                if (!(ri.getLeftExpr() instanceof VarExpr)) {
-                   if (ri.getLeftExpr().isValue()) {
-                       Updates up=new Updates(ri.getLeftExpr(),leftindex);
+                   if (ri.getLeftExpr().isValue(ri.getRelation().getDomain().getType())) {
+                       Updates up=new Updates(ri.getLeftExpr(),leftindex,ri.getRelation().getDomain().getType());
                        un.addUpdate(up);
                    } else {
                        if (inverted)
@@ -691,14 +716,14 @@ public class Termination {
                } else {
                    VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
                    if (vd.isGlobal()) {
-                       Updates up=new Updates(ri.getLeftExpr(),leftindex);
+                       Updates up=new Updates(ri.getLeftExpr(),leftindex,null);
                        un.addUpdate(up);
                    } else if (inverted)
                        goodflag=false;
                }
                if (!(ri.getRightExpr() instanceof VarExpr)) {
-                   if (ri.getRightExpr().isValue()) {
-                       Updates up=new Updates(ri.getRightExpr(),rightindex);
+                   if (ri.getRightExpr().isValue(ri.getRelation().getRange().getType())) {
+                       Updates up=new Updates(ri.getRightExpr(),rightindex,ri.getRelation().getRange().getType());
                        un.addUpdate(up);
                    } else {
                        if (!inverted)
@@ -709,7 +734,7 @@ public class Termination {
                } else {
                    VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
                    if (vd.isGlobal()) {
-                       Updates up=new Updates(ri.getRightExpr(),rightindex);
+                       Updates up=new Updates(ri.getRightExpr(),rightindex,null);
                        un.addUpdate(up);
                    } else if (!inverted) 
                        goodflag=false;
@@ -738,6 +763,8 @@ public class Termination {
     void generateaddtosetrelation(GraphNode gn, AbstractRepair ar) {
        for(int i=0;i<state.vRules.size();i++) {
            Rule r=(Rule) state.vRules.get(i);
+
+
            /* See if this is a good rule*/
            if ((r.getInclusion() instanceof SetInclusion&&
                 ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet())||
@@ -747,10 +774,16 @@ public class Termination {
                /* First solve for quantifiers */
                Vector bindings=new Vector();
                /* Construct bindings */
-               if (!constructbindings(bindings,r,false))
+
+               Hashtable setmapping=new Hashtable();
+               System.out.println("Attempting to construct bindings");
+
+               if (!constructbindings(bindings,r,ar,setmapping,false))
                    continue;
+               System.out.println("Bindings constructed");
                //Generate add instruction
                DNFRule dnfrule=r.getDNFGuardExpr();
+                endloop:
                for(int j=0;j<dnfrule.size();j++) {
                    Inclusion inc=r.getInclusion();
                    UpdateNode un=new UpdateNode(r);
@@ -758,78 +791,105 @@ public class Termination {
                    /* Now build update for tuple/set inclusion condition */
                    if(inc instanceof SetInclusion) {
                        SetInclusion si=(SetInclusion)inc;
-                       if (!(si.elementexpr instanceof VarExpr)) {
-                           if (si.elementexpr.isValue()) {
-                               Updates up=new Updates(si.elementexpr,0);
+                       Expr e=si.elementexpr;
+                       
+                       while(e instanceof CastExpr) {
+                           CastExpr ce=(CastExpr)e;
+                           if (ce.getType()!=si.getSet().getType())
+                               continue endloop;
+                           e=ce.getExpr();
+                       }
+
+                       if (!(e instanceof VarExpr)) {
+                           if (e.isValue(si.getSet().getType())) {
+                               Updates up=new Updates(e,0,si.getSet().getType());
                                un.addUpdate(up);
                            } else {
                                /* We're an add to set*/
-                               System.out.println("Rule: "+r);
-                               ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,si.elementexpr);
-                               System.out.println("Attempting perform array add");
+                               ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,e);
                                SetDescriptor set=sources.setSource(si.getSet())?
                                    sources.getSourceSet(si.getSet()):null;
                                if (set==null)
                                    continue;
-                               System.out.println("Non-null source set");
                                ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
                                if (rap==ArrayAnalysis.AccessPath.NONE)
                                    continue;
-                               System.out.println("A");
                                if (!rap.equal(ap))
                                    continue;
-                               System.out.println("B");
-                               if (!constructarrayupdate(un, si.elementexpr, rap, 0))
+                               if (!constructarrayupdate(un, e, rap, 0))
                                    continue;
-                               System.out.println("C");
                            }
                        } else {
-                           VarDescriptor vd=((VarExpr)si.elementexpr).getVar();
+                           VarDescriptor vd=((VarExpr)e).getVar();
                            if (vd.isGlobal()) {
-                               Updates up=new Updates(si.elementexpr,0);
+                               Updates up=new Updates(e,0,null);
                                un.addUpdate(up);
                            }
                        }
                    } else if (inc instanceof RelationInclusion) {
                        RelationInclusion ri=(RelationInclusion)inc;
-                       if (!(ri.getLeftExpr() instanceof VarExpr)) {
-                           if (ri.getLeftExpr().isValue()) {
-                               Updates up=new Updates(ri.getLeftExpr(),0);
+
+                       Expr e=ri.getLeftExpr();
+                       
+                       while(e instanceof CastExpr) {
+                           CastExpr ce=(CastExpr)e;
+                           if (ce.getType()!=ri.getRelation().getDomain().getType())
+                               continue endloop;
+                           e=ce.getExpr();
+                       }
+                       if (!(e instanceof VarExpr)) {
+                           if (e.isValue(ri.getRelation().getDomain().getType())) {
+                               Updates up=new Updates(e,0,ri.getRelation().getDomain().getType());
+                               if (ar.getDomainSet()!=null)
+                                   setmapping.put(e,ar.getDomainSet());
                                un.addUpdate(up);
                            } else {
                                /* We don't handly relation modifies */
                                if (ar.getType()==AbstractRepair.MODIFYRELATION)
                                    continue;
                                /* We're an add to relation*/
-                               ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,ri.getLeftExpr());
+                               ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,e);
                                SetDescriptor set=sources.relsetSource(ri.getRelation(),true /* Domain*/)?
                                    sources.relgetSourceSet(ri.getRelation(),true):null;
                                if (set==null)
                                    continue;
                                ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
-                               
                                if (rap==ArrayAnalysis.AccessPath.NONE||
                                    !rap.equal(ap)||
-                                   !constructarrayupdate(un, ri.getLeftExpr(), rap, 0))
+                                   !constructarrayupdate(un, e, rap, 0))
                                    continue;
+                               if (ar.getDomainSet()!=null)
+                                   setmapping.put(e,ar.getDomainSet());
+
                            }
                        } else {
-                           VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
+                           VarDescriptor vd=((VarExpr)e).getVar();
                            if (vd.isGlobal()) {
-                               Updates up=new Updates(ri.getLeftExpr(),0);
+                               Updates up=new Updates(e,0,null);
+                               if (ar.getDomainSet()!=null)
+                                   setmapping.put(e,ar.getDomainSet());
                                un.addUpdate(up);
                            }
                        }
-                       if (!(ri.getRightExpr() instanceof VarExpr)) {
-                           if (ri.getRightExpr().isValue()) {
-                               Updates up=new Updates(ri.getRightExpr(),1);
+                       
+                       e=ri.getRightExpr();
+                       
+                       while(e instanceof CastExpr) {
+                           CastExpr ce=(CastExpr)e;
+                           if (ce.getType()!=ri.getRelation().getRange().getType())
+                               continue endloop;
+                           e=ce.getExpr();
+                       }
+                       if (!(e instanceof VarExpr)) {
+                           if (e.isValue(ri.getRelation().getRange().getType())) {
+                               Updates up=new Updates(e,1,ri.getRelation().getRange().getType());
                                un.addUpdate(up);
                            } else {
                                /* We don't handly relation modifies */
                                if (ar.getType()==AbstractRepair.MODIFYRELATION)
                                    continue;
                                /* We're an add to relation*/
-                               ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,ri.getRightExpr());
+                               ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,e);
                                SetDescriptor set=sources.relsetSource(ri.getRelation(),false /* Range*/)?
                                    sources.relgetSourceSet(ri.getRelation(),false):null;
                                if (set==null)
@@ -838,17 +898,22 @@ public class Termination {
                                
                                if (rap==ArrayAnalysis.AccessPath.NONE||
                                    !rap.equal(ap)||
-                                   !constructarrayupdate(un, ri.getRightExpr(), rap, 1))
+                                   !constructarrayupdate(un, e, rap, 1))
                                    continue;
+                               if (ar.getRangeSet()!=null)
+                                   setmapping.put(e,ar.getRangeSet());
                            }
                        } else {
-                           VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
+                           VarDescriptor vd=((VarExpr)e).getVar();
                            if (vd.isGlobal()) {
-                               Updates up=new Updates(ri.getRightExpr(),1);
+                               Updates up=new Updates(e,1,null);
+                               if (ar.getRangeSet()!=null)
+                                   setmapping.put(e,ar.getRangeSet());
                                un.addUpdate(up);
                            }
                        }
                    }
+                   System.out.println("Built inclusion condition updates.");
                    //Finally build necessary updates to satisfy conjunction
                    RuleConjunction ruleconj=dnfrule.get(j);
 
@@ -857,9 +922,13 @@ public class Termination {
                    TermNode tn=new TermNode(mun);
                    GraphNode gn2=new GraphNode("UpdateAdd"+addtocount,tn);
 
-                   if (processquantifiers(gn2,un, r)&&
-                       processconjunction(un,ruleconj)&&
-                       un.checkupdates()) {
+                   if (debugmsg("Start processing quantifiers")&&
+                       processquantifiers(gn2,un, r,setmapping)&&
+                       debugmsg("Finished processing quantifiers")&&
+                       processconjunction(un,ruleconj,setmapping)&&
+                       debugmsg("Finished processing conjunction")&&
+                       un.checkupdates()&&
+                       debugmsg("Updates checked")) {
                        mun.addUpdate(un);
                        GraphNode.Edge e=new GraphNode.Edge("abstract"+addtocount,gn2);
                        addtocount++;
@@ -869,6 +938,12 @@ public class Termination {
                }
            }
        }
+
+    }
+
+    boolean debugmsg(String st) {
+       System.out.println(st);
+       return true;
     }
 
     boolean constructarrayupdate(UpdateNode un, Expr lexpr, ArrayAnalysis.AccessPath ap, int slotnumber) {
@@ -888,7 +963,7 @@ public class Termination {
            if (fd instanceof ArrayDescriptor) {
                // We have an ArrayDescriptor!
                Expr index=de.getIndex();
-               if (!index.isValue()) {/* Not assignable */
+               if (!index.isValue(null)) {/* Not assignable */
                    System.out.println("ERROR:Index isn't assignable");
                    return false;
                }
@@ -904,9 +979,10 @@ public class Termination {
      * performs a removal.  The function returns true if it is able to
      * generate a valid set of bindings and false otherwise. */
 
-    boolean constructbindings(Vector bindings, Rule r, boolean isremoval) {
+    boolean constructbindings(Vector bindings, Rule r, AbstractRepair ar, Hashtable setmapping, boolean isremoval) {
        boolean goodupdate=true;
        Inclusion inc=r.getInclusion();
+
        for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
            Quantifier q=(Quantifier)iterator.next();
            if ((q instanceof SetQuantifier)||(q instanceof ForQuantifier)) {
@@ -920,9 +996,21 @@ public class Termination {
                }
                if(inc instanceof SetInclusion) {
                    SetInclusion si=(SetInclusion)inc;
-                   if ((si.elementexpr instanceof VarExpr)&&
-                       (((VarExpr)si.elementexpr).getVar()==vd)) {
+                   Expr tmpe=si.elementexpr;
+                   Expr e=si.elementexpr;
+
+                   while(e instanceof CastExpr) {
+                       CastExpr ce=(CastExpr)e;
+                       if (ce.getType()!=si.getSet().getType())
+                           return false;
+                       e=ce.getExpr();
+                   }
+                   
+                   if ((e instanceof VarExpr)&&
+                       (((VarExpr)e).getVar()==vd)) {
                        /* Can solve for v */
+                       if (!si.getSet().getType().isSubtypeOf(set.getType()))
+                           return false;
                        Binding binding=new Binding(vd,0);
                        bindings.add(binding);
                    } else {
@@ -932,16 +1020,45 @@ public class Termination {
                    RelationInclusion ri=(RelationInclusion)inc;
                    boolean f1=true;
                    boolean f2=true;
-                   if ((ri.getLeftExpr() instanceof VarExpr)&&
-                       (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
+
+                   Expr e=ri.getLeftExpr();
+
+                   while(e instanceof CastExpr) {
+                       CastExpr ce=(CastExpr)e;
+                       if (ce.getType()!=ri.getRelation().getDomain().getType())
+                           return false;
+                       e=ce.getExpr();
+                   }
+                   
+                   if ((e instanceof VarExpr)&&
+                       (((VarExpr)e).getVar()==vd)) {
                                /* Can solve for v */
                        Binding binding=new Binding(vd,0);
+                       if (!ri.getRelation().getDomain().getType().isSubtypeOf(set.getType()))
+                           return false;
+                       if (ar.getDomainSet()!=null)
+                           setmapping.put(ri.getLeftExpr(),ar.getDomainSet());
                        bindings.add(binding);
                    } else f1=false;
-                   if ((ri.getRightExpr() instanceof VarExpr)&&
-                       (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
+
+
+                   e=ri.getRightExpr();
+
+                   while(e instanceof CastExpr) {
+                       CastExpr ce=(CastExpr)e;
+                       if (ce.getType()!=ri.getRelation().getRange().getType())
+                           return false;
+                       e=ce.getExpr();
+                   }
+                   
+                   if ((e instanceof VarExpr)&&
+                       (((VarExpr)e).getVar()==vd)) {
                                /* Can solve for v */
-                       Binding binding=new Binding(vd,0);
+                       Binding binding=new Binding(vd,1);
+                       if (!ri.getRelation().getRange().getType().isSubtypeOf(set.getType()))
+                           return false;
+                       if (ar.getRangeSet()!=null)
+                           setmapping.put(ri.getRightExpr(),ar.getRangeSet());
                        bindings.add(binding);
                    } else f2=false;
                    if (!(f1||f2))
@@ -966,12 +1083,26 @@ public class Termination {
                RelationQuantifier rq=(RelationQuantifier)q;
                for(int k=0;k<2;k++) {
                    VarDescriptor vd=(k==0)?rq.x:rq.y;
+                   TypeDescriptor td=(k==0)?rq.getRelation().getDomain().getType():rq.getRelation().getRange().getType();
                    if(inc instanceof SetInclusion) {
                        SetInclusion si=(SetInclusion)inc;
-                       if ((si.elementexpr instanceof VarExpr)&&
-                           (((VarExpr)si.elementexpr).getVar()==vd)) {
+
+                       Expr e=si.elementexpr;
+                       
+                       while(e instanceof CastExpr) {
+                           CastExpr ce=(CastExpr)e;
+                           if (ce.getType()!=td)
+                               return false;
+                           e=ce.getExpr();
+                       }
+
+                       if ((e instanceof VarExpr)&&
+                           (((VarExpr)e).getVar()==vd)) {
                            /* Can solve for v */
                            Binding binding=new Binding(vd,0);
+
+                           if (!si.getSet().getType().isSubtypeOf(td))
+                               return false;
                            bindings.add(binding);
                        } else
                            goodupdate=false;
@@ -979,16 +1110,44 @@ public class Termination {
                        RelationInclusion ri=(RelationInclusion)inc;
                        boolean f1=true;
                        boolean f2=true;
+
+
+                       Expr e=ri.getLeftExpr();
+                       
+                       while(e instanceof CastExpr) {
+                           CastExpr ce=(CastExpr)e;
+                           if (ce.getType()!=ri.getRelation().getDomain().getType())
+                               return false;
+                           e=ce.getExpr();
+                       }
                        if ((ri.getLeftExpr() instanceof VarExpr)&&
                            (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
                            /* Can solve for v */
                            Binding binding=new Binding(vd,0);
+                           if (!ri.getRelation().getDomain().getType().isSubtypeOf(td))
+                               return false;
+                           if (ar.getDomainSet()!=null)
+                               setmapping.put(ri.getLeftExpr(),ar.getDomainSet());
                            bindings.add(binding);
                        } else f1=false;
+
+
+                       e=ri.getRightExpr();
+                       
+                       while(e instanceof CastExpr) {
+                           CastExpr ce=(CastExpr)e;
+                           if (ce.getType()!=ri.getRelation().getRange().getType())
+                               return false;
+                           e=ce.getExpr();
+                       }
                        if ((ri.getRightExpr() instanceof VarExpr)&&
                            (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
                            /* Can solve for v */
-                           Binding binding=new Binding(vd,0);
+                           Binding binding=new Binding(vd,1);
+                           if (!ri.getRelation().getRange().getType().isSubtypeOf(td))
+                               return false;
+                           if (ar.getRangeSet()!=null)
+                               setmapping.put(ri.getRightExpr(),ar.getRangeSet());
                            bindings.add(binding);
                        } else f2=false;
                        if (!(f1||f2))
@@ -1013,12 +1172,11 @@ public class Termination {
     /** Adds updates that add an item to the appropriate set or
      * relation quantified over by the model definition rule.. */
     
-    boolean processquantifiers(GraphNode gn,UpdateNode un, Rule r) {
+    boolean processquantifiers(GraphNode gn,UpdateNode un, Rule r,Hashtable setmapping) {
        Inclusion inc=r.getInclusion();
        for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
            Quantifier q=(Quantifier)iterator.next();
            /* Add quantifier */
-           /* FIXME: Analysis to determine when this update is necessary */
            if (q instanceof RelationQuantifier) {
                RelationQuantifier rq=(RelationQuantifier)q;
                TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
@@ -1041,8 +1199,12 @@ public class Termination {
                    constraintdependence.requiresConstraint(gn,reqc);
                    continue; /* Don't need to ensure addition for search */
                }
+               VarExpr ve=new VarExpr(sq.var);
+               SetDescriptor sd=findExpr(setmapping, ve);
+               if (sd!=null&&sd.isSubset(sq.set))
+                   continue; /* this update is trivially true */
 
-               ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
+               ElementOfExpr eoe=new ElementOfExpr(ve,sq.set);
                eoe.td=ReservedTypeDescriptor.INT;
                Updates u=new Updates(eoe,false);
                un.addUpdate(u);
@@ -1058,10 +1220,23 @@ public class Termination {
        return true;
     }
 
+    static SetDescriptor findExpr(Hashtable setmapping, Expr e) {
+       if (setmapping==null)
+           return null;
+       Set kset=setmapping.keySet();
+       for(Iterator it=kset.iterator();it.hasNext();) {
+           Expr expr=(Expr)it.next();
+           if (expr.equals(null,e)) {
+               return (SetDescriptor)setmapping.get(expr);
+           }
+       }
+       return null;
+    }
+
     /** This method generates the necessary updates to satisfy the
      * conjunction ruleconj. */
 
-    boolean  processconjunction(UpdateNode un,RuleConjunction ruleconj){
+    boolean  processconjunction(UpdateNode un,RuleConjunction ruleconj,Hashtable setmapping) {
        boolean okay=true;
        for(int k=0;k<ruleconj.size();k++) {
            DNFExpr de=ruleconj.get(k);
@@ -1072,6 +1247,9 @@ public class Termination {
                Updates up=new Updates(ex.left,ex.right,op, de.getNegation());
                un.addUpdate(up);
            } else if (e instanceof ElementOfExpr) {
+               SetDescriptor sd=findExpr(setmapping, ((ElementOfExpr)e).element);
+               if (sd!=null&&sd.isSubset(((ElementOfExpr)e).set))
+                   continue; /* this update is trivially true */
                Updates up=new Updates(e,de.getNegation());
                un.addUpdate(up);
            } else if (e instanceof TupleOfExpr) {
@@ -1092,4 +1270,66 @@ public class Termination {
        }
        return okay;
     }
+
+    /** This method sees if when the quantifiers listed in set are
+     *  fixed, whether there can be more than one unique binding for
+     *  the constraint or model definition rule qs.*/
+    public boolean analyzeQuantifiers(Quantifiers qs,Set set) {
+       for(int i=0;i<qs.numQuantifiers();i++) {
+           Quantifier q=qs.getQuantifier(i);
+           if (set.contains(q))
+               continue;
+           if (q instanceof SetQuantifier) {
+               SetDescriptor sd=((SetQuantifier)q).getSet();
+               if (maxsize.getsize(sd)<=1&&
+                   maxsize.getsize(sd)>=0)
+                   continue;
+           } else if (q instanceof RelationQuantifier) {
+               RelationDescriptor rd=((RelationQuantifier)q).getRelation();
+               if (maxsize.getsize(rd)<=1&&
+                   maxsize.getsize(rd)>=0)
+                   continue;
+           }
+           return false;
+       }
+       return true;
+    }
+
+    public boolean mutuallyexclusive(SetDescriptor sd1, SetDescriptor sd2) {
+       if (mutualexclusive(sd1,sd2)||
+           mutualexclusive(sd2,sd1))
+           return true;
+       else
+           return false;
+    }
+
+    private boolean mutualexclusive(SetDescriptor sd1, SetDescriptor sd2) {
+       Vector rules=state.vRules;
+       for(int i=0;i<rules.size();i++) {
+           Rule r=(Rule)rules.get(i);
+           if (r.getInclusion().getTargetDescriptors().contains(sd1)) {
+               /* Rule may add items to sd1 */
+               SetInclusion si=(SetInclusion)r.getInclusion();
+               Expr ve=si.getExpr();
+               DNFRule drule=r.getDNFGuardExpr();
+               for(int j=0;j<drule.size();j++) {
+                   RuleConjunction rconj=drule.get(j);
+                   boolean containsexclusion=false;
+                   for (int k=0;k<rconj.size();k++) {
+                       DNFExpr dexpr=rconj.get(k);
+                       if (dexpr.getNegation()&&
+                           dexpr.getExpr() instanceof ElementOfExpr&&
+                           ((ElementOfExpr)dexpr.getExpr()).element.equals(null,ve)) {
+                           SetDescriptor sd=((ElementOfExpr)dexpr.getExpr()).set;
+                           if (sd.isSubset(sd2))
+                               containsexclusion=true;
+                       }
+                   }
+                   if (!containsexclusion)
+                       return false;
+               }
+           }
+       }
+       return true;
+    }
 }