-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathList.scala
613 lines (537 loc) · 15.1 KB
/
List.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
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
/* Copyright 2009-2015 EPFL, Lausanne */
package leon.collection
import leon._
import leon.lang._
import leon.annotation._
import leon.math._
@library
sealed abstract class List[T] {
def size: BigInt = (this match {
case Nil() => BigInt(0)
case Cons(h, t) => 1 + t.size
}) ensuring (_ >= 0)
def content: Set[T] = this match {
case Nil() => Set()
case Cons(h, t) => Set(h) ++ t.content
}
def contains(v: T): Boolean = (this match {
case Cons(h, t) if h == v => true
case Cons(_, t) => t.contains(v)
case Nil() => false
}) ensuring { _ == (content contains v) }
def ++(that: List[T]): List[T] = (this match {
case Nil() => that
case Cons(x, xs) => Cons(x, xs ++ that)
}) ensuring { res =>
(res.content == this.content ++ that.content) &&
(res.size == this.size + that.size)
}
def head: T = {
require(this != Nil[T]())
val Cons(h, _) = this
h
}
def tail: List[T] = {
require(this != Nil[T]())
val Cons(_, t) = this
t
}
def apply(index: BigInt): T = {
require(0 <= index && index < size)
if (index == BigInt(0)) {
head
} else {
tail(index-1)
}
}
def ::(t:T): List[T] = Cons(t, this)
def :+(t:T): List[T] = {
this match {
case Nil() => Cons(t, this)
case Cons(x, xs) => Cons(x, xs :+ (t))
}
} ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t)))
def reverse: List[T] = {
this match {
case Nil() => this
case Cons(x,xs) => xs.reverse :+ x
}
} ensuring (res => (res.size == size) && (res.content == content))
def take(i: BigInt): List[T] = { (this, i) match {
case (Nil(), _) => Nil[T]()
case (Cons(h, t), i) =>
if (i <= BigInt(0)) {
Nil[T]()
} else {
Cons(h, t.take(i-1))
}
}} ensuring { res =>
res.content.subsetOf(this.content) && (res.size == (
if (i <= 0) BigInt(0)
else if (i >= this.size) this.size
else i
))
}
def drop(i: BigInt): List[T] = { (this, i) match {
case (Nil(), _) => Nil[T]()
case (Cons(h, t), i) =>
if (i <= BigInt(0)) {
Cons[T](h, t)
} else {
t.drop(i-1)
}
}} ensuring { res =>
res.content.subsetOf(this.content) && (res.size == (
if (i <= 0) this.size
else if (i >= this.size) BigInt(0)
else this.size - i
))
}
def slice(from: BigInt, to: BigInt): List[T] = {
require(0 <= from && from <= to && to <= size)
drop(from).take(to-from)
}
def replace(from: T, to: T): List[T] = { this match {
case Nil() => Nil[T]()
case Cons(h, t) =>
val r = t.replace(from, to)
if (h == from) {
Cons(to, r)
} else {
Cons(h, r)
}
}} ensuring { (res: List[T]) =>
res.size == this.size &&
res.content == (
(this.content -- Set(from)) ++
(if (this.content contains from) Set(to) else Set[T]())
)
}
private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match {
case Nil() =>
if (acc.size > 0) {
res :+ acc
} else {
res
}
case Cons(h, t) =>
if (s0 == BigInt(0)) {
chunk0(s, l, Nil(), res :+ acc, s)
} else {
chunk0(s, t, acc :+ h, res, s0-1)
}
}
def chunks(s: BigInt): List[List[T]] = {
require(s > 0)
chunk0(s, this, Nil(), Nil(), s)
}
def zip[B](that: List[B]): List[(T, B)] = { (this, that) match {
case (Cons(h1, t1), Cons(h2, t2)) =>
Cons((h1, h2), t1.zip(t2))
case _ =>
Nil[(T, B)]()
}} ensuring { _.size == (
if (this.size <= that.size) this.size else that.size
)}
def -(e: T): List[T] = { this match {
case Cons(h, t) =>
if (e == h) {
t - e
} else {
Cons(h, t - e)
}
case Nil() =>
Nil[T]()
}} ensuring { res =>
res.size <= this.size &&
res.content == this.content -- Set(e)
}
def --(that: List[T]): List[T] = { this match {
case Cons(h, t) =>
if (that.contains(h)) {
t -- that
} else {
Cons(h, t -- that)
}
case Nil() =>
Nil[T]()
}} ensuring { res =>
res.size <= this.size &&
res.content == this.content -- that.content
}
def &(that: List[T]): List[T] = { this match {
case Cons(h, t) =>
if (that.contains(h)) {
Cons(h, t & that)
} else {
t & that
}
case Nil() =>
Nil[T]()
}} ensuring { res =>
res.size <= this.size &&
res.content == (this.content & that.content)
}
def padTo(s: BigInt, e: T): List[T] = { (this, s) match {
case (_, s) if s <= 0 =>
this
case (Nil(), s) =>
Cons(e, Nil().padTo(s-1, e))
case (Cons(h, t), s) =>
Cons(h, t.padTo(s-1, e))
}} ensuring { res =>
if (s <= this.size)
res == this
else
res.size == s &&
res.content == this.content ++ Set(e)
}
def find(e: T): Option[BigInt] = { this match {
case Nil() => None[BigInt]()
case Cons(h, t) =>
if (h == e) {
Some[BigInt](0)
} else {
t.find(e) match {
case None() => None[BigInt]()
case Some(i) => Some(i+1)
}
}
}} ensuring { _.isDefined == this.contains(e) }
def init: List[T] = {
require(!isEmpty)
(this match {
case Cons(h, Nil()) =>
Nil[T]()
case Cons(h, t) =>
Cons[T](h, t.init)
})
} ensuring ( (r: List[T]) =>
r.size == this.size - 1 &&
r.content.subsetOf(this.content)
)
def last: T = {
require(!isEmpty)
this match {
case Cons(h, Nil()) => h
case Cons(_, t) => t.last
}
} ensuring { this.contains _ }
def lastOption: Option[T] = { this match {
case Cons(h, t) =>
t.lastOption.orElse(Some(h))
case Nil() =>
None[T]()
}} ensuring { _.isDefined != this.isEmpty }
def firstOption: Option[T] = { this match {
case Cons(h, t) =>
Some(h)
case Nil() =>
None[T]()
}} ensuring { _.isDefined != this.isEmpty }
def unique: List[T] = this match {
case Nil() => Nil()
case Cons(h, t) =>
Cons(h, t.unique - h)
}
def splitAt(e: T): List[List[T]] = split(Cons(e, Nil()))
def split(seps: List[T]): List[List[T]] = this match {
case Cons(h, t) =>
if (seps.contains(h)) {
Cons(Nil(), t.split(seps))
} else {
val r = t.split(seps)
Cons(Cons(h, r.head), r.tail)
}
case Nil() =>
Cons(Nil(), Nil())
}
def evenSplit: (List[T], List[T]) = {
val c = size/2
(take(c), drop(c))
}
def insertAt(pos: BigInt, l: List[T]): List[T] = {
if(pos < 0) {
insertAt(size + pos, l)
} else if(pos == BigInt(0)) {
l ++ this
} else {
this match {
case Cons(h, t) =>
Cons(h, t.insertAt(pos-1, l))
case Nil() =>
l
}
}
} ensuring { res =>
res.size == this.size + l.size &&
res.content == this.content ++ l.content
}
def replaceAt(pos: BigInt, l: List[T]): List[T] = {
if(pos < 0) {
replaceAt(size + pos, l)
} else if(pos == BigInt(0)) {
l ++ this.drop(l.size)
} else {
this match {
case Cons(h, t) =>
Cons(h, t.replaceAt(pos-1, l))
case Nil() =>
l
}
}
} ensuring { res =>
res.content.subsetOf(l.content ++ this.content)
}
def rotate(s: BigInt): List[T] = {
if (s < 0) {
rotate(size + s)
} else if (s > size) {
rotate(s - size)
} else {
drop(s) ++ take(s)
}
} ensuring { res =>
res.size == this.size
}
def isEmpty = this match {
case Nil() => true
case _ => false
}
// Higher-order API
def map[R](f: T => R): List[R] = { this match {
case Nil() => Nil[R]()
case Cons(h, t) => f(h) :: t.map(f)
}} ensuring { _.size == this.size }
def foldLeft[R](z: R)(f: (R,T) => R): R = this match {
case Nil() => z
case Cons(h,t) => t.foldLeft(f(z,h))(f)
}
def foldRight[R](z: R)(f: (T,R) => R): R = this match {
case Nil() => z
case Cons(h, t) => f(h, t.foldRight(z)(f))
}
def scanLeft[R](z: R)(f: (R,T) => R): List[R] = { this match {
case Nil() => z :: Nil()
case Cons(h,t) => z :: t.scanLeft(f(z,h))(f)
}} ensuring { !_.isEmpty }
def scanRight[R](z: R)(f: (T,R) => R): List[R] = { this match {
case Nil() => z :: Nil[R]()
case Cons(h, t) =>
val rest@Cons(h1,_) = t.scanRight(z)(f)
f(h, h1) :: rest
}} ensuring { !_.isEmpty }
def flatMap[R](f: T => List[R]): List[R] =
ListOps.flatten(this map f)
def filter(p: T => Boolean): List[T] = { this match {
case Nil() => Nil[T]()
case Cons(h, t) if p(h) => Cons(h, t.filter(p))
case Cons(_, t) => t.filter(p)
}} ensuring { res =>
res.size <= this.size &&
res.content.subsetOf(this.content) &&
res.forall(p)
}
def filterNot(p: T => Boolean): List[T] =
filter(!p(_)) ensuring { res =>
res.size <= this.size &&
res.content.subsetOf(this.content) &&
res.forall(!p(_))
}
def partition(p: T => Boolean): (List[T], List[T]) = { this match {
case Nil() => (Nil[T](), Nil[T]())
case Cons(h, t) =>
val (l1, l2) = t.partition(p)
if (p(h)) (h :: l1, l2)
else (l1, h :: l2)
}} ensuring { res =>
res._1 == filter(p) &&
res._2 == filterNot(p)
}
// In case we implement for-comprehensions
def withFilter(p: T => Boolean) = filter(p)
def forall(p: T => Boolean): Boolean = this match {
case Nil() => true
case Cons(h, t) => p(h) && t.forall(p)
}
def exists(p: T => Boolean) = !forall(!p(_))
def find(p: T => Boolean): Option[T] = { this match {
case Nil() => None[T]()
case Cons(h, t) if p(h) => Some(h)
case Cons(_, t) => t.find(p)
}} ensuring { _.isDefined == exists(p) }
def groupBy[R](f: T => R): Map[R, List[T]] = this match {
case Nil() => Map.empty[R, List[T]]
case Cons(h, t) =>
val key: R = f(h)
val rest: Map[R, List[T]] = t.groupBy(f)
val prev: List[T] = if (rest isDefinedAt key) rest(key) else Nil[T]()
(rest ++ Map((key, h :: prev))) : Map[R, List[T]]
}
def takeWhile(p: T => Boolean): List[T] = { this match {
case Cons(h,t) if p(h) => Cons(h, t.takeWhile(p))
case _ => Nil[T]()
}} ensuring { res =>
(res forall p) &&
(res.size <= this.size) &&
(res.content subsetOf this.content)
}
def dropWhile(p: T => Boolean): List[T] = { this match {
case Cons(h,t) if p(h) => t.dropWhile(p)
case _ => this
}} ensuring { res =>
(res.size <= this.size) &&
(res.content subsetOf this.content) &&
(res.isEmpty || !p(res.head))
}
def count(p: T => Boolean): BigInt = { this match {
case Nil() => BigInt(0)
case Cons(h, t) =>
(if (p(h)) BigInt(1) else BigInt(0)) + t.count(p)
}} ensuring {
_ == this.filter(p).size
}
}
@ignore
object List {
def apply[T](elems: T*): List[T] = {
var l: List[T] = Nil[T]()
for (e <- elems) {
l = Cons(e, l)
}
l.reverse
}
}
@library
object ListOps {
def flatten[T](ls: List[List[T]]): List[T] = ls match {
case Cons(h, t) => h ++ flatten(t)
case Nil() => Nil()
}
def isSorted(ls: List[BigInt]): Boolean = ls match {
case Nil() => true
case Cons(_, Nil()) => true
case Cons(h1, Cons(h2, _)) if(h1 > h2) => false
case Cons(_, t) => isSorted(t)
}
def sorted(ls: List[BigInt]): List[BigInt] = ls match {
case Cons(h, t) => insSort(sorted(t), h)
case Nil() => Nil()
}
def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match {
case Nil() => Cons(v, Nil())
case Cons(h, t) =>
if (v <= h) {
Cons(v, t)
} else {
Cons(h, insSort(t, v))
}
}
}
case class Cons[T](h: T, t: List[T]) extends List[T]
case class Nil[T]() extends List[T]
@library
object ListSpecs {
def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = {
require(0 <= i && i < l.size + 1)
// proof:
(l match {
case Nil() => true
case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true
}) &&
// claim:
((l :+ t).apply(i) == (if (i < l.size) l(i) else t))
}.holds
def reverseIndex[T](l : List[T], i : BigInt) : Boolean = {
require(0 <= i && i < l.size)
(l match {
case Nil() => true
case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i)
}) &&
(l.reverse.apply(i) == l.apply(l.size - 1 - i))
}.holds
def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = {
require(0 <= i && i < l1.size + l2.size)
(l1 match {
case Nil() => true
case Cons(x,xs) => if (i==BigInt(0)) true else appendIndex[T](xs,l2,i-1)
}) &&
((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size)))
}.holds
def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = {
(l1 match {
case Nil() => true
case Cons(x,xs) => appendAssoc(xs,l2,l3)
}) &&
(((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3)))
}.holds
def snocIsAppend[T](l : List[T], t : T) : Boolean = {
(l match {
case Nil() => true
case Cons(x,xs) => snocIsAppend(xs,t)
}) &&
((l :+ t) == l ++ Cons[T](t, Nil()))
}.holds
def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = {
(l1 match {
case Nil() => true
case Cons(x,xs) => snocAfterAppend(xs,l2,t)
}) &&
((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t)))
}.holds
def snocReverse[T](l : List[T], t : T) : Boolean = {
(l match {
case Nil() => true
case Cons(x,xs) => snocReverse(xs,t)
}) &&
((l :+ t).reverse == Cons(t, l.reverse))
}.holds
def reverseReverse[T](l : List[T]) : Boolean = {
(l match {
case Nil() => true
case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x)
}) &&
(l.reverse.reverse == l)
}.holds
//// my hand calculation shows this should work, but it does not seem to be found
//def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = {
// (l1 match {
// case Nil() => true
// case Cons(x,xs) => {
// reverseAppend(xs,l2) &&
// snocAfterAppend[T](l2.reverse, xs.reverse, x) &&
// l1.reverse == (xs.reverse :+ x)
// }
// }) &&
// ((l1 ++ l2).reverse == (l2.reverse ++ l1.reverse))
//}.holds
//def associative[T,U](l1: List[T], l2: List[T], f: List[T] => U, op: (U,U) => U) = {
// f(l1 ++ l2) == op(f(l1), f(l2))
//}
//
//def existsAssoc[T](l1: List[T], l2: List[T], p: T => Boolean) = {
// associative[T, Boolean](l1, l2, _.exists(p), _ || _ )
//}.holds
//
//def forallAssoc[T](l1: List[T], l2: List[T], p: T => Boolean) = {
// associative[T, Boolean](l1, l2, _.exists(p), _ && _ )
//}.holds
//@induct
//def folds[T,R](l : List[T], z : R, f : (R,T) => R) = {
// { l match {
// case Nil() => true
// case Cons(h,t) => snocReverse[T](t, h)
// }} &&
// l.foldLeft(z)(f) == l.reverse.foldRight((x:T,y:R) => f(y,x))(z)
//}.holds
//
//Can't prove this
//@induct
//def scanVsFoldLeft[A,B](l : List[A], z: B, f: (B,A) => B): Boolean = {
// l.scanLeft(z)(f).last == l.foldLeft(z)(f)
//}.holds
@induct
def scanVsFoldRight[A,B](l: List[A], z: B, f: (A,B) => B): Boolean = {
l.scanRight(z)(f).head == l.foldRight(z)(f)
}.holds
}