-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSystemF.scala
380 lines (339 loc) · 12.2 KB
/
SystemF.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
/*
* Copyright 2009-2022 EPFL, Lausanne
*
* Authors: Andrea Gilot, Noé De Santo
*/
import stainless.lang._
import stainless.collection._
import stainless.annotation._
object SystemF {
sealed trait Type {
def freeVars: List[BigInt] = {
this match {
case BasicType(_) => Nil()
case ArrowType(t1, t2) => t1.freeVars ++ t2.freeVars
case VariableType(v) => List[BigInt](v)
case UniversalType(body) => body.freeVars.filter(x => x > 0).map(x => x - 1)
}
}
def hasFreeVariablesIn(c: BigInt, d: BigInt): Boolean = {
require(c >= 0)
require(d >= 0)
this match {
case BasicType(_) => false
case ArrowType(t1, t2) => t1.hasFreeVariablesIn(c, d) || t2.hasFreeVariablesIn(c, d)
case VariableType(v) => (c <= v) && (v < c+d)
case UniversalType(body) => body.hasFreeVariablesIn(c+1, d)
}
}.ensuring(res => (d == 0) ==> !res)
def isClosed: Boolean = {
def rec(t: Type, c: BigInt): Boolean = {
t match {
case BasicType(_) => true
case ArrowType(t1, t2) => rec(t1, c) && rec(t2, c)
case VariableType(v) => v < c
case UniversalType(body) => rec(body, c+1)
}
}
rec(this, 0)
}
}
case class BasicType(s: String) extends Type
case class ArrowType(t1: Type, t2: Type) extends Type
case class VariableType(v: BigInt) extends Type { require(v >= 0) }
case class UniversalType(t: Type) extends Type
type Environment = List[Type]
def hasFreeVariablesIn(env: Environment, c: BigInt, d: BigInt): Boolean = {
require(c >= 0)
require(d >= 0)
env match {
case Nil() => false
case Cons(h, t) => h.hasFreeVariablesIn(c, d) || hasFreeVariablesIn(t, c, d)
}
}.ensuring(res =>
( !res ==> env.forall(!_.hasFreeVariablesIn(c, d)) ) &&
( res ==> env.exists(_.hasFreeVariablesIn(c, d)) ) &&
( (d == 0) ==> !res )
)
sealed trait Term {
def isValue: Boolean = {
this match {
case Abs(_, _) => true
case TAbs(_) => true
case _ => false
}
}
def hasFreeVariablesIn(c: BigInt, d: BigInt): Boolean = {
require(c >= 0)
require(d >= 0)
this match {
case Var(k) => (c <= k) && (k < c+d)
case Abs(_, body) => body.hasFreeVariablesIn(c+1, d)
case App(t1, t2) => t1.hasFreeVariablesIn(c, d) || t2.hasFreeVariablesIn(c, d)
case Fix(f) => f.hasFreeVariablesIn(c, d)
case TAbs(body) => body.hasFreeVariablesIn(c, d)
case TApp(t, _) => t.hasFreeVariablesIn(c, d)
}
}.ensuring(res => (d == 0) ==> !res)
def isClosed: Boolean = {
def rec(t: Term, c: BigInt): Boolean = {
t match {
case Var(k) => k < c
case Abs(_, body) => rec(body, c+1)
case App(t1, t2) => rec(t1, c) && rec(t2, c)
case Fix(f) => rec(f, c)
case TAbs(body) => rec(body, c)
case TApp(t, _) => rec(t, c)
}
}
rec(this, 0)
}
def hasFreeTypeVariablesIn(c: BigInt, d: BigInt): Boolean = {
require(c >= 0)
require(d >= 0)
this match {
case Var(k) => false
case Abs(typ, body) => typ.hasFreeVariablesIn(c, d) || body.hasFreeTypeVariablesIn(c, d)
case App(t1, t2) => t1.hasFreeTypeVariablesIn(c, d) || t2.hasFreeTypeVariablesIn(c, d)
case Fix(f) => f.hasFreeTypeVariablesIn(c, d)
case TAbs(body) => body.hasFreeTypeVariablesIn(c+1, d)
case TApp(t, typ) => t.hasFreeTypeVariablesIn(c, d) || typ.hasFreeVariablesIn(c, d)
}
}.ensuring(res => (d == 0) ==> !res)
}
case class Var(k: BigInt) extends Term { require(k >= 0) }
case class Abs(argType: Type, body: Term) extends Term
case class App(t1: Term, t2: Term) extends Term
case class Fix(t: Term) extends Term
case class TAbs(t: Term) extends Term
case class TApp(t: Term, typ: Type) extends Term
}
object SystemFProperties {
import SystemF._
object Terms {
@opaque @pure
def boundRangeDecrease(t: Term, c: BigInt, d1: BigInt, d2: BigInt): Unit = {
require(d1 >= 0 && d2 >= 0)
require(c >= 0)
require(d2 <= d1)
require(!t.hasFreeVariablesIn(c, d1))
t match{
case Var(_) => ()
case Abs(targ, body) => {
boundRangeDecrease(body, c + 1, d1, d2)
}
case App(t1, t2) => {
boundRangeDecrease(t1, c, d1, d2)
boundRangeDecrease(t2, c, d1, d2)
}
case Fix(f) => boundRangeDecrease(f, c, d1, d2)
case TAbs(body) => boundRangeDecrease(body, c, d1, d2)
case TApp(t, _) => boundRangeDecrease(t, c, d1, d2)
}
}.ensuring(!t.hasFreeVariablesIn(c, d2))
@opaque @pure
def boundRangeIncreaseCutoff(t: Term, c1: BigInt, c2: BigInt, d: BigInt): Unit = {
require(c1 >= 0 && c2 >= 0)
require(0 <= d && c2 - c1 <= d)
require(c1 <= c2)
require(!t.hasFreeVariablesIn(c1, d))
t match {
case Var(_) => ()
case Abs(targ, body) => boundRangeIncreaseCutoff(body, c1 + 1, c2 + 1, d)
case App(t1, t2) => {
boundRangeIncreaseCutoff(t1, c1, c2, d)
boundRangeIncreaseCutoff(t2, c1, c2, d)
}
case Fix(f) => boundRangeIncreaseCutoff(f, c1, c2, d)
case TAbs(body) => boundRangeIncreaseCutoff(body, c1, c2, d)
case TApp(t, _) => boundRangeIncreaseCutoff(t, c1, c2, d)
}
}.ensuring(!t.hasFreeVariablesIn(c2, d - (c2 - c1)))
@opaque @pure
def boundRangeConcatenation(t: Term, a: BigInt, b: BigInt, c: BigInt): Unit = {
require(a >= 0)
require(b >= 0)
require(c >= 0)
require(!t.hasFreeVariablesIn(a, b))
require(!t.hasFreeVariablesIn(a + b, c))
t match{
case Var(k) => ()
case Abs(targ, body) => {
boundRangeConcatenation(body, a + 1, b, c)
}
case App(t1, t2) => {
boundRangeConcatenation(t1, a, b, c)
boundRangeConcatenation(t2, a, b, c)
}
case Fix(f) => boundRangeConcatenation(f, a, b, c)
case TAbs(body) => boundRangeConcatenation(body, a, b, c)
case TApp(t, _) => boundRangeConcatenation(t, a, b, c)
}
}.ensuring(!t.hasFreeVariablesIn(a, b + c))
}
object Types {
def hasFreeVariablesInCompleteness(t: Type, k: BigInt, c: BigInt, d: BigInt): Unit = {
require(t.freeVars.contains(k))
require(c >= 0)
require(d >= 0)
require(c <= k && k < c + d)
t match{
case BasicType(_) => ()
case ArrowType(t1, t2) => {
if(t1.freeVars.contains(k)){
hasFreeVariablesInCompleteness(t1, k, c, d)
}
else{
hasFreeVariablesInCompleteness(t2, k, c, d)
}
}
case VariableType(_) => ()
case UniversalType(body) => {
ListProperties.mapInvertAddContains(body.freeVars.filter(x => x > 0), k, 1)
hasFreeVariablesInCompleteness(body, k + 1, c + 1, d)
}
}
}.ensuring(t.hasFreeVariablesIn(c, d))
def hasFreeVariablesInSoundness(t: Type, c: BigInt, d: BigInt): Unit = {
require(c >= 0)
require(d >= 0)
require(t.freeVars.forall(x => x < c || x >= c + d))
t match{
case BasicType(_) => ()
case ArrowType(t1, t2) => {
ListProperties.forallConcat(t1.freeVars, t2.freeVars, (x: BigInt) => x < c || x >= c + d)
hasFreeVariablesInSoundness(t1, c, d)
hasFreeVariablesInSoundness(t2, c, d)
}
case VariableType(_) => ()
case UniversalType(body) => {
ListProperties.forallMapLemma(body.freeVars.filter(x => x > 0), c, d, 1)
ListProperties.forallFilterLemma(body.freeVars, c, d, 1)
hasFreeVariablesInSoundness(body, c + 1, d)
}
}
}.ensuring(!t.hasFreeVariablesIn(c, d))
@opaque @pure
def boundRangeDecrease(t: Type, c: BigInt, d1: BigInt, d2: BigInt): Unit = {
require(d1 >= 0 && d2 >= 0)
require(c >= 0)
require(d2 <= d1)
require(!t.hasFreeVariablesIn(c, d1))
t match{
case BasicType(_) => ()
case ArrowType(t1, t2) => {
boundRangeDecrease(t1, c, d1, d2)
boundRangeDecrease(t2, c, d1, d2)
}
case VariableType(v) => ()
case UniversalType(body) => boundRangeDecrease(body, c+1, d1, d2)
}
}.ensuring(!t.hasFreeVariablesIn(c, d2))
@opaque @pure
def boundRangeIncreaseCutoff(t: Type, c1: BigInt, c2: BigInt, d: BigInt): Unit = {
require(c1 >= 0 && c2 >= 0)
require(0 <= d && c2 - c1 <= d)
require(c1 <= c2)
require(!t.hasFreeVariablesIn(c1, d))
t match {
case BasicType(_) => ()
case ArrowType(t1, t2) => {
boundRangeIncreaseCutoff(t1, c1, c2, d)
boundRangeIncreaseCutoff(t2, c1, c2, d)
}
case VariableType(v) => ()
case UniversalType(body) => boundRangeIncreaseCutoff(body, c1 + 1, c2 + 1, d)
}
}.ensuring(!t.hasFreeVariablesIn(c2, d - (c2 - c1)))
@opaque @pure
def boundRangeConcatenation(t: Type, a: BigInt, b: BigInt, c: BigInt): Unit = {
require(a >= 0)
require(b >= 0)
require(c >= 0)
require(!t.hasFreeVariablesIn(a, b))
require(!t.hasFreeVariablesIn(a + b, c))
t match{
case BasicType(_) => ()
case ArrowType(t1, t2) => {
boundRangeConcatenation(t1, a, b, c)
boundRangeConcatenation(t2, a, b, c)
}
case VariableType(v) => ()
case UniversalType(body) => boundRangeConcatenation(body, a + 1, b, c)
}
}.ensuring(!t.hasFreeVariablesIn(a, b + c))
@opaque @pure
def boundRangeDecrease(t: Term, c: BigInt, d1: BigInt, d2: BigInt): Unit = {
require(d1 >= 0 && d2 >= 0)
require(c >= 0)
require(d2 <= d1)
require(!t.hasFreeTypeVariablesIn(c, d1))
t match{
case Var(_) => ()
case Abs(targ, body) => {
boundRangeDecrease(targ, c, d1, d2)
boundRangeDecrease(body, c, d1, d2)
}
case App(t1, t2) => {
boundRangeDecrease(t1, c, d1, d2)
boundRangeDecrease(t2, c, d1, d2)
}
case Fix(f) => boundRangeDecrease(f, c, d1, d2)
case TAbs(body) => boundRangeDecrease(body, c+1, d1, d2)
case TApp(t, typ) => {
boundRangeDecrease(t, c, d1, d2)
boundRangeDecrease(typ, c, d1, d2)
}
}
}.ensuring(!t.hasFreeTypeVariablesIn(c, d2))
@opaque @pure
def boundRangeIncreaseCutoff(t: Term, c1: BigInt, c2: BigInt, d: BigInt): Unit = {
require(c1 >= 0 && c2 >= 0)
require(0 <= d && c2 - c1 <= d)
require(c1 <= c2)
require(!t.hasFreeTypeVariablesIn(c1, d))
t match{
case Var(_) => ()
case Abs(targ, body) => {
boundRangeIncreaseCutoff(targ, c1, c2, d)
boundRangeIncreaseCutoff(body, c1, c2, d)
}
case App(t1, t2) => {
boundRangeIncreaseCutoff(t1, c1, c2, d)
boundRangeIncreaseCutoff(t2, c1, c2, d)
}
case Fix(f) => boundRangeIncreaseCutoff(f, c1, c2, d)
case TAbs(body) => boundRangeIncreaseCutoff(body, c1+1, c2+1, d)
case TApp(t, typ) => {
boundRangeIncreaseCutoff(t, c1, c2, d)
boundRangeIncreaseCutoff(typ, c1, c2, d)
}
}
}.ensuring(!t.hasFreeTypeVariablesIn(c2, d - (c2 - c1)))
@opaque @pure
def boundRangeConcatenation(t: Term, a: BigInt, b: BigInt, c: BigInt): Unit = {
require(a >= 0)
require(b >= 0)
require(c >= 0)
require(!t.hasFreeTypeVariablesIn(a, b))
require(!t.hasFreeTypeVariablesIn(a + b, c))
t match{
case Var(_) => ()
case Abs(targ, body) => {
boundRangeConcatenation(targ, a, b, c)
boundRangeConcatenation(body, a, b, c)
}
case App(t1, t2) => {
boundRangeConcatenation(t1, a, b, c)
boundRangeConcatenation(t2, a, b, c)
}
case Fix(f) => boundRangeConcatenation(f, a, b, c)
case TAbs(body) => boundRangeConcatenation(body, a+1, b, c)
case TApp(t, typ) => {
boundRangeConcatenation(t, a, b, c)
boundRangeConcatenation(typ, a, b, c)
}
}
}.ensuring(!t.hasFreeTypeVariablesIn(a, b + c))
}
}