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 |
method_setup constrains = {*
|
|
152 |
Method.ctxt_args (fn ctxt =>
|
|
153 |
Method.METHOD (fn facts =>
|
|
154 |
gen_constrains_tac (local_clasimpset_of ctxt) 1)) *}
|
|
155 |
"for proving safety properties"
|
|
156 |
|
|
157 |
|
|
158 |
declare Mutex_def [THEN def_prg_Init, simp]
|
|
159 |
ML
|
|
160 |
{*
|
|
161 |
program_defs_ref := [thm"Mutex_def"]
|
|
162 |
*}
|
|
163 |
|
|
164 |
declare U0_def [THEN def_act_simp, simp]
|
|
165 |
declare U1_def [THEN def_act_simp, simp]
|
|
166 |
declare U2_def [THEN def_act_simp, simp]
|
|
167 |
declare U3_def [THEN def_act_simp, simp]
|
|
168 |
declare U4_def [THEN def_act_simp, simp]
|
|
169 |
|
|
170 |
declare V0_def [THEN def_act_simp, simp]
|
|
171 |
declare V1_def [THEN def_act_simp, simp]
|
|
172 |
declare V2_def [THEN def_act_simp, simp]
|
|
173 |
declare V3_def [THEN def_act_simp, simp]
|
|
174 |
declare V4_def [THEN def_act_simp, simp]
|
|
175 |
|
|
176 |
declare U0_def [THEN def_set_simp, simp]
|
|
177 |
declare U1_def [THEN def_set_simp, simp]
|
|
178 |
declare U2_def [THEN def_set_simp, simp]
|
|
179 |
declare U3_def [THEN def_set_simp, simp]
|
|
180 |
declare U4_def [THEN def_set_simp, simp]
|
|
181 |
|
|
182 |
declare V0_def [THEN def_set_simp, simp]
|
|
183 |
declare V1_def [THEN def_set_simp, simp]
|
|
184 |
declare V2_def [THEN def_set_simp, simp]
|
|
185 |
declare V3_def [THEN def_set_simp, simp]
|
|
186 |
declare V4_def [THEN def_set_simp, simp]
|
|
187 |
|
|
188 |
declare IU_def [THEN def_set_simp, simp]
|
|
189 |
declare IV_def [THEN def_set_simp, simp]
|
|
190 |
declare bad_IU_def [THEN def_set_simp, simp]
|
|
191 |
|
|
192 |
lemma IU: "Mutex \<in> Always(IU)"
|
|
193 |
apply (rule AlwaysI, force)
|
|
194 |
apply (unfold Mutex_def, constrains, auto)
|
|
195 |
done
|
|
196 |
|
|
197 |
lemma IV: "Mutex \<in> Always(IV)"
|
|
198 |
apply (rule AlwaysI, force)
|
|
199 |
apply (unfold Mutex_def, constrains)
|
|
200 |
done
|
|
201 |
|
|
202 |
(*The safety property: mutual exclusion*)
|
|
203 |
lemma mutual_exclusion: "Mutex \<in> Always({s \<in> state. ~(s`m = #3 & s`n = #3)})"
|
|
204 |
apply (rule Always_weaken)
|
|
205 |
apply (rule Always_Int_I [OF IU IV], auto)
|
|
206 |
done
|
|
207 |
|
|
208 |
(*The bad invariant FAILS in V1*)
|
|
209 |
|
|
210 |
lemma less_lemma: "[| x$<#1; #3 $<= x |] ==> P"
|
|
211 |
apply (drule_tac j = "#1" and k = "#3" in zless_zle_trans)
|
|
212 |
apply (drule_tac [2] j = x in zle_zless_trans, auto)
|
|
213 |
done
|
|
214 |
|
|
215 |
lemma "Mutex \<in> Always(bad_IU)"
|
|
216 |
apply (rule AlwaysI, force)
|
|
217 |
apply (unfold Mutex_def, constrains, auto)
|
|
218 |
apply (subgoal_tac "#1 $<= #3")
|
|
219 |
apply (drule_tac x = "#1" and y = "#3" in zle_trans, auto)
|
|
220 |
apply (simp (no_asm) add: not_zless_iff_zle [THEN iff_sym])
|
|
221 |
apply auto
|
|
222 |
(*Resulting state: n=1, p=false, m=4, u=false.
|
|
223 |
Execution of V1 (the command of process v guarded by n=1) sets p:=true,
|
|
224 |
violating the invariant!*)
|
|
225 |
oops
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
(*** Progress for U ***)
|
|
230 |
|
|
231 |
lemma U_F0: "Mutex \<in> {s \<in> state. s`m=#2} Unless {s \<in> state. s`m=#3}"
|
|
232 |
by (unfold Unless_def Mutex_def, constrains)
|
|
233 |
|
|
234 |
lemma U_F1:
|
|
235 |
"Mutex \<in> {s \<in> state. s`m=#1} LeadsTo {s \<in> state. s`p = s`v & s`m = #2}"
|
|
236 |
by (unfold Mutex_def, ensures_tac U1)
|
|
237 |
|
|
238 |
lemma U_F2: "Mutex \<in> {s \<in> state. s`p =0 & s`m = #2} LeadsTo {s \<in> state. s`m = #3}"
|
|
239 |
apply (cut_tac IU)
|
|
240 |
apply (unfold Mutex_def, ensures_tac U2)
|
|
241 |
done
|
|
242 |
|
|
243 |
lemma U_F3: "Mutex \<in> {s \<in> state. s`m = #3} LeadsTo {s \<in> state. s`p=1}"
|
|
244 |
apply (rule_tac B = "{s \<in> state. s`m = #4}" in LeadsTo_Trans)
|
|
245 |
apply (unfold Mutex_def)
|
|
246 |
apply (ensures_tac U3)
|
|
247 |
apply (ensures_tac U4)
|
|
248 |
done
|
|
249 |
|
|
250 |
|
|
251 |
lemma U_lemma2: "Mutex \<in> {s \<in> state. s`m = #2} LeadsTo {s \<in> state. s`p=1}"
|
|
252 |
apply (rule LeadsTo_Diff [OF LeadsTo_weaken_L
|
|
253 |
Int_lower2 [THEN subset_imp_LeadsTo]])
|
|
254 |
apply (rule LeadsTo_Trans [OF U_F2 U_F3], auto)
|
|
255 |
apply (auto dest!: p_value_type simp add: bool_def)
|
|
256 |
done
|
|
257 |
|
|
258 |
lemma U_lemma1: "Mutex \<in> {s \<in> state. s`m = #1} LeadsTo {s \<in> state. s`p =1}"
|
|
259 |
by (rule LeadsTo_Trans [OF U_F1 [THEN LeadsTo_weaken_R] U_lemma2], blast)
|
|
260 |
|
|
261 |
lemma eq_123: "i \<in> int ==> (#1 $<= i & i $<= #3) <-> (i=#1 | i=#2 | i=#3)"
|
|
262 |
apply auto
|
|
263 |
apply (auto simp add: neq_iff_zless)
|
|
264 |
apply (drule_tac [4] j = "#3" and i = i in zle_zless_trans)
|
|
265 |
apply (drule_tac [2] j = i and i = "#1" in zle_zless_trans)
|
|
266 |
apply (drule_tac j = i and i = "#1" in zle_zless_trans, auto)
|
|
267 |
apply (rule zle_anti_sym)
|
|
268 |
apply (simp_all (no_asm_simp) add: zless_add1_iff_zle [THEN iff_sym])
|
|
269 |
done
|
|
270 |
|
|
271 |
|
|
272 |
lemma U_lemma123: "Mutex \<in> {s \<in> state. #1 $<= s`m & s`m $<= #3} LeadsTo {s \<in> state. s`p=1}"
|
|
273 |
by (simp add: eq_123 Collect_disj_eq LeadsTo_Un_distrib U_lemma1 U_lemma2 U_F3)
|
|
274 |
|
|
275 |
|
|
276 |
(*Misra's F4*)
|
|
277 |
lemma u_Leadsto_p: "Mutex \<in> {s \<in> state. s`u = 1} LeadsTo {s \<in> state. s`p=1}"
|
|
278 |
by (rule Always_LeadsTo_weaken [OF IU U_lemma123], auto)
|
|
279 |
|
|
280 |
|
|
281 |
(*** Progress for V ***)
|
|
282 |
|
|
283 |
lemma V_F0: "Mutex \<in> {s \<in> state. s`n=#2} Unless {s \<in> state. s`n=#3}"
|
|
284 |
by (unfold Unless_def Mutex_def, constrains)
|
|
285 |
|
|
286 |
lemma V_F1: "Mutex \<in> {s \<in> state. s`n=#1} LeadsTo {s \<in> state. s`p = not(s`u) & s`n = #2}"
|
|
287 |
by (unfold Mutex_def, ensures_tac "V1")
|
|
288 |
|
|
289 |
lemma V_F2: "Mutex \<in> {s \<in> state. s`p=1 & s`n = #2} LeadsTo {s \<in> state. s`n = #3}"
|
|
290 |
apply (cut_tac IV)
|
|
291 |
apply (unfold Mutex_def, ensures_tac "V2")
|
|
292 |
done
|
|
293 |
|
|
294 |
lemma V_F3: "Mutex \<in> {s \<in> state. s`n = #3} LeadsTo {s \<in> state. s`p=0}"
|
|
295 |
apply (rule_tac B = "{s \<in> state. s`n = #4}" in LeadsTo_Trans)
|
|
296 |
apply (unfold Mutex_def)
|
|
297 |
apply (ensures_tac V3)
|
|
298 |
apply (ensures_tac V4)
|
|
299 |
done
|
|
300 |
|
|
301 |
lemma V_lemma2: "Mutex \<in> {s \<in> state. s`n = #2} LeadsTo {s \<in> state. s`p=0}"
|
|
302 |
apply (rule LeadsTo_Diff [OF LeadsTo_weaken_L
|
|
303 |
Int_lower2 [THEN subset_imp_LeadsTo]])
|
|
304 |
apply (rule LeadsTo_Trans [OF V_F2 V_F3], auto)
|
|
305 |
apply (auto dest!: p_value_type simp add: bool_def)
|
|
306 |
done
|
|
307 |
|
|
308 |
lemma V_lemma1: "Mutex \<in> {s \<in> state. s`n = #1} LeadsTo {s \<in> state. s`p = 0}"
|
|
309 |
by (rule LeadsTo_Trans [OF V_F1 [THEN LeadsTo_weaken_R] V_lemma2], blast)
|
|
310 |
|
|
311 |
lemma V_lemma123: "Mutex \<in> {s \<in> state. #1 $<= s`n & s`n $<= #3} LeadsTo {s \<in> state. s`p = 0}"
|
|
312 |
by (simp add: eq_123 Collect_disj_eq LeadsTo_Un_distrib V_lemma1 V_lemma2 V_F3)
|
|
313 |
|
|
314 |
(*Misra's F4*)
|
|
315 |
lemma v_Leadsto_not_p: "Mutex \<in> {s \<in> state. s`v = 1} LeadsTo {s \<in> state. s`p = 0}"
|
|
316 |
by (rule Always_LeadsTo_weaken [OF IV V_lemma123], auto)
|
|
317 |
|
|
318 |
|
|
319 |
(** Absence of starvation **)
|
|
320 |
|
|
321 |
(*Misra's F6*)
|
|
322 |
lemma m1_Leadsto_3: "Mutex \<in> {s \<in> state. s`m = #1} LeadsTo {s \<in> state. s`m = #3}"
|
|
323 |
apply (rule LeadsTo_cancel2 [THEN LeadsTo_Un_duplicate])
|
|
324 |
apply (rule_tac [2] U_F2)
|
|
325 |
apply (simp add: Collect_conj_eq)
|
|
326 |
apply (subst Un_commute)
|
|
327 |
apply (rule LeadsTo_cancel2 [THEN LeadsTo_Un_duplicate])
|
|
328 |
apply (rule_tac [2] PSP_Unless [OF v_Leadsto_not_p U_F0])
|
|
329 |
apply (rule U_F1 [THEN LeadsTo_weaken_R], auto)
|
|
330 |
apply (auto dest!: v_value_type simp add: bool_def)
|
|
331 |
done
|
|
332 |
|
|
333 |
|
|
334 |
(*The same for V*)
|
|
335 |
lemma n1_Leadsto_3: "Mutex \<in> {s \<in> state. s`n = #1} LeadsTo {s \<in> state. s`n = #3}"
|
|
336 |
apply (rule LeadsTo_cancel2 [THEN LeadsTo_Un_duplicate])
|
|
337 |
apply (rule_tac [2] V_F2)
|
|
338 |
apply (simp add: Collect_conj_eq)
|
|
339 |
apply (subst Un_commute)
|
|
340 |
apply (rule LeadsTo_cancel2 [THEN LeadsTo_Un_duplicate])
|
|
341 |
apply (rule_tac [2] PSP_Unless [OF u_Leadsto_p V_F0])
|
|
342 |
apply (rule V_F1 [THEN LeadsTo_weaken_R], auto)
|
|
343 |
apply (auto dest!: u_value_type simp add: bool_def)
|
|
344 |
done
|
|
345 |
|
11479
|
346 |
end |