author | wenzelm |
Tue, 13 Sep 2005 22:19:40 +0200 | |
changeset 17356 | 09afdf37cdb3 |
parent 16183 | 052d9aba392d |
child 24051 | 896fb015079c |
permissions | -rw-r--r-- |
15634 | 1 |
(* ID: $Id$ |
11479 | 2 |
Author: Sidi O Ehmety, Computer Laboratory |
3 |
Copyright 2001 University of Cambridge |
|
4 |
*) |
|
5 |
||
15634 | 6 |
header{*Mutual Exclusion*} |
7 |
||
8 |
theory Mutex |
|
9 |
imports SubstAx |
|
10 |
begin |
|
11 |
||
12 |
text{*Based on "A Family of 2-Process Mutual Exclusion Algorithms" by J Misra |
|
13 |
||
14 |
Variables' types are introduced globally so that type verification reduces to |
|
15 |
the usual ZF typechecking: an ill-tyed expressions reduce to the empty set. |
|
16 |
*} |
|
17 |
||
11479 | 18 |
consts |
15634 | 19 |
p :: i |
20 |
m :: i |
|
21 |
n :: i |
|
22 |
u :: i |
|
23 |
v :: i |
|
11479 | 24 |
|
25 |
translations |
|
14046 | 26 |
"p" == "Var([0])" |
27 |
"m" == "Var([1])" |
|
28 |
"n" == "Var([0,0])" |
|
29 |
"u" == "Var([0,1])" |
|
30 |
"v" == "Var([1,0])" |
|
11479 | 31 |
|
15634 | 32 |
axioms --{** Type declarations **} |
33 |
p_type: "type_of(p)=bool & default_val(p)=0" |
|
34 |
m_type: "type_of(m)=int & default_val(m)=#0" |
|
35 |
n_type: "type_of(n)=int & default_val(n)=#0" |
|
36 |
u_type: "type_of(u)=bool & default_val(u)=0" |
|
37 |
v_type: "type_of(v)=bool & default_val(v)=0" |
|
11479 | 38 |
|
39 |
constdefs |
|
40 |
(** The program for process U **) |
|
41 |
U0 :: i |
|
42 |
"U0 == {<s,t>:state*state. t = s(u:=1, m:=#1) & s`m = #0}" |
|
43 |
||
44 |
U1 :: i |
|
45 |
"U1 == {<s,t>:state*state. t = s(p:= s`v, m:=#2) & s`m = #1}" |
|
46 |
||
47 |
U2 :: i |
|
48 |
"U2 == {<s,t>:state*state. t = s(m:=#3) & s`p=0 & s`m = #2}" |
|
49 |
||
50 |
U3 :: i |
|
51 |
"U3 == {<s,t>:state*state. t=s(u:=0, m:=#4) & s`m = #3}" |
|
52 |
||
53 |
U4 :: i |
|
54 |
"U4 == {<s,t>:state*state. t = s(p:=1, m:=#0) & s`m = #4}" |
|
55 |
||
56 |
||
57 |
(** The program for process V **) |
|
58 |
||
59 |
V0 :: i |
|
60 |
"V0 == {<s,t>:state*state. t = s (v:=1, n:=#1) & s`n = #0}" |
|
61 |
||
62 |
V1 :: i |
|
63 |
"V1 == {<s,t>:state*state. t = s(p:=not(s`u), n:=#2) & s`n = #1}" |
|
64 |
||
65 |
V2 :: i |
|
66 |
"V2 == {<s,t>:state*state. t = s(n:=#3) & s`p=1 & s`n = #2}" |
|
67 |
||
68 |
V3 :: i |
|
69 |
"V3 == {<s,t>:state*state. t = s (v:=0, n:=#4) & s`n = #3}" |
|
70 |
||
71 |
V4 :: i |
|
72 |
"V4 == {<s,t>:state*state. t = s (p:=0, n:=#0) & s`n = #4}" |
|
73 |
||
74 |
Mutex :: i |
|
75 |
"Mutex == mk_program({s:state. s`u=0 & s`v=0 & s`m = #0 & s`n = #0}, |
|
14046 | 76 |
{U0, U1, U2, U3, U4, V0, V1, V2, V3, V4}, Pow(state*state))" |
11479 | 77 |
|
78 |
(** The correct invariants **) |
|
79 |
||
80 |
IU :: i |
|
81 |
"IU == {s:state. (s`u = 1<->(#1 $<= s`m & s`m $<= #3)) |
|
82 |
& (s`m = #3 --> s`p=0)}" |
|
83 |
||
84 |
IV :: i |
|
85 |
"IV == {s:state. (s`v = 1 <-> (#1 $<= s`n & s`n $<= #3)) |
|
86 |
& (s`n = #3 --> s`p=1)}" |
|
87 |
||
88 |
(** The faulty invariant (for U alone) **) |
|
89 |
||
90 |
bad_IU :: i |
|
91 |
"bad_IU == {s:state. (s`u = 1 <-> (#1 $<= s`m & s`m $<= #3))& |
|
92 |
(#3 $<= s`m & s`m $<= #4 --> s`p=0)}" |
|
93 |
||
15634 | 94 |
|
95 |
(* Title: ZF/UNITY/Mutex.ML |
|
96 |
ID: $Id \<in> Mutex.ML,v 1.4 2003/05/27 09:39:05 paulson Exp $ |
|
97 |
Author: Sidi O Ehmety, Computer Laboratory |
|
98 |
Copyright 2001 University of Cambridge |
|
99 |
||
100 |
Based on "A Family of 2-Process Mutual Exclusion Algorithms" by J Misra |
|
101 |
||
102 |
Variables' types are introduced globally so that type verification |
|
103 |
reduces to the usual ZF typechecking \<in> an ill-tyed expression will |
|
104 |
reduce to the empty set. |
|
105 |
||
106 |
*) |
|
107 |
||
108 |
(** Variables' types **) |
|
109 |
||
110 |
declare p_type [simp] u_type [simp] v_type [simp] m_type [simp] n_type [simp] |
|
111 |
||
112 |
lemma u_value_type: "s \<in> state ==>s`u \<in> bool" |
|
113 |
apply (unfold state_def) |
|
114 |
apply (drule_tac a = u in apply_type, auto) |
|
115 |
done |
|
116 |
||
117 |
lemma v_value_type: "s \<in> state ==> s`v \<in> bool" |
|
118 |
apply (unfold state_def) |
|
119 |
apply (drule_tac a = v in apply_type, auto) |
|
120 |
done |
|
121 |
||
122 |
lemma p_value_type: "s \<in> state ==> s`p \<in> bool" |
|
123 |
apply (unfold state_def) |
|
124 |
apply (drule_tac a = p in apply_type, auto) |
|
125 |
done |
|
126 |
||
127 |
lemma m_value_type: "s \<in> state ==> s`m \<in> int" |
|
128 |
apply (unfold state_def) |
|
129 |
apply (drule_tac a = m in apply_type, auto) |
|
130 |
done |
|
131 |
||
132 |
lemma n_value_type: "s \<in> state ==>s`n \<in> int" |
|
133 |
apply (unfold state_def) |
|
134 |
apply (drule_tac a = n in apply_type, auto) |
|
135 |
done |
|
136 |
||
137 |
declare p_value_type [simp] u_value_type [simp] v_value_type [simp] |
|
138 |
m_value_type [simp] n_value_type [simp] |
|
139 |
||
140 |
declare p_value_type [TC] u_value_type [TC] v_value_type [TC] |
|
141 |
m_value_type [TC] n_value_type [TC] |
|
142 |
||
143 |
||
144 |
||
145 |
text{*Mutex is a program*} |
|
146 |
||
147 |
lemma Mutex_in_program [simp,TC]: "Mutex \<in> program" |
|
148 |
by (simp add: Mutex_def) |
|
149 |
||
150 |
||
151 |
declare Mutex_def [THEN def_prg_Init, simp] |
|
152 |
ML |
|
153 |
{* |
|
154 |
program_defs_ref := [thm"Mutex_def"] |
|
155 |
*} |
|
156 |
||
157 |
declare U0_def [THEN def_act_simp, simp] |
|
158 |
declare U1_def [THEN def_act_simp, simp] |
|
159 |
declare U2_def [THEN def_act_simp, simp] |
|
160 |
declare U3_def [THEN def_act_simp, simp] |
|
161 |
declare U4_def [THEN def_act_simp, simp] |
|
162 |
||
163 |
declare V0_def [THEN def_act_simp, simp] |
|
164 |
declare V1_def [THEN def_act_simp, simp] |
|
165 |
declare V2_def [THEN def_act_simp, simp] |
|
166 |
declare V3_def [THEN def_act_simp, simp] |
|
167 |
declare V4_def [THEN def_act_simp, simp] |
|
168 |
||
169 |
declare U0_def [THEN def_set_simp, simp] |
|
170 |
declare U1_def [THEN def_set_simp, simp] |
|
171 |
declare U2_def [THEN def_set_simp, simp] |
|
172 |
declare U3_def [THEN def_set_simp, simp] |
|
173 |
declare U4_def [THEN def_set_simp, simp] |
|
174 |
||
175 |
declare V0_def [THEN def_set_simp, simp] |
|
176 |
declare V1_def [THEN def_set_simp, simp] |
|
177 |
declare V2_def [THEN def_set_simp, simp] |
|
178 |
declare V3_def [THEN def_set_simp, simp] |
|
179 |
declare V4_def [THEN def_set_simp, simp] |
|
180 |
||
181 |
declare IU_def [THEN def_set_simp, simp] |
|
182 |
declare IV_def [THEN def_set_simp, simp] |
|
183 |
declare bad_IU_def [THEN def_set_simp, simp] |
|
184 |
||
185 |
lemma IU: "Mutex \<in> Always(IU)" |
|
186 |
apply (rule AlwaysI, force) |
|
16183
052d9aba392d
renamed "constrains" to "safety" to avoid keyword clash
paulson
parents:
15634
diff
changeset
|
187 |
apply (unfold Mutex_def, safety, auto) |
15634 | 188 |
done |
189 |
||
190 |
lemma IV: "Mutex \<in> Always(IV)" |
|
191 |
apply (rule AlwaysI, force) |
|
16183
052d9aba392d
renamed "constrains" to "safety" to avoid keyword clash
paulson
parents:
15634
diff
changeset
|
192 |
apply (unfold Mutex_def, safety) |
15634 | 193 |
done |
194 |
||
195 |
(*The safety property: mutual exclusion*) |
|
196 |
lemma mutual_exclusion: "Mutex \<in> Always({s \<in> state. ~(s`m = #3 & s`n = #3)})" |
|
197 |
apply (rule Always_weaken) |
|
198 |
apply (rule Always_Int_I [OF IU IV], auto) |
|
199 |
done |
|
200 |
||
201 |
(*The bad invariant FAILS in V1*) |
|
202 |
||
203 |
lemma less_lemma: "[| x$<#1; #3 $<= x |] ==> P" |
|
204 |
apply (drule_tac j = "#1" and k = "#3" in zless_zle_trans) |
|
205 |
apply (drule_tac [2] j = x in zle_zless_trans, auto) |
|
206 |
done |
|
207 |
||
208 |
lemma "Mutex \<in> Always(bad_IU)" |
|
209 |
apply (rule AlwaysI, force) |
|
16183
052d9aba392d
renamed "constrains" to "safety" to avoid keyword clash
paulson
parents:
15634
diff
changeset
|
210 |
apply (unfold Mutex_def, safety, auto) |
15634 | 211 |
apply (subgoal_tac "#1 $<= #3") |
212 |
apply (drule_tac x = "#1" and y = "#3" in zle_trans, auto) |
|
213 |
apply (simp (no_asm) add: not_zless_iff_zle [THEN iff_sym]) |
|
214 |
apply auto |
|
215 |
(*Resulting state: n=1, p=false, m=4, u=false. |
|
216 |
Execution of V1 (the command of process v guarded by n=1) sets p:=true, |
|
217 |
violating the invariant!*) |
|
218 |
oops |
|
219 |
||
220 |
||
221 |
||
222 |
(*** Progress for U ***) |
|
223 |
||
224 |
lemma U_F0: "Mutex \<in> {s \<in> state. s`m=#2} Unless {s \<in> state. s`m=#3}" |
|
16183
052d9aba392d
renamed "constrains" to "safety" to avoid keyword clash
paulson
parents:
15634
diff
changeset
|
225 |
by (unfold Unless_def Mutex_def, safety) |
15634 | 226 |
|
227 |
lemma U_F1: |
|
228 |
"Mutex \<in> {s \<in> state. s`m=#1} LeadsTo {s \<in> state. s`p = s`v & s`m = #2}" |
|
229 |
by (unfold Mutex_def, ensures_tac U1) |
|
230 |
||
231 |
lemma U_F2: "Mutex \<in> {s \<in> state. s`p =0 & s`m = #2} LeadsTo {s \<in> state. s`m = #3}" |
|
232 |
apply (cut_tac IU) |
|
233 |
apply (unfold Mutex_def, ensures_tac U2) |
|
234 |
done |
|
235 |
||
236 |
lemma U_F3: "Mutex \<in> {s \<in> state. s`m = #3} LeadsTo {s \<in> state. s`p=1}" |
|
237 |
apply (rule_tac B = "{s \<in> state. s`m = #4}" in LeadsTo_Trans) |
|
238 |
apply (unfold Mutex_def) |
|
239 |
apply (ensures_tac U3) |
|
240 |
apply (ensures_tac U4) |
|
241 |
done |
|
242 |
||
243 |
||
244 |
lemma U_lemma2: "Mutex \<in> {s \<in> state. s`m = #2} LeadsTo {s \<in> state. s`p=1}" |
|
245 |
apply (rule LeadsTo_Diff [OF LeadsTo_weaken_L |
|
246 |
Int_lower2 [THEN subset_imp_LeadsTo]]) |
|
247 |
apply (rule LeadsTo_Trans [OF U_F2 U_F3], auto) |
|
248 |
apply (auto dest!: p_value_type simp add: bool_def) |
|
249 |
done |
|
250 |
||
251 |
lemma U_lemma1: "Mutex \<in> {s \<in> state. s`m = #1} LeadsTo {s \<in> state. s`p =1}" |
|
252 |
by (rule LeadsTo_Trans [OF U_F1 [THEN LeadsTo_weaken_R] U_lemma2], blast) |
|
253 |
||
254 |
lemma eq_123: "i \<in> int ==> (#1 $<= i & i $<= #3) <-> (i=#1 | i=#2 | i=#3)" |
|
255 |
apply auto |
|
256 |
apply (auto simp add: neq_iff_zless) |
|
257 |
apply (drule_tac [4] j = "#3" and i = i in zle_zless_trans) |
|
258 |
apply (drule_tac [2] j = i and i = "#1" in zle_zless_trans) |
|
259 |
apply (drule_tac j = i and i = "#1" in zle_zless_trans, auto) |
|
260 |
apply (rule zle_anti_sym) |
|
261 |
apply (simp_all (no_asm_simp) add: zless_add1_iff_zle [THEN iff_sym]) |
|
262 |
done |
|
263 |
||
264 |
||
265 |
lemma U_lemma123: "Mutex \<in> {s \<in> state. #1 $<= s`m & s`m $<= #3} LeadsTo {s \<in> state. s`p=1}" |
|
266 |
by (simp add: eq_123 Collect_disj_eq LeadsTo_Un_distrib U_lemma1 U_lemma2 U_F3) |
|
267 |
||
268 |
||
269 |
(*Misra's F4*) |
|
270 |
lemma u_Leadsto_p: "Mutex \<in> {s \<in> state. s`u = 1} LeadsTo {s \<in> state. s`p=1}" |
|
271 |
by (rule Always_LeadsTo_weaken [OF IU U_lemma123], auto) |
|
272 |
||
273 |
||
274 |
(*** Progress for V ***) |
|
275 |
||
276 |
lemma V_F0: "Mutex \<in> {s \<in> state. s`n=#2} Unless {s \<in> state. s`n=#3}" |
|
16183
052d9aba392d
renamed "constrains" to "safety" to avoid keyword clash
paulson
parents:
15634
diff
changeset
|
277 |
by (unfold Unless_def Mutex_def, safety) |
15634 | 278 |
|
279 |
lemma V_F1: "Mutex \<in> {s \<in> state. s`n=#1} LeadsTo {s \<in> state. s`p = not(s`u) & s`n = #2}" |
|
280 |
by (unfold Mutex_def, ensures_tac "V1") |
|
281 |
||
282 |
lemma V_F2: "Mutex \<in> {s \<in> state. s`p=1 & s`n = #2} LeadsTo {s \<in> state. s`n = #3}" |
|
283 |
apply (cut_tac IV) |
|
284 |
apply (unfold Mutex_def, ensures_tac "V2") |
|
285 |
done |
|
286 |
||
287 |
lemma V_F3: "Mutex \<in> {s \<in> state. s`n = #3} LeadsTo {s \<in> state. s`p=0}" |
|
288 |
apply (rule_tac B = "{s \<in> state. s`n = #4}" in LeadsTo_Trans) |
|
289 |
apply (unfold Mutex_def) |
|
290 |
apply (ensures_tac V3) |
|
291 |
apply (ensures_tac V4) |
|
292 |
done |
|
293 |
||
294 |
lemma V_lemma2: "Mutex \<in> {s \<in> state. s`n = #2} LeadsTo {s \<in> state. s`p=0}" |
|
295 |
apply (rule LeadsTo_Diff [OF LeadsTo_weaken_L |
|
296 |
Int_lower2 [THEN subset_imp_LeadsTo]]) |
|
297 |
apply (rule LeadsTo_Trans [OF V_F2 V_F3], auto) |
|
298 |
apply (auto dest!: p_value_type simp add: bool_def) |
|
299 |
done |
|
300 |
||
301 |
lemma V_lemma1: "Mutex \<in> {s \<in> state. s`n = #1} LeadsTo {s \<in> state. s`p = 0}" |
|
302 |
by (rule LeadsTo_Trans [OF V_F1 [THEN LeadsTo_weaken_R] V_lemma2], blast) |
|
303 |
||
304 |
lemma V_lemma123: "Mutex \<in> {s \<in> state. #1 $<= s`n & s`n $<= #3} LeadsTo {s \<in> state. s`p = 0}" |
|
305 |
by (simp add: eq_123 Collect_disj_eq LeadsTo_Un_distrib V_lemma1 V_lemma2 V_F3) |
|
306 |
||
307 |
(*Misra's F4*) |
|
308 |
lemma v_Leadsto_not_p: "Mutex \<in> {s \<in> state. s`v = 1} LeadsTo {s \<in> state. s`p = 0}" |
|
309 |
by (rule Always_LeadsTo_weaken [OF IV V_lemma123], auto) |
|
310 |
||
311 |
||
312 |
(** Absence of starvation **) |
|
313 |
||
314 |
(*Misra's F6*) |
|
315 |
lemma m1_Leadsto_3: "Mutex \<in> {s \<in> state. s`m = #1} LeadsTo {s \<in> state. s`m = #3}" |
|
316 |
apply (rule LeadsTo_cancel2 [THEN LeadsTo_Un_duplicate]) |
|
317 |
apply (rule_tac [2] U_F2) |
|
318 |
apply (simp add: Collect_conj_eq) |
|
319 |
apply (subst Un_commute) |
|
320 |
apply (rule LeadsTo_cancel2 [THEN LeadsTo_Un_duplicate]) |
|
321 |
apply (rule_tac [2] PSP_Unless [OF v_Leadsto_not_p U_F0]) |
|
322 |
apply (rule U_F1 [THEN LeadsTo_weaken_R], auto) |
|
323 |
apply (auto dest!: v_value_type simp add: bool_def) |
|
324 |
done |
|
325 |
||
326 |
||
327 |
(*The same for V*) |
|
328 |
lemma n1_Leadsto_3: "Mutex \<in> {s \<in> state. s`n = #1} LeadsTo {s \<in> state. s`n = #3}" |
|
329 |
apply (rule LeadsTo_cancel2 [THEN LeadsTo_Un_duplicate]) |
|
330 |
apply (rule_tac [2] V_F2) |
|
331 |
apply (simp add: Collect_conj_eq) |
|
332 |
apply (subst Un_commute) |
|
333 |
apply (rule LeadsTo_cancel2 [THEN LeadsTo_Un_duplicate]) |
|
334 |
apply (rule_tac [2] PSP_Unless [OF u_Leadsto_p V_F0]) |
|
335 |
apply (rule V_F1 [THEN LeadsTo_weaken_R], auto) |
|
336 |
apply (auto dest!: u_value_type simp add: bool_def) |
|
337 |
done |
|
338 |
||
11479 | 339 |
end |