summaryrefslogtreecommitdiff
path: root/Source/VCExpr/Clustering.cs
blob: 1ab101072cd7b932a51df35b06913d68e308d19f (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
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Text;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.Basetypes;
using Microsoft.Boogie.VCExprAST;

// Code for managing and clusterings sets of terms; this is used to
// compress the input given to the theorem prover

namespace Microsoft.Boogie.Clustering {
  using Microsoft.Boogie.VCExprAST;


  public class SubtermCollector : BoundVarTraversingVCExprVisitor<bool, bool> {

    private readonly VCExpressionGenerator/*!*/ Gen;
    [ContractInvariantMethod]
    void ObjectInvariant() {
      Contract.Invariant(Gen != null);
      Contract.Invariant(cce.NonNullDictionaryAndValues(GlobalVariables));
      Contract.Invariant(cce.NonNullDictionaryAndValues(SubtermClusters));
    }


    public SubtermCollector(VCExpressionGenerator gen) {
      Contract.Requires(gen != null);
      Gen = gen;
    }

    // variables that are global and treated like constants
    private readonly IDictionary<VCExprVar/*!*/, VCExprVar/*!*/> GlobalVariables = new Dictionary<VCExprVar/*!*/, VCExprVar/*!*/>();

    private readonly IDictionary<VCExprOp/*!*/, TermClustersSameHead/*!*/> SubtermClusters =
      new Dictionary<VCExprOp/*!*/, TermClustersSameHead/*!*/>();

    public void UnifyClusters() {
      foreach (KeyValuePair<VCExprOp/*!*/, TermClustersSameHead/*!*/> pair
                 in SubtermClusters) {
        Contract.Assert(cce.NonNullElements(pair));
        pair.Value.UnifyClusters();
      }
    }

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

    protected override bool StandardResult(VCExpr node, bool arg) {
      //Contract.Requires(node != null);
      return false;   // by default, do not collect terms containing node
    }

    public override bool Visit(VCExprLiteral node, bool arg) {
      Contract.Requires(node != null);
      return true;
    }

    public override bool Visit(VCExprNAry node, bool arg) {
      Contract.Requires(node != null);
      VCExprBoogieFunctionOp op = node.Op as VCExprBoogieFunctionOp;
      if (op == null) {
        base.Visit(node, arg);
        return false;
      }

      bool res = true;
      foreach (VCExpr subexpr in node) {
        Contract.Assert(subexpr != null);
        res &= this.Traverse(subexpr, arg);
      }

      if (res) {
        TermClustersSameHead clusters;
        if (!SubtermClusters.TryGetValue(op, out clusters)) {
          clusters = new TermClustersSameHead(op, GlobalVariables, Gen);
          SubtermClusters.Add(op, clusters);
        }
        cce.NonNull(clusters).AddExpr(node);
      }

      return res;
    }

    public override bool Visit(VCExprVar node, bool arg) {
      Contract.Requires(node != null);
      if (!BoundTermVars.Contains(node))
        GlobalVariables[node] = node;
      return true;
    }

    [Pure]
    public override string ToString() {
      Contract.Ensures(Contract.Result<string>() != null);
      string/*!*/ res = "";
      foreach (KeyValuePair<VCExprOp/*!*/, TermClustersSameHead/*!*/> pair
                 in SubtermClusters) {
        Contract.Assert(cce.NonNullElements(pair));
        res = res + pair.Value + "\n";
      }
      return res;
    }
  }

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

  // Class for managing and clustering a set of terms that all start
  // with the same function symbol
  internal class TermClustersSameHead {

    [ContractInvariantMethod]
    void ObjectInvariant() {
      Contract.Invariant(Op != null);
      Contract.Invariant(Gen != null);
      Contract.Invariant(cce.NonNullDictionaryAndValues(GlobalVariables));
    }
    // variables that are global and treated like constants
    private readonly IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ GlobalVariables;
    public readonly VCExprOp/*!*/ Op;
    private readonly VCExpressionGenerator/*!*/ Gen;

    public TermClustersSameHead(VCExprOp op, IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ globalVars, VCExpressionGenerator/*!*/ gen) {
      Contract.Requires(cce.NonNullDictionaryAndValues(globalVars));
      Contract.Requires(gen != null);
      Contract.Requires(op != null);
      Op = op;
      GlobalVariables = globalVars;
      Gen = gen;
    }

    private readonly List<Cluster>/*!*/ Clusters = new List<Cluster>();

    private struct Cluster {
      public readonly VCExprNAry/*!*/ Generator;
      public readonly int Size;
      [ContractInvariantMethod]
      void ObjectInvariant() {
        Contract.Invariant(Generator != null);
      }

      public Cluster(VCExprNAry generator, int size) {
        Contract.Requires(generator != null);
        Generator = generator;
        Size = size;
      }
    }

    private int Distance(Cluster a, Cluster b) {
      AntiUnificationVisitor/*!*/ visitor = new AntiUnificationVisitor(Gen);
      visitor.AntiUnify(a.Generator, b.Generator);

      int reprSizeA, reprSizeB;
      visitor.RepresentationSize(GlobalVariables, out reprSizeA, out reprSizeB);
      return (a.Size - 1) * reprSizeA + (b.Size - 1) * reprSizeB;
    }

    private bool EqualUpToRenaming(Cluster a, Cluster b) {
      AntiUnificationVisitor/*!*/ visitor = new AntiUnificationVisitor(Gen);
      visitor.AntiUnify(a.Generator, b.Generator);
      return visitor.RepresentationIsRenaming(GlobalVariables);
    }

    private Cluster Merge(Cluster a, Cluster b) {
      AntiUnificationVisitor/*!*/ visitor = new AntiUnificationVisitor(Gen);
      VCExpr/*!*/ generator = visitor.AntiUnify(a.Generator, b.Generator);
      Contract.Assert(generator != null);
      VCExprNAry generatorNAry = generator as VCExprNAry;
      Contract.Assert(generatorNAry != null && Op.Equals(generatorNAry.Op));
      return new Cluster(generatorNAry, a.Size + b.Size);
    }

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

    public void AddExpr(VCExprNAry expr) {
      Contract.Requires(expr != null);
      Contract.Requires(Op.Equals(expr.Op));

      Cluster c = new Cluster(expr, 1);
      for (int i = 0; i < Clusters.Count; ++i) {
        Cluster d = Clusters[i];
        if (EqualUpToRenaming(c, d)) {
          Clusters[i] = new Cluster(d.Generator, d.Size + 1);
          return;
        }
      }

      Clusters.Add(c);
    }

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

    private struct ClusteringMatrix {

      private readonly VCExpressionGenerator/*!*/ Gen;
      private readonly IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ GlobalVariables;

      public readonly List<Cluster>/*!*/ Clusters;
      public readonly bool[]/*!*/ RemainingClusters;

      public readonly Distance[,]/*!*/ Distances;

      [ContractInvariantMethod]
      void ObjectInvariant() {
        Contract.Invariant(Gen != null);
        Contract.Invariant(cce.NonNullDictionaryAndValues(GlobalVariables));
        Contract.Invariant(Clusters != null);
        Contract.Invariant(RemainingClusters != null);
        Contract.Invariant(Distances != null);
      }


      public struct Distance {
        public readonly int Dist;
        public readonly VCExprNAry/*!*/ Generator;

        public Distance(Cluster a, Cluster b, IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ globalVars, VCExpressionGenerator gen) {
          Contract.Requires(gen != null);
          Contract.Requires(cce.NonNullDictionaryAndValues(globalVars));
          AntiUnificationVisitor/*!*/ visitor = new AntiUnificationVisitor(gen);
          Generator = (VCExprNAry)visitor.AntiUnify(a.Generator, b.Generator);

          int reprSizeA, reprSizeB;
          visitor.RepresentationSize(globalVars, out reprSizeA, out reprSizeB);
          Dist = (a.Size - 1) * reprSizeA + (b.Size - 1) * reprSizeB;
        }
      }

      public ClusteringMatrix(List<Cluster> clusters, IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ globalVars, VCExpressionGenerator gen) {
        Contract.Requires(gen != null);
        Contract.Requires(clusters != null);
        Contract.Requires(cce.NonNullDictionaryAndValues(globalVars));
        List<Cluster> c = new List<Cluster>();
        c.AddRange(clusters);
        Clusters = c;

        GlobalVariables = globalVars;
        Gen = gen;

        bool[] remaining = new bool[clusters.Count];
        RemainingClusters = remaining;
        for (int i = 0; i < remaining.Length; ++i)
          remaining[i] = true;

        Distance[,]/*!*/ distances = new Distance[clusters.Count, clusters.Count];
        Distances = distances;
        for (int i = 1; i < clusters.Count; ++i)
          for (int j = 0; j < i; ++j)
            distances[i, j] =
              new Distance(clusters[i], clusters[j], GlobalVariables, Gen);
      }

      public void UnifyClusters(int maxDist) {
        while (true) {
          int i, j;
          int minDist = FindMinDistance(out i, out j);

          if (minDist > maxDist)
            return;

          MergeClusters(i, j);
        }
      }

      public void ResultingClusters(List<Cluster> clusters) {
        Contract.Requires(clusters != null);
        clusters.Clear();
        for (int i = 0; i < Clusters.Count; ++i)
          if (RemainingClusters[i])
            clusters.Add(Clusters[i]);
      }

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

      private void Update(int i) {
        for (int j = 0; j < i; ++j) {
          if (RemainingClusters[j])
            Distances[i, j] =
              new Distance(Clusters[i], Clusters[j], GlobalVariables, Gen);
        }
        for (int j = i + 1; j < Clusters.Count; ++j) {
          if (RemainingClusters[j])
            Distances[j, i] =
              new Distance(Clusters[j], Clusters[i], GlobalVariables, Gen);
        }
      }

      private int FindMinDistance(out int c0, out int c1) {
        int minDist = int.MaxValue;
        c0 = -1;
        c1 = -1;

        for (int i = 0; i < Clusters.Count; ++i)
          if (RemainingClusters[i]) {
            for (int j = 0; j < i; ++j)
              if (RemainingClusters[j]) {
                if (Distances[i, j].Dist < minDist) {
                  minDist = Distances[i, j].Dist;
                  c0 = i;
                  c1 = j;
                }
              }
          }

        Contract.Assert(c0 == -1 && c1 == -1 || c0 > c1 && c1 >= 0);
        return minDist;
      }

      private void MergeClusters(int i, int j) {
        Contract.Requires(j >= 0 && i > j && RemainingClusters[i] && RemainingClusters[j]);
        Clusters[i] = new Cluster(Distances[i, j].Generator,
                                   Clusters[i].Size + Clusters[j].Size);
        RemainingClusters[j] = false;
        Update(i);
      }
    }

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

    public void UnifyClusters() {
      ClusteringMatrix matrix =
        new ClusteringMatrix(Clusters, GlobalVariables, Gen);
      matrix.UnifyClusters(50);
      matrix.ResultingClusters(Clusters);
    }

    [Pure]
    public override string ToString() {
      Contract.Ensures(Contract.Result<string>() != null);
      string/*!*/ res = "";
      foreach (Cluster c in Clusters)
        res = res + c.Generator + "\t" + c.Size + "\n";
      return res;
    }
  }

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

  internal class AntiUnificationVisitor : TraversingVCExprVisitor<VCExpr/*!*/, VCExpr/*!*/> {

    private readonly VCExpressionGenerator/*!*/ Gen;
    [ContractInvariantMethod]
    void ObjectInvariant() {
      Contract.Invariant(Gen != null);
      Contract.Invariant(cce.NonNullDictionaryAndValues(Representation));
    }


    public AntiUnificationVisitor(VCExpressionGenerator gen) {
      Contract.Requires(gen != null);
      Gen = gen;
    }

    // Sub-expressions in the first and second expression to be
    // anti-unified that are replaced with variables
    private readonly IDictionary<ExprPair, VCExprVar/*!*/>/*!*/ Representation =
      new Dictionary<ExprPair, VCExprVar/*!*/>();



    private struct ExprPair {
      public readonly VCExpr/*!*/ Expr0, Expr1;
      [ContractInvariantMethod]
      void ObjectInvariant() {
        Contract.Invariant(Expr0 != null);
        Contract.Invariant(Expr1 != null);
      }

      public ExprPair(VCExpr expr0, VCExpr expr1) {
        Contract.Requires(expr1 != null);
        Contract.Requires(expr0 != null);
        Expr0 = expr0;
        Expr1 = expr1;
      }
      [Pure]
      [Reads(ReadsAttribute.Reads.Nothing)]
      public override bool Equals(object that) {
        if (that is ExprPair) {
          ExprPair thatPair = (ExprPair)that;
          return this.Expr0.Equals(thatPair.Expr0) &&
                 this.Expr1.Equals(thatPair.Expr1);
        }
        return false;
      }
      [Pure]
      public override int GetHashCode() {
        return Expr0.GetHashCode() + Expr1.GetHashCode() * 13;
      }
    }

    public void Reset() {
      Representation.Clear();
    }

    public bool RepresentationIsRenaming(IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ globalVars) {
      Contract.Requires(cce.NonNullDictionaryAndValues(globalVars));
      if (!Representation.Any(pair => pair.Key.Expr0 is VCExprVar && pair.Key.Expr1 is VCExprVar && !globalVars.ContainsKey(cce.NonNull((VCExprVar)pair.Key.Expr0)) && !globalVars.ContainsKey(cce.NonNull((VCExprVar/*!*/)pair.Key.Expr1))))
        return false;
      // check that all substituted variables are distinct
      // TODO: optimise
      return
        Representation.All(pair1 => Representation.All(pair2 => pair1.Value.Equals(pair2.Value) || !pair1.Key.Expr0.Equals(pair2.Key.Expr0) && !pair1.Key.Expr1.Equals(pair2.Key.Expr1)));
    }

    public void RepresentationSize(IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ globalVars, out int expr0Size, out int expr1Size) {
      Contract.Requires(cce.NonNullDictionaryAndValues(globalVars));
      ReprSizeComputingVisitor/*!*/ size0Visitor = new ReprSizeComputingVisitor();
      ReprSizeComputingVisitor/*!*/ size1Visitor = new ReprSizeComputingVisitor();

      foreach (KeyValuePair<ExprPair, VCExprVar/*!*/> pair in Representation) {
        Contract.Assert(pair.Value != null);
        size0Visitor.ComputeSize(pair.Key.Expr0, globalVars);
        size1Visitor.ComputeSize(pair.Key.Expr1, globalVars);
      }

      expr0Size = size0Visitor.Size;
      expr1Size = size1Visitor.Size;
    }

    public VCExpr AntiUnify(VCExpr s, VCExpr t) {
      Contract.Requires(t != null);
      Contract.Requires(s != null);
      Contract.Requires((s.Type.Equals(t.Type)));
Contract.Ensures(Contract.Result<VCExpr>() != null);
      return Traverse(s, t);
    }

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

    private VCExprVar AbstractWithVariable(VCExpr s, VCExpr t) {
      Contract.Requires(t != null);
      Contract.Requires(s != null);
      Contract.Requires((s.Type.Equals(t.Type)));
Contract.Ensures(Contract.Result<VCExprVar>() != null);

      ExprPair pair = new ExprPair(s, t);
      VCExprVar repr;
      if (!Representation.TryGetValue(pair, out repr)) {
        repr = Gen.Variable("abs" + Representation.Count, s.Type);
        Representation.Add(pair, repr);
      }
      return cce.NonNull(repr);
    }

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

    public override VCExpr Visit(VCExprLiteral node, VCExpr that) {
      Contract.Requires(that != null);
      Contract.Requires(node != null);
      Contract.Ensures(Contract.Result<VCExpr>() != null);
      if (node.Equals(that))
        return node;
      return AbstractWithVariable(node, that);
    }

    public override VCExpr Visit(VCExprNAry node, VCExpr that) {
      Contract.Requires(that != null);
      Contract.Requires(node != null);
      Contract.Ensures(Contract.Result<VCExpr>() != null);
      VCExprNAry thatNAry = that as VCExprNAry;
      if (thatNAry != null && node.Op.Equals(thatNAry.Op)) {
        // type parameters should already have been eliminated at this
        // stage
        Contract.Assert(node.TypeParamArity == 0 && thatNAry.TypeParamArity == 0 && node.Arity == thatNAry.Arity);

        List<VCExpr/*!*/>/*!*/ unifiedArgs = new List<VCExpr/*!*/>();
        for (int i = 0; i < node.Arity; ++i)
          unifiedArgs.Add(Traverse(node[i], thatNAry[i]));

        return Gen.Function(node.Op, unifiedArgs);
      }
      return AbstractWithVariable(node, that);
    }

    public override VCExpr Visit(VCExprVar node, VCExpr that) {
      Contract.Requires(that != null);
      Contract.Requires(node != null);
      Contract.Ensures(Contract.Result<VCExpr>() != null);
      if (node.Equals(that))
        return node;
      return AbstractWithVariable(node, that);
    }

    protected override VCExpr StandardResult(VCExpr node, VCExpr that) {
      //Contract.Requires(that != null);
      //Contract.Requires(node != null);
      Contract.Ensures(Contract.Result<VCExpr>() != null);
      Contract.Assert(false);
      throw new cce.UnreachableException(); // not handled here      
    }
  }

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

  internal class ReprSizeComputingVisitor
                 : TraversingVCExprVisitor<bool,
    // variables considered as global constants
                                           IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/> {

    public int Size = 0;

    public void ComputeSize(VCExpr expr, IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ globalVars) {
      Contract.Requires(expr != null);
      Contract.Requires(cce.NonNullDictionaryAndValues(globalVars));
      Traverse(expr, globalVars);
    }

    protected override bool StandardResult(VCExpr node, IDictionary<VCExprVar/*!*/, VCExprVar/*!*/>/*!*/ globalVars) {
      //Contract.Requires(node != null);
      //Contract.Requires(cce.NonNullElements(globalVars));
      VCExprVar nodeAsVar = node as VCExprVar;
      if (nodeAsVar == null || globalVars.ContainsKey(nodeAsVar))
        Size = Size + 1;
      return true;
    }
  }
}