summaryrefslogtreecommitdiff
path: root/Source/VCGeneration/RPFP.cs
blob: 9d38eb4734033f1f84468c804a589f073d15fbab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//-----------------------------------------------------------------------------
//
// Copyright (C) 2012 Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Term = Microsoft.Boogie.VCExprAST.VCExpr;
using FuncDecl = Microsoft.Boogie.VCExprAST.VCExprOp;
using Sort = Microsoft.Boogie.Type;
using Microsoft.Boogie.VCExprAST;


using Microsoft.Boogie.ExprExtensions;


namespace Microsoft.Boogie
{

    
    

    /** This class represents a relation post-fixed point (RPFP) problem as
     *  a "problem graph". The graph consists of Nodes and hyper-edges.
     * 
     * A node consists of
     * - Annotation, a symbolic relation
     * - Bound, a symbolic relation giving an upper bound on Annotation
     * 
     *
     * A hyper-edge consists of:
     *  - Children, a sequence of children Nodes,
     *  - F, a symbolic relational transformer,
     *  - Parent, a single parent Node.
     *  
     * The graph is "solved" when:
     * - For every Node n, n.Annotation subseteq n.Bound
     * - For every hyperedge e, e.F(e.Children.Annotation) subseteq e.Parent.Annotation
     * 
     * where, if x is a sequence of Nodes, x.Annotation is the sequences
     * of Annotations of the nodes in the sequence.
     * 
     * A symbolic Transformer consists of
     * - RelParams, a sequence of relational symbols
     * - IndParams, a sequence of individual symbols
     * - Formula, a formula over RelParams and IndParams
     * 
     * A Transformer t represents a function that takes sequence R of relations
     * and yields the relation lambda (t.Indparams). Formula(R/RelParams).
     * 
     * As a special case, a nullary Transformer (where RelParams is the empty sequence)
     * represents a fixed relation.
     * 
     * An RPFP consists of
     * - Nodes, a set of Nodes
     * - Edges, a set of hyper-edges
     * - Context, a prover context that contains formula AST's
     *  
     * Multiple RPFP's can use the same Context, but you should be careful
     * that only one RPFP asserts constraints in the context at any time. 
     * 
     *  */
    public class RPFP
    {
        /** Symbolic representation of a relational transformer */
        public class Transformer
        {
            public FuncDecl[] RelParams;
            public Term[] IndParams;
            public Term Formula;
            public RPFP owner;

            public Transformer Clone()
            {
                return (Transformer)this.MemberwiseClone();
            }
        }

        /** Create a symbolic transformer. */
        public Transformer CreateTransformer(FuncDecl[] _RelParams, Term[] _IndParams, Term _Formula)
            {
                Transformer t = new Transformer();
                t.RelParams = _RelParams;
                t.IndParams = _IndParams;
                t.Formula = _Formula;
                t.owner = this;
                return t;
            }

        /** Create a relation (nullary relational transformer) */
        public Transformer CreateRelation(Term[] _IndParams, Term _Formula)
        {
            return CreateTransformer(new FuncDecl[0], _IndParams, _Formula);
        }

        /** A node in the RPFP graph */
        public class Node
        {
            public FuncDecl Name;
            public Transformer Annotation;
            public Transformer Bound;
            public RPFP owner;
            public int number;
            public Edge Outgoing;
            public List<Edge> Incoming;
            public Term dual;
            public Node map;
        }

        /** Create a node in the graph. The input is a term R(v_1...v_n)
         *  where R is an arbitrary relational symbol and v_1...v_n are
         *  arbitary distinct variables. The names are only of mnemonic value,
         *  however, the number and type of arguments determine the type
         *  of the relation at this node. */

        public Node CreateNode(Term t)
        {
            Node n = new Node();
            // Microsoft.Boogie.VCExprAST.VCExprNAry tn = t as Microsoft.Boogie.VCExprAST.VCExprNAry;
            // Term[] _IndParams = tn.ToArray();
            Term[] _IndParams = t.GetAppArgs();
            FuncDecl Name = t.GetAppDecl();
            n.Annotation = CreateRelation(_IndParams,ctx.MkTrue());
            n.Bound = CreateRelation(_IndParams, ctx.MkTrue());
            n.owner = this;
            n.number = ++nodeCount;
            n.Name = Name; // just to have a unique name
            n.Incoming = new List<Edge>();
            return n;
        }

