Esempio n. 1
0
        private Term SubstPredsRec(TermDict <Term> memo, Dictionary <FuncDecl, FuncDecl> subst, Term t)
        {
            Term res;

            if (memo.TryGetValue(t, out res))
            {
                return(res);
            }
            if (t.GetKind() == TermKind.App)
            {
                var args = t.GetAppArgs();
                args = args.Select(x => SubstPredsRec(memo, subst, x)).ToArray();
                FuncDecl nf = null;
                var      f  = t.GetAppDecl();
                if (subst.TryGetValue(f, out nf))
                {
                    res = ctx.MkApp(nf, args);
                }
                else
                {
                    res = ctx.CloneApp(t, args);
                }
            } // TODO: handle quantifiers
            else
            {
                res = t;
            }

            memo.Add(t, res);
            return(res);
        }
Esempio n. 2
0
 private void CollectVariables(TermDict <bool> memo, Term t, List <Term> vars)
 {
     if (memo.ContainsKey(t))
     {
         return;
     }
     if (IsVariable(t))
     {
         vars.Add(t);
     }
     if (t.GetKind() == TermKind.App)
     {
         foreach (var s in t.GetAppArgs())
         {
             CollectVariables(memo, s, vars);
         }
     }
     memo.Add(t, true);
 }
Esempio n. 3
0
        private Term CollectParamsRec(TermDict <Term> memo, Term t, List <FuncDecl> parms, List <RPFP.Node> nodes,
                                      Dictionary <Term, Term> done)
        {
            Term res;

            if (memo.TryGetValue(t, out res))
            {
                return(res);
            }
            if (t.GetKind() == TermKind.App)
            {
                var  f = t.GetAppDecl();
                Node node;
                if (relationToNode.TryGetValue(f, out node))
                {
                    if (done.ContainsKey(t))
                    {
                        res = done[t];
                    }
                    else
                    {
                        f = SuffixFuncDecl(t, parms.Count);
                        parms.Add(f);
                        nodes.Add(node);
                        done.Add(t, res); // don't count same expression twice!
                    }
                }

                var args = t.GetAppArgs();
                args = args.Select(x => CollectParamsRec(memo, x, parms, nodes, done)).ToArray();
                res  = ctx.CloneApp(t, args);
            } // TODO: handle quantifiers
            else
            {
                res = t;
            }

            memo.Add(t, res);
            return(res);
        }
Esempio n. 4
0
 private void CollectVariables(TermDict< bool> memo, Term t, List<Term> vars)
 {
     if (memo.ContainsKey(t))
         return;
     if (IsVariable(t))
         vars.Add(t);
     if (t.GetKind() == TermKind.App)
     {
         foreach (var s in t.GetAppArgs())
             CollectVariables(memo, s, vars);
     }
     memo.Add(t, true);
 }
Esempio n. 5
0
 private Term SubstPredsRec(TermDict< Term> memo, Dictionary<FuncDecl,FuncDecl> subst, Term t)
 {
     Term res;
     if (memo.TryGetValue(t, out res))
         return res;
     if (t.GetKind() == TermKind.App)
     {
         var args = t.GetAppArgs();
         args = args.Select(x => SubstPredsRec(memo,subst,x)).ToArray();
         FuncDecl nf = null;
         var f = t.GetAppDecl();
         if (subst.TryGetValue(f, out nf))
         {
             res = ctx.MkApp(nf, args);
         }
         else
         {
             res = ctx.CloneApp(t, args);
         }
     } // TODO: handle quantifiers
     else
         res = t;
     memo.Add(t, res);
     return res;
 }
Esempio n. 6
0
 private void FactorVCs(Term t, List<Term> vcs)
 {
     List<Term> small = new List<Term>();
     ExtractSmallerVCs(t, small);
     foreach (var smm in small)
     {
         List<Term> goals = new List<Term>();
         List<Term> cruft = new List<Term>();
         var sm = largeblock ? MergeGoals(smm) : smm;
         CollectGoals(sm, goals,cruft);
         foreach (var goal in goals)
         {
             TermDict< Term> memo = new TermDict< Term>();
             foreach (var othergoal in goals)
                 memo.Add(othergoal, othergoal.Equals(goal) ? ctx.MkFalse() : ctx.MkTrue());
             foreach (var thing in cruft)
                 memo.Add(thing, ctx.MkTrue());
             var vc = SubstRecGoals(memo, sm);
             vc = ctx.MkImplies(ctx.MkNot(vc), goal);
             vcs.Add(vc);
         }
         {
             TermDict< Term> memo = new TermDict< Term>();
             foreach (var othergoal in goals)
                 memo.Add(othergoal, ctx.MkTrue());
             var vc = SubstRecGoals(memo, sm);
             if (vc != ctx.MkTrue())
             {
                 vc = ctx.MkImplies(ctx.MkNot(vc), ctx.MkFalse());
                 vcs.Add(vc);
             }
         }
     }
 }
