-
Notifications
You must be signed in to change notification settings - Fork 0
/
hl-upd.mlw
1362 lines (954 loc) · 38.5 KB
/
hl-upd.mlw
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Propositional Hoare logic with updates *)
theory ImpLanguage
use export map.Map
use export int.Int
(* identifiers *)
type ident =
| MkIdent int
(* expressions *)
type operator = Oplus | Ominus | Omult
type expr =
| Econst int
| Evar ident
| Ebin expr operator expr
(* Boolean expressions *)
type boperator = BOeq | BOlt | BOlteq | BOgt | BOgteq
type bexpr =
| Bcomp expr boperator expr
| Btrue
| Bfalse
| Band bexpr bexpr
| Bor bexpr bexpr
| Bnot bexpr
(* program states *)
type state = map ident int
(* evaluation of expressions *)
function eval_bin (x:int) (op:operator) (y:int) : int =
match op with
| Oplus -> x+y
| Ominus -> x-y
| Omult -> x*y
end
function eval_expr (s:state) (e:expr) : int =
match e with
| Econst n -> n
| Evar x -> get s x
| Ebin e1 op e2 ->
eval_bin (eval_expr s e1) op (eval_expr s e2)
end
predicate eval_bop (x:int) (bop:boperator) (y:int) =
match bop with
| BOeq -> x = y
| BOlt -> x < y
| BOlteq -> x <= y
| BOgt -> x > y
| BOgteq -> x >= y
end
(* Boolean expressions *)
(* could be intepreted as Why3 bool instead *)
predicate eval_bexpr (s:state) (b:bexpr) =
match b with
| Bcomp e1 bop e2 -> eval_bop (eval_expr s e1) bop (eval_expr s e2)
| Btrue -> true
| Bfalse -> false
| Band b1 b2 -> (eval_bexpr s b1) /\ (eval_bexpr s b2)
| Bor b1 b2 -> (eval_bexpr s b1) \/ (eval_bexpr s b2)
| Bnot b1 -> not (eval_bexpr s b1)
end
(* formulas - extension of bexpr *)
(* no quantifiers for now *)
type fmla =
| Fcomp expr boperator expr
| Fembed bexpr
| Ftrue
| Ffalse
| Fand fmla fmla
| For fmla fmla
| Fnot fmla
| Fimplies fmla fmla
(* semantics of formulas *)
predicate satisfies (s:state) (p:fmla) =
match p with
| Fcomp e1 bop e2 -> eval_bop (eval_expr s e1) bop (eval_expr s e2)
| Fembed b -> (eval_bexpr s b)
| Ftrue -> true
| Ffalse -> false
| Fand p1 p2 -> (satisfies s p1) /\ (satisfies s p2)
| For p1 p2 -> (satisfies s p1) \/ (satisfies s p2)
| Fnot p1 -> not (satisfies s p1)
| Fimplies p1 p2 -> (not (satisfies s p1)) \/ (satisfies s p2)
end
predicate valid_fmla (p:fmla) = forall s:state. satisfies s p
(* required by one of the VCs for completeness *)
lemma deduction:
forall p q :fmla.
(forall s: state. satisfies s p -> satisfies s q)
->
valid_fmla (Fimplies p q)
(* programs include invariants as annotations *)
type stmt =
| Sskip
| Sassign ident expr
| Sif bexpr stmt stmt
| Swhile bexpr fmla stmt
| Sseq stmt stmt
(* This notion of size of programs will be useful *)
(* to construct variants because of the seqseq rule *)
function size (c:stmt) : int =
match c with
| Sskip -> 1
| Sassign _ _ -> 1
| Sif _ c1 c2 -> 1 + size c1 + size c2
| Sseq c1 c2 -> 1 + 2*size c1 + size c2
| Swhile _ _ c -> 1 + size c
end
(* 3 ways to prove size c >= 0 for all c *)
(* induction_ty_lex *)
(* lemma size_pos : forall c[@induction] :stmt. size c >= 0 *)
let rec lemma size_posLF (c:stmt) =
ensures { size c >= 0 }
match c with
| Sskip -> ()
| Sassign _ _ -> ()
| Sif _ c1 c2 -> size_posLF c1 ; size_posLF c2
| Sseq c1 c2 -> size_posLF c1 ; size_posLF c2
| Swhile _ _ c -> size_posLF c
end
(* An alternative would be to define size as a program function *)
(* [could be ghost] *)
let rec function sizeC (c:stmt) : int =
ensures { result >= 0 }
match c with
| Sskip -> 1
| Sassign _ _ -> 1
| Sif _ c1 c2 -> 1 + sizeC c1 + sizeC c2
| Sseq c1 c2 -> 1 + 2*sizeC c1 + sizeC c2
| Swhile _ _ c -> 1 + sizeC c
end
(* Natural Semantics *)
inductive big_step state stmt state =
| big_step_skip:
forall s:state. big_step s Sskip s
| big_step_assign:
forall s:state, e:expr, x:ident.
big_step s (Sassign x e) (set s x (eval_expr s e))
| big_step_seq:
forall s1 s2 s3:state, c1 c2:stmt.
big_step s1 c1 s2 ->
big_step s2 c2 s3 ->
big_step s1 (Sseq c1 c2) s3
| big_step_if_true:
forall s s':state, b:bexpr, c1 c2:stmt.
eval_bexpr s b ->
big_step s c1 s'->
big_step s (Sif b c1 c2) s'
| big_step_if_false:
forall s s':state, b:bexpr, c1 c2:stmt.
not (eval_bexpr s b) ->
big_step s c2 s' ->
big_step s (Sif b c1 c2) s'
| big_step_while_true:
forall s s' s'':state, b:bexpr, i:fmla, c:stmt.
eval_bexpr s b ->
big_step s c s' ->
big_step s' (Swhile b i c) s'' ->
big_step s (Swhile b i c) s''
| big_step_while_false:
forall s:state, b:bexpr, i:fmla, c:stmt.
not (eval_bexpr s b) ->
big_step s (Swhile b i c) s
(* not required but a nice example of using two forms of induction *)
(* use induction_ty_lex then induction_pr for the loop case *)
lemma deterministic_execution : forall c [@induction]:stmt, s s' s'' :state.
(big_step s c s') -> (big_step s c s'') -> s' = s''
(* Required for soundness *)
lemma AssignSeq:
forall x :ident, e :expr, c :stmt, s s' :state.
big_step s (Sseq (Sassign x e) c) s' ->
big_step (set s x (eval_expr s e)) c s'
lemma IfSeqTrue:
forall b :bexpr, c1 c2 c :stmt, s s' :state.
big_step s (Sseq (Sif b c1 c2) c) s' ->
eval_bexpr s b -> big_step s (Sseq c1 c) s'
lemma IfSeqFalse:
forall b :bexpr, c1 c2 c :stmt, s s' :state.
big_step s (Sseq (Sif b c1 c2) c) s' ->
eval_bexpr s (Bnot b) -> big_step s (Sseq c2 c) s'
(* Required for soundness and completeness *)
lemma SeqSeq:
forall c1 c2 c:stmt, s s' :state.
big_step s (Sseq c1 (Sseq c2 c)) s'
<->
big_step s (Sseq (Sseq c1 c2) c) s'
end
theory Updates
use ImpLanguage
(* updates are (total) mappings *)
type upd = map ident expr
(* The empty update will be represented as the identity mapping *)
let constant idUpd : upd =
fun (x:ident) -> Evar x
predicate isId (u:upd) =
forall a: ident. get u a = Evar a
(* update application to states *)
function applyS (u:upd) (s:state) : state =
fun (x:ident) -> eval_expr s (u x)
lemma applySId:
forall u :upd, s :state.
isId u -> applyS u s = s
(* update application to expressions *)
let rec function applyE (u:upd) (e:expr) : expr =
ensures { forall s :state. eval_expr s result = eval_expr (applyS u s) e }
variant { e }
match e with
| Econst n -> Econst n
| Evar x -> get u x
| Ebin e1 op e2 -> Ebin (applyE u e1) op (applyE u e2)
end
(* could be logic function + lemma *)
(* lemma exprUpdate: forall s:state, e[@induction]:expr, u: upd. *)
(* eval_expr s (applyE u e) = eval_expr (applyS u s) e *)
(* let rec lemma exprUpdateLF (s:state) (e: expr) (u: upd) : unit *)
(* ensures { eval_expr s (applyE u e) = eval_expr (applyS u s) e } *)
(* variant { e } *)
(* = match e with *)
(* | Econst _ -> () *)
(* | Evar _ -> () *)
(* | Ebin e1 _ e2 -> exprUpdateLF s e1 u; exprUpdateLF s e2 u *)
(* end *)
(* lemma applyEId: *)
(* forall u :upd, e[@induction] :expr. *)
(* isId u -> applyE u e = e *)
let rec lemma applyEIdLF (u:upd) (e:expr)
requires { isId u }
ensures { applyE u e = e }
= match e with
| Econst _ -> ()
| Evar _ -> ()
| Ebin e1 _ e2 -> applyEIdLF u e1 ; applyEIdLF u e2
end
(* update application to Boolean expressions *)
let rec ghost function applyB (u:upd) (b:bexpr) : bexpr =
ensures { forall s :state. eval_bexpr s result <-> eval_bexpr (applyS u s) b }
variant { b }
match b with
| Bcomp e1 bop e2 -> Bcomp (applyE u e1) bop (applyE u e2)
| Btrue -> Btrue
| Bfalse -> Bfalse
| Band b1 b2 -> Band (applyB u b1) (applyB u b2)
| Bor b1 b2 -> Bor (applyB u b1) (applyB u b2)
| Bnot b1 -> Bnot (applyB u b1)
end
(* lemma applyBId: *)
(* forall u :upd, b[@induction] :bexpr. *)
(* isId u -> applyB u b = b *)
let rec lemma applyBIdLF (u:upd) (b:bexpr)
requires { isId u }
ensures { applyB u b = b }
= match b with
| Bcomp _ _ _ -> ()
| Btrue -> ()
| Bfalse -> ()
| Band b1 b2 -> applyBIdLF u b1 ; applyBIdLF u b2
| Bor b1 b2 -> applyBIdLF u b1 ; applyBIdLF u b2
| Bnot b1 -> applyBIdLF u b1
end
(* update application to formulas *)
let rec ghost function applyF (u:upd) (p:fmla) : fmla =
ensures { forall s :state. satisfies s result <-> satisfies (applyS u s) p }
variant { p }
match p with
| Fcomp e1 bop e2 -> Fcomp (applyE u e1) bop (applyE u e2)
| Fembed b -> Fembed (applyB u b)
| Ftrue -> Ftrue
| Ffalse -> Ffalse
| Fand p1 p2 -> Fand (applyF u p1) (applyF u p2)
| For p1 p2 -> For (applyF u p1) (applyF u p2)
| Fnot p1 -> Fnot (applyF u p1)
| Fimplies p1 p2 -> Fimplies (applyF u p1) (applyF u p2)
end
(* lemma applyFId: *)
(* forall u :upd, p[@induction] :fmla. *)
(* isId u -> applyF u p = p *)
let rec lemma applyFIdLF (u:upd) (p:fmla)
requires { isId u }
ensures { applyF u p = p }
= match p with
| Fcomp _ _ _ -> ()
| Fembed _ -> ()
| Ftrue -> ()
| Ffalse -> ()
| Fand p1 p2 -> applyFIdLF u p1 ; applyFIdLF u p2
| For p1 p2 -> applyFIdLF u p1 ; applyFIdLF u p2
| Fnot p1 -> applyFIdLF u p1
| Fimplies p1 p2 -> applyFIdLF u p1 ; applyFIdLF u p2
end
(* "update update" with an assignment *)
(* If defined as program function it would necessarily *)
(* be ghost since the map set function is ghost *)
function assign (u:upd) (x:ident) (e:expr) : upd =
set u x (applyE u e)
(* interaction between assignment and application of update to state *)
lemma applySAssign :
forall x: ident, e: expr, u: upd, s: state.
applyS (assign u x e) s = let s' = applyS u s
in set s' x (eval_expr s' e)
(* Some tests... *)
goal AssignTest1 :
let x = MkIdent 0 in
let u = (fun _ -> Econst 2) in (* constant update in two differente senses... *)
let u = assign u x (Ebin (Evar x) Oplus (Econst 13)) in
get u x = (Ebin (Econst 2) Oplus (Econst 13))
goal AssignTest2 :
let x = MkIdent 0 in
let u = idUpd in (* identity update *)
let u = assign u x (Ebin (Evar x) Oplus (Econst 10)) in
let u = assign u x (Ebin (Evar x) Oplus (Econst 20)) in
get u x = Ebin (Ebin (Evar x) Oplus (Econst 10)) Oplus (Econst 20)
goal AssignTestSwap :
let x = MkIdent 0 in
let y = MkIdent 1 in
let t = MkIdent 2 in
let u = idUpd in
let u = assign u t (Evar x) in
let u = assign u x (Evar y) in
let u = assign u y (Evar t) in
(get u y = Evar x) /\ (get u x = Evar y)
goal AssignTestSwap2 :
forall x y t :ident, u u1 u2 u3 :upd.
x <> t -> y <> t ->
isId u ->
u1 = assign u t (Evar x) ->
u2 = assign u1 x (Evar y) ->
u3 = assign u2 y (Evar t) ->
(get u3 y = Evar x) /\ (get u3 x = Evar y)
end
theory Semantics
use export ImpLanguage
use export Updates
(* Hoare triples *)
predicate valid_triple (p:fmla) (u:upd) (c:stmt) (q:fmla) =
forall s s' :state.
satisfies s p ->
big_step (applyS u s) c s' ->
satisfies s' q
(* Example triples tested for validity *)
goal TestValidTripleSimpleProg1 :
let x = MkIdent 0 in
let y = MkIdent 1 in
let prog = Sseq (Sassign y (Ebin (Evar y) Oplus (Econst 10))) Sskip in
let p = Fcomp (Evar y) BOeq (Evar x) in
let q = Fcomp (Evar y) BOeq (Ebin (Evar x) Oplus (Econst 10)) in
valid_triple p idUpd prog q
goal TestValidTripleSimpleProg2 :
let x = MkIdent 0 in
let y = MkIdent 1 in
let prog = Sseq (Sassign y (Ebin (Evar y) Oplus (Econst 10))) Sskip in
let p = Fcomp (Evar y) BOeq (Evar x) in
let q = Fcomp (Evar y) BOeq (Ebin (Evar x) Oplus (Econst 10)) in
valid_triple p idUpd prog q
goal TestValidTripleSwapProg :
let x = MkIdent 0 in
let y = MkIdent 1 in
let t = MkIdent 2 in
let a = MkIdent 3 in
let b = MkIdent 4 in
let swap = Sseq (Sassign t (Evar x))
(Sseq (Sassign x (Evar y))
(Sassign y (Evar t))) in
let p = Fand (Fcomp (Evar x) BOeq (Evar a)) (Fcomp (Evar y) BOeq (Evar b)) in
let q = Fand (Fcomp (Evar y) BOeq (Evar a)) (Fcomp (Evar x) BOeq (Evar b)) in
valid_triple p idUpd swap q
goal TestValidTripleAltSwapProg :
let x = MkIdent 0 in
let y = MkIdent 1 in
let t = MkIdent 2 in
let a = MkIdent 3 in
let b = MkIdent 4 in
let swap = Sseq (Sseq (Sassign t (Evar x))
(Sassign x (Evar y)))
(Sassign y (Evar t)) in
let p = Fand (Fcomp (Evar x) BOeq (Evar a)) (Fcomp (Evar y) BOeq (Evar b)) in
let q = Fand (Fcomp (Evar y) BOeq (Evar a)) (Fcomp (Evar x) BOeq (Evar b)) in
valid_triple p idUpd swap q
end
theory SystemHu
use Semantics
(* Inference system of Hoare Logic with Updates *)
(* DOES NOT MAKE USE OF ANNOTATED LOOP INVARIANTS *)
inductive infHu fmla upd stmt fmla =
| infHu_skip:
forall p q :fmla, u :upd.
valid_fmla (Fimplies p (applyF u q)) ->
infHu p u Sskip q
| infHu_assign:
forall p :fmla, q :fmla, x :ident, e :expr, u :upd.
valid_fmla (Fimplies p (applyF (assign u x e) q)) ->
infHu p u (Sassign x e) q
| infHu_if:
forall p q :fmla, c1 c2 :stmt, b :bexpr, u :upd.
infHu (Fand p (applyF u (Fembed b))) u c1 q ->
infHu (Fand p (applyF u (Fnot (Fembed b)))) u c2 q ->
infHu p u (Sif b c1 c2) q
| infHu_while:
forall p q :fmla, c :stmt, b :bexpr, inv ainv :fmla, u :upd.
valid_fmla (Fimplies p (applyF u inv)) ->
infHu (Fand inv (Fembed b)) idUpd c inv ->
valid_fmla (Fimplies (Fand inv (Fnot (Fembed b))) q) ->
infHu p u (Swhile b ainv c) q
| infHu_skipseq:
forall p q :fmla, u :upd, c :stmt.
infHu p u c q ->
infHu p u (Sseq Sskip c) q
| infHu_assignseq:
forall p :fmla, q :fmla, x :ident, e :expr, c :stmt, u :upd.
infHu p (assign u x e) c q ->
infHu p u (Sseq (Sassign x e) c) q
| infHu_ifseq:
forall p q :fmla, c1 c2 c :stmt, b :bexpr, u :upd.
infHu (Fand p (applyF u (Fembed b))) u (Sseq c1 c) q ->
infHu (Fand p (applyF u (Fnot (Fembed b)))) u (Sseq c2 c) q ->
infHu p u (Sseq (Sif b c1 c2) c) q
| infHu_whileseq:
forall p q :fmla, c cc :stmt, b :bexpr, inv ainv :fmla, u :upd.
valid_fmla (Fimplies p (applyF u inv)) ->
infHu (Fand inv (Fembed b)) idUpd c inv ->
infHu (Fand inv (Fnot (Fembed b))) idUpd cc q ->
infHu p u (Sseq (Swhile b ainv c) cc) q
| infHu_seqseq:
forall p q :fmla, c1 c2 c :stmt, u :upd.
infHu p u (Sseq c1 (Sseq c2 c)) q ->
infHu p u (Sseq (Sseq c1 c2) c) q
(* Example inferences *)
goal TestInfSimpleProg1 :
let x = MkIdent 0 in
let y = MkIdent 1 in
let prog = Sseq (Sassign y (Ebin (Evar y) Oplus (Econst 10))) Sskip in
let p = Fcomp (Evar y) BOeq (Evar x) in
let q = Fcomp (Evar y) BOeq (Ebin (Evar x) Oplus (Econst 10)) in
infHu p idUpd prog q
goal TestInfSimpleProg2 :
let x = MkIdent 0 in
let y = MkIdent 1 in
let prog = Sseq (Sassign y (Ebin (Evar y) Oplus (Econst 10))) Sskip in
let p = Fcomp (Evar y) BOeq (Evar x) in
let q = Fcomp (Evar y) BOeq (Ebin (Evar x) Oplus (Econst 10)) in
infHu p idUpd prog q
goal TestInfSwapProg :
let x = MkIdent 0 in
let y = MkIdent 1 in
let t = MkIdent 2 in
let a = MkIdent 3 in
let b = MkIdent 4 in
let swap = Sseq (Sassign t (Evar x))
(Sseq (Sassign x (Evar y))
(Sseq (Sassign y (Evar t))
Sskip)) in
let p = Fand (Fcomp (Evar x) BOeq (Evar a)) (Fcomp (Evar y) BOeq (Evar b)) in
let q = Fand (Fcomp (Evar y) BOeq (Evar a)) (Fcomp (Evar x) BOeq (Evar b)) in
infHu p idUpd swap q
goal TestInfAltSwapProg :
let x = MkIdent 0 in
let y = MkIdent 1 in
let t = MkIdent 2 in
let a = MkIdent 3 in
let b = MkIdent 4 in
let swap = Sseq (Sseq (Sassign t (Evar x))
(Sassign x (Evar y)))
(Sassign y (Evar t)) in
let p = Fand (Fcomp (Evar x) BOeq (Evar a)) (Fcomp (Evar y) BOeq (Evar b)) in
let q = Fand (Fcomp (Evar y) BOeq (Evar a)) (Fcomp (Evar x) BOeq (Evar b)) in
infHu p idUpd swap q
end
theory HuSoundness
use Semantics
use SystemHu
(* The following three lemmas are required *)
(* the first is proved using "induction_pr" after "unfold valid_triple *)
(* the others follow from the first without induction *)
lemma core_while_rule:
forall c:stmt, b:bexpr, inv :fmla.
valid_triple (Fand inv (Fembed b)) idUpd c inv ->
forall ainv:fmla. valid_triple inv idUpd (Swhile b ainv c) (Fand inv (Fnot (Fembed b)))
lemma while_rule:
forall c:stmt, u:upd, b:bexpr, inv ainv p q :fmla.
valid_triple (Fand inv (Fembed b)) idUpd c inv ->
valid_fmla (Fimplies p (applyF u inv)) ->
valid_fmla (Fimplies (Fand inv (Fnot (Fembed b))) q) ->
valid_triple p u (Swhile b ainv c) q
lemma seq_while_rule:
forall c cc:stmt, b:bexpr, u:upd, p q inv ainv :fmla.
valid_fmla (Fimplies p (applyF u inv)) ->
valid_triple (Fand inv (Fembed b)) idUpd c inv ->
valid_triple (Fand inv (Fnot (Fembed b))) idUpd cc q ->
valid_triple p u (Sseq (Swhile b ainv c) cc) q
(* proved with "induction_pr" *)
lemma infHu_sound :
forall c :stmt, u :upd, p q :fmla.
infHu p u c q -> valid_triple p u c q
end
theory HuSoundness_LF
use Semantics
use SystemHu
(* Alternative proof using a lemma function *)
(* Interestingly, induction_pr can be replaced by structural induction *)
(* Only the following core rule about loops is required *)
(* The above proof needed more intermediate results *)
(* probably because with the function lemma the control *)
(* over quantifiers is tight, and no generality is lost *)
(* proved using "induction_pr" after "unfold valid_triple *)
lemma core_while_rule:
forall c:stmt, b:bexpr, inv :fmla.
valid_triple (Fand inv (Fembed b)) idUpd c inv ->
forall ainv :fmla. valid_triple inv idUpd (Swhile b ainv c) (Fand inv (Fnot (Fembed b)))
(* Lemma function follows the structure of the inductive predicate *)
(* termination requires defining an appropriate variant *)
(* we use the previously defined function size *)
let rec lemma infHu_sound_LF (c:stmt)
ensures { forall p q :fmla, u :upd. infHu p u c q -> valid_triple p u c q }
variant { size c }
= match c with
| Sskip -> ()
| Sassign _ _ -> ()
| Sif _ c1 c2 -> infHu_sound_LF c1 ; infHu_sound_LF c2
| Swhile _ _ c -> infHu_sound_LF c
| Sseq Sskip c -> infHu_sound_LF c
| Sseq (Sassign _ _) c -> infHu_sound_LF c
| Sseq (Sif _ c1 c2) c -> infHu_sound_LF (Sseq c1 c) ; infHu_sound_LF (Sseq c2 c)
| Sseq (Swhile _ _ c1) c -> infHu_sound_LF c1 ; infHu_sound_LF c
| Sseq (Sseq c1 c2) c -> infHu_sound_LF (Sseq c1 (Sseq c2 c))
end
end
theory ReverseRules
use export Semantics
use export SystemHu
(* Expressiveness *)
(* Following Cooke and Clarke, we postulate the existence of weakest preconditions *)
(* This implies that loop invariants exist, defined as the wp of the entire loop *)
predicate pre (s:state) (c:stmt) (q:fmla) =
forall s' :state. big_step s c s' -> satisfies s' q
val function wp (c:stmt) (q:fmla) : fmla
ensures { forall s :state. satisfies s result <-> pre s c q }
(* axiom expressiveness : forall c :stmt, q :fmla. *)
(* exists p :fmla. forall s :state. (satisfies s p <-> pre s c q) *)
(* The following lemmas are required to prove the reverse Hu rules below *)
(* wp is used in the lemmas involving loops *)
lemma valid_triple_skip :
forall p q :fmla, u :upd, s :state.
valid_triple p u Sskip q ->
satisfies s p -> big_step (applyS u s) Sskip (applyS u s) -> satisfies (applyS u s) q
(* This one does not seem to be required... *)
(* lemma valid_triple_assign : *)
(* forall p q :fmla, x :ident, e :expr, u :upd, s s' :state. *)
(* valid_triple p u (Sassign x e) q -> *)
(* satisfies s p -> *)
(* big_step (applyS u s) (Sassign x e) (set s x (eval_expr s e)) -> *)
(* satisfies (set s x (eval_expr s e)) q *)
lemma valid_triple_if_true :
forall c1 c2 :stmt, b: bexpr, p q :fmla, u :upd, s s' :state.
valid_triple p u (Sif b c1 c2) q ->
satisfies s p ->
eval_bexpr (applyS u s) b ->
big_step (applyS u s) c1 s' ->
big_step (applyS u s) (Sif b c1 c2) s' /\
satisfies s' q
lemma valid_triple_if_false :
forall c1 c2 :stmt, b: bexpr, p q :fmla, u :upd, s s' :state.
valid_triple p u (Sif b c1 c2) q ->
satisfies s p ->
not (eval_bexpr (applyS u s) b) ->
big_step (applyS u s) c2 s' ->
big_step (applyS u s) (Sif b c1 c2) s' /\
satisfies s' q
lemma valid_triple_while_wp :
forall c :stmt, b: bexpr, p q ainv :fmla, u :upd.
valid_triple p u (Swhile b ainv c) q ->
let inv = wp (Swhile b ainv c) q in
valid_fmla (Fimplies p (applyF u inv)) /\
(forall s s':state. satisfies s (Fand inv (Fembed b)) -> big_step s c s' -> satisfies s' inv) /\
valid_fmla (Fimplies (Fand inv (Fnot (Fembed b))) q)
lemma valid_triple_seq :
forall c1 c2 :stmt, p q :fmla, u :upd, s s' s'' :state.
valid_triple p u (Sseq c1 c2) q ->
satisfies s p-> big_step (applyS u s) c1 s' -> big_step s' c2 s'' -> satisfies s'' q
lemma valid_triple_if_seq_true:
forall c1 c2 cc :stmt, b: bexpr, p q :fmla, u :upd, s s' s'' :state.
valid_triple p u (Sseq (Sif b c1 c2) cc) q ->
satisfies s p ->
eval_bexpr (applyS u s) b ->
big_step (applyS u s) c1 s' ->
big_step (applyS u s) (Sif b c1 c2) s' -> (* this is not required in the lemma but allows it to be proved *)
big_step s' cc s'' ->
satisfies s'' q
lemma valid_triple_if_seq_false :
forall c1 c2 cc :stmt, b: bexpr, p q :fmla, u :upd, s s' s'' :state.
valid_triple p u (Sseq (Sif b c1 c2) cc) q ->
satisfies s p ->
not (eval_bexpr (applyS u s) b) ->
big_step (applyS u s) c2 s' ->
big_step (applyS u s) (Sif b c1 c2) s' -> (* same here *)
big_step s' cc s'' ->
satisfies s'' q
lemma valid_triple_while_seq :
forall c cc :stmt, b: bexpr, p q ainv :fmla, u :upd.
valid_triple p u (Sseq (Swhile b ainv c) cc) q ->
let inv = wp (Swhile b ainv c) (wp cc q) in
valid_fmla (Fimplies p (applyF u inv)) /\
(forall s s':state. satisfies s (Fand inv (Fembed b)) -> big_step s c s' -> satisfies s' inv) /\
(forall s s':state. satisfies s (Fand inv (Fnot (Fembed b))) -> big_step s cc s' -> satisfies s' q)
(* Reverse rules of Hu *)
(* the loop rules state the existence of an invariant *)
lemma skip_rule_rev:
forall p q:fmla, u:upd.
valid_triple p u Sskip q -> valid_fmla (Fimplies p (applyF u q))
lemma assign_rule_rev:
forall p:fmla, q:fmla, x:ident, e:expr, u:upd.
valid_triple p u (Sassign x e) q ->
valid_fmla (Fimplies p (applyF (assign u x e) q))
lemma if_rule_rev:
forall p q:fmla, c1 c2 :stmt, b:bexpr, u:upd.
valid_triple p u (Sif b c1 c2) q ->
valid_triple (Fand p (applyF u (Fembed b))) u c1 q /\
valid_triple (Fand p (applyF u (Fnot (Fembed b)))) u c2 q
lemma while_rule_rev:
forall c:stmt, u:upd, b:bexpr, ainv p q :fmla.
valid_triple p u (Swhile b ainv c) q ->
exists inv:fmla.
valid_fmla (Fimplies p (applyF u inv)) /\
valid_triple (Fand inv (Fembed b)) idUpd c inv /\
valid_fmla (Fimplies (Fand inv (Fnot (Fembed b))) q)
lemma seq_skip_rule_rev:
forall p q:fmla, u:upd, c :stmt.
valid_triple p u (Sseq Sskip c) q -> valid_triple p u c q
lemma seq_assign_rule_rev:
forall p:fmla, q:fmla, x:ident, e:expr, i:stmt, u:upd.
valid_triple p u (Sseq (Sassign x e) i) q ->
valid_triple p (assign u x e) i q
lemma seq_if_rule_rev:
forall p q:fmla, s1 s2 s:stmt, b:bexpr, u:upd.
valid_triple p u (Sseq (Sif b s1 s2) s) q ->
valid_triple (Fand p (applyF u (Fembed b))) u (Sseq s1 s) q /\
valid_triple (Fand p (applyF u (Fnot (Fembed b)))) u (Sseq s2 s) q
lemma seq_while_rule_rev:
forall p q ainv:fmla, c cc:stmt, b:bexpr, u:upd.
valid_triple p u (Sseq (Swhile b ainv c) cc) q ->
exists inv :fmla.
valid_fmla (Fimplies p (applyF u inv)) /\
valid_triple (Fand inv (Fembed b)) idUpd c inv /\
valid_triple (Fand inv (Fnot (Fembed b))) idUpd cc q
lemma seq_seq_rule_rev:
forall c1 c2 c:stmt, u:upd, p q:fmla.
valid_triple p u (Sseq (Sseq c1 c2) c) q ->
valid_triple p u (Sseq c1 (Sseq c2 c)) q
end
theory HuCompleteness
use Semantics
use SystemHu
use ReverseRules
(* This cannot be proved with induction_ty_lex *)
(* lemma infHu_complete : *)
(* forall c[@induction] :stmt, p q :fmla, u :upd. *)
(* valid_triple p u c q -> infHu p u c q *)
(* Lemma Function CAN be proved *)
let rec lemma infHu_complete_LF (c:stmt) =
ensures { forall p q :fmla, u :upd. valid_triple p u c q -> infHu p u c q }
variant { size c }
match c with
| Sskip -> ()
| Sassign _ _ -> ()
| Sif _ c1 c2 -> infHu_complete_LF c1 ; infHu_complete_LF c2
| Swhile _ _ c -> infHu_complete_LF c
| Sseq Sskip c -> infHu_complete_LF c
| Sseq (Sassign _ _) c -> infHu_complete_LF c
| Sseq (Sif _ c1 c2) c -> infHu_complete_LF (Sseq c1 c) ; infHu_complete_LF (Sseq c2 c)
| Sseq (Swhile _ _ c1) c -> infHu_complete_LF c1 ; infHu_complete_LF c
| Sseq (Sseq c1 c2) c -> infHu_complete_LF (Sseq c1 (Sseq c2 c))
end
end
theory SystemHuAnnot
use Semantics
use SystemHu
(* Inference system directed by annotated invariants *)
inductive infHuA fmla upd stmt fmla =
| infHuA_skip:
forall p q:fmla, u:upd.
valid_fmla (Fimplies p (applyF u q)) ->
infHuA p u Sskip q
| infHuA_assign:
forall p:fmla, q:fmla, x:ident, e:expr, u:upd.
valid_fmla (Fimplies p (applyF (assign u x e) q)) ->
infHuA p u (Sassign x e) q
| infHuA_if:
forall p q:fmla, c1 c2 :stmt, b:bexpr, u:upd.
infHuA (Fand p (applyF u (Fembed b))) u c1 q ->
infHuA (Fand p (applyF u (Fnot (Fembed b)))) u c2 q ->
infHuA p u (Sif b c1 c2) q
| infHuA_while:
forall p q:fmla, c :stmt, b:bexpr, ainv:fmla, u:upd.
valid_fmla (Fimplies p (applyF u ainv)) ->
infHuA (Fand ainv (Fembed b)) idUpd c ainv ->
valid_fmla (Fimplies (Fand ainv (Fnot (Fembed b))) q) ->
infHuA p u (Swhile b ainv c) q
| infHuA_skipseq:
forall p q:fmla, u:upd, c :stmt.
infHuA p u c q ->
infHuA p u (Sseq Sskip c) q
| infHuA_assignseq:
forall p:fmla, q:fmla, x:ident, e:expr, c:stmt, u:upd.
infHuA p (assign u x e) c q ->
infHuA p u (Sseq (Sassign x e) c) q
| infHuA_ifseq:
forall p q:fmla, c1 c2 c:stmt, b:bexpr, u:upd.
infHuA (Fand p (applyF u (Fembed b))) u (Sseq c1 c) q ->
infHuA (Fand p (applyF u (Fnot (Fembed b)))) u (Sseq c2 c) q ->
infHuA p u (Sseq (Sif b c1 c2) c) q