summaryrefslogtreecommitdiff
path: root/Source/Provers/SMTLib/TypeDeclCollector.cs
blob: eaed83e9184e30468c667d4fd939ef4129009fef (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
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using Microsoft.Boogie.VCExprAST;

namespace Microsoft.Boogie.SMTLib
{
  // Visitor for collecting the occurring function symbols in a VCExpr,
  // and for creating the corresponding declarations

  public class TypeDeclCollector : BoundVarTraversingVCExprVisitor<bool, bool> {

    private readonly UniqueNamer Namer;
    private readonly SMTLibProverOptions Options;
    private HashSet<Function/*!*/>/*!*/ RegisteredRelations = new HashSet<Function>();

    [ContractInvariantMethod]
void ObjectInvariant() 
{
    Contract.Invariant(Namer!=null);
      Contract.Invariant(AllDecls != null);
      Contract.Invariant(IncDecls != null);
      Contract.Invariant(KnownFunctions != null);
      Contract.Invariant(KnownVariables != null);
}


    public TypeDeclCollector(SMTLibProverOptions opts, UniqueNamer namer) {
      Contract.Requires(namer != null);
      this.Namer = namer;
      this.Options = opts;
      InitializeKnownDecls();
    }

    // not used
    protected override bool StandardResult(VCExpr node, bool arg) {
      //Contract.Requires(node != null);
      return true;
    }

    private readonly List<string/*!>!*/> AllDecls = new List<string/*!*/> ();
    private readonly List<string/*!>!*/> IncDecls = new List<string/*!*/> ();

    // In order to support push/pop interface of the theorem prover, the "known" declarations
    // must be kept in a stack

    private HashSet<Function/*!*/>/*!*/ KnownFunctions
    {
        get { return _KnownFunctions.Peek(); }
    }

    private HashSet<VCExprVar/*!*/>/*!*/ KnownVariables
    {
        get { return _KnownVariables.Peek(); }
    }

    private HashSet<Type/*!*/>/*!*/ KnownTypes
    {
        get { return _KnownTypes.Peek(); }
    }

    private HashSet<string/*!*/>/*!*/ KnownStoreFunctions
    {
        get { return _KnownStoreFunctions.Peek(); }
    }

    private HashSet<string/*!*/>/*!*/ KnownSelectFunctions
    {
        get { return _KnownSelectFunctions.Peek(); }
    }

    private HashSet<string> KnownLBL
    {
        get { return _KnownLBL.Peek(); }
    }

    // ------
    private readonly Stack<HashSet<Function/*!*/>/*!*/> _KnownFunctions = new Stack<HashSet<Function/*!*/>>();
    private readonly Stack<HashSet<VCExprVar/*!*/>/*!*/> _KnownVariables = new Stack<HashSet<VCExprVar/*!*/>>();

    private readonly Stack<HashSet<Type/*!*/>/*!*/> _KnownTypes = new Stack<HashSet<Type>>();
    private readonly Stack<HashSet<string/*!*/>/*!*/> _KnownStoreFunctions = new Stack<HashSet<string>>();
    private readonly Stack<HashSet<string/*!*/>/*!*/> _KnownSelectFunctions = new Stack<HashSet<string>>();
    private readonly Stack<HashSet<string>> _KnownLBL = new Stack<HashSet<string>>();
		
	// lets RPFP checker capture decls	
	public abstract class DeclHandler {
		public abstract void VarDecl(VCExprVar v);
		public abstract void FuncDecl(Function f);
	}
		
	private DeclHandler declHandler = null;

	public void SetDeclHandler(DeclHandler _d){
		declHandler = _d;
	}
		
    private void InitializeKnownDecls()
    {
        _KnownFunctions.Push(new HashSet<Function>());
        _KnownVariables.Push(new HashSet<VCExprVar>());
        _KnownTypes.Push(new HashSet<Type>());
        _KnownStoreFunctions.Push(new HashSet<string>());
        _KnownSelectFunctions.Push(new HashSet<string>());
        _KnownLBL.Push(new HashSet<string>());
    }

    public void Reset()
    {
      _KnownFunctions.Clear();
      _KnownVariables.Clear();
      _KnownTypes.Clear();
      _KnownStoreFunctions.Clear();
      _KnownSelectFunctions.Clear();
      _KnownLBL.Clear();
      AllDecls.Clear();
      IncDecls.Clear();
      InitializeKnownDecls();
    }

    public void Push()
    {
        Contract.Assert(_KnownFunctions.Count > 0);
        _KnownFunctions.Push(new HashSet<Function>(_KnownFunctions.Peek()));
        _KnownVariables.Push(new HashSet<VCExprVar>(_KnownVariables.Peek()));
        _KnownTypes.Push(new HashSet<Type>(_KnownTypes.Peek()));
        _KnownStoreFunctions.Push(new HashSet<string>(_KnownStoreFunctions.Peek()));
        _KnownSelectFunctions.Push(new HashSet<string>(_KnownSelectFunctions.Peek()));
        _KnownLBL.Push(new HashSet<string>(_KnownLBL.Peek()));
    }

    public void Pop()
    {
        Contract.Assert(_KnownFunctions.Count > 1);
        _KnownFunctions.Pop();
        _KnownVariables.Pop();
        _KnownTypes.Pop();
        _KnownStoreFunctions.Pop();
        _KnownSelectFunctions.Pop();
        _KnownLBL.Pop();
    }

    public List<string/*!>!*/> AllDeclarations { get {
      Contract.Ensures(cce.NonNullElements(Contract.Result<List<string>>() ));

      List<string>/*!>!*/ res = new List<string/*!*/> ();
      res.AddRange(AllDecls);
      return res;
    } }

    public List<string/*!>!*/> GetNewDeclarations() {
      Contract.Ensures(cce.NonNullElements(Contract.Result<List<string>>() ));
      List<string>/*!>!*/ res = new List<string/*!*/>();
      res.AddRange(IncDecls);
      IncDecls.Clear();
      return res;
    }

    private void AddDeclaration(string decl) {
      Contract.Requires(decl != null);
      AllDecls.Add(decl);
      IncDecls.Add(decl);
    }

    public void Collect(VCExpr expr) {
      Contract.Requires(expr != null);
      Traverse(expr, true);
    }

    ///////////////////////////////////////////////////////////////////////////

    private static string TypeToString(Type t) {
      Contract.Requires(t != null);
      Contract.Ensures(Contract.Result<string>() != null);

      return SMTLibExprLineariser.TypeToString(t);
    }

    public string TypeToStringReg(Type t)
    {
      RegisterType(t);
      return TypeToString(t);
    }

    public void AddFunction(Function func) {
      if (KnownFunctions.Contains(func))
        return;
      KnownFunctions.Add(func);
	  if(declHandler != null)
		declHandler.FuncDecl(func);
    }

    public void RegisterRelation(Function func)
    {
      if (RegisteredRelations.Contains(func))
        return;
      RegisteredRelations.Add(func);
    }

    public override bool Visit(VCExprNAry node, bool arg) {
      Contract.Requires(node != null);

      if (node.Op is VCExprStoreOp) RegisterStore(node);
      else if (node.Op is VCExprSelectOp) RegisterSelect(node);
      else if (node.Op is VCExprSoftOp) {
        var exprVar = node[0] as VCExprVar;
        AddDeclaration(string.Format("(declare-fun {0} () Bool)", exprVar.Name));
        AddDeclaration(string.Format("(assert-soft {0} :weight {1})", exprVar.Name, ((VCExprSoftOp)node.Op).Weight));
      } else if (node.Op.Equals(VCExpressionGenerator.NamedAssumeOp)) {
        var exprVar = node[0] as VCExprVar;
        AddDeclaration(string.Format("(declare-fun {0} () Bool)", exprVar.Name));
        if (CommandLineOptions.Clo.PrintNecessaryAssumes)
        {
          AddDeclaration(string.Format("(assert (! {0} :named {1}))", exprVar.Name, "aux$$" + exprVar.Name));
        }
      } else {
        VCExprBoogieFunctionOp op = node.Op as VCExprBoogieFunctionOp;
        if (op != null && 
          !(op.Func is DatatypeConstructor) && !(op.Func is DatatypeMembership) && !(op.Func is DatatypeSelector) && 
          !KnownFunctions.Contains(op.Func)) {
          Function f = op.Func;
          Contract.Assert(f != null);
          
          var builtin = SMTLibExprLineariser.ExtractBuiltin(f);
          if (builtin == null) {
            string printedName = Namer.GetQuotedName(f, f.Name);
            Contract.Assert(printedName != null);

            Contract.Assert(f.OutParams.Count == 1);
            var argTypes = f.InParams.Cast<Variable>().MapConcat(p => TypeToStringReg(p.TypedIdent.Type), " ");
            string decl;
            if(RegisteredRelations.Contains(op.Func))
                decl = "(declare-rel " + printedName + " (" + argTypes + ") " + ")";
            else
                decl = "(declare-fun " + printedName + " (" + argTypes + ") " + TypeToStringReg(f.OutParams[0].TypedIdent.Type) + ")";
            AddDeclaration(decl);
            if (declHandler != null) declHandler.FuncDecl(f);
          }
          KnownFunctions.Add(f);
        } else {
          var lab = node.Op as VCExprLabelOp;
          if (lab != null && !KnownLBL.Contains(lab.label)) {
            KnownLBL.Add(lab.label);
            var name = SMTLibNamer.QuoteId(SMTLibNamer.LabelVar(lab.label));
            AddDeclaration("(declare-fun " + name + " () Bool)");
          }
        }
      }

      return base.Visit(node, arg);
    }

    public override bool Visit(VCExprVar node, bool arg) {
      Contract.Requires(node != null);
      if (!BoundTermVars.Contains(node) && !KnownVariables.Contains(node)) {
        string printedName = Namer.GetQuotedName(node, node.Name);
        Contract.Assert(printedName!=null);
        RegisterType(node.Type);
        string decl =
          "(declare-fun " + printedName + " () " + TypeToString(node.Type) + ")";
        if (!(printedName.StartsWith("assume$$") || printedName.StartsWith("soft$$") || printedName.StartsWith("try$$")))
        {
          AddDeclaration(decl);
        }
        KnownVariables.Add(node);
		if(declHandler != null)
			declHandler.VarDecl(node);
      }

      return base.Visit(node, arg);
    }

    public override bool Visit(VCExprQuantifier node, bool arg)
    {
      Contract.Requires(node != null);
      foreach (VCExprVar v in node.BoundVars) {
        Contract.Assert(v != null);
        RegisterType(v.Type);
      }

      return base.Visit(node, arg);
    } 

    private void RegisterType(Type type)
    {
      Contract.Requires(type != null);
      if (KnownTypes.Contains(type)) return;

      if (type.IsMap && CommandLineOptions.Clo.MonomorphicArrays) {
        KnownTypes.Add(type);
        MapType mapType = type.AsMap;
        Contract.Assert(mapType != null);

        foreach (Type t in mapType.Arguments) {
          Contract.Assert(t != null);
          RegisterType(t);
        }
        RegisterType(mapType.Result);

        if (!CommandLineOptions.Clo.UseArrayTheory)
          AddDeclaration("(declare-sort " + TypeToString(type) + " 0)");

        return;
      }

      if (type.IsBool || type.IsInt || type.IsReal || type.IsBv)
        return;

      CtorType ctorType = type as CtorType;
      if (ctorType != null && ctorType.IsDatatype())
        return;

      if (CommandLineOptions.Clo.TypeEncodingMethod == CommandLineOptions.TypeEncoding.Monomorphic) {
        AddDeclaration("(declare-sort " + TypeToString(type) + " 0)");
        KnownTypes.Add(type);
        return;
      }
    }  

    private void RegisterSelect(VCExprNAry node)
    {
      RegisterType(node[0].Type);

      if (CommandLineOptions.Clo.UseArrayTheory)
        return;

      string name = SimplifyLikeExprLineariser.SelectOpName(node);
      name = Namer.GetQuotedName(name, name);

      if (!KnownSelectFunctions.Contains(name)) {
        string decl = "(declare-fun " + name + " (" + node.MapConcat(n => TypeToString(n.Type), " ") + ") " + TypeToString(node.Type) + ")";
        AddDeclaration(decl);
        KnownSelectFunctions.Add(name);
      }
    }

    private void RegisterStore(VCExprNAry node)
    {
      RegisterType(node.Type);        // this is the map type, registering it should register also the index and value types

      if (CommandLineOptions.Clo.UseArrayTheory)
        return;

      string name = SimplifyLikeExprLineariser.StoreOpName(node);
      name = Namer.GetQuotedName(name, name);

      if (!KnownStoreFunctions.Contains(name)) {
        string decl = "(declare-fun " + name + " (" + node.MapConcat(n => TypeToString(n.Type), " ") + ") " + TypeToString(node.Type) + ")";
        AddDeclaration(decl);

        if (CommandLineOptions.Clo.MonomorphicArrays) {
          var sel = SimplifyLikeExprLineariser.SelectOpName(node);
          sel = Namer.GetQuotedName(sel, sel);
          
          if (!KnownSelectFunctions.Contains(sel)) {
            // need to declare it before reference
            var args = node.SkipEnd(1);
            var ret = node.Last();
            string seldecl = "(declare-fun " + sel + " (" + args.MapConcat(n => TypeToString(n.Type), " ") + ") " + TypeToString(ret.Type) + ")";
            AddDeclaration(seldecl);
            KnownSelectFunctions.Add(sel);
          }

          string ax1 = "(assert (forall (";
          string ax2 = "(assert (forall (";

          string argX = "", argY = "";
          string dist = "";
          for (int i = 0; i < node.Arity; i++) {
            var t = " " + TypeToString(node[i].Type);
            var x = " ?x" + i;
            var y = " ?y" + i;
            ax1 += " (" + x + t + ")";
            ax2 += " (" + x + t + ")";
            if (i != 0 && i != node.Arity - 1) {
              argX += x;
              argY += y;
              ax2 += " (" + y + t + ")";
              dist += " (not (=" + x + y + "))";
            }
          }
          string v = " ?x" + (node.Arity - 1);
          ax1 += ") (= (" + sel + " (" + name + " ?x0" + argX + v + ")" + argX + ") " + v + ")";
          ax1 += "))";

          if (node.Arity > 3)
            dist = "(or " + dist + ")";
          ax2 += ") (=> " + dist + " (= (" + sel + " (" + name + " ?x0" + argX + v + ")" + argY + ") (" + sel + " ?x0" + argY + ")))";
          ax2 += "))";

          AddDeclaration(ax1);
          AddDeclaration(ax2);
        }

        KnownStoreFunctions.Add(name);
      }
      //
    }

  }

}