Esempio n. 7
0
 private Term CollectParamsRec(TermDict<Term> memo, Term t, List<FuncDecl> parms, List<RPFP.Node> nodes, Dictionary<Term, Term> done)
 {
     Term res;
     if (memo.TryGetValue(t, out res))
         return res;
     if (t.GetKind() == TermKind.App)
     {
         var f = t.GetAppDecl();
         Node node;
         if (relationToNode.TryGetValue(f, out node))
         {
             if (done.ContainsKey(t))
                 res = done[t];
             else
             {
                 f = SuffixFuncDecl(t, parms.Count);
                 parms.Add(f);
                 nodes.Add(node);
                 done.Add(t,res); // don't count same expression twice!
             }
         }
         var args = t.GetAppArgs();
         args = args.Select(x => CollectParamsRec(memo, x, parms, nodes, done)).ToArray();
         res = ctx.CloneApp(t, args);
     } // TODO: handle quantifiers
     else
         res = t;
     memo.Add(t, res);
     return res;
 }
Esempio n. 8
0
 private Term ExtractSmallerVCsRec(TermDict< Term> memo, Term t, List<Term> small, Term lbl = null)
 {
     Term res;
     if (memo.TryGetValue(t, out res))
         return res;
     var kind = t.GetKind();
     if (kind == TermKind.App)
     {
         var f = t.GetAppDecl();
         if (f.GetKind() == DeclKind.Implies){
             var lhs = t.GetAppArgs()[0];
             if(lhs.GetKind() == TermKind.App){
                 var r = lhs.GetAppDecl();
                 if (r.GetKind() == DeclKind.And)
                 {
                     Term q = t.GetAppArgs()[1];
                     var lhsargs = lhs.GetAppArgs();
                     for (int i = lhsargs.Length-1; i >= 0; --i)
                     {
                         q = ctx.MkImplies(lhsargs[i], q);
                     }
                     res = ExtractSmallerVCsRec(memo, q, small,lbl);
                     goto done;
                 }
                 if (r.GetKind() == DeclKind.Label)
                 {
                     var arg = lhs;
                     arg = lhs.GetAppArgs()[0];
                     if (!(arg.GetKind() == TermKind.App && arg.GetAppDecl().GetKind() == DeclKind.Uninterpreted))
                         goto normal;
                     if (!(annotationInfo.ContainsKey(arg.GetAppDecl().GetDeclName()) && annotationInfo[arg.GetAppDecl().GetDeclName()].type == AnnotationInfo.AnnotationType.LoopInvariant))
                         goto normal;
                     var sm = ctx.MkImplies(lhs, ExtractSmallerVCsRec(memo, t.GetAppArgs()[1], small));
                     if (lbl != null)
                         sm = ctx.MkImplies(lbl, sm);
                     small.Add(sm);
                     res = ctx.MkTrue();
                     goto done;
                 }
                 if (r.GetKind() == DeclKind.Uninterpreted)
                 {
                     var arg = lhs;
                     if (!(annotationInfo.ContainsKey(arg.GetAppDecl().GetDeclName()) && annotationInfo[arg.GetAppDecl().GetDeclName()].type == AnnotationInfo.AnnotationType.LoopInvariant))
                         goto normal;
                     var sm = ctx.MkImplies(lhs,ExtractSmallerVCsRec(memo,t.GetAppArgs()[1],small));
                     if (lbl != null)
                         sm = ctx.MkImplies(lbl, sm);
                     small.Add(sm);
                     res = ctx.MkTrue();
                     goto done;
                 }
             }
         normal:
             Term newlbl = null;
             if (lhs.IsLabel() && lhs.GetAppArgs()[0] == ctx.MkTrue())
                 newlbl = lhs;
             res = ctx.MkImplies(lhs,ExtractSmallerVCsRec(memo,t.GetAppArgs()[1],small,newlbl));
         }
         else if (f.GetKind() == DeclKind.And)
         {
             res = ctx.MkApp(f,t.GetAppArgs().Select(x => ExtractSmallerVCsRec(memo, x, small)).ToArray());
         }
         else
             res = t;
     }
     else
         res = t;
     done:
     memo.Add(t, res);
     return res;
 }
