-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefinitions2.lean
680 lines (567 loc) · 26.9 KB
/
definitions2.lean
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
-- second part of definitions
import .definitions1 .qiaux
-- ################################
-- ### QUANTIFIER INSTANTIATION ###
-- ################################
-- first part is in definitions1.lean
-- the following definitions need some additional lemmas from qiaux.lean to prove termination
-- lift_all(P) performs repeated lifting of quantifiers in positive
-- positions until there is no more quantifier to be lifted
def prop.lift_all: prop → prop
| P :=
let r := P.lift_p P.fresh_var in
let z := r in
have h: z = r, from rfl,
@option.cases_on prop (λr, (z = r) → prop) r (
assume : z = none,
P
) (
assume P': prop,
assume : z = (some P'),
have r_id: r = (some P'), from eq.trans h this,
have P'.num_quantifiers < P.num_quantifiers,
from (lifted_prop_smaller P').left r_id,
prop.lift_all P'
) rfl
using_well_founded {
rel_tac := λ _ _, `[exact ⟨_, measure_wf $ λ s, s.num_quantifiers ⟩],
dec_tac := tactic.assumption
}
-- erase_p(P) / erase_n(P) replaces all triggers and quantifiers
-- in either positive or negative position with 'true'
mutual def prop.erased_p, prop.erased_n
with prop.erased_p: prop → vc
| (prop.term t) := vc.term t
| (prop.not P) := have P.sizeof < P.not.sizeof, from sizeof_prop_not,
vc.not P.erased_n
| (prop.and P₁ P₂) := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁,
have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂,
P₁.erased_p ⋀ P₂.erased_p
| (prop.or P₁ P₂) := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁,
have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂,
P₁.erased_p ⋁ P₂.erased_p
| (prop.pre t₁ t₂) := vc.pre t₁ t₂
| (prop.pre₁ op t) := vc.pre₁ op t
| (prop.pre₂ op t₁ t₂) := vc.pre₂ op t₁ t₂
| (prop.post t₁ t₂) := vc.post t₁ t₂
| (prop.call _) := vc.term value.true
| (prop.forallc x P) := vc.term value.true
| (prop.exis x P) := have P.sizeof < (prop.exis x P).sizeof, from sizeof_prop_exis,
vc.not (vc.univ x (vc.not P.erased_p))
with prop.erased_n: prop → vc
| (prop.term t) := vc.term t
| (prop.not P) := have P.sizeof < P.not.sizeof, from sizeof_prop_not,
vc.not P.erased_p
| (prop.and P₁ P₂) := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁,
have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂,
P₁.erased_n ⋀ P₂.erased_n
| (prop.or P₁ P₂) := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁,
have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂,
P₁.erased_n ⋁ P₂.erased_n
| (prop.pre t₁ t₂) := vc.pre t₁ t₂
| (prop.pre₁ op t) := vc.pre₁ op t
| (prop.pre₂ op t₁ t₂) := vc.pre₂ op t₁ t₂
| (prop.post t₁ t₂) := vc.post t₁ t₂
| (prop.call _) := vc.term value.true
| (prop.forallc x P) := have P.sizeof < (prop.forallc x P).sizeof, from sizeof_prop_forall,
vc.univ x P.erased_n
| (prop.exis x P) := have P.sizeof < (prop.exis x P).sizeof, from sizeof_prop_exis,
vc.not (vc.univ x (vc.not P.erased_n))
using_well_founded {
rel_tac := λ _ _, `[exact erased_measure],
dec_tac := tactic.assumption
}
-- given a call trigger t, inst_with_p(P, t) / inst_with_n(P, t) instantiates all quantifiers in
-- either positive or negative positions by adding a conjunction where the quantified
-- variable is replaced by the term in the given trigger
mutual def prop.instantiate_with_p, prop.instantiate_with_n
with prop.instantiate_with_p: prop → calltrigger → prop
| (prop.term t) _ := prop.term t
| (prop.not P) t := have P.sizeof < P.not.sizeof, from sizeof_prop_not,
prop.not (P.instantiate_with_n t)
| (prop.and P₁ P₂) t := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁,
have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂,
P₁.instantiate_with_p t ⋀ P₂.instantiate_with_p t
| (prop.or P₁ P₂) t := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁,
have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂,
P₁.instantiate_with_p t ⋁ P₂.instantiate_with_p t
| (prop.pre t₁ t₂) _ := prop.pre t₁ t₂
| (prop.pre₁ op t) _ := prop.pre₁ op t
| (prop.pre₂ op t₁ t₂) _ := prop.pre₂ op t₁ t₂
| (prop.post t₁ t₂) _ := prop.post t₁ t₂
| (prop.call t) _ := prop.call t
| (prop.forallc x P) t := prop.forallc x P ⋀ P.substt x t.x -- instantiate
| (prop.exis x P) t := prop.exis x P
with prop.instantiate_with_n: prop → calltrigger → prop
| (prop.term t) _ := prop.term t
| (prop.not P) t := have P.sizeof < P.not.sizeof, from sizeof_prop_not,
prop.not (P.instantiate_with_p t)
| (prop.and P₁ P₂) t := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁,
have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂,
P₁.instantiate_with_n t ⋀ P₂.instantiate_with_n t
| (prop.or P₁ P₂) t := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁,
have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂,
P₁.instantiate_with_n t ⋁ P₂.instantiate_with_n t
| (prop.pre t₁ t₂) _ := prop.pre t₁ t₂
| (prop.pre₁ op t) _ := prop.pre₁ op t
| (prop.pre₂ op t₁ t₂) _ := prop.pre₂ op t₁ t₂
| (prop.post t₁ t₂) _ := prop.post t₁ t₂
| (prop.call t) _ := prop.call t
| (prop.forallc x P) t := prop.forallc x P
| (prop.exis x P) t := prop.exis x P
using_well_founded {
rel_tac := λ _ _, `[exact instantiate_with_measure],
dec_tac := tactic.assumption
}
-- finds all call triggers in either positive or negative positions and returns these as list
mutual def prop.find_calls_p, prop.find_calls_n
with prop.find_calls_p: prop → list calltrigger
| (prop.term t) := []
| (prop.not P) := have P.sizeof < P.not.sizeof, from sizeof_prop_not,
P.find_calls_n
| (prop.and P₁ P₂) := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁,
have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂,
P₁.find_calls_p ++ P₂.find_calls_p
| (prop.or P₁ P₂) := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁,
have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂,
P₁.find_calls_p ++ P₂.find_calls_p
| (prop.pre t₁ t₂) := []
| (prop.pre₁ op t) := []
| (prop.pre₂ op t₁ t₂) := []
| (prop.post t₁ t₂) := []
| (prop.call t) := [ ⟨ t ⟩ ]
| (prop.forallc x P) := []
| (prop.exis x P) := []
with prop.find_calls_n: prop → list calltrigger
| (prop.term t) := []
| (prop.not P) := have P.sizeof < P.not.sizeof, from sizeof_prop_not,
P.find_calls_p
| (prop.and P₁ P₂) := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁,
have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂,
P₁.find_calls_n ++ P₂.find_calls_n
| (prop.or P₁ P₂) := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁,
have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂,
P₁.find_calls_n ++ P₂.find_calls_n
| (prop.pre t₁ t₂) := []
| (prop.pre₁ op t) := []
| (prop.pre₂ op t₁ t₂) := []
| (prop.post t₁ t₂) := []
| (prop.call t) := []
| (prop.forallc x P) := []
| (prop.exis x P) := []
using_well_founded {
rel_tac := λ _ _, `[exact find_calls_measure],
dec_tac := tactic.assumption
}
-- performs one full instantiation for each of the triggers in the provided list
def prop.instantiate_with_all: prop → list calltrigger → prop
| P list.nil := P
| P (list.cons t r) := (P.instantiate_with_n t).instantiate_with_all r
using_well_founded {
rel_tac := λ _ _, `[exact ⟨_, measure_wf $ λ s, s.2.sizeof⟩]
}
-- performs n rounds of instantiations. each round also involves a repeated lifting.
-- once all rounds are complete, remaining quantifiers and triggers in negative positions will be erased
def prop.instantiate_rep: prop → ℕ → vc
| P 0 := P.lift_all.erased_n
| P (nat.succ n) := have n < n + 1, from lt_of_add_one,
(P.lift_all.instantiate_with_all (P.lift_all.find_calls_n)).instantiate_rep n
using_well_founded {
rel_tac := λ _ _, `[exact ⟨_, measure_wf $ λ s, s.2⟩]
}
-- finds the maximum quantifier nesting level of a given proposition
def prop.max_nesting_level: prop → ℕ
| (prop.term t) := 0
| (prop.not P) := have P.sizeof < P.not.sizeof, from sizeof_prop_not,
P.max_nesting_level
| (prop.and P₁ P₂) := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁,
have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂,
max P₁.max_nesting_level P₂.max_nesting_level
| (prop.or P₁ P₂) := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁,
have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂,
max P₁.max_nesting_level P₂.max_nesting_level
| (prop.pre t₁ t₂) := 0
| (prop.pre₁ op t) := 0
| (prop.pre₂ op t₁ t₂) := 0
| (prop.post t₁ t₂) := 0
| (prop.call t) := 0
| (prop.forallc x P) := have P.sizeof < (prop.forallc x P).sizeof, from sizeof_prop_forall,
nat.succ P.max_nesting_level
| (prop.exis x P) := 0
using_well_founded {
rel_tac := λ _ _, `[exact ⟨_, measure_wf $ λ s, s.sizeof⟩],
dec_tac := tactic.assumption
}
-- the main instantiation algorithm performs n rounds of instantiations
-- where n is the maximum quantifier nesting level and returns the erased proposition
def prop.instantiated_n (P: prop): vc := P.instantiate_rep P.max_nesting_level
-- #############################
-- ### OPERATIONAL SEMANTICS ###
-- #############################
-- semantics of unary operators
def unop.apply: unop → value → option value
| unop.not value.true := value.false
| unop.not value.false := value.true
| unop.isInt (value.num _) := value.true
| unop.isInt _ := value.false
| unop.isBool value.true := value.true
| unop.isBool value.false := value.true
| unop.isBool _ := value.false
| unop.isFunc (value.func _ _ _ _ _ _) := value.true
| unop.isFunc _ := value.false
| _ _ := none
-- semantics of binary operators
def binop.apply: binop → value → value → option value
| binop.plus (value.num n₁) (value.num n₂) := value.num (n₁ + n₂)
| binop.minus (value.num n₁) (value.num n₂) := value.num (n₁ - n₂)
| binop.times (value.num n₁) (value.num n₂) := value.num (n₁ * n₂)
| binop.div (value.num n₁) (value.num n₂) := value.num (n₁ / n₂)
| binop.and value.true value.true := value.true
| binop.and value.true value.false := value.false
| binop.and value.false value.true := value.false
| binop.and value.false value.false := value.false
| binop.or value.true value.true := value.true
| binop.or value.true value.false := value.true
| binop.or value.false value.true := value.true
| binop.or value.false value.false := value.false
| binop.eq v₁ v₂ := if v₁ = v₂ then value.true else value.false
| binop.lt (value.num n₁) (value.num n₂) := if n₁ < n₂ then value.true else value.false
| _ _ _ := none
-- small-step stack-based semantics
inductive step : stack → stack → Prop
notation s₁ `⟶` s₂:100 := step s₁ s₂
| ctx {s s': stack} {σ: env} {y f x: var} {e: exp}:
(s ⟶ s') →
(s · [σ, letapp y = f[x] in e] ⟶ (s' · [σ, letapp y = f[x] in e]))
| tru {σ: env} {x: var} {e: exp}:
(σ, lett x = true in e) ⟶ (σ[x↦value.true], e)
| fals {σ: env} {x: var} {e: exp}:
(σ, letf x = false in e) ⟶ (σ[x↦value.false], e)
| num {σ: env} {x: var} {e: exp} {n: ℤ}:
(σ, letn x = n in e) ⟶ (σ[x↦value.num n], e)
| closure {σ: env} {R' R S: spec} {f x: var} {e₁ e₂: exp}:
(σ, letf f[x] req R ens S {e₁} in e₂) ⟶ (σ[f↦value.func f x R S e₁ σ], e₂)
| unop {op: unop} {σ: env} {x y: var} {e: exp} {v₁ v: value}:
(σ x = v₁) →
(unop.apply op v₁ = v) →
((σ, letop y = op [x] in e) ⟶ (σ[y↦v], e))
| binop {op: binop} {σ: env} {x y z: var} {e: exp} {v₁ v₂ v: value}:
(σ x = v₁) →
(σ y = v₂) →
(binop.apply op v₁ v₂ = v) →
((σ, letop2 z = op [x, y] in e) ⟶ (σ[z↦v], e))
| app {σ σ': env} {R S: spec} {f g x y z: var} {e e': exp} {v: value}:
(σ f = value.func g z R S e σ') →
(σ x = v) →
((σ, letapp y = f[x] in e') ⟶ ((σ'[g↦value.func g z R S e σ'][z↦v], e) · [σ, letapp y = f[x] in e']))
| return {σ₁ σ₂ σ₃: env} {f g gx x y z: var} {R S: spec} {e e': exp} {v vₓ: value}:
(σ₁ z = v) →
(σ₂ f = value.func g gx R S e σ₃) →
(σ₂ x = vₓ) →
((σ₁, exp.return z) · [σ₂, letapp y = f[x] in e'] ⟶ (σ₂[y↦v], e'))
| ite_true {σ: env} {e₁ e₂: exp} {x: var}:
(σ x = value.true) →
((σ, exp.ite x e₁ e₂) ⟶ (σ, e₁))
| ite_false {σ: env} {e₁ e₂: exp} {x: var}:
(σ x = value.false) →
((σ, exp.ite x e₁ e₂) ⟶ (σ, e₂))
notation s₁ `⟶` s₂:100 := step s₁ s₂
-- transitive closure
inductive trans_step : stack → stack → Prop
notation s `⟶*` s':100 := trans_step s s'
| rfl {s: stack} : s ⟶* s
| trans {s s' s'': stack} : (s ⟶* s') → (s' ⟶ s'') → (s ⟶* s'')
notation s `⟶*` s':100 := trans_step s s'
def is_value (s: stack) :=
∃(σ: env) (x: var) (v: value), s = (σ, exp.return x) ∧ (σ x = v)
-- #######################################
-- ### VALIDTY OF LOGICAL PROPOSITIONS ###
-- #######################################
-- validity is axiomatized instead defined
-- see axioms below
constant valid : vc → Prop
notation `⊨` p: 20 := valid p
notation σ `⊨` p: 20 := ⊨ (vc.subst_env σ p)
notation `⟪` P `⟫`: 100 := ∀ (σ: env), closed_subst σ P → ⊨ (prop.subst_env σ P).instantiated_n
-- simple axioms for logical reasoning
axiom valid.tru:
⊨ value.true
axiom valid.and {P Q: vc}:
(⊨ P) ∧ (⊨ Q)
↔
⊨ P ⋀ Q
axiom valid.or.left {P Q: vc}:
(⊨ P) →
⊨ P ⋁ Q
axiom valid.or.right {P Q: vc}:
(⊨ Q) →
⊨ P ⋁ Q
axiom valid.or.elim {P Q: vc}:
(⊨ P ⋁ Q)
→
(⊨ P) ∨ (⊨ Q)
-- no contradictions
axiom valid.contradiction {P: vc}:
¬ (⊨ P ⋀ P.not)
-- law of excluded middle
axiom valid.em {P: vc}:
(⊨ P ⋁ P.not)
-- a term is valid if it equals true
axiom valid.eq.true {t: term}:
⊨ t
↔
⊨ value.true ≡ t
-- universal quantifier valid if true for all values
axiom valid.univ.mp {x: var} {P: vc}:
(∀v, ⊨ vc.subst x v P)
→
⊨ vc.univ x P
-- a free top-level variable is implicitly universally quantified
axiom valid.univ.free {x: var} {P: vc}:
(x ∈ FV P ∧ ⊨ P)
→
⊨ vc.univ x P
-- universal quantifier can be instantiated with any term
axiom valid.univ.mpr {x: var} {P: vc}:
(⊨ vc.univ x P)
→
(∀t, ⊨ vc.substt x t P)
-- unary and binary operators are decidable, so equalities with operators are decidable
axiom valid.unop {op: unop} {vₓ v: value}:
unop.apply op vₓ = some v
↔
⊨ v ≡ term.unop op vₓ
axiom valid.binop {op: binop} {v₁ v₂ v: value}:
binop.apply op v₁ v₂ = some v
↔
⊨ v ≡ term.binop op v₁ v₂
-- can write pre₁ and pre₂ to check domain of operators
axiom valid.pre₁ {vₓ: value} {op: unop}:
(⊨ vc.pre₁ op vₓ)
→
option.is_some (unop.apply op vₓ)
axiom valid.pre₂ {v₁ v₂: value} {op: binop}:
(⊨ vc.pre₂ op v₁ v₂)
→
option.is_some (binop.apply op v₁ v₂)
-- #####################################
-- ### VERIFICATION RELATION (VCGEN) ###
-- #####################################
reserve infix `⊢`:10
-- verification of expressions
inductive exp.vcgen : prop → exp → propctx → Prop
notation P `⊢` e `:` Q : 10 := exp.vcgen P e Q
| tru {P: prop} {x: var} {e: exp} {Q: propctx}:
x ∉ FV P →
(P ⋀ x ≡ value.true ⊢ e : Q) →
(P ⊢ lett x = true in e : propctx.exis x (x ≡ value.true ⋀ Q))
| fals {P: prop} {x: var} {e: exp} {Q: propctx}:
x ∉ FV P →
(P ⋀ x ≡ value.false ⊢ e : Q) →
(P ⊢ letf x = false in e : propctx.exis x (x ≡ value.false ⋀ Q))
| num {P: prop} {x: var} {n: ℕ} {e: exp} {Q: propctx}:
x ∉ FV P →
(P ⋀ x ≡ value.num n ⊢ e : Q) →
(P ⊢ letn x = n in e : propctx.exis x (x ≡ value.num n ⋀ Q))
| func {P: prop} {f x: var} {R S: spec} {e₁ e₂: exp} {Q₁ Q₂: propctx}:
f ∉ FV P →
x ∉ FV P →
f ≠ x →
x ∈ FV R.to_prop.to_vc →
FV R.to_prop ⊆ FV P ∪ { f, x } →
FV S.to_prop ⊆ FV P ∪ { f, x } →
(P ⋀ spec.func f x R S ⋀ R ⊢ e₁ : Q₁) →
(P ⋀ prop.func f x R (Q₁ (term.app f x) ⋀ S) ⊢ e₂ : Q₂) →
⟪ prop.implies (P ⋀ spec.func f x R S ⋀ R ⋀ Q₁ (term.app f x)) S ⟫ →
(P ⊢ letf f[x] req R ens S {e₁} in e₂ : propctx.exis f (prop.func f x R (Q₁ (term.app f x) ⋀ S) ⋀ Q₂))
| unop {P: prop} {op: unop} {e: exp} {x y: var} {Q: propctx}:
x ∈ FV P →
y ∉ FV P →
(P ⋀ y ≡ term.unop op x ⊢ e : Q) →
⟪ prop.implies P (prop.pre₁ op x) ⟫ →
(P ⊢ letop y = op [x] in e : propctx.exis y (y ≡ term.unop op x ⋀ Q))
| binop {P: prop} {op: binop} {e: exp} {x y z: var} {Q: propctx}:
x ∈ FV P →
y ∈ FV P →
z ∉ FV P →
(P ⋀ z ≡ term.binop op x y ⊢ e : Q) →
⟪ prop.implies P (prop.pre₂ op x y) ⟫ →
(P ⊢ letop2 z = op [x, y] in e : propctx.exis z (z ≡ term.binop op x y ⋀ Q))
| app {P: prop} {e: exp} {y f x: var} {Q: propctx}:
f ∈ FV P →
x ∈ FV P →
y ∉ FV P →
(P ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x ⊢ e : Q) →
⟪ prop.implies (P ⋀ prop.call x) (term.unop unop.isFunc f ⋀ prop.pre f x) ⟫ →
(P ⊢ letapp y = f [x] in e : propctx.exis y (prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x ⋀ Q))
| ite {P: prop} {e₁ e₂: exp} {x: var} {Q₁ Q₂: propctx}:
x ∈ FV P →
(P ⋀ x ⊢ e₁ : Q₁) →
(P ⋀ prop.not x ⊢ e₂ : Q₂) →
⟪ prop.implies P (term.unop unop.isBool x) ⟫ →
(P ⊢ exp.ite x e₁ e₂ : propctx.implies x Q₁ ⋀ propctx.implies (prop.not x) Q₂)
| return {P: prop} {x: var}:
x ∈ FV P →
(P ⊢ exp.return x : x ≣ •)
notation P `⊢` e `:` Q : 10 := exp.vcgen P e Q
-- verification of environments/translation into logic
inductive env.vcgen : env → prop → Prop
notation `⊢` σ `:` Q : 10 := env.vcgen σ Q
| empty:
⊢ env.empty : value.true
| tru {σ: env} {x: var} {Q: prop}:
x ∉ σ →
(⊢ σ : Q) →
(⊢ (σ[x ↦ value.true]) : Q ⋀ x ≡ value.true)
| fls {σ: env} {x: var} {Q: prop}:
x ∉ σ →
(⊢ σ : Q) →
(⊢ (σ[x ↦ value.false]) : Q ⋀ x ≡ value.false)
| num {n: ℤ} {σ: env} {x: var} {Q: prop}:
x ∉ σ →
(⊢ σ : Q) →
(⊢ (σ[x ↦ value.num n]) : Q ⋀ x ≡ value.num n)
| func {σ₁ σ₂: env} {f g x: var} {R S: spec} {e: exp} {Q₁ Q₂: prop} {Q₃: propctx}:
f ∉ σ₁ →
g ∉ σ₂ →
x ∉ σ₂ →
g ≠ x →
(⊢ σ₁ : Q₁) →
(⊢ σ₂ : Q₂) →
x ∈ FV R.to_prop.to_vc →
FV R.to_prop ⊆ FV Q₂ ∪ { g, x } →
FV S.to_prop ⊆ FV Q₂ ∪ { g, x } →
(Q₂ ⋀ spec.func g x R S ⋀ R ⊢ e : Q₃) →
⟪ prop.implies (Q₂ ⋀ spec.func g x R S ⋀ R ⋀ Q₃ (term.app g x)) S ⟫ →
(⊢ (σ₁[f ↦ value.func g x R S e σ₂]) :
(Q₁
⋀ f ≡ value.func g x R S e σ₂
⋀ prop.subst_env (σ₂[g↦value.func g x R S e σ₂]) (prop.func g x R (Q₃ (term.app g x) ⋀ S))))
notation `⊢` σ `:` Q : 10 := env.vcgen σ Q
-- ###############################
-- ### VERIFICATION WITHOUT QI ###
-- ###############################
-- verification conditions without quantifier instantiation algorithm
notation `⦃` P `⦄`: 100 := ∀ (σ: env), closed_subst σ P → σ ⊨ P.to_vc
reserve infix `⊩`:10
-- verification of expressions
inductive exp.dvcgen : prop → exp → propctx → Prop
notation P `⊩` e `:` Q : 10 := exp.dvcgen P e Q
| tru {P: prop} {x: var} {e: exp} {Q: propctx}:
x ∉ FV P →
(P ⋀ x ≡ value.true ⊩ e : Q) →
(P ⊩ lett x = true in e : propctx.exis x (x ≡ value.true ⋀ Q))
| fals {P: prop} {x: var} {e: exp} {Q: propctx}:
x ∉ FV P →
(P ⋀ x ≡ value.false ⊩ e : Q) →
(P ⊩ letf x = false in e : propctx.exis x (x ≡ value.false ⋀ Q))
| num {P: prop} {x: var} {n: ℕ} {e: exp} {Q: propctx}:
x ∉ FV P →
(P ⋀ x ≡ value.num n ⊩ e : Q) →
(P ⊩ letn x = n in e : propctx.exis x (x ≡ value.num n ⋀ Q))
| func {P: prop} {f x: var} {R S: spec} {e₁ e₂: exp} {Q₁ Q₂: propctx}:
f ∉ FV P →
x ∉ FV P →
f ≠ x →
x ∈ FV R.to_prop.to_vc →
FV R.to_prop ⊆ FV P ∪ { f, x } →
FV S.to_prop ⊆ FV P ∪ { f, x } →
(P ⋀ spec.func f x R S ⋀ R ⊩ e₁ : Q₁) →
(P ⋀ prop.func f x R (Q₁ (term.app f x) ⋀ S) ⊩ e₂ : Q₂) →
⦃ prop.implies (P ⋀ spec.func f x R S ⋀ R ⋀ Q₁ (term.app f x)) S ⦄ →
(P ⊩ letf f[x] req R ens S {e₁} in e₂ : propctx.exis f (prop.func f x R (Q₁ (term.app f x) ⋀ S) ⋀ Q₂))
| unop {P: prop} {op: unop} {e: exp} {x y: var} {Q: propctx}:
x ∈ FV P →
y ∉ FV P →
(P ⋀ y ≡ term.unop op x ⊩ e : Q) →
⦃ prop.implies P (prop.pre₁ op x) ⦄ →
(P ⊩ letop y = op [x] in e : propctx.exis y (y ≡ term.unop op x ⋀ Q))
| binop {P: prop} {op: binop} {e: exp} {x y z: var} {Q: propctx}:
x ∈ FV P →
y ∈ FV P →
z ∉ FV P →
(P ⋀ z ≡ term.binop op x y ⊩ e : Q) →
⦃ prop.implies P (prop.pre₂ op x y) ⦄ →
(P ⊩ letop2 z = op [x, y] in e : propctx.exis z (z ≡ term.binop op x y ⋀ Q))
| app {P: prop} {e: exp} {y f x: var} {Q: propctx}:
f ∈ FV P →
x ∈ FV P →
y ∉ FV P →
(P ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x ⊩ e : Q) →
⦃ prop.implies (P ⋀ prop.call x) (term.unop unop.isFunc f ⋀ prop.pre f x) ⦄ →
(P ⊩ letapp y = f [x] in e : propctx.exis y (prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x ⋀ Q))
| ite {P: prop} {e₁ e₂: exp} {x: var} {Q₁ Q₂: propctx}:
x ∈ FV P →
(P ⋀ x ⊩ e₁ : Q₁) →
(P ⋀ prop.not x ⊩ e₂ : Q₂) →
⦃ prop.implies P (term.unop unop.isBool x) ⦄ →
(P ⊩ exp.ite x e₁ e₂ : propctx.implies x Q₁ ⋀ propctx.implies (prop.not x) Q₂)
| return {P: prop} {x: var}:
x ∈ FV P →
(P ⊩ exp.return x : x ≣ •)
notation P `⊩` e `:` Q : 10 := exp.dvcgen P e Q
-- verification of environments/translation into logic
inductive env.dvcgen : env → prop → Prop
notation `⊩` σ `:` Q : 10 := env.dvcgen σ Q
| empty:
⊩ env.empty : value.true
| tru {σ: env} {x: var} {Q: prop}:
x ∉ σ →
(⊩ σ : Q) →
(⊩ (σ[x ↦ value.true]) : Q ⋀ x ≡ value.true)
| fls {σ: env} {x: var} {Q: prop}:
x ∉ σ →
(⊩ σ : Q) →
(⊩ (σ[x ↦ value.false]) : Q ⋀ x ≡ value.false)
| num {n: ℤ} {σ: env} {x: var} {Q: prop}:
x ∉ σ →
(⊩ σ : Q) →
(⊩ (σ[x ↦ value.num n]) : Q ⋀ x ≡ value.num n)
| func {σ₁ σ₂: env} {f g x: var} {R S: spec} {e: exp} {Q₁ Q₂: prop} {Q₃: propctx}:
f ∉ σ₁ →
g ∉ σ₂ →
x ∉ σ₂ →
g ≠ x →
(⊩ σ₁ : Q₁) →
(⊩ σ₂ : Q₂) →
x ∈ FV R.to_prop.to_vc →
FV R.to_prop ⊆ FV Q₂ ∪ { g, x } →
FV S.to_prop ⊆ FV Q₂ ∪ { g, x } →
(Q₂ ⋀ spec.func g x R S ⋀ R ⊩ e : Q₃) →
⦃ prop.implies (Q₂ ⋀ spec.func g x R S ⋀ R ⋀ Q₃ (term.app g x)) S ⦄ →
(⊩ (σ₁[f ↦ value.func g x R S e σ₂]) :
(Q₁
⋀ f ≡ value.func g x R S e σ₂
⋀ prop.subst_env (σ₂[g↦value.func g x R S e σ₂]) (prop.func g x R (Q₃ (term.app g x) ⋀ S))))
notation `⊩` σ `:` Q : 10 := env.dvcgen σ Q
-- #################################################################
-- ### AXIOMS ABOUT FUNCTION EXPRESSIONS, PRE and POSTCONDITIONS ###
-- #################################################################
-- The following equality axiom is non-standard and makes validity undecidable.
-- It is only used in the preservation proof of e-return and in no other lemmas.
-- The logic treats `f(x)` uninterpreted, so there is no way to derive it naturally.
-- However, given a complete evaluation derivation of a function call, we can add the
-- equality `f(x)=y` as new axiom for closed values f, x, y and the resulting set
-- of axioms is still sound due to deterministic evaluation.
axiom valid.app {vₓ v: value} {σ σ': env} {f x y: var} {R S: spec} {e: exp}:
(σ[f↦value.func f x R S e σ][x↦vₓ], e) ⟶* (σ', exp.return y) →
(σ' y = some v)
→
⊨ v ≡ term.app (value.func f x R S e σ) vₓ
-- can write pre and post to extract pre- and postcondition of function values
axiom valid.pre {vₓ: value} {σ: env} {f x: var} {R S: spec} {e: exp}:
(σ[f↦value.func f x R S e σ][x↦vₓ] ⊨ R.to_prop.to_vc)
↔
⊨ vc.pre (value.func f x R S e σ) vₓ
axiom valid.post.mp {vₓ: value} {σ: env} {Q: prop} {Q₂: propctx} {f x: var} {R S: spec} {e: exp}:
(⊩ σ : Q) →
(Q ⋀ spec.func f x R S ⋀ R ⊩ e : Q₂) →
(σ[f↦value.func f x R S e σ][x↦vₓ] ⊨ (Q₂ (term.app f x) ⋀ S.to_prop).to_vc)
→
(⊨ vc.post (value.func f x R S e σ) vₓ)
axiom valid.post.mpr {vₓ: value} {σ: env} {Q: prop} {Q₂: propctx} {f x: var} {R S: spec} {e: exp}:
(⊩ σ : Q) →
(Q ⋀ spec.func f x R S ⋀ R ⊩ e : Q₂) →
(⊨ vc.post (value.func f x R S e σ) vₓ)
→
(σ[f↦value.func f x R S e σ][x↦vₓ] ⊨ (Q₂ (term.app f x) ⋀ S.to_prop).to_vc)