summaryrefslogtreecommitdiff
path: root/Test/dafny0/BinaryTree.dfy
blob: 138a22dfae5a33cd429a6ae542fbcd6851575643 (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
class IntSet {
  var contents: set<int>;
  var footprint: set<object>;

  var root: Node;

  function Valid(): bool
    reads this, footprint;
  {
    this in footprint &&
    (root == null ==> contents == {}) &&
    (root != null ==>
       root in footprint && root.Valid() &&
       contents == root.contents &&
       root.footprint <= footprint && this !in root.footprint)
  }

  method Init()
    modifies this;
    ensures Valid() && fresh(footprint - {this});
    ensures contents == {};
  {
    root := null;
    footprint := {this};
    contents := {};
  }

  method Find(x: int) returns (present: bool)
    requires Valid();
    ensures present <==> x in contents;
  {
    if (root == null) {
      present := false;
    } else {
      call present := root.Find(x);
    }
  }

  method Insert(x: int)
    requires Valid();
    modifies footprint;
    ensures Valid() && fresh(footprint - old(footprint));
    ensures contents == old(contents) + {x};
  { 
    call t := InsertHelper(x, root);
    root := t;
    contents := root.contents;
    footprint := root.footprint + {this};
  }

  method InsertHelper(x: int, n: Node) returns (m: Node)
    requires n == null || n.Valid();
    modifies n.footprint;
    ensures m != null && m.Valid();
    ensures n == null ==> fresh(m.footprint) && m.contents == {x};
    ensures n != null ==> m == n && n.contents == old(n.contents) + {x};
    ensures n != null ==> fresh(n.footprint - old(n.footprint));
  {
    if (n == null) {
      m := new Node;
      call m.Init(x);
    } else if (x == n.data) {
      m := n;
    } else {
      if (x < n.data) {
        call t := InsertHelper(x, n.left);
        n.left := t;
        n.footprint := n.footprint + n.left.footprint;
      } else {
        call t := InsertHelper(x, n.right);
        n.right := t;
        n.footprint := n.footprint + n.right.footprint;
      }
      n.contents := n.contents + {x};
      m := n;
    }
  }

  method Remove(x: int)
    requires Valid();
    modifies footprint;
    ensures Valid() && fresh(footprint - old(footprint));
    ensures contents == old(contents) - {x};
  {
    if (root != null) {
      call newRoot := root.Remove(x);
      root := newRoot;
      if (root == null) {
        contents := {};
        footprint := {this};
      } else {
        contents := root.contents;
        footprint := root.footprint + {this};
      }
    }
  }
}

class Node {
  var contents: set<int>;
  var footprint: set<object>;
  
  var data: int;
  var left: Node;
  var right: Node;

  function Valid(): bool
    reads this, footprint;
  {
    this in footprint &&
    null !in footprint &&
    (left != null ==>
      left in footprint && left.Valid() &&
      left.footprint <= footprint && this !in left.footprint &&
      (forall y :: y in left.contents ==> y < data)) &&
    (right != null ==>
      right in footprint && right.Valid() &&
      right.footprint <= footprint && this !in right.footprint &&
      (forall y :: y in right.contents ==> data < y)) &&
    (left == null && right == null ==>
      contents == {data}) &&
    (left != null && right == null ==>
      contents == left.contents + {data}) &&
    (left == null && right != null ==>
      contents == {data} + right.contents) &&
    (left != null && right != null ==>
      left.footprint !! right.footprint &&
      contents == left.contents + {data} + right.contents)
  }

  method Init(x: int)
    modifies this;
    ensures Valid() && fresh(footprint - {this});
    ensures contents == {x};
  {
    data := x;
    left := null;
    right := null;
    contents := {x};
    footprint := {this};
  }

  method Find(x: int) returns (present: bool)
    requires Valid();
    ensures present <==> x in contents;
  {
    if (x == data) {
      present := true;
    } else if (left != null && x < data) {
      call present := left.Find(x);
    } else if (right != null && data < x) {
      call present := right.Find(x);
    } else {
      present := false;
    }
  }

  method Remove(x: int) returns (node: Node)
    requires Valid();
    modifies footprint;
    ensures fresh(footprint - old(footprint));
    ensures node != null ==> node.Valid();
    ensures node == null ==> old(contents) <= {x};
    ensures node != null ==> node.footprint <= footprint && node.contents == old(contents) - {x};
  {
    node := this;
    if (left != null && x < data) {
      call t := left.Remove(x);
      left := t;
      contents := contents - {x};
      if (left != null) { footprint := footprint + left.footprint; }
    } else if (right != null && data < x) {
      call t := right.Remove(x);
      right := t;
      contents := contents - {x};
      if (right != null) { footprint := footprint + right.footprint; }
    } else if (x == data) {
      if (left == null && right == null) {
        node := null;
      } else if (left == null) {
        node := right;
      } else if (right == null) {
        node := left;
      } else {
        // rotate
        call min, r := right.RemoveMin();
        data := min;  right := r;
        contents := contents - {x};
        if (right != null) { footprint := footprint + right.footprint; }
      }
    }
  }

  method RemoveMin() returns (min: int, node: Node)
    requires Valid();
    modifies footprint;
    ensures fresh(footprint - old(footprint));
    ensures node != null ==> node.Valid();
    ensures node == null ==> old(contents) == {min};
    ensures node != null ==> node.footprint <= footprint && node.contents == old(contents) - {min};
    ensures min in old(contents) && (forall x :: x in old(contents) ==> min <= x);
  {
    if (left == null) {
      min := data;
      node := right;
    } else {
      call min, t := left.RemoveMin();
      left := t;
      node := this;
      contents := contents - {min};
      if (left != null) { footprint := footprint + left.footprint; }
    }
  }
}

class Main {
  method Client0(x: int)
  {
    var s := new IntSet;
    call s.Init();

    call s.Insert(12);
    call s.Insert(24);
    call present := s.Find(x);
    assert present <==> x == 12 || x == 24;
  }

  method Client1(s: IntSet, x: int)
    requires s != null && s.Valid();
    modifies s.footprint;
  {
    call s.Insert(x);
    call s.Insert(24);
    assert old(s.contents) - {x,24} == s.contents - {x,24};
  }
}