summaryrefslogtreecommitdiff
path: root/Source/AIFramework/Lattice.cs
blob: ab10be9ae4850b3d9d08f9c907bfa83a4ecc4ab7 (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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
namespace Microsoft.AbstractInterpretationFramework {
  using System;
  using System.Diagnostics.Contracts;
  using System.Collections;
  using G = System.Collections.Generic;
  using System.Diagnostics;
  using Microsoft.AbstractInterpretationFramework.Collections;
  using Microsoft.Boogie;

  using ArraySet = Microsoft.Boogie.GSet<object>;
  using IMutableSet = Microsoft.Boogie.GSet<object>; 
  using HashSet = Microsoft.Boogie.GSet<object>;
  using ISet = Microsoft.Boogie.GSet<object>;
  using Set = Microsoft.Boogie.GSet<object>;


  /// <summary>
  ///  Specifies the operations (e.g., join) on a mathematical lattice that depend
  ///  only on the elements of the lattice.
  /// </summary>
  [ContractClass(typeof(MathematicalLatticeContracts))]
  public abstract class MathematicalLattice {
    #region Element
    /// <summary>
    ///  An element of the lattice.  This class should be derived from in any
    ///  implementation of MathematicalLattice.
    /// </summary>
    [ContractClass(typeof(ElementContracts))]
    public abstract class Element : System.ICloneable {
      /// <summary>
      /// Print out a debug-useful representation of the internal data structure of the lattice element.
      /// </summary>
      public virtual void Dump(string/*!*/ msg) {
        Contract.Requires(msg != null);
        System.Console.WriteLine("Dump({0}) = {1}", msg, this);
      }

      public abstract Element/*!*/ Clone();
      object/*!*/ System.ICloneable.Clone() {
        return this.Clone();
      }

      public abstract G.ICollection<IVariable/*!*/>/*!*/ FreeVariables();

    }
    [ContractClassFor(typeof(Element))]
    public abstract class ElementContracts : Element {
      public override Element Clone() {
        Contract.Ensures(Contract.Result<Element>() != null);
        throw new NotImplementedException();

      }

      public override System.Collections.Generic.ICollection<IVariable> FreeVariables() {
        Contract.Ensures(cce.NonNullElements(Contract.Result<G.ICollection<IVariable>>()));
        Contract.Ensures(Contract.Result<System.Collections.Generic.ICollection<IVariable>>().IsReadOnly);
        throw new System.NotImplementedException();
      }
    }
    #endregion

    public abstract Element/*!*/ Top {
      get;
    }
    public abstract Element/*!*/ Bottom {
      get;
    }

    public abstract bool IsTop(Element/*!*/ e);
    public abstract bool IsBottom(Element/*!*/ e);

    /// <summary>
    /// Returns true if a &lt;= this.
    /// </summary>
    protected abstract bool AtMost(Element/*!*/ a, Element/*!*/ b);
    /* The following cases are handled elsewhere and need not be considered in subclass. */
    //  requires a.GetType() == b.GetType();
    //  requires ! a.IsTop;
    //  requires ! a.IsBottom;
    //  requires ! b.IsTop;
    //  requires ! b.IsBottom;


    protected Answer TrivialLowerThan(Element/*!*/ a, Element/*!*/ b) {
      Contract.Requires(b != null);
      Contract.Requires(a != null);
      if (a.GetType() != b.GetType()) {
        throw new System.InvalidOperationException(
          "operands to <= must be of same Element type"
          );
      }
      if (IsBottom(a)) {
        return Answer.Yes;
      }
      if (IsTop(b)) {
        return Answer.Yes;
      }
      if (IsTop(a)) {
        return Answer.No;
      }
      if (IsBottom(b)) {
        return Answer.No;
      }

      return Answer.Maybe;
    }

    // Is 'a' better information than 'b'?
    //
    public bool LowerThan(Element/*!*/ a, Element/*!*/ b) {
      Contract.Requires(b != null);
      Contract.Requires(a != null);
      Answer ans = TrivialLowerThan(a, b);
      return ans != Answer.Maybe ? ans == Answer.Yes : AtMost(a, b);
    }

    // Is 'a' worse information than 'b'?
    //
    public bool HigherThan(Element/*!*/ a, Element/*!*/ b) {
      Contract.Requires(b != null);
      Contract.Requires(a != null);
      return LowerThan(b, a);
    }

    // Are 'a' and 'b' equivalent?
    //
    public bool Equivalent(Element/*!*/ a, Element/*!*/ b) {
      Contract.Requires(b != null);
      Contract.Requires(a != null);
      return LowerThan(a, b) && LowerThan(b, a);
    }

    public abstract Element/*!*/ NontrivialJoin(Element/*!*/ a, Element/*!*/ b);
    /* The following cases are handled elsewhere and need not be considered in subclass. */
    //  requires a.GetType() == b.GetType();
    //  requires ! a.IsTop;
    //  requires ! a.IsBottom;
    //  requires ! b.IsTop;
    //  requires ! b.IsBottom;


    protected Element/*?*/ TrivialJoin(Element/*!*/ a, Element/*!*/ b) {
      Contract.Requires(b != null);
      Contract.Requires(a != null);
      if (a.GetType() != b.GetType()) {
        throw new System.InvalidOperationException(
          "operands to Join must be of same Lattice.Element type"
          );
      }
      if (IsTop(a)) {
        return a;
      }
      if (IsTop(b)) {
        return b;
      }
      if (IsBottom(a)) {
        return b;
      }
      if (IsBottom(b)) {
        return a;
      }

      return null;
    }

    public Element/*!*/ Join(Element/*!*/ a, Element/*!*/ b) {
      Contract.Requires(b != null);
      Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      Element/*?*/ r = TrivialJoin(a, b);
      return r != null ? r : NontrivialJoin(a, b);
    }

    public abstract Element/*!*/ NontrivialMeet(Element/*!*/ a, Element/*!*/ b)
      /* The following cases are handled elsewhere and need not be considered in subclass. */
      //  requires a.GetType() == b.GetType();
      //  requires ! a.IsTop;
      //  requires ! a.IsBottom;
      //  requires ! b.IsTop;
      //  requires ! b.IsBottom;
    ;

    protected Element/*?*/ TrivialMeet(Element/*!*/ a, Element/*!*/ b) {
      Contract.Requires(b != null);
      Contract.Requires(a != null);
      if (a.GetType() != b.GetType()) {
        throw new System.InvalidOperationException(
          "operands to Meet must be of same Lattice.Element type"
          );
      }
      if (IsTop(a)) {
        return b;
      }
      if (IsTop(b)) {
        return a;
      }
      if (IsBottom(a)) {
        return a;
      }
      if (IsBottom(b)) {
        return b;
      }

      return null;
    }

    public Element/*!*/ Meet(Element/*!*/ a, Element/*!*/ b) {
      Contract.Requires(b != null);
      Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      Element/*?*/ r = TrivialMeet(a, b);
      return r != null ? r : NontrivialMeet(a, b);
    }

    public abstract Element/*!*/ Widen(Element/*!*/ a, Element/*!*/ b);

    public virtual void Validate() {
      Debug.Assert(IsTop(Top));
      Debug.Assert(IsBottom(Bottom));
      Debug.Assert(!IsBottom(Top));
      Debug.Assert(!IsTop(Bottom));

      Debug.Assert(LowerThan(Top, Top));
      Debug.Assert(LowerThan(Bottom, Top));
      Debug.Assert(LowerThan(Bottom, Bottom));

      Debug.Assert(IsTop(Join(Top, Top)));
      Debug.Assert(IsBottom(Join(Bottom, Bottom)));
    }
  }
  [ContractClassFor(typeof(MathematicalLattice))]
  public abstract class MathematicalLatticeContracts : MathematicalLattice {
    public override MathematicalLattice.Element Top {
      get {
        Contract.Ensures(Contract.Result<Element>() != null);
        throw new NotImplementedException();
      }
    }

    public override MathematicalLattice.Element Bottom {
      get {
        Contract.Ensures(Contract.Result<Element>() != null);
        throw new NotImplementedException();
      }
    }

    public override bool IsTop(MathematicalLattice.Element e) {
      Contract.Requires(e != null);
      throw new NotImplementedException();
    }

    public override bool IsBottom(MathematicalLattice.Element e) {
      Contract.Requires(e != null);
      throw new NotImplementedException();
    }

    protected override bool AtMost(MathematicalLattice.Element a, MathematicalLattice.Element b) {
      Contract.Requires(a != null);
      Contract.Requires(b != null);
      throw new NotImplementedException();
    }

    public override MathematicalLattice.Element NontrivialJoin(MathematicalLattice.Element a, MathematicalLattice.Element b) {
      Contract.Requires(a != null);
      Contract.Requires(b != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      throw new NotImplementedException();
    }

    public override MathematicalLattice.Element NontrivialMeet(MathematicalLattice.Element a, MathematicalLattice.Element b) {
      Contract.Requires(a != null);
      Contract.Requires(b != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      throw new NotImplementedException();
    }

    public override MathematicalLattice.Element Widen(MathematicalLattice.Element a, MathematicalLattice.Element b) {
      Contract.Requires(a != null);
      Contract.Requires(b != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      throw new NotImplementedException();
    }
  }


  /// <summary>
  ///  Provides an abstract interface for the operations of a lattice specific
  ///  to abstract interpretation (i.e., that deals with the expression language).
  /// </summary>
  [ContractClass(typeof(LatticeContracts))]
  public abstract class Lattice : MathematicalLattice {
    internal readonly IValueExprFactory/*!*/ valueExprFactory;
    [ContractInvariantMethod]
    void ObjectInvariant() {
      Contract.Invariant(valueExprFactory != null);
    }


    public Lattice(IValueExprFactory/*!*/ valueExprFactory) {
      Contract.Requires(valueExprFactory != null);
      this.valueExprFactory = valueExprFactory;
      // base();
    }

    #region Primitives that commands translate into

    public abstract Element/*!*/ Eliminate(Element/*!*/ e, IVariable/*!*/ variable);

    public abstract Element/*!*/ Rename(Element/*!*/ e, IVariable/*!*/ oldName, IVariable/*!*/ newName);

    public abstract Element/*!*/ Constrain(Element/*!*/ e, IExpr/*!*/ expr);

    #endregion


    //        TODO keep this?
    //        public Element! Eliminate(Element! e, VariableSeq! variables)
    //        {
    //            Lattice.Element result = e;
    //            foreach (IVariable var in variables)
    //            {
    //                result = this.Eliminate(result, var);
    //            }
    //            return result;
    //        }


    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    // Note!
    // 
    // Concrete classes that implement Lattice must implement one of the AtMost
    // overloads.  We provide here a default implementation for one given a "real"
    // implementation of the other.  Otherwise, there will be an infinite loop!
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    protected override bool AtMost(Element/*!*/ a, Element/*!*/ b) {
      //Contract.Requires(b != null);
      //Contract.Requires(a != null);
      return AtMost(a, IdentityCombineNameMap.Map, b, IdentityCombineNameMap.Map);
    }

    protected virtual bool AtMost(Element/*!*/ a, ICombineNameMap/*!*/ aToResult, Element/*!*/ b, ICombineNameMap/*!*/ bToResult) {
      Contract.Requires(bToResult != null);
      Contract.Requires(b != null);
      Contract.Requires(aToResult != null);
      Contract.Requires(a != null);
      return AtMost(ApplyCombineNameMap(a, aToResult), ApplyCombineNameMap(b, bToResult));
    }

    public bool LowerThan(Element/*!*/ a, ICombineNameMap/*!*/ aToResult, Element/*!*/ b, ICombineNameMap/*!*/ bToResult) {
      Contract.Requires(bToResult != null);
      Contract.Requires(b != null);
      Contract.Requires(aToResult != null);
      Contract.Requires(a != null);
      Answer ans = TrivialLowerThan(a, b);
      return ans != Answer.Maybe ? ans == Answer.Yes : AtMost(a, aToResult, b, bToResult);
    }

    public bool HigherThan(Element/*!*/ a, ICombineNameMap/*!*/ aToResult, Element/*!*/ b, ICombineNameMap/*!*/ bToResult) {
      Contract.Requires(bToResult != null);
      Contract.Requires(b != null);
      Contract.Requires(aToResult != null);
      Contract.Requires(a != null);
      return LowerThan(b, bToResult, a, aToResult);
    }


    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    // Note!
    // 
    // Concrete classes that implement Lattice must implement one of the NontrivialJoin
    // overloads.  We provide here a default implementation for one given a "real"
    // implementation of the other.  Otherwise, there will be an infinite loop!
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    public override Element/*!*/ NontrivialJoin(Element/*!*/ a, Element/*!*/ b) {
      //Contract.Requires(b != null);
      //Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      return NontrivialJoin(a, IdentityCombineNameMap.Map, b, IdentityCombineNameMap.Map);
    }

    public virtual Element/*!*/ NontrivialJoin(Element/*!*/ a, ICombineNameMap/*!*/ aToResult, Element/*!*/ b, ICombineNameMap/*!*/ bToResult) {
      Contract.Requires(bToResult != null);
      Contract.Requires(b != null);
      Contract.Requires(aToResult != null);
      Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      return NontrivialJoin(ApplyCombineNameMap(a, aToResult), ApplyCombineNameMap(b, bToResult));
    }

    public Element/*!*/ Join(Element/*!*/ a, ICombineNameMap/*!*/ aToResult, Element/*!*/ b, ICombineNameMap/*!*/ bToResult) {
      Contract.Requires(bToResult != null);
      Contract.Requires(b != null);
      Contract.Requires(aToResult != null);
      Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      Element/*?*/ r = TrivialJoin(a, b);
      return r != null ? r : NontrivialJoin(a, aToResult, b, bToResult);
    }


    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    // Note!
    // 
    // Concrete classes that implement Lattice must implement one of the Widen
    // overloads.  We provide here a default implementation for one given a "real"
    // implementation of the other.  Otherwise, there will be an infinite loop!
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    public override Element/*!*/ Widen(Element/*!*/ a, Element/*!*/ b) {
      //Contract.Requires(b != null);
      //Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      return Widen(a, IdentityCombineNameMap.Map, b, IdentityCombineNameMap.Map);
    }

    public virtual Element/*!*/ Widen(Element/*!*/ a, ICombineNameMap/*!*/ aToResult, Element/*!*/ b, ICombineNameMap/*!*/ bToResult) {
      Contract.Requires(bToResult != null);
      Contract.Requires(b != null);
      Contract.Requires(aToResult != null);
      Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      return Widen(ApplyCombineNameMap(a, aToResult), ApplyCombineNameMap(b, bToResult));
    }



    /// <summary>
    ///  A default implementation of the <see cref="CheckVariableDisequality"/> given
    ///  the appropriate expression factories by calling CheckPredicate.
    /// </summary>
    protected Answer DefaultCheckVariableDisequality(IPropExprFactory/*!*/ propExprFactory, IValueExprFactory/*!*/ valExprFactory, Element/*!*/ e, IVariable/*!*/ var1, IVariable/*!*/ var2) {
      Contract.Requires(propExprFactory != null);
      Contract.Requires(valExprFactory != null);
      Contract.Requires(e != null);
      Contract.Requires(var1 != null);
      Contract.Requires(var2 != null);
      return this.CheckPredicate(e, propExprFactory.Not(valExprFactory.Eq(var1, var2)));
    }

    private Element/*!*/ ApplyCombineNameMap(Element/*!*/ e, ICombineNameMap/*!*/ eToResult) {
      Contract.Requires(eToResult != null);
      Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      Element/*!*/ result = e;

      foreach (G.KeyValuePair<IVariable/*!*/, ISet/*<IVariable!>*//*!*/> entry in eToResult.GetSourceToResult()) {
        IVariable/*!*/ sourceName = entry.Key;
        Contract.Assert(sourceName != null);
        ISet/*<IVariable!*//*!*/ resultNames = entry.Value;
        Contract.Assert(resultNames != null);
        // Renaming s to r is okay if
        //       (1) s is not used in the result
        //   and (2) s has not been renamed already
        bool renameOkay = !resultNames.Contains(sourceName);
        IVariable/*!*/ representative = sourceName;
        Contract.Assert(representative != null);

        foreach (IVariable/*!*/ rname in resultNames) {
          Contract.Assert(rname != null);
          // skip if sourceName and rname are the same
          if (object.Equals(sourceName, rname)) {
            continue;
          }

          if (renameOkay) {
            result = this.Rename(result, sourceName, rname);
            representative = rname; // representative now rname
            renameOkay = false;  // no longer okay to rename
          } else {
            result = this.Constrain(result, valueExprFactory.Eq(representative, rname));
          }
        }
      }

      return result;
    }

    private sealed class IdentityCombineNameMap : ICombineNameMap {
      public static readonly IdentityCombineNameMap/*!*/ Map = new IdentityCombineNameMap();

      private static readonly G.Dictionary<IVariable/*!*/, ISet/*<IVariable!>*//*!*/>/*!*/ emptyDictionary1 = new G.Dictionary<IVariable/*!*/, ISet/*<IVariable!>*//*!*/>();
      private static readonly G.Dictionary<IVariable/*!*/, IVariable/*!*/>/*!*/ emptyDictionary2 = new G.Dictionary<IVariable/*!*/, IVariable/*!*/>();
      [ContractInvariantMethod]
      void ObjectInvariant() {
        Contract.Invariant(Map != null);
        Contract.Invariant(cce.NonNullDictionaryAndValues(emptyDictionary1) && Contract.ForAll(emptyDictionary1.Values, set =>/*cce.NonNullElements(set)*/set != null));
        Contract.Invariant(cce.NonNullDictionaryAndValues(emptyDictionary2));
        Contract.Invariant(indexMap != null);
        Contract.Invariant(reverseIndexMap != null);

      }


      public ISet/*<IVariable!>*//*?*/ GetResultNames(IVariable/*!*/ srcname) {
        //Contract.Requires(srcname != null);
        ArraySet a = new ArraySet();
        a.Add(srcname);
        return a;
      }

      public IVariable/*?*/ GetSourceName(IVariable/*!*/ resname) {
        //Contract.Requires(resname != null);
        return resname;
      }

      //TODO: uncomment when works in compiler
      //public G.IEnumerable<G.KeyValuePair<IVariable!,ISet/*<IVariable!>*/!>> GetSourceToResult()
      public IEnumerable/*!*/ GetSourceToResult() {
        Contract.Ensures(Contract.Result<IEnumerable>() != null);
        return emptyDictionary1;
      }

      //public G.IEnumerable<G.KeyValuePair<IVariable!,IVariable!>> GetResultToSource()
      public IEnumerable/*!*/ GetResultToSource() {
        Contract.Ensures(Contract.Result<IEnumerable>() != null);
        return emptyDictionary2;
      }

      private IdentityCombineNameMap() {
      }
    }

    #region Support for MultiLattice to uniquely number every subclass of Lattice


    private static Hashtable/*<System.Type,int>*//*!*/ indexMap = new Hashtable();
    private static Hashtable/*<int,Lattice>*//*!*/ reverseIndexMap = new Hashtable();
    private static int globalCount = 0;

    protected virtual object/*!*/ UniqueId {
      get {
        Contract.Ensures(Contract.Result<object>() != null);
        return cce.NonNull(this.GetType());
      }
    }

    public int Index {
      get {
        object unique = this.UniqueId;
        if (indexMap.ContainsKey(unique)) {
          object index = indexMap[unique];
          Contract.Assert(index != null); // this does nothing for nonnull analysis
          if (index != null) {
            return (int)index;
          }
          return 0;
        } else {
          int myIndex = globalCount++;
          indexMap[unique] = myIndex;
          reverseIndexMap[myIndex] = this;
          return myIndex;
        }
      }
    }

    public static Lattice GetGlobalLattice(int i) {
      return reverseIndexMap[i] as Lattice;
    }
    #endregion

    public static bool LogSwitch = false;
    /// <summary>
    ///  Returns the predicate that corresponds to the given lattice element.
    /// </summary>
    public abstract IExpr/*!*/ ToPredicate(Element/*!*/ e);

    /// <summary>
    ///  Allows the lattice to specify whether it understands a particular function symbol.
    /// 
    ///  The lattice is always allowed to return "true" even when it really can't do anything
    ///  with such functions; however, it is advantageous to say "false" when possible to
    ///  avoid being called to do certain things.
    /// 
    ///  The arguments to a function are provided for context so that the lattice can say
    ///  true or false for the same function symbol in different situations.  For example,
    ///  a lattice may understand the multiplication of a variable and a constant but not
    ///  of two variables.  The implementation of a lattice should not hold on to the
    ///  arguments.
    /// </summary>
    /// <param name="f">The function symbol.</param>
    /// <param name="args">The argument context.</param>
    /// <returns>True if it may understand f, false if it does not understand f.</returns>
    public abstract bool Understands(IFunctionSymbol/*!*/ f, IList/*<IExpr!>*//*!*/ args);

    /// <summary>
    ///  Return an expression that is equivalent to the given expression that does not
    ///  contain the given variable according to the lattice element and queryable.
    /// </summary>
    /// <param name="e">The lattice element.</param>
    /// <param name="q">A queryable for asking addtional information.</param>
    /// <param name="expr">The expression to find an equivalent expression.</param>
    /// <param name="var">The variable to eliminate.</param>
    /// <param name="prohibitedVars">The set of variables that can't be used in the resulting expression.</param>
    /// <returns>
    /// An equivalent expression to <paramref name="expr"/> without <paramref name="var"/>
    /// or null if not possible.
    /// </returns>
    public abstract IExpr/*?*/ EquivalentExpr(Element/*!*/ e, IQueryable/*!*/ q, IExpr/*!*/ expr, IVariable/*!*/ var, Set/*<IVariable!>*//*!*/ prohibitedVars);

    /// <summary>
    ///  Answers a query about whether the given predicate holds given the lattice element.
    /// </summary>
    /// <param name="e">The lattice element.</param>
    /// <param name="pred">The predicate.</param>
    /// <returns>Yes, No, or Maybe.</returns>
    public abstract Answer CheckPredicate(Element/*!*/ e, IExpr/*!*/ pred);

    /// <summary>
    ///  Answers a disequality about two variables.  The same information could be obtained
    ///  by asking CheckPredicate, but a different implementation may be simpler and more
    ///  efficient.
    /// </summary>
    /// <param name="e">The lattice element.</param>
    /// <param name="var1">The first variable.</param>
    /// <param name="var2">The second variable.</param>
    /// <returns>Yes, No, or Maybe.</returns>
    public abstract Answer CheckVariableDisequality(Element/*!*/ e, IVariable/*!*/ var1, IVariable/*!*/ var2);

    public abstract string/*!*/ ToString(Element/*!*/ e); // for debugging

  }
  [ContractClassFor(typeof(Lattice))]
  abstract class LatticeContracts : Lattice {
    public LatticeContracts()
      : base(null) {
    }
    public override IExpr ToPredicate(MathematicalLattice.Element e) {
      Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<IExpr>() != null);
      throw new NotImplementedException();
    }
    public override bool Understands(IFunctionSymbol f, IList args) {
      Contract.Requires(f != null);
      Contract.Requires(args != null);
      throw new NotImplementedException();
    }
    public override IExpr EquivalentExpr(MathematicalLattice.Element e, IQueryable q, IExpr expr, IVariable var, Set prohibitedVars) {
      Contract.Requires(e != null);
      Contract.Requires(q != null);
      Contract.Requires(expr != null);
      Contract.Requires(var != null);
      Contract.Requires(prohibitedVars != null);
      Contract.Ensures(Contract.Result<IExpr>() != null);
      throw new NotImplementedException();
    }
    public override Answer CheckPredicate(MathematicalLattice.Element e, IExpr pred) {
      Contract.Requires(e != null);
      Contract.Requires(pred != null);
      throw new NotImplementedException();
    }
    public override Answer CheckVariableDisequality(MathematicalLattice.Element e, IVariable var1, IVariable var2) {
      Contract.Requires(e != null);
      Contract.Requires(var1 != null);
      Contract.Requires(var2 != null);
      throw new NotImplementedException();
    }
    public override string ToString(Element e) {
      Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<string>() != null);
      throw new NotImplementedException();
    }
    public override MathematicalLattice.Element Eliminate(MathematicalLattice.Element e, IVariable variable) {
      Contract.Requires(e != null);
      Contract.Requires(variable != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      throw new NotImplementedException();
    }
    public override MathematicalLattice.Element Rename(MathematicalLattice.Element e, IVariable oldName, IVariable newName) {
      Contract.Requires(e != null);
      Contract.Requires(oldName != null);
      Contract.Requires(newName != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      throw new NotImplementedException();
    }
    public override MathematicalLattice.Element Constrain(MathematicalLattice.Element e, IExpr expr) {
      Contract.Requires(e != null);
      Contract.Requires(expr != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      throw new NotImplementedException();
    }
  }

  /// <summary>
  ///   Defines the relation between names used in the respective input lattice elements to the
  ///   various combination operators (Join,Widen,Meet,AtMost) and the names that should be used
  ///   in the resulting lattice element.
  /// </summary>
  [ContractClass(typeof(ICombineNameMapContracts))]
  public interface ICombineNameMap {
    ISet/*<IVariable!>*//*?*/ GetResultNames(IVariable/*!*/ srcname);
    IVariable/*?*/ GetSourceName(IVariable/*!*/ resname);

    //TODO: uncommet when works in compiler
    //G.IEnumerable<G.KeyValuePair<IVariable!,ISet/*<IVariable!>*/!>> GetSourceToResult();
    IEnumerable/*!*/ GetSourceToResult();
    //G.IEnumerable<G.KeyValuePair<IVariable!,IVariable!>> GetResultToSource();
    IEnumerable/*!*/ GetResultToSource();
  }
  [ContractClassFor(typeof(ICombineNameMap))]
  public abstract class ICombineNameMapContracts : ICombineNameMap {
    #region ICombineNameMap Members

    public Set GetResultNames(IVariable srcname) {
      Contract.Requires(srcname != null);
      throw new NotImplementedException();
    }

    public IVariable GetSourceName(IVariable resname) {
      Contract.Requires(resname != null);
      throw new NotImplementedException();
    }

    public IEnumerable GetSourceToResult() {
      Contract.Ensures(Contract.Result<IEnumerable>() != null);
      throw new NotImplementedException();
    }

    public IEnumerable GetResultToSource() {
      Contract.Ensures(Contract.Result<IEnumerable>() != null);
      throw new NotImplementedException();
    }

    #endregion
  }

  /// <summary>
  ///   Provides statistics on the number of times an operation is performed
  ///   and forwards the real operations to the given lattice in the constructor.
  /// </summary>
  public class StatisticsLattice : Lattice {
    [ContractInvariantMethod]
    void ObjectInvariant() {
      Contract.Invariant(lattice != null);
    }

    readonly Lattice/*!*/ lattice;
    int eliminateCount;
    int renameCount;
    int constrainCount;
    int toPredicateCount;
    int atMostCount;
    int topCount;
    int bottomCount;
    int isTopCount;
    int isBottomCount;
    int joinCount;
    int meetCount;
    int widenCount;
    int understandsCount;
    int equivalentExprCount;
    int checkPredicateCount;
    int checkVariableDisequalityCount;

    public StatisticsLattice(Lattice/*!*/ lattice)
      : base(lattice.valueExprFactory) {
      Contract.Requires(lattice != null);
      this.lattice = lattice;
      // base(lattice.valueExprFactory);
    }

    public override Element/*!*/ Eliminate(Element/*!*/ e, IVariable/*!*/ variable) {
      //Contract.Requires(variable != null);
      //Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      eliminateCount++;
      return lattice.Eliminate(e, variable);
    }

    public override Element/*!*/ Rename(Element/*!*/ e, IVariable/*!*/ oldName, IVariable/*!*/ newName) {
      //Contract.Requires(newName != null);
      //Contract.Requires(oldName != null);
      //Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      renameCount++;
      return lattice.Rename(e, oldName, newName);
    }

    public override Element/*!*/ Constrain(Element/*!*/ e, IExpr/*!*/ expr) {
      //Contract.Requires(expr != null);
      //Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      constrainCount++;
      return lattice.Constrain(e, expr);
    }


    public override bool Understands(IFunctionSymbol/*!*/ f, IList/*!*/ args) {
      //Contract.Requires(args != null);
      //Contract.Requires(f != null);
      understandsCount++;
      return lattice.Understands(f, args);
    }


    public override IExpr/*?*/ EquivalentExpr(Element/*!*/ e, IQueryable/*!*/ q, IExpr/*!*/ expr, IVariable/*!*/ var, ISet/*<IVariable!>*//*!*/ prohibitedVars) {
      //Contract.Requires(prohibitedVars != null);
      //Contract.Requires(var != null);
      //Contract.Requires(expr != null);
      //Contract.Requires(q != null);
      //Contract.Requires(e != null);
      equivalentExprCount++;
      return lattice.EquivalentExpr(e, q, expr, var, prohibitedVars);
    }


    public override Answer CheckPredicate(Element/*!*/ e, IExpr/*!*/ pred) {
      //Contract.Requires(pred != null);
      //Contract.Requires(e != null);
      checkPredicateCount++;
      return lattice.CheckPredicate(e, pred);
    }


    public override Answer CheckVariableDisequality(Element/*!*/ e, IVariable/*!*/ var1, IVariable/*!*/ var2) {
      //Contract.Requires(var2 != null);
      //Contract.Requires(var1 != null);
      //Contract.Requires(e != null);
      checkVariableDisequalityCount++;
      return lattice.CheckVariableDisequality(e, var1, var2);
    }



    public override IExpr/*!*/ ToPredicate(Element/*!*/ e) {
      //Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<IExpr>() != null);
      toPredicateCount++;
      return lattice.ToPredicate(e);
    }

    public override string/*!*/ ToString(Element/*!*/ e) {
      //Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<string>() != null);
      return lattice.ToString(e);
    }

    [Pure]
    public override string/*!*/ ToString() {
      Contract.Ensures(Contract.Result<string>() != null);
      return string.Format(
        "StatisticsLattice: #Eliminate={0} #Rename={1} #Constrain={2} #ToPredicate={3} " +
        "#Understands={4} #EquivalentExpr={5} #CheckPredicate={6} #CheckVariableDisequality={7} " +
        "#AtMost={8} #Top={9} #Bottom={9} #IsTop={10} #IsBottom={11} " +
        "#NonTrivialJoin={12} #NonTrivialMeet={13} #Widen={14}",
        eliminateCount, renameCount, constrainCount, toPredicateCount,
        understandsCount, equivalentExprCount, checkPredicateCount, checkVariableDisequalityCount,
        atMostCount, topCount, bottomCount, isTopCount, isBottomCount,
        joinCount, meetCount, widenCount);
    }

    protected override bool AtMost(Element/*!*/ a, Element/*!*/ b) {
      //Contract.Requires(b != null);
      //Contract.Requires(a != null);
      atMostCount++;
      return lattice.LowerThan(a, b);
    }

    public override Element/*!*/ Top {
      get {
        Contract.Ensures(Contract.Result<Element>() != null);
        topCount++;
        return lattice.Top;
      }
    }
    public override Element/*!*/ Bottom {
      get {
        Contract.Ensures(Contract.Result<Element>() != null);
        bottomCount++;
        return lattice.Bottom;
      }
    }

    public override bool IsTop(Element/*!*/ e) {
      //Contract.Requires(e != null);
      isTopCount++;
      return lattice.IsTop(e);
    }

    public override bool IsBottom(Element/*!*/ e) {
      //Contract.Requires(e != null);
      isBottomCount++;
      return lattice.IsBottom(e);
    }

    public override Element/*!*/ NontrivialJoin(Element/*!*/ a, Element/*!*/ b) {
      //Contract.Requires(b != null);
      //Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      joinCount++;
      return lattice.NontrivialJoin(a, b);
    }

    public override Element/*!*/ NontrivialMeet(Element/*!*/ a, Element/*!*/ b) {
      //Contract.Requires(b != null);
      //Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      meetCount++;
      return lattice.NontrivialMeet(a, b);
    }

    public override Element/*!*/ Widen(Element/*!*/ a, Element/*!*/ b) {
      //Contract.Requires(b != null);
      //Contract.Requires(a != null);
      Contract.Ensures(Contract.Result<Element>() != null);
      widenCount++;
      return lattice.Widen(a, b);
    }

    public override void Validate() {
      base.Validate();
      lattice.Validate();
    }

    protected override object/*!*/ UniqueId {
      get {
        Contract.Ensures(Contract.Result<object>() != null);
        // use the base id, not the underlying-lattice id (is that the right thing to do?)
        return base.UniqueId;
      }
    }
  }


  public sealed class LatticeQueryable : IQueryable {
    private Lattice/*!*/ lattice;
    private Lattice.Element/*!*/ element;
    [ContractInvariantMethod]
    void ObjectInvariant() {
      Contract.Invariant(lattice != null);
      Contract.Invariant(element != null);
    }


    public LatticeQueryable(Lattice/*!*/ lattice, Lattice.Element/*!*/ element) {
      Contract.Requires(element != null);
      Contract.Requires(lattice != null);
      this.lattice = lattice;
      this.element = element;
      // base();
    }

    public Answer CheckPredicate(IExpr/*!*/ pred) {
      //Contract.Requires(pred != null);
      return lattice.CheckPredicate(element, pred);
    }

    public Answer CheckVariableDisequality(IVariable/*!*/ var1, IVariable/*!*/ var2) {
      //Contract.Requires(var2 != null);
      //Contract.Requires(var1 != null);
      return lattice.CheckVariableDisequality(element, var1, var2);
    }
  }
}