|
1 header {* |
|
2 {\em abacus} a kind of register machine |
|
3 *} |
|
4 |
|
5 theory abacus |
|
6 imports Main "../Separation_Algebra/Sep_Tactics" |
|
7 begin |
|
8 |
|
9 instantiation set :: (type)sep_algebra |
|
10 begin |
|
11 |
|
12 definition set_zero_def: "0 = {}" |
|
13 |
|
14 definition plus_set_def: "s1 + s2 = s1 \<union> s2" |
|
15 |
|
16 definition sep_disj_set_def: "sep_disj s1 s2 = (s1 \<inter> s2 = {})" |
|
17 |
|
18 lemmas set_ins_def = sep_disj_set_def plus_set_def set_zero_def |
|
19 |
|
20 instance |
|
21 apply(default) |
|
22 apply(simp add:set_ins_def) |
|
23 apply(simp add:sep_disj_set_def plus_set_def set_zero_def) |
|
24 apply (metis inf_commute) |
|
25 apply(simp add:sep_disj_set_def plus_set_def set_zero_def) |
|
26 apply(simp add:sep_disj_set_def plus_set_def set_zero_def) |
|
27 apply (metis sup_commute) |
|
28 apply(simp add:sep_disj_set_def plus_set_def set_zero_def) |
|
29 apply (metis (lifting) Un_assoc) |
|
30 apply(simp add:sep_disj_set_def plus_set_def set_zero_def) |
|
31 apply (metis (lifting) Int_Un_distrib Un_empty inf_sup_distrib1 sup_eq_bot_iff) |
|
32 apply(simp add:sep_disj_set_def plus_set_def set_zero_def) |
|
33 by (metis (lifting) Int_Un_distrib Int_Un_distrib2 sup_eq_bot_iff) |
|
34 end |
|
35 |
|
36 |
|
37 text {* |
|
38 {\em Abacus} instructions: |
|
39 *} |
|
40 |
|
41 datatype abc_inst = |
|
42 -- {* @{text "Inc n"} increments the memory cell (or register) |
|
43 with address @{text "n"} by one. |
|
44 *} |
|
45 Inc nat |
|
46 -- {* |
|
47 @{text "Dec n label"} decrements the memory cell with address @{text "n"} by one. |
|
48 If cell @{text "n"} is already zero, no decrements happens and the executio jumps to |
|
49 the instruction labeled by @{text "label"}. |
|
50 *} |
|
51 | Dec nat nat |
|
52 -- {* |
|
53 @{text "Goto label"} unconditionally jumps to the instruction labeled by @{text "label"}. |
|
54 *} |
|
55 | Goto nat |
|
56 |
|
57 datatype apg = |
|
58 Instr abc_inst |
|
59 | Label nat |
|
60 | Seq apg apg |
|
61 | Local "(nat \<Rightarrow> apg)" |
|
62 |
|
63 notation Local (binder "L " 10) |
|
64 |
|
65 abbreviation prog_instr :: "abc_inst \<Rightarrow> apg" ("\<guillemotright>_" [61] 61) |
|
66 where "\<guillemotright>i \<equiv> Instr i" |
|
67 |
|
68 abbreviation prog_seq :: "apg \<Rightarrow> apg \<Rightarrow> apg" (infixr ";" 52) |
|
69 where "c1 ; c2 \<equiv> Seq c1 c2" |
|
70 |
|
71 type_synonym aconf = "((nat \<rightharpoonup> abc_inst) \<times> nat \<times> (nat \<rightharpoonup> nat) \<times> nat)" |
|
72 |
|
73 fun astep :: "aconf \<Rightarrow> aconf" |
|
74 where "astep (prog, pc, m, faults) = |
|
75 (case (prog pc) of |
|
76 Some (Inc i) \<Rightarrow> |
|
77 case m(i) of |
|
78 Some n \<Rightarrow> (prog, pc + 1, m(i:= Some (n + 1)), faults) |
|
79 | None \<Rightarrow> (prog, pc, m, faults + 1) |
|
80 | Some (Dec i e) \<Rightarrow> |
|
81 case m(i) of |
|
82 Some n \<Rightarrow> if (n = 0) then (prog, e, m, faults) |
|
83 else (prog, pc + 1, m(i:= Some (n - 1)), faults) |
|
84 | None \<Rightarrow> (prog, pc, m, faults + 1) |
|
85 | Some (Goto pc') \<Rightarrow> (prog, pc', m, faults) |
|
86 | None \<Rightarrow> (prog, pc, m, faults + 1))" |
|
87 |
|
88 definition "run n = astep ^^ n" |
|
89 |
|
90 datatype aresource = |
|
91 M nat nat |
|
92 | C nat abc_inst |
|
93 | At nat |
|
94 | Faults nat |
|
95 |
|
96 definition "prog_set prog = {C i inst | i inst. prog i = Some inst}" |
|
97 definition "pc_set pc = {At pc}" |
|
98 definition "mem_set m = {M i n | i n. m (i) = Some n} " |
|
99 definition "faults_set faults = {Faults faults}" |
|
100 |
|
101 lemmas cpn_set_def = prog_set_def pc_set_def mem_set_def faults_set_def |
|
102 |
|
103 fun rset_of :: "aconf \<Rightarrow> aresource set" |
|
104 where "rset_of (prog, pc, m, faults) = |
|
105 prog_set prog \<union> pc_set pc \<union> mem_set m \<union> faults_set faults" |
|
106 |
|
107 definition "sg e = (\<lambda> s. s = e)" |
|
108 |
|
109 definition "pc l = sg (pc_set l)" |
|
110 |
|
111 definition "m a v =sg ({M a v})" |
|
112 |
|
113 declare rset_of.simps[simp del] |
|
114 |
|
115 type_synonym assert = "aresource set \<Rightarrow> bool" |
|
116 |
|
117 primrec assemble_to :: "apg \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> assert" |
|
118 where |
|
119 "assemble_to (Instr ai) i j = (sg ({C i ai}) ** \<langle>(j = i + 1)\<rangle>)" | |
|
120 "assemble_to (Seq p1 p2) i j = (EXS j'. (assemble_to p1 i j') ** (assemble_to p2 j' j))" | |
|
121 "assemble_to (Local fp) i j = (EXS l. (assemble_to (fp l) i j))" | |
|
122 "assemble_to (Label l) i j = \<langle>(i = j \<and> j = l)\<rangle>" |
|
123 |
|
124 abbreviation asmb_to :: "nat \<Rightarrow> apg \<Rightarrow> nat \<Rightarrow> assert" ("_ :[ _ ]: _" [60, 60, 60] 60) |
|
125 where "i :[ apg ]: j \<equiv> assemble_to apg i j" |
|
126 |
|
127 lemma stimes_sgD: "(sg x ** q) s \<Longrightarrow> q (s - x) \<and> x \<subseteq> s" |
|
128 apply(erule_tac sep_conjE) |
|
129 apply(unfold set_ins_def sg_def) |
|
130 by (metis Diff_Int2 Diff_Int_distrib2 Diff_Un Diff_cancel |
|
131 Diff_empty Diff_idemp Diff_triv Int_Diff Un_Diff |
|
132 Un_Diff_cancel inf_commute inf_idem sup_bot_right sup_commute sup_ge2) |
|
133 |
|
134 lemma pcD: "(pc i ** r) (rset_of (prog, i', mem, fault)) |
|
135 \<Longrightarrow> i' = i" |
|
136 proof - |
|
137 assume "(pc i ** r) (rset_of (prog, i', mem, fault))" |
|
138 from stimes_sgD [OF this[unfolded pc_def], unfolded rset_of.simps] |
|
139 have "pc_set i \<subseteq> prog_set prog \<union> pc_set i' \<union> mem_set mem \<union> faults_set fault" by auto |
|
140 thus ?thesis |
|
141 by (unfold cpn_set_def, auto) |
|
142 qed |
|
143 |
|
144 lemma codeD: "(pc i ** sg {C i inst} ** r) (rset_of (prog, pos, mem, fault)) |
|
145 \<Longrightarrow> prog pos = Some inst" |
|
146 proof - |
|
147 assume "(pc i ** sg {C i inst} ** r) (rset_of (prog, pos, mem, fault))" |
|
148 thus ?thesis |
|
149 apply(sep_subst pcD) |
|
150 apply(unfold sep_conj_def set_ins_def sg_def rset_of.simps cpn_set_def) |
|
151 by auto |
|
152 qed |
|
153 |
|
154 lemma memD: "((m a v) ** r) (rset_of (prog, pos, mem, fault)) \<Longrightarrow> mem a = Some v" |
|
155 proof - |
|
156 assume "((m a v) ** r) (rset_of (prog, pos, mem, fault))" |
|
157 from stimes_sgD[OF this[unfolded rset_of.simps cpn_set_def m_def]] |
|
158 have "{M a v} \<subseteq> {C i inst |i inst. prog i = Some inst} \<union> |
|
159 {At pos} \<union> {M i n |i n. mem i = Some n} \<union> {Faults fault}" by auto |
|
160 thus ?thesis by auto |
|
161 qed |
|
162 |
|
163 definition |
|
164 Hoare_abc :: "assert \<Rightarrow> assert \<Rightarrow> assert \<Rightarrow> bool" ("({(1_)}/ (_)/ {(1_)})" 50) |
|
165 where |
|
166 "{p} c {q} \<equiv> (\<forall> s r. (p**c**r) (rset_of s) \<longrightarrow> |
|
167 (\<exists> k. ((q ** c ** r) (rset_of (run k s)))))" |
|
168 |
|
169 definition "dec_fun v j e = (if (v = 0) then (e, v) else (j, v - 1))" |
|
170 |
|
171 lemma disj_Diff: "a \<inter> b = {} \<Longrightarrow> a \<union> b - b = a" |
|
172 by (metis (lifting) Diff_cancel Un_Diff Un_Diff_Int) |
|
173 |
|
174 lemma diff_pc_set: "prog_set aa \<union> pc_set i \<union> mem_set ab \<union> faults_set b - pc_set i = |
|
175 prog_set aa \<union> mem_set ab \<union> faults_set b" (is "?L = ?R") |
|
176 proof - |
|
177 have "?L = (prog_set aa \<union> mem_set ab \<union> faults_set b \<union> pc_set i) - pc_set i" |
|
178 by auto |
|
179 also have "\<dots> = ?R" |
|
180 proof(rule disj_Diff) |
|
181 show " (prog_set aa \<union> mem_set ab \<union> faults_set b) \<inter> pc_set i = {}" |
|
182 by (unfold cpn_set_def, auto) |
|
183 qed |
|
184 finally show ?thesis . |
|
185 qed |
|
186 |
|
187 lemma M_in_simp: "({M a v} \<subseteq> prog_set x \<union> pc_set y \<union> mem_set mem \<union> faults_set f) = |
|
188 ({M a v} \<subseteq> mem_set mem)" |
|
189 by (unfold cpn_set_def, auto) |
|
190 |
|
191 lemma mem_set_upd: |
|
192 "{M a v} \<subseteq> mem_set mem \<Longrightarrow> mem_set (mem(a:=Some v')) = ((mem_set mem) - {M a v}) \<union> {M a v'}" |
|
193 by (unfold cpn_set_def, auto) |
|
194 |
|
195 lemma mem_set_disj: "{M a v} \<subseteq> mem_set mem \<Longrightarrow> {M a v'} \<inter> (mem_set mem - {M a v}) = {}" |
|
196 by (unfold cpn_set_def, auto) |
|
197 |
|
198 lemma smem_upd: "((m a v) ** r) (rset_of (x, y, mem, f)) \<Longrightarrow> |
|
199 ((m a v') ** r) (rset_of (x, y, mem(a := Some v'), f))" |
|
200 proof - |
|
201 have eq_s:" |
|
202 (prog_set x \<union> pc_set y \<union> mem_set mem \<union> faults_set f - {M a v}) = |
|
203 (prog_set x \<union> pc_set y \<union> (mem_set mem - {M a v}) \<union> faults_set f)" |
|
204 by (unfold cpn_set_def, auto) |
|
205 assume "(m a v \<and>* r) (rset_of (x, y, mem, f))" |
|
206 from this[unfolded rset_of.simps m_def] |
|
207 have h: "(sg {M a v} \<and>* r) (prog_set x \<union> pc_set y \<union> mem_set mem \<union> faults_set f)" . |
|
208 hence h0: "r (prog_set x \<union> pc_set y \<union> (mem_set mem - {M a v}) \<union> faults_set f)" |
|
209 by(sep_drule stimes_sgD, clarify, unfold eq_s, auto) |
|
210 from h M_in_simp have "{M a v} \<subseteq> mem_set mem" |
|
211 by(sep_drule stimes_sgD, auto) |
|
212 from mem_set_upd [OF this] mem_set_disj[OF this] |
|
213 have h2: "mem_set (mem(a \<mapsto> v')) = {M a v'} \<union> (mem_set mem - {M a v})" |
|
214 "{M a v'} \<inter> (mem_set mem - {M a v}) = {}" by auto |
|
215 show ?thesis |
|
216 proof - |
|
217 have "(m a v' ** r) (mem_set (mem(a \<mapsto> v')) \<union> prog_set x \<union> pc_set y \<union> faults_set f)" |
|
218 proof(rule sep_conjI) |
|
219 from h0 show "r (prog_set x \<union> pc_set y \<union> (mem_set mem - {M a v}) \<union> faults_set f)" . |
|
220 next |
|
221 show "m a v' ({M a v'})" by (unfold m_def sg_def, simp) |
|
222 next |
|
223 show "mem_set (mem(a \<mapsto> v')) \<union> prog_set x \<union> pc_set y \<union> faults_set f = |
|
224 {M a v'} + (prog_set x \<union> pc_set y \<union> (mem_set mem - {M a v}) \<union> faults_set f)" |
|
225 by (unfold h2(1) set_ins_def eq_s, auto) |
|
226 next |
|
227 from h2(2) |
|
228 show " {M a v'} ## prog_set x \<union> pc_set y \<union> (mem_set mem - {M a v}) \<union> faults_set f" |
|
229 by (unfold cpn_set_def set_ins_def, auto) |
|
230 qed |
|
231 thus ?thesis |
|
232 apply (unfold rset_of.simps) |
|
233 by (metis sup_commute sup_left_commute) |
|
234 qed |
|
235 qed |
|
236 |
|
237 lemma pc_dest: "(pc i') (pc_set i) \<Longrightarrow> i = i'" |
|
238 sorry |
|
239 |
|
240 lemma spc_upd: "(pc i' ** r) (rset_of (x, i, y, z)) \<Longrightarrow> |
|
241 (pc i'' ** r) (rset_of (x, i'', y, z))" |
|
242 proof - |
|
243 assume h: "rset_of (x, i, y, z) \<in> pc i' * r" |
|
244 from stimes_sgD [OF h[unfolded rset_of.simps pc_set_def pc_def]] |
|
245 have h1: "prog_set x \<union> {At i} \<union> mem_set y \<union> faults_set z - {At i'} \<in> r" |
|
246 "{At i'} \<subseteq> prog_set x \<union> {At i} \<union> mem_set y \<union> faults_set z" by auto |
|
247 from h1(2) have eq_i: "i' = i" by (unfold cpn_set_def, auto) |
|
248 have "prog_set x \<union> {At i} \<union> mem_set y \<union> faults_set z - {At i'} = |
|
249 prog_set x \<union> mem_set y \<union> faults_set z " |
|
250 apply (unfold eq_i) |
|
251 by (metis (full_types) Un_insert_left Un_insert_right |
|
252 diff_pc_set faults_set_def insert_commute insert_is_Un |
|
253 pc_set_def sup_assoc sup_bot_left sup_commute) |
|
254 with h1(1) have in_r: "prog_set x \<union> mem_set y \<union> faults_set z \<in> r" by auto |
|
255 show ?thesis |
|
256 proof(unfold rset_of.simps, rule stimesI[OF _ _ _ in_r]) |
|
257 show "{At i''} \<in> pc i''" by (unfold pc_def pc_set_def, simp) |
|
258 next |
|
259 show "prog_set x \<union> pc_set i'' \<union> mem_set y \<union> faults_set z = |
|
260 {At i''} \<union> (prog_set x \<union> mem_set y \<union> faults_set z)" |
|
261 by (unfold pc_set_def, auto) |
|
262 next |
|
263 show "{At i''} \<inter> (prog_set x \<union> mem_set y \<union> faults_set z) = {}" |
|
264 by (auto simp:cpn_set_def) |
|
265 qed |
|
266 qed |
|
267 |
|
268 lemma condD: "s \<in> <b>*r \<Longrightarrow> b" |
|
269 by (unfold st_def pasrt_def, auto) |
|
270 |
|
271 lemma condD1: "s \<in> <b>*r \<Longrightarrow> s \<in> r" |
|
272 by (unfold st_def pasrt_def, auto) |
|
273 |
|
274 lemma hoare_dec_suc: "{(pc i * m a v) * <(v > 0)>} |
|
275 i:[\<guillemotright>(Dec a e) ]:j |
|
276 {pc j * m a (v - 1)}" |
|
277 proof(unfold Hoare_abc_def, clarify) |
|
278 fix prog i' ab b r |
|
279 assume h: "rset_of (prog, i', ab, b) \<in> ((pc i * m a v) * <(0 < v)>) * (i :[ \<guillemotright>Dec a e ]: j) * r" |
|
280 (is "?r \<in> ?S") |
|
281 show "\<exists>k. rset_of (run k (prog, i', ab, b)) \<in> (pc j * m a (v - 1)) * (i :[ \<guillemotright>Dec a e ]: j) * r" |
|
282 proof - |
|
283 from h [unfolded assemble_to.simps] |
|
284 have h1: "?r \<in> pc i * {{C i (Dec a e)}} * m a v * <(0 < v)> * <(j = i + 1)> * r" |
|
285 "?r \<in> m a v * pc i * {{C i (Dec a e)}} * <(0 < v)> * <(j = i + 1)> * r" |
|
286 "?r \<in> <(0 < v)> * <(j = i + 1)> * m a v * pc i * {{C i (Dec a e)}} * r" |
|
287 "?r \<in> <(j = i + 1)> * <(0 < v)> * m a v * pc i * {{C i (Dec a e)}} * r" |
|
288 by ((metis stimes_ac)+) |
|
289 note h2 = condD [OF h1(3)] condD[OF h1(4)] pcD[OF h1(1)] codeD[OF h1(1)] memD[OF h1(2)] |
|
290 hence stp: "run 1 (prog, i', ab, b) = (prog, Suc i, ab(a \<mapsto> v - Suc 0), b)" (is "?x = ?y") |
|
291 by (unfold run_def, auto) |
|
292 have "rset_of ?x \<in> (pc j * m a (v - 1)) * (i :[ \<guillemotright>Dec a e ]: j) * r" |
|
293 proof - |
|
294 have "rset_of ?y \<in> (pc j * m a (v - 1)) * (i :[ \<guillemotright>Dec a e ]: j) * r" |
|
295 proof - |
|
296 from spc_upd[OF h1(1), of "Suc i"] |
|
297 have "rset_of (prog, (Suc i), ab, b) \<in> |
|
298 m a v * pc (Suc i) * {{C i (Dec a e)}} * <(0 < v)> * <(j = i + 1)> * r" |
|
299 by (metis stimes_ac) |
|
300 from smem_upd[OF this, of "v - (Suc 0)"] |
|
301 have "rset_of ?y \<in> |
|
302 m a (v - Suc 0) * pc (Suc i) * {{C i (Dec a e)}} * <(0 < v)> * <(j = i + 1)> * r" . |
|
303 hence "rset_of ?y \<in> <(0 < v)> * |
|
304 (pc (Suc i) * m a (v - Suc 0)) * ({{C i (Dec a e)}} * <(j = i + 1)>) * r" |
|
305 by (metis stimes_ac) |
|
306 from condD1[OF this] |
|
307 have "rset_of ?y \<in> (pc (Suc i) * m a (v - Suc 0)) * ({{C i (Dec a e)}} * <(j = i + 1)>) * r" . |
|
308 thus ?thesis |
|
309 by (unfold h2(2) assemble_to.simps, simp) |
|
310 qed |
|
311 with stp show ?thesis by simp |
|
312 qed |
|
313 thus ?thesis by blast |
|
314 qed |
|
315 qed |
|
316 |
|
317 lemma hoare_dec_fail: "{pc i * m a 0} |
|
318 i:[ \<guillemotright>(Dec a e) ]:j |
|
319 {pc e * m a 0}" |
|
320 proof(unfold Hoare_abc_def, clarify) |
|
321 fix prog i' ab b r |
|
322 assume h: "rset_of (prog, i', ab, b) \<in> (pc i * m a 0) * (i :[ \<guillemotright>Dec a e ]: j) * r" |
|
323 (is "?r \<in> ?S") |
|
324 show "\<exists>k. rset_of (run k (prog, i', ab, b)) \<in> (pc e * m a 0) * (i :[ \<guillemotright>Dec a e ]: j) * r" |
|
325 proof - |
|
326 from h [unfolded assemble_to.simps] |
|
327 have h1: "?r \<in> pc i * {{C i (Dec a e)}} * m a 0 * <(j = i + 1)> * r" |
|
328 "?r \<in> m a 0 * pc i * {{C i (Dec a e)}} * <(j = i + 1)> * r" |
|
329 "?r \<in> <(j = i + 1)> * m a 0 * pc i * {{C i (Dec a e)}} * r" |
|
330 by ((metis stimes_ac)+) |
|
331 note h2 = condD [OF h1(3)] pcD[OF h1(1)] codeD[OF h1(1)] memD[OF h1(2)] |
|
332 hence stp: "run 1 (prog, i', ab, b) = (prog, e, ab, b)" (is "?x = ?y") |
|
333 by (unfold run_def, auto) |
|
334 have "rset_of ?x \<in> (pc e * m a 0) * (i :[ \<guillemotright>Dec a e ]: j) * r" |
|
335 proof - |
|
336 have "rset_of ?y \<in> (pc e * m a 0) * (i :[ \<guillemotright>Dec a e ]: j) * r" |
|
337 proof - |
|
338 from spc_upd[OF h1(1), of "e"] |
|
339 have "rset_of ?y \<in> pc e * {{C i (Dec a e)}} * m a 0 * <(j = i + 1)> * r" . |
|
340 thus ?thesis |
|
341 by (unfold assemble_to.simps, metis stimes_ac) |
|
342 qed |
|
343 with stp show ?thesis by simp |
|
344 qed |
|
345 thus ?thesis by blast |
|
346 qed |
|
347 qed |
|
348 |
|
349 lemma pasrtD_p: "\<lbrakk>{p*<b>} c {q}\<rbrakk> \<Longrightarrow> (b \<longrightarrow> {p} c {q})" |
|
350 apply (unfold Hoare_abc_def pasrt_def, auto) |
|
351 by (fold emp_def, simp add:emp_unit_r) |
|
352 |
|
353 lemma hoare_dec: "dec_fun v j e = (pc', v') \<Longrightarrow> |
|
354 {pc i * m a v} |
|
355 i:[ \<guillemotright>(Dec a e) ]:j |
|
356 {pc pc' * m a v'}" |
|
357 proof - |
|
358 assume "dec_fun v j e = (pc', v')" |
|
359 thus "{pc i * m a v} |
|
360 i:[ \<guillemotright>(Dec a e) ]:j |
|
361 {pc pc' * m a v'}" |
|
362 apply (auto split:if_splits simp:dec_fun_def) |
|
363 apply (insert hoare_dec_fail, auto)[1] |
|
364 apply (insert hoare_dec_suc, auto) |
|
365 apply (atomize) |
|
366 apply (erule_tac x = i in allE, erule_tac x = a in allE, |
|
367 erule_tac x = v in allE, erule_tac x = e in allE, erule_tac x = pc' in allE) |
|
368 by (drule_tac pasrtD_p, clarify) |
|
369 qed |
|
370 |
|
371 lemma hoare_inc: "{pc i * m a v} |
|
372 i:[ \<guillemotright>(Inc a) ]:j |
|
373 {pc j * m a (v + 1)}" |
|
374 proof(unfold Hoare_abc_def, clarify) |
|
375 fix prog i' ab b r |
|
376 assume h: "rset_of (prog, i', ab, b) \<in> (pc i * m a v) * (i :[ \<guillemotright>Inc a ]: j) * r" |
|
377 (is "?r \<in> ?S") |
|
378 show "\<exists>k. rset_of (run k (prog, i', ab, b)) \<in> (pc j * m a (v + 1)) * (i :[ \<guillemotright>Inc a ]: j) * r" |
|
379 proof - |
|
380 from h [unfolded assemble_to.simps] |
|
381 have h1: "?r \<in> pc i * {{C i (Inc a)}} * m a v * <(j = i + 1)> * r" |
|
382 "?r \<in> m a v * pc i * {{C i (Inc a)}} * <(j = i + 1)> * r" |
|
383 "?r \<in> <(j = i + 1)> * m a v * pc i * {{C i (Inc a)}} * r" |
|
384 by ((metis stimes_ac)+) |
|
385 note h2 = condD [OF h1(3)] pcD[OF h1(1)] codeD[OF h1(1)] memD[OF h1(2)] |
|
386 hence stp: "run 1 (prog, i', ab, b) = (prog, Suc i, ab(a \<mapsto> Suc v), b)" (is "?x = ?y") |
|
387 by (unfold run_def, auto) |
|
388 have "rset_of ?x \<in> (pc j * m a (v + 1)) * (i :[ \<guillemotright>Inc a]: j) * r" |
|
389 proof - |
|
390 have "rset_of ?y \<in> (pc j * m a (v + 1)) * (i :[ \<guillemotright>Inc a ]: j) * r" |
|
391 proof - |
|
392 from spc_upd[OF h1(1), of "Suc i"] |
|
393 have "rset_of (prog, (Suc i), ab, b) \<in> |
|
394 m a v * pc (Suc i) * {{C i (Inc a)}} * <(j = i + 1)> * r" |
|
395 by (metis stimes_ac) |
|
396 from smem_upd[OF this, of "Suc v"] |
|
397 have "rset_of ?y \<in> |
|
398 m a (v + 1) * pc (i + 1) * {{C i (Inc a)}} * <(j = i + 1)> * r" by simp |
|
399 thus ?thesis |
|
400 by (unfold h2(1) assemble_to.simps, metis stimes_ac) |
|
401 qed |
|
402 with stp show ?thesis by simp |
|
403 qed |
|
404 thus ?thesis by blast |
|
405 qed |
|
406 qed |
|
407 |
|
408 lemma hoare_goto: "{pc i} |
|
409 i:[ \<guillemotright>(Goto e) ]:j |
|
410 {pc e}" |
|
411 proof(unfold Hoare_abc_def, clarify) |
|
412 fix prog i' ab b r |
|
413 assume h: "rset_of (prog, i', ab, b) \<in> pc i * (i :[ \<guillemotright>Goto e ]: j) * r" |
|
414 (is "?r \<in> ?S") |
|
415 show "\<exists>k. rset_of (run k (prog, i', ab, b)) \<in> pc e * (i :[ \<guillemotright>Goto e ]: j) * r" |
|
416 proof - |
|
417 from h [unfolded assemble_to.simps] |
|
418 have h1: "?r \<in> pc i * {{C i (Goto e)}} * <(j = i + 1)> * r" |
|
419 by ((metis stimes_ac)+) |
|
420 note h2 = pcD[OF h1(1)] codeD[OF h1(1)] |
|
421 hence stp: "run 1 (prog, i', ab, b) = (prog, e, ab, b)" (is "?x = ?y") |
|
422 by (unfold run_def, auto) |
|
423 have "rset_of ?x \<in> pc e * (i :[ \<guillemotright>Goto e]: j) * r" |
|
424 proof - |
|
425 from spc_upd[OF h1(1), of "e"] |
|
426 show ?thesis |
|
427 by (unfold stp assemble_to.simps, metis stimes_ac) |
|
428 qed |
|
429 thus ?thesis by blast |
|
430 qed |
|
431 qed |
|
432 |
|
433 no_notation stimes (infixr "*" 70) |
|
434 |
|
435 interpretation foo: comm_monoid_mult |
|
436 "stimes :: 'a set set => 'a set set => 'a set set" "emp::'a set set" |
|
437 apply(default) |
|
438 apply(simp add: stimes_assoc) |
|
439 apply(simp add: stimes_comm) |
|
440 apply(simp add: emp_def[symmetric]) |
|
441 done |
|
442 |
|
443 |
|
444 notation stimes (infixr "*" 70) |
|
445 |
|
446 (*used by simplifier for numbers *) |
|
447 thm mult_cancel_left |
|
448 |
|
449 (* |
|
450 interpretation foo: comm_ring_1 "op * :: 'a set set => 'a set set => 'a set set" "{{}}::'a set set" |
|
451 apply(default) |
|
452 *) |
|
453 |
|
454 lemma frame: "{p} c {q} \<Longrightarrow> \<forall> r. {p * r} c {q * r}" |
|
455 apply (unfold Hoare_abc_def, clarify) |
|
456 apply (erule_tac x = "(a, aa, ab, b)" in allE) |
|
457 apply (erule_tac x = "r * ra" in allE) |
|
458 apply(metis stimes_ac) |
|
459 done |
|
460 |
|
461 lemma code_extension: "\<lbrakk>{p} c {q}\<rbrakk> \<Longrightarrow> (\<forall> e. {p} c * e {q})" |
|
462 apply (unfold Hoare_abc_def, clarify) |
|
463 apply (erule_tac x = "(a, aa, ab, b)" in allE) |
|
464 apply (erule_tac x = "e * r" in allE) |
|
465 apply(metis stimes_ac) |
|
466 done |
|
467 |
|
468 lemma run_add: "run (n1 + n2) s = run n1 (run n2 s)" |
|
469 apply (unfold run_def) |
|
470 by (metis funpow_add o_apply) |
|
471 |
|
472 lemma composition: "\<lbrakk>{p} c1 {q}; {q} c2 {r}\<rbrakk> \<Longrightarrow> {p} c1 * c2 {r}" |
|
473 proof - |
|
474 assume h: "{p} c1 {q}" "{q} c2 {r}" |
|
475 from code_extension [OF h(1), rule_format, of "c2"] |
|
476 have "{p} c1 * c2 {q}" . |
|
477 moreover from code_extension [OF h(2), rule_format, of "c1"] and stimes_comm |
|
478 have "{q} c1 * c2 {r}" by metis |
|
479 ultimately show "{p} c1 * c2 {r}" |
|
480 apply (unfold Hoare_abc_def, clarify) |
|
481 proof - |
|
482 fix a aa ab b ra |
|
483 assume h1: "\<forall>s r. rset_of s \<in> p * (c1 * c2) * r \<longrightarrow> |
|
484 (\<exists>k. rset_of (run k s) \<in> q * (c1 * c2) * r)" |
|
485 and h2: "\<forall>s ra. rset_of s \<in> q * (c1 * c2) * ra \<longrightarrow> |
|
486 (\<exists>k. rset_of (run k s) \<in> r * (c1 * c2) * ra)" |
|
487 and h3: "rset_of (a, aa, ab, b) \<in> p * (c1 * c2) * ra" |
|
488 show "\<exists>k. rset_of (run k (a, aa, ab, b)) \<in> r * (c1 * c2) * ra" |
|
489 proof - |
|
490 let ?s = "(a, aa, ab, b)" |
|
491 from h1 [rule_format, of ?s, OF h3] |
|
492 obtain n1 where "rset_of (run n1 ?s) \<in> q * (c1 * c2) * ra" by blast |
|
493 from h2 [rule_format, OF this] |
|
494 obtain n2 where "rset_of (run n2 (run n1 ?s)) \<in> r * (c1 * c2) * ra" by blast |
|
495 with run_add show ?thesis by metis |
|
496 qed |
|
497 qed |
|
498 qed |
|
499 |
|
500 lemma stimes_simp: "s \<in> x * y = (\<exists> s1 s2. (s = s1 \<union> s2 \<and> s1 \<inter> s2 = {} \<and> s1 \<in> x \<and> s2 \<in> y))" |
|
501 by (metis (lifting) stimesE stimesI) |
|
502 |
|
503 lemma hoare_seq: |
|
504 "\<lbrakk>\<forall> i j. {pc i * p} i:[c1]:j {pc j * q}; |
|
505 \<forall> j k. {pc j * q} j:[c2]:k {pc k * r}\<rbrakk> \<Longrightarrow> {pc i * p} i:[(c1 ; c2)]:k {pc k *r}" |
|
506 proof - |
|
507 assume h: "\<forall>i j. {pc i * p} i :[ c1 ]: j {pc j * q}" "\<forall> j k. {pc j * q} j:[c2]:k {pc k * r}" |
|
508 show "{pc i * p} i:[(c1 ; c2)]:k {pc k *r}" |
|
509 proof(subst Hoare_abc_def, clarify) |
|
510 fix a aa ab b ra |
|
511 assume "rset_of (a, aa, ab, b) \<in> (pc i * p) * (i :[ (c1 ; c2) ]: k) * ra" |
|
512 hence "rset_of (a, aa, ab, b) \<in> (i :[ (c1 ; c2) ]: k) * (pc i * p * ra)" (is "?s \<in> ?X * ?Y") |
|
513 by (metis stimes_ac) |
|
514 from stimesE[OF this] obtain s1 s2 where |
|
515 sp: "rset_of(a, aa, ab, b) = s1 \<union> s2" "s1 \<inter> s2 = {}" "s1 \<in> ?X" "s2 \<in> ?Y" by blast |
|
516 from sp (3) obtain j' where |
|
517 "s1 \<in> (i:[c1]:j') * (j':[c2]:k)" (is "s1 \<in> ?Z") |
|
518 by (auto simp:assemble_to.simps) |
|
519 from stimesI[OF sp(1, 2) this sp(4)] |
|
520 have "?s \<in> (pc i * p) * (i :[ c1 ]: j') * (j' :[ c2 ]: k) * ra" by (metis stimes_ac) |
|
521 from h(1)[unfolded Hoare_abc_def, rule_format, OF this] |
|
522 obtain ka where |
|
523 "rset_of (run ka (a, aa, ab, b)) \<in> (pc j' * q) * (j' :[ c2 ]: k) * ((i :[ c1 ]: j') * ra)" |
|
524 sorry |
|
525 from h(2)[unfolded Hoare_abc_def, rule_format, OF this] |
|
526 obtain kb where |
|
527 "rset_of (run kb (run ka (a, aa, ab, b))) |
|
528 \<in> (pc k * r) * (j' :[ c2 ]: k) * (i :[ c1 ]: j') * ra" by blast |
|
529 hence h3: "rset_of (run (kb + ka) (a, aa, ab, b)) |
|
530 \<in> pc k * r * (j' :[ c2 ]: k) * ((i :[ c1 ]: j') * ra)" |
|
531 sorry |
|
532 hence "rset_of (run (kb + ka) (a, aa, ab, b)) \<in> pc k * r * (i :[ (c1 ; c2) ]: k) * ra" |
|
533 proof - |
|
534 have "rset_of (run (kb + ka) (a, aa, ab, b)) \<in> (i :[ (c1 ; c2) ]: k) * (pc k * r * ra)" |
|
535 proof - |
|
536 from h3 have "rset_of (run (kb + ka) (a, aa, ab, b)) |
|
537 \<in> ((j' :[ c2 ]: k) * ((i :[ c1 ]: j'))) * (pc k * r * ra)" |
|
538 by (metis stimes_ac) |
|
539 then obtain |
|
540 s1 s2 where h4: "rset_of (run (kb + ka) (a, aa, ab, b)) = s1 \<union> s2" |
|
541 " s1 \<inter> s2 = {}" "s1 \<in> (j' :[ c2 ]: k) * (i :[ c1 ]: j')" |
|
542 "s2 \<in> pc k * r * ra" by (rule stimesE, blast) |
|
543 from h4(3) have "s1 \<in> (i :[ (c1 ; c2) ]: k)" |
|
544 sorry |
|
545 from stimesI [OF h4(1, 2) this h4(4)] |
|
546 show ?thesis . |
|
547 qed |
|
548 thus ?thesis by (metis stimes_ac) |
|
549 qed |
|
550 thus "\<exists>ka. rset_of (run ka (a, aa, ab, b)) \<in> (pc k * r) * (i :[ (c1 ; c2) ]: k) * ra" |
|
551 by (metis stimes_ac) |
|
552 qed |
|
553 qed |
|
554 |
|
555 lemma hoare_local: |
|
556 "\<forall> l i j. {pc i*p} i:[c(l)]:j {pc j * q} |
|
557 \<Longrightarrow> {pc i * p} i:[Local c]:j {pc j * q}" |
|
558 proof - |
|
559 assume h: "\<forall> l i j. {pc i*p} i:[c(l)]:j {pc j * q} " |
|
560 show "{pc i * p} i:[Local c]:j {pc j * q}" |
|
561 proof(unfold assemble_to.simps Hoare_abc_def, clarify) |
|
562 fix a aa ab b r |
|
563 assume h1: "rset_of (a, aa, ab, b) \<in> (pc i * p) * (\<Union>l. i :[ c l ]: j) * r" |
|
564 hence "rset_of (a, aa, ab, b) \<in> (\<Union>l. i :[ c l ]: j) * (pc i * p * r)" |
|
565 by (metis stimes_ac) |
|
566 then obtain s1 s2 l |
|
567 where "rset_of (a, aa, ab, b) = s1 \<union> s2" |
|
568 "s1 \<inter> s2 = {}" |
|
569 "s1 \<in> (i :[ c l ]: j)" |
|
570 "s2 \<in> pc i * p * r" |
|
571 by (rule stimesE, auto) |
|
572 from stimesI[OF this] |
|
573 have "rset_of (a, aa, ab, b) \<in> (pc i * p) * (i :[ c l ]: j) * r" |
|
574 by (metis stimes_ac) |
|
575 from h[unfolded Hoare_abc_def, rule_format, OF this] |
|
576 obtain k where "rset_of (run k (a, aa, ab, b)) \<in> (i :[ c l ]: j) * (pc j * q * r)" |
|
577 sorry |
|
578 then obtain s1 s2 |
|
579 where h3: "rset_of (run k (a, aa, ab, b)) = s1 \<union> s2" |
|
580 " s1 \<inter> s2 = {}" "s1 \<in> (\<Union> l. (i :[ c l ]: j))" "s2 \<in> pc j * q * r" |
|
581 by(rule stimesE, auto) |
|
582 from stimesI[OF this] |
|
583 show "\<exists>k. rset_of (run k (a, aa, ab, b)) \<in> (pc j * q) * (\<Union>l. i :[ c l ]: j) * r" |
|
584 by (metis stimes_ac) |
|
585 qed |
|
586 qed |
|
587 |
|
588 lemma move_pure: "{p*<b>} c {q} = (b \<longrightarrow> {p} c {q})" |
|
589 proof(unfold Hoare_abc_def, default, clarify) |
|
590 fix prog i' mem ft r |
|
591 assume h: "\<forall>s r. rset_of s \<in> (p * <b>) * c * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
592 "b" "rset_of (prog, i', mem, ft) \<in> p * c * r" |
|
593 show "\<exists>k. rset_of (run k (prog, i', mem, ft)) \<in> q * c * r" |
|
594 proof(rule h(1)[rule_format]) |
|
595 have "(p * <b>) * c * r = <b> * p * c * r" by (metis stimes_ac) |
|
596 moreover have "rset_of (prog, i', mem, ft) \<in> \<dots>" |
|
597 proof(rule stimesI[OF _ _ _ h(3)]) |
|
598 from h(2) show "{} \<in> <b>" by (auto simp:pasrt_def) |
|
599 qed auto |
|
600 ultimately show "rset_of (prog, i', mem, ft) \<in> (p * <b>) * c * r" |
|
601 by (simp) |
|
602 qed |
|
603 next |
|
604 assume h: "b \<longrightarrow> (\<forall>s r. rset_of s \<in> p * c * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * c * r))" |
|
605 show "\<forall>s r. rset_of s \<in> (p * <b>) * c * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
606 proof - |
|
607 { fix s r |
|
608 assume "rset_of s \<in> (p * <b>) * c * r" |
|
609 hence h1: "rset_of s \<in> <b> * p * c * r" by (metis stimes_ac) |
|
610 have "(\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
611 proof(rule h[rule_format]) |
|
612 from condD[OF h1] show b . |
|
613 next |
|
614 from condD1[OF h1] show "rset_of s \<in> p * c * r" . |
|
615 qed |
|
616 } thus ?thesis by blast |
|
617 qed |
|
618 qed |
|
619 |
|
620 lemma precond_ex: "{\<Union> x. p x} c {q} = (\<forall> x. {p x} c {q})" |
|
621 proof(unfold Hoare_abc_def, default, clarify) |
|
622 fix x prog i' mem ft r |
|
623 assume h: "\<forall>s r. rset_of s \<in> UNION UNIV p * c * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
624 "rset_of (prog, i', mem, ft) \<in> p x * c * r" |
|
625 show "\<exists>k. rset_of (run k (prog, i', mem, ft)) \<in> q * c * r" |
|
626 proof(rule h[rule_format]) |
|
627 from h(2) show "rset_of (prog, i', mem, ft) \<in> UNION UNIV p * c * r" by (auto simp:stimes_def) |
|
628 qed |
|
629 next |
|
630 assume h: "\<forall>x s r. rset_of s \<in> p x * c * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
631 show "\<forall>s r. rset_of s \<in> UNION UNIV p * c * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
632 proof - |
|
633 { fix s r |
|
634 assume "rset_of s \<in> UNION UNIV p * c * r" |
|
635 then obtain x where "rset_of s \<in> p x * c * r" |
|
636 by (unfold st_def, auto, metis) |
|
637 hence "(\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
638 by(rule h[rule_format]) |
|
639 } thus ?thesis by blast |
|
640 qed |
|
641 qed |
|
642 |
|
643 lemma code_exI: "\<lbrakk>\<And>l. {p} c l * c' {q}\<rbrakk> \<Longrightarrow> {p} (\<Union> l. c l) * c' {q}" |
|
644 proof(unfold Hoare_abc_def, default, clarify) |
|
645 fix prog i' mem ft r |
|
646 assume h: "\<And>l. \<forall>s r. rset_of s \<in> p * (c l * c') * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * (c l * c') * r)" |
|
647 "rset_of (prog, i', mem, ft) \<in> p * (UNION UNIV c * c') * r" |
|
648 show " \<exists>k. rset_of (run k (prog, i', mem, ft)) \<in> q * (UNION UNIV c * c') * r" |
|
649 proof - |
|
650 from h(2) obtain l where "rset_of (prog, i', mem, ft) \<in> p * (c l * c') * r" |
|
651 apply (unfold st_def, auto) |
|
652 by metis |
|
653 from h(1)[rule_format, OF this] |
|
654 obtain k where " rset_of (run k (prog, i', mem, ft)) \<in> q * (c l * c') * r" by blast |
|
655 thus ?thesis by (unfold st_def, auto, metis) |
|
656 qed |
|
657 qed |
|
658 |
|
659 lemma code_exIe: "\<lbrakk>\<And>l. {p} c l{q}\<rbrakk> \<Longrightarrow> {p} \<Union> l. (c l) {q}" |
|
660 proof - |
|
661 assume "\<And>l. {p} c l {q}" |
|
662 thus "{p} \<Union>l. c l {q}" |
|
663 by(rule code_exI[where c'= "emp", unfolded emp_unit_r]) |
|
664 qed |
|
665 |
|
666 lemma pre_stren: "\<lbrakk>{p} c {q}; r \<subseteq> p\<rbrakk> \<Longrightarrow> {r} c {q}" |
|
667 proof(unfold Hoare_abc_def, clarify) |
|
668 fix prog i' mem ft r' |
|
669 assume h: "\<forall>s r. rset_of s \<in> p * c * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
670 " r \<subseteq> p" " rset_of (prog, i', mem, ft) \<in> r * c * r'" |
|
671 show "\<exists>k. rset_of (run k (prog, i', mem, ft)) \<in> q * c * r'" |
|
672 proof(rule h(1)[rule_format]) |
|
673 from stimes_mono[OF h(2), of "c * r'"] h(3) |
|
674 show "rset_of (prog, i', mem, ft) \<in> p * c * r'" by auto |
|
675 qed |
|
676 qed |
|
677 |
|
678 lemma post_weaken: "\<lbrakk>{p} c {q}; q \<subseteq> r\<rbrakk> \<Longrightarrow> {p} c {r}" |
|
679 proof(unfold Hoare_abc_def, clarify) |
|
680 fix prog i' mem ft r' |
|
681 assume h: "\<forall>s r. rset_of s \<in> p * c * r \<longrightarrow> (\<exists>k. rset_of (run k s) \<in> q * c * r)" |
|
682 " q \<subseteq> r" "rset_of (prog, i', mem, ft) \<in> p * c * r'" |
|
683 show "\<exists>k. rset_of (run k (prog, i', mem, ft)) \<in> r * c * r'" |
|
684 proof - |
|
685 from h(1)[rule_format, OF h(3)] |
|
686 obtain k where "rset_of (run k (prog, i', mem, ft)) \<in> q * c * r'" by auto |
|
687 moreover from h(2) have "\<dots> \<subseteq> r * c * r'" by (metis stimes_mono) |
|
688 ultimately show ?thesis by auto |
|
689 qed |
|
690 qed |
|
691 |
|
692 definition "clear a = (L start exit. Label start; \<guillemotright>Dec a exit; \<guillemotright> Goto start; Label exit)" |
|
693 |
|
694 lemma "{pc i * m a v} i:[clear a]:j {pc j*m a 0}" |
|
695 proof (unfold clear_def, rule hoare_local, default+) |
|
696 fix l i j |
|
697 show "{pc i * m a v} i :[ (L exit. Label l ; \<guillemotright>Dec a exit ; \<guillemotright>Goto l ; Label exit) ]: j |
|
698 {pc j * m a 0}" |
|
699 proof(rule hoare_local, default+) |
|
700 fix la i j |
|
701 show "{pc i * m a v} i :[ (Label l ; \<guillemotright>Dec a la ; \<guillemotright>Goto l ; Label la) ]: j {pc j * m a 0}" |
|
702 proof(subst assemble_to.simps, rule code_exIe) |
|
703 have "\<And>j'. {pc i * m a v} (j' :[ (\<guillemotright>Dec a la ; \<guillemotright>Goto l ; Label la) ]: j) * (i :[ Label l ]: j') |
|
704 {pc j * m a 0}" |
|
705 proof(subst assemble_to.simps, rule code_exI) |
|
706 fix j' j'a |
|
707 show "{pc i * m a v} |
|
708 ((j' :[ \<guillemotright>Dec a la ]: j'a) * (j'a :[ (\<guillemotright>Goto l ; Label la) ]: j)) * (i :[ Label l ]: j') |
|
709 {pc j * m a 0}" |
|
710 proof(unfold assemble_to.simps) |
|
711 have "{pc i * m a v} |
|
712 ((\<Union>j'. ({{C j'a (Goto l)}} * <(j' = j'a + 1)>) * ({{C j' (Dec a la)}} * <(j'a = j' + 1)>) |
|
713 * <(j' = j \<and> j = la)>)) * |
|
714 <(i = j' \<and> j' = l)> |
|
715 {pc j * m a 0}" |
|
716 proof(rule code_exI, fold assemble_to.simps, unfold assemble_to.simps(4)) |
|
717 thm assemble_to.simps |
|
718 qed |
|
719 thus "{pc i * m a v} |
|
720 (({{C j' (Dec a la)}} * <(j'a = j' + 1)>) * |
|
721 (\<Union>j'. ({{C j'a (Goto l)}} * <(j' = j'a + 1)>) * <(j' = j \<and> j = la)>)) * |
|
722 <(i = j' \<and> j' = l)> |
|
723 {pc j * m a 0}" sorry |
|
724 qed |
|
725 qed |
|
726 thus "\<And>j'. {pc i * m a v} (i :[ Label l ]: j') * (j' :[ (\<guillemotright>Dec a la ; \<guillemotright>Goto l ; Label la) ]: j) |
|
727 {pc j * m a 0}" by (metis stimes_ac) |
|
728 qed |
|
729 qed |
|
730 qed |
|
731 |
|
732 end |