10 |
10 |
11 (* --------------------------------------------------------------------------- *) |
11 (* --------------------------------------------------------------------------- *) |
12 (* access to definitions *) |
12 (* access to definitions *) |
13 (* --------------------------------------------------------------------------- *) |
13 (* --------------------------------------------------------------------------- *) |
14 |
14 |
15 qed_goalw "step_def2" Loop.thy [step_def] |
15 val step_def2 = prove_goalw Loop.thy [step_def] |
16 "step[b][g][x] = If b[x] then g[x] else x fi" |
16 "step[b][g][x] = If b[x] then g[x] else x fi" |
17 (fn prems => |
17 (fn prems => |
18 [ |
18 [ |
19 (simp_tac Cfun_ss 1) |
19 (simp_tac Cfun_ss 1) |
20 ]); |
20 ]); |
21 |
21 |
22 qed_goalw "while_def2" Loop.thy [while_def] |
22 val while_def2 = prove_goalw Loop.thy [while_def] |
23 "while[b][g] = fix[LAM f x. If b[x] then f[g[x]] else x fi]" |
23 "while[b][g] = fix[LAM f x. If b[x] then f[g[x]] else x fi]" |
24 (fn prems => |
24 (fn prems => |
25 [ |
25 [ |
26 (simp_tac Cfun_ss 1) |
26 (simp_tac Cfun_ss 1) |
27 ]); |
27 ]); |
29 |
29 |
30 (* ------------------------------------------------------------------------- *) |
30 (* ------------------------------------------------------------------------- *) |
31 (* rekursive properties of while *) |
31 (* rekursive properties of while *) |
32 (* ------------------------------------------------------------------------- *) |
32 (* ------------------------------------------------------------------------- *) |
33 |
33 |
34 qed_goal "while_unfold" Loop.thy |
34 val while_unfold = prove_goal Loop.thy |
35 "while[b][g][x] = If b[x] then while[b][g][g[x]] else x fi" |
35 "while[b][g][x] = If b[x] then while[b][g][g[x]] else x fi" |
36 (fn prems => |
36 (fn prems => |
37 [ |
37 [ |
38 (fix_tac5 while_def2 1), |
38 (fix_tac5 while_def2 1), |
39 (simp_tac Cfun_ss 1) |
39 (simp_tac Cfun_ss 1) |
40 ]); |
40 ]); |
41 |
41 |
42 qed_goal "while_unfold2" Loop.thy |
42 val while_unfold2 = prove_goal Loop.thy |
43 "!x.while[b][g][x] = while[b][g][iterate(k,step[b][g],x)]" |
43 "!x.while[b][g][x] = while[b][g][iterate(k,step[b][g],x)]" |
44 (fn prems => |
44 (fn prems => |
45 [ |
45 [ |
46 (nat_ind_tac "k" 1), |
46 (nat_ind_tac "k" 1), |
47 (simp_tac (HOLCF_ss addsimps [iterate_0,iterate_Suc]) 1), |
47 (simp_tac (HOLCF_ss addsimps [iterate_0,iterate_Suc]) 1), |
65 (asm_simp_tac HOLCF_ss 1), |
65 (asm_simp_tac HOLCF_ss 1), |
66 (rtac (while_unfold RS ssubst) 1), |
66 (rtac (while_unfold RS ssubst) 1), |
67 (asm_simp_tac HOLCF_ss 1) |
67 (asm_simp_tac HOLCF_ss 1) |
68 ]); |
68 ]); |
69 |
69 |
70 qed_goal "while_unfold3" Loop.thy |
70 val while_unfold3 = prove_goal Loop.thy |
71 "while[b][g][x] = while[b][g][step[b][g][x]]" |
71 "while[b][g][x] = while[b][g][step[b][g][x]]" |
72 (fn prems => |
72 (fn prems => |
73 [ |
73 [ |
74 (res_inst_tac [("s","while[b][g][iterate(Suc(0),step[b][g],x)]")] trans 1), |
74 (res_inst_tac [("s","while[b][g][iterate(Suc(0),step[b][g],x)]")] trans 1), |
75 (rtac (while_unfold2 RS spec) 1), |
75 (rtac (while_unfold2 RS spec) 1), |
79 |
79 |
80 (* --------------------------------------------------------------------------- *) |
80 (* --------------------------------------------------------------------------- *) |
81 (* properties of while and iterations *) |
81 (* properties of while and iterations *) |
82 (* --------------------------------------------------------------------------- *) |
82 (* --------------------------------------------------------------------------- *) |
83 |
83 |
84 qed_goal "loop_lemma1" Loop.thy |
84 val loop_lemma1 = prove_goal Loop.thy |
85 "[|? y.b[y]=FF; iterate(k,step[b][g],x)=UU|]==>iterate(Suc(k),step[b][g],x)=UU" |
85 "[|? y.b[y]=FF; iterate(k,step[b][g],x)=UU|]==>iterate(Suc(k),step[b][g],x)=UU" |
86 (fn prems => |
86 (fn prems => |
87 [ |
87 [ |
88 (cut_facts_tac prems 1), |
88 (cut_facts_tac prems 1), |
89 (simp_tac iterate_ss 1), |
89 (simp_tac iterate_ss 1), |
94 (etac (flat_tr RS flat_codom RS disjE) 1), |
94 (etac (flat_tr RS flat_codom RS disjE) 1), |
95 (asm_simp_tac HOLCF_ss 1), |
95 (asm_simp_tac HOLCF_ss 1), |
96 (asm_simp_tac HOLCF_ss 1) |
96 (asm_simp_tac HOLCF_ss 1) |
97 ]); |
97 ]); |
98 |
98 |
99 qed_goal "loop_lemma2" Loop.thy |
99 val loop_lemma2 = prove_goal Loop.thy |
100 "[|? y.b[y]=FF;~iterate(Suc(k),step[b][g],x)=UU |]==>\ |
100 "[|? y.b[y]=FF;~iterate(Suc(k),step[b][g],x)=UU |]==>\ |
101 \~iterate(k,step[b][g],x)=UU" |
101 \~iterate(k,step[b][g],x)=UU" |
102 (fn prems => |
102 (fn prems => |
103 [ |
103 [ |
104 (cut_facts_tac prems 1), |
104 (cut_facts_tac prems 1), |
106 (etac loop_lemma1 2), |
106 (etac loop_lemma1 2), |
107 (atac 1), |
107 (atac 1), |
108 (atac 1) |
108 (atac 1) |
109 ]); |
109 ]); |
110 |
110 |
111 qed_goal "loop_lemma3" Loop.thy |
111 val loop_lemma3 = prove_goal Loop.thy |
112 "[|!x. INV(x) & b[x]=TT & ~g[x]=UU --> INV(g[x]);\ |
112 "[|!x. INV(x) & b[x]=TT & ~g[x]=UU --> INV(g[x]);\ |
113 \? y.b[y]=FF; INV(x)|] ==>\ |
113 \? y.b[y]=FF; INV(x)|] ==>\ |
114 \~iterate(k,step[b][g],x)=UU --> INV(iterate(k,step[b][g],x))" |
114 \~iterate(k,step[b][g],x)=UU --> INV(iterate(k,step[b][g],x))" |
115 (fn prems => |
115 (fn prems => |
116 [ |
116 [ |
132 (asm_simp_tac (HOLCF_ss addsimps [iterate_Suc,step_def2] ) 1), |
132 (asm_simp_tac (HOLCF_ss addsimps [iterate_Suc,step_def2] ) 1), |
133 (asm_simp_tac (HOLCF_ss addsimps [loop_lemma2] ) 1) |
133 (asm_simp_tac (HOLCF_ss addsimps [loop_lemma2] ) 1) |
134 ]); |
134 ]); |
135 |
135 |
136 |
136 |
137 qed_goal "loop_lemma4" Loop.thy |
137 val loop_lemma4 = prove_goal Loop.thy |
138 "!x. b[iterate(k,step[b][g],x)]=FF --> while[b][g][x]=iterate(k,step[b][g],x)" |
138 "!x. b[iterate(k,step[b][g],x)]=FF --> while[b][g][x]=iterate(k,step[b][g],x)" |
139 (fn prems => |
139 (fn prems => |
140 [ |
140 [ |
141 (nat_ind_tac "k" 1), |
141 (nat_ind_tac "k" 1), |
142 (simp_tac iterate_ss 1), |
142 (simp_tac iterate_ss 1), |
149 (rtac trans 1), |
149 (rtac trans 1), |
150 (rtac while_unfold3 1), |
150 (rtac while_unfold3 1), |
151 (asm_simp_tac HOLCF_ss 1) |
151 (asm_simp_tac HOLCF_ss 1) |
152 ]); |
152 ]); |
153 |
153 |
154 qed_goal "loop_lemma5" Loop.thy |
154 val loop_lemma5 = prove_goal Loop.thy |
155 "!k. ~b[iterate(k,step[b][g],x)]=FF ==>\ |
155 "!k. ~b[iterate(k,step[b][g],x)]=FF ==>\ |
156 \ !m. while[b][g][iterate(m,step[b][g],x)]=UU" |
156 \ !m. while[b][g][iterate(m,step[b][g],x)]=UU" |
157 (fn prems => |
157 (fn prems => |
158 [ |
158 [ |
159 (cut_facts_tac prems 1), |
159 (cut_facts_tac prems 1), |
177 (dtac spec 1), |
177 (dtac spec 1), |
178 (contr_tac 1) |
178 (contr_tac 1) |
179 ]); |
179 ]); |
180 |
180 |
181 |
181 |
182 qed_goal "loop_lemma6" Loop.thy |
182 val loop_lemma6 = prove_goal Loop.thy |
183 "!k. ~b[iterate(k,step[b][g],x)]=FF ==> while[b][g][x]=UU" |
183 "!k. ~b[iterate(k,step[b][g],x)]=FF ==> while[b][g][x]=UU" |
184 (fn prems => |
184 (fn prems => |
185 [ |
185 [ |
186 (res_inst_tac [("t","x")] (iterate_0 RS subst) 1), |
186 (res_inst_tac [("t","x")] (iterate_0 RS subst) 1), |
187 (rtac (loop_lemma5 RS spec) 1), |
187 (rtac (loop_lemma5 RS spec) 1), |
188 (resolve_tac prems 1) |
188 (resolve_tac prems 1) |
189 ]); |
189 ]); |
190 |
190 |
191 qed_goal "loop_lemma7" Loop.thy |
191 val loop_lemma7 = prove_goal Loop.thy |
192 "~while[b][g][x]=UU ==> ? k. b[iterate(k,step[b][g],x)]=FF" |
192 "~while[b][g][x]=UU ==> ? k. b[iterate(k,step[b][g],x)]=FF" |
193 (fn prems => |
193 (fn prems => |
194 [ |
194 [ |
195 (cut_facts_tac prems 1), |
195 (cut_facts_tac prems 1), |
196 (etac swap 1), |
196 (etac swap 1), |
197 (rtac loop_lemma6 1), |
197 (rtac loop_lemma6 1), |
198 (fast_tac HOL_cs 1) |
198 (fast_tac HOL_cs 1) |
199 ]); |
199 ]); |
200 |
200 |
201 qed_goal "loop_lemma8" Loop.thy |
201 val loop_lemma8 = prove_goal Loop.thy |
202 "~while[b][g][x]=UU ==> ? y. b[y]=FF" |
202 "~while[b][g][x]=UU ==> ? y. b[y]=FF" |
203 (fn prems => |
203 (fn prems => |
204 [ |
204 [ |
205 (cut_facts_tac prems 1), |
205 (cut_facts_tac prems 1), |
206 (dtac loop_lemma7 1), |
206 (dtac loop_lemma7 1), |
210 |
210 |
211 (* --------------------------------------------------------------------------- *) |
211 (* --------------------------------------------------------------------------- *) |
212 (* an invariant rule for loops *) |
212 (* an invariant rule for loops *) |
213 (* --------------------------------------------------------------------------- *) |
213 (* --------------------------------------------------------------------------- *) |
214 |
214 |
215 qed_goal "loop_inv2" Loop.thy |
215 val loop_inv2 = prove_goal Loop.thy |
216 "[| (!y. INV(y) & b[y]=TT & ~g[y]=UU --> INV(g[y]));\ |
216 "[| (!y. INV(y) & b[y]=TT & ~g[y]=UU --> INV(g[y]));\ |
217 \ (!y. INV(y) & b[y]=FF --> Q(y));\ |
217 \ (!y. INV(y) & b[y]=FF --> Q(y));\ |
218 \ INV(x); ~while[b][g][x]=UU |] ==> Q(while[b][g][x])" |
218 \ INV(x); ~while[b][g][x]=UU |] ==> Q(while[b][g][x])" |
219 (fn prems => |
219 (fn prems => |
220 [ |
220 [ |
237 (rtac (loop_lemma4 RS spec RS mp RS sym) 1), |
237 (rtac (loop_lemma4 RS spec RS mp RS sym) 1), |
238 (atac 1), |
238 (atac 1), |
239 (rtac refl 1) |
239 (rtac refl 1) |
240 ]); |
240 ]); |
241 |
241 |
242 qed_goal "loop_inv3" Loop.thy |
242 val loop_inv3 = prove_goal Loop.thy |
243 "[| !!y.[| INV(y); b[y]=TT; ~g[y]=UU|] ==> INV(g[y]);\ |
243 "[| !!y.[| INV(y); b[y]=TT; ~g[y]=UU|] ==> INV(g[y]);\ |
244 \ !!y.[| INV(y); b[y]=FF|]==> Q(y);\ |
244 \ !!y.[| INV(y); b[y]=FF|]==> Q(y);\ |
245 \ INV(x); ~while[b][g][x]=UU |] ==> Q(while[b][g][x])" |
245 \ INV(x); ~while[b][g][x]=UU |] ==> Q(while[b][g][x])" |
246 (fn prems => |
246 (fn prems => |
247 [ |
247 [ |
259 (fast_tac HOL_cs 1), |
259 (fast_tac HOL_cs 1), |
260 (resolve_tac prems 1), |
260 (resolve_tac prems 1), |
261 (resolve_tac prems 1) |
261 (resolve_tac prems 1) |
262 ]); |
262 ]); |
263 |
263 |
264 qed_goal "loop_inv" Loop.thy |
264 val loop_inv = prove_goal Loop.thy |
265 "[| P(x);\ |
265 "[| P(x);\ |
266 \ !!y.P(y) ==> INV(y);\ |
266 \ !!y.P(y) ==> INV(y);\ |
267 \ !!y.[| INV(y); b[y]=TT; ~g[y]=UU|] ==> INV(g[y]);\ |
267 \ !!y.[| INV(y); b[y]=TT; ~g[y]=UU|] ==> INV(g[y]);\ |
268 \ !!y.[| INV(y); b[y]=FF|]==> Q(y);\ |
268 \ !!y.[| INV(y); b[y]=FF|]==> Q(y);\ |
269 \ ~while[b][g][x]=UU |] ==> Q(while[b][g][x])" |
269 \ ~while[b][g][x]=UU |] ==> Q(while[b][g][x])" |