forked from achlipala/frap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelChecking.v
1595 lines (1377 loc) · 49.9 KB
/
ModelChecking.v
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
(** Formal Reasoning About Programs <http://adam.chlipala.net/frap/>
* Chapter 7: Model Checking
* Author: Adam Chlipala
* License: https://creativecommons.org/licenses/by-nc-nd/4.0/ *)
Set Warnings "-notation-overridden". (* <-- needed while we play with defining one
* of the book's notations ourselves locally *)
Require Import Frap TransitionSystems.
Set Implicit Arguments.
(* Coming up with invariants ourselves can be tedious! Let's investigate how we
* can automate the choice of invariants, for systems with only finitely many
* reachable states. This style is known as model checking. First, let's think
* more deliberately about how to grow a candidate invariant by adding new cases
* that we missed. Here's what it means for one invariant to retain all cases of
* another. *)
Definition oneStepClosure_current {state} (sys : trsys state)
(invariant1 invariant2 : state -> Prop) :=
forall st, invariant1 st
-> invariant2 st.
(* And here's what it means to add all new states reachable from the original
* set. *)
Definition oneStepClosure_new {state} (sys : trsys state)
(invariant1 invariant2 : state -> Prop) :=
forall st st', invariant1 st
-> sys.(Step) st st'
-> invariant2 st'.
(* Putting together the two conditions, we have a closure operator, for
* enriching a candidate invariant with all new states reachable from it in a
* single step. *)
Definition oneStepClosure {state} (sys : trsys state)
(invariant1 invariant2 : state -> Prop) :=
oneStepClosure_current sys invariant1 invariant2
/\ oneStepClosure_new sys invariant1 invariant2.
(* Here's a simple restatement of [oneStepClosure] as a theorem with two
* premises. *)
Theorem prove_oneStepClosure : forall state (sys : trsys state) (inv1 inv2 : state -> Prop),
(forall st, inv1 st -> inv2 st)
-> (forall st st', inv1 st -> sys.(Step) st st' -> inv2 st')
-> oneStepClosure sys inv1 inv2.
Proof.
unfold oneStepClosure.
propositional.
Qed.
(* Now imagine the following general procedure to find an invariant. Start with
* the initial states as the candidate invariant. Now take the one-step
* closure, adding all states reachable in one step. Then take it again, and
* again, until the invariant is "big enough." What is the formal meaning of
* this termination condition? We are done if one-step closure brings us back
* to the original set. (Of course, we must also retain all the initial
* states.) *)
Theorem oneStepClosure_done : forall state (sys : trsys state) (invariant : state -> Prop),
(forall st, sys.(Initial) st -> invariant st)
-> oneStepClosure sys invariant invariant
-> invariantFor sys invariant.
Proof.
unfold oneStepClosure, oneStepClosure_current, oneStepClosure_new.
propositional.
apply invariant_induction.
assumption.
simplify.
eapply H2.
eassumption.
assumption.
Qed.
(* Now we define an inductive relation, capturing repeated closure until
* convergence. *)
Inductive multiStepClosure {state} (sys : trsys state)
: (state -> Prop) -> (state -> Prop) -> Prop :=
(* We might be done, if one-step closure has no effect. *)
| MscDone : forall inv,
oneStepClosure sys inv inv
-> multiStepClosure sys inv inv
(* Or we might need to run another one-step closure and recurse. *)
| MscStep : forall inv inv' inv'',
oneStepClosure sys inv inv'
-> multiStepClosure sys inv' inv''
-> multiStepClosure sys inv inv''.
(* Now, with the help of a lemma, we prove that multi-step closure is a sound
* way to find an invariant for any transition system. Note that we really do
* not have that silver bullet here, because, for many systems, multi-step
* closure does not terminate! However, if it does, we get a correct
* invariant. *)
Lemma multiStepClosure_ok' : forall state (sys : trsys state) (inv inv' : state -> Prop),
multiStepClosure sys inv inv'
-> (forall st, sys.(Initial) st -> inv st)
-> invariantFor sys inv'.
Proof.
induct 1; simplify.
apply oneStepClosure_done.
assumption.
assumption.
apply IHmultiStepClosure.
simplify.
unfold oneStepClosure, oneStepClosure_current in *.
propositional.
apply H3.
apply H1.
assumption.
Qed.
Theorem multiStepClosure_ok : forall state (sys : trsys state) (inv : state -> Prop),
multiStepClosure sys sys.(Initial) inv
-> invariantFor sys inv.
Proof.
simplify.
eapply multiStepClosure_ok'.
eassumption.
propositional.
Qed.
(* OK, great. We know how to find invariants if we can evaluate one-step
* closure efficiently. Here's one case that is particularly easy to evaluate,
* starting from the empty set as the invariant. We use a function [constant]
* from the FRAP library, for sets of finite size. In general, we write
* [constant [x1; ..., xN]] for the set [{x1, ..., xN}], and in fact the latter
* notation is available, too. *)
Theorem oneStepClosure_empty : forall state (sys : trsys state),
oneStepClosure sys (constant nil) (constant nil).
Proof.
unfold oneStepClosure, oneStepClosure_current, oneStepClosure_new; propositional.
Qed.
(* In general, for finite sets, we'll compute one-step closure by closing
* separately over each element of the set. This theorem implements one step of
* that process, where we learn that [inv1] accurately captures where we might
* get from state [st] in one step. States [sts] are those left over to
* consider. *)
Theorem oneStepClosure_split : forall state (sys : trsys state) st sts (inv1 inv2 : state -> Prop),
(forall st', sys.(Step) st st' -> inv1 st')
-> oneStepClosure sys (constant sts) inv2
-> oneStepClosure sys (constant (st :: sts)) ({st} \cup inv1 \cup inv2).
Proof.
unfold oneStepClosure, oneStepClosure_current, oneStepClosure_new; propositional.
invert H0.
left.
(* [left] and [right]: prove a disjunction by proving the left or right case,
* respectively. Note that here, we are using the fact that set union
* [\cup] is defined in terms of disjunction. *)
left.
simplify.
propositional.
right.
apply H1.
assumption.
simplify.
propositional.
right.
left.
apply H.
equality.
right.
right.
eapply H2.
eassumption.
assumption.
Qed.
(* A trivial fact about union and singleton sets.
* Note that we model sets as functions that are passed elements, deciding in
* each case whether that element belongs to the set. *)
Theorem singleton_in : forall {A} (x : A) rest,
({x} \cup rest) x.
Proof.
simplify.
left.
simplify.
equality.
Qed.
(* OK, back to our example from last chapter, of factorial as a transition
* system. Here's a good overall correctness condition, which we didn't bother
* to state before. *)
Definition fact_correct (original_input : nat) (st : fact_state) : Prop :=
match st with
| AnswerIs ans => fact original_input = ans
| WithAccumulator _ _ => True
end.
(* Let's also restate the initial-states set using a singleton set. *)
Theorem fact_init_is : forall original_input,
fact_init original_input = {WithAccumulator original_input 1}.
Proof.
simplify.
apply sets_equal; simplify.
(* Note the use of a theorem [sets_equal], saying that sets are equal if they
* have the same elements. *)
propositional.
invert H.
equality.
rewrite <- H0.
constructor.
Qed.
(* Now we will prove that factorial is correct, for the input 2, without needing
* to write out an inductive invariant ourselves. Note that it's important that
* we choose a small, constant input, so that the reachable state space is
* finite and tractable. *)
Theorem factorial_ok_2 :
invariantFor (factorial_sys 2) (fact_correct 2).
Proof.
simplify.
eapply invariant_weaken.
(* We begin like in last chapter, by strengthening to an inductive
* invariant. *)
apply multiStepClosure_ok.
(* The difference is that we will use multi-step closure to find the invariant
* automatically. Note that the invariant appears as an existential variable,
* whose name begins with a question mark. *)
simplify.
rewrite fact_init_is.
(* It's important to phrase the current candidate invariant explicitly as a
* finite set, before continuing. Otherwise, it won't be obvious how to take
* the one-step closure. *)
(* Compute which states are reachable after one step. *)
eapply MscStep.
apply oneStepClosure_split; simplify.
invert H; simplify.
apply singleton_in.
apply oneStepClosure_empty.
simplify.
(* Compute which states are reachable after two steps. *)
eapply MscStep.
apply oneStepClosure_split; simplify.
invert H; simplify.
apply singleton_in.
apply oneStepClosure_split; simplify.
invert H; simplify.
apply singleton_in.
apply oneStepClosure_empty.
simplify.
(* Compute which states are reachable after three steps. *)
eapply MscStep.
apply oneStepClosure_split; simplify.
invert H; simplify.
apply singleton_in.
apply oneStepClosure_split; simplify.
invert H; simplify.
apply singleton_in.
apply oneStepClosure_split; simplify.
invert H; simplify.
apply singleton_in.
apply oneStepClosure_empty.
simplify.
(* Now the candidate invariant is closed under single steps. Let's prove
* it. *)
apply MscDone.
apply prove_oneStepClosure; simplify.
propositional.
propositional; invert H0; try equality.
invert H; equality.
invert H1; equality.
(* Finally, we prove that our new invariant implies the simpler, noninductive
* one that we started with. *)
simplify.
propositional; subst; simplify; propositional.
(* [subst]: remove all hypotheses like [x = e] for variables [x], simply
* replacing all uses of [x] by [e]. *)
Qed.
(* That process was so rote that we can automate it all, in a generic way that
* will work for most transition systems that have finitely many reachable
* states. Here is a definition of some tactics to do the work.
* BEGIN CODE THAT WILL NOT BE EXPLAINED IN DETAIL! *)
Local Hint Rewrite fact_init_is.
Ltac model_check_done :=
apply MscDone; apply prove_oneStepClosure; simplify; propositional; subst;
repeat match goal with
| [ H : _ |- _ ] => invert H
end; simplify; equality.
Theorem singleton_in_other : forall {A} (x : A) (s1 s2 : set A),
s2 x
-> (s1 \cup s2) x.
Proof.
simplify.
right.
right.
assumption.
Qed.
Ltac singletoner :=
repeat match goal with
| [ |- _ ?S ] => idtac S; apply singleton_in
| [ |- (_ \cup _) _ ] => apply singleton_in_other
end.
Ltac model_check_step0 :=
eapply MscStep; [
repeat ((apply oneStepClosure_empty; simplify)
|| (apply oneStepClosure_split; [ simplify;
repeat match goal with
| [ H : _ |- _ ] => invert H; try congruence
end; solve [ singletoner ] | ]))
| simplify ].
Ltac model_check_step :=
match goal with
| [ |- multiStepClosure _ ?inv1 _ ] =>
model_check_step0;
match goal with
| [ |- multiStepClosure _ ?inv2 _ ] =>
(assert (inv1 = inv2) by compare_sets; fail 3)
|| idtac
end
end.
Ltac model_check_steps1 := model_check_step || model_check_done.
Ltac model_check_steps := repeat model_check_steps1.
Ltac model_check_finish := simplify; propositional; subst; simplify; equality.
Ltac model_check_infer :=
apply multiStepClosure_ok; simplify; model_check_steps.
Ltac model_check_find_invariant :=
simplify; eapply invariant_weaken; [ model_check_infer | ]; cbv beta in *.
Ltac model_check := model_check_find_invariant; model_check_finish.
(* END CODE THAT WILL NOT BE EXPLAINED IN DETAIL! *)
(* Now watch this. We can check various instances of factorial
* automatically. Notice that reachable states are printed as we encounter them
* in exploration, using [idtac] invocations above. This printing is for the
* user's understanding and has no logical meaning. *)
Theorem factorial_ok_2_snazzy :
invariantFor (factorial_sys 2) (fact_correct 2).
Proof.
model_check.
Qed.
Theorem factorial_ok_3 :
invariantFor (factorial_sys 3) (fact_correct 3).
Proof.
model_check.
Qed.
Theorem factorial_ok_5 :
invariantFor (factorial_sys 5) (fact_correct 5).
Proof.
model_check.
Qed.
(* Let's see that last one broken into two steps, so that we get a look at the
* inferred invariant. *)
Theorem factorial_ok_5_again :
invariantFor (factorial_sys 5) (fact_correct 5).
Proof.
model_check_find_invariant.
model_check_finish.
Qed.
(** * Abstraction *)
(* It's lovely when we happen to be analyzing a system with a finite state
* space, but usually we aren't that lucky. For instance, imagine that we are
* using a programming language with infinite-precision integers, and we want to
* check this program:
* <<
int global = 0;
thread() {
int local;
while (true) {
local = global;
global = local + 2;
}
}
>>
* The program loops indefinitely, adding 2 to a global variable. We want to
* prove that "global" always holds an even value. Here's how we can formalize
* evenness inductively. *)
Inductive isEven : nat -> Prop :=
| EvenO : isEven 0
| EvenSS : forall n, isEven n -> isEven (S (S n)).
(* And now we define a transition system for the program, in a process that
* should be routine by now. We use last chapter's concept of a multithreaded
* transition system. *)
Inductive add2_thread :=
| Read
| Write (local : nat).
Inductive add2_init : threaded_state nat add2_thread -> Prop :=
| Add2Init : add2_init {| Shared := 0; Private := Read |}.
Inductive add2_step : threaded_state nat add2_thread -> threaded_state nat add2_thread -> Prop :=
| StepRead : forall global,
add2_step {| Shared := global; Private := Read |}
{| Shared := global; Private := Write global |}
| StepWrite : forall global local,
add2_step {| Shared := global; Private := Write local |}
{| Shared := S (S local); Private := Read |}.
Definition add2_sys1 := {|
Initial := add2_init;
Step := add2_step
|}.
Definition add2_sys := parallel add2_sys1 add2_sys1.
(* Here is the invariant we want to prove. *)
Definition add2_correct (st : threaded_state nat (add2_thread * add2_thread)) :=
isEven st.(Shared).
(* We can't model-check [add2_sys] directly, because it can reach infinitely
* many states. Even if we worked with fixed-precision integers, say with 64
* bits, the state space would be impractically large to explore directly.
* Instead, we will *abstract* this system into another one that retains its
* essential properties. In particular, we want to find another transition
* system that *simulates* this one, in the sense made precise by this
* definition, where [sys1] will be [add2_sys] for this example. *)
Inductive simulates state1 state2 (R : state1 -> state2 -> Prop)
(* [R] is a relation connecting the states of the two systems. *)
(sys1 : trsys state1) (sys2 : trsys state2) : Prop :=
| Simulates :
(* Every initial state of [sys1] has some matching initial state of [sys2]. *)
(forall st1, sys1.(Initial) st1
-> exists st2, R st1 st2
/\ sys2.(Initial) st2)
(* Starting from a pair of related states, every step in [sys1] can be matched
* in [sys2], to destinations that are also related. *)
-> (forall st1 st2, R st1 st2
-> forall st1', sys1.(Step) st1 st1'
-> exists st2', R st1' st2'
/\ sys2.(Step) st2 st2')
-> simulates R sys1 sys2.
(* Given an invariant for [sys2], we now have a generic way of defining an
* invariant for [sys1], by composing with [R]. *)
Inductive invariantViaSimulation state1 state2 (R : state1 -> state2 -> Prop)
(inv2 : state2 -> Prop)
: state1 -> Prop :=
| InvariantViaSimulation : forall st1 st2, R st1 st2
-> inv2 st2
-> invariantViaSimulation R inv2 st1.
(* By way of a lemma, let's prove that, given a simulation, any
* invariant-via-simulation really is an invariant for the original system. *)
Lemma invariant_simulates' : forall state1 state2 (R : state1 -> state2 -> Prop)
(sys1 : trsys state1) (sys2 : trsys state2),
(forall st1 st2, R st1 st2
-> forall st1', sys1.(Step) st1 st1'
-> exists st2', R st1' st2'
/\ sys2.(Step) st2 st2')
-> forall st1 st1', sys1.(Step)^* st1 st1'
-> forall st2, R st1 st2
-> exists st2', R st1' st2'
/\ sys2.(Step)^* st2 st2'.
Proof.
induct 2.
simplify.
exists st2.
(* [exists E]: prove [exists x, P(x)] by proving [P(E)]. *)
propositional.
constructor.
simplify.
eapply H in H2.
first_order.
(* [first_order]: simplify first-order logic structure. Be forewarned: this
* one is especially likely to run forever! *)
apply IHtrc in H2.
first_order.
exists x1.
propositional.
econstructor.
eassumption.
assumption.
assumption.
Qed.
Theorem invariant_simulates : forall state1 state2 (R : state1 -> state2 -> Prop)
(sys1 : trsys state1) (sys2 : trsys state2) (inv2 : state2 -> Prop),
simulates R sys1 sys2
-> invariantFor sys2 inv2
-> invariantFor sys1 (invariantViaSimulation R inv2).
Proof.
simplify.
invert H.
unfold invariantFor; simplify.
apply H1 in H.
first_order.
apply invariant_simulates' with (sys2 := sys2) (R := R) (st2 := x) in H3; try assumption.
first_order.
unfold invariantFor in H0.
apply H0 with (s' := x0) in H4; try assumption.
econstructor.
eassumption.
assumption.
Qed.
(* OK, that's a general theory for abstracting a system with another one that
* simulates it. What abstraction will work for our example of the two threads
* and the counter? Here's another program that has replaced integers with
* Booleans, where the Boolean is true iff the matching integer is even.
* <<
bool global = true;
thread() {
bool local;
while (true) {
local = global;
global = local;
}
}
>>
* We can formalize this program as a transition system, too. *)
Inductive add2_bthread :=
| BRead
| BWrite (local : bool).
Inductive add2_binit : threaded_state bool add2_bthread -> Prop :=
| Add2BInit : add2_binit {| Shared := true; Private := BRead |}.
Inductive add2_bstep : threaded_state bool add2_bthread -> threaded_state bool add2_bthread -> Prop :=
| StepBRead : forall global,
add2_bstep {| Shared := global; Private := BRead |}
{| Shared := global; Private := BWrite global |}
| StepBWrite : forall global local,
add2_bstep {| Shared := global; Private := BWrite local |}
{| Shared := local; Private := BRead |}.
Definition add2_bsys1 := {|
Initial := add2_binit;
Step := add2_bstep
|}.
Definition add2_bsys := parallel add2_bsys1 add2_bsys1.
(* This invariant formalizes the connection between local states of threads, in
* the original and abstracted systems. *)
Inductive R_private1 : add2_thread -> add2_bthread -> Prop :=
| RpRead : R_private1 Read BRead
| RpWrite : forall n b, (b = true <-> isEven n)
-> R_private1 (Write n) (BWrite b).
(* We lift [R_private1] to a relation over whole states. *)
Inductive add2_R : threaded_state nat (add2_thread * add2_thread)
-> threaded_state bool (add2_bthread * add2_bthread)
-> Prop :=
| Add2_R : forall n b th1 th2 th1' th2',
(b = true <-> isEven n)
-> R_private1 th1 th1'
-> R_private1 th2 th2'
-> add2_R {| Shared := n; Private := (th1, th2) |}
{| Shared := b; Private := (th1', th2') |}.
(* Let's also recharacterize the initial states via a singleton set. *)
Theorem add2_init_is :
parallel_init add2_binit add2_binit = { {| Shared := true; Private := (BRead, BRead) |} }.
Proof.
simplify.
apply sets_equal; simplify.
propositional.
invert H.
invert H2.
invert H4.
equality.
invert H0.
constructor.
constructor.
constructor.
Qed.
(* We ask Coq to remember this lemma as a hint, which will be used by the
* model-checking tactics that we refrain from explaining in detail. *)
Local Hint Rewrite add2_init_is.
(* Now, let's verify the original system. *)
Theorem add2_ok :
invariantFor add2_sys add2_correct.
Proof.
(* First step: strengthen the invariant. We leave an underscore for the
* unknown invariant, to be found by model checking. *)
eapply invariant_weaken with (invariant1 := invariantViaSimulation add2_R _).
(* One way to find an invariant-by-simulation is to find an invariant for the
* abstracted system, as this step asks to do. *)
apply invariant_simulates with (sys2 := add2_bsys).
(* Now we must prove that the simulation via [add2_R] is valid, which is
* routine. *)
constructor; simplify.
invert H.
invert H0.
invert H1.
exists {| Shared := true; Private := (BRead, BRead) |}; simplify.
propositional.
constructor.
propositional.
constructor.
constructor.
constructor.
invert H.
invert H0; simplify.
invert H7.
invert H2.
exists {| Shared := b; Private := (BWrite b, th2') |}.
propositional.
constructor.
propositional.
constructor.
propositional.
assumption.
constructor.
constructor.
invert H2.
exists {| Shared := b0; Private := (BRead, th2') |}.
propositional.
constructor.
propositional.
constructor.
assumption.
invert H0.
propositional.
constructor.
assumption.
constructor.
constructor.
invert H7.
invert H3.
exists {| Shared := b; Private := (th1', BWrite b) |}.
propositional.
constructor.
propositional.
assumption.
constructor.
propositional.
constructor.
constructor.
invert H3.
exists {| Shared := b0; Private := (th1', BRead) |}.
propositional.
constructor.
propositional.
constructor.
assumption.
invert H0.
propositional.
assumption.
constructor.
constructor.
constructor.
(* OK, we're glad to have that over with! Such a process could also be
* automated, but we won't bother doing so here. However, we are now in a
* good state, where our model checker can find the invariant
* automatically. *)
model_check_infer.
(* It finds exactly four reachable states. We finish by showing that they all
* obey the original invariant. *)
invert 1. (* Note that this [1] means "first premise below the double
* line." *)
invert H0.
simplify.
unfold add2_correct.
simplify.
propositional; subst.
invert H.
propositional.
invert H1.
propositional.
invert H.
propositional.
invert H1.
propositional.
Qed.
(** * Another abstraction example *)
(* Let's try a fancier example of abstraction. Here's a simple integer
* function.
* <<
f(int n) {
int i, j;
i = 0;
j = 0;
while (n > 0) {
i = i + n;
j = j + n;
n = n - 1;
}
}
>>
* We might want to prove that "i" and "j" are always equal at the end.
* First, we formalize the transition system. *)
Inductive pc :=
| i_gets_0
| j_gets_0
| Loop
| i_add_n
| j_add_n
| n_sub_1
| Done.
Record vars := {
N : nat;
I : nat;
J : nat
}.
Record state := {
Pc : pc;
Vars : vars
}.
Inductive initial : state -> Prop :=
| Init : forall vs, initial {| Pc := i_gets_0; Vars := vs |}.
Inductive step : state -> state -> Prop :=
| Step_i_gets_0 : forall n i j,
step {| Pc := i_gets_0; Vars := {| N := n;
I := i;
J := j |} |}
{| Pc := j_gets_0; Vars := {| N := n;
I := 0;
J := j |} |}
| Step_j_gets_0 : forall n i j,
step {| Pc := j_gets_0; Vars := {| N := n;
I := i;
J := j |} |}
{| Pc := Loop; Vars := {| N := n;
I := i;
J := 0 |} |}
| Step_Loop_done : forall i j,
step {| Pc := Loop; Vars := {| N := 0;
I := i;
J := j |} |}
{| Pc := Done; Vars := {| N := 0;
I := i;
J := j |} |}
| Step_Loop_enter : forall n i j,
step {| Pc := Loop; Vars := {| N := S n;
I := i;
J := j |} |}
{| Pc := i_add_n; Vars := {| N := S n;
I := i;
J := j |} |}
| Step_i_add_n : forall n i j,
step {| Pc := i_add_n; Vars := {| N := n;
I := i;
J := j |} |}
{| Pc := j_add_n; Vars := {| N := n;
I := i + n;
J := j |} |}
| Step_j_add_n : forall n i j,
step {| Pc := j_add_n; Vars := {| N := n;
I := i;
J := j |} |}
{| Pc := n_sub_1; Vars := {| N := n;
I := i;
J := j + n |} |}
| Step_n_sub_1 : forall n i j,
step {| Pc := n_sub_1; Vars := {| N := n;
I := i;
J := j |} |}
{| Pc := Loop; Vars := {| N := n - 1;
I := i;
J := j |} |}.
Definition loopy_sys := {|
Initial := initial;
Step := step
|}.
Definition loopy_correct (st : state) :=
st.(Pc) = Done -> st.(Vars).(I) = st.(Vars).(J).
(* Which abstraction will give us a finite-state system? Unlike with factorial,
* here we are more ambitious, seeking an abstraction that will be finite-state
* even when considering all possible parameter values "n". Let's try this
* simple abstract version of variable state. *)
Inductive absvars :=
| Unknown
(* We don't know anything about the values of the variables. *)
| i_is_0
(* We know [i == 0]. *)
| i_eq_j
(* We know [i == j]. *)
| i_eq_j_plus_n.
(* We know [i == j + n]. *)
(* To get our abstract states, we keep the same program counters and just change
* out the variable state. *)
Record absstate := {
APc : pc;
AVars : absvars
}.
(* Here's the rather boring new abstract step relation. Note the clever state
* transformations, in terms of our new abstraction. *)
Inductive absstep : absstate -> absstate -> Prop :=
| AStep_i_gets_0 : forall vs,
absstep {| APc := i_gets_0; AVars := vs |}
{| APc := j_gets_0; AVars := i_is_0 |}
| AStep_j_gets_0_i_is_0 :
absstep {| APc := j_gets_0; AVars := i_is_0 |}
{| APc := Loop; AVars := i_eq_j |}
| AStep_j_gets_0_Other : forall vs,
vs <> i_is_0
-> absstep {| APc := j_gets_0; AVars := vs |}
{| APc := Loop; AVars := Unknown |}
| AStep_Loop_done : forall vs,
absstep {| APc := Loop; AVars := vs |}
{| APc := Done; AVars := vs |}
| AStep_Loop_enter : forall vs,
absstep {| APc := Loop; AVars := vs |}
{| APc := i_add_n; AVars := vs |}
| AStep_i_add_n_i_eq_j :
absstep {| APc := i_add_n; AVars := i_eq_j |}
{| APc := j_add_n; AVars := i_eq_j_plus_n |}
| AStep_i_add_n_Other : forall vs,
vs <> i_eq_j
-> absstep {| APc := i_add_n; AVars := vs |}
{| APc := j_add_n; AVars := Unknown |}
| AStep_j_add_n_i_eq_j_plus_n :
absstep {| APc := j_add_n; AVars := i_eq_j_plus_n |}
{| APc := n_sub_1; AVars := i_eq_j |}
| AStep_j_add_n_i_Other : forall vs,
vs <> i_eq_j_plus_n
-> absstep {| APc := j_add_n; AVars := vs |}
{| APc := n_sub_1; AVars := Unknown |}
| AStep_n_sub_1_bad :
absstep {| APc := n_sub_1; AVars := i_eq_j_plus_n |}
{| APc := Loop; AVars := Unknown |}
| AStep_n_sub_1_good : forall vs,
vs <> i_eq_j_plus_n
-> absstep {| APc := n_sub_1; AVars := vs |}
{| APc := Loop; AVars := vs |}.
Definition absloopy_sys := {|
Initial := { {| APc := i_gets_0; AVars := Unknown |} };
Step := absstep
|}.
(* Now we need our simulation relation. First, we define one just at the level
* of local-variable state. It formalizes our intuition about those values. *)
Inductive Rvars : vars -> absvars -> Prop :=
| Rv_Unknown : forall vs, Rvars vs Unknown
| Rv_i_is_0 : forall vs, vs.(I) = 0 -> Rvars vs i_is_0
| Rv_i_eq_j : forall vs, vs.(I) = vs.(J) -> Rvars vs i_eq_j
| Rv_i_eq_j_plus_n : forall vs, vs.(I) = vs.(J) + vs.(N) -> Rvars vs i_eq_j_plus_n.
(* We lift to full states in the obvious way. *)
Inductive R : state -> absstate -> Prop :=
| Rcon : forall pc vs avs, Rvars vs avs -> R {| Pc := pc; Vars := vs |}
{| APc := pc; AVars := avs |}.
(* Now we are ready to prove the original system correct. *)
Theorem loopy_ok :
invariantFor loopy_sys loopy_correct.
Proof.
eapply invariant_weaken with (invariant1 := invariantViaSimulation R _).
apply invariant_simulates with (sys2 := absloopy_sys).
(* Here comes another boring simulation proof. *)
constructor; simplify.
invert H.
exists {| APc := i_gets_0; AVars := Unknown |}.
propositional.
constructor.
constructor.
invert H0.
invert H.
exists {| APc := j_gets_0; AVars := i_is_0 |}.
propositional; repeat constructor.
invert H.
invert H3.
exists {| APc := Loop; AVars := Unknown |}; propositional; repeat constructor; equality.
exists {| APc := Loop; AVars := i_eq_j |}; propositional; repeat constructor; equality.
exists {| APc := Loop; AVars := Unknown |}; propositional; repeat constructor; equality.
exists {| APc := Loop; AVars := Unknown |}; propositional; repeat constructor; equality.
exists {| APc := Done; AVars := st2.(AVars) |}.
invert H; simplify; propositional; repeat constructor; equality.
exists {| APc := i_add_n; AVars := st2.(AVars) |}.
invert H; simplify; propositional; repeat constructor; equality.
invert H.
invert H3.
exists {| APc := j_add_n; AVars := Unknown |}; repeat constructor; equality.
exists {| APc := j_add_n; AVars := Unknown |}; repeat constructor; equality.
exists {| APc := j_add_n; AVars := i_eq_j_plus_n |}; repeat constructor; simplify; equality.
exists {| APc := j_add_n; AVars := Unknown |}; repeat constructor; equality.
invert H.
invert H3.
exists {| APc := n_sub_1; AVars := Unknown |}; repeat constructor; equality.
exists {| APc := n_sub_1; AVars := Unknown |}; repeat constructor; equality.
exists {| APc := n_sub_1; AVars := Unknown |}; repeat constructor; equality.
exists {| APc := n_sub_1; AVars := i_eq_j |}; repeat constructor; simplify; equality.
invert H.
invert H3.
exists {| APc := Loop; AVars := Unknown |}; propositional; repeat constructor; equality.
exists {| APc := Loop; AVars := i_is_0 |}; propositional; repeat constructor; equality.
exists {| APc := Loop; AVars := i_eq_j |}; propositional; repeat constructor; equality.
exists {| APc := Loop; AVars := Unknown |}; propositional; repeat constructor; equality.
(* Finally, we can call the model checker to find an invariant of the abstract
* system. *)
model_check_infer.
(* We get 7 neat little states, one per program counter. Next, we prove that
* each of them implies the original invariant. *)
invert 1.
invert H0.
unfold loopy_correct.
simplify.
propositional; subst.
(* Most of the hypotheses we invert are contradictory, implying that distinct
* program counters are equal. *)
invert H2.
invert H1.
invert H2.
invert H1.
invert H.
assumption.
invert H2.
invert H1.
invert H2.
Qed.
(** * Modularity *)
(* Throughout the book, we'll come again and again to our two main weapons in
* soundly modeling complex transition systems with simpler ones. We just