-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathGodelNumbering.scala
550 lines (497 loc) · 18.3 KB
/
GodelNumbering.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
import stainless.lang._
import stainless.equations._
import stainless.annotation._
import stainless.proof.check
object GodelNumbering {
sealed abstract class Nat {
def +(that: Nat): Nat = {
decreases(this)
this match {
case Zero => that
case Succ(n) => Succ(n + that)
}
}
def *(that: Nat): Nat = {
decreases(this)
this match {
case Zero => Zero
case Succ(n) => (n * that) + that
}
}
def -(that: Nat): Nat = {
decreases(this)
((this, that) match {
case (Succ(n1), Succ(n2)) => n1 - n2
case _ => this
})
}.ensuring { res =>
res.repr <= repr &&
((this > Zero && that > Zero) ==> res.repr < repr)
}
def <(that: Nat): Boolean = {
decreases(this)
((this, that) match {
case (Succ(n1), Succ(n2)) => n1 < n2
case (Zero, Succ(_)) => true
case _ => false
})
}.ensuring(_ == repr < that.repr)
def <=(that: Nat): Boolean = (this < that) || (this == that)
def >(that: Nat): Boolean = !(this < that) && (this != that)
def >=(that: Nat): Boolean = (this > that) || (this == that)
def /(that: Nat): Nat = {
require(that > Zero)
decreases(repr)
if (this < that) Zero else
Succ((this - that) / that)
}.ensuring { res =>
res.repr <= repr &&
((this > Zero && that > One) ==> res.repr < repr)
}
def %(that: Nat): Nat = {
require(that > Zero)
decreases(repr)
if (this < that) this
else (this - that) % that
}
def repr: BigInt = {
decreases(this)
this match {
case Zero => BigInt(0)
case Succ(n) => n.repr + BigInt(1)
}
}.ensuring(_ >= BigInt(0))
}
case object Zero extends Nat
case class Succ(n: Nat) extends Nat
lazy val One = Succ(Zero)
lazy val Two = Succ(One)
@induct @opaque @inlineOnce
def plus_zero(n: Nat): Unit = {
()
}.ensuring(_ => n + Zero == n)
@opaque @inlineOnce
def zero_plus(n: Nat): Unit = {
()
}.ensuring(_ => Zero + n == n)
@opaque @inlineOnce
def minus_identity(@induct n: Nat): Unit = {
()
}.ensuring(_ =>
n - n == Zero
)
@opaque @inlineOnce
def associative_plus(@induct n1: Nat, n2: Nat, n3: Nat): Unit = {
()
}.ensuring(_ => (n1 + n2) + n3 == n1 + (n2 + n3))
@opaque @inlineOnce
def plus_succ(@induct n1: Nat, n2: Nat): Unit = {
()
}.ensuring(_ => n1 + Succ(n2) == Succ(n1 + n2))
@opaque @inlineOnce
def commutative_plus(n1: Nat, n2: Nat): Unit = {
decreases(n1, n2)
n1 match {
case Zero =>
plus_zero(n2)
case Succ(p1) => {
n1 + n2 ==:| trivial |:
Succ(p1) + n2 ==:| trivial |:
(Succ(p1 + n2): Nat) ==:| commutative_plus(p1, n2) |:
(Succ(n2 + p1): Nat) ==:| plus_succ(n2, p1) |:
n2 + Succ(p1) ==:| trivial |:
n2 + n1
}.qed
}
}.ensuring(_ => n1 + n2 == n2 + n1)
@opaque @inlineOnce
def distributive_times(n1: Nat, n2: Nat, n3: Nat): Unit = {
decreases(n1)
n1 match {
case Zero => ()
case Succ(p1) => {
Succ(p1) * (n2 + n3) ==:| trivial |:
p1 * (n2 + n3) + (n2 + n3) ==:| distributive_times(p1, n2, n3) |:
(p1 * n2) + (p1 * n3) + (n2 + n3) ==:| associative_plus((p1 * n2) + (p1 * n3), n2, n3) |:
(p1 * n2) + (p1 * n3) + n2 + n3 ==:| associative_plus(p1 * n2, p1 * n3, n2) |:
(p1 * n2) + ((p1 * n3) + n2) + n3 ==:| commutative_plus(p1 * n3, n2) |:
(p1 * n2) + (n2 + (p1 * n3)) + n3 ==:| associative_plus(p1 * n2, n2, p1 * n3) |:
((p1 * n2) + n2) + (p1 * n3) + n3 ==:| associative_plus((p1 * n2) + n2, p1 * n3, n3) |:
((p1 * n2) + n2) + ((p1 * n3) + n3) ==:| trivial |:
(n1 * n2) + (n1 * n3)
}.qed
}
}.ensuring(_ => n1 * (n2 + n3) == (n1 * n2) + (n1 * n3))
@opaque @inlineOnce
def commutative_times(n1: Nat, n2: Nat): Unit = {
decreases(n1,n2)
(n1, n2) match {
case (Zero, Zero) => ()
case (Zero, Succ(p2)) => commutative_times(n1, p2)
case (Succ(p1), Zero) => commutative_times(p1, n2)
case (Succ(p1), Succ(p2)) => {
n1 * n2 ==:| trivial |:
(p1 * n2) + n2 ==:| commutative_times(p1, n2) |:
(n2 * p1) + n2 ==:| trivial |:
((p2 * p1) + p1) + n2 ==:| commutative_times(p1, p2) |:
((p1 * p2) + p1) + n2 ==:| associative_plus(p1 * p2, p1, n2) |:
(p1 * p2) + (p1 + n2) ==:| commutative_plus(p1, n2) |:
(p1 * p2) + (n2 + p1) ==:| { associative_plus(p2, One, p1); commutative_plus(p2, One) } |:
(p1 * p2) + (p2 + n1) ==:| associative_plus(p1 * p2, p2, n1) |:
((p1 * p2) + p2) + n1 ==:| trivial |:
(n1 * p2) + n1 ==:| commutative_times(n1, p2) |:
(p2 * n1) + n1 ==:| trivial |:
n2 * n1
}.qed
}
}.ensuring(_ => n1 * n2 == n2 * n1)
@opaque @inlineOnce
def distributive_times2(n1: Nat, n2: Nat, n3: Nat): Unit = {
commutative_times(n1 + n2, n3)
distributive_times(n3, n1, n2)
commutative_times(n1, n3)
commutative_times(n2, n3)
()
}.ensuring(_ => (n1 + n2) * n3 == (n1 * n3) + (n2 * n3))
@opaque @inlineOnce
def associative_times(n1: Nat, n2: Nat, n3: Nat): Unit = {
decreases(n1)
n1 match {
case Zero => ()
case Succ(p1) => {
n1 * (n2 * n3) ==:| trivial |:
(p1 * (n2 * n3)) + (n2 * n3) ==:| associative_times(p1, n2, n3) |:
((p1 * n2) * n3) + (n2 * n3) ==:| commutative_plus((p1 * n2) * n3, n2 * n3) |:
(n2 * n3) + ((p1 * n2) * n3) ==:| distributive_times2(n2, p1 * n2, n3) |:
(n2 + (p1 * n2)) * n3 ==:| commutative_plus(n2, p1 * n2) |:
((p1 * n2) + n2) * n3 ==:| trivial |:
(n1 * n2) * n3
}.qed
}
}.ensuring(_ => n1 * (n2 * n3) == (n1 * n2) * n3)
@opaque @inlineOnce
def succ_<(n1: Nat, n2: Nat): Unit = {
require(n1 <= n2)
decreases(n1)
n1 match {
case Zero => ()
case Succ(n) =>
val Succ(p2) = n2: @unchecked
succ_<(n, p2)
}
}.ensuring(_ => n1 < Succ(n2))
@opaque @inlineOnce
def succ_<=(n1: Nat, n2: Nat): Unit = {
require(n1 < n2)
decreases(n2)
n2 match {
case Succ(p2) if n1 != p2 =>
pred_<(n1, n2)
succ_<=(n1, p2)
case _ => ()
}
}.ensuring(_ => Succ(n1) <= n2)
@opaque @inlineOnce
def pred_<(n1: Nat, n2: Nat): Unit = {
require(n1 < n2)
decreases(n1)
val Succ(n) = n2: @unchecked
n2 match {
case Succ(n) if n == n1 => ()
case Succ(p2) => n1 match {
case Zero => ()
case Succ(p1) => pred_<(p1, p2)
}
}
}.ensuring {_ =>
val Succ(n) = n2: @unchecked
(n1 != n) ==> (n1 < n)
}
@opaque @inlineOnce
def pred_<=(n1: Nat, n2: Nat): Unit = {
require(n1 > Zero && n1 <= n2)
val Succ(p1) = n1: @unchecked
succ_<(p1, p1)
if (n1 != n2) transitive_<(p1, n1, n2)
}.ensuring { _ =>
val Succ(p1) = n1: @unchecked
p1 < n2
}
@opaque @inlineOnce
def transitive_<(n1: Nat, n2: Nat, n3: Nat): Unit = {
require(n1 < n2 && n2 < n3)
decreases(n3)
n3 match {
case Zero => ()
case Succ(n) if n == n2 => succ_<(n1, n)
case Succ(n) =>
pred_<(n2, n3)
transitive_<(n1, n2, n)
succ_<(n1, n)
}
}.ensuring(_ => n1 < n3)
@opaque @inlineOnce
def antisymmetric_<(n1: Nat, n2: Nat): Unit = {
decreases(n1)
(n1, n2) match {
case (Succ(p1), Succ(p2)) => antisymmetric_<(p1, p2)
case _ => ()
}
}.ensuring(_ => n1 < n2 == !(n2 <= n1))
@opaque @inlineOnce
def plus_<(n1: Nat, n2: Nat, n3: Nat): Unit = {
require(n2 < n3)
decreases(n3)
n3 match {
case Succ(p3) if n2 == p3 =>
plus_succ(n1, n2)
succ_<(n1 + n2, n1 + n2)
case Succ(p3) =>
plus_succ(n1, p3)
pred_<(n2, n3)
plus_<(n1, n2, p3)
succ_<(n1 + n2, n1 + p3)
}
}.ensuring(_ => n1 + n2 < n1 + n3)
@opaque @inlineOnce
def plus_<(n1: Nat, n2: Nat, n3: Nat, n4: Nat): Unit = {
require(n1 <= n3 && n2 <= n4)
decreases(n3)
n3 match {
case Zero => ()
case Succ(_) if n1 == n3 && n2 == n4 => ()
case Succ(_) if n1 == n3 => plus_<(n1, n2, n4)
case Succ(p3) =>
pred_<(n1, n3)
plus_<(n1, n2, p3, n4)
succ_<(n1 + n2, p3 + n4)
}
}.ensuring(_ => n1 + n2 <= n3 + n4)
@opaque @inlineOnce
def associative_plus_minus(n1: Nat, n2: Nat, n3: Nat): Unit = {
require(n2 >= n3)
decreases(n2)
(n2, n3) match {
case (Succ(p2), Succ(p3)) =>
{
(n1 + Succ(p2)) - Succ(p3) ==:| commutative_plus(One, p2) |:
(n1 + (p2 + One)) - Succ(p3) ==:| associative_plus(n1, p2, One) |:
((n1 + p2) + One) - Succ(p3) ==:| commutative_plus(n1 + p2, One) |:
Succ(n1 + p2) - Succ(p3) ==:| trivial |:
(n1 + p2) - p3 ==:| associative_plus_minus(n1, p2, p3) |:
n1 + (p2 - p3) ==:| trivial |:
n1 + (n2 - n3)
}.qed
case _ => ()
}
}.ensuring(_ => (n1 + n2) - n3 == n1 + (n2 - n3))
@opaque @inlineOnce
def additive_inverse(n1: Nat, n2: Nat): Unit = {
associative_plus_minus(n1, n2, n2)
minus_identity(n2)
plus_zero(n1)
}.ensuring(_ => n1 + n2 - n2 == n1)
@opaque @inlineOnce
def multiplicative_inverse(n1: Nat, n2: Nat): Unit = {
require(n2 > Zero)
decreases(n1)
n1 match {
case Succ(p1) =>
{
(n1 * n2) / n2 ==:| trivial |:
(p1 * n2 + n2) / n2 ==:|
{ commutative_plus(p1 * n2, n2); increasing_plus(n2, p1 * n2); antisymmetric_<(p1 * n2 + n2, n2) } |:
(Succ(((p1 * n2 + n2) - n2) / n2) : Nat) ==:| additive_inverse(p1 * n2, n2) |:
(Succ((p1 * n2) / n2) : Nat) ==:| multiplicative_inverse(p1, n2) |:
n1
}.qed
case _ => ()
}
}.ensuring(_ => (n1 * n2) / n2 == n1)
@induct @opaque @inlineOnce
def increasing_plus(n1: Nat, n2: Nat): Unit = {
()
}.ensuring(_ => n1 <= n1 + n2)
@induct @opaque @inlineOnce
def increasing_plus_strict(n1: Nat, n2: Nat): Unit = {
require(n2 > Zero)
()
}.ensuring(_ => n1 < n1 + n2)
@opaque @inlineOnce
def increasing_times(n1: Nat, n2: Nat): Unit = {
require(n1 > Zero && n2 > Zero)
decreases(n1)
n1 match {
case Succ(Zero) => ()
case Succ(p1) =>
increasing_times(p1, n2)
increasing_plus_strict(p1 * n2, n2)
if (p1 != p1 * n2)
transitive_<(p1, p1 * n2, p1 * n2 + n2)
succ_<=(p1, p1 * n2 + n2)
check(n1 <= n1 * n2)
()
}
}.ensuring(_ => n1 <= n1 * n2)
def pow(n1: Nat, n2: Nat): Nat = {
decreases(n2)
n2 match {
case Succ(n) => n1 * pow(n1, n)
case Zero => One
}
}
@opaque @inlineOnce
def pow_positive(n1: Nat, n2: Nat): Unit = {
require(n1 > Zero)
decreases(n2)
n2 match {
case Succ(p2) =>
pow_positive(n1, p2)
increasing_times(n1, pow(n1, p2))
case _ => ()
}
}.ensuring(_ => pow(n1, n2) > Zero)
def isEven(n: Nat): Boolean = {
decreases(n)
n match {
case Zero => true
case Succ(Zero) => false
case Succ(n) => !isEven(n)
}
}
@opaque @inlineOnce
def times_two_even(n: Nat): Unit = {
decreases(n)
n match {
case Zero => ()
case Succ(p) => {
isEven(Two * n) ==:| commutative_times(Two, n) |:
isEven(n * Two) ==:| trivial |:
isEven(p * Two + Two) ==:| commutative_plus(p * Two, Two) |:
isEven(p * Two) ==:| commutative_times(Two, p) |:
isEven(Two * p) ==:| times_two_even(p) |:
true
}.qed
}
}.ensuring(_ => isEven(Two * n))
@opaque @inlineOnce
def times_two_plus_one_odd(n: Nat): Unit = {
times_two_even(n)
assert(!isEven(One + Two * n))
commutative_plus(Two * n, One)
}.ensuring(_ => !isEven(Two * n + One))
@opaque @inlineOnce
def succ_times_two_odd(n: Nat): Unit = {
times_two_plus_one_odd(n)
commutative_plus(Two * n, One)
}.ensuring(_ => !isEven(Succ(Two * n)))
@opaque @inlineOnce
def power_two_even(n: Nat): Unit = {
require(n > Zero)
n match {
case Succ(p) => times_two_even(pow(Two, p))
}
}.ensuring(_ => isEven(pow(Two, n)))
def pair(n1: Nat, n2: Nat): Nat = pow(Two, n1) * (Two * n2 + One) - One
def log2_and_remainder(n: Nat): (Nat, Nat) = {
decreases(n.repr)
if (isEven(n) && n > Zero) {
val (a, b) = log2_and_remainder(n / Two)
(Succ(a), b)
} else {
(Zero, n)
}
}
def project(n: Nat): (Nat, Nat) = {
val (a, b) = log2_and_remainder(Succ(n))
(a, (b - One) / Two)
}
@opaque @inlineOnce
def assoc_plus_minus_one(n: Nat, n2: Nat): Unit = {
pow_positive(Two, n)
commutative_plus(Two * n2, One)
increasing_times(pow(Two, n), (Two * n2 + One))
associative_plus_minus(One, pow(Two, n) * (Two * n2 + One), One)
}.ensuring(_ =>
One + (pow(Two, n) * (Two * n2 + One) - One) ==
(One + pow(Two, n) * (Two * n2 + One)) - One
)
@opaque @inlineOnce
def project_1_inverse(n1: Nat, n2: Nat): Unit = {
decreases(n1)
n1 match {
case Succ(p1) =>
{
log2_and_remainder(Succ(pair(n1, n2))) ==:|
trivial |:
log2_and_remainder(Succ(pow(Two, n1) * (Two * n2 + One) - One)) ==:|
trivial |:
log2_and_remainder(One + (pow(Two, n1) * (Two * n2 + One) - One)) ==:|
assoc_plus_minus_one(n1,n2) |:
log2_and_remainder((One + pow(Two, n1) * (Two * n2 + One)) - One) ==:|
commutative_plus(One, pow(Two, n1) * (Two * n2 + One)) |:
log2_and_remainder(pow(Two, n1) * (Two * n2 + One) + One - One) ==:|
additive_inverse(pow(Two, n1) * (Two * n2 + One), One) |:
log2_and_remainder(pow(Two, n1) * (Two * n2 + One)) ==:|
trivial |:
log2_and_remainder((Two * pow(Two, p1)) * (Two * n2 + One)) ==:|
associative_times(Two, pow(Two, p1), Two * n2 + One) |:
log2_and_remainder(Two * (pow(Two, p1) * (Two * n2 + One))) ==:|
additive_inverse(pow(Two, p1) * (Two * n2 + One), One) |:
log2_and_remainder(Two * (pow(Two, p1) * (Two * n2 + One) + One - One)) ==:|
commutative_plus(One, pow(Two, p1) * (Two * n2 + One)) |:
log2_and_remainder(Two * (One + pow(Two, p1) * (Two * n2 + One) - One)) ==:|
assoc_plus_minus_one(p1,n2) |:
log2_and_remainder(Two * Succ(pow(Two, p1) * (Two * n2 + One) - One)) ==:|
trivial |:
log2_and_remainder(Two * Succ(pair(p1, n2))) ==:|
{ times_two_even(Succ(pair(p1, n2)))
project_1_inverse(p1, n2)
commutative_times(Two, Succ(pair(p1, n2)))
multiplicative_inverse(Succ(pair(p1, n2)), Two) } |:
(n1, Two * n2 + One)
}.qed
case _ =>
{
log2_and_remainder(Succ(pair(n1, n2))) ==:|
trivial |:
log2_and_remainder(Succ(pow(Two, n1) * (Two * n2 + One) - One)) ==:|
trivial |:
log2_and_remainder(Succ(Two * n2 + One - One)) ==:|
additive_inverse(Two * n2, One) |:
log2_and_remainder(Succ(Two * n2)) ==:|
succ_times_two_odd(n2) |:
((Zero, Succ(Two * n2)): (Nat, Nat)) ==:|
commutative_plus(Two * n2, One) |:
((Zero, Two * n2 + One): (Nat, Nat))
}.qed
}
}.ensuring(_ => log2_and_remainder(Succ(pair(n1, n2))) == (n1, (Two * n2 + One)))
@opaque @inlineOnce
def inverse_lemma(n1: Nat, n2: Nat): Unit = {
val (p1, remainder) = log2_and_remainder(Succ(pair(n1, n2)))
val p2 = (remainder - One) / Two
{
project(pair(n1, n2)) ==:| trivial |:
(p1, p2) ==:| trivial |:
(p1, (remainder - One) / Two) ==:| project_1_inverse(n1, n2) |:
(n1, ((Two * n2 + One) - One) / Two) ==:| additive_inverse(Two * n2, One) |:
(n1, (Two * n2) / Two) ==:| commutative_times(Two, n2) |:
(n1, (n2 * Two) / Two) ==:| multiplicative_inverse(n2, Two) |:
(n1, n2)
}.qed
}.ensuring(_ => project(pair(n1, n2)) == (n1, n2))
@opaque @inlineOnce
def pair_unique(n1: Nat, n2: Nat, n3: Nat, n4: Nat): Unit = {
if (pair(n1, n2) == pair(n3, n4)) {
assert(project(pair(n1, n2)) == project(pair(n3, n4)))
inverse_lemma(n1, n2)
inverse_lemma(n3, n4)
assert((n1, n2) == (n3, n4))
((n1, n2) == (n3, n4) ==:| trivial |: true).qed
} else {
assert((n1, n2) != (n3, n4))
((n1, n2) == (n3, n4) ==:| trivial |: false).qed
}
}.ensuring(_ => (pair(n1, n2) == pair(n3, n4)) == ((n1, n2) == (n3, n4)))
}