-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathConcTree.scala
320 lines (290 loc) · 9.66 KB
/
ConcTree.scala
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
package conctrees
// By Ravi Kandhadai Madhavan @ LARA, EPFL. (c) EPFL
import stainless.collection._
import stainless.lang._
import ListSpecs._
import stainless.annotation._
object ConcTrees {
@inline
def max(x: BigInt, y: BigInt): BigInt = if (x >= y) x else y
def abs(x: BigInt): BigInt = if (x < 0) -x else x
sealed abstract class Conc[T] {
def isEmpty: Boolean = {
this == Empty[T]()
}
def isLeaf: Boolean = {
this match {
case Empty() => true
case Single(_) => true
case _ => false
}
}
@inline
def valid: Boolean = {
concInv && balanced
}
/**
* (a) left and right trees of conc node should be non-empty
* (b) they cannot be append nodes
*/
def concInv: Boolean = this match {
case CC(l, r) =>
!l.isEmpty && !r.isEmpty &&
l.concInv && r.concInv
case _ => true
}
def balanced: Boolean = {
this match {
case CC(l, r) =>
l.level - r.level >= -1 && l.level - r.level <= 1 &&
l.balanced && r.balanced
case _ => true
}
}
lazy val level: BigInt = {
(this match {
case Empty() => 0
case Single(x) => 0
case CC(l, r) =>
1 + max(l.level, r.level)
}): BigInt
}.ensuring(_ >= 0)
lazy val size: BigInt = {
(this match {
case Empty() => 0
case Single(x) => 1
case CC(l, r) =>
l.size + r.size
}): BigInt
}.ensuring(_ >= 0)
def toList: List[T] = {
this match {
case Empty() => Nil[T]()
case Single(x) => Cons(x, Nil[T]())
case CC(l, r) =>
l.toList ++ r.toList // note: left elements precede the right elements in the list
}
}.ensuring(res => res.size == this.size)
}
case class Empty[T]() extends Conc[T]
case class Single[T](x: T) extends Conc[T]
case class CC[T](left: Conc[T], right: Conc[T]) extends Conc[T]
/*class Chunk(val array: Array[T], val size: Int, val k: Int) extends Leaf[T] {
def level = 0
override def toString = s"Chunk(${array.mkString("", ", ", "")}; $size; $k)"
}*/
def lookup[T](xs: Conc[T], i: BigInt): T = {
require(xs.valid && !xs.isEmpty && i >= 0 && i < xs.size)
xs match {
case Single(x) => x
case CC(l, r) =>
if (i < l.size) lookup(l, i)
else lookup(r, i - l.size)
}
}.ensuring(res =>
// axiom instantiation
instAppendIndexAxiom(xs, i) &&
res == xs.toList(i)) // lookup time is linear in the height
def instAppendIndexAxiom[T](xs: Conc[T], i: BigInt): Boolean = {
require(0 <= i && i < xs.size)
xs match {
case CC(l, r) =>
appendIndex(l.toList, r.toList, i)
case _ => true
}
}.holds
def update[T](xs: Conc[T], i: BigInt, y: T): Conc[T] = {
require(xs.valid && !xs.isEmpty && i >= 0 && i < xs.size)
xs match {
case Single(x) => Single(y)
case CC(l, r) =>
if (i < l.size) CC(update(l, i, y), r)
else CC(l, update(r, i - l.size, y))
}
}.ensuring(res => instAppendUpdateAxiom(xs, i, y) && // an auxiliary axiom instantiation
res.level == xs.level && // heights of the input and output trees are equal
res.valid && // tree invariants are preserved
res.toList == xs.toList.updated(i, y)) // update time is linear in the height of the tree
def instAppendUpdateAxiom[T](xs: Conc[T], i: BigInt, y: T): Boolean = {
require(i >= 0 && i < xs.size)
xs match {
case CC(l, r) =>
appendUpdate(l.toList, r.toList, i, y)
case _ => true
}
}.holds
/**
* A generic concat that applies to general concTrees
*/
// def concat[T](xs: Conc[T], ys: Conc[T]): Conc[T] = {
// require(xs.valid && ys.valid)
// concatNormalized(normalize(xs), normalize(ys))
// }
def concatNonEmpty[T](xs: Conc[T], ys: Conc[T]): Conc[T] = {
require(xs.valid && ys.valid && !xs.isEmpty && !ys.isEmpty)
val diff = ys.level - xs.level
if (diff >= -1 && diff <= 1) CC(xs, ys)
else if (diff < -1) { // ys is smaller than xs
xs match {
case CC(l, r) =>
if (l.level >= r.level)
CC(l, concatNonEmpty(r, ys))
else {
r match {
case CC(rl, rr) =>
val nrr = concatNonEmpty(rr, ys)
if (nrr.level == xs.level - 3)
CC(l, CC(rl, nrr))
else
CC(CC(l, rl), nrr)
}
}
}
} else ys match {
case CC(l, r) =>
if (r.level >= l.level)
CC(concatNonEmpty(xs, l), r)
else {
l match {
case CC(ll, lr) =>
val nll = concatNonEmpty(xs, ll)
if (nll.level == ys.level - 3)
CC(CC(nll, lr), r)
else
CC(nll, CC(lr, r))
}
}
}
}.ensuring(res =>
res.level <= max(xs.level, ys.level) + 1 && // height invariants
res.level >= max(xs.level, ys.level) &&
res.valid &&
appendAssocInst(xs, ys) && // instantiation of an axiom
concatCorrectness(res, xs, ys)) // time bounds
def appendAssocInst[T](xs: Conc[T], ys: Conc[T]): Boolean = {
(xs match {
case CC(l, r) =>
appendAssoc(l.toList, r.toList, ys.toList) && //instantiation of associativity of concatenation
(r match {
case CC(rl, rr) =>
appendAssoc(rl.toList, rr.toList, ys.toList) &&
appendAssoc(l.toList, rl.toList, rr.toList ++ ys.toList)
case _ => true
})
case _ => true
}) &&
(ys match {
case CC(l, r) =>
appendAssoc(xs.toList, l.toList, r.toList) &&
(l match {
case CC(ll, lr) =>
appendAssoc(xs.toList, ll.toList, lr.toList) &&
appendAssoc(xs.toList ++ ll.toList, lr.toList, r.toList)
case _ => true
})
case _ => true
})
}.holds
/**
* This concat applies only to normalized trees.
* This prevents concat from being recursive
*/
def concatNormalized[T](xs: Conc[T], ys: Conc[T]): Conc[T] = {
require(xs.valid && ys.valid)
(xs, ys) match {
case (xs, Empty()) => xs
case (Empty(), ys) => ys
case _ => concatNonEmpty(xs, ys)
}
}.ensuring(res => res.valid && // tree invariants
res.level <= max(xs.level, ys.level) + 1 && // height invariants
res.level >= max(xs.level, ys.level) &&
concatCorrectness(res, xs, ys))
def concatCorrectness[T](res: Conc[T], xs: Conc[T], ys: Conc[T]): Boolean =
(res.toList == xs.toList ++ ys.toList)
def insert[T](xs: Conc[T], i: BigInt, y: T): Conc[T] = {
require(xs.valid && i >= 0 && i <= xs.size) //note the precondition
xs match {
case Empty() => Single(y)
case Single(x) =>
if (i == 0) CC(Single(y), xs)
else CC(xs, Single(y))
case CC(l, r) =>
if (i < l.size)
concatNonEmpty(insert(l, i, y), r)
else
concatNonEmpty(l, insert(r, i - l.size, y))
}
}.ensuring(res =>
res.valid && // tree invariants
res.level - xs.level <= 1 && res.level >= xs.level && // height of the output tree is at most 1 greater than that of the input tree
!res.isEmpty &&
insertAppendAxiomInst(xs, i, y) && // instantiation of an axiom
res.toList == insertAtIndex(xs.toList, i, y))
/**
* Using a different version of insert than of the library
* because the library implementation in unnecessarily complicated.
* TODO: update the code to use the library instead ?
*/
def insertAtIndex[T](l: List[T], i: BigInt, y: T): List[T] = {
require(0 <= i && i <= l.size)
l match {
case Nil() =>
Cons[T](y, Nil())
case Cons(x, tail) =>
if (i == 0) Cons[T](y, l)
else Cons[T](x, insertAtIndex(tail, i - 1, y))
}
}
// A lemma about `append` and `insertAtIndex`
def appendInsertIndex[T](l1: List[T], l2: List[T], i: BigInt, y: T): Boolean = {
require(0 <= i && i <= l1.size + l2.size)
(l1 match {
case Nil() => true
case Cons(x, xs) => if (i == 0) true else appendInsertIndex[T](xs, l2, i - 1, y)
}) &&
// lemma
(insertAtIndex((l1 ++ l2), i, y) == (
if (i < l1.size) insertAtIndex(l1, i, y) ++ l2
else l1 ++ insertAtIndex(l2, (i - l1.size), y)))
}.holds
def insertAppendAxiomInst[T](xs: Conc[T], i: BigInt, y: T): Boolean = {
require(i >= 0 && i <= xs.size)
xs match {
case CC(l, r) => appendInsertIndex(l.toList, r.toList, i, y)
case _ => true
}
}.holds
def split[T](xs: Conc[T], n: BigInt): (Conc[T], Conc[T]) = {
require(xs.valid)
xs match {
case Empty() => (Empty[T](), Empty[T]())
case s @ Single(x) =>
if (n <= 0) (Empty[T](), s) //a minor fix
else (s, Empty[T]())
case CC(l, r) =>
if (n < l.size) {
val (ll, lr) = split(l, n)
(ll, concatNormalized(lr, r))
} else if (n > l.size) {
val (rl, rr) = split(r, n - l.size)
(concatNormalized(l, rl), rr)
} else {
(l, r)
}
}
}.ensuring(res => res._1.valid && res._2.valid && // tree invariants are preserved
xs.level >= res._1.level && xs.level >= res._2.level && // height bounds of the resulting tree
instSplitAxiom(xs, n) && // instantiation of an axiom
splitCorrectness(res, xs, n))
def splitCorrectness[T](r: (Conc[T], Conc[T]), xs: Conc[T], n: BigInt): Boolean = {
r._1.toList == xs.toList.take(n) && r._2.toList == xs.toList.drop(n)
}
def instSplitAxiom[T](xs: Conc[T], n: BigInt): Boolean = {
xs match {
case CC(l, r) =>
appendTakeDrop(l.toList, r.toList, n)
case _ => true
}
}.holds
}