-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathStrictlyOrderedList.scala
181 lines (156 loc) · 6.29 KB
/
StrictlyOrderedList.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
/* Copyright 2009-2021 EPFL, Lausanne */
/* Written by Clément Burgelin */
import stainless.annotation._
import stainless.collection._
import stainless.lang._
import stainless.proof._
object StrictlyOrderedList {
// Some helpers
def concatLast(@induct left: List[BigInt], right: List[BigInt]): Boolean = {
right.nonEmpty ==> ((left ++ right).last == right.last)
}.holds
def addLast(left: List[BigInt], elem: BigInt): Boolean = {
(left :+ elem) == (left ++ List(elem))
}.holds
def concatElem(@induct left: List[BigInt], elem: BigInt, right: List[BigInt]): Boolean = {
(left ++ (elem :: right)) == ((left :+ elem) ++ right)
}.holds
// StrictlyOrderedList is a strictly sorted List
def isInorder(l: List[BigInt]): Boolean = l match {
case Cons(h1, Cons(h2, _)) if(h1 >= h2) => false
case Cons(_, t) => isInorder(t)
case _ => true
}
// Validity spreads to sub-parts
def inorderSpread(@induct xs: List[BigInt], y: BigInt): Boolean = (
isInorder(xs :+ y) == (
isInorder(xs) &&
(xs.isEmpty || xs.last < y)
)
).holds
def inorderSpread(@induct xs: List[BigInt], ys: List[BigInt]): Boolean = (
isInorder(xs ++ ys) == (
isInorder(xs) &&
isInorder(ys) &&
(xs.isEmpty || ys.isEmpty || xs.last < ys.head)
)
).holds
def inorderSpread(@induct xs: List[BigInt], y: BigInt, ys: List[BigInt]): Boolean = (
isInorder(xs ++ (y :: ys)) == (
isInorder(xs :+ y) &&
isInorder(y :: ys)
) && inorderSpread(xs, y :: ys)
).holds
def inorderSpread(x: BigInt, @induct xs: List[BigInt], y: BigInt, ys: List[BigInt]): Boolean = (
isInorder((x :: xs) ++ (y :: ys)) == (
x < y &&
isInorder(x :: xs) &&
isInorder(xs :+ y) &&
isInorder(y :: ys)
) && inorderSpread(x :: xs, y, ys)
).holds
// Inequalities and contains
def bigger(@induct xs: List[BigInt], y: BigInt, e: BigInt): Boolean = {
require(isInorder(xs :+ y) && y <= e)
xs.forall(_ < e) && !xs.contains(e)
}.holds
def bigger(xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)) && y <= e)
inorderSpread(xs, y, ys)
bigger(xs, y, e)
xs.forall(_ < e) && !xs.contains(e)
}.holds
def smaller(y: BigInt, @induct ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(y :: ys) && e <= y)
ys.forall(e < _) && !ys.contains(e)
}.holds
def smaller(xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)) && e <= y)
inorderSpread(xs, y, ys)
smaller(y, ys, e)
ys.forall(e < _) && !ys.contains(e)
}.holds
def contains(xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)))
val l = xs ++ (y :: ys)
if(e < y) {
smaller(xs, y, ys, e) && (l.contains(e) == xs.contains(e))
} else {
bigger(xs, y, ys, e) && (l.contains(e) == (y :: ys).contains(e))
}
}
// Insert
def inorderInsert(l: List[BigInt], e: BigInt): List[BigInt] = {
require(isInorder(l))
decreases(l.length)
l match {
case Nil() => Cons(e, Nil())
case h :: t if(h == e) => l
case h :: t if(e < h) => Cons(e, l)
case h :: t => Cons(h, inorderInsert(t, e))
}
}.ensuring(res =>
isInorder(res) &&
(res.content == (l.content + e))
)
def insertLemma(@induct xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)))
inorderInsert(xs ++ (y :: ys), e) == (
if(e < y)
inorderInsert(xs, e) ++ (y :: ys)
else
xs ++ inorderInsert(y :: ys, e)
)
}.holds
def insertSmallerLemma(@induct xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)) && e < y)
inorderInsert(xs ++ (y :: ys), e) == (inorderInsert(xs, e) ++ (y :: ys))
}.holds
def insertEqualLemma(xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)) && y == e)
check(insertBiggerLemma(xs, y, ys, e))
inorderInsert(xs ++ (y :: ys), e) == (xs ++ (y :: ys))
}.holds
def insertBiggerLemma(@induct xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)) && y <= e)
inorderInsert(xs ++ (y :: ys), e) == (xs ++ inorderInsert(y :: ys, e))
}.holds
// Delete
def deleteFirst(@induct l: List[BigInt], e: BigInt): List[BigInt] = {
require(isInorder(l))
l match {
case h :: t if(h == e) => t
case h :: t => Cons(h, deleteFirst(t, e))
case _ => l
}
}.ensuring(res =>
isInorder(res) &&
(if(l.contains(e)) res.size == l.size - 1 else res == l)
)
def deleteFirstLemma(@induct xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)))
deleteFirst(xs ++ (y :: ys), e) == {
contains(xs, y, ys, e)
if(e < y) {
deleteFirst(xs, e) ++ (y :: ys)
} else {
xs ++ deleteFirst(y :: ys, e)
}
}
}.holds
def deleteSmallerLemma(@induct xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)) && e < y)
check(smaller(y, ys, e))
deleteFirst(xs ++ (y :: ys), e) == (deleteFirst(xs, e) ++ (y :: ys))
}.holds
def deleteEqualLemma(xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)) && y == e)
check(deleteBiggerLemma(xs, y, ys, e))
deleteFirst(xs ++ (y :: ys), e) == (xs ++ ys)
}.holds
def deleteBiggerLemma(@induct xs: List[BigInt], y: BigInt, ys: List[BigInt], e: BigInt): Boolean = {
require(isInorder(xs ++ (y :: ys)) && y <= e)
check(bigger(xs, y, e))
deleteFirst(xs ++ (y :: ys), e) == (xs ++ deleteFirst(y :: ys, e))
}.holds
}