-
Notifications
You must be signed in to change notification settings - Fork 7
/
Lab3.v
260 lines (211 loc) · 7.74 KB
/
Lab3.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
(** * 6.887 Formal Reasoning About Programs - Lab 3
* Model Checking, including abstraction *)
Require Import Frap.
Set Implicit Arguments.
(* Authors: Peng Wang ([email protected]), Adam Chlipala ([email protected]) *)
(* Examples based on ideas introduced in ModelChecking.v, from the book source.
* Key definitions about model checking are imported from the Frap library. *)
(* Use model checking to verify the following program
* against the property that j==3 when the program finishes.
<<
int i = 0;
int j = 0;
void foo() {
while (i <= 2){
++i;
++j;
}
}
>>
*)
(* CHALLENGE #1: We've made a small mistake in formalizing this program as a
* transition system. Correct the mistake. (Otherwise, the program will not,
* in fact, satisfy the spec!) *)
Inductive pc :=
| Loop
| i_add_1
| j_add_1
| Done.
Record vars := {
I : nat;
J : nat
}.
Record state := {
Pc : pc;
Vars : vars
}.
Definition foo_init := { {| Pc := Loop; Vars := {| I := 0; J := 0 |} |} }.
Inductive step : state -> state -> Prop :=
| Step_Loop_done : forall i j,
step {| Pc := Loop; Vars := {| I := i;
J := j |} |}
{| Pc := Done; Vars := {| I := i;
J := j |} |}
| Step_Loop_enter : forall i j,
step {| Pc := Loop; Vars := {| I := i;
J := j |} |}
{| Pc := i_add_1; Vars := {| I := i;
J := j |} |}
| Step_i_add_1 : forall i j,
step {| Pc := i_add_1; Vars := {| I := i;
J := j |} |}
{| Pc := j_add_1; Vars := {| I := 1 + i;
J := j |} |}
| Step_j_add_1 : forall i j,
step {| Pc := j_add_1; Vars := {| I := i;
J := j |} |}
{| Pc := Loop; Vars := {| I := i;
J := 1 + j |} |}.
Definition foo_sys := {|
Initial := foo_init;
Step := step
|}.
Definition foo_correct (st : state) :=
st.(Pc) = Done -> st.(Vars).(J) = 3.
(* A hint to help the model checker, related to when to unfold definitions *)
Arguments foo_correct / .
(* CHALLENGE #2: Prove the system correct.
* WARNING: with the broken system above, the model checker is likely to run for
* intractably long! However, when you fix the system definition, this should
* be a very easy proof, thanks to the magic of automatic state-space
* exploration. *)
Theorem foo_ok :
invariantFor foo_sys foo_correct.
Proof.
Admitted.
(* Next, we'll look at verifying the following two-thread producer/consumer
* program against the property that MIN <= x <= MAX always holds.
<<
x = MIN;
void producer(){
while(true){
lock();
if (x < MAX) {
++x;
}
unlock();
}
}
void consumer(){
while(true){
lock();
if (x > MIN) {
--x;
}
unlock();
}
}
>>
*)
(* Here's our formal encoding of the system, which this time you can trust to be
* correct. *)
Notation num := nat.
Section prco.
(* We use sections and variables to introduce local, unknown parameters. *)
Variable MIN : num.
Variable MAX : num.
Record shared_state val := { Locked : bool; X : val }.
Inductive producer_pc :=
| PrLock
| PrTest
| PrInc
| PrUnlock.
Definition producer_state := threaded_state (shared_state num) producer_pc.
Definition producer_init := { {| Shared := {| Locked := false; X := MIN |};
Private := PrLock |} }.
Inductive producer_step : producer_state -> producer_state -> Prop :=
| PrStep_Lock : forall x,
producer_step {| Shared := {| Locked := false; X := x|}; Private := PrLock |}
{| Shared := {| Locked := true; X := x|}; Private := PrTest |}
| PrStep_Test_true : forall l x,
x < MAX ->
producer_step {| Shared := {| Locked := l; X := x|}; Private := PrTest |}
{| Shared := {| Locked := l; X := x|}; Private := PrInc |}
| PrStep_Test_false : forall l x,
x >= MAX ->
producer_step {| Shared := {| Locked := l; X := x|}; Private := PrTest |}
{| Shared := {| Locked := l; X := x|}; Private := PrUnlock |}
| PrStep_Inc : forall l x,
producer_step {| Shared := {| Locked := l; X := x|}; Private := PrInc |}
{| Shared := {| Locked := l; X := 1 + x|}; Private := PrUnlock |}
| PrStep_Unlock : forall l x,
producer_step {| Shared := {| Locked := l; X := x|}; Private := PrUnlock |}
{| Shared := {| Locked := false; X := x|}; Private := PrLock |}.
Definition producer_sys := {|
Initial := producer_init;
Step := producer_step
|}.
Inductive consumer_pc :=
| CoLock
| CoTest
| CoDec
| CoUnlock.
Definition consumer_state := threaded_state (shared_state num) consumer_pc.
Definition consumer_init := { {| Shared := {| Locked := false; X := MIN |};
Private := CoLock |} }.
Inductive consumer_step : consumer_state -> consumer_state -> Prop :=
| CoStep_Lock : forall x,
consumer_step {| Shared := {| Locked := false; X := x|}; Private := CoLock |}
{| Shared := {| Locked := true; X := x|}; Private := CoTest |}
| CoStep_Test_true : forall l x,
x > MIN ->
consumer_step {| Shared := {| Locked := l; X := x|}; Private := CoTest |}
{| Shared := {| Locked := l; X := x|}; Private := CoDec |}
| CoStep_Test_false : forall l x,
x <= MIN ->
consumer_step {| Shared := {| Locked := l; X := x|}; Private := CoTest |}
{| Shared := {| Locked := l; X := x|}; Private := CoUnlock |}
| CoStep_Dec : forall l x,
consumer_step {| Shared := {| Locked := l; X := x|}; Private := CoDec |}
{| Shared := {| Locked := l; X := x - 1|}; Private := CoUnlock |}
| CoStep_Unlock : forall l x,
consumer_step {| Shared := {| Locked := l; X := x|}; Private := CoUnlock |}
{| Shared := {| Locked := false; X := x|}; Private := CoLock |}.
Definition consumer_sys := {|
Initial := consumer_init;
Step := consumer_step
|}.
Definition prco_sys := parallel producer_sys consumer_sys.
Definition prco_state := threaded_state (shared_state num) (producer_pc * consumer_pc).
Definition prco_correct (s : prco_state) :=
MIN <= s.(Shared).(X) <= MAX.
(* Let's re-express the combined initial state as a singleton set. *)
Theorem prco_init_is :
parallel1 producer_init consumer_init = { {| Shared := {| Locked := false; X := MIN|};
Private := (PrLock, CoLock) |} }.
Proof.
simplify.
apply sets_equal; simplify.
propositional.
{
invert H; invert H2; invert H4; equality.
}
invert H0.
repeat constructor.
Qed.
End prco.
Hint Rewrite prco_init_is.
Arguments prco_correct / .
(* We can try model checking [proco_sys] with different [MIN] and [MAX]
* values. *)
Theorem prco_ok_example :
invariantFor (prco_sys 1 2) (prco_correct 1 2).
Proof.
Time model_check_find_invariant.
model_check_finish.
Qed.
(* But the time for model checking grows rapidly with larger constants.
* We should apply abstraction. *)
(* CHALLENGE #3: Come up with a suitable abstraction of this system and use it
* to verify the original program *for all [MIN] and [MAX] values (assuming 0 < MIN < MAX)*, by reduction
* to a finite-state system that can be model checked. *)
Section prco_a.
Variable MIN : num.
Variable MAX : num.
(* We make this assumption about MIN and MAX, which becomes a hypothesis that can be used in the following part of the section *)
Hypothesis MIN_MAX : 0 < MIN < MAX.
Theorem prco_ok :
invariantFor (prco_sys MIN MAX) (prco_correct MIN MAX).
Proof.
Admitted.
End prco_a.