        /** Clone a node (can be from another graph).  */

        public Node CloneNode(Node old)
        {
            Node n = new Node();
            n.Annotation = old.Annotation.Clone();
            n.Bound = old.Bound.Clone();
            n.owner = this;
            n.number = ++nodeCount;
            n.Name = old.Name; // just to have a unique name
            n.Incoming = new List<Edge>();
            return n;
        }

        /** This class represents a hyper-edge in the RPFP graph */

        public class Edge
        {
            public Transformer F;
            public Node Parent;
            public Node[] Children;
            public RPFP owner;
            public int number;
            public Edge map;
            public HashSet<string> labels;
            internal Term dual;
            internal TermDict<Term> valuation;
        }

        
        /** Create a hyper-edge. */
        public Edge CreateEdge(Node _Parent, Transformer _F, Node[] _Children)
        {
            Edge e = new Edge();
            e.Parent = _Parent;
            e.F = _F;
            e.Children = _Children;
            e.owner = this;
            e.number = ++edgeCount;
            _Parent.Outgoing = e;
            foreach (var c in _Children)
                if(c != null)
                  c.Incoming.Add(e);
            return e;
        }

        /** Create an edge that lower-bounds its parent. */
        public Edge CreateLowerBoundEdge(Node _Parent)
        {
            return CreateEdge(_Parent, _Parent.Annotation, new RPFP.Node[0]);
        }

        
        

        /** Assert a background axiom. Background axioms can be used to provide the
         *  theory of auxilliary functions or relations. All symbols appearing in
         *  background axioms are considered global, and may appear in both transformer
         *  and relational solutions. Semantically, a solution to the RPFP gives
         *  an interpretation of the unknown relations for each interpretation of the
         *  auxilliary symbols that is consistent with the axioms. Axioms should be
         *  asserted before any calls to Push. They cannot be de-asserted by Pop. */

        public void AssertAxiom(Term t)
        {
            ctx.AddAxiom(t);
        }

        /** Do not call this. */

        public void RemoveAxiom(Term t)
        {
            ctx.RemoveAxiom(t);
        }

        /** Type of solve results */
        public enum LBool { False, True, Undef };
        

        /** Solve an RPFP graph. This means either strengthen the annotation
         *  so that the bound at the given root node is satisfied, or
         *  show that this cannot be done by giving a dual solution 
         *  (i.e., a counterexample). 
         *  
         * In the current implementation, this only works for graphs that
         * are:
         * - tree-like
         * 
         * - closed.
         * 
         * In a tree-like graph, every nod has out most one incoming and one out-going edge,
         * and there are no cycles. In a closed graph, every node has exactly one out-going
         * edge. This means that the leaves of the tree are all hyper-edges with no
         * children. Such an edge represents a relation (nullary transformer) and thus
         * a lower bound on its parent. The parameter root must be the root of this tree.
         * 
         * If Solve returns LBool.False, this indicates success. The annotation of the tree
         * has been updated to satisfy the upper bound at the root. 
         * 
         * If Solve returns LBool.True, this indicates a counterexample. For each edge,
         * you can then call Eval to determine the values of symbols in the transformer formula.
         * You can also call Empty on a node to determine if its value in the counterexample
         * is the empty relation.
         * 
         *    \param root The root of the tree
         *    \param persist Number of context pops through which result should persist 
         * 
         * 
        */

        public LBool Solve(Node root, int persist)
        {
            return LBool.False; // TODO
        }


        /** Dispose of the dual model (counterexample) if there is one. */

        public void DisposeDualModel()
        {
            // TODO dualModel = null;
        }


        /** Determines the value in the counterexample of a symbol occuring in the transformer formula of
         *  a given edge. */

        public Term Eval(Edge e, Term t)
        {
            if (e.valuation == null)
                e.valuation = new TermDict< Term>();
            if (e.valuation.ContainsKey(t))
                return e.valuation[t];
            return null; // TODO
        }

        /** Sets the value in the counterexample of a symbol occuring in the transformer formula of
         *  a given edge. */

