summaryrefslogtreecommitdiff
path: root/Test/VSI-Benchmarks/b5.dfy
blob: 612b96bae68b92fd13a484b9d4c53b89ab2cc4cd (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


class Queue<T> {
  var head: Node<T>;
  var tail: Node<T>;

  ghost var contents: seq<T>;
  ghost var footprint: set<object>;
  ghost var spine: set<Node<T>>;

  function Valid(): bool
    reads this, footprint;
  {
    this in footprint && spine <= footprint &&
    head != null && head in spine &&
    tail != null && tail in spine &&
    tail.next == null &&
    (forall n ::
      n in spine ==>
        n != null && n.footprint <= footprint && this !in n.footprint &&
        n.Valid() &&
        (n.next == null ==> n == tail)) &&
    (forall n ::
      n in spine ==>
        n.next != null ==> n.next in spine) &&
    contents == head.tailContents
  }

  method Init()
    modifies this;
    ensures Valid() && fresh(footprint - {this});
    ensures |contents| == 0;
  {
    var n := new Node<T>.Init();
    head := n;
    tail := n;
    contents := n.tailContents;
    footprint := {this} + n.footprint;
    spine := {n};
  }

  method IsEmpty() returns (isEmpty: bool)
    requires Valid();
    ensures isEmpty <==> |contents| == 0;
  {
    isEmpty := head == tail;
  }

  method Enqueue(t: T)
    requires Valid();
    modifies footprint;
    ensures Valid() && fresh(footprint - old(footprint));
    ensures contents == old(contents) + [t];
  {
    var n := new Node<T>.Init();
    n.data := t;
    tail.next := n;
    tail := n;

    parallel (m | m in spine) {
      m.tailContents := m.tailContents + [t];
    }
    contents := head.tailContents;

    parallel (m | m in spine) {
      m.footprint := m.footprint + n.footprint;
    }
    footprint := footprint + n.footprint;

    spine := spine + {n};
  }

  method Front() returns (t: T)
    requires Valid();
    requires 0 < |contents|;
    ensures t == contents[0];
  {
    t := head.next.data;
  }

  method Dequeue()
    requires Valid();
    requires 0 < |contents|;
    modifies footprint;
    ensures Valid() && fresh(footprint - old(footprint));
    ensures contents == old(contents)[1..];
  {
    var n := head.next;
    head := n;
    contents := n.tailContents;
  }
  
  
  method Rotate()
    requires Valid();
    requires 0 < |contents|;
    modifies footprint;
    ensures Valid() && fresh(footprint - old(footprint));
    ensures contents == old(contents)[1..] + old(contents)[..1];
  {
    var t := Front();
    Dequeue();
    Enqueue(t);
  }

  method RotateAny()
    requires Valid();
    requires 0 < |contents|;
    modifies footprint;
    ensures Valid() && fresh(footprint - old(footprint));
    ensures |contents| == |old(contents)|;
    ensures (exists i :: 0 <= i && i <= |contents| &&
              contents == old(contents)[i..] + old(contents)[..i]);
  {
    var t := Front();
    Dequeue();
    Enqueue(t);
  }

}

class Node<T> {
  var data: T;
  var next: Node<T>;

  ghost var tailContents: seq<T>;
  ghost var footprint: set<object>;

  function Valid(): bool
    reads this, footprint;
  {
    this in footprint &&
    (next != null ==> next in footprint && next.footprint <= footprint) &&
    (next == null ==> tailContents == []) &&
    (next != null ==> tailContents == [next.data] + next.tailContents)
  }

  method Init()
    modifies this;
    ensures Valid() && fresh(footprint - {this});
    ensures next == null;
  {
    next := null;
    tailContents := [];
    footprint := {this};
  }
}

class Main<U> {
  method A<T>(t: T, u: T, v: T)
  {
    var q0 := new Queue<T>.Init();
    var q1 := new Queue<T>.Init();

    q0.Enqueue(t);
    q0.Enqueue(u);

    q1.Enqueue(v);

    assert |q0.contents| == 2;

    var w := q0.Front();
    assert w == t;
    q0.Dequeue();

    w := q0.Front();
    assert w == u;

    assert |q0.contents| == 1;
    assert |q1.contents| == 1;
  }

  method Main2(t: U, u: U, v: U, q0: Queue<U>, q1: Queue<U>)
    requires q0 != null && q0.Valid();
    requires q1 != null && q1.Valid();
    requires q0.footprint !! q1.footprint;
    requires |q0.contents| == 0;
    modifies q0.footprint, q1.footprint;
    ensures fresh(q0.footprint - old(q0.footprint));
    ensures fresh(q1.footprint - old(q1.footprint));
  {
    q0.Enqueue(t);
    q0.Enqueue(u);

    q1.Enqueue(v);

    assert |q0.contents| == 2;

    var w := q0.Front();
    assert w == t;
    q0.Dequeue();

    w := q0.Front();
    assert w == u;

    assert |q0.contents| == 1;
    assert |q1.contents| == old(|q1.contents|) + 1;
  }
}