Esempio n. 9
0
 private Term CollectGoalsRec(TermDict< Term> memo, Term t, List<Term> goals, List<Term> cruft)
 {
     Term res;
     if (memo.TryGetValue(t, out res))
         return res;
     var kind = t.GetKind();
     if (kind == TermKind.App)
     {
         var f = t.GetAppDecl();
         if (f.GetKind() == DeclKind.Implies)
         {
             CollectGoalsRec(memo, t.GetAppArgs()[1], goals, cruft);
             goto done;
         }
         else if (f.GetKind() == DeclKind.And)
         {
             foreach (var arg in t.GetAppArgs())
             {
                 CollectGoalsRec(memo, arg, goals, cruft);
             }
             goto done;
         }
         else if (f.GetKind() == DeclKind.Label)
         {
             var arg = t.GetAppArgs()[0];
             var r = arg.GetAppDecl();
             if (r.GetKind() == DeclKind.Uninterpreted)
             {
                 if (memo.TryGetValue(arg, out res))
                     goto done;
                 if (!annotationInfo.ContainsKey(r.GetDeclName()) && !arg.GetAppDecl().GetDeclName().StartsWith("_solve_"))
                     goto done;
                 goals.Add(arg);
                 memo.Add(arg, arg);
                 goto done;
             }
             else
                 return CollectGoalsRec(memo, arg, goals, cruft);
         }
         else if (f.GetKind() == DeclKind.Uninterpreted)
         {
             string name = f.GetDeclName();
             if (name.StartsWith("_solve_"))
             {
                 if (memo.TryGetValue(t, out res))
                     goto done;
                 goals.Add(t);
                 memo.Add(t, t);
                 return t;
             }
         }
     }
     // else the goal must be cruft
     cruft.Add(t);
     done:
     res = t; // just to return something
     memo.Add(t, res);
     return res;
 }
Esempio n. 10
0
        private Term SubstRecGoals(TermDict< Term> memo, Term t)
        {
            Term res;
            if (memo.TryGetValue(t, out res))
                return res;
            var kind = t.GetKind();
            if (kind == TermKind.App)
            {
                var f = t.GetAppDecl();
                var args = t.GetAppArgs();
                if (f.GetKind() == DeclKind.Implies){
                    res = SubstRecGoals(memo, args[1]);
                    if (res != ctx.MkTrue())
                      res = ctx.MkImplies(args[0],res);
                    goto done;
                }
                else if (f.GetKind() == DeclKind.And)
                {
                    args = args.Select(x => SubstRecGoals(memo, x)).ToArray();
                    args = args.Where(x => x != ctx.MkTrue()).ToArray();
                    res = ctx.MkAnd(args);
                    goto done;
                }
                else if (f.GetKind() == DeclKind.Label)
                {
                    var arg = t.GetAppArgs()[0];
                    var r = arg.GetAppDecl();
                    if (r.GetKind() == DeclKind.Uninterpreted)
                    {
                        if (memo.TryGetValue(arg, out res))
                        {
                            if(res != ctx.MkTrue())
                                res = ctx.MkApp(f, res);
                            goto done;
                        }
                    }
                    else
                    {
                        res = ctx.MkApp(f, SubstRecGoals(memo, arg));
                        goto done;
                    }

                }
                // what's left could be cruft!
                if (memo.TryGetValue(t, out res))
                {
                    goto done;
                }
            }
            res = t;
              done:
            memo.Add(t, res);
            return res;
        }
Esempio n. 11
0
 private Term SubstRec(TermDict< Term> memo, Term t)
 {
     Term res;
     if (memo.TryGetValue(t, out res))
         return res;
     var kind = t.GetKind();
     if (kind == TermKind.App)
     {
         // var f = t.GetAppDecl();
         var args = t.GetAppArgs().Select(x => SubstRec(memo, x)).ToArray();
         res = ctx.CloneApp(t, args);
     }
     else res = t;
     memo.Add(t, res);
     return res;
 }
Esempio n. 12
0
 private Term MergeGoalsRec(TermDict< Term> memo, Term t)
 {
     Term res;
     if (memo.TryGetValue(t, out res))
         return res;
     var kind = t.GetKind();
     if (kind == TermKind.App)
     {
         var f = t.GetAppDecl();
         var args = t.GetAppArgs();
         if (f.GetKind() == DeclKind.Implies)
         {
             res = ctx.MkImplies(args[0], MergeGoalsRec(memo, args[1]));
             goto done;
         }
         else if (f.GetKind() == DeclKind.And)
         {
             args = args.Select(x => MergeGoalsRec(memo, x)).ToArray();
             res = ctx.MkApp(f, args);
             goto done;
         }
         else if (f.GetKind() == DeclKind.Label)
         {
             var arg = t.GetAppArgs()[0];
             var r = arg.GetAppDecl();
             if (r.GetKind() == DeclKind.Uninterpreted)
             {
                 res = NormalizeGoal(arg, f);
                 goto done;
             }
         }
     }
     res = t;
     done:
     memo.Add(t, res);
     return res;
 }