        public void SetValue(Edge e, Term variable, Term value)
        {
            if (e.valuation == null)
                e.valuation = new TermDict< Term>(); 
            e.valuation.Add(variable, value);
        }


        /** Returns true if the given node is empty in the primal solution. For proecudure summaries,
         this means that the procedure is not called in the current counter-model. */

        public bool Empty(Node p)
        {
            return false; // TODO
        }

        /** Push a scope. Assertions made after Push can be undone by Pop. */

        public void Push()
        {
            stack.Push(new stack_entry());
            // TODO: do we need push/pop?
        }

        /** Pop a scope (see Push). Note, you cannot pop axioms. */

        public void Pop(int num_scopes)
        {
            //TODO ctx.Pop((uint)num_scopes);
            for (uint i = 0; i < num_scopes; i++)
            {
                stack_entry back = stack.Pop();
                foreach (var e in back.edges)
                    e.dual = null;
                foreach (var n in back.nodes)
                    n.dual = null;
            }
        }

        public Context ctx;

        public class LogicSolver  {
            public Context ctx;
        };

        public LogicSolver solver;

        static public LogicSolver CreateLogicSolver(Context _ctx){
            LogicSolver res = new LogicSolver();
            res.ctx = _ctx;
            return res;
        }

        /** This represents a conjecture that a given node is upper-boudned
            by bound. */
        public class Conjecture
        {
            public Node node;
            public Transformer bound;
        }

        /** This is a list of conjectures generated during solving. */

        public List<Conjecture> conjectures = new List<Conjecture>();

        /** Construct an RPFP graph with a given interpolating prover context. It is allowed to
            have multiple RPFP's use the same context, but you should never have teo RPFP's
            with the same conext asserting nodes or edges at the same time. Note, if you create
            axioms in one RPFP, them create a second RPFP with the same context, the second will
            inherit the axioms. 
         */

        public RPFP(LogicSolver slvr)
        {
            solver = slvr;
            ctx = slvr.ctx;
            stack = new Stack<stack_entry>();
            stack.Push(new stack_entry());
        }


        /** Convert an array of clauses to an RPFP. 
         */

        public void FromClauses(Term[] clauses){
            FuncDecl failName = ctx.MkFuncDecl("@Fail", ctx.MkBoolSort());
            foreach(var clause in clauses){
                Node foo = GetNodeFromClause(clause,failName);
                if(foo != null)
                    nodes.Add(foo);
            }
            foreach (var clause in clauses)
                edges.Add(GetEdgeFromClause(clause,failName));
        }

        
        // This returns a new FuncDel with same sort as top-level function
        // of term t, but with numeric suffix appended to name.

        private FuncDecl SuffixFuncDecl(Term t, int n)
        {
            var name = t.GetAppDecl().GetDeclName() + "_" + n.ToString();
            return ctx.MkFuncDecl(name, t.GetAppDecl());
        }

        // Collect the relational paremeters

        Dictionary<FuncDecl, Node> relationToNode = new Dictionary<FuncDecl, Node>();

        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;
        }

        private bool IsVariable(Term t)
        {
            // TODO: is this right?
            // return t.IsFunctionApp() && t.GetAppArgs().Length == 0;
            return t is VCExprVar && !(t is VCExprConstant);
        }

        private Edge GetEdgeFromClause(Term t, FuncDecl failName)
        {
            Term[] args = t.GetAppArgs();
            Term body = args[0];
            Term head = args[1];
            Term[] _IndParams;
            FuncDecl Name;
            if (head.IsFalse())
            {
                Name = failName;
                _IndParams = new Term[0];
            }
            else
            {
                _IndParams = head.GetAppArgs();
                Name = head.GetAppDecl();
            }
            for(int i = 0; i < _IndParams.Length; i++)
                if (!IsVariable(_IndParams[i]))
                {
                    Term v = ctx.MkConst("@a" + i.ToString(), _IndParams[i].GetSort());
                    body = ctx.MkAnd(body, ctx.MkEq(v, _IndParams[i]));
                    _IndParams[i] = v;
                }
            var relParams = new List<FuncDecl>();
            var nodeParams = new List<RPFP.Node>();
            var memo = new TermDict< Term>();
            var done = new Dictionary<Term, Term>(); // note this hashes on equality, not reference!
            body = CollectParamsRec(memo, body, relParams, nodeParams,done);
            Transformer F = CreateTransformer(relParams.ToArray(), _IndParams, body);
            Node parent = relationToNode[Name];
            return CreateEdge(parent, F, nodeParams.ToArray());
        }

        private Node GetNodeFromClause(Term t, FuncDecl failName)
        {
            Term[] args = t.GetAppArgs();
            Term body = args[0];
            Term head = args[1];
            FuncDecl Name;
            Term[] _IndParams;
            bool is_query = false;
            if (head.Equals(ctx.MkFalse()))
            {
                Name = failName;
                is_query = true;
                _IndParams = new Term[0];
            }
            else
            {
                Name = head.GetAppDecl();
                _IndParams = head.GetAppArgs();
            }
            if (relationToNode.ContainsKey(Name))
                return null;
            for (int i = 0; i < _IndParams.Length; i++)
                if (!IsVariable(_IndParams[i]))
                {
                    Term v = ctx.MkConst("@a" + i.ToString(), _IndParams[i].GetSort());
                    _IndParams[i] = v;
                }
            Term foo = ctx.MkApp(Name, _IndParams);
            Node node = CreateNode(foo);
            relationToNode[Name] = node;
            if (is_query)
                node.Bound = CreateRelation(new Term[0], ctx.MkFalse());
            return node;
        }

        /////////////////////////////////////////////////////////////////////////////////////////
        // Convert RPFP to Z3 rules
        /////////////////////////////////////////////////////////////////////////////////////////

        /** Get the Z3 rule corresponding to an edge */
         
        public Term GetRule(Edge edge)
        {
            Dictionary<FuncDecl, FuncDecl> predSubst = new Dictionary<FuncDecl, FuncDecl>();
            for (int i = 0; i < edge.Children.Length; i++)
                predSubst.Add(edge.F.RelParams[i], edge.Children[i].Name);
            Term body = SubstPreds(predSubst, edge.F.Formula);
            Term head = ctx.MkApp(edge.Parent.Name, edge.F.IndParams);
            var rule = BindVariables(ctx.MkImplies(body, head));
            rule = ctx.Letify(rule); // put in let bindings for theorem prover
            return rule;
        }

        /** Get the Z3 query corresponding to the conjunction of the node bounds. */

        public Term GetQuery()
        {
            List<Term> conjuncts = new List<Term>();
            foreach (var node in nodes)
            {
                if (node.Bound.Formula != ctx.MkTrue())
                    conjuncts.Add(ctx.MkImplies(ctx.MkApp(node.Name, node.Bound.IndParams), node.Bound.Formula));
            }
            Term query = ctx.MkNot(ctx.MkAnd(conjuncts.ToArray()));
            query = BindVariables(query,false); // bind variables existentially
            query = ctx.Letify(query); // put in let bindings for theorem prover
            return query;
        }

        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);
        }

        private Term BindVariables(Term t, bool universal = true)
        {
            TermDict< bool> memo = new TermDict<bool>();
            List<Term> vars = new List<Term>();
            CollectVariables(memo,t,vars);
            return universal ? ctx.MkForall(vars.ToArray(), t) : ctx.MkExists(vars.ToArray(), t);
        }

        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;
        }

        private Term SubstPreds(Dictionary<FuncDecl, FuncDecl> subst, Term t)
        {
            TermDict< Term> memo = new TermDict< Term>();
            return SubstPredsRec(memo, subst, t);
        }

        /* Everything after here is private. */

        private class stack_entry
        {
            public List<Edge> edges = new List<Edge>();
            public List<Node> nodes = new List<Node>();
        };

        /** Set the model of the background theory used in a counterexample. */
        public void SetBackgroundModel(Model m)
        {
            dualModel = m;
        }

        /** Set the model of the background theory used in a counterexample. */
        public Model GetBackgroundModel()
        {
            return dualModel;
        }

        private int nodeCount = 0;
        private int edgeCount = 0;
        private Model dualModel;
        // private LabeledLiterals dualLabels;
        private Stack<stack_entry> stack;
        public List<Node> nodes = new List<Node>();
        public List<Edge> edges = new List<Edge>();

       
    }
}