|
1 header {* |
|
2 Separation logic for TM |
|
3 *} |
|
4 |
|
5 theory Hoare_tm |
|
6 imports Hoare_gen My_block Data_slot |
|
7 begin |
|
8 |
|
9 |
|
10 ML {* |
|
11 fun pretty_terms ctxt trms = |
|
12 Pretty.block (Pretty.commas (map (Syntax.pretty_term ctxt) trms)) |
|
13 *} |
|
14 |
|
15 lemma int_add_C :"x + (y::int) = y + x" |
|
16 by simp |
|
17 |
|
18 lemma int_add_A : "(x + y) + z = x + (y + (z::int))" |
|
19 by simp |
|
20 |
|
21 lemma int_add_LC: "x + (y + (z::int)) = y + (x + z)" |
|
22 by simp |
|
23 |
|
24 lemmas int_add_ac = int_add_A int_add_C int_add_LC |
|
25 |
|
26 method_setup prune = {* Scan.succeed (SIMPLE_METHOD' o (K (K prune_params_tac))) *} |
|
27 "pruning parameters" |
|
28 |
|
29 ML {* |
|
30 structure StepRules = Named_Thms |
|
31 (val name = @{binding "step"} |
|
32 val description = "Theorems for hoare rules for every step") |
|
33 *} |
|
34 |
|
35 ML {* |
|
36 structure FwdRules = Named_Thms |
|
37 (val name = @{binding "fwd"} |
|
38 val description = "Theorems for fwd derivation of seperation implication") |
|
39 *} |
|
40 |
|
41 setup {* StepRules.setup *} |
|
42 |
|
43 setup {* FwdRules.setup *} |
|
44 |
|
45 section {* Operational Semantics of TM *} |
|
46 |
|
47 datatype taction = W0 | W1 | L | R |
|
48 |
|
49 type_synonym tstate = nat |
|
50 |
|
51 type_synonym tm_inst = "(taction \<times> tstate) \<times> (taction \<times> tstate)" |
|
52 |
|
53 datatype Block = Oc | Bk |
|
54 |
|
55 type_synonym tconf = "nat \<times> (nat \<rightharpoonup> tm_inst) \<times> nat \<times> int \<times> (int \<rightharpoonup> Block)" |
|
56 |
|
57 fun next_tape :: "taction \<Rightarrow> (int \<times> (int \<rightharpoonup> Block)) \<Rightarrow> (int \<times> (int \<rightharpoonup> Block))" |
|
58 where "next_tape W0 (pos, m) = (pos, m(pos \<mapsto> Bk))" | |
|
59 "next_tape W1 (pos, m) = (pos, m(pos \<mapsto> Oc))" | |
|
60 "next_tape L (pos, m) = (pos - 1, m)" | |
|
61 "next_tape R (pos, m) = (pos + 1, m)" |
|
62 |
|
63 fun tstep :: "tconf \<Rightarrow> tconf" |
|
64 where "tstep (faults, prog, pc, pos, m) = |
|
65 (case (prog pc) of |
|
66 Some ((action0, pc0), (action1, pc1)) \<Rightarrow> |
|
67 case (m pos) of |
|
68 Some Bk \<Rightarrow> (faults, prog, pc0, next_tape action0 (pos, m)) | |
|
69 Some Oc \<Rightarrow> (faults, prog, pc1, next_tape action1 (pos, m)) | |
|
70 None \<Rightarrow> (faults + 1, prog, pc, pos, m) |
|
71 | None \<Rightarrow> (faults + 1, prog, pc, pos, m))" |
|
72 |
|
73 lemma tstep_splits: |
|
74 "(P (tstep s)) = ((\<forall> faults prog pc pos m action0 pc0 action1 pc1. |
|
75 s = (faults, prog, pc, pos, m) \<longrightarrow> |
|
76 prog pc = Some ((action0, pc0), (action1, pc1)) \<longrightarrow> |
|
77 m pos = Some Bk \<longrightarrow> P (faults, prog, pc0, next_tape action0 (pos, m))) \<and> |
|
78 (\<forall> faults prog pc pos m action0 pc0 action1 pc1. |
|
79 s = (faults, prog, pc, pos, m) \<longrightarrow> |
|
80 prog pc = Some ((action0, pc0), (action1, pc1)) \<longrightarrow> |
|
81 m pos = Some Oc \<longrightarrow> P (faults, prog, pc1, next_tape action1 (pos, m))) \<and> |
|
82 (\<forall> faults prog pc pos m action0 pc0 action1 pc1. |
|
83 s = (faults, prog, pc, pos, m) \<longrightarrow> |
|
84 prog pc = Some ((action0, pc0), (action1, pc1)) \<longrightarrow> |
|
85 m pos = None \<longrightarrow> P (faults + 1, prog, pc, pos, m)) \<and> |
|
86 (\<forall> faults prog pc pos m . |
|
87 s = (faults, prog, pc, pos, m) \<longrightarrow> |
|
88 prog pc = None \<longrightarrow> P (faults + 1, prog, pc, pos, m)) |
|
89 )" |
|
90 by (case_tac s, auto split:option.splits Block.splits) |
|
91 |
|
92 datatype tresource = |
|
93 TM int Block |
|
94 | TC nat tm_inst |
|
95 | TAt nat |
|
96 | TPos int |
|
97 | TFaults nat |
|
98 |
|
99 definition "tprog_set prog = {TC i inst | i inst. prog i = Some inst}" |
|
100 definition "tpc_set pc = {TAt pc}" |
|
101 definition "tmem_set m = {TM i n | i n. m (i) = Some n}" |
|
102 definition "tpos_set pos = {TPos pos}" |
|
103 definition "tfaults_set faults = {TFaults faults}" |
|
104 |
|
105 lemmas tpn_set_def = tprog_set_def tpc_set_def tmem_set_def tfaults_set_def tpos_set_def |
|
106 |
|
107 fun trset_of :: "tconf \<Rightarrow> tresource set" |
|
108 where "trset_of (faults, prog, pc, pos, m) = |
|
109 tprog_set prog \<union> tpc_set pc \<union> tpos_set pos \<union> tmem_set m \<union> tfaults_set faults" |
|
110 |
|
111 interpretation tm: sep_exec tstep trset_of . |
|
112 |
|
113 section {* Assembly language and its program logic *} |
|
114 |
|
115 subsection {* The assembly language *} |
|
116 |
|
117 datatype tpg = |
|
118 TInstr tm_inst |
|
119 | TLabel nat |
|
120 | TSeq tpg tpg |
|
121 | TLocal "(nat \<Rightarrow> tpg)" |
|
122 |
|
123 notation TLocal (binder "TL " 10) |
|
124 |
|
125 abbreviation tprog_instr :: "tm_inst \<Rightarrow> tpg" ("\<guillemotright> _" [61] 61) |
|
126 where "\<guillemotright> i \<equiv> TInstr i" |
|
127 |
|
128 abbreviation tprog_seq :: "tpg \<Rightarrow> tpg \<Rightarrow> tpg" (infixr ";" 52) |
|
129 where "c1 ; c2 \<equiv> TSeq c1 c2" |
|
130 |
|
131 definition "sg e = (\<lambda> s. s = e)" |
|
132 |
|
133 type_synonym tassert = "tresource set \<Rightarrow> bool" |
|
134 |
|
135 abbreviation t_hoare :: |
|
136 "tassert \<Rightarrow> tassert \<Rightarrow> tassert \<Rightarrow> bool" ("(\<lbrace>(1_)\<rbrace> / (_)/ \<lbrace>(1_)\<rbrace>)" 50) |
|
137 where "\<lbrace> p \<rbrace> c \<lbrace> q \<rbrace> == sep_exec.Hoare_gen tstep trset_of p c q" |
|
138 |
|
139 abbreviation it_hoare :: |
|
140 "(('a::sep_algebra) \<Rightarrow> tresource set \<Rightarrow> bool) \<Rightarrow> |
|
141 ('a \<Rightarrow> bool) \<Rightarrow> (tresource set \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" |
|
142 ("(1_).(\<lbrace>(1_)\<rbrace> / (_)/ \<lbrace>(1_)\<rbrace>)" 50) |
|
143 where "I. \<lbrace>P\<rbrace> c \<lbrace>Q\<rbrace> == sep_exec.IHoare tstep trset_of I P c Q" |
|
144 |
|
145 (* |
|
146 primrec tpg_len :: "tpg \<Rightarrow> nat" where |
|
147 "tpg_len (TInstr ai) = 1" | |
|
148 "tpg_len (TSeq p1 p2) = tpg_len p1 + tpg_len " | |
|
149 "tpg_len (TLocal fp) = tpg_len (fp 0)" | |
|
150 "tpg_len (TLabel l) = 0" *) |
|
151 |
|
152 primrec tassemble_to :: "tpg \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> tassert" |
|
153 where |
|
154 "tassemble_to (TInstr ai) i j = (sg ({TC i ai}) ** <(j = i + 1)>)" | |
|
155 "tassemble_to (TSeq p1 p2) i j = (EXS j'. (tassemble_to p1 i j') ** (tassemble_to p2 j' j))" | |
|
156 "tassemble_to (TLocal fp) i j = (EXS l. (tassemble_to (fp l) i j))" | |
|
157 "tassemble_to (TLabel l) i j = <(i = j \<and> j = l)>" |
|
158 |
|
159 declare tassemble_to.simps [simp del] |
|
160 |
|
161 lemmas tasmp = tassemble_to.simps (2, 3, 4) |
|
162 |
|
163 abbreviation tasmb_to :: "nat \<Rightarrow> tpg \<Rightarrow> nat \<Rightarrow> tassert" ("_ :[ _ ]: _" [60, 60, 60] 60) |
|
164 where "i :[ tpg ]: j \<equiv> tassemble_to tpg i j" |
|
165 |
|
166 lemma EXS_intro: |
|
167 assumes h: "(P x) s" |
|
168 shows "((EXS x. P(x))) s" |
|
169 by (smt h pred_ex_def sep_conj_impl) |
|
170 |
|
171 lemma EXS_elim: |
|
172 assumes "(EXS x. P x) s" |
|
173 obtains x where "P x s" |
|
174 by (metis assms pred_ex_def) |
|
175 |
|
176 lemma EXS_eq: |
|
177 fixes x |
|
178 assumes h: "Q (p x)" |
|
179 and h1: "\<And> y s. \<lbrakk>p y s\<rbrakk> \<Longrightarrow> y = x" |
|
180 shows "p x = (EXS x. p x)" |
|
181 proof |
|
182 fix s |
|
183 show "p x s = (EXS x. p x) s" |
|
184 proof |
|
185 assume "p x s" |
|
186 thus "(EXS x. p x) s" by (auto simp:pred_ex_def) |
|
187 next |
|
188 assume "(EXS x. p x) s" |
|
189 thus "p x s" |
|
190 proof(rule EXS_elim) |
|
191 fix y |
|
192 assume "p y s" |
|
193 from this[unfolded h1[OF this]] show "p x s" . |
|
194 qed |
|
195 qed |
|
196 qed |
|
197 |
|
198 definition "tpg_fold tpgs = foldr TSeq (butlast tpgs) (last tpgs)" |
|
199 |
|
200 lemma tpg_fold_sg: "tpg_fold [tpg] = tpg" |
|
201 by (simp add:tpg_fold_def) |
|
202 |
|
203 lemma tpg_fold_cons: |
|
204 assumes h: "tpgs \<noteq> []" |
|
205 shows "tpg_fold (tpg#tpgs) = (tpg; (tpg_fold tpgs))" |
|
206 using h |
|
207 proof(induct tpgs arbitrary:tpg) |
|
208 case (Cons tpg1 tpgs1) |
|
209 thus ?case |
|
210 proof(cases "tpgs1 = []") |
|
211 case True |
|
212 thus ?thesis by (simp add:tpg_fold_def) |
|
213 next |
|
214 case False |
|
215 show ?thesis |
|
216 proof - |
|
217 have eq_1: "butlast (tpg # tpg1 # tpgs1) = tpg # (butlast (tpg1 # tpgs1))" |
|
218 by simp |
|
219 from False have eq_2: "last (tpg # tpg1 # tpgs1) = last (tpg1 # tpgs1)" |
|
220 by simp |
|
221 have eq_3: "foldr (op ;) (tpg # butlast (tpg1 # tpgs1)) (last (tpg1 # tpgs1)) = |
|
222 (tpg ; (foldr (op ;) (butlast (tpg1 # tpgs1)) (last (tpg1 # tpgs1))))" |
|
223 by simp |
|
224 show ?thesis |
|
225 apply (subst (1) tpg_fold_def, unfold eq_1 eq_2 eq_3) |
|
226 by (fold tpg_fold_def, simp) |
|
227 qed |
|
228 qed |
|
229 qed auto |
|
230 |
|
231 lemmas tpg_fold_simps = tpg_fold_sg tpg_fold_cons |
|
232 |
|
233 lemma tpg_fold_app: |
|
234 assumes h1: "tpgs1 \<noteq> []" |
|
235 and h2: "tpgs2 \<noteq> []" |
|
236 shows "i:[(tpg_fold (tpgs1 @ tpgs2))]:j = i:[(tpg_fold (tpgs1); tpg_fold tpgs2)]:j" |
|
237 using h1 h2 |
|
238 proof(induct tpgs1 arbitrary: i j tpgs2) |
|
239 case (Cons tpg1' tpgs1') |
|
240 thus ?case (is "?L = ?R") |
|
241 proof(cases "tpgs1' = []") |
|
242 case False |
|
243 from h2 have "(tpgs1' @ tpgs2) \<noteq> []" |
|
244 by (metis Cons.prems(2) Nil_is_append_conv) |
|
245 have "?L = (i :[ tpg_fold (tpg1' # (tpgs1' @ tpgs2)) ]: j )" by simp |
|
246 also have "\<dots> = (i:[(tpg1'; (tpg_fold (tpgs1' @ tpgs2)))]:j )" |
|
247 by (simp add:tpg_fold_cons[OF `(tpgs1' @ tpgs2) \<noteq> []`]) |
|
248 also have "\<dots> = ?R" |
|
249 proof - |
|
250 have "(EXS j'. i :[ tpg1' ]: j' \<and>* j' :[ tpg_fold (tpgs1' @ tpgs2) ]: j) = |
|
251 (EXS j'. (EXS j'a. i :[ tpg1' ]: j'a \<and>* j'a :[ tpg_fold tpgs1' ]: j') \<and>* |
|
252 j' :[ tpg_fold tpgs2 ]: j)" |
|
253 proof(default+) |
|
254 fix s |
|
255 assume "(EXS j'. i :[ tpg1' ]: j' \<and>* j' :[ tpg_fold (tpgs1' @ tpgs2) ]: j) s" |
|
256 thus "(EXS j'. (EXS j'a. i :[ tpg1' ]: j'a \<and>* j'a :[ tpg_fold tpgs1' ]: j') \<and>* |
|
257 j' :[ tpg_fold tpgs2 ]: j) s" |
|
258 proof(elim EXS_elim) |
|
259 fix j' |
|
260 assume "(i :[ tpg1' ]: j' \<and>* j' :[ tpg_fold (tpgs1' @ tpgs2) ]: j) s" |
|
261 from this[unfolded Cons(1)[OF False Cons(3)] tassemble_to.simps] |
|
262 show "(EXS j'. (EXS j'a. i :[ tpg1' ]: j'a \<and>* j'a :[ tpg_fold tpgs1' ]: j') \<and>* |
|
263 j' :[ tpg_fold tpgs2 ]: j) s" |
|
264 proof(elim sep_conjE EXS_elim) |
|
265 fix x y j'a xa ya |
|
266 assume h: "(i :[ tpg1' ]: j') x" |
|
267 "x ## y" "s = x + y" |
|
268 "(j' :[ tpg_fold tpgs1' ]: j'a) xa" |
|
269 "(j'a :[ tpg_fold tpgs2 ]: j) ya" |
|
270 " xa ## ya" "y = xa + ya" |
|
271 show "(EXS j'. (EXS j'a. i :[ tpg1' ]: j'a \<and>* |
|
272 j'a :[ tpg_fold tpgs1' ]: j') \<and>* j' :[ tpg_fold tpgs2 ]: j) s" |
|
273 (is "(EXS j'. (?P j' \<and>* ?Q j')) s") |
|
274 proof(rule EXS_intro[where x = "j'a"]) |
|
275 from `(j'a :[ tpg_fold tpgs2 ]: j) ya` have "(?Q j'a) ya" . |
|
276 moreover have "(?P j'a) (x + xa)" |
|
277 proof(rule EXS_intro[where x = j']) |
|
278 have "x + xa = x + xa" by simp |
|
279 moreover from `x ## y` `y = xa + ya` `xa ## ya` |
|
280 have "x ## xa" by (metis sep_disj_addD) |
|
281 moreover note `(i :[ tpg1' ]: j') x` `(j' :[ tpg_fold tpgs1' ]: j'a) xa` |
|
282 ultimately show "(i :[ tpg1' ]: j' \<and>* j' :[ tpg_fold tpgs1' ]: j'a) (x + xa)" |
|
283 by (auto intro!:sep_conjI) |
|
284 qed |
|
285 moreover from `x##y` `y = xa + ya` `xa ## ya` |
|
286 have "(x + xa) ## ya" |
|
287 by (metis sep_disj_addI1 sep_disj_commuteI) |
|
288 moreover from `s = x + y` `y = xa + ya` |
|
289 have "s = (x + xa) + ya" |
|
290 by (metis h(2) h(6) sep_add_assoc sep_disj_addD1 sep_disj_addD2) |
|
291 ultimately show "(?P j'a \<and>* ?Q j'a) s" |
|
292 by (auto intro!:sep_conjI) |
|
293 qed |
|
294 qed |
|
295 qed |
|
296 next |
|
297 fix s |
|
298 assume "(EXS j'. (EXS j'a. i :[ tpg1' ]: j'a \<and>* j'a :[ tpg_fold tpgs1' ]: j') \<and>* |
|
299 j' :[ tpg_fold tpgs2 ]: j) s" |
|
300 thus "(EXS j'. i :[ tpg1' ]: j' \<and>* j' :[ tpg_fold (tpgs1' @ tpgs2) ]: j) s" |
|
301 proof(elim EXS_elim sep_conjE) |
|
302 fix j' x y j'a xa ya |
|
303 assume h: "(j' :[ tpg_fold tpgs2 ]: j) y" |
|
304 "x ## y" "s = x + y" "(i :[ tpg1' ]: j'a) xa" |
|
305 "(j'a :[ tpg_fold tpgs1' ]: j') ya" "xa ## ya" "x = xa + ya" |
|
306 show "(EXS j'. i :[ tpg1' ]: j' \<and>* j' :[ tpg_fold (tpgs1' @ tpgs2) ]: j) s" |
|
307 proof(rule EXS_intro[where x = j'a]) |
|
308 from `(i :[ tpg1' ]: j'a) xa` have "(i :[ tpg1' ]: j'a) xa" . |
|
309 moreover have "(j'a :[ tpg_fold (tpgs1' @ tpgs2) ]: j) (ya + y)" |
|
310 proof(unfold Cons(1)[OF False Cons(3)] tassemble_to.simps) |
|
311 show "(EXS j'. j'a :[ tpg_fold tpgs1' ]: j' \<and>* j' :[ tpg_fold tpgs2 ]: j) (ya + y)" |
|
312 proof(rule EXS_intro[where x = "j'"]) |
|
313 from `x ## y` `x = xa + ya` `xa ## ya` |
|
314 have "ya ## y" by (metis sep_add_disjD) |
|
315 moreover have "ya + y = ya + y" by simp |
|
316 moreover note `(j'a :[ tpg_fold tpgs1' ]: j') ya` |
|
317 `(j' :[ tpg_fold tpgs2 ]: j) y` |
|
318 ultimately show "(j'a :[ tpg_fold tpgs1' ]: j' \<and>* |
|
319 j' :[ tpg_fold tpgs2 ]: j) (ya + y)" |
|
320 by (auto intro!:sep_conjI) |
|
321 qed |
|
322 qed |
|
323 moreover from `s = x + y` `x = xa + ya` |
|
324 have "s = xa + (ya + y)" |
|
325 by (metis h(2) h(6) sep_add_assoc sep_add_disjD) |
|
326 moreover from `xa ## ya` `x ## y` `x = xa + ya` |
|
327 have "xa ## (ya + y)" by (metis sep_disj_addI3) |
|
328 ultimately show "(i :[ tpg1' ]: j'a \<and>* j'a :[ tpg_fold (tpgs1' @ tpgs2) ]: j) s" |
|
329 by (auto intro!:sep_conjI) |
|
330 qed |
|
331 qed |
|
332 qed |
|
333 thus ?thesis |
|
334 by (simp add:tassemble_to.simps, unfold tpg_fold_cons[OF False], |
|
335 unfold tassemble_to.simps, simp) |
|
336 qed |
|
337 finally show ?thesis . |
|
338 next |
|
339 case True |
|
340 thus ?thesis |
|
341 by (simp add:tpg_fold_cons[OF Cons(3)] tpg_fold_sg) |
|
342 qed |
|
343 qed auto |
|
344 |
|
345 |
|
346 subsection {* Assertions and program logic for this assembly language *} |
|
347 |
|
348 definition "st l = sg (tpc_set l)" |
|
349 definition "ps p = sg (tpos_set p)" |
|
350 definition "tm a v =sg ({TM a v})" |
|
351 definition "one i = tm i Oc" |
|
352 definition "zero i= tm i Bk" |
|
353 definition "any i = (EXS v. tm i v)" |
|
354 |
|
355 declare trset_of.simps[simp del] |
|
356 |
|
357 lemma stimes_sgD: "(sg x ** q) s \<Longrightarrow> q (s - x) \<and> x \<subseteq> s" |
|
358 apply(erule_tac sep_conjE) |
|
359 apply(unfold set_ins_def sg_def) |
|
360 by (metis Diff_Int2 Diff_Int_distrib2 Diff_Un Diff_cancel |
|
361 Diff_empty Diff_idemp Diff_triv Int_Diff Un_Diff |
|
362 Un_Diff_cancel inf_commute inf_idem sup_bot_right sup_commute sup_ge2) |
|
363 |
|
364 lemma stD: "(st i ** r) (trset_of (ft, prog, i', pos, mem)) |
|
365 \<Longrightarrow> i' = i" |
|
366 proof - |
|
367 assume "(st i ** r) (trset_of (ft, prog, i', pos, mem))" |
|
368 from stimes_sgD [OF this[unfolded st_def], unfolded trset_of.simps] |
|
369 have "tpc_set i \<subseteq> tprog_set prog \<union> tpc_set i' \<union> tpos_set pos \<union> |
|
370 tmem_set mem \<union> tfaults_set ft" by auto |
|
371 thus ?thesis |
|
372 by (unfold tpn_set_def, auto) |
|
373 qed |
|
374 |
|
375 lemma psD: "(ps p ** r) (trset_of (ft, prog, i', pos, mem)) |
|
376 \<Longrightarrow> pos = p" |
|
377 proof - |
|
378 assume "(ps p ** r) (trset_of (ft, prog, i', pos, mem))" |
|
379 from stimes_sgD [OF this[unfolded ps_def], unfolded trset_of.simps] |
|
380 have "tpos_set p \<subseteq> tprog_set prog \<union> tpc_set i' \<union> |
|
381 tpos_set pos \<union> tmem_set mem \<union> tfaults_set ft" |
|
382 by simp |
|
383 thus ?thesis |
|
384 by (unfold tpn_set_def, auto) |
|
385 qed |
|
386 |
|
387 lemma codeD: "(st i ** sg {TC i inst} ** r) (trset_of (ft, prog, i', pos, mem)) |
|
388 \<Longrightarrow> prog i = Some inst" |
|
389 proof - |
|
390 assume "(st i ** sg {TC i inst} ** r) (trset_of (ft, prog, i', pos, mem))" |
|
391 thus ?thesis |
|
392 apply(unfold sep_conj_def set_ins_def sg_def trset_of.simps tpn_set_def) |
|
393 by auto |
|
394 qed |
|
395 |
|
396 lemma memD: "((tm a v) ** r) (trset_of (ft, prog, i, pos, mem)) \<Longrightarrow> mem a = Some v" |
|
397 proof - |
|
398 assume "((tm a v) ** r) (trset_of (ft, prog, i, pos, mem))" |
|
399 from stimes_sgD[OF this[unfolded trset_of.simps tpn_set_def tm_def]] |
|
400 have "{TM a v} \<subseteq> {TC i inst |i inst. prog i = Some inst} \<union> {TAt i} \<union> |
|
401 {TPos pos} \<union> {TM i n |i n. mem i = Some n} \<union> {TFaults ft}" by simp |
|
402 thus ?thesis by auto |
|
403 qed |
|
404 |
|
405 lemma t_hoare_seq: |
|
406 "\<lbrakk>\<And> i j. \<lbrace>st i ** p\<rbrace> i:[c1]:j \<lbrace>st j ** q\<rbrace>; |
|
407 \<And> j k. \<lbrace>st j ** q\<rbrace> j:[c2]:k \<lbrace>st k ** r\<rbrace>\<rbrakk> \<Longrightarrow> \<lbrace>st i ** p\<rbrace> i:[(c1 ; c2)]:k \<lbrace>st k ** r\<rbrace>" |
|
408 proof - |
|
409 assume h: "\<And> i j. \<lbrace>st i ** p\<rbrace> i:[c1]:j \<lbrace>st j ** q\<rbrace>" |
|
410 "\<And> j k. \<lbrace>st j ** q\<rbrace> j:[c2]:k \<lbrace>st k ** r\<rbrace>" |
|
411 show "\<lbrace>st i ** p\<rbrace> i:[(c1 ; c2)]:k \<lbrace>st k ** r\<rbrace>" |
|
412 proof(subst tassemble_to.simps, rule tm.code_exI) |
|
413 fix j' |
|
414 show "\<lbrace>st i \<and>* p\<rbrace> i :[ c1 ]: j' \<and>* j' :[ c2 ]: k \<lbrace>st k \<and>* r\<rbrace>" |
|
415 proof(rule tm.composition) |
|
416 from h(1) show "\<lbrace>st i \<and>* p\<rbrace> i :[ c1 ]: j' \<lbrace> st j' \<and>* q \<rbrace>" by auto |
|
417 next |
|
418 from h(2) show "\<lbrace>st j' \<and>* q\<rbrace> j' :[ c2 ]: k \<lbrace>st k \<and>* r\<rbrace>" by auto |
|
419 qed |
|
420 qed |
|
421 qed |
|
422 |
|
423 lemma t_hoare_seq1: |
|
424 "\<lbrakk>\<And>j'. \<lbrace>st i ** p\<rbrace> i:[c1]:j' \<lbrace>st j' ** q\<rbrace>; |
|
425 \<And>j'. \<lbrace>st j' ** q\<rbrace> j':[c2]:k \<lbrace>st k' ** r\<rbrace>\<rbrakk> \<Longrightarrow> |
|
426 \<lbrace>st i ** p\<rbrace> i:[(c1 ; c2)]:k \<lbrace>st k' ** r\<rbrace>" |
|
427 proof - |
|
428 assume h: "\<And>j'. \<lbrace>st i \<and>* p\<rbrace> i :[ c1 ]: j' \<lbrace>st j' \<and>* q\<rbrace>" |
|
429 "\<And>j'. \<lbrace>st j' \<and>* q\<rbrace> j' :[ c2 ]: k \<lbrace>st k' \<and>* r\<rbrace>" |
|
430 show "\<lbrace>st i \<and>* p\<rbrace> i :[ (c1 ; c2) ]: k \<lbrace>st k' \<and>* r\<rbrace>" |
|
431 proof(subst tassemble_to.simps, rule tm.code_exI) |
|
432 fix j' |
|
433 show "\<lbrace>st i \<and>* p\<rbrace> i :[ c1 ]: j' \<and>* j' :[ c2 ]: k \<lbrace>st k' \<and>* r\<rbrace>" |
|
434 proof(rule tm.composition) |
|
435 from h(1) show "\<lbrace>st i \<and>* p\<rbrace> i :[ c1 ]: j' \<lbrace> st j' \<and>* q \<rbrace>" by auto |
|
436 next |
|
437 from h(2) show " \<lbrace>st j' \<and>* q\<rbrace> j' :[ c2 ]: k \<lbrace>st k' \<and>* r\<rbrace>" by auto |
|
438 qed |
|
439 qed |
|
440 qed |
|
441 |
|
442 lemma t_hoare_seq2: |
|
443 assumes h: "\<And>j. \<lbrace>st i ** p\<rbrace> i:[c1]:j \<lbrace>st k' \<and>* r\<rbrace>" |
|
444 shows "\<lbrace>st i ** p\<rbrace> i:[(c1 ; c2)]:j \<lbrace>st k' ** r\<rbrace>" |
|
445 apply (unfold tassemble_to.simps, rule tm.code_exI) |
|
446 by (rule tm.code_extension, rule h) |
|
447 |
|
448 lemma t_hoare_local: |
|
449 assumes h: "(\<And>l. \<lbrace>st i \<and>* p\<rbrace> i :[ c l ]: j \<lbrace>st k \<and>* q\<rbrace>)" |
|
450 shows "\<lbrace>st i ** p\<rbrace> i:[TLocal c]:j \<lbrace>st k ** q\<rbrace>" using h |
|
451 by (unfold tassemble_to.simps, intro tm.code_exI, simp) |
|
452 |
|
453 lemma t_hoare_label: |
|
454 "(\<And>l. l = i \<Longrightarrow> \<lbrace>st l \<and>* p\<rbrace> l :[ c l ]: j \<lbrace>st k \<and>* q\<rbrace>) \<Longrightarrow> |
|
455 \<lbrace>st i \<and>* p \<rbrace> |
|
456 i:[(TLabel l; c l)]:j |
|
457 \<lbrace>st k \<and>* q\<rbrace>" |
|
458 by (unfold tassemble_to.simps, intro tm.code_exI tm.code_condI, clarify, auto) |
|
459 |
|
460 primrec t_split_cmd :: "tpg \<Rightarrow> (tpg \<times> tpg option)" |
|
461 where "t_split_cmd (\<guillemotright>inst) = (\<guillemotright>inst, None)" | |
|
462 "t_split_cmd (TLabel l) = (TLabel l, None)" | |
|
463 "t_split_cmd (TSeq c1 c2) = (case (t_split_cmd c2) of |
|
464 (c21, Some c22) \<Rightarrow> (TSeq c1 c21, Some c22) | |
|
465 (c21, None) \<Rightarrow> (c1, Some c21))" | |
|
466 "t_split_cmd (TLocal c) = (TLocal c, None)" |
|
467 |
|
468 definition "t_last_cmd tpg = (snd (t_split_cmd tpg))" |
|
469 |
|
470 declare t_last_cmd_def [simp] |
|
471 |
|
472 definition "t_blast_cmd tpg = (fst (t_split_cmd tpg))" |
|
473 |
|
474 declare t_blast_cmd_def [simp] |
|
475 |
|
476 lemma "t_last_cmd (c1; c2; (TLabel l)) = (Some (TLabel l))" |
|
477 by simp |
|
478 |
|
479 lemma "t_blast_cmd (c1; c2; TLabel l) = (c1; c2)" |
|
480 by simp |
|
481 |
|
482 lemma t_split_cmd_eq: |
|
483 assumes "t_split_cmd c = (c1, Some c2)" |
|
484 shows "(i:[c]:j) = (i:[(c1; c2)]:j)" |
|
485 using assms |
|
486 proof(induct c arbitrary: c1 c2 i j) |
|
487 case (TSeq cx cy) |
|
488 from `t_split_cmd (cx ; cy) = (c1, Some c2)` |
|
489 show ?case |
|
490 apply (simp split:prod.splits option.splits) |
|
491 apply (cases cy, auto split:prod.splits option.splits) |
|
492 proof - |
|
493 fix a |
|
494 assume h: "t_split_cmd cy = (a, Some c2)" |
|
495 show "i :[ (cx ; cy) ]: j = i :[ ((cx ; a) ; c2) ]: j" |
|
496 apply (simp only: tassemble_to.simps(2) TSeq(2)[OF h] sep_conj_exists) |
|
497 apply (simp add:sep_conj_ac) |
|
498 by (simp add:pred_ex_def, default, auto) |
|
499 qed |
|
500 qed auto |
|
501 |
|
502 lemma t_hoare_label_last_pre: |
|
503 fixes l |
|
504 assumes h1: "t_split_cmd c = (c', Some (TLabel l))" |
|
505 and h2: "l = j \<Longrightarrow> \<lbrace>p\<rbrace> i:[c']:j \<lbrace>q\<rbrace>" |
|
506 shows "\<lbrace>p\<rbrace> i:[c]:j \<lbrace>q\<rbrace>" |
|
507 by (unfold t_split_cmd_eq[OF h1], |
|
508 simp only:tassemble_to.simps sep_conj_cond, |
|
509 intro tm.code_exI tm.code_condI, insert h2, auto) |
|
510 |
|
511 lemma t_hoare_label_last: |
|
512 fixes l |
|
513 assumes h1: "t_last_cmd c = Some (TLabel l)" |
|
514 and h2: "l = j \<Longrightarrow> \<lbrace>p\<rbrace> i:[t_blast_cmd c]:j \<lbrace>q\<rbrace>" |
|
515 shows "\<lbrace>p\<rbrace> i:[c]:j \<lbrace>q\<rbrace>" |
|
516 proof - |
|
517 have "t_split_cmd c = (t_blast_cmd c, t_last_cmd c)" |
|
518 by simp |
|
519 from t_hoare_label_last_pre[OF this[unfolded h1]] h2 |
|
520 show ?thesis by auto |
|
521 qed |
|
522 |
|
523 subsection {* Basic assertions for TM *} |
|
524 |
|
525 function ones :: "int \<Rightarrow> int \<Rightarrow> tassert" where |
|
526 "ones i j = (if j < i then <(i = j + 1)> else |
|
527 (one i) ** ones (i + 1) j)" |
|
528 by auto |
|
529 termination by (relation "measure(\<lambda> (i::int, j). nat (j - i + 1))") auto |
|
530 |
|
531 lemma ones_base_simp: "j < i \<Longrightarrow> ones i j = <(i = j + 1)>" |
|
532 by simp |
|
533 |
|
534 lemma ones_step_simp: "\<not> j < i \<Longrightarrow> ones i j = ((one i) ** ones (i + 1) j)" |
|
535 by simp |
|
536 |
|
537 lemmas ones_simps = ones_base_simp ones_step_simp |
|
538 |
|
539 declare ones.simps [simp del] ones_simps [simp] |
|
540 |
|
541 lemma ones_induct [case_names Base Step]: |
|
542 fixes P i j |
|
543 assumes h1: "\<And> i j. j < i \<Longrightarrow> P i j (<(i = j + (1::int))>)" |
|
544 and h2: "\<And> i j. \<lbrakk>\<not> j < i; P (i + 1) j (ones (i + 1) j)\<rbrakk> \<Longrightarrow> P i j ((one i) ** ones (i + 1) j)" |
|
545 shows "P i j (ones i j)" |
|
546 proof(induct i j rule:ones.induct) |
|
547 fix i j |
|
548 assume ih: "(\<not> j < i \<Longrightarrow> P (i + 1) j (ones (i + 1) j))" |
|
549 show "P i j (ones i j)" |
|
550 proof(cases "j < i") |
|
551 case True |
|
552 with h1 [OF True] |
|
553 show ?thesis by auto |
|
554 next |
|
555 case False |
|
556 from h2 [OF False] and ih[OF False] |
|
557 have "P i j (one i \<and>* ones (i + 1) j)" by blast |
|
558 with False show ?thesis by auto |
|
559 qed |
|
560 qed |
|
561 |
|
562 function ones' :: "int \<Rightarrow> int \<Rightarrow> tassert" where |
|
563 "ones' i j = (if j < i then <(i = j + 1)> else |
|
564 ones' i (j - 1) ** one j)" |
|
565 by auto |
|
566 termination by (relation "measure(\<lambda> (i::int, j). nat (j - i + 1))") auto |
|
567 |
|
568 lemma ones_rev: "\<not> j < i \<Longrightarrow> (ones i j) = ((ones i (j - 1)) ** one j)" |
|
569 proof(induct i j rule:ones_induct) |
|
570 case Base |
|
571 thus ?case by auto |
|
572 next |
|
573 case (Step i j) |
|
574 show ?case |
|
575 proof(cases "j < i + 1") |
|
576 case True |
|
577 with Step show ?thesis |
|
578 by simp |
|
579 next |
|
580 case False |
|
581 with Step show ?thesis |
|
582 by (auto simp:sep_conj_ac) |
|
583 qed |
|
584 qed |
|
585 |
|
586 lemma ones_rev_induct [case_names Base Step]: |
|
587 fixes P i j |
|
588 assumes h1: "\<And> i j. j < i \<Longrightarrow> P i j (<(i = j + (1::int))>)" |
|
589 and h2: "\<And> i j. \<lbrakk>\<not> j < i; P i (j - 1) (ones i (j - 1))\<rbrakk> \<Longrightarrow> P i j ((ones i (j - 1)) ** one j)" |
|
590 shows "P i j (ones i j)" |
|
591 proof(induct i j rule:ones'.induct) |
|
592 fix i j |
|
593 assume ih: "(\<not> j < i \<Longrightarrow> P i (j - 1) (ones i (j - 1)))" |
|
594 show "P i j (ones i j)" |
|
595 proof(cases "j < i") |
|
596 case True |
|
597 with h1 [OF True] |
|
598 show ?thesis by auto |
|
599 next |
|
600 case False |
|
601 from h2 [OF False] and ih[OF False] |
|
602 have "P i j (ones i (j - 1) \<and>* one j)" by blast |
|
603 with ones_rev and False |
|
604 show ?thesis |
|
605 by simp |
|
606 qed |
|
607 qed |
|
608 |
|
609 function zeros :: "int \<Rightarrow> int \<Rightarrow> tassert" where |
|
610 "zeros i j = (if j < i then <(i = j + 1)> else |
|
611 (zero i) ** zeros (i + 1) j)" |
|
612 by auto |
|
613 termination by (relation "measure(\<lambda> (i::int, j). nat (j - i + 1))") auto |
|
614 |
|
615 lemma zeros_base_simp: "j < i \<Longrightarrow> zeros i j = <(i = j + 1)>" |
|
616 by simp |
|
617 |
|
618 lemma zeros_step_simp: "\<not> j < i \<Longrightarrow> zeros i j = ((zero i) ** zeros (i + 1) j)" |
|
619 by simp |
|
620 |
|
621 declare zeros.simps [simp del] |
|
622 lemmas zeros_simps [simp] = zeros_base_simp zeros_step_simp |
|
623 |
|
624 lemma zeros_induct [case_names Base Step]: |
|
625 fixes P i j |
|
626 assumes h1: "\<And> i j. j < i \<Longrightarrow> P i j (<(i = j + (1::int))>)" |
|
627 and h2: "\<And> i j. \<lbrakk>\<not> j < i; P (i + 1) j (zeros (i + 1) j)\<rbrakk> \<Longrightarrow> |
|
628 P i j ((zero i) ** zeros (i + 1) j)" |
|
629 shows "P i j (zeros i j)" |
|
630 proof(induct i j rule:zeros.induct) |
|
631 fix i j |
|
632 assume ih: "(\<not> j < i \<Longrightarrow> P (i + 1) j (zeros (i + 1) j))" |
|
633 show "P i j (zeros i j)" |
|
634 proof(cases "j < i") |
|
635 case True |
|
636 with h1 [OF True] |
|
637 show ?thesis by auto |
|
638 next |
|
639 case False |
|
640 from h2 [OF False] and ih[OF False] |
|
641 have "P i j (zero i \<and>* zeros (i + 1) j)" by blast |
|
642 with False show ?thesis by auto |
|
643 qed |
|
644 qed |
|
645 |
|
646 lemma zeros_rev: "\<not> j < i \<Longrightarrow> (zeros i j) = ((zeros i (j - 1)) ** zero j)" |
|
647 proof(induct i j rule:zeros_induct) |
|
648 case (Base i j) |
|
649 thus ?case by auto |
|
650 next |
|
651 case (Step i j) |
|
652 show ?case |
|
653 proof(cases "j < i + 1") |
|
654 case True |
|
655 with Step show ?thesis by auto |
|
656 next |
|
657 case False |
|
658 with Step show ?thesis by (auto simp:sep_conj_ac) |
|
659 qed |
|
660 qed |
|
661 |
|
662 lemma zeros_rev_induct [case_names Base Step]: |
|
663 fixes P i j |
|
664 assumes h1: "\<And> i j. j < i \<Longrightarrow> P i j (<(i = j + (1::int))>)" |
|
665 and h2: "\<And> i j. \<lbrakk>\<not> j < i; P i (j - 1) (zeros i (j - 1))\<rbrakk> \<Longrightarrow> |
|
666 P i j ((zeros i (j - 1)) ** zero j)" |
|
667 shows "P i j (zeros i j)" |
|
668 proof(induct i j rule:ones'.induct) |
|
669 fix i j |
|
670 assume ih: "(\<not> j < i \<Longrightarrow> P i (j - 1) (zeros i (j - 1)))" |
|
671 show "P i j (zeros i j)" |
|
672 proof(cases "j < i") |
|
673 case True |
|
674 with h1 [OF True] |
|
675 show ?thesis by auto |
|
676 next |
|
677 case False |
|
678 from h2 [OF False] and ih[OF False] |
|
679 have "P i j (zeros i (j - 1) \<and>* zero j)" by blast |
|
680 with zeros_rev and False |
|
681 show ?thesis by simp |
|
682 qed |
|
683 qed |
|
684 |
|
685 definition "rep i j k = ((ones i (i + (int k))) ** <(j = i + int k)>)" |
|
686 |
|
687 fun reps :: "int \<Rightarrow> int \<Rightarrow> nat list\<Rightarrow> tassert" |
|
688 where |
|
689 "reps i j [] = <(i = j + 1)>" | |
|
690 "reps i j [k] = (ones i (i + int k) ** <(j = i + int k)>)" | |
|
691 "reps i j (k # ks) = (ones i (i + int k) ** zero (i + int k + 1) ** reps (i + int k + 2) j ks)" |
|
692 |
|
693 lemma reps_simp3: "ks \<noteq> [] \<Longrightarrow> |
|
694 reps i j (k # ks) = (ones i (i + int k) ** zero (i + int k + 1) ** reps (i + int k + 2) j ks)" |
|
695 by (cases ks, auto) |
|
696 |
|
697 definition "reps_sep_len ks = (if length ks \<le> 1 then 0 else (length ks) - 1)" |
|
698 |
|
699 definition "reps_ctnt_len ks = (\<Sum> k \<leftarrow> ks. (1 + k))" |
|
700 |
|
701 definition "reps_len ks = (reps_sep_len ks) + (reps_ctnt_len ks)" |
|
702 |
|
703 definition "splited xs ys zs = ((xs = ys @ zs) \<and> (ys \<noteq> []) \<and> (zs \<noteq> []))" |
|
704 |
|
705 lemma list_split: |
|
706 assumes h: "k # ks = ys @ zs" |
|
707 and h1: "ys \<noteq> []" |
|
708 shows "(ys = [k] \<and> zs = ks) \<or> (\<exists> ys'. ys' \<noteq> [] \<and> ys = k # ys' \<and> ks = ys' @ zs)" |
|
709 proof(cases ys) |
|
710 case Nil |
|
711 with h1 show ?thesis by auto |
|
712 next |
|
713 case (Cons y' ys') |
|
714 with h have "k#ks = y'#(ys' @ zs)" by simp |
|
715 hence hh: "y' = k" "ks = ys' @ zs" by auto |
|
716 show ?thesis |
|
717 proof(cases "ys' = []") |
|
718 case False |
|
719 show ?thesis |
|
720 proof(rule disjI2) |
|
721 show " \<exists>ys'. ys' \<noteq> [] \<and> ys = k # ys' \<and> ks = ys' @ zs" |
|
722 proof(rule exI[where x = ys']) |
|
723 from False hh Cons show "ys' \<noteq> [] \<and> ys = k # ys' \<and> ks = ys' @ zs" by auto |
|
724 qed |
|
725 qed |
|
726 next |
|
727 case True |
|
728 show ?thesis |
|
729 proof(rule disjI1) |
|
730 from hh True Cons |
|
731 show "ys = [k] \<and> zs = ks" by auto |
|
732 qed |
|
733 qed |
|
734 qed |
|
735 |
|
736 lemma splited_cons[elim_format]: |
|
737 assumes h: "splited (k # ks) ys zs" |
|
738 shows "(ys = [k] \<and> zs = ks) \<or> (\<exists> ys'. ys' \<noteq> [] \<and> ys = k # ys' \<and> splited ks ys' zs)" |
|
739 proof - |
|
740 from h have "k # ks = ys @ zs" "ys \<noteq> [] " unfolding splited_def by auto |
|
741 from list_split[OF this] |
|
742 have "ys = [k] \<and> zs = ks \<or> (\<exists>ys'. ys' \<noteq> [] \<and> ys = k # ys' \<and> ks = ys' @ zs)" . |
|
743 thus ?thesis |
|
744 proof |
|
745 assume " ys = [k] \<and> zs = ks" thus ?thesis by auto |
|
746 next |
|
747 assume "\<exists>ys'. ys' \<noteq> [] \<and> ys = k # ys' \<and> ks = ys' @ zs" |
|
748 then obtain ys' where hh: "ys' \<noteq> []" "ys = k # ys'" "ks = ys' @ zs" by auto |
|
749 show ?thesis |
|
750 proof(rule disjI2) |
|
751 show "\<exists>ys'. ys' \<noteq> [] \<and> ys = k # ys' \<and> splited ks ys' zs" |
|
752 proof(rule exI[where x = ys']) |
|
753 from h have "zs \<noteq> []" by (unfold splited_def, simp) |
|
754 with hh(1) hh(3) |
|
755 have "splited ks ys' zs" |
|
756 by (unfold splited_def, auto) |
|
757 with hh(1) hh(2) show "ys' \<noteq> [] \<and> ys = k # ys' \<and> splited ks ys' zs" by auto |
|
758 qed |
|
759 qed |
|
760 qed |
|
761 qed |
|
762 |
|
763 lemma splited_cons_elim: |
|
764 assumes h: "splited (k # ks) ys zs" |
|
765 and h1: "\<lbrakk>ys = [k]; zs = ks\<rbrakk> \<Longrightarrow> P" |
|
766 and h2: "\<And> ys'. \<lbrakk>ys' \<noteq> []; ys = k#ys'; splited ks ys' zs\<rbrakk> \<Longrightarrow> P" |
|
767 shows P |
|
768 proof(rule splited_cons[OF h]) |
|
769 assume "ys = [k] \<and> zs = ks \<or> (\<exists>ys'. ys' \<noteq> [] \<and> ys = k # ys' \<and> splited ks ys' zs)" |
|
770 thus P |
|
771 proof |
|
772 assume hh: "ys = [k] \<and> zs = ks" |
|
773 show P |
|
774 proof(rule h1) |
|
775 from hh show "ys = [k]" by simp |
|
776 next |
|
777 from hh show "zs = ks" by simp |
|
778 qed |
|
779 next |
|
780 assume "\<exists>ys'. ys' \<noteq> [] \<and> ys = k # ys' \<and> splited ks ys' zs" |
|
781 then obtain ys' where hh: "ys' \<noteq> []" "ys = k # ys'" "splited ks ys' zs" by auto |
|
782 from h2[OF this] |
|
783 show P . |
|
784 qed |
|
785 qed |
|
786 |
|
787 lemma list_nth_expand: |
|
788 "i < length xs \<Longrightarrow> xs = take i xs @ [xs!i] @ drop (Suc i) xs" |
|
789 by (metis Cons_eq_appendI append_take_drop_id drop_Suc_conv_tl eq_Nil_appendI) |
|
790 |
|
791 lemma reps_len_nil: "reps_len [] = 0" |
|
792 by (auto simp:reps_len_def reps_sep_len_def reps_ctnt_len_def) |
|
793 |
|
794 lemma reps_len_sg: "reps_len [k] = 1 + k" |
|
795 by (auto simp:reps_len_def reps_sep_len_def reps_ctnt_len_def) |
|
796 |
|
797 lemma reps_len_cons: "ks \<noteq> [] \<Longrightarrow> (reps_len (k # ks)) = 2 + k + reps_len ks " |
|
798 proof(induct ks arbitrary:k) |
|
799 case (Cons n ns) |
|
800 thus ?case |
|
801 by(cases "ns = []", |
|
802 auto simp:reps_len_def reps_sep_len_def reps_ctnt_len_def) |
|
803 qed auto |
|
804 |
|
805 lemma reps_len_correct: |
|
806 assumes h: "(reps i j ks) s" |
|
807 shows "j = i + int (reps_len ks) - 1" |
|
808 using h |
|
809 proof(induct ks arbitrary:i j s) |
|
810 case Nil |
|
811 thus ?case |
|
812 by (auto simp:reps_len_nil pasrt_def) |
|
813 next |
|
814 case (Cons n ns) |
|
815 thus ?case |
|
816 proof(cases "ns = []") |
|
817 case False |
|
818 from Cons(2)[unfolded reps_simp3[OF False]] |
|
819 obtain s' where "(reps (i + int n + 2) j ns) s'" |
|
820 by (auto elim!:sep_conjE) |
|
821 from Cons.hyps[OF this] |
|
822 show ?thesis |
|
823 by (unfold reps_len_cons[OF False], simp) |
|
824 next |
|
825 case True |
|
826 with Cons(2) |
|
827 show ?thesis |
|
828 by (auto simp:reps_len_sg pasrt_def) |
|
829 qed |
|
830 qed |
|
831 |
|
832 lemma reps_len_expand: |
|
833 shows "(EXS j. (reps i j ks)) = (reps i (i + int (reps_len ks) - 1) ks)" |
|
834 proof(default+) |
|
835 fix s |
|
836 assume "(EXS j. reps i j ks) s" |
|
837 with reps_len_correct show "reps i (i + int (reps_len ks) - 1) ks s" |
|
838 by (auto simp:pred_ex_def) |
|
839 next |
|
840 fix s |
|
841 assume "reps i (i + int (reps_len ks) - 1) ks s" |
|
842 thus "(EXS j. reps i j ks) s" by (auto simp:pred_ex_def) |
|
843 qed |
|
844 |
|
845 lemma reps_len_expand1: "(EXS j. (reps i j ks \<and>* r)) = (reps i (i + int (reps_len ks) - 1) ks \<and>* r)" |
|
846 by (metis reps_len_def reps_len_expand sep.mult_commute sep_conj_exists1) |
|
847 |
|
848 lemma reps_splited: |
|
849 assumes h: "splited xs ys zs" |
|
850 shows "reps i j xs = (reps i (i + int (reps_len ys) - 1) ys \<and>* |
|
851 zero (i + int (reps_len ys)) \<and>* |
|
852 reps (i + int (reps_len ys) + 1) j zs)" |
|
853 using h |
|
854 proof(induct xs arbitrary: i j ys zs) |
|
855 case Nil |
|
856 thus ?case |
|
857 by (unfold splited_def, auto) |
|
858 next |
|
859 case (Cons k ks) |
|
860 from Cons(2) |
|
861 show ?case |
|
862 proof(rule splited_cons_elim) |
|
863 assume h: "ys = [k]" "zs = ks" |
|
864 with Cons(2) |
|
865 show ?thesis |
|
866 proof(cases "ks = []") |
|
867 case True |
|
868 with Cons(2) have False |
|
869 by (simp add:splited_def, cases ys, auto) |
|
870 thus ?thesis by auto |
|
871 next |
|
872 case False |
|
873 have ss: "i + int k + 1 = i + (1 + int k)" |
|
874 "i + int k + 2 = 2 + (i + int k)" by auto |
|
875 show ?thesis |
|
876 by (unfold h(1), unfold reps_simp3[OF False] reps.simps(2) |
|
877 reps_len_sg, auto simp:sep_conj_ac, |
|
878 unfold ss h(2), simp) |
|
879 qed |
|
880 next |
|
881 fix ys' |
|
882 assume h: "ys' \<noteq> []" "ys = k # ys'" "splited ks ys' zs" |
|
883 hence nnks: "ks \<noteq> []" |
|
884 by (unfold splited_def, auto) |
|
885 have ss: "i + int k + 2 + int (reps_len ys') = |
|
886 i + (2 + (int k + int (reps_len ys')))" by auto |
|
887 show ?thesis |
|
888 by (simp add: reps_simp3[OF nnks] reps_simp3[OF h(1)] |
|
889 Cons(1)[OF h(3)] h(2) |
|
890 reps_len_cons[OF h(1)] |
|
891 sep_conj_ac, subst ss, simp) |
|
892 qed |
|
893 qed |
|
894 |
|
895 subsection {* A theory of list extension *} |
|
896 |
|
897 definition "list_ext n xs = xs @ replicate ((Suc n) - length xs) 0" |
|
898 |
|
899 lemma list_ext_len_eq: "length (list_ext a xs) = length xs + (Suc a - length xs)" |
|
900 by (metis length_append length_replicate list_ext_def) |
|
901 |
|
902 lemma list_ext_len: "a < length (list_ext a xs)" |
|
903 by (unfold list_ext_len_eq, auto) |
|
904 |
|
905 lemma list_ext_lt: "a < length xs \<Longrightarrow> list_ext a xs = xs" |
|
906 by (smt append_Nil2 list_ext_def replicate_0) |
|
907 |
|
908 lemma list_ext_lt_get: |
|
909 assumes h: "a' < length xs" |
|
910 shows "(list_ext a xs)!a' = xs!a'" |
|
911 proof(cases "a < length xs") |
|
912 case True |
|
913 with h |
|
914 show ?thesis by (auto simp:list_ext_lt) |
|
915 next |
|
916 case False |
|
917 with h show ?thesis |
|
918 apply (unfold list_ext_def) |
|
919 by (metis nth_append) |
|
920 qed |
|
921 |
|
922 lemma list_ext_get_upd: "((list_ext a xs)[a:=v])!a = v" |
|
923 by (metis list_ext_len nth_list_update_eq) |
|
924 |
|
925 lemma nth_app: "length xs \<le> a \<Longrightarrow> (xs @ ys)!a = ys!(a - length xs)" |
|
926 by (metis not_less nth_append) |
|
927 |
|
928 lemma pred_exI: |
|
929 assumes "(P(x) \<and>* r) s" |
|
930 shows "((EXS x. P(x)) \<and>* r) s" |
|
931 by (metis assms pred_ex_def sep_globalise) |
|
932 |
|
933 lemma list_ext_tail: |
|
934 assumes h1: "length xs \<le> a" |
|
935 and h2: "length xs \<le> a'" |
|
936 and h3: "a' \<le> a" |
|
937 shows "(list_ext a xs)!a' = 0" |
|
938 proof - |
|
939 from h1 h2 |
|
940 have "a' - length xs < length (replicate (Suc a - length xs) 0)" |
|
941 by (metis diff_less_mono h3 le_imp_less_Suc length_replicate) |
|
942 moreover from h1 have "replicate (Suc a - length xs) 0 \<noteq> []" |
|
943 by (smt empty_replicate) |
|
944 ultimately have "(replicate (Suc a - length xs) 0)!(a' - length xs) = 0" |
|
945 by (metis length_replicate nth_replicate) |
|
946 moreover have "(xs @ (replicate (Suc a - length xs) 0))!a' = |
|
947 (replicate (Suc a - length xs) 0)!(a' - length xs)" |
|
948 by (rule nth_app[OF h2]) |
|
949 ultimately show ?thesis |
|
950 by (auto simp:list_ext_def) |
|
951 qed |
|
952 |
|
953 lemmas list_ext_simps = list_ext_lt_get list_ext_lt list_ext_len list_ext_len_eq |
|
954 |
|
955 lemma listsum_upd_suc: |
|
956 "a < length ks \<Longrightarrow> listsum (map Suc (ks[a := Suc (ks ! a)]))= (Suc (listsum (map Suc ks)))" |
|
957 by (smt Ex_list_of_length Skolem_list_nth elem_le_listsum_nat |
|
958 length_induct length_list_update length_map length_splice |
|
959 list_eq_iff_nth_eq list_ext_get_upd list_ext_lt_get list_update_beyond |
|
960 list_update_id list_update_overwrite list_update_same_conv list_update_swap |
|
961 listsum_update_nat map_eq_imp_length_eq map_update neq_if_length_neq |
|
962 nth_equalityI nth_list_update nth_list_update_eq nth_list_update_neq nth_map reps_sep_len_def) |
|
963 |
|
964 lemma reps_len_suc: |
|
965 assumes "a < length ks" |
|
966 shows "reps_len (ks[a:=Suc(ks!a)]) = 1 + reps_len ks" |
|
967 proof(cases "length ks \<le> 1") |
|
968 case True |
|
969 with `a < length ks` |
|
970 obtain k where "ks = [k]" "a = 0" |
|
971 by (smt length_0_conv length_Suc_conv) |
|
972 thus ?thesis |
|
973 apply (unfold `ks = [k]` `a = 0`) |
|
974 by (unfold reps_len_def reps_sep_len_def reps_ctnt_len_def, auto) |
|
975 next |
|
976 case False |
|
977 have "Suc = (op + (Suc 0))" |
|
978 by (default, auto) |
|
979 with listsum_upd_suc[OF `a < length ks`] False |
|
980 show ?thesis |
|
981 by (unfold reps_len_def reps_sep_len_def reps_ctnt_len_def, simp) |
|
982 qed |
|
983 |
|
984 lemma ks_suc_len: |
|
985 assumes h1: "(reps i j ks) s" |
|
986 and h2: "ks!a = v" |
|
987 and h3: "a < length ks" |
|
988 and h4: "(reps i j' (ks[a:=Suc v])) s'" |
|
989 shows "j' = j + int (reps_len (list_ext a ks)) - int (reps_len ks) + 1" |
|
990 proof - |
|
991 from reps_len_correct[OF h1, unfolded list_ext_len_eq] |
|
992 reps_len_correct[OF h4, unfolded list_ext_len_eq] |
|
993 reps_len_suc[OF `a < length ks`] h2 h3 |
|
994 show ?thesis |
|
995 by (unfold list_ext_lt[OF `a < length ks`], auto) |
|
996 qed |
|
997 |
|
998 lemma ks_ext_len: |
|
999 assumes h1: "(reps i j ks) s" |
|
1000 and h3: "length ks \<le> a" |
|
1001 and h4: "reps i j' (list_ext a ks) s'" |
|
1002 shows "j' = j + int (reps_len (list_ext a ks)) - int (reps_len ks)" |
|
1003 proof - |
|
1004 from reps_len_correct[OF h1, unfolded list_ext_len_eq] |
|
1005 and reps_len_correct[OF h4, unfolded list_ext_len_eq] |
|
1006 h3 |
|
1007 show ?thesis by auto |
|
1008 qed |
|
1009 |
|
1010 definition |
|
1011 "reps' i j ks = |
|
1012 (if ks = [] then (<(i = j + 1)>) else (reps i (j - 1) ks \<and>* zero j))" |
|
1013 |
|
1014 lemma reps'_expand: |
|
1015 assumes h: "ks \<noteq> []" |
|
1016 shows "(EXS j. reps' i j ks) = reps' i (i + int (reps_len ks)) ks" |
|
1017 proof - |
|
1018 show ?thesis |
|
1019 proof(default+) |
|
1020 fix s |
|
1021 assume "(EXS j. reps' i j ks) s" |
|
1022 with h have "(EXS j. reps i (j - 1) ks \<and>* zero j) s" |
|
1023 by (simp add:reps'_def) |
|
1024 hence "(reps i (i + int (reps_len ks) - 1) ks \<and>* zero (i + int (reps_len ks))) s" |
|
1025 proof(elim EXS_elim) |
|
1026 fix j |
|
1027 assume "(reps i (j - 1) ks \<and>* zero j) s" |
|
1028 then obtain s1 s2 where "s = s1 + s2" "s1 ## s2" "reps i (j - 1) ks s1" "zero j s2" |
|
1029 by (auto elim!:sep_conjE) |
|
1030 from reps_len_correct[OF this(3)] |
|
1031 have "j = i + int (reps_len ks)" by auto |
|
1032 with `reps i (j - 1) ks s1` have "reps i (i + int (reps_len ks) - 1) ks s1" |
|
1033 by simp |
|
1034 moreover from `j = i + int (reps_len ks)` and `zero j s2` |
|
1035 have "zero (i + int (reps_len ks)) s2" by auto |
|
1036 ultimately show "(reps i (i + int (reps_len ks) - 1) ks \<and>* zero (i + int (reps_len ks))) s" |
|
1037 using `s = s1 + s2` `s1 ## s2` by (auto intro!:sep_conjI) |
|
1038 qed |
|
1039 thus "reps' i (i + int (reps_len ks)) ks s" |
|
1040 by (simp add:h reps'_def) |
|
1041 next |
|
1042 fix s |
|
1043 assume "reps' i (i + int (reps_len ks)) ks s" |
|
1044 thus "(EXS j. reps' i j ks) s" |
|
1045 by (auto intro!:EXS_intro) |
|
1046 qed |
|
1047 qed |
|
1048 |
|
1049 lemma reps'_len_correct: |
|
1050 assumes h: "(reps' i j ks) s" |
|
1051 and h1: "ks \<noteq> []" |
|
1052 shows "(j = i + int (reps_len ks))" |
|
1053 proof - |
|
1054 from h1 have "reps' i j ks s = (reps i (j - 1) ks \<and>* zero j) s" by (simp add:reps'_def) |
|
1055 from h[unfolded this] |
|
1056 obtain s' where "reps i (j - 1) ks s'" |
|
1057 by (auto elim!:sep_conjE) |
|
1058 from reps_len_correct[OF this] |
|
1059 show ?thesis by simp |
|
1060 qed |
|
1061 |
|
1062 lemma reps'_append: |
|
1063 "reps' i j (ks1 @ ks2) = (EXS m. (reps' i (m - 1) ks1 \<and>* reps' m j ks2))" |
|
1064 proof(cases "ks1 = []") |
|
1065 case True |
|
1066 thus ?thesis |
|
1067 by (auto simp:reps'_def pred_ex_def pasrt_def set_ins_def sep_conj_def) |
|
1068 next |
|
1069 case False |
|
1070 show ?thesis |
|
1071 proof(cases "ks2 = []") |
|
1072 case False |
|
1073 from `ks1 \<noteq> []` and `ks2 \<noteq> []` |
|
1074 have "splited (ks1 @ ks2) ks1 ks2" by (auto simp:splited_def) |
|
1075 from reps_splited[OF this, of i "j - 1"] |
|
1076 have eq_1: "reps i (j - 1) (ks1 @ ks2) = |
|
1077 (reps i (i + int (reps_len ks1) - 1) ks1 \<and>* |
|
1078 zero (i + int (reps_len ks1)) \<and>* |
|
1079 reps (i + int (reps_len ks1) + 1) (j - 1) ks2)" . |
|
1080 from `ks1 \<noteq> []` |
|
1081 have eq_2: "reps' i j (ks1 @ ks2) = (reps i (j - 1) (ks1 @ ks2) \<and>* zero j)" |
|
1082 by (unfold reps'_def, simp) |
|
1083 show ?thesis |
|
1084 proof(default+) |
|
1085 fix s |
|
1086 assume "reps' i j (ks1 @ ks2) s" |
|
1087 show "(EXS m. reps' i (m - 1) ks1 \<and>* reps' m j ks2) s" |
|
1088 proof(rule EXS_intro[where x = "i + int(reps_len ks1) + 1"]) |
|
1089 from `reps' i j (ks1 @ ks2) s`[unfolded eq_2 eq_1] |
|
1090 and `ks1 \<noteq> []` `ks2 \<noteq> []` |
|
1091 show "(reps' i (i + int (reps_len ks1) + 1 - 1) ks1 \<and>* |
|
1092 reps' (i + int (reps_len ks1) + 1) j ks2) s" |
|
1093 by (unfold reps'_def, simp, sep_cancel+) |
|
1094 qed |
|
1095 next |
|
1096 fix s |
|
1097 assume "(EXS m. reps' i (m - 1) ks1 \<and>* reps' m j ks2) s" |
|
1098 thus "reps' i j (ks1 @ ks2) s" |
|
1099 proof(elim EXS_elim) |
|
1100 fix m |
|
1101 assume "(reps' i (m - 1) ks1 \<and>* reps' m j ks2) s" |
|
1102 then obtain s1 s2 where h: |
|
1103 "s = s1 + s2" "s1 ## s2" "reps' i (m - 1) ks1 s1" |
|
1104 " reps' m j ks2 s2" by (auto elim!:sep_conjE) |
|
1105 from reps'_len_correct[OF this(3) `ks1 \<noteq> []`] |
|
1106 have eq_m: "m = i + int (reps_len ks1) + 1" by simp |
|
1107 have "((reps i (i + int (reps_len ks1) - 1) ks1 \<and>* zero (i + int (reps_len ks1))) \<and>* |
|
1108 ((reps (i + int (reps_len ks1) + 1) (j - 1) ks2) \<and>* zero j)) s" |
|
1109 (is "(?P \<and>* ?Q) s") |
|
1110 proof(rule sep_conjI) |
|
1111 from h(3) eq_m `ks1 \<noteq> []` show "?P s1" |
|
1112 by (simp add:reps'_def) |
|
1113 next |
|
1114 from h(4) eq_m `ks2 \<noteq> []` show "?Q s2" |
|
1115 by (simp add:reps'_def) |
|
1116 next |
|
1117 from h(2) show "s1 ## s2" . |
|
1118 next |
|
1119 from h(1) show "s = s1 + s2" . |
|
1120 qed |
|
1121 thus "reps' i j (ks1 @ ks2) s" |
|
1122 by (unfold eq_2 eq_1, auto simp:sep_conj_ac) |
|
1123 qed |
|
1124 qed |
|
1125 next |
|
1126 case True |
|
1127 have "-1 + j = j - 1" by auto |
|
1128 with `ks1 \<noteq> []` True |
|
1129 show ?thesis |
|
1130 apply (auto simp:reps'_def pred_ex_def pasrt_def) |
|
1131 apply (unfold `-1 + j = j - 1`) |
|
1132 by (fold sep_empty_def, simp only:sep_conj_empty) |
|
1133 qed |
|
1134 qed |
|
1135 |
|
1136 lemma reps'_sg: |
|
1137 "reps' i j [k] = |
|
1138 (<(j = i + int k + 1)> \<and>* ones i (i + int k) \<and>* zero j)" |
|
1139 apply (unfold reps'_def, default, auto simp:sep_conj_ac) |
|
1140 by (sep_cancel+, simp add:pasrt_def)+ |
|
1141 |
|
1142 |
|
1143 section {* Basic macros for TM *} |
|
1144 |
|
1145 definition "write_zero = (TL exit. \<guillemotright>((W0, exit), (W0, exit)); TLabel exit)" |
|
1146 |
|
1147 lemma st_upd: |
|
1148 assumes pre: "(st i' ** r) (trset_of (f, x, i, y, z))" |
|
1149 shows "(st i'' ** r) (trset_of (f, x, i'', y, z))" |
|
1150 proof - |
|
1151 from stimes_sgD[OF pre[unfolded st_def], unfolded trset_of.simps, unfolded stD[OF pre]] |
|
1152 have "r (tprog_set x \<union> tpc_set i' \<union> tpos_set y \<union> tmem_set z \<union> tfaults_set f - tpc_set i')" |
|
1153 by blast |
|
1154 moreover have |
|
1155 "(tprog_set x \<union> tpc_set i' \<union> tpos_set y \<union> tmem_set z \<union> tfaults_set f - tpc_set i') = |
|
1156 (tprog_set x \<union> tpos_set y \<union> tmem_set z \<union> tfaults_set f)" |
|
1157 by (unfold tpn_set_def, auto) |
|
1158 ultimately have r_rest: "r (tprog_set x \<union> tpos_set y \<union> tmem_set z \<union> tfaults_set f)" |
|
1159 by auto |
|
1160 show ?thesis |
|
1161 proof(rule sep_conjI[where Q = r, OF _ r_rest]) |
|
1162 show "st i'' (tpc_set i'')" |
|
1163 by (unfold st_def sg_def, simp) |
|
1164 next |
|
1165 show "tpc_set i'' ## tprog_set x \<union> tpos_set y \<union> tmem_set z \<union> tfaults_set f" |
|
1166 by (unfold tpn_set_def sep_disj_set_def, auto) |
|
1167 next |
|
1168 show "trset_of (f, x, i'', y, z) = |
|
1169 tpc_set i'' + (tprog_set x \<union> tpos_set y \<union> tmem_set z \<union> tfaults_set f)" |
|
1170 by (unfold trset_of.simps plus_set_def, auto) |
|
1171 qed |
|
1172 qed |
|
1173 |
|
1174 lemma pos_upd: |
|
1175 assumes pre: "(ps i ** r) (trset_of (f, x, y, i', z))" |
|
1176 shows "(ps j ** r) (trset_of (f, x, y, j, z))" |
|
1177 proof - |
|
1178 from stimes_sgD[OF pre[unfolded ps_def], unfolded trset_of.simps, unfolded psD[OF pre]] |
|
1179 have "r (tprog_set x \<union> tpc_set y \<union> tpos_set i \<union> tmem_set z \<union> |
|
1180 tfaults_set f - tpos_set i)" (is "r ?xs") |
|
1181 by blast |
|
1182 moreover have |
|
1183 "?xs = tprog_set x \<union> tpc_set y \<union> tmem_set z \<union> tfaults_set f" |
|
1184 by (unfold tpn_set_def, auto) |
|
1185 ultimately have r_rest: "r \<dots>" |
|
1186 by auto |
|
1187 show ?thesis |
|
1188 proof(rule sep_conjI[where Q = r, OF _ r_rest]) |
|
1189 show "ps j (tpos_set j)" |
|
1190 by (unfold ps_def sg_def, simp) |
|
1191 next |
|
1192 show "tpos_set j ## tprog_set x \<union> tpc_set y \<union> tmem_set z \<union> tfaults_set f" |
|
1193 by (unfold tpn_set_def sep_disj_set_def, auto) |
|
1194 next |
|
1195 show "trset_of (f, x, y, j, z) = |
|
1196 tpos_set j + (tprog_set x \<union> tpc_set y \<union> tmem_set z \<union> tfaults_set f)" |
|
1197 by (unfold trset_of.simps plus_set_def, auto) |
|
1198 qed |
|
1199 qed |
|
1200 |
|
1201 lemma TM_in_simp: "({TM a v} \<subseteq> |
|
1202 tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tmem_set mem \<union> tfaults_set f) = |
|
1203 ({TM a v} \<subseteq> tmem_set mem)" |
|
1204 by (unfold tpn_set_def, auto) |
|
1205 |
|
1206 lemma tmem_set_upd: |
|
1207 "{TM a v} \<subseteq> tmem_set mem \<Longrightarrow> |
|
1208 tmem_set (mem(a:=Some v')) = ((tmem_set mem) - {TM a v}) \<union> {TM a v'}" |
|
1209 by (unfold tpn_set_def, auto) |
|
1210 |
|
1211 lemma tmem_set_disj: "{TM a v} \<subseteq> tmem_set mem \<Longrightarrow> |
|
1212 {TM a v'} \<inter> (tmem_set mem - {TM a v}) = {}" |
|
1213 by (unfold tpn_set_def, auto) |
|
1214 |
|
1215 lemma smem_upd: "((tm a v) ** r) (trset_of (f, x, y, z, mem)) \<Longrightarrow> |
|
1216 ((tm a v') ** r) (trset_of (f, x, y, z, mem(a := Some v')))" |
|
1217 proof - |
|
1218 have eq_s: "(tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tmem_set mem \<union> tfaults_set f - {TM a v}) = |
|
1219 (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)" |
|
1220 by (unfold tpn_set_def, auto) |
|
1221 assume g: "(tm a v \<and>* r) (trset_of (f, x, y, z, mem))" |
|
1222 from this[unfolded trset_of.simps tm_def] |
|
1223 have h: "(sg {TM a v} \<and>* r) (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tmem_set mem \<union> tfaults_set f)" . |
|
1224 hence h0: "r (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)" |
|
1225 by(sep_drule stimes_sgD, clarify, unfold eq_s, auto) |
|
1226 from h TM_in_simp have "{TM a v} \<subseteq> tmem_set mem" |
|
1227 by(sep_drule stimes_sgD, auto) |
|
1228 from tmem_set_upd [OF this] tmem_set_disj[OF this] |
|
1229 have h2: "tmem_set (mem(a \<mapsto> v')) = {TM a v'} \<union> (tmem_set mem - {TM a v})" |
|
1230 "{TM a v'} \<inter> (tmem_set mem - {TM a v}) = {}" by auto |
|
1231 show ?thesis |
|
1232 proof - |
|
1233 have "(tm a v' ** r) (tmem_set (mem(a \<mapsto> v')) \<union> tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tfaults_set f)" |
|
1234 proof(rule sep_conjI) |
|
1235 show "tm a v' ({TM a v'})" by (unfold tm_def sg_def, simp) |
|
1236 next |
|
1237 from h0 show "r (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)" . |
|
1238 next |
|
1239 show "{TM a v'} ## tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f" |
|
1240 proof - |
|
1241 from g have " mem a = Some v" |
|
1242 by(sep_frule memD, simp) |
|
1243 thus "?thesis" |
|
1244 by(unfold tpn_set_def set_ins_def, auto) |
|
1245 qed |
|
1246 next |
|
1247 show "tmem_set (mem(a \<mapsto> v')) \<union> tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tfaults_set f = |
|
1248 {TM a v'} + (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)" |
|
1249 by (unfold h2(1) set_ins_def eq_s, auto) |
|
1250 qed |
|
1251 thus ?thesis |
|
1252 apply (unfold trset_of.simps) |
|
1253 by (metis sup_commute sup_left_commute) |
|
1254 qed |
|
1255 qed |
|
1256 |
|
1257 lemma hoare_write_zero: |
|
1258 "\<lbrace>st i ** ps p ** tm p v\<rbrace> |
|
1259 i:[write_zero]:j |
|
1260 \<lbrace>st j ** ps p ** tm p Bk\<rbrace>" |
|
1261 proof(unfold write_zero_def, intro t_hoare_local, rule t_hoare_label_last, simp, simp) |
|
1262 show "\<lbrace>st i \<and>* ps p \<and>* tm p v\<rbrace> i :[ \<guillemotright> ((W0, j), W0, j) ]: j \<lbrace>st j \<and>* ps p \<and>* tm p Bk\<rbrace>" |
|
1263 proof(unfold tassemble_to.simps, simp only:sep_conj_cond, |
|
1264 intro tm.code_condI, simp) |
|
1265 assume eq_j: "j = Suc i" |
|
1266 show "\<lbrace>st i \<and>* ps p \<and>* tm p v\<rbrace> sg {TC i ((W0, Suc i), W0, Suc i)} |
|
1267 \<lbrace>st (Suc i) \<and>* ps p \<and>* tm p Bk\<rbrace>" |
|
1268 proof(fold eq_j, unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify) |
|
1269 fix ft prog cs i' mem r |
|
1270 assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* sg {TC i ((W0, j), W0, j)}) |
|
1271 (trset_of (ft, prog, cs, i', mem))" |
|
1272 from h have "prog i = Some ((W0, j), W0, j)" |
|
1273 apply(rule_tac r = "r \<and>* ps p \<and>* tm p v" in codeD) |
|
1274 by(simp add: sep_conj_ac) |
|
1275 from h and this have stp: |
|
1276 "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, j, i', mem(i'\<mapsto> Bk))" (is "?x = ?y") |
|
1277 apply(sep_frule psD) |
|
1278 apply(sep_frule stD) |
|
1279 apply(sep_frule memD, simp) |
|
1280 by(cases v, unfold tm.run_def, auto) |
|
1281 from h have "i' = p" |
|
1282 by(sep_drule psD, simp) |
|
1283 with h |
|
1284 have "(r \<and>* ps p \<and>* st j \<and>* tm p Bk \<and>* sg {TC i ((W0, j), W0, j)}) (trset_of ?x)" |
|
1285 apply(unfold stp) |
|
1286 apply(sep_drule pos_upd, sep_drule st_upd, sep_drule smem_upd) |
|
1287 apply(auto simp: sep_conj_ac) |
|
1288 done |
|
1289 thus "\<exists>k. (r \<and>* ps p \<and>* st j \<and>* tm p Bk \<and>* sg {TC i ((W0, j), W0, j)}) |
|
1290 (trset_of (tm.run (Suc k) (ft, prog, cs, i', mem)))" |
|
1291 apply (rule_tac x = 0 in exI) |
|
1292 by auto |
|
1293 qed |
|
1294 qed |
|
1295 qed |
|
1296 |
|
1297 lemma hoare_write_zero_gen[step]: |
|
1298 assumes "p = q" |
|
1299 shows "\<lbrace>st i ** ps p ** tm q v\<rbrace> |
|
1300 i:[write_zero]:j |
|
1301 \<lbrace>st j ** ps p ** tm q Bk\<rbrace>" |
|
1302 by (unfold assms, rule hoare_write_zero) |
|
1303 |
|
1304 definition "write_one = (TL exit. \<guillemotright>((W1, exit), (W1, exit)); TLabel exit)" |
|
1305 |
|
1306 lemma hoare_write_one: |
|
1307 "\<lbrace>st i ** ps p ** tm p v\<rbrace> |
|
1308 i:[write_one]:j |
|
1309 \<lbrace>st j ** ps p ** tm p Oc\<rbrace>" |
|
1310 proof(unfold write_one_def, intro t_hoare_local, rule t_hoare_label_last, simp+) |
|
1311 fix l |
|
1312 show "\<lbrace>st i \<and>* ps p \<and>* tm p v\<rbrace> i :[ \<guillemotright> ((W1, j), W1, j) ]: j \<lbrace>st j \<and>* ps p \<and>* tm p Oc\<rbrace>" |
|
1313 proof(unfold tassemble_to.simps, simp only:sep_conj_cond, |
|
1314 rule_tac tm.code_condI, simp add: sep_conj_ac) |
|
1315 let ?j = "Suc i" |
|
1316 show "\<lbrace>ps p \<and>* st i \<and>* tm p v\<rbrace> sg {TC i ((W1, ?j), W1, ?j)} |
|
1317 \<lbrace>ps p \<and>* st ?j \<and>* tm p Oc\<rbrace>" |
|
1318 proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify) |
|
1319 fix ft prog cs i' mem r |
|
1320 assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* sg {TC i ((W1, ?j), W1, ?j)}) |
|
1321 (trset_of (ft, prog, cs, i', mem))" |
|
1322 from h have "prog i = Some ((W1, ?j), W1, ?j)" |
|
1323 apply(rule_tac r = "r \<and>* ps p \<and>* tm p v" in codeD) |
|
1324 by(simp add: sep_conj_ac) |
|
1325 from h and this have stp: |
|
1326 "tm.run 1 (ft, prog, cs, i', mem) = |
|
1327 (ft, prog, ?j, i', mem(i'\<mapsto> Oc))" (is "?x = ?y") |
|
1328 apply(sep_frule psD) |
|
1329 apply(sep_frule stD) |
|
1330 apply(sep_frule memD, simp) |
|
1331 by(cases v, unfold tm.run_def, auto) |
|
1332 from h have "i' = p" |
|
1333 by(sep_drule psD, simp) |
|
1334 with h |
|
1335 have "(r \<and>* ps p \<and>* st ?j \<and>* tm p Oc \<and>* sg {TC i ((W1, ?j), W1, ?j)}) (trset_of ?x)" |
|
1336 apply(unfold stp) |
|
1337 apply(sep_drule pos_upd, sep_drule st_upd, sep_drule smem_upd) |
|
1338 apply(auto simp: sep_conj_ac) |
|
1339 done |
|
1340 thus "\<exists>k. (r \<and>* ps p \<and>* st ?j \<and>* tm p Oc \<and>* sg {TC i ((W1, ?j), W1, ?j)}) |
|
1341 (trset_of (tm.run (Suc k) (ft, prog, cs, i', mem)))" |
|
1342 apply (rule_tac x = 0 in exI) |
|
1343 by auto |
|
1344 qed |
|
1345 qed |
|
1346 qed |
|
1347 |
|
1348 lemma hoare_write_one_gen[step]: |
|
1349 assumes "p = q" |
|
1350 shows "\<lbrace>st i ** ps p ** tm q v\<rbrace> |
|
1351 i:[write_one]:j |
|
1352 \<lbrace>st j ** ps p ** tm q Oc\<rbrace>" |
|
1353 by (unfold assms, rule hoare_write_one) |
|
1354 |
|
1355 definition "move_left = (TL exit . \<guillemotright>((L, exit), (L, exit)); TLabel exit)" |
|
1356 |
|
1357 lemma hoare_move_left: |
|
1358 "\<lbrace>st i ** ps p ** tm p v2\<rbrace> |
|
1359 i:[move_left]:j |
|
1360 \<lbrace>st j ** ps (p - 1) ** tm p v2\<rbrace>" |
|
1361 proof(unfold move_left_def, intro t_hoare_local, rule t_hoare_label_last, simp+) |
|
1362 fix l |
|
1363 show "\<lbrace>st i \<and>* ps p \<and>* tm p v2\<rbrace> i :[ \<guillemotright> ((L, l), L, l) ]: l |
|
1364 \<lbrace>st l \<and>* ps (p - 1) \<and>* tm p v2\<rbrace>" |
|
1365 proof(unfold tassemble_to.simps, simp only:sep_conj_cond, |
|
1366 intro tm.code_condI, simp add: sep_conj_ac) |
|
1367 let ?j = "Suc i" |
|
1368 show "\<lbrace>ps p \<and>* st i \<and>* tm p v2\<rbrace> sg {TC i ((L, ?j), L, ?j)} |
|
1369 \<lbrace>st ?j \<and>* tm p v2 \<and>* ps (p - 1)\<rbrace>" |
|
1370 proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify) |
|
1371 fix ft prog cs i' mem r |
|
1372 assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v2 \<and>* sg {TC i ((L, ?j), L, ?j)}) |
|
1373 (trset_of (ft, prog, cs, i', mem))" |
|
1374 from h have "prog i = Some ((L, ?j), L, ?j)" |
|
1375 apply(rule_tac r = "r \<and>* ps p \<and>* tm p v2" in codeD) |
|
1376 by(simp add: sep_conj_ac) |
|
1377 from h and this have stp: |
|
1378 "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, ?j, i' - 1, mem)" (is "?x = ?y") |
|
1379 apply(sep_frule psD) |
|
1380 apply(sep_frule stD) |
|
1381 apply(sep_frule memD, simp) |
|
1382 apply(unfold tm.run_def, case_tac v2, auto) |
|
1383 done |
|
1384 from h have "i' = p" |
|
1385 by(sep_drule psD, simp) |
|
1386 with h |
|
1387 have "(r \<and>* st ?j \<and>* tm p v2 \<and>* ps (p - 1) \<and>* sg {TC i ((L, ?j), L, ?j)}) |
|
1388 (trset_of ?x)" |
|
1389 apply(unfold stp) |
|
1390 apply(sep_drule pos_upd, sep_drule st_upd, simp) |
|
1391 proof - |
|
1392 assume "(st ?j \<and>* ps (p - 1) \<and>* r \<and>* tm p v2 \<and>* sg {TC i ((L, ?j), L, ?j)}) |
|
1393 (trset_of (ft, prog, ?j, p - 1, mem))" |
|
1394 thus "(r \<and>* st ?j \<and>* tm p v2 \<and>* ps (p - 1) \<and>* sg {TC i ((L, ?j), L, ?j)}) |
|
1395 (trset_of (ft, prog, ?j, p - 1, mem))" |
|
1396 by(simp add: sep_conj_ac) |
|
1397 qed |
|
1398 thus "\<exists>k. (r \<and>* st ?j \<and>* tm p v2 \<and>* ps (p - 1) \<and>* sg {TC i ((L, ?j), L, ?j)}) |
|
1399 (trset_of (tm.run (Suc k) (ft, prog, cs, i', mem)))" |
|
1400 apply (rule_tac x = 0 in exI) |
|
1401 by auto |
|
1402 qed |
|
1403 qed |
|
1404 qed |
|
1405 |
|
1406 lemma hoare_move_left_gen[step]: |
|
1407 assumes "p = q" |
|
1408 shows "\<lbrace>st i ** ps p ** tm q v2\<rbrace> |
|
1409 i:[move_left]:j |
|
1410 \<lbrace>st j ** ps (p - 1) ** tm q v2\<rbrace>" |
|
1411 by (unfold assms, rule hoare_move_left) |
|
1412 |
|
1413 definition "move_right = (TL exit . \<guillemotright>((R, exit), (R, exit)); TLabel exit)" |
|
1414 |
|
1415 lemma hoare_move_right: |
|
1416 "\<lbrace>st i ** ps p ** tm p v1 \<rbrace> |
|
1417 i:[move_right]:j |
|
1418 \<lbrace>st j ** ps (p + 1) ** tm p v1 \<rbrace>" |
|
1419 proof(unfold move_right_def, intro t_hoare_local, rule t_hoare_label_last, simp+) |
|
1420 fix l |
|
1421 show "\<lbrace>st i \<and>* ps p \<and>* tm p v1\<rbrace> i :[ \<guillemotright> ((R, l), R, l) ]: l |
|
1422 \<lbrace>st l \<and>* ps (p + 1) \<and>* tm p v1\<rbrace>" |
|
1423 proof(unfold tassemble_to.simps, simp only:sep_conj_cond, |
|
1424 intro tm.code_condI, simp add: sep_conj_ac) |
|
1425 let ?j = "Suc i" |
|
1426 show "\<lbrace>ps p \<and>* st i \<and>* tm p v1\<rbrace> sg {TC i ((R, ?j), R, ?j)} |
|
1427 \<lbrace>st ?j \<and>* tm p v1 \<and>* ps (p + 1)\<rbrace>" |
|
1428 proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify) |
|
1429 fix ft prog cs i' mem r |
|
1430 assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v1 \<and>* sg {TC i ((R, ?j), R, ?j)}) |
|
1431 (trset_of (ft, prog, cs, i', mem))" |
|
1432 from h have "prog i = Some ((R, ?j), R, ?j)" |
|
1433 apply(rule_tac r = "r \<and>* ps p \<and>* tm p v1" in codeD) |
|
1434 by(simp add: sep_conj_ac) |
|
1435 from h and this have stp: |
|
1436 "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, ?j, i'+ 1, mem)" (is "?x = ?y") |
|
1437 apply(sep_frule psD) |
|
1438 apply(sep_frule stD) |
|
1439 apply(sep_frule memD, simp) |
|
1440 apply(unfold tm.run_def, case_tac v1, auto) |
|
1441 done |
|
1442 from h have "i' = p" |
|
1443 by(sep_drule psD, simp) |
|
1444 with h |
|
1445 have "(r \<and>* st ?j \<and>* tm p v1 \<and>* ps (p + 1) \<and>* |
|
1446 sg {TC i ((R, ?j), R, ?j)}) (trset_of ?x)" |
|
1447 apply(unfold stp) |
|
1448 apply(sep_drule pos_upd, sep_drule st_upd, simp) |
|
1449 proof - |
|
1450 assume "(st ?j \<and>* ps (p + 1) \<and>* r \<and>* tm p v1 \<and>* sg {TC i ((R, ?j), R, ?j)}) |
|
1451 (trset_of (ft, prog, ?j, p + 1, mem))" |
|
1452 thus "(r \<and>* st ?j \<and>* tm p v1 \<and>* ps (p + 1) \<and>* sg {TC i ((R, ?j), R, ?j)}) |
|
1453 (trset_of (ft, prog, ?j, p + 1, mem))" |
|
1454 by (simp add: sep_conj_ac) |
|
1455 qed |
|
1456 thus "\<exists>k. (r \<and>* st ?j \<and>* tm p v1 \<and>* ps (p + 1) \<and>* sg {TC i ((R, ?j), R, ?j)}) |
|
1457 (trset_of (tm.run (Suc k) (ft, prog, cs, i', mem)))" |
|
1458 apply (rule_tac x = 0 in exI) |
|
1459 by auto |
|
1460 qed |
|
1461 qed |
|
1462 qed |
|
1463 |
|
1464 lemma hoare_move_right_gen[step]: |
|
1465 assumes "p = q" |
|
1466 shows "\<lbrace>st i ** ps p ** tm q v1 \<rbrace> |
|
1467 i:[move_right]:j |
|
1468 \<lbrace>st j ** ps (p + 1) ** tm q v1 \<rbrace>" |
|
1469 by (unfold assms, rule hoare_move_right) |
|
1470 |
|
1471 definition "if_one e = (TL exit . \<guillemotright>((W0, exit), (W1, e)); TLabel exit)" |
|
1472 |
|
1473 lemma hoare_if_one_true: |
|
1474 "\<lbrace>st i ** ps p ** one p\<rbrace> |
|
1475 i:[if_one e]:j |
|
1476 \<lbrace>st e ** ps p ** one p\<rbrace>" |
|
1477 proof(unfold if_one_def, intro t_hoare_local, rule t_hoare_label_last, simp+) |
|
1478 fix l |
|
1479 show " \<lbrace>st i \<and>* ps p \<and>* one p\<rbrace> i :[ \<guillemotright> ((W0, l), W1, e) ]: l \<lbrace>st e \<and>* ps p \<and>* one p\<rbrace>" |
|
1480 proof(unfold tassemble_to.simps, simp only:sep_conj_cond, |
|
1481 intro tm.code_condI, simp add: sep_conj_ac) |
|
1482 let ?j = "Suc i" |
|
1483 show "\<lbrace>one p \<and>* ps p \<and>* st i\<rbrace> sg {TC i ((W0, ?j), W1, e)} \<lbrace>one p \<and>* ps p \<and>* st e\<rbrace>" |
|
1484 proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify) |
|
1485 fix ft prog cs pc mem r |
|
1486 assume h: "(r \<and>* one p \<and>* ps p \<and>* st i \<and>* sg {TC i ((W0, ?j), W1, e)}) |
|
1487 (trset_of (ft, prog, cs, pc, mem))" |
|
1488 from h have k1: "prog i = Some ((W0, ?j), W1, e)" |
|
1489 apply(rule_tac r = "r \<and>* one p \<and>* ps p" in codeD) |
|
1490 by(simp add: sep_conj_ac) |
|
1491 from h have k2: "pc = p" |
|
1492 by(sep_drule psD, simp) |
|
1493 from h and this have k3: "mem pc = Some Oc" |
|
1494 apply(unfold one_def) |
|
1495 by(sep_drule memD, simp) |
|
1496 from h k1 k2 k3 have stp: |
|
1497 "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, e, pc, mem)" (is "?x = ?y") |
|
1498 apply(sep_drule stD) |
|
1499 by(unfold tm.run_def, auto) |
|
1500 from h k2 |
|
1501 have "(r \<and>* one p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, ?j), W1, e)}) (trset_of ?x)" |
|
1502 apply(unfold stp) |
|
1503 by(sep_drule st_upd, simp add: sep_conj_ac) |
|
1504 thus "\<exists>k.(r \<and>* one p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, ?j), W1, e)}) |
|
1505 (trset_of (tm.run (Suc k) (ft, prog, cs, pc, mem)))" |
|
1506 apply (rule_tac x = 0 in exI) |
|
1507 by auto |
|
1508 qed |
|
1509 qed |
|
1510 qed |
|
1511 |
|
1512 text {* |
|
1513 The following problematic lemma is not provable now |
|
1514 lemma hoare_self: "\<lbrace>p\<rbrace> i :[ap]: j \<lbrace>p\<rbrace>" |
|
1515 apply(simp add: tm.Hoare_gen_def, clarify) |
|
1516 apply(rule_tac x = 0 in exI, simp add: tm.run_def) |
|
1517 done |
|
1518 *} |
|
1519 |
|
1520 lemma hoare_if_one_true_gen[step]: |
|
1521 assumes "p = q" |
|
1522 shows |
|
1523 "\<lbrace>st i ** ps p ** one q\<rbrace> |
|
1524 i:[if_one e]:j |
|
1525 \<lbrace>st e ** ps p ** one q\<rbrace>" |
|
1526 by (unfold assms, rule hoare_if_one_true) |
|
1527 |
|
1528 lemma hoare_if_one_true1: |
|
1529 "\<lbrace>st i ** ps p ** one p\<rbrace> |
|
1530 i:[(if_one e; c)]:j |
|
1531 \<lbrace>st e ** ps p ** one p\<rbrace>" |
|
1532 proof(unfold tassemble_to.simps, rule tm.code_exI, |
|
1533 simp add: sep_conj_ac tm.Hoare_gen_def, clarify) |
|
1534 fix j' ft prog cs pos mem r |
|
1535 assume h: "(r \<and>* one p \<and>* ps p \<and>* st i \<and>* j' :[ c ]: j \<and>* i :[ if_one e ]: j') |
|
1536 (trset_of (ft, prog, cs, pos, mem))" |
|
1537 from tm.frame_rule[OF hoare_if_one_true] |
|
1538 have "\<And> r. \<lbrace>st i \<and>* ps p \<and>* one p \<and>* r\<rbrace> i :[ if_one e ]: j' \<lbrace>st e \<and>* ps p \<and>* one p \<and>* r\<rbrace>" |
|
1539 by(simp add: sep_conj_ac) |
|
1540 from this[unfolded tm.Hoare_gen_def tassemble_to.simps, rule_format, of "j' :[ c ]: j"] h |
|
1541 have "\<exists> k. (r \<and>* one p \<and>* ps p \<and>* st e \<and>* i :[ if_one e ]: j' \<and>* j' :[ c ]: j) |
|
1542 (trset_of (tm.run (Suc k) (ft, prog, cs, pos, mem)))" |
|
1543 by(auto simp: sep_conj_ac) |
|
1544 thus "\<exists>k. (r \<and>* one p \<and>* ps p \<and>* st e \<and>* j' :[ c ]: j \<and>* i :[ if_one e ]: j') |
|
1545 (trset_of (tm.run (Suc k) (ft, prog, cs, pos, mem)))" |
|
1546 by(simp add: sep_conj_ac) |
|
1547 qed |
|
1548 |
|
1549 lemma hoare_if_one_true1_gen[step]: |
|
1550 assumes "p = q" |
|
1551 shows |
|
1552 "\<lbrace>st i ** ps p ** one q\<rbrace> |
|
1553 i:[(if_one e; c)]:j |
|
1554 \<lbrace>st e ** ps p ** one q\<rbrace>" |
|
1555 by (unfold assms, rule hoare_if_one_true1) |
|
1556 |
|
1557 lemma hoare_if_one_false: |
|
1558 "\<lbrace>st i ** ps p ** zero p\<rbrace> |
|
1559 i:[if_one e]:j |
|
1560 \<lbrace>st j ** ps p ** zero p\<rbrace>" |
|
1561 proof(unfold if_one_def, intro t_hoare_local, rule t_hoare_label_last, simp+) |
|
1562 show "\<lbrace>st i \<and>* ps p \<and>* zero p\<rbrace> i :[ (\<guillemotright> ((W0, j), W1, e)) ]: j |
|
1563 \<lbrace>st j \<and>* ps p \<and>* zero p\<rbrace>" |
|
1564 proof(unfold tassemble_to.simps, simp only:sep_conj_cond, |
|
1565 intro tm.code_condI, simp add: sep_conj_ac) |
|
1566 let ?j = "Suc i" |
|
1567 show "\<lbrace>ps p \<and>* st i \<and>* zero p\<rbrace> sg {TC i ((W0, ?j), W1, e)} \<lbrace>ps p \<and>* zero p \<and>* st ?j \<rbrace>" |
|
1568 proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify) |
|
1569 fix ft prog cs pc mem r |
|
1570 assume h: "(r \<and>* ps p \<and>* st i \<and>* zero p \<and>* sg {TC i ((W0, ?j), W1, e)}) |
|
1571 (trset_of (ft, prog, cs, pc, mem))" |
|
1572 from h have k1: "prog i = Some ((W0, ?j), W1, e)" |
|
1573 apply(rule_tac r = "r \<and>* zero p \<and>* ps p" in codeD) |
|
1574 by(simp add: sep_conj_ac) |
|
1575 from h have k2: "pc = p" |
|
1576 by(sep_drule psD, simp) |
|
1577 from h and this have k3: "mem pc = Some Bk" |
|
1578 apply(unfold zero_def) |
|
1579 by(sep_drule memD, simp) |
|
1580 from h k1 k2 k3 have stp: |
|
1581 "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, ?j, pc, mem)" (is "?x = ?y") |
|
1582 apply(sep_drule stD) |
|
1583 by(unfold tm.run_def, auto) |
|
1584 from h k2 |
|
1585 have "(r \<and>* zero p \<and>* ps p \<and>* st ?j \<and>* sg {TC i ((W0, ?j), W1, e)}) (trset_of ?x)" |
|
1586 apply (unfold stp) |
|
1587 by (sep_drule st_upd[where i''="?j"], auto simp:sep_conj_ac) |
|
1588 thus "\<exists>k. (r \<and>* ps p \<and>* zero p \<and>* st ?j \<and>* sg {TC i ((W0, ?j), W1, e)}) |
|
1589 (trset_of (tm.run (Suc k) (ft, prog, cs, pc, mem)))" |
|
1590 by(auto simp: sep_conj_ac) |
|
1591 qed |
|
1592 qed |
|
1593 qed |
|
1594 |
|
1595 lemma hoare_if_one_false_gen[step]: |
|
1596 assumes "p = q" |
|
1597 shows "\<lbrace>st i ** ps p ** zero q\<rbrace> |
|
1598 i:[if_one e]:j |
|
1599 \<lbrace>st j ** ps p ** zero q\<rbrace>" |
|
1600 by (unfold assms, rule hoare_if_one_false) |
|
1601 |
|
1602 definition "if_zero e = (TL exit . \<guillemotright>((W0, e), (W1, exit)); TLabel exit)" |
|
1603 |
|
1604 lemma hoare_if_zero_true: |
|
1605 "\<lbrace>st i ** ps p ** zero p\<rbrace> |
|
1606 i:[if_zero e]:j |
|
1607 \<lbrace>st e ** ps p ** zero p\<rbrace>" |
|
1608 proof(unfold if_zero_def, intro t_hoare_local, rule t_hoare_label_last, simp+) |
|
1609 fix l |
|
1610 show "\<lbrace>st i \<and>* ps p \<and>* zero p\<rbrace> i :[ \<guillemotright> ((W0, e), W1, l) ]: l \<lbrace>st e \<and>* ps p \<and>* zero p\<rbrace>" |
|
1611 proof(unfold tassemble_to.simps, simp only:sep_conj_cond, |
|
1612 intro tm.code_condI, simp add: sep_conj_ac) |
|
1613 let ?j = "Suc i" |
|
1614 show "\<lbrace>ps p \<and>* st i \<and>* zero p\<rbrace> sg {TC i ((W0, e), W1, ?j)} \<lbrace>ps p \<and>* st e \<and>* zero p\<rbrace>" |
|
1615 proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify) |
|
1616 fix ft prog cs pc mem r |
|
1617 assume h: "(r \<and>* ps p \<and>* st i \<and>* zero p \<and>* sg {TC i ((W0, e), W1, ?j)}) |
|
1618 (trset_of (ft, prog, cs, pc, mem))" |
|
1619 from h have k1: "prog i = Some ((W0, e), W1, ?j)" |
|
1620 apply(rule_tac r = "r \<and>* zero p \<and>* ps p" in codeD) |
|
1621 by(simp add: sep_conj_ac) |
|
1622 from h have k2: "pc = p" |
|
1623 by(sep_drule psD, simp) |
|
1624 from h and this have k3: "mem pc = Some Bk" |
|
1625 apply(unfold zero_def) |
|
1626 by(sep_drule memD, simp) |
|
1627 from h k1 k2 k3 have stp: |
|
1628 "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, e, pc, mem)" (is "?x = ?y") |
|
1629 apply(sep_drule stD) |
|
1630 by(unfold tm.run_def, auto) |
|
1631 from h k2 |
|
1632 have "(r \<and>* zero p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, e), W1, ?j)}) (trset_of ?x)" |
|
1633 apply(unfold stp) |
|
1634 by(sep_drule st_upd, simp add: sep_conj_ac) |
|
1635 thus "\<exists>k. (r \<and>* ps p \<and>* st e \<and>* zero p \<and>* sg {TC i ((W0, e), W1, ?j)}) |
|
1636 (trset_of (tm.run (Suc k) (ft, prog, cs, pc, mem)))" |
|
1637 by(auto simp: sep_conj_ac) |
|
1638 qed |
|
1639 qed |
|
1640 qed |
|
1641 |
|
1642 lemma hoare_if_zero_true_gen[step]: |
|
1643 assumes "p = q" |
|
1644 shows |
|
1645 "\<lbrace>st i ** ps p ** zero q\<rbrace> |
|
1646 i:[if_zero e]:j |
|
1647 \<lbrace>st e ** ps p ** zero q\<rbrace>" |
|
1648 by (unfold assms, rule hoare_if_zero_true) |
|
1649 |
|
1650 lemma hoare_if_zero_true1: |
|
1651 "\<lbrace>st i ** ps p ** zero p\<rbrace> |
|
1652 i:[(if_zero e; c)]:j |
|
1653 \<lbrace>st e ** ps p ** zero p\<rbrace>" |
|
1654 proof(unfold tassemble_to.simps, rule tm.code_exI, simp add: sep_conj_ac |
|
1655 tm.Hoare_gen_def, clarify) |
|
1656 fix j' ft prog cs pos mem r |
|
1657 assume h: "(r \<and>* ps p \<and>* st i \<and>* zero p \<and>* j' :[ c ]: j \<and>* i :[ if_zero e ]: j') |
|
1658 (trset_of (ft, prog, cs, pos, mem))" |
|
1659 from tm.frame_rule[OF hoare_if_zero_true] |
|
1660 have "\<And> r. \<lbrace>st i \<and>* ps p \<and>* zero p \<and>* r\<rbrace> i :[ if_zero e ]: j' \<lbrace>st e \<and>* ps p \<and>* zero p \<and>* r\<rbrace>" |
|
1661 by(simp add: sep_conj_ac) |
|
1662 from this[unfolded tm.Hoare_gen_def tassemble_to.simps, rule_format, of "j' :[ c ]: j"] h |
|
1663 have "\<exists> k. (r \<and>* zero p \<and>* ps p \<and>* st e \<and>* i :[ if_zero e ]: j' \<and>* j' :[ c ]: j) |
|
1664 (trset_of (tm.run (Suc k) (ft, prog, cs, pos, mem)))" |
|
1665 by(auto simp: sep_conj_ac) |
|
1666 thus "\<exists>k. (r \<and>* ps p \<and>* st e \<and>* zero p \<and>* j' :[ c ]: j \<and>* i :[ if_zero e ]: j') |
|
1667 (trset_of (tm.run (Suc k) (ft, prog, cs, pos, mem)))" |
|
1668 by(simp add: sep_conj_ac) |
|
1669 qed |
|
1670 |
|
1671 lemma hoare_if_zero_true1_gen[step]: |
|
1672 assumes "p = q" |
|
1673 shows |
|
1674 "\<lbrace>st i ** ps p ** zero q\<rbrace> |
|
1675 i:[(if_zero e; c)]:j |
|
1676 \<lbrace>st e ** ps p ** zero q\<rbrace>" |
|
1677 by (unfold assms, rule hoare_if_zero_true1) |
|
1678 |
|
1679 lemma hoare_if_zero_false: |
|
1680 "\<lbrace>st i ** ps p ** tm p Oc\<rbrace> |
|
1681 i:[if_zero e]:j |
|
1682 \<lbrace>st j ** ps p ** tm p Oc\<rbrace>" |
|
1683 proof(unfold if_zero_def, intro t_hoare_local, rule t_hoare_label_last, simp, simp) |
|
1684 fix l |
|
1685 show "\<lbrace>st i \<and>* ps p \<and>* tm p Oc\<rbrace> i :[ \<guillemotright> ((W0, e), W1, l) ]: l |
|
1686 \<lbrace>st l \<and>* ps p \<and>* tm p Oc\<rbrace>" |
|
1687 proof(unfold tassemble_to.simps, simp only:sep_conj_cond, |
|
1688 intro tm.code_condI, simp add: sep_conj_ac) |
|
1689 let ?j = "Suc i" |
|
1690 show "\<lbrace>ps p \<and>* st i \<and>* tm p Oc\<rbrace> sg {TC i ((W0, e), W1, ?j)} |
|
1691 \<lbrace>ps p \<and>* st ?j \<and>* tm p Oc\<rbrace>" |
|
1692 proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify) |
|
1693 fix ft prog cs pc mem r |
|
1694 assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p Oc \<and>* sg {TC i ((W0, e), W1, ?j)}) |
|
1695 (trset_of (ft, prog, cs, pc, mem))" |
|
1696 from h have k1: "prog i = Some ((W0, e), W1, ?j)" |
|
1697 apply(rule_tac r = "r \<and>* tm p Oc \<and>* ps p" in codeD) |
|
1698 by(simp add: sep_conj_ac) |
|
1699 from h have k2: "pc = p" |
|
1700 by(sep_drule psD, simp) |
|
1701 from h and this have k3: "mem pc = Some Oc" |
|
1702 by(sep_drule memD, simp) |
|
1703 from h k1 k2 k3 have stp: |
|
1704 "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, ?j, pc, mem)" (is "?x = ?y") |
|
1705 apply(sep_drule stD) |
|
1706 by(unfold tm.run_def, auto) |
|
1707 from h k2 |
|
1708 have "(r \<and>* tm p Oc \<and>* ps p \<and>* st ?j \<and>* sg {TC i ((W0, e), W1, ?j)}) (trset_of ?x)" |
|
1709 apply(unfold stp) |
|
1710 by(sep_drule st_upd, simp add: sep_conj_ac) |
|
1711 thus "\<exists>k. (r \<and>* ps p \<and>* st ?j \<and>* tm p Oc \<and>* sg {TC i ((W0, e), W1, ?j)}) |
|
1712 (trset_of (tm.run (Suc k) (ft, prog, cs, pc, mem)))" |
|
1713 by(auto simp: sep_conj_ac) |
|
1714 qed |
|
1715 qed |
|
1716 qed |
|
1717 |
|
1718 lemma hoare_if_zero_false_gen[step]: |
|
1719 assumes "p = q" |
|
1720 shows |
|
1721 "\<lbrace>st i ** ps p ** tm q Oc\<rbrace> |
|
1722 i:[if_zero e]:j |
|
1723 \<lbrace>st j ** ps p ** tm q Oc\<rbrace>" |
|
1724 by (unfold assms, rule hoare_if_zero_false) |
|
1725 |
|
1726 |
|
1727 definition "jmp e = \<guillemotright>((W0, e), (W1, e))" |
|
1728 |
|
1729 lemma hoare_jmp: |
|
1730 "\<lbrace>st i \<and>* ps p \<and>* tm p v\<rbrace> i:[jmp e]:j \<lbrace>st e \<and>* ps p \<and>* tm p v\<rbrace>" |
|
1731 proof(unfold jmp_def tm.Hoare_gen_def tassemble_to.simps sep_conj_ac, clarify) |
|
1732 fix ft prog cs pos mem r |
|
1733 assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* <(j = i + 1)> \<and>* sg {TC i ((W0, e), W1, e)}) |
|
1734 (trset_of (ft, prog, cs, pos, mem))" |
|
1735 from h have k1: "prog i = Some ((W0, e), W1, e)" |
|
1736 apply(rule_tac r = "r \<and>* <(j = i + 1)> \<and>* tm p v \<and>* ps p" in codeD) |
|
1737 by(simp add: sep_conj_ac) |
|
1738 from h have k2: "p = pos" |
|
1739 by(sep_drule psD, simp) |
|
1740 from h k2 have k3: "mem pos = Some v" |
|
1741 by(sep_drule memD, simp) |
|
1742 from h k1 k2 k3 have |
|
1743 stp: "tm.run 1 (ft, prog, cs, pos, mem) = (ft, prog, e, pos, mem)" (is "?x = ?y") |
|
1744 apply(sep_drule stD) |
|
1745 by(unfold tm.run_def, cases "mem pos", simp, cases v, auto) |
|
1746 from h k2 |
|
1747 have "(r \<and>* ps p \<and>* st e \<and>* tm p v \<and>* <(j = i + 1)> \<and>* |
|
1748 sg {TC i ((W0, e), W1, e)}) (trset_of ?x)" |
|
1749 apply(unfold stp) |
|
1750 by(sep_drule st_upd, simp add: sep_conj_ac) |
|
1751 thus "\<exists> k. (r \<and>* ps p \<and>* st e \<and>* tm p v \<and>* <(j = i + 1)> \<and>* sg {TC i ((W0, e), W1, e)}) |
|
1752 (trset_of (tm.run (Suc k) (ft, prog, cs, pos, mem)))" |
|
1753 apply (rule_tac x = 0 in exI) |
|
1754 by auto |
|
1755 qed |
|
1756 |
|
1757 lemma hoare_jmp_gen[step]: |
|
1758 assumes "p = q" |
|
1759 shows "\<lbrace>st i \<and>* ps p \<and>* tm q v\<rbrace> i:[jmp e]:j \<lbrace>st e \<and>* ps p \<and>* tm q v\<rbrace>" |
|
1760 by (unfold assms, rule hoare_jmp) |
|
1761 |
|
1762 lemma hoare_jmp1: |
|
1763 "\<lbrace>st i \<and>* ps p \<and>* tm p v\<rbrace> |
|
1764 i:[(jmp e; c)]:j |
|
1765 \<lbrace>st e \<and>* ps p \<and>* tm p v\<rbrace>" |
|
1766 proof(unfold tassemble_to.simps, rule tm.code_exI, simp |
|
1767 add: sep_conj_ac tm.Hoare_gen_def, clarify) |
|
1768 fix j' ft prog cs pos mem r |
|
1769 assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* j' :[ c ]: j \<and>* i :[ jmp e ]: j') |
|
1770 (trset_of (ft, prog, cs, pos, mem))" |
|
1771 from tm.frame_rule[OF hoare_jmp] |
|
1772 have "\<And> r. \<lbrace>st i \<and>* ps p \<and>* tm p v \<and>* r\<rbrace> i :[ jmp e ]: j' \<lbrace>st e \<and>* ps p \<and>* tm p v \<and>* r\<rbrace>" |
|
1773 by(simp add: sep_conj_ac) |
|
1774 from this[unfolded tm.Hoare_gen_def tassemble_to.simps, rule_format, of "j' :[ c ]: j"] h |
|
1775 have "\<exists> k. (r \<and>* tm p v \<and>* ps p \<and>* st e \<and>* i :[ jmp e ]: j' \<and>* j' :[ c ]: j) |
|
1776 (trset_of (tm.run (Suc k) (ft, prog, cs, pos, mem)))" |
|
1777 by(auto simp: sep_conj_ac) |
|
1778 thus "\<exists>k. (r \<and>* ps p \<and>* st e \<and>* tm p v \<and>* j' :[ c ]: j \<and>* i :[ jmp e ]: j') |
|
1779 (trset_of (tm.run (Suc k) (ft, prog, cs, pos, mem)))" |
|
1780 by(simp add: sep_conj_ac) |
|
1781 qed |
|
1782 |
|
1783 |
|
1784 lemma hoare_jmp1_gen[step]: |
|
1785 assumes "p = q" |
|
1786 shows "\<lbrace>st i \<and>* ps p \<and>* tm q v\<rbrace> |
|
1787 i:[(jmp e; c)]:j |
|
1788 \<lbrace>st e \<and>* ps p \<and>* tm q v\<rbrace>" |
|
1789 by (unfold assms, rule hoare_jmp1) |
|
1790 |
|
1791 |
|
1792 lemma condI: |
|
1793 assumes h1: b |
|
1794 and h2: "b \<Longrightarrow> p s" |
|
1795 shows "(<b> \<and>* p) s" |
|
1796 by (metis (full_types) cond_true_eq1 h1 h2) |
|
1797 |
|
1798 lemma condE: |
|
1799 assumes "(<b> \<and>* p) s" |
|
1800 obtains "b" and "p s" |
|
1801 proof(atomize_elim) |
|
1802 from condD[OF assms] |
|
1803 show "b \<and> p s" . |
|
1804 qed |
|
1805 |
|
1806 |
|
1807 section {* Tactics *} |
|
1808 |
|
1809 ML {* |
|
1810 val trace_step = Attrib.setup_config_bool @{binding trace_step} (K false) |
|
1811 val trace_fwd = Attrib.setup_config_bool @{binding trace_fwd} (K false) |
|
1812 *} |
|
1813 |
|
1814 |
|
1815 ML {* |
|
1816 val tracing = (fn ctxt => fn str => |
|
1817 if (Config.get ctxt trace_step) then tracing str else ()) |
|
1818 fun not_pred p = fn s => not (p s) |
|
1819 fun break_sep_conj (Const (@{const_name sep_conj},_) $ t1 $ t2 $ _) = |
|
1820 (break_sep_conj t1) @ (break_sep_conj t2) |
|
1821 | break_sep_conj (Const (@{const_name sep_conj},_) $ t1 $ t2) = |
|
1822 (break_sep_conj t1) @ (break_sep_conj t2) |
|
1823 (* dig through eta exanded terms: *) |
|
1824 | break_sep_conj (Abs (_, _, t $ Bound 0)) = break_sep_conj t |
|
1825 | break_sep_conj t = [t]; |
|
1826 |
|
1827 val empty_env = (Vartab.empty, Vartab.empty) |
|
1828 |
|
1829 fun match_env ctxt pat trm env = |
|
1830 Pattern.match (ctxt |> Proof_Context.theory_of) (pat, trm) env |
|
1831 |
|
1832 fun match ctxt pat trm = match_env ctxt pat trm empty_env; |
|
1833 |
|
1834 val inst = Envir.subst_term; |
|
1835 |
|
1836 fun term_of_thm thm = thm |> prop_of |> HOLogic.dest_Trueprop |
|
1837 |
|
1838 fun get_cmd ctxt code = |
|
1839 let val pat = term_of @{cpat "_:[(?cmd)]:_"} |
|
1840 val pat1 = term_of @{cpat "?cmd::tpg"} |
|
1841 val env = match ctxt pat code |
|
1842 in inst env pat1 end |
|
1843 |
|
1844 fun is_seq_term (Const (@{const_name TSeq}, _) $ _ $ _) = true |
|
1845 | is_seq_term _ = false |
|
1846 |
|
1847 fun get_hcmd (Const (@{const_name TSeq}, _) $ hcmd $ _) = hcmd |
|
1848 | get_hcmd hcmd = hcmd |
|
1849 |
|
1850 fun last [a] = a | |
|
1851 last (a::b) = last b |
|
1852 |
|
1853 fun but_last [a] = [] | |
|
1854 but_last (a::b) = a::(but_last b) |
|
1855 |
|
1856 fun foldr f [] = (fn x => x) | |
|
1857 foldr f (x :: xs) = (f x) o (foldr f xs) |
|
1858 |
|
1859 fun concat [] = [] | |
|
1860 concat (x :: xs) = x @ concat xs |
|
1861 |
|
1862 fun match_any ctxt pats tm = |
|
1863 fold |
|
1864 (fn pat => fn b => (b orelse Pattern.matches |
|
1865 (ctxt |> Proof_Context.theory_of) (pat, tm))) |
|
1866 pats false |
|
1867 |
|
1868 fun is_ps_term (Const (@{const_name ps}, _) $ _) = true |
|
1869 | is_ps_term _ = false |
|
1870 |
|
1871 fun string_of_term ctxt t = t |> Syntax.pretty_term ctxt |> Pretty.str_of |
|
1872 fun string_of_cterm ctxt ct = ct |> term_of |> string_of_term ctxt |
|
1873 fun pterm ctxt t = |
|
1874 t |> string_of_term ctxt |> tracing ctxt |
|
1875 fun pcterm ctxt ct = ct |> string_of_cterm ctxt |> tracing ctxt |
|
1876 fun string_for_term ctxt t = |
|
1877 Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN) |
|
1878 (print_mode_value ())) (Syntax.string_of_term ctxt) t |
|
1879 |> String.translate (fn c => if Char.isPrint c then str c else "") |
|
1880 |> Sledgehammer_Util.simplify_spaces |
|
1881 fun string_for_cterm ctxt ct = ct |> term_of |> string_for_term ctxt |
|
1882 fun attemp tac = fn i => fn st => (tac i st) handle exn => Seq.empty |
|
1883 fun try_tac tac = fn i => fn st => (tac i st) handle exn => (Seq.single st) |
|
1884 (* aux end *) |
|
1885 *} |
|
1886 |
|
1887 ML {* (* Functions specific to Hoare triples *) |
|
1888 fun get_pre ctxt t = |
|
1889 let val pat = term_of @{cpat "\<lbrace>?P\<rbrace> ?c \<lbrace>?Q\<rbrace>"} |
|
1890 val env = match ctxt pat t |
|
1891 in inst env (term_of @{cpat "?P::tresource set \<Rightarrow> bool"}) end |
|
1892 |
|
1893 fun can_process ctxt t = ((get_pre ctxt t; true) handle _ => false) |
|
1894 |
|
1895 fun get_post ctxt t = |
|
1896 let val pat = term_of @{cpat "\<lbrace>?P\<rbrace> ?c \<lbrace>?Q\<rbrace>"} |
|
1897 val env = match ctxt pat t |
|
1898 in inst env (term_of @{cpat "?Q::tresource set \<Rightarrow> bool"}) end; |
|
1899 |
|
1900 fun get_mid ctxt t = |
|
1901 let val pat = term_of @{cpat "\<lbrace>?P\<rbrace> ?c \<lbrace>?Q\<rbrace>"} |
|
1902 val env = match ctxt pat t |
|
1903 in inst env (term_of @{cpat "?c::tresource set \<Rightarrow> bool"}) end; |
|
1904 |
|
1905 fun is_pc_term (Const (@{const_name st}, _) $ _) = true |
|
1906 | is_pc_term _ = false |
|
1907 |
|
1908 fun mk_pc_term x = |
|
1909 Const (@{const_name st}, @{typ "nat \<Rightarrow> tresource set \<Rightarrow> bool"}) $ Free (x, @{typ "nat"}) |
|
1910 |
|
1911 val sconj_term = term_of @{cterm "sep_conj::tassert \<Rightarrow> tassert \<Rightarrow> tassert"} |
|
1912 |
|
1913 fun mk_ps_term x = |
|
1914 Const (@{const_name ps}, @{typ "int \<Rightarrow> tresource set \<Rightarrow> bool"}) $ Free (x, @{typ "int"}) |
|
1915 |
|
1916 fun atomic tac = ((SOLVED' tac) ORELSE' (K all_tac)) |
|
1917 |
|
1918 fun pure_sep_conj_ac_tac ctxt = |
|
1919 (auto_tac (ctxt |> Simplifier.map_simpset (fn ss => ss addsimps @{thms sep_conj_ac})) |
|
1920 |> SELECT_GOAL) |
|
1921 |
|
1922 |
|
1923 fun potential_facts ctxt prop = Facts.could_unify (Proof_Context.facts_of ctxt) |
|
1924 ((Term.strip_all_body prop) |> Logic.strip_imp_concl); |
|
1925 |
|
1926 fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) => |
|
1927 (Method.insert_tac (potential_facts ctxt goal) i) THEN |
|
1928 (pure_sep_conj_ac_tac ctxt i)); |
|
1929 |
|
1930 fun sep_conj_ac_tac ctxt = |
|
1931 (SOLVED' (auto_tac (ctxt |> Simplifier.map_simpset (fn ss => ss addsimps @{thms sep_conj_ac})) |
|
1932 |> SELECT_GOAL)) ORELSE' (atomic (some_fact_tac ctxt)) |
|
1933 *} |
|
1934 |
|
1935 ML {* |
|
1936 type HoareTriple = { |
|
1937 binding: binding, |
|
1938 can_process: Proof.context -> term -> bool, |
|
1939 get_pre: Proof.context -> term -> term, |
|
1940 get_mid: Proof.context -> term -> term, |
|
1941 get_post: Proof.context -> term -> term, |
|
1942 is_pc_term: term -> bool, |
|
1943 mk_pc_term: string -> term, |
|
1944 sconj_term: term, |
|
1945 sep_conj_ac_tac: Proof.context -> int -> tactic, |
|
1946 hoare_seq1: thm, |
|
1947 hoare_seq2: thm, |
|
1948 pre_stren: thm, |
|
1949 post_weaken: thm, |
|
1950 frame_rule: thm |
|
1951 } |
|
1952 |
|
1953 val tm_triple = {binding = @{binding "tm_triple"}, |
|
1954 can_process = can_process, |
|
1955 get_pre = get_pre, |
|
1956 get_mid = get_mid, |
|
1957 get_post = get_post, |
|
1958 is_pc_term = is_pc_term, |
|
1959 mk_pc_term = mk_pc_term, |
|
1960 sconj_term = sconj_term, |
|
1961 sep_conj_ac_tac = sep_conj_ac_tac, |
|
1962 hoare_seq1 = @{thm t_hoare_seq1}, |
|
1963 hoare_seq2 = @{thm t_hoare_seq2}, |
|
1964 pre_stren = @{thm tm.pre_stren}, |
|
1965 post_weaken = @{thm tm.post_weaken}, |
|
1966 frame_rule = @{thm tm.frame_rule} |
|
1967 }:HoareTriple |
|
1968 *} |
|
1969 |
|
1970 ML {* |
|
1971 val _ = data_slot "HoareTriples" "HoareTriple list" "[]" |
|
1972 *} |
|
1973 |
|
1974 ML {* |
|
1975 val _ = HoareTriples_store [tm_triple] |
|
1976 *} |
|
1977 |
|
1978 ML {* (* aux1 functions *) |
|
1979 |
|
1980 fun focus_params t ctxt = |
|
1981 let |
|
1982 val (xs, Ts) = |
|
1983 split_list (Term.variant_frees t (Term.strip_all_vars t)); (*as they are printed :-*) |
|
1984 (* val (xs', ctxt') = variant_fixes xs ctxt; *) |
|
1985 (* val ps = xs' ~~ Ts; *) |
|
1986 val ps = xs ~~ Ts |
|
1987 val (_, ctxt'') = ctxt |> Variable.add_fixes xs |
|
1988 in ((xs, ps), ctxt'') end |
|
1989 |
|
1990 fun focus_concl ctxt t = |
|
1991 let |
|
1992 val ((xs, ps), ctxt') = focus_params t ctxt |
|
1993 val t' = Term.subst_bounds (rev (map Free ps), Term.strip_all_body t); |
|
1994 in (t' |> Logic.strip_imp_concl, ctxt') end |
|
1995 |
|
1996 fun get_concl ctxt (i, state) = |
|
1997 nth (Thm.prems_of state) (i - 1) |
|
1998 |> focus_concl ctxt |> (fn (x, _) => x |> HOLogic.dest_Trueprop) |
|
1999 (* aux1 end *) |
|
2000 *} |
|
2001 |
|
2002 ML {* |
|
2003 fun indexing xs = upto (0, length xs - 1) ~~ xs |
|
2004 fun select_idxs idxs ps = |
|
2005 map_index (fn (i, e) => if (member (op =) idxs i) then [e] else []) ps |> flat |
|
2006 fun select_out_idxs idxs ps = |
|
2007 map_index (fn (i, e) => if (member (op =) idxs i) then [] else [e]) ps |> flat |
|
2008 fun match_pres ctxt mf env ps qs = |
|
2009 let fun sel_match mf env [] qs = [(env, [])] |
|
2010 | sel_match mf env (p::ps) qs = |
|
2011 let val pm = map (fn (i, q) => [(i, |
|
2012 let val _ = tracing ctxt "Matching:" |
|
2013 val _ = [p, q] |> |
|
2014 (pretty_terms ctxt) |> Pretty.str_of |> tracing ctxt |
|
2015 val r = mf p q env |
|
2016 in r end)] |
|
2017 handle _ => ( |
|
2018 let val _ = tracing ctxt "Failed matching:" |
|
2019 val _ = [p, q] |> |
|
2020 (pretty_terms ctxt) |> Pretty.str_of |> tracing ctxt |
|
2021 in [] end)) qs |> flat |
|
2022 val r = pm |> map (fn (i, env') => |
|
2023 let val qs' = filter_out (fn (j, q) => j = i) qs |
|
2024 in sel_match mf env' ps qs' |> |
|
2025 map (fn (env'', idxs) => (env'', i::idxs)) end) |
|
2026 |> flat |
|
2027 in r end |
|
2028 in sel_match mf env ps (indexing qs) end |
|
2029 |
|
2030 fun provable tac ctxt goal = |
|
2031 let |
|
2032 val _ = tracing ctxt "Provable trying to prove:" |
|
2033 val _ = [goal] |> (pretty_terms ctxt) |> Pretty.str_of |> tracing ctxt |
|
2034 in |
|
2035 (Goal.prove ctxt [] [] goal (fn {context, ...} => tac context 1); true) |
|
2036 handle exn => false |
|
2037 end |
|
2038 fun make_sense tac ctxt thm_assms env = |
|
2039 thm_assms |> map (inst env) |> forall (provable tac ctxt) |
|
2040 *} |
|
2041 |
|
2042 ML {* |
|
2043 fun triple_for ctxt goal = |
|
2044 filter (fn trpl => (#can_process trpl) ctxt goal) (HoareTriples.get (Proof_Context.theory_of ctxt)) |> hd |
|
2045 |
|
2046 fun step_terms_for thm goal ctxt = |
|
2047 let |
|
2048 val _ = tracing ctxt "This is the new version of step_terms_for!" |
|
2049 val _ = tracing ctxt "Tring to find triple processor: TP" |
|
2050 val TP = triple_for ctxt goal |
|
2051 val _ = #binding TP |> Binding.name_of |> tracing ctxt |
|
2052 fun mk_sep_conj tms = foldr (fn tm => fn rtm => |
|
2053 ((#sconj_term TP)$tm$rtm)) (but_last tms) (last tms) |
|
2054 val thm_concl = thm |> prop_of |
|
2055 |> Logic.strip_imp_concl |> HOLogic.dest_Trueprop |
|
2056 val thm_assms = thm |> prop_of |
|
2057 |> Logic.strip_imp_prems |
|
2058 val cmd_pat = thm_concl |> #get_mid TP ctxt |> get_cmd ctxt |
|
2059 val cmd = goal |> #get_mid TP ctxt |> get_cmd ctxt |
|
2060 val _ = tracing ctxt "matching command ... " |
|
2061 val _ = tracing ctxt "cmd_pat = " |
|
2062 val _ = pterm ctxt cmd_pat |
|
2063 val (hcmd, env1, is_last) = (cmd, match ctxt cmd_pat cmd, true) |
|
2064 handle exn => (cmd |> get_hcmd, match ctxt cmd_pat (cmd |> get_hcmd), false) |
|
2065 val _ = tracing ctxt "hcmd =" |
|
2066 val _ = pterm ctxt hcmd |
|
2067 val _ = tracing ctxt "match command succeed! " |
|
2068 val _ = tracing ctxt "pres =" |
|
2069 val pres = goal |> #get_pre TP ctxt |> break_sep_conj |
|
2070 val _ = pres |> (pretty_terms ctxt) |> Pretty.str_of |> tracing ctxt |
|
2071 val _ = tracing ctxt "pre_pats =" |
|
2072 val pre_pats = thm_concl |> #get_pre TP ctxt |> inst env1 |> break_sep_conj |
|
2073 val _ = pre_pats |> (pretty_terms ctxt) |> Pretty.str_of |> tracing ctxt |
|
2074 val _ = tracing ctxt "post_pats =" |
|
2075 val post_pats = thm_concl |> #get_post TP ctxt |> inst env1 |> break_sep_conj |
|
2076 val _ = post_pats |> (pretty_terms ctxt) |> Pretty.str_of |> tracing ctxt |
|
2077 val _ = tracing ctxt "Calculating sols" |
|
2078 val sols = match_pres ctxt (match_env ctxt) env1 pre_pats pres |
|
2079 val _ = tracing ctxt "End calculating sols, sols =" |
|
2080 val _ = tracing ctxt (@{make_string} sols) |
|
2081 val _ = tracing ctxt "Calulating env2 and idxs" |
|
2082 val (env2, idxs) = filter (fn (env, idxs) => make_sense (#sep_conj_ac_tac TP) |
|
2083 ctxt thm_assms env) sols |> hd |
|
2084 val _ = tracing ctxt "End calculating env2 and idxs" |
|
2085 val _ = tracing ctxt "mterms =" |
|
2086 val mterms = select_idxs idxs pres |> map (inst env2) |
|
2087 val _ = mterms |> (pretty_terms ctxt) |> Pretty.str_of |> tracing ctxt |
|
2088 val _ = tracing ctxt "nmterms = " |
|
2089 val nmterms = select_out_idxs idxs pres |> map (inst env2) |
|
2090 val _ = nmterms |> (pretty_terms ctxt) |> Pretty.str_of |> tracing ctxt |
|
2091 val pre_cond = pre_pats |> map (inst env2) |> mk_sep_conj |
|
2092 val post_cond = post_pats |> map (inst env2) |> mk_sep_conj |
|
2093 val post_cond_npc = |
|
2094 post_cond |> break_sep_conj |> filter (not_pred (#is_pc_term TP)) |
|
2095 |> (fn x => x @ nmterms) |> mk_sep_conj |> cterm_of (Proof_Context.theory_of ctxt) |
|
2096 fun mk_frame cond rest = |
|
2097 if rest = [] then cond else ((#sconj_term TP)$ cond) $ (mk_sep_conj rest) |
|
2098 val pre_cond_frame = mk_frame pre_cond nmterms |> cterm_of (Proof_Context.theory_of ctxt) |
|
2099 fun post_cond_frame j' = post_cond |> break_sep_conj |> filter (not_pred (#is_pc_term TP)) |
|
2100 |> (fn x => [#mk_pc_term TP j']@x) |> mk_sep_conj |
|
2101 |> (fn x => mk_frame x nmterms) |
|
2102 |> cterm_of (Proof_Context.theory_of ctxt) |
|
2103 val need_frame = (nmterms <> []) |
|
2104 in |
|
2105 (post_cond_npc, |
|
2106 pre_cond_frame, |
|
2107 post_cond_frame, need_frame, is_last) |
|
2108 end |
|
2109 *} |
|
2110 |
|
2111 ML {* |
|
2112 fun step_tac ctxt thm i state = |
|
2113 let |
|
2114 val _ = tracing ctxt "This is the new version of step_tac" |
|
2115 val (goal, ctxt) = nth (Thm.prems_of state) (i - 1) |
|
2116 |> focus_concl ctxt |
|
2117 |> (apfst HOLogic.dest_Trueprop) |
|
2118 val _ = tracing ctxt "step_tac: goal = " |
|
2119 val _ = goal |> pterm ctxt |
|
2120 val _ = tracing ctxt "Start to calculate intermediate terms ... " |
|
2121 val (post_cond_npc, pre_cond_frame, post_cond_frame, need_frame, is_last) |
|
2122 = step_terms_for thm goal ctxt |
|
2123 val _ = tracing ctxt "Tring to find triple processor: TP" |
|
2124 val TP = triple_for ctxt goal |
|
2125 val _ = #binding TP |> Binding.name_of |> tracing ctxt |
|
2126 fun mk_sep_conj tms = foldr (fn tm => fn rtm => |
|
2127 ((#sconj_term TP)$tm$rtm)) (but_last tms) (last tms) |
|
2128 val _ = tracing ctxt "Calculate intermediate terms finished! " |
|
2129 val post_cond_npc_str = post_cond_npc |> string_for_cterm ctxt |
|
2130 val pre_cond_frame_str = pre_cond_frame |> string_for_cterm ctxt |
|
2131 val _ = tracing ctxt "step_tac: post_cond_npc = " |
|
2132 val _ = post_cond_npc |> pcterm ctxt |
|
2133 val _ = tracing ctxt "step_tac: pre_cond_frame = " |
|
2134 val _ = pre_cond_frame |> pcterm ctxt |
|
2135 fun tac1 i state = |
|
2136 if is_last then (K all_tac) i state else |
|
2137 res_inst_tac ctxt [(("q", 0), post_cond_npc_str)] |
|
2138 (#hoare_seq1 TP) i state |
|
2139 fun tac2 i state = res_inst_tac ctxt [(("p", 0), pre_cond_frame_str)] |
|
2140 (#pre_stren TP) i state |
|
2141 fun foc_tac post_cond_frame ctxt i state = |
|
2142 let |
|
2143 val goal = get_concl ctxt (i, state) |
|
2144 val pc_term = goal |> #get_post TP ctxt |> break_sep_conj |
|
2145 |> filter (#is_pc_term TP) |> hd |
|
2146 val (_$Free(j', _)) = pc_term |
|
2147 val psd = post_cond_frame j' |
|
2148 val str_psd = psd |> string_for_cterm ctxt |
|
2149 val _ = tracing ctxt "foc_tac: psd = " |
|
2150 val _ = psd |> pcterm ctxt |
|
2151 in |
|
2152 res_inst_tac ctxt [(("q", 0), str_psd)] |
|
2153 (#post_weaken TP) i state |
|
2154 end |
|
2155 val frame_tac = if need_frame then (rtac (#frame_rule TP)) else (K all_tac) |
|
2156 val print_tac = if (Config.get ctxt trace_step) then Tactical.print_tac else (K all_tac) |
|
2157 val tac = (tac1 THEN' (K (print_tac "tac1 success"))) THEN' |
|
2158 (tac2 THEN' (K (print_tac "tac2 success"))) THEN' |
|
2159 ((foc_tac post_cond_frame ctxt) THEN' (K (print_tac "foc_tac success"))) THEN' |
|
2160 (frame_tac THEN' (K (print_tac "frame_tac success"))) THEN' |
|
2161 (((rtac thm) THEN_ALL_NEW (#sep_conj_ac_tac TP ctxt)) THEN' (K (print_tac "rtac thm success"))) THEN' |
|
2162 (K (ALLGOALS (atomic (#sep_conj_ac_tac TP ctxt)))) THEN' |
|
2163 (* (#sep_conj_ac_tac TP ctxt) THEN' (#sep_conj_ac_tac TP ctxt) THEN' *) |
|
2164 (K prune_params_tac) |
|
2165 in |
|
2166 tac i state |
|
2167 end |
|
2168 |
|
2169 fun unfold_cell_tac ctxt = (Local_Defs.unfold_tac ctxt @{thms one_def zero_def}) |
|
2170 fun fold_cell_tac ctxt = (Local_Defs.fold_tac ctxt @{thms one_def zero_def}) |
|
2171 *} |
|
2172 |
|
2173 ML {* |
|
2174 fun sg_step_tac thms ctxt = |
|
2175 let val sg_step_tac' = (map (fn thm => attemp (step_tac ctxt thm)) thms) |
|
2176 (* @ [attemp (goto_tac ctxt)] *) |
|
2177 |> FIRST' |
|
2178 val sg_step_tac'' = (K (unfold_cell_tac ctxt)) THEN' sg_step_tac' THEN' (K (fold_cell_tac ctxt)) |
|
2179 in |
|
2180 sg_step_tac' ORELSE' sg_step_tac'' |
|
2181 end |
|
2182 fun steps_tac thms ctxt i = REPEAT (sg_step_tac thms ctxt i) THEN (prune_params_tac) |
|
2183 *} |
|
2184 |
|
2185 method_setup hstep = {* |
|
2186 Attrib.thms >> (fn thms => fn ctxt => |
|
2187 (SIMPLE_METHOD' (fn i => |
|
2188 sg_step_tac (thms@(StepRules.get ctxt)) ctxt i))) |
|
2189 *} |
|
2190 "One step symbolic execution using step theorems." |
|
2191 |
|
2192 method_setup hsteps = {* |
|
2193 Attrib.thms >> (fn thms => fn ctxt => |
|
2194 (SIMPLE_METHOD' (fn i => |
|
2195 steps_tac (thms@(StepRules.get ctxt)) ctxt i))) |
|
2196 *} |
|
2197 "Sequential symbolic execution using step theorems." |
|
2198 |
|
2199 |
|
2200 ML {* |
|
2201 fun goto_tac ctxt thm i state = |
|
2202 let |
|
2203 val (goal, ctxt) = nth (Thm.prems_of state) (i - 1) |
|
2204 |> focus_concl ctxt |> (apfst HOLogic.dest_Trueprop) |
|
2205 val _ = tracing ctxt "goto_tac: goal = " |
|
2206 val _ = goal |> string_of_term ctxt |> tracing ctxt |
|
2207 val (post_cond_npc, pre_cond_frame, post_cond_frame, need_frame, is_last) |
|
2208 = step_terms_for thm goal ctxt |
|
2209 val _ = tracing ctxt "Tring to find triple processor: TP" |
|
2210 val TP = triple_for ctxt goal |
|
2211 val _ = #binding TP |> Binding.name_of |> tracing ctxt |
|
2212 val _ = tracing ctxt "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
|
2213 val post_cond_npc_str = post_cond_npc |> string_for_cterm ctxt |
|
2214 val pre_cond_frame_str = pre_cond_frame |> string_for_cterm ctxt |
|
2215 val _ = tracing ctxt "goto_tac: post_cond_npc = " |
|
2216 val _ = post_cond_npc_str |> tracing ctxt |
|
2217 val _ = tracing ctxt "goto_tac: pre_cond_frame = " |
|
2218 val _ = pre_cond_frame_str |> tracing ctxt |
|
2219 fun tac1 i state = |
|
2220 if is_last then (K all_tac) i state else |
|
2221 res_inst_tac ctxt [] |
|
2222 (#hoare_seq2 TP) i state |
|
2223 fun tac2 i state = res_inst_tac ctxt [(("p", 0), pre_cond_frame_str)] |
|
2224 (#pre_stren TP) i state |
|
2225 fun foc_tac post_cond_frame ctxt i state = |
|
2226 let |
|
2227 val goal = get_concl ctxt (i, state) |
|
2228 val pc_term = goal |> #get_post TP ctxt |> break_sep_conj |
|
2229 |> filter (#is_pc_term TP) |> hd |
|
2230 val (_$Free(j', _)) = pc_term |
|
2231 val psd = post_cond_frame j' |
|
2232 val str_psd = psd |> string_for_cterm ctxt |
|
2233 val _ = tracing ctxt "goto_tac: psd = " |
|
2234 val _ = str_psd |> tracing ctxt |
|
2235 in |
|
2236 res_inst_tac ctxt [(("q", 0), str_psd)] |
|
2237 (#post_weaken TP) i state |
|
2238 end |
|
2239 val frame_tac = if need_frame then (rtac (#frame_rule TP)) else (K all_tac) |
|
2240 val _ = tracing ctxt "goto_tac: starting to apply tacs" |
|
2241 val print_tac = if (Config.get ctxt trace_step) then Tactical.print_tac else (K all_tac) |
|
2242 val tac = (tac1 THEN' (K (print_tac "tac1 success"))) THEN' |
|
2243 (tac2 THEN' (K (print_tac "tac2 success"))) THEN' |
|
2244 ((foc_tac post_cond_frame ctxt) THEN' (K (print_tac "foc_tac success"))) THEN' |
|
2245 (frame_tac THEN' (K (print_tac "frame_tac success"))) THEN' |
|
2246 ((((rtac thm) THEN_ALL_NEW (#sep_conj_ac_tac TP ctxt))) THEN' |
|
2247 (K (print_tac "rtac success")) |
|
2248 ) THEN' |
|
2249 (K (ALLGOALS (atomic (#sep_conj_ac_tac TP ctxt)))) THEN' |
|
2250 (K prune_params_tac) |
|
2251 in |
|
2252 tac i state |
|
2253 end |
|
2254 *} |
|
2255 |
|
2256 ML {* |
|
2257 fun sg_goto_tac thms ctxt = |
|
2258 let val sg_goto_tac' = (map (fn thm => attemp (goto_tac ctxt thm)) thms) |
|
2259 |> FIRST' |
|
2260 val sg_goto_tac'' = (K (unfold_cell_tac ctxt)) THEN' sg_goto_tac' THEN' (K (fold_cell_tac ctxt)) |
|
2261 in |
|
2262 sg_goto_tac' ORELSE' sg_goto_tac'' |
|
2263 end |
|
2264 fun gotos_tac thms ctxt i = REPEAT (sg_goto_tac thms ctxt i) THEN (prune_params_tac) |
|
2265 *} |
|
2266 |
|
2267 method_setup hgoto = {* |
|
2268 Attrib.thms >> (fn thms => fn ctxt => |
|
2269 (SIMPLE_METHOD' (fn i => |
|
2270 sg_goto_tac (thms@(StepRules.get ctxt)) ctxt i))) |
|
2271 *} |
|
2272 "One step symbolic execution using goto theorems." |
|
2273 |
|
2274 subsection {* Tactic for forward reasoning *} |
|
2275 |
|
2276 ML {* |
|
2277 fun mk_msel_rule ctxt conclusion idx term = |
|
2278 let |
|
2279 val cjt_count = term |> break_sep_conj |> length |
|
2280 fun variants nctxt names = fold_map Name.variant names nctxt; |
|
2281 |
|
2282 val (state, nctxt0) = Name.variant "s" (Variable.names_of ctxt); |
|
2283 |
|
2284 fun sep_conj_prop cjts = |
|
2285 FunApp.fun_app_free |
|
2286 (FunApp.fun_app_foldr SepConj.sep_conj_term cjts) state |
|
2287 |> HOLogic.mk_Trueprop; |
|
2288 |
|
2289 (* concatenate string and string of an int *) |
|
2290 fun conc_str_int str int = str ^ Int.toString int; |
|
2291 |
|
2292 (* make the conjunct names *) |
|
2293 val (cjts, _) = ListExtra.range 1 cjt_count |
|
2294 |> map (conc_str_int "a") |> variants nctxt0; |
|
2295 |
|
2296 fun skel_sep_conj names (Const (@{const_name sep_conj}, _) $ t1 $ t2 $ y) = |
|
2297 (let val nm1 = take (length (break_sep_conj t1)) names |
|
2298 val nm2 = drop (length (break_sep_conj t1)) names |
|
2299 val t1' = skel_sep_conj nm1 t1 |
|
2300 val t2' = skel_sep_conj nm2 t2 |
|
2301 in (SepConj.sep_conj_term $ t1' $ t2' $ y) end) |
|
2302 | skel_sep_conj names (Const (@{const_name sep_conj}, _) $ t1 $ t2) = |
|
2303 (let val nm1 = take (length (break_sep_conj t1)) names |
|
2304 val nm2 = drop (length (break_sep_conj t1)) names |
|
2305 val t1' = skel_sep_conj nm1 t1 |
|
2306 val t2' = skel_sep_conj nm2 t2 |
|
2307 in (SepConj.sep_conj_term $ t1' $ t2') end) |
|
2308 | skel_sep_conj names (Abs (x, y, t $ Bound 0)) = |
|
2309 let val t' = (skel_sep_conj names t) |
|
2310 val ty' = t' |> type_of |> domain_type |
|
2311 in (Abs (x, ty', (t' $ Bound 0))) end |
|
2312 | skel_sep_conj names t = Free (hd names, SepConj.sep_conj_term |> type_of |> domain_type); |
|
2313 val _ = tracing ctxt "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
|
2314 val oskel = skel_sep_conj cjts term; |
|
2315 val _ = tracing ctxt "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" |
|
2316 val ttt = oskel |> type_of |
|
2317 val _ = tracing ctxt "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" |
|
2318 val orig = FunApp.fun_app_free oskel state |> HOLogic.mk_Trueprop |
|
2319 val _ = tracing ctxt "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" |
|
2320 val is_selected = member (fn (x, y) => x = y) idx |
|
2321 val all_idx = ListExtra.range 0 cjt_count |
|
2322 val selected_idx = idx |
|
2323 val unselected_idx = filter_out is_selected all_idx |
|
2324 val selected = map (nth cjts) selected_idx |
|
2325 val unselected = map (nth cjts) unselected_idx |
|
2326 |
|
2327 fun fun_app_foldr f [a,b] = FunApp.fun_app_free (FunApp.fun_app_free f a) b |
|
2328 | fun_app_foldr f [a] = Free (a, SepConj.sep_conj_term |> type_of |> domain_type) |
|
2329 | fun_app_foldr f (x::xs) = (FunApp.fun_app_free f x) $ (fun_app_foldr f xs) |
|
2330 | fun_app_foldr _ _ = raise Fail "fun_app_foldr"; |
|
2331 |
|
2332 val reordered_skel = |
|
2333 if unselected = [] then (fun_app_foldr SepConj.sep_conj_term selected) |
|
2334 else (SepConj.sep_conj_term $ (fun_app_foldr SepConj.sep_conj_term selected) |
|
2335 $ (fun_app_foldr SepConj.sep_conj_term unselected)) |
|
2336 |
|
2337 val reordered = FunApp.fun_app_free reordered_skel state |> HOLogic.mk_Trueprop |
|
2338 val goal = Logic.mk_implies |
|
2339 (if conclusion then (orig, reordered) else (reordered, orig)); |
|
2340 val rule = |
|
2341 Goal.prove ctxt [] [] goal (fn _ => |
|
2342 auto_tac (ctxt |> Simplifier.map_simpset (fn ss => ss addsimps @{thms sep_conj_ac}))) |
|
2343 |> Drule.export_without_context |
|
2344 in |
|
2345 rule |
|
2346 end |
|
2347 *} |
|
2348 |
|
2349 lemma fwd_rule: |
|
2350 assumes "\<And> s . U s \<longrightarrow> V s" |
|
2351 shows "(U ** RR) s \<Longrightarrow> (V ** RR) s" |
|
2352 by (metis assms sep_globalise) |
|
2353 |
|
2354 ML {* |
|
2355 fun sg_sg_fwd_tac ctxt thm pos i state = |
|
2356 let |
|
2357 |
|
2358 val tracing = (fn str => |
|
2359 if (Config.get ctxt trace_fwd) then Output.tracing str else ()) |
|
2360 fun pterm t = |
|
2361 t |> string_of_term ctxt |> tracing |
|
2362 fun pcterm ct = ct |> string_of_cterm ctxt |> tracing |
|
2363 |
|
2364 fun atm thm = |
|
2365 let |
|
2366 (* val thm = thm |> Drule.forall_intr_vars *) |
|
2367 val res = thm |> cprop_of |> Object_Logic.atomize |
|
2368 val res' = Raw_Simplifier.rewrite_rule [res] thm |
|
2369 in res' end |
|
2370 |
|
2371 fun find_idx ctxt pats terms = |
|
2372 let val result = |
|
2373 map (fn pat => (find_index (fn trm => ((match ctxt pat trm; true) |
|
2374 handle _ => false)) terms)) pats |
|
2375 in (assert_all (fn x => x >= 0) result (K "match of precondition failed")); |
|
2376 result |
|
2377 end |
|
2378 |
|
2379 val goal = nth (Drule.cprems_of state) (i - 1) |> term_of |
|
2380 val _ = tracing "goal = " |
|
2381 val _ = goal |> pterm |
|
2382 |
|
2383 val ctxt_orig = ctxt |
|
2384 |
|
2385 val ((ps, goal), ctxt) = Variable.focus goal ctxt_orig |
|
2386 |
|
2387 val prems = goal |> Logic.strip_imp_prems |
|
2388 |
|
2389 val cprem = nth prems (pos - 1) |
|
2390 val (_ $ (the_prem $ _)) = cprem |
|
2391 val cjts = the_prem |> break_sep_conj |
|
2392 val thm_prems = thm |> cprems_of |> hd |> Thm.dest_arg |> Thm.dest_fun |
|
2393 val thm_assms = thm |> cprems_of |> tl |> map term_of |
|
2394 val thm_cjts = thm_prems |> term_of |> break_sep_conj |
|
2395 val thm_trm = thm |> prop_of |
|
2396 |
|
2397 val _ = tracing "cjts = " |
|
2398 val _ = cjts |> map pterm |
|
2399 val _ = tracing "thm_cjts = " |
|
2400 val _ = thm_cjts |> map pterm |
|
2401 |
|
2402 val _ = tracing "Calculating sols" |
|
2403 val sols = match_pres ctxt (match_env ctxt) empty_env thm_cjts cjts |
|
2404 val _ = tracing "End calculating sols, sols =" |
|
2405 val _ = tracing (@{make_string} sols) |
|
2406 val _ = tracing "Calulating env2 and idxs" |
|
2407 val (env2, idx) = filter (fn (env, idxs) => make_sense sep_conj_ac_tac ctxt thm_assms env) sols |> hd |
|
2408 val ([thm'_trm], ctxt') = thm_trm |> inst env2 |> single |
|
2409 |> (fn trms => Variable.import_terms true trms ctxt) |
|
2410 val thm'_prem = Logic.strip_imp_prems thm'_trm |> hd |
|
2411 val thm'_concl = Logic.strip_imp_concl thm'_trm |
|
2412 val thm'_prem = (Goal.prove ctxt' [] [thm'_prem] thm'_concl |
|
2413 (fn {context, prems = [prem]} => |
|
2414 (rtac (prem RS thm) THEN_ALL_NEW (sep_conj_ac_tac ctxt)) 1)) |
|
2415 val [thm'] = Variable.export ctxt' ctxt_orig [thm'_prem] |
|
2416 val trans_rule = |
|
2417 mk_msel_rule ctxt true idx the_prem |
|
2418 val _ = tracing "trans_rule = " |
|
2419 val _ = trans_rule |> cprop_of |> pcterm |
|
2420 val app_rule = |
|
2421 if (length cjts = length thm_cjts) then thm' else |
|
2422 ((thm' |> atm) RS @{thm fwd_rule}) |
|
2423 val _ = tracing "app_rule = " |
|
2424 val _ = app_rule |> cprop_of |> pcterm |
|
2425 val print_tac = if (Config.get ctxt trace_fwd) then Tactical.print_tac else (K all_tac) |
|
2426 val the_tac = (dtac trans_rule THEN' (K (print_tac "dtac1 success"))) THEN' |
|
2427 ((dtac app_rule THEN' (K (print_tac "dtac2 success")))) |
|
2428 in |
|
2429 (the_tac i state) handle _ => no_tac state |
|
2430 end |
|
2431 *} |
|
2432 |
|
2433 ML {* |
|
2434 fun sg_fwd_tac ctxt thm i state = |
|
2435 let |
|
2436 val goal = nth (Drule.cprems_of state) (i - 1) |
|
2437 val prems = goal |> term_of |> Term.strip_all_body |> Logic.strip_imp_prems |
|
2438 val posx = ListExtra.range 1 (length prems) |
|
2439 in |
|
2440 ((map (fn pos => attemp (sg_sg_fwd_tac ctxt thm pos)) posx) |> FIRST') i state |
|
2441 end |
|
2442 |
|
2443 fun fwd_tac ctxt thms i state = |
|
2444 ((map (fn thm => sg_fwd_tac ctxt thm) thms) |> FIRST') i state |
|
2445 *} |
|
2446 |
|
2447 method_setup fwd = {* |
|
2448 Attrib.thms >> (fn thms => fn ctxt => |
|
2449 (SIMPLE_METHOD' (fn i => |
|
2450 fwd_tac ctxt (thms@(FwdRules.get ctxt)) i))) |
|
2451 *} |
|
2452 "Forward derivation of separation implication" |
|
2453 |
|
2454 text {* Testing the fwd tactic *} |
|
2455 |
|
2456 lemma ones_abs: |
|
2457 assumes "(ones u v \<and>* ones w x) s" "w = v + 1" |
|
2458 shows "ones u x s" |
|
2459 using assms(1) unfolding assms(2) |
|
2460 proof(induct u v arbitrary: x s rule:ones_induct) |
|
2461 case (Base i j x s) |
|
2462 thus ?case by (auto elim!:condE) |
|
2463 next |
|
2464 case (Step i j x s) |
|
2465 hence h: "\<And> x s. (ones (i + 1) j \<and>* ones (j + 1) x) s \<longrightarrow> ones (i + 1) x s" |
|
2466 by metis |
|
2467 hence "(ones (i + 1) x \<and>* one i) s" |
|
2468 by (rule fwd_rule, insert Step(3), auto simp:sep_conj_ac) |
|
2469 thus ?case |
|
2470 by (smt condD ones.simps sep_conj_commute) |
|
2471 qed |
|
2472 |
|
2473 lemma one_abs: "(one m) s \<Longrightarrow> (ones m m) s" |
|
2474 by (smt cond_true_eq2 ones.simps) |
|
2475 |
|
2476 lemma ones_reps_abs: |
|
2477 assumes "ones m n s" |
|
2478 "m \<le> n" |
|
2479 shows "(reps m n [nat (n - m)]) s" |
|
2480 using assms |
|
2481 by simp |
|
2482 |
|
2483 lemma reps_reps'_abs: |
|
2484 assumes "(reps m n xs \<and>* zero u) s" "u = n + 1" "xs \<noteq> []" |
|
2485 shows "(reps' m u xs) s" |
|
2486 unfolding assms using assms |
|
2487 by (unfold reps'_def, simp) |
|
2488 |
|
2489 lemma reps'_abs: |
|
2490 assumes "(reps' m n xs \<and>* reps' u v ys) s" "u = n + 1" |
|
2491 shows "(reps' m v (xs @ ys)) s" |
|
2492 apply (unfold reps'_append, rule_tac x = u in EXS_intro) |
|
2493 by (insert assms, simp) |
|
2494 |
|
2495 lemmas abs_ones = one_abs ones_abs |
|
2496 |
|
2497 lemmas abs_reps' = ones_reps_abs reps_reps'_abs reps'_abs |
|
2498 |
|
2499 |
|
2500 section {* Modular TM programming and verification *} |
|
2501 |
|
2502 definition "right_until_zero = |
|
2503 (TL start exit. |
|
2504 TLabel start; |
|
2505 if_zero exit; |
|
2506 move_right; |
|
2507 jmp start; |
|
2508 TLabel exit |
|
2509 )" |
|
2510 |
|
2511 lemma ones_false [simp]: "j < i - 1 \<Longrightarrow> (ones i j) = sep_false" |
|
2512 by (simp add:pasrt_def) |
|
2513 |
|
2514 lemma hoare_right_until_zero: |
|
2515 "\<lbrace>st i ** ps u ** ones u (v - 1) ** zero v \<rbrace> |
|
2516 i:[right_until_zero]:j |
|
2517 \<lbrace>st j ** ps v ** ones u (v - 1) ** zero v \<rbrace>" |
|
2518 proof(unfold right_until_zero_def, |
|
2519 intro t_hoare_local t_hoare_label, clarify, |
|
2520 rule t_hoare_label_last, simp, simp) |
|
2521 fix la |
|
2522 let ?body = "i :[ (if_zero la ; move_right ; jmp i) ]: la" |
|
2523 let ?j = la |
|
2524 show "\<lbrace>st i \<and>* ps u \<and>* ones u (v - 1) \<and>* zero v\<rbrace> ?body |
|
2525 \<lbrace>st ?j \<and>* ps v \<and>* ones u (v - 1) \<and>* zero v\<rbrace>" (is "?P u (v - 1) (ones u (v - 1))") |
|
2526 proof(induct "u" "v - 1" rule:ones_induct) |
|
2527 case (Base k) |
|
2528 moreover have "\<lbrace>st i \<and>* ps v \<and>* zero v\<rbrace> ?body |
|
2529 \<lbrace>st ?j \<and>* ps v \<and>* zero v\<rbrace>" by hsteps |
|
2530 ultimately show ?case by (auto intro!:tm.pre_condI simp:sep_conj_cond) |
|
2531 next |
|
2532 case (Step k) |
|
2533 moreover have "\<lbrace>st i \<and>* ps k \<and>* (one k \<and>* ones (k + 1) (v - 1)) \<and>* zero v\<rbrace> |
|
2534 i :[ (if_zero ?j ; move_right ; jmp i) ]: ?j |
|
2535 \<lbrace>st ?j \<and>* ps v \<and>* (one k \<and>* ones (k + 1) (v - 1)) \<and>* zero v\<rbrace>" |
|
2536 proof - |
|
2537 have s1: "\<lbrace>st i \<and>* ps k \<and>* (one k \<and>* ones (k + 1) (v - 1)) \<and>* zero v\<rbrace> |
|
2538 ?body |
|
2539 \<lbrace>st i \<and>* ps (k + 1) \<and>* one k \<and>* ones (k + 1) (v - 1) \<and>* zero v\<rbrace>" |
|
2540 proof(cases "k + 1 \<ge> v") |
|
2541 case True |
|
2542 with Step(1) have "v = k + 1" by arith |
|
2543 thus ?thesis |
|
2544 apply(simp add: one_def) |
|
2545 by hsteps |
|
2546 next |
|
2547 case False |
|
2548 hence eq_ones: "ones (k + 1) (v - 1) = |
|
2549 (one (k + 1) \<and>* ones ((k + 1) + 1) (v - 1))" |
|
2550 by simp |
|
2551 show ?thesis |
|
2552 apply(simp only: eq_ones) |
|
2553 by hsteps |
|
2554 qed |
|
2555 note Step(2)[step] |
|
2556 have s2: "\<lbrace>st i \<and>* ps (k + 1) \<and>* one k \<and>* ones (k + 1) (v - 1) \<and>* zero v\<rbrace> |
|
2557 ?body |
|
2558 \<lbrace>st ?j \<and>* ps v \<and>* one k \<and>* ones (k + 1) (v - 1) \<and>* zero v\<rbrace>" |
|
2559 by hsteps |
|
2560 from tm.sequencing [OF s1 s2, step] |
|
2561 show ?thesis |
|
2562 by (auto simp:sep_conj_ac) |
|
2563 qed |
|
2564 ultimately show ?case by simp |
|
2565 qed |
|
2566 qed |
|
2567 |
|
2568 lemma hoare_right_until_zero_gen[step]: |
|
2569 assumes "u = v" "w = x - 1" |
|
2570 shows "\<lbrace>st i ** ps u ** ones v w ** zero x \<rbrace> |
|
2571 i:[right_until_zero]:j |
|
2572 \<lbrace>st j ** ps x ** ones v w ** zero x \<rbrace>" |
|
2573 by (unfold assms, rule hoare_right_until_zero) |
|
2574 |
|
2575 definition "left_until_zero = |
|
2576 (TL start exit. |
|
2577 TLabel start; |
|
2578 if_zero exit; |
|
2579 move_left; |
|
2580 jmp start; |
|
2581 TLabel exit |
|
2582 )" |
|
2583 |
|
2584 lemma hoare_left_until_zero: |
|
2585 "\<lbrace>st i ** ps v ** zero u ** ones (u + 1) v \<rbrace> |
|
2586 i:[left_until_zero]:j |
|
2587 \<lbrace>st j ** ps u ** zero u ** ones (u + 1) v \<rbrace>" |
|
2588 proof(unfold left_until_zero_def, |
|
2589 intro t_hoare_local t_hoare_label, clarify, |
|
2590 rule t_hoare_label_last, simp+) |
|
2591 fix la |
|
2592 let ?body = "i :[ (if_zero la ; move_left ; jmp i) ]: la" |
|
2593 let ?j = la |
|
2594 show "\<lbrace>st i \<and>* ps v \<and>* zero u \<and>* ones (u + 1) v\<rbrace> ?body |
|
2595 \<lbrace>st ?j \<and>* ps u \<and>* zero u \<and>* ones (u + 1) v\<rbrace>" |
|
2596 proof(induct "u+1" v rule:ones_rev_induct) |
|
2597 case (Base k) |
|
2598 thus ?case |
|
2599 by (simp add:sep_conj_cond, intro tm.pre_condI, simp, hstep) |
|
2600 next |
|
2601 case (Step k) |
|
2602 have "\<lbrace>st i \<and>* ps k \<and>* zero u \<and>* ones (u + 1) (k - 1) \<and>* one k\<rbrace> |
|
2603 ?body |
|
2604 \<lbrace>st ?j \<and>* ps u \<and>* zero u \<and>* ones (u + 1) (k - 1) \<and>* one k\<rbrace>" |
|
2605 proof(rule tm.sequencing[where q = |
|
2606 "st i \<and>* ps (k - 1) \<and>* zero u \<and>* ones (u + 1) (k - 1) \<and>* one k"]) |
|
2607 show "\<lbrace>st i \<and>* ps k \<and>* zero u \<and>* ones (u + 1) (k - 1) \<and>* one k\<rbrace> |
|
2608 ?body |
|
2609 \<lbrace>st i \<and>* ps (k - 1) \<and>* zero u \<and>* ones (u + 1) (k - 1) \<and>* one k\<rbrace>" |
|
2610 proof(induct "u + 1" "k - 1" rule:ones_rev_induct) |
|
2611 case Base with Step(1) have "k = u + 1" by arith |
|
2612 thus ?thesis |
|
2613 by (simp, hsteps) |
|
2614 next |
|
2615 case Step |
|
2616 show ?thesis |
|
2617 apply (unfold ones_rev[OF Step(1)], simp) |
|
2618 apply (unfold one_def) |
|
2619 by hsteps |
|
2620 qed |
|
2621 next |
|
2622 note Step(2) [step] |
|
2623 show "\<lbrace>st i \<and>* ps (k - 1) \<and>* zero u \<and>* ones (u + 1) (k - 1) \<and>* one k\<rbrace> |
|
2624 ?body |
|
2625 \<lbrace>st ?j \<and>* ps u \<and>* zero u \<and>* ones (u + 1) (k - 1) \<and>* one k\<rbrace>" by hsteps |
|
2626 qed |
|
2627 thus ?case by (unfold ones_rev[OF Step(1)], simp) |
|
2628 qed |
|
2629 qed |
|
2630 |
|
2631 lemma hoare_left_until_zero_gen[step]: |
|
2632 assumes "u = x" "w = v + 1" |
|
2633 shows "\<lbrace>st i ** ps u ** zero v ** ones w x \<rbrace> |
|
2634 i:[left_until_zero]:j |
|
2635 \<lbrace>st j ** ps v ** zero v ** ones w x \<rbrace>" |
|
2636 by (unfold assms, rule hoare_left_until_zero) |
|
2637 |
|
2638 definition "right_until_one = |
|
2639 (TL start exit. |
|
2640 TLabel start; |
|
2641 if_one exit; |
|
2642 move_right; |
|
2643 jmp start; |
|
2644 TLabel exit |
|
2645 )" |
|
2646 |
|
2647 lemma hoare_right_until_one: |
|
2648 "\<lbrace>st i ** ps u ** zeros u (v - 1) ** one v \<rbrace> |
|
2649 i:[right_until_one]:j |
|
2650 \<lbrace>st j ** ps v ** zeros u (v - 1) ** one v \<rbrace>" |
|
2651 proof(unfold right_until_one_def, |
|
2652 intro t_hoare_local t_hoare_label, clarify, |
|
2653 rule t_hoare_label_last, simp+) |
|
2654 fix la |
|
2655 let ?body = "i :[ (if_one la ; move_right ; jmp i) ]: la" |
|
2656 let ?j = la |
|
2657 show "\<lbrace>st i \<and>* ps u \<and>* zeros u (v - 1) \<and>* one v\<rbrace> ?body |
|
2658 \<lbrace>st ?j \<and>* ps v \<and>* zeros u (v - 1) \<and>* one v\<rbrace>" |
|
2659 proof(induct u "v - 1" rule:zeros_induct) |
|
2660 case (Base k) |
|
2661 thus ?case |
|
2662 by (simp add:sep_conj_cond, intro tm.pre_condI, simp, hsteps) |
|
2663 next |
|
2664 case (Step k) |
|
2665 have "\<lbrace>st i \<and>* ps k \<and>* zero k \<and>* zeros (k + 1) (v - 1) \<and>* one v\<rbrace> |
|
2666 ?body |
|
2667 \<lbrace>st ?j \<and>* ps v \<and>* zero k \<and>* zeros (k + 1) (v - 1) \<and>* one v\<rbrace>" |
|
2668 proof(rule tm.sequencing[where q = |
|
2669 "st i \<and>* ps (k + 1) \<and>* zero k \<and>* zeros (k + 1) (v - 1) \<and>* one v"]) |
|
2670 show "\<lbrace>st i \<and>* ps k \<and>* zero k \<and>* zeros (k + 1) (v - 1) \<and>* one v\<rbrace> |
|
2671 ?body |
|
2672 \<lbrace>st i \<and>* ps (k + 1) \<and>* zero k \<and>* zeros (k + 1) (v - 1) \<and>* one v\<rbrace>" |
|
2673 proof(induct "k + 1" "v - 1" rule:zeros_induct) |
|
2674 case Base |
|
2675 with Step(1) have eq_v: "k + 1 = v" by arith |
|
2676 from Base show ?thesis |
|
2677 apply (simp add:sep_conj_cond, intro tm.pre_condI, simp) |
|
2678 apply (hstep, clarsimp) |
|
2679 by hsteps |
|
2680 next |
|
2681 case Step |
|
2682 thus ?thesis |
|
2683 by (simp, hsteps) |
|
2684 qed |
|
2685 next |
|
2686 note Step(2)[step] |
|
2687 show "\<lbrace>st i \<and>* ps (k + 1) \<and>* zero k \<and>* zeros (k + 1) (v - 1) \<and>* one v\<rbrace> |
|
2688 ?body |
|
2689 \<lbrace>st ?j \<and>* ps v \<and>* zero k \<and>* zeros (k + 1) (v - 1) \<and>* one v\<rbrace>" |
|
2690 by hsteps |
|
2691 qed |
|
2692 thus ?case by (auto simp: sep_conj_ac Step(1)) |
|
2693 qed |
|
2694 qed |
|
2695 |
|
2696 lemma hoare_right_until_one_gen[step]: |
|
2697 assumes "u = v" "w = x - 1" |
|
2698 shows |
|
2699 "\<lbrace>st i ** ps u ** zeros v w ** one x \<rbrace> |
|
2700 i:[right_until_one]:j |
|
2701 \<lbrace>st j ** ps x ** zeros v w ** one x \<rbrace>" |
|
2702 by (unfold assms, rule hoare_right_until_one) |
|
2703 |
|
2704 definition "left_until_one = |
|
2705 (TL start exit. |
|
2706 TLabel start; |
|
2707 if_one exit; |
|
2708 move_left; |
|
2709 jmp start; |
|
2710 TLabel exit |
|
2711 )" |
|
2712 |
|
2713 lemma hoare_left_until_one: |
|
2714 "\<lbrace>st i ** ps v ** one u ** zeros (u + 1) v \<rbrace> |
|
2715 i:[left_until_one]:j |
|
2716 \<lbrace>st j ** ps u ** one u ** zeros (u + 1) v \<rbrace>" |
|
2717 proof(unfold left_until_one_def, |
|
2718 intro t_hoare_local t_hoare_label, clarify, |
|
2719 rule t_hoare_label_last, simp+) |
|
2720 fix la |
|
2721 let ?body = "i :[ (if_one la ; move_left ; jmp i) ]: la" |
|
2722 let ?j = la |
|
2723 show "\<lbrace>st i \<and>* ps v \<and>* one u \<and>* zeros (u + 1) v\<rbrace> ?body |
|
2724 \<lbrace>st ?j \<and>* ps u \<and>* one u \<and>* zeros (u + 1) v\<rbrace>" |
|
2725 proof(induct u v rule: ones'.induct) |
|
2726 fix ia ja |
|
2727 assume h: "\<not> ja < ia \<Longrightarrow> |
|
2728 \<lbrace>st i \<and>* ps (ja - 1) \<and>* one ia \<and>* zeros (ia + 1) (ja - 1)\<rbrace> ?body |
|
2729 \<lbrace>st ?j \<and>* ps ia \<and>* one ia \<and>* zeros (ia + 1) (ja - 1)\<rbrace>" |
|
2730 show "\<lbrace>st i \<and>* ps ja \<and>* one ia \<and>* zeros (ia + 1) ja\<rbrace> ?body |
|
2731 \<lbrace>st ?j \<and>* ps ia \<and>* one ia \<and>* zeros (ia + 1) ja\<rbrace>" |
|
2732 proof(cases "ja < ia") |
|
2733 case False |
|
2734 note lt = False |
|
2735 from h[OF this] have [step]: |
|
2736 "\<lbrace>st i \<and>* ps (ja - 1) \<and>* one ia \<and>* zeros (ia + 1) (ja - 1)\<rbrace> ?body |
|
2737 \<lbrace>st ?j \<and>* ps ia \<and>* one ia \<and>* zeros (ia + 1) (ja - 1)\<rbrace>" . |
|
2738 show ?thesis |
|
2739 proof(cases "ja = ia") |
|
2740 case True |
|
2741 moreover |
|
2742 have "\<lbrace>st i \<and>* ps ja \<and>* one ja\<rbrace> ?body \<lbrace>st ?j \<and>* ps ja \<and>* one ja\<rbrace>" |
|
2743 by hsteps |
|
2744 ultimately show ?thesis by auto |
|
2745 next |
|
2746 case False |
|
2747 with lt have k1: "ia < ja" by auto |
|
2748 from zeros_rev[of "ja" "ia + 1"] this |
|
2749 have eq_zeros: "zeros (ia + 1) ja = (zeros (ia + 1) (ja - 1) \<and>* zero ja)" |
|
2750 by simp |
|
2751 have s1: "\<lbrace>st i \<and>* ps ja \<and>* one ia \<and>* zeros (ia + 1) (ja - 1) \<and>* zero ja\<rbrace> |
|
2752 ?body |
|
2753 \<lbrace>st i \<and>* ps (ja - 1) \<and>* one ia \<and>* zeros (ia + 1) (ja - 1) \<and>* zero ja\<rbrace>" |
|
2754 proof(cases "ia + 1 \<ge> ja") |
|
2755 case True |
|
2756 from k1 True have "ja = ia + 1" by arith |
|
2757 moreover have "\<lbrace>st i \<and>* ps (ia + 1) \<and>* one (ia + 1 - 1) \<and>* zero (ia + 1)\<rbrace> |
|
2758 i :[ (if_one ?j ; move_left ; jmp i) ]: ?j |
|
2759 \<lbrace>st i \<and>* ps (ia + 1 - 1) \<and>* one (ia + 1 - 1) \<and>* zero (ia + 1)\<rbrace>" |
|
2760 by (hsteps) |
|
2761 ultimately show ?thesis |
|
2762 by (simp) |
|
2763 next |
|
2764 case False |
|
2765 from zeros_rev[of "ja - 1" "ia + 1"] False |
|
2766 have k: "zeros (ia + 1) (ja - 1) = |
|
2767 (zeros (ia + 1) (ja - 1 - 1) \<and>* zero (ja - 1))" |
|
2768 by auto |
|
2769 show ?thesis |
|
2770 apply (unfold k, simp) |
|
2771 by hsteps |
|
2772 qed |
|
2773 have s2: "\<lbrace>st i \<and>* ps (ja - 1) \<and>* one ia \<and>* zeros (ia + 1) (ja - 1) \<and>* zero ja\<rbrace> |
|
2774 ?body |
|
2775 \<lbrace>st ?j \<and>* ps ia \<and>* one ia \<and>* zeros (ia + 1) (ja - 1) \<and>* zero ja\<rbrace>" |
|
2776 by hsteps |
|
2777 from tm.sequencing[OF s1 s2, step] |
|
2778 show ?thesis |
|
2779 apply (unfold eq_zeros) |
|
2780 by hstep |
|
2781 qed (* ccc *) |
|
2782 next |
|
2783 case True |
|
2784 thus ?thesis by (auto intro:tm.hoare_sep_false) |
|
2785 qed |
|
2786 qed |
|
2787 qed |
|
2788 |
|
2789 lemma hoare_left_until_one_gen[step]: |
|
2790 assumes "u = x" "w = v + 1" |
|
2791 shows "\<lbrace>st i ** ps u ** one v ** zeros w x \<rbrace> |
|
2792 i:[left_until_one]:j |
|
2793 \<lbrace>st j ** ps v ** one v ** zeros w x \<rbrace>" |
|
2794 by (unfold assms, rule hoare_left_until_one) |
|
2795 |
|
2796 definition "left_until_double_zero = |
|
2797 (TL start exit. |
|
2798 TLabel start; |
|
2799 if_zero exit; |
|
2800 left_until_zero; |
|
2801 move_left; |
|
2802 if_one start; |
|
2803 TLabel exit)" |
|
2804 |
|
2805 declare ones.simps[simp del] |
|
2806 |
|
2807 lemma reps_simps3: "ks \<noteq> [] \<Longrightarrow> |
|
2808 reps i j (k # ks) = (ones i (i + int k) ** zero (i + int k + 1) ** reps (i + int k + 2) j ks)" |
|
2809 by(case_tac ks, simp, simp add: reps.simps) |
|
2810 |
|
2811 lemma cond_eqI: |
|
2812 assumes h: "b \<Longrightarrow> r = s" |
|
2813 shows "(<b> ** r) = (<b> ** s)" |
|
2814 proof(cases b) |
|
2815 case True |
|
2816 from h[OF this] show ?thesis by simp |
|
2817 next |
|
2818 case False |
|
2819 thus ?thesis |
|
2820 by (unfold sep_conj_def set_ins_def pasrt_def, auto) |
|
2821 qed |
|
2822 |
|
2823 lemma reps_rev: "ks \<noteq> [] |
|
2824 \<Longrightarrow> reps i j (ks @ [k]) = (reps i (j - int (k + 1) - 1 ) ks \<and>* |
|
2825 zero (j - int (k + 1)) \<and>* ones (j - int k) j)" |
|
2826 proof(induct ks arbitrary: i j) |
|
2827 case Nil |
|
2828 thus ?case by simp |
|
2829 next |
|
2830 case (Cons a ks) |
|
2831 show ?case |
|
2832 proof(cases "ks = []") |
|
2833 case True |
|
2834 thus ?thesis |
|
2835 proof - |
|
2836 have eq_cond: "(j = i + int a + 2 + int k) = (-2 + (j - int k) = i + int a)" by auto |
|
2837 have "(<(-2 + (j - int k) = i + int a)> \<and>* |
|
2838 one i \<and>* ones (i + 1) (i + int a) \<and>* |
|
2839 zero (i + int a + 1) \<and>* one (i + int a + 2) \<and>* ones (3 + (i + int a)) (i + int a + 2 + int k)) |
|
2840 = |
|
2841 (<(-2 + (j - int k) = i + int a)> \<and>* one i \<and>* ones (i + 1) (i + int a) \<and>* |
|
2842 zero (j - (1 + int k)) \<and>* one (j - int k) \<and>* ones (j - int k + 1) j)" |
|
2843 (is "(<?X> \<and>* ?L) = (<?X> \<and>* ?R)") |
|
2844 proof(rule cond_eqI) |
|
2845 assume h: "-2 + (j - int k) = i + int a" |
|
2846 hence eqs: "i + int a + 1 = j - (1 + int k)" |
|
2847 "i + int a + 2 = j - int k" |
|
2848 "3 + (i + int a) = j - int k + 1" |
|
2849 "(i + int a + 2 + int k) = j" |
|
2850 by auto |
|
2851 show "?L = ?R" |
|
2852 by (unfold eqs, auto simp:sep_conj_ac) |
|
2853 qed |
|
2854 with True |
|
2855 show ?thesis |
|
2856 apply (simp del:ones_simps reps.simps) |
|
2857 apply (simp add:sep_conj_cond eq_cond) |
|
2858 by (auto simp:sep_conj_ac) |
|
2859 qed |
|
2860 next |
|
2861 case False |
|
2862 from Cons(1)[OF False, of "i + int a + 2" j] this |
|
2863 show ?thesis |
|
2864 by(simp add: reps_simps3 sep_conj_ac) |
|
2865 qed |
|
2866 qed |
|
2867 |
|
2868 lemma hoare_if_one_reps: |
|
2869 assumes nn: "ks \<noteq> []" |
|
2870 shows "\<lbrace>st i ** ps v ** reps u v ks\<rbrace> |
|
2871 i:[if_one e]:j |
|
2872 \<lbrace>st e ** ps v ** reps u v ks\<rbrace>" |
|
2873 proof(rule rev_exhaust[of ks]) |
|
2874 assume "ks = []" with nn show ?thesis by simp |
|
2875 next |
|
2876 fix y ys |
|
2877 assume eq_ks: "ks = ys @ [y]" |
|
2878 show " \<lbrace>st i \<and>* ps v \<and>* reps u v ks\<rbrace> i :[ if_one e ]: j \<lbrace>st e \<and>* ps v \<and>* reps u v ks\<rbrace>" |
|
2879 proof(cases "ys = []") |
|
2880 case False |
|
2881 have "\<lbrace>st i \<and>* ps v \<and>* reps u v (ys @ [y])\<rbrace> i :[ if_one e ]: j \<lbrace>st e \<and>* ps v \<and>* reps u v (ys @ [y])\<rbrace>" |
|
2882 apply(unfold reps_rev[OF False], simp del:ones_simps add:ones_rev) |
|
2883 by hstep |
|
2884 thus ?thesis |
|
2885 by (simp add:eq_ks) |
|
2886 next |
|
2887 case True |
|
2888 with eq_ks |
|
2889 show ?thesis |
|
2890 apply (simp del:ones_simps add:ones_rev sep_conj_cond, intro tm.pre_condI, simp) |
|
2891 by hstep |
|
2892 qed |
|
2893 qed |
|
2894 |
|
2895 lemma hoare_if_one_reps_gen[step]: |
|
2896 assumes nn: "ks \<noteq> []" "u = w" |
|
2897 shows "\<lbrace>st i ** ps u ** reps v w ks\<rbrace> |
|
2898 i:[if_one e]:j |
|
2899 \<lbrace>st e ** ps u ** reps v w ks\<rbrace>" |
|
2900 by (unfold `u = w`, rule hoare_if_one_reps[OF `ks \<noteq> []`]) |
|
2901 |
|
2902 lemma hoare_if_zero_ones_false[step]: |
|
2903 assumes "\<not> w < u" "v = w" |
|
2904 shows "\<lbrace>st i \<and>* ps v \<and>* ones u w\<rbrace> |
|
2905 i :[if_zero e]: j |
|
2906 \<lbrace>st j \<and>* ps v \<and>* ones u w\<rbrace>" |
|
2907 by (unfold `v = w` ones_rev[OF `\<not> w < u`], hstep) |
|
2908 |
|
2909 lemma hoare_left_until_double_zero_nil[step]: |
|
2910 assumes "u = v" |
|
2911 shows "\<lbrace>st i ** ps u ** zero v\<rbrace> |
|
2912 i:[left_until_double_zero]:j |
|
2913 \<lbrace>st j ** ps u ** zero v\<rbrace>" |
|
2914 apply (unfold `u = v` left_until_double_zero_def, |
|
2915 intro t_hoare_local t_hoare_label, clarsimp, |
|
2916 rule t_hoare_label_last, simp+) |
|
2917 by (hsteps) |
|
2918 |
|
2919 lemma hoare_if_zero_reps_false: |
|
2920 assumes nn: "ks \<noteq> []" |
|
2921 shows "\<lbrace>st i ** ps v ** reps u v ks\<rbrace> |
|
2922 i:[if_zero e]:j |
|
2923 \<lbrace>st j ** ps v ** reps u v ks\<rbrace>" |
|
2924 proof(rule rev_exhaust[of ks]) |
|
2925 assume "ks = []" with nn show ?thesis by simp |
|
2926 next |
|
2927 fix y ys |
|
2928 assume eq_ks: "ks = ys @ [y]" |
|
2929 show " \<lbrace>st i \<and>* ps v \<and>* reps u v ks\<rbrace> i :[ if_zero e ]: j \<lbrace>st j \<and>* ps v \<and>* reps u v ks\<rbrace>" |
|
2930 proof(cases "ys = []") |
|
2931 case False |
|
2932 have "\<lbrace>st i \<and>* ps v \<and>* reps u v (ys @ [y])\<rbrace> i :[ if_zero e ]: j \<lbrace>st j \<and>* ps v \<and>* reps u v (ys @ [y])\<rbrace>" |
|
2933 apply(unfold reps_rev[OF False], simp del:ones_simps add:ones_rev) |
|
2934 by hstep |
|
2935 thus ?thesis |
|
2936 by (simp add:eq_ks) |
|
2937 next |
|
2938 case True |
|
2939 with eq_ks |
|
2940 show ?thesis |
|
2941 apply (simp del:ones_simps add:ones_rev sep_conj_cond, intro tm.pre_condI, simp) |
|
2942 by hstep |
|
2943 qed |
|
2944 qed |
|
2945 |
|
2946 lemma hoare_if_zero_reps_false_gen[step]: |
|
2947 assumes "ks \<noteq> []" "u = w" |
|
2948 shows "\<lbrace>st i ** ps u ** reps v w ks\<rbrace> |
|
2949 i:[if_zero e]:j |
|
2950 \<lbrace>st j ** ps u ** reps v w ks\<rbrace>" |
|
2951 by (unfold `u = w`, rule hoare_if_zero_reps_false[OF `ks \<noteq> []`]) |
|
2952 |
|
2953 |
|
2954 lemma hoare_if_zero_reps_false1: |
|
2955 assumes nn: "ks \<noteq> []" |
|
2956 shows "\<lbrace>st i ** ps u ** reps u v ks\<rbrace> |
|
2957 i:[if_zero e]:j |
|
2958 \<lbrace>st j ** ps u ** reps u v ks\<rbrace>" |
|
2959 proof - |
|
2960 from nn obtain y ys where eq_ys: "ks = y#ys" |
|
2961 by (metis neq_Nil_conv) |
|
2962 show ?thesis |
|
2963 apply (unfold eq_ys) |
|
2964 by (case_tac ys, (simp, hsteps)+) |
|
2965 qed |
|
2966 |
|
2967 lemma hoare_if_zero_reps_false1_gen[step]: |
|
2968 assumes nn: "ks \<noteq> []" |
|
2969 and h: "u = w" |
|
2970 shows "\<lbrace>st i ** ps u ** reps w v ks\<rbrace> |
|
2971 i:[if_zero e]:j |
|
2972 \<lbrace>st j ** ps u ** reps w v ks\<rbrace>" |
|
2973 by (unfold h, rule hoare_if_zero_reps_false1[OF `ks \<noteq> []`]) |
|
2974 |
|
2975 lemma hoare_left_until_double_zero: |
|
2976 assumes h: "ks \<noteq> []" |
|
2977 shows "\<lbrace>st i ** ps v ** zero u ** zero (u + 1) ** reps (u+2) v ks\<rbrace> |
|
2978 i:[left_until_double_zero]:j |
|
2979 \<lbrace>st j ** ps u ** zero u ** zero (u + 1) ** reps (u+2) v ks\<rbrace>" |
|
2980 proof(unfold left_until_double_zero_def, |
|
2981 intro t_hoare_local t_hoare_label, clarsimp, |
|
2982 rule t_hoare_label_last, simp+) |
|
2983 fix la |
|
2984 let ?body = "i :[ (if_zero la ; left_until_zero ; move_left ; if_one i) ]: j" |
|
2985 let ?j = j |
|
2986 show "\<lbrace>st i \<and>* ps v \<and>* zero u \<and>* zero (u + 1) \<and>* reps (u + 2) v ks\<rbrace> |
|
2987 ?body |
|
2988 \<lbrace>st ?j \<and>* ps u \<and>* zero u \<and>* zero (u + 1) \<and>* reps (u + 2) v ks\<rbrace>" |
|
2989 using h |
|
2990 proof(induct ks arbitrary: v rule:rev_induct) |
|
2991 case Nil |
|
2992 with h show ?case by auto |
|
2993 next |
|
2994 case (snoc k ks) |
|
2995 show ?case |
|
2996 proof(cases "ks = []") |
|
2997 case True |
|
2998 have eq_ones: |
|
2999 "ones (u + 2) (u + 2 + int k) = (ones (u + 2) (u + 1 + int k) \<and>* one (u + 2 + int k))" |
|
3000 by (smt ones_rev) |
|
3001 have eq_ones': "(one (u + 2) \<and>* ones (3 + u) (u + 2 + int k)) = |
|
3002 (one (u + 2 + int k) \<and>* ones (u + 2) (u + 1 + int k))" |
|
3003 by (smt eq_ones ones.simps sep.mult_commute) |
|
3004 thus ?thesis |
|
3005 apply (insert True, simp del:ones_simps add:sep_conj_cond) |
|
3006 apply (rule tm.pre_condI, simp del:ones_simps, unfold eq_ones) |
|
3007 apply hsteps |
|
3008 apply (rule_tac p = "st j' \<and>* ps (u + 2 + int k) \<and>* zero u \<and>* |
|
3009 zero (u + 1) \<and>* ones (u + 2) (u + 2 + int k)" |
|
3010 in tm.pre_stren) |
|
3011 by (hsteps) |
|
3012 next |
|
3013 case False |
|
3014 from False have spt: "splited (ks @ [k]) ks [k]" by (unfold splited_def, auto) |
|
3015 show ?thesis |
|
3016 apply (unfold reps_splited[OF spt], simp del:ones_simps add:sep_conj_cond) |
|
3017 apply (rule tm.pre_condI, simp del:ones_simps) |
|
3018 apply (rule_tac q = "st i \<and>* |
|
3019 ps (1 + (u + int (reps_len ks))) \<and>* |
|
3020 zero u \<and>* |
|
3021 zero (u + 1) \<and>* |
|
3022 reps (u + 2) (1 + (u + int (reps_len ks))) ks \<and>* |
|
3023 zero (u + 2 + int (reps_len ks)) \<and>* |
|
3024 ones (3 + (u + int (reps_len ks))) (3 + (u + int (reps_len ks)) + int k)" in |
|
3025 tm.sequencing) |
|
3026 apply hsteps[1] |
|
3027 by (hstep snoc(1)) |
|
3028 qed |
|
3029 qed |
|
3030 qed |
|
3031 |
|
3032 lemma hoare_left_until_double_zero_gen[step]: |
|
3033 assumes h1: "ks \<noteq> []" |
|
3034 and h: "u = y" "w = v + 1" "x = v + 2" |
|
3035 shows "\<lbrace>st i ** ps u ** zero v ** zero w ** reps x y ks\<rbrace> |
|
3036 i:[left_until_double_zero]:j |
|
3037 \<lbrace>st j ** ps v ** zero v ** zero w ** reps x y ks\<rbrace>" |
|
3038 by (unfold h, rule hoare_left_until_double_zero[OF h1]) |
|
3039 |
|
3040 lemma hoare_jmp_reps1: |
|
3041 assumes "ks \<noteq> []" |
|
3042 shows "\<lbrace> st i \<and>* ps u \<and>* reps u v ks\<rbrace> |
|
3043 i:[jmp e]:j |
|
3044 \<lbrace> st e \<and>* ps u \<and>* reps u v ks\<rbrace>" |
|
3045 proof - |
|
3046 from assms obtain k ks' where Cons:"ks = k#ks'" |
|
3047 by (metis neq_Nil_conv) |
|
3048 thus ?thesis |
|
3049 proof(cases "ks' = []") |
|
3050 case True with Cons |
|
3051 show ?thesis |
|
3052 apply(simp add:sep_conj_cond reps.simps, intro tm.pre_condI, simp add:ones_simps) |
|
3053 by (hgoto hoare_jmp_gen) |
|
3054 next |
|
3055 case False |
|
3056 show ?thesis |
|
3057 apply (unfold `ks = k#ks'` reps_simp3[OF False], simp add:ones_simps) |
|
3058 by (hgoto hoare_jmp[where p = u]) |
|
3059 qed |
|
3060 qed |
|
3061 |
|
3062 lemma hoare_jmp_reps1_gen[step]: |
|
3063 assumes "ks \<noteq> []" "u = v" |
|
3064 shows "\<lbrace> st i \<and>* ps u \<and>* reps v w ks\<rbrace> |
|
3065 i:[jmp e]:j |
|
3066 \<lbrace> st e \<and>* ps u \<and>* reps v w ks\<rbrace>" |
|
3067 by (unfold assms, rule hoare_jmp_reps1[OF `ks \<noteq> []`]) |
|
3068 |
|
3069 lemma hoare_jmp_reps: |
|
3070 "\<lbrace> st i \<and>* ps u \<and>* reps u v ks \<and>* tm (v + 1) x \<rbrace> |
|
3071 i:[(jmp e; c)]:j |
|
3072 \<lbrace> st e \<and>* ps u \<and>* reps u v ks \<and>* tm (v + 1) x \<rbrace>" |
|
3073 proof(cases "ks") |
|
3074 case Nil |
|
3075 thus ?thesis |
|
3076 by (simp add:sep_conj_cond, intro tm.pre_condI, simp, hsteps) |
|
3077 next |
|
3078 case (Cons k ks') |
|
3079 thus ?thesis |
|
3080 proof(cases "ks' = []") |
|
3081 case True with Cons |
|
3082 show ?thesis |
|
3083 apply(simp add:sep_conj_cond, intro tm.pre_condI, simp) |
|
3084 by (hgoto hoare_jmp[where p = u]) |
|
3085 next |
|
3086 case False |
|
3087 show ?thesis |
|
3088 apply (unfold `ks = k#ks'` reps_simp3[OF False], simp) |
|
3089 by (hgoto hoare_jmp[where p = u]) |
|
3090 qed |
|
3091 qed |
|
3092 |
|
3093 definition "shift_right = |
|
3094 (TL start exit. |
|
3095 TLabel start; |
|
3096 if_zero exit; |
|
3097 write_zero; |
|
3098 move_right; |
|
3099 right_until_zero; |
|
3100 write_one; |
|
3101 move_right; |
|
3102 jmp start; |
|
3103 TLabel exit |
|
3104 )" |
|
3105 |
|
3106 lemma hoare_shift_right_cons: |
|
3107 assumes h: "ks \<noteq> []" |
|
3108 shows "\<lbrace>st i \<and>* ps u ** reps u v ks \<and>* zero (v + 1) \<and>* zero (v + 2) \<rbrace> |
|
3109 i:[shift_right]:j |
|
3110 \<lbrace>st j ** ps (v + 2) ** zero u ** reps (u + 1) (v + 1) ks ** zero (v + 2) \<rbrace>" |
|
3111 proof(unfold shift_right_def, intro t_hoare_local t_hoare_label, clarify, |
|
3112 rule t_hoare_label_last, auto) |
|
3113 fix la |
|
3114 have eq_ones: "\<And> u k. (one (u + int k + 1) \<and>* ones (u + 1) (u + int k)) = |
|
3115 (one (u + 1) \<and>* ones (2 + u) (u + 1 + int k))" |
|
3116 by (smt cond_true_eq2 ones.simps ones_rev sep.mult_assoc sep.mult_commute |
|
3117 sep.mult_left_commute sep_conj_assoc sep_conj_commute |
|
3118 sep_conj_cond1 sep_conj_cond2 sep_conj_cond3 sep_conj_left_commute |
|
3119 sep_conj_trivial_strip2) |
|
3120 show "\<lbrace>st i \<and>* ps u \<and>* reps u v ks \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace> |
|
3121 i :[ (if_zero la ; |
|
3122 write_zero ; move_right ; right_until_zero ; write_one ; move_right ; jmp i) ]: la |
|
3123 \<lbrace>st la \<and>* ps (v + 2) \<and>* zero u \<and>* reps (u + 1) (v + 1) ks \<and>* zero (v + 2)\<rbrace>" |
|
3124 using h |
|
3125 proof(induct ks arbitrary:i u v) |
|
3126 case (Cons k ks) |
|
3127 thus ?case |
|
3128 proof(cases "ks = []") |
|
3129 let ?j = la |
|
3130 case True |
|
3131 let ?body = "i :[ (if_zero ?j ; |
|
3132 write_zero ; |
|
3133 move_right ; |
|
3134 right_until_zero ; |
|
3135 write_one ; move_right ; jmp i) ]: ?j" |
|
3136 have first_interation: |
|
3137 "\<lbrace>st i \<and>* ps u \<and>* one u \<and>* ones (u + 1) (u + int k) \<and>* zero (u + int k + 1) \<and>* |
|
3138 zero (u + int k + 2)\<rbrace> |
|
3139 ?body |
|
3140 \<lbrace>st i \<and>* |
|
3141 ps (u + int k + 2) \<and>* |
|
3142 one (u + int k + 1) \<and>* ones (u + 1) (u + int k) \<and>* zero u \<and>* zero (u + int k + 2)\<rbrace>" |
|
3143 apply (hsteps) |
|
3144 by (simp add:sep_conj_ac, sep_cancel+, smt) |
|
3145 hence "\<lbrace>st i \<and>* ps u \<and>* one u \<and>* ones (u + 1) (u + int k) \<and>* zero (u + int k + 1) \<and>* |
|
3146 zero (u + int k + 2)\<rbrace> |
|
3147 ?body |
|
3148 \<lbrace>st ?j \<and>* ps (u + int k + 2) \<and>* zero u \<and>* one (u + 1) \<and>* |
|
3149 ones (2 + u) (u + 1 + int k) \<and>* zero (u + int k + 2)\<rbrace>" |
|
3150 proof(rule tm.sequencing) |
|
3151 show "\<lbrace>st i \<and>* |
|
3152 ps (u + int k + 2) \<and>* |
|
3153 one (u + int k + 1) \<and>* ones (u + 1) (u + int k) \<and>* zero u \<and>* zero (u + int k + 2)\<rbrace> |
|
3154 ?body |
|
3155 \<lbrace>st ?j \<and>* |
|
3156 ps (u + int k + 2) \<and>* |
|
3157 zero u \<and>* one (u + 1) \<and>* ones (2 + u) (u + 1 + int k) \<and>* zero (u + int k + 2)\<rbrace>" |
|
3158 apply (hgoto hoare_if_zero_true_gen) |
|
3159 by (simp add:sep_conj_ac eq_ones) |
|
3160 qed |
|
3161 with True |
|
3162 show ?thesis |
|
3163 by (simp, simp only:sep_conj_cond, intro tm.pre_condI, auto simp:sep_conj_ac) |
|
3164 next |
|
3165 case False |
|
3166 let ?j = la |
|
3167 let ?body = "i :[ (if_zero ?j ; |
|
3168 write_zero ; |
|
3169 move_right ; right_until_zero ; |
|
3170 write_one ; move_right ; jmp i) ]: ?j" |
|
3171 have eq_ones': |
|
3172 "(one (u + int k + 1) \<and>* |
|
3173 ones (u + 1) (u + int k) \<and>* |
|
3174 zero u \<and>* reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* zero (v + 2)) |
|
3175 = |
|
3176 (zero u \<and>* |
|
3177 ones (u + 1) (u + int k) \<and>* |
|
3178 one (u + int k + 1) \<and>* reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* zero (v + 2))" |
|
3179 by (simp add:eq_ones sep_conj_ac) |
|
3180 have "\<lbrace>st i \<and>* ps u \<and>* one u \<and>* ones (u + 1) (u + int k) \<and>* zero (u + int k + 1) \<and>* |
|
3181 reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace> |
|
3182 ?body |
|
3183 \<lbrace>st i \<and>* ps (u + int k + 2) \<and>* zero u \<and>* ones (u + 1) (u + int k) \<and>* |
|
3184 one (u + int k + 1) \<and>* reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace>" |
|
3185 apply (hsteps) |
|
3186 by (auto simp:sep_conj_ac, sep_cancel+, smt) |
|
3187 hence "\<lbrace>st i \<and>* ps u \<and>* one u \<and>* ones (u + 1) (u + int k) \<and>* zero (u + int k + 1) \<and>* |
|
3188 reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace> |
|
3189 ?body |
|
3190 \<lbrace>st ?j \<and>* ps (v + 2) \<and>* zero u \<and>* one (u + 1) \<and>* ones (2 + u) (u + 1 + int k) \<and>* |
|
3191 zero (2 + (u + int k)) \<and>* reps (3 + (u + int k)) (v + 1) ks \<and>* zero (v + 2)\<rbrace>" |
|
3192 proof(rule tm.sequencing) |
|
3193 have eq_ones': |
|
3194 "\<And> u k. (one (u + int k + 1) \<and>* ones (u + 1) (u + int k) \<and>* zero (u + int k + 2)) = |
|
3195 (one (u + 1) \<and>* zero (2 + (u + int k)) \<and>* ones (2 + u) (u + 1 + int k))" |
|
3196 by (smt eq_ones sep.mult_assoc sep_conj_commute) |
|
3197 show "\<lbrace>st i \<and>* ps (u + int k + 2) \<and>* zero u \<and>* |
|
3198 ones (u + 1) (u + int k) \<and>* one (u + int k + 1) \<and>* reps (u + int k + 2) v ks \<and>* |
|
3199 zero (v + 1) \<and>* zero (v + 2)\<rbrace> |
|
3200 ?body |
|
3201 \<lbrace>st ?j \<and>* ps (v + 2) \<and>* zero u \<and>* one (u + 1) \<and>* ones (2 + u) (u + 1 + int k) \<and>* |
|
3202 zero (2 + (u + int k)) \<and>* reps (3 + (u + int k)) (v + 1) ks \<and>* zero (v + 2)\<rbrace>" |
|
3203 apply (hsteps Cons.hyps) |
|
3204 by (simp add:sep_conj_ac eq_ones, sep_cancel+, smt) |
|
3205 qed |
|
3206 thus ?thesis |
|
3207 by (unfold reps_simp3[OF False], auto simp:sep_conj_ac) |
|
3208 qed |
|
3209 qed auto |
|
3210 qed |
|
3211 |
|
3212 lemma hoare_shift_right_cons_gen[step]: |
|
3213 assumes h: "ks \<noteq> []" |
|
3214 and h1: "u = v" "x = w + 1" "y = w + 2" |
|
3215 shows "\<lbrace>st i \<and>* ps u ** reps v w ks \<and>* zero x \<and>* zero y \<rbrace> |
|
3216 i:[shift_right]:j |
|
3217 \<lbrace>st j ** ps y ** zero v ** reps (v + 1) x ks ** zero y\<rbrace>" |
|
3218 by (unfold h1, rule hoare_shift_right_cons[OF h]) |
|
3219 |
|
3220 lemma shift_right_nil [step]: |
|
3221 assumes "u = v" |
|
3222 shows |
|
3223 "\<lbrace> st i \<and>* ps u \<and>* zero v \<rbrace> |
|
3224 i:[shift_right]:j |
|
3225 \<lbrace> st j \<and>* ps u \<and>* zero v \<rbrace>" |
|
3226 by (unfold assms shift_right_def, intro t_hoare_local t_hoare_label, clarify, |
|
3227 rule t_hoare_label_last, simp+, hstep) |
|
3228 |
|
3229 |
|
3230 text {* |
|
3231 @{text "clear_until_zero"} is useful to implement @{text "drag"}. |
|
3232 *} |
|
3233 |
|
3234 definition "clear_until_zero = |
|
3235 (TL start exit. |
|
3236 TLabel start; |
|
3237 if_zero exit; |
|
3238 write_zero; |
|
3239 move_right; |
|
3240 jmp start; |
|
3241 TLabel exit)" |
|
3242 |
|
3243 lemma hoare_clear_until_zero[step]: |
|
3244 "\<lbrace>st i ** ps u ** ones u v ** zero (v + 1)\<rbrace> |
|
3245 i :[clear_until_zero]: j |
|
3246 \<lbrace>st j ** ps (v + 1) ** zeros u v ** zero (v + 1)\<rbrace> " |
|
3247 proof(unfold clear_until_zero_def, intro t_hoare_local, rule t_hoare_label, |
|
3248 rule t_hoare_label_last, simp+) |
|
3249 let ?body = "i :[ (if_zero j ; write_zero ; move_right ; jmp i) ]: j" |
|
3250 show "\<lbrace>st i \<and>* ps u \<and>* ones u v \<and>* zero (v + 1)\<rbrace> ?body |
|
3251 \<lbrace>st j \<and>* ps (v + 1) \<and>* zeros u v \<and>* zero (v + 1)\<rbrace>" |
|
3252 proof(induct u v rule: zeros.induct) |
|
3253 fix ia ja |
|
3254 assume h: "\<not> ja < ia \<Longrightarrow> |
|
3255 \<lbrace>st i \<and>* ps (ia + 1) \<and>* ones (ia + 1) ja \<and>* zero (ja + 1)\<rbrace> ?body |
|
3256 \<lbrace>st j \<and>* ps (ja + 1) \<and>* zeros (ia + 1) ja \<and>* zero (ja + 1)\<rbrace>" |
|
3257 show "\<lbrace>st i \<and>* ps ia \<and>* ones ia ja \<and>* zero (ja + 1)\<rbrace> ?body |
|
3258 \<lbrace>st j \<and>* ps (ja + 1) \<and>* zeros ia ja \<and>* zero (ja + 1)\<rbrace>" |
|
3259 proof(cases "ja < ia") |
|
3260 case True |
|
3261 thus ?thesis |
|
3262 by (simp add: ones.simps zeros.simps sep_conj_ac, simp only:sep_conj_cond, |
|
3263 intro tm.pre_condI, simp, hsteps) |
|
3264 next |
|
3265 case False |
|
3266 note h[OF False, step] |
|
3267 from False have ones_eq: "ones ia ja = (one ia \<and>* ones (ia + 1) ja)" |
|
3268 by(simp add: ones.simps) |
|
3269 from False have zeros_eq: "zeros ia ja = (zero ia \<and>* zeros (ia + 1) ja)" |
|
3270 by(simp add: zeros.simps) |
|
3271 have s1: "\<lbrace>st i \<and>* ps ia \<and>* one ia \<and>* ones (ia + 1) ja \<and>* zero (ja + 1)\<rbrace> ?body |
|
3272 \<lbrace>st i \<and>* ps (ia + 1) \<and>* zero ia \<and>* ones (ia + 1) ja \<and>* zero (ja + 1)\<rbrace>" |
|
3273 proof(cases "ja < ia + 1") |
|
3274 case True |
|
3275 from True False have "ja = ia" by auto |
|
3276 thus ?thesis |
|
3277 apply(simp add: ones.simps) |
|
3278 by (hsteps) |
|
3279 next |
|
3280 case False |
|
3281 from False have "ones (ia + 1) ja = (one (ia + 1) \<and>* ones (ia + 1 + 1) ja)" |
|
3282 by(simp add: ones.simps) |
|
3283 thus ?thesis |
|
3284 by (simp, hsteps) |
|
3285 qed |
|
3286 have s2: "\<lbrace>st i \<and>* ps (ia + 1) \<and>* zero ia \<and>* ones (ia + 1) ja \<and>* zero (ja + 1)\<rbrace> |
|
3287 ?body |
|
3288 \<lbrace>st j \<and>* ps (ja + 1) \<and>* zero ia \<and>* zeros (ia + 1) ja \<and>* zero (ja + 1)\<rbrace>" |
|
3289 by hsteps |
|
3290 from tm.sequencing[OF s1 s2] have |
|
3291 "\<lbrace>st i \<and>* ps ia \<and>* one ia \<and>* ones (ia + 1) ja \<and>* zero (ja + 1)\<rbrace> ?body |
|
3292 \<lbrace>st j \<and>* ps (ja + 1) \<and>* zero ia \<and>* zeros (ia + 1) ja \<and>* zero (ja + 1)\<rbrace>" . |
|
3293 thus ?thesis |
|
3294 unfolding ones_eq zeros_eq by(simp add: sep_conj_ac) |
|
3295 qed |
|
3296 qed |
|
3297 qed |
|
3298 |
|
3299 lemma hoare_clear_until_zero_gen[step]: |
|
3300 assumes "u = v" "x = w + 1" |
|
3301 shows "\<lbrace>st i ** ps u ** ones v w ** zero x\<rbrace> |
|
3302 i :[clear_until_zero]: j |
|
3303 \<lbrace>st j ** ps x ** zeros v w ** zero x\<rbrace>" |
|
3304 by (unfold assms, rule hoare_clear_until_zero) |
|
3305 |
|
3306 definition "shift_left = |
|
3307 (TL start exit. |
|
3308 TLabel start; |
|
3309 if_zero exit; |
|
3310 move_left; |
|
3311 write_one; |
|
3312 right_until_zero; |
|
3313 move_left; |
|
3314 write_zero; |
|
3315 move_right; |
|
3316 move_right; |
|
3317 jmp start; |
|
3318 TLabel exit) |
|
3319 " |
|
3320 |
|
3321 declare ones_simps[simp del] |
|
3322 |
|
3323 lemma hoare_move_left_reps[step]: |
|
3324 assumes "ks \<noteq> []" "u = v" |
|
3325 shows |
|
3326 "\<lbrace>st i ** ps u ** reps v w ks\<rbrace> |
|
3327 i:[move_left]:j |
|
3328 \<lbrace>st j ** ps (u - 1) ** reps v w ks\<rbrace>" |
|
3329 proof - |
|
3330 from `ks \<noteq> []` obtain y ys where eq_ks: "ks = y#ys" |
|
3331 by (metis neq_Nil_conv) |
|
3332 show ?thesis |
|
3333 apply (unfold assms eq_ks) |
|
3334 apply (case_tac ys, simp) |
|
3335 my_block |
|
3336 have "(ones v (v + int y)) = (one v \<and>* ones (v + 1) (v + int y))" |
|
3337 by (smt ones_step_simp) |
|
3338 my_block_end |
|
3339 apply (unfold this, hsteps) |
|
3340 by (simp add:this, hsteps) |
|
3341 qed |
|
3342 |
|
3343 lemma hoare_shift_left_cons: |
|
3344 assumes h: "ks \<noteq> []" |
|
3345 shows "\<lbrace>st i \<and>* ps u \<and>* tm (u - 1) x \<and>* reps u v ks \<and>* zero (v + 1) \<and>* zero (v + 2) \<rbrace> |
|
3346 i:[shift_left]:j |
|
3347 \<lbrace>st j \<and>* ps (v + 2) \<and>* reps (u - 1) (v - 1) ks \<and>* zero v \<and>* zero (v + 1) \<and>* zero (v + 2) \<rbrace>" |
|
3348 proof(unfold shift_left_def, intro t_hoare_local t_hoare_label, clarify, |
|
3349 rule t_hoare_label_last, simp+, clarify, prune) |
|
3350 show " \<lbrace>st i \<and>* ps u \<and>* tm (u - 1) x \<and>* reps u v ks \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace> |
|
3351 i :[ (if_zero j ; |
|
3352 move_left ; |
|
3353 write_one ; |
|
3354 right_until_zero ; |
|
3355 move_left ; write_zero ; |
|
3356 move_right ; move_right ; jmp i) ]: j |
|
3357 \<lbrace>st j \<and>* ps (v + 2) \<and>* reps (u - 1) (v - 1) ks \<and>* zero v \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace>" |
|
3358 using h |
|
3359 proof(induct ks arbitrary:i u v x) |
|
3360 case (Cons k ks) |
|
3361 thus ?case |
|
3362 proof(cases "ks = []") |
|
3363 let ?body = "i :[ (if_zero j ; move_left ; write_one ; right_until_zero ; |
|
3364 move_left ; write_zero ; move_right ; move_right ; jmp i) ]: j" |
|
3365 case True |
|
3366 have "\<lbrace>st i \<and>* ps u \<and>* tm (u - 1) x \<and>* (one u \<and>* ones (u + 1) (u + int k)) \<and>* |
|
3367 zero (u + int k + 1) \<and>* zero (u + int k + 2)\<rbrace> |
|
3368 ?body |
|
3369 \<lbrace>st j \<and>* ps (u + int k + 2) \<and>* (one (u - 1) \<and>* ones u (u - 1 + int k)) \<and>* |
|
3370 zero (u + int k) \<and>* zero (u + int k + 1) \<and>* zero (u + int k + 2)\<rbrace>" |
|
3371 apply(rule tm.sequencing [where q = "st i \<and>* ps (u + int k + 2) \<and>* |
|
3372 (one (u - 1) \<and>* ones u (u - 1 + int k)) \<and>* |
|
3373 zero (u + int k) \<and>* zero (u + int k + 1) \<and>* zero (u + int k + 2)"]) |
|
3374 apply (hsteps) |
|
3375 apply (rule_tac p = "st j' \<and>* ps (u - 1) \<and>* ones (u - 1) (u + int k) \<and>* |
|
3376 zero (u + int k + 1) \<and>* zero (u + int k + 2)" |
|
3377 in tm.pre_stren) |
|
3378 apply (hsteps) |
|
3379 my_block |
|
3380 have "(ones (u - 1) (u + int k)) = (ones (u - 1) (u + int k - 1) \<and>* one (u + int k))" |
|
3381 by (smt ones_rev) |
|
3382 my_block_end |
|
3383 apply (unfold this) |
|
3384 apply hsteps |
|
3385 apply (simp add:sep_conj_ac, sep_cancel+) |
|
3386 apply (smt ones.simps sep.mult_assoc sep_conj_commuteI) |
|
3387 apply (simp add:sep_conj_ac)+ |
|
3388 apply (sep_cancel+) |
|
3389 apply (smt ones.simps sep.mult_left_commute sep_conj_commuteI this) |
|
3390 by hstep |
|
3391 with True show ?thesis |
|
3392 by (simp add:ones_simps, simp only:sep_conj_cond, intro tm.pre_condI, simp) |
|
3393 next |
|
3394 case False |
|
3395 let ?body = "i :[ (if_zero j ; move_left ; write_one ;right_until_zero ; move_left ; |
|
3396 write_zero ; move_right ; move_right ; jmp i) ]: j" |
|
3397 have "\<lbrace>st i \<and>* ps u \<and>* tm (u - 1) x \<and>* one u \<and>* ones (u + 1) (u + int k) \<and>* |
|
3398 zero (u + int k + 1) \<and>* reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace> |
|
3399 ?body |
|
3400 \<lbrace>st j \<and>* ps (v + 2) \<and>* one (u - 1) \<and>* ones u (u - 1 + int k) \<and>* |
|
3401 zero (u + int k) \<and>* reps (1 + (u + int k)) (v - 1) ks \<and>* |
|
3402 zero v \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace>" |
|
3403 apply (rule tm.sequencing[where q = "st i \<and>* ps (u + int k + 2) \<and>* |
|
3404 zero (u + int k + 1) \<and>* reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* |
|
3405 zero (v + 2) \<and>* one (u - 1) \<and>* ones u (u - 1 + int k) \<and>* zero (u + int k)"]) |
|
3406 apply (hsteps) |
|
3407 apply (rule_tac p = "st j' \<and>* ps (u - 1) \<and>* |
|
3408 ones (u - 1) (u + int k) \<and>* |
|
3409 zero (u + int k + 1) \<and>* |
|
3410 reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* zero (v + 2) |
|
3411 " in tm.pre_stren) |
|
3412 apply hsteps |
|
3413 my_block |
|
3414 have "(ones (u - 1) (u + int k)) = |
|
3415 (ones (u - 1) (u + int k - 1) \<and>* one (u + int k))" |
|
3416 by (smt ones_rev) |
|
3417 my_block_end |
|
3418 apply (unfold this) |
|
3419 apply (hsteps) |
|
3420 apply (sep_cancel+) |
|
3421 apply (smt ones.simps sep.mult_assoc sep_conj_commuteI) |
|
3422 apply (sep_cancel+) |
|
3423 apply (smt ones.simps this) |
|
3424 my_block |
|
3425 have eq_u: "1 + (u + int k) = u + int k + 1" by simp |
|
3426 from Cons.hyps[OF `ks \<noteq> []`, of i "u + int k + 2" Bk v, folded zero_def] |
|
3427 have "\<lbrace>st i \<and>* ps (u + int k + 2) \<and>* zero (u + int k + 1) \<and>* |
|
3428 reps (u + int k + 2) v ks \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace> |
|
3429 ?body |
|
3430 \<lbrace>st j \<and>* ps (v + 2) \<and>* reps (1 + (u + int k)) (v - 1) ks \<and>* |
|
3431 zero v \<and>* zero (v + 1) \<and>* zero (v + 2)\<rbrace>" |
|
3432 by (simp add:eq_u) |
|
3433 my_block_end my_note hh[step] = this |
|
3434 by hsteps |
|
3435 thus ?thesis |
|
3436 by (unfold reps_simp3[OF False], auto simp:sep_conj_ac ones_simps) |
|
3437 qed |
|
3438 qed auto |
|
3439 qed |
|
3440 |
|
3441 lemma hoare_shift_left_cons_gen[step]: |
|
3442 assumes h: "ks \<noteq> []" |
|
3443 "v = u - 1" "w = u" "y = x + 1" "z = x + 2" |
|
3444 shows "\<lbrace>st i \<and>* ps u \<and>* tm v vv \<and>* reps w x ks \<and>* tm y Bk \<and>* tm z Bk\<rbrace> |
|
3445 i:[shift_left]:j |
|
3446 \<lbrace>st j \<and>* ps z \<and>* reps v (x - 1) ks \<and>* zero x \<and>* zero y \<and>* zero z \<rbrace>" |
|
3447 by (unfold assms, fold zero_def, rule hoare_shift_left_cons[OF `ks \<noteq> []`]) |
|
3448 |
|
3449 definition "bone c1 c2 = (TL exit l_one. |
|
3450 if_one l_one; |
|
3451 (c1; |
|
3452 jmp exit); |
|
3453 TLabel l_one; |
|
3454 c2; |
|
3455 TLabel exit |
|
3456 )" |
|
3457 |
|
3458 lemma hoare_bone_1_out: |
|
3459 assumes h: |
|
3460 "\<And> i j . \<lbrace>st i \<and>* ps u \<and>* zero u \<and>* p \<rbrace> |
|
3461 i:[c1]:j |
|
3462 \<lbrace>st e \<and>* q \<rbrace> |
|
3463 " |
|
3464 shows "\<lbrace>st i \<and>* ps u \<and>* zero u \<and>* p \<rbrace> |
|
3465 i:[(bone c1 c2)]:j |
|
3466 \<lbrace>st e \<and>* q \<rbrace> |
|
3467 " |
|
3468 apply (unfold bone_def, intro t_hoare_local) |
|
3469 apply hsteps |
|
3470 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension) |
|
3471 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension) |
|
3472 by (rule h) |
|
3473 |
|
3474 lemma hoare_bone_1: |
|
3475 assumes h: |
|
3476 "\<And> i j . \<lbrace>st i \<and>* ps u \<and>* zero u \<and>* p \<rbrace> |
|
3477 i:[c1]:j |
|
3478 \<lbrace>st j \<and>* ps v \<and>* tm v x \<and>* q \<rbrace> |
|
3479 " |
|
3480 shows "\<lbrace>st i \<and>* ps u \<and>* zero u \<and>* p \<rbrace> |
|
3481 i:[(bone c1 c2)]:j |
|
3482 \<lbrace>st j \<and>* ps v \<and>* tm v x \<and>* q \<rbrace> |
|
3483 " |
|
3484 proof - |
|
3485 note h[step] |
|
3486 show ?thesis |
|
3487 apply (unfold bone_def, intro t_hoare_local) |
|
3488 apply (rule t_hoare_label_last, auto) |
|
3489 apply hsteps |
|
3490 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension) |
|
3491 by hsteps |
|
3492 qed |
|
3493 |
|
3494 lemma hoare_bone_2: |
|
3495 assumes h: |
|
3496 "\<And> i j . \<lbrace>st i \<and>* ps u \<and>* one u \<and>* p \<rbrace> |
|
3497 i:[c2]:j |
|
3498 \<lbrace>st j \<and>* q \<rbrace> |
|
3499 " |
|
3500 shows "\<lbrace>st i \<and>* ps u \<and>* one u \<and>* p \<rbrace> |
|
3501 i:[(bone c1 c2)]:j |
|
3502 \<lbrace>st j \<and>* q \<rbrace> |
|
3503 " |
|
3504 apply (unfold bone_def, intro t_hoare_local) |
|
3505 apply (rule_tac q = "st la \<and>* ps u \<and>* one u \<and>* p" in tm.sequencing) |
|
3506 apply hsteps |
|
3507 apply (subst tassemble_to.simps(2), intro tm.code_exI, intro tm.code_extension1) |
|
3508 apply (subst tassemble_to.simps(2), intro tm.code_exI, intro tm.code_extension1) |
|
3509 apply (subst tassemble_to.simps(2), intro tm.code_exI) |
|
3510 apply (subst tassemble_to.simps(4), intro tm.code_condI, simp) |
|
3511 apply (subst tassemble_to.simps(2), intro tm.code_exI) |
|
3512 apply (subst tassemble_to.simps(4), simp add:sep_conj_cond, rule tm.code_condI, simp) |
|
3513 by (rule h) |
|
3514 |
|
3515 lemma hoare_bone_2_out: |
|
3516 assumes h: |
|
3517 "\<And> i j . \<lbrace>st i \<and>* ps u \<and>* one u \<and>* p \<rbrace> |
|
3518 i:[c2]:j |
|
3519 \<lbrace>st e \<and>* q \<rbrace> |
|
3520 " |
|
3521 shows "\<lbrace>st i \<and>* ps u \<and>* one u \<and>* p \<rbrace> |
|
3522 i:[(bone c1 c2)]:j |
|
3523 \<lbrace>st e \<and>* q \<rbrace> |
|
3524 " |
|
3525 apply (unfold bone_def, intro t_hoare_local) |
|
3526 apply (rule_tac q = "st la \<and>* ps u \<and>* one u \<and>* p" in tm.sequencing) |
|
3527 apply hsteps |
|
3528 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension1) |
|
3529 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension1) |
|
3530 apply (subst tassemble_to.simps(2), intro tm.code_exI) |
|
3531 apply (subst tassemble_to.simps(4), rule tm.code_condI, simp) |
|
3532 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension) |
|
3533 by (rule h) |
|
3534 |
|
3535 definition "bzero c1 c2 = (TL exit l_zero. |
|
3536 if_zero l_zero; |
|
3537 (c1; |
|
3538 jmp exit); |
|
3539 TLabel l_zero; |
|
3540 c2; |
|
3541 TLabel exit |
|
3542 )" |
|
3543 |
|
3544 lemma hoare_bzero_1: |
|
3545 assumes h[step]: |
|
3546 "\<And> i j . \<lbrace>st i \<and>* ps u \<and>* one u \<and>* p \<rbrace> |
|
3547 i:[c1]:j |
|
3548 \<lbrace>st j \<and>* ps v \<and>* tm v x \<and>* q \<rbrace> |
|
3549 " |
|
3550 shows "\<lbrace>st i \<and>* ps u \<and>* one u \<and>* p \<rbrace> |
|
3551 i:[(bzero c1 c2)]:j |
|
3552 \<lbrace>st j \<and>* ps v \<and>* tm v x \<and>* q \<rbrace> |
|
3553 " |
|
3554 apply (unfold bzero_def, intro t_hoare_local) |
|
3555 apply hsteps |
|
3556 apply (rule_tac c = " ((c1 ; jmp l) ; TLabel la ; c2 ; TLabel l)" in t_hoare_label_last, auto) |
|
3557 apply (subst tassemble_to.simps(2), intro tm.code_exI, intro tm.code_extension) |
|
3558 by hsteps |
|
3559 |
|
3560 lemma hoare_bzero_1_out: |
|
3561 assumes h[step]: |
|
3562 "\<And> i j . \<lbrace>st i \<and>* ps u \<and>* one u \<and>* p \<rbrace> |
|
3563 i:[c1]:j |
|
3564 \<lbrace>st e \<and>* q \<rbrace> |
|
3565 " |
|
3566 shows "\<lbrace>st i \<and>* ps u \<and>* one u \<and>* p \<rbrace> |
|
3567 i:[(bzero c1 c2)]:j |
|
3568 \<lbrace>st e \<and>* q \<rbrace> |
|
3569 " |
|
3570 apply (unfold bzero_def, intro t_hoare_local) |
|
3571 apply hsteps |
|
3572 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension) |
|
3573 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension) |
|
3574 by (rule h) |
|
3575 |
|
3576 lemma hoare_bzero_2: |
|
3577 assumes h: |
|
3578 "\<And> i j. \<lbrace>st i \<and>* ps u \<and>* zero u \<and>* p \<rbrace> |
|
3579 i:[c2]:j |
|
3580 \<lbrace>st j \<and>* q \<rbrace> |
|
3581 " |
|
3582 shows "\<lbrace>st i \<and>* ps u \<and>* zero u \<and>* p \<rbrace> |
|
3583 i:[(bzero c1 c2)]:j |
|
3584 \<lbrace>st j \<and>* q \<rbrace> |
|
3585 " |
|
3586 apply (unfold bzero_def, intro t_hoare_local) |
|
3587 apply (rule_tac q = "st la \<and>* ps u \<and>* zero u \<and>* p" in tm.sequencing) |
|
3588 apply hsteps |
|
3589 apply (subst tassemble_to.simps(2), intro tm.code_exI, intro tm.code_extension1) |
|
3590 apply (subst tassemble_to.simps(2), intro tm.code_exI, intro tm.code_extension1) |
|
3591 apply (subst tassemble_to.simps(2), intro tm.code_exI) |
|
3592 apply (subst tassemble_to.simps(4)) |
|
3593 apply (rule tm.code_condI, simp) |
|
3594 apply (subst tassemble_to.simps(2)) |
|
3595 apply (rule tm.code_exI) |
|
3596 apply (subst tassemble_to.simps(4), simp add:sep_conj_cond) |
|
3597 apply (rule tm.code_condI, simp) |
|
3598 by (rule h) |
|
3599 |
|
3600 lemma hoare_bzero_2_out: |
|
3601 assumes h: |
|
3602 "\<And> i j . \<lbrace>st i \<and>* ps u \<and>* zero u \<and>* p \<rbrace> |
|
3603 i:[c2]:j |
|
3604 \<lbrace>st e \<and>* q \<rbrace> |
|
3605 " |
|
3606 shows "\<lbrace>st i \<and>* ps u \<and>* zero u \<and>* p\<rbrace> |
|
3607 i:[(bzero c1 c2)]:j |
|
3608 \<lbrace>st e \<and>* q \<rbrace> |
|
3609 " |
|
3610 apply (unfold bzero_def, intro t_hoare_local) |
|
3611 apply (rule_tac q = "st la \<and>* ps u \<and>* zero u \<and>* p" in tm.sequencing) |
|
3612 apply hsteps |
|
3613 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension1) |
|
3614 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension1) |
|
3615 apply (subst tassemble_to.simps(2), intro tm.code_exI) |
|
3616 apply (subst tassemble_to.simps(4), rule tm.code_condI, simp) |
|
3617 apply (subst tassemble_to.simps(2), intro tm.code_exI, rule tm.code_extension) |
|
3618 by (rule h) |
|
3619 |
|
3620 definition "skip_or_set = bone (write_one; move_right; move_right) |
|
3621 (right_until_zero; move_right)" |
|
3622 |
|
3623 lemma reps_len_split: |
|
3624 assumes "xs \<noteq> []" "ys \<noteq> []" |
|
3625 shows "reps_len (xs @ ys) = reps_len xs + reps_len ys + 1" |
|
3626 using assms |
|
3627 proof(induct xs arbitrary:ys) |
|
3628 case (Cons x1 xs1) |
|
3629 show ?case |
|
3630 proof(cases "xs1 = []") |
|
3631 case True |
|
3632 thus ?thesis |
|
3633 by (simp add:reps_len_cons[OF `ys \<noteq> []`] reps_len_sg) |
|
3634 next |
|
3635 case False |
|
3636 hence " xs1 @ ys \<noteq> []" by simp |
|
3637 thus ?thesis |
|
3638 apply (simp add:reps_len_cons[OF `xs1@ys \<noteq> []`] reps_len_cons[OF `xs1 \<noteq> []`]) |
|
3639 by (simp add: Cons.hyps[OF `xs1 \<noteq> []` `ys \<noteq> []`]) |
|
3640 qed |
|
3641 qed auto |
|
3642 |
|
3643 lemma hoare_skip_or_set_set: |
|
3644 "\<lbrace> st i \<and>* ps u \<and>* zero u \<and>* zero (u + 1) \<and>* tm (u + 2) x\<rbrace> |
|
3645 i:[skip_or_set]:j |
|
3646 \<lbrace> st j \<and>* ps (u + 2) \<and>* one u \<and>* zero (u + 1) \<and>* tm (u + 2) x\<rbrace>" |
|
3647 apply(unfold skip_or_set_def) |
|
3648 apply(rule_tac q = "st j \<and>* ps (u + 2) \<and>* tm (u + 2) x \<and>* one u \<and>* zero (u + 1)" |
|
3649 in tm.post_weaken) |
|
3650 apply(rule hoare_bone_1) |
|
3651 apply hsteps |
|
3652 by (auto simp:sep_conj_ac, sep_cancel+, smt) |
|
3653 |
|
3654 lemma hoare_skip_or_set_set_gen[step]: |
|
3655 assumes "u = v" "w = v + 1" "x = v + 2" |
|
3656 shows "\<lbrace>st i \<and>* ps u \<and>* zero v \<and>* zero w \<and>* tm x xv\<rbrace> |
|
3657 i:[skip_or_set]:j |
|
3658 \<lbrace>st j \<and>* ps x \<and>* one v \<and>* zero w \<and>* tm x xv\<rbrace>" |
|
3659 by (unfold assms, rule hoare_skip_or_set_set) |
|
3660 |
|
3661 lemma hoare_skip_or_set_skip: |
|
3662 "\<lbrace> st i \<and>* ps u \<and>* reps u v [k] \<and>* zero (v + 1)\<rbrace> |
|
3663 i:[skip_or_set]:j |
|
3664 \<lbrace> st j \<and>* ps (v + 2) \<and>* reps u v [k] \<and>* zero (v + 1)\<rbrace>" |
|
3665 proof - |
|
3666 show ?thesis |
|
3667 apply(unfold skip_or_set_def, unfold reps.simps, simp add:sep_conj_cond) |
|
3668 apply(rule tm.pre_condI, simp) |
|
3669 apply(rule_tac p = "st i \<and>* ps u \<and>* one u \<and>* ones (u + 1) (u + int k) \<and>* |
|
3670 zero (u + int k + 1)" |
|
3671 in tm.pre_stren) |
|
3672 apply (rule_tac q = "st j \<and>* ps (u + int k + 2) \<and>* |
|
3673 one u \<and>* ones (u + 1) (u + int k) \<and>* zero (u + int k + 1) |
|
3674 " in tm.post_weaken) |
|
3675 apply (rule hoare_bone_2) |
|
3676 apply (rule_tac p = " st i \<and>* ps u \<and>* ones u (u + int k) \<and>* zero (u + int k + 1) |
|
3677 " in tm.pre_stren) |
|
3678 apply hsteps |
|
3679 apply (simp add:sep_conj_ac, sep_cancel+, auto simp:sep_conj_ac ones_simps) |
|
3680 by (sep_cancel+, smt) |
|
3681 qed |
|
3682 |
|
3683 lemma hoare_skip_or_set_skip_gen[step]: |
|
3684 assumes "u = v" "x = w + 1" |
|
3685 shows "\<lbrace> st i \<and>* ps u \<and>* reps v w [k] \<and>* zero x\<rbrace> |
|
3686 i:[skip_or_set]:j |
|
3687 \<lbrace> st j \<and>* ps (w + 2) \<and>* reps v w [k] \<and>* zero x\<rbrace>" |
|
3688 by (unfold assms, rule hoare_skip_or_set_skip) |
|
3689 |
|
3690 |
|
3691 definition "if_reps_z e = (move_right; |
|
3692 bone (move_left; jmp e) (move_left) |
|
3693 )" |
|
3694 |
|
3695 lemma hoare_if_reps_z_true: |
|
3696 assumes h: "k = 0" |
|
3697 shows |
|
3698 "\<lbrace>st i \<and>* ps u \<and>* reps u v [k] \<and>* zero (v + 1)\<rbrace> |
|
3699 i:[if_reps_z e]:j |
|
3700 \<lbrace>st e \<and>* ps u \<and>* reps u v [k] \<and>* zero (v + 1)\<rbrace>" |
|
3701 apply (unfold reps.simps, simp add:sep_conj_cond) |
|
3702 apply (rule tm.pre_condI, simp add:h) |
|
3703 apply (unfold if_reps_z_def) |
|
3704 apply (simp add:ones_simps) |
|
3705 apply (hsteps) |
|
3706 apply (rule_tac p = "st j' \<and>* ps (u + 1) \<and>* zero (u + 1) \<and>* one u" in tm.pre_stren) |
|
3707 apply (rule hoare_bone_1_out) |
|
3708 by (hsteps) |
|
3709 |
|
3710 lemma hoare_if_reps_z_true_gen[step]: |
|
3711 assumes "k = 0" "u = v" "x = w + 1" |
|
3712 shows "\<lbrace>st i \<and>* ps u \<and>* reps v w [k] \<and>* zero x\<rbrace> |
|
3713 i:[if_reps_z e]:j |
|
3714 \<lbrace>st e \<and>* ps u \<and>* reps v w [k] \<and>* zero x\<rbrace>" |
|
3715 by (unfold assms, rule hoare_if_reps_z_true, simp) |
|
3716 |
|
3717 lemma hoare_if_reps_z_false: |
|
3718 assumes h: "k \<noteq> 0" |
|
3719 shows |
|
3720 "\<lbrace>st i \<and>* ps u \<and>* reps u v [k]\<rbrace> |
|
3721 i:[if_reps_z e]:j |
|
3722 \<lbrace>st j \<and>* ps u \<and>* reps u v [k]\<rbrace>" |
|
3723 proof - |
|
3724 from h obtain k' where "k = Suc k'" by (metis not0_implies_Suc) |
|
3725 show ?thesis |
|
3726 apply (unfold `k = Suc k'`) |
|
3727 apply (simp add:sep_conj_cond, rule tm.pre_condI, simp) |
|
3728 apply (unfold if_reps_z_def) |
|
3729 apply (simp add:ones_simps) |
|
3730 apply hsteps |
|
3731 apply (rule_tac p = "st j' \<and>* ps (u + 1) \<and>* one (u + 1) \<and>* one u \<and>* |
|
3732 ones (2 + u) (u + (1 + int k'))" in tm.pre_stren) |
|
3733 apply (rule_tac hoare_bone_2) |
|
3734 by (hsteps) |
|
3735 qed |
|
3736 |
|
3737 lemma hoare_if_reps_z_false_gen[step]: |
|
3738 assumes h: "k \<noteq> 0" "u = v" |
|
3739 shows |
|
3740 "\<lbrace>st i \<and>* ps u \<and>* reps v w [k]\<rbrace> |
|
3741 i:[if_reps_z e]:j |
|
3742 \<lbrace>st j \<and>* ps u \<and>* reps v w [k]\<rbrace>" |
|
3743 by (unfold assms, rule hoare_if_reps_z_false[OF `k \<noteq> 0`]) |
|
3744 |
|
3745 definition "if_reps_nz e = (move_right; |
|
3746 bzero (move_left; jmp e) (move_left) |
|
3747 )" |
|
3748 |
|
3749 lemma EXS_postI: |
|
3750 assumes "\<lbrace>P\<rbrace> |
|
3751 c |
|
3752 \<lbrace>Q x\<rbrace>" |
|
3753 shows "\<lbrace>P\<rbrace> |
|
3754 c |
|
3755 \<lbrace>EXS x. Q x\<rbrace>" |
|
3756 by (metis EXS_intro assms tm.hoare_adjust) |
|
3757 |
|
3758 lemma hoare_if_reps_nz_true: |
|
3759 assumes h: "k \<noteq> 0" |
|
3760 shows |
|
3761 "\<lbrace>st i \<and>* ps u \<and>* reps u v [k]\<rbrace> |
|
3762 i:[if_reps_nz e]:j |
|
3763 \<lbrace>st e \<and>* ps u \<and>* reps u v [k]\<rbrace>" |
|
3764 proof - |
|
3765 from h obtain k' where "k = Suc k'" by (metis not0_implies_Suc) |
|
3766 show ?thesis |
|
3767 apply (unfold `k = Suc k'`) |
|
3768 apply (unfold reps.simps, simp add:sep_conj_cond, rule tm.pre_condI, simp) |
|
3769 apply (unfold if_reps_nz_def) |
|
3770 apply (simp add:ones_simps) |
|
3771 apply hsteps |
|
3772 apply (rule_tac p = "st j' \<and>* ps (u + 1) \<and>* one (u + 1) \<and>* one u \<and>* |
|
3773 ones (2 + u) (u + (1 + int k'))" in tm.pre_stren) |
|
3774 apply (rule hoare_bzero_1_out) |
|
3775 by hsteps |
|
3776 qed |
|
3777 |
|
3778 |
|
3779 lemma hoare_if_reps_nz_true_gen[step]: |
|
3780 assumes h: "k \<noteq> 0" "u = v" |
|
3781 shows |
|
3782 "\<lbrace>st i \<and>* ps u \<and>* reps v w [k]\<rbrace> |
|
3783 i:[if_reps_nz e]:j |
|
3784 \<lbrace>st e \<and>* ps u \<and>* reps v w [k]\<rbrace>" |
|
3785 by (unfold assms, rule hoare_if_reps_nz_true[OF `k\<noteq> 0`]) |
|
3786 |
|
3787 lemma hoare_if_reps_nz_false: |
|
3788 assumes h: "k = 0" |
|
3789 shows |
|
3790 "\<lbrace>st i \<and>* ps u \<and>* reps u v [k] \<and>* zero (v + 1)\<rbrace> |
|
3791 i:[if_reps_nz e]:j |
|
3792 \<lbrace>st j \<and>* ps u \<and>* reps u v [k] \<and>* zero (v + 1)\<rbrace>" |
|
3793 apply (simp add:h sep_conj_cond) |
|
3794 apply (rule tm.pre_condI, simp) |
|
3795 apply (unfold if_reps_nz_def) |
|
3796 apply (simp add:ones_simps) |
|
3797 apply (hsteps) |
|
3798 apply (rule_tac p = "st j' \<and>* ps (u + 1) \<and>* zero (u + 1) \<and>* one u" in tm.pre_stren) |
|
3799 apply (rule hoare_bzero_2) |
|
3800 by (hsteps) |
|
3801 |
|
3802 lemma hoare_if_reps_nz_false_gen[step]: |
|
3803 assumes h: "k = 0" "u = v" "x = w + 1" |
|
3804 shows |
|
3805 "\<lbrace>st i \<and>* ps u \<and>* reps v w [k] \<and>* zero x\<rbrace> |
|
3806 i:[if_reps_nz e]:j |
|
3807 \<lbrace>st j \<and>* ps u \<and>* reps v w [k] \<and>* zero x\<rbrace>" |
|
3808 by (unfold assms, rule hoare_if_reps_nz_false, simp) |
|
3809 |
|
3810 definition "skip_or_sets n = tpg_fold (replicate n skip_or_set)" |
|
3811 |
|
3812 |
|
3813 |
|
3814 lemma hoare_skip_or_sets_set: |
|
3815 shows "\<lbrace>st i \<and>* ps u \<and>* zeros u (u + int (reps_len (replicate (Suc n) 0))) \<and>* |
|
3816 tm (u + int (reps_len (replicate (Suc n) 0)) + 1) x\<rbrace> |
|
3817 i:[skip_or_sets (Suc n)]:j |
|
3818 \<lbrace>st j \<and>* ps (u + int (reps_len (replicate (Suc n) 0)) + 1) \<and>* |
|
3819 reps' u (u + int (reps_len (replicate (Suc n) 0))) (replicate (Suc n) 0) \<and>* |
|
3820 tm (u + int (reps_len (replicate (Suc n) 0)) + 1) x \<rbrace>" |
|
3821 proof(induct n arbitrary:i j u x) |
|
3822 case 0 |
|
3823 from 0 show ?case |
|
3824 apply (simp add:reps'_def reps_len_def reps_ctnt_len_def reps_sep_len_def reps.simps) |
|
3825 apply (unfold skip_or_sets_def, simp add:tpg_fold_sg) |
|
3826 apply hsteps |
|
3827 by (auto simp:sep_conj_ac, smt cond_true_eq2 ones.simps sep_conj_left_commute) |
|
3828 next |
|
3829 case (Suc n) |
|
3830 { fix n |
|
3831 have "listsum (replicate n (Suc 0)) = n" |
|
3832 by (induct n, auto) |
|
3833 } note eq_sum = this |
|
3834 have eq_len: "\<And>n. n \<noteq> 0 \<Longrightarrow> reps_len (replicate (Suc n) 0) = reps_len (replicate n 0) + 2" |
|
3835 by (simp add:reps_len_def reps_sep_len_def reps_ctnt_len_def) |
|
3836 have eq_zero: "\<And> u v. (zeros u (u + int (v + 2))) = |
|
3837 (zeros u (u + (int v)) \<and>* zero (u + (int v) + 1) \<and>* zero (u + (int v) + 2))" |
|
3838 by (smt sep.mult_assoc zeros_rev) |
|
3839 hence eq_z: |
|
3840 "zeros u (u + int (reps_len (replicate (Suc (Suc n)) 0))) = |
|
3841 (zeros u (u + int (reps_len (replicate (Suc n) 0))) \<and>* |
|
3842 zero ((u + int (reps_len (replicate (Suc n) 0))) + 1) \<and>* |
|
3843 zero ((u + int (reps_len (replicate (Suc n) 0))) + 2)) |
|
3844 " by (simp only:eq_len) |
|
3845 have hh: "\<And>x. (replicate (Suc (Suc n)) x) = (replicate (Suc n) x) @ [x]" |
|
3846 by (metis replicate_Suc replicate_append_same) |
|
3847 have hhh: "replicate (Suc n) skip_or_set \<noteq> []" "[skip_or_set] \<noteq> []" by auto |
|
3848 have eq_code: |
|
3849 "(i :[ skip_or_sets (Suc (Suc n)) ]: j) = |
|
3850 (i :[ (skip_or_sets (Suc n); skip_or_set) ]: j)" |
|
3851 proof(unfold skip_or_sets_def) |
|
3852 show "i :[ tpg_fold (replicate (Suc (Suc n)) skip_or_set) ]: j = |
|
3853 i :[ (tpg_fold (replicate (Suc n) skip_or_set) ; skip_or_set) ]: j" |
|
3854 apply (insert tpg_fold_app[OF hhh, of i j], unfold hh) |
|
3855 by (simp only:tpg_fold_sg) |
|
3856 qed |
|
3857 have "Suc n \<noteq> 0" by simp |
|
3858 show ?case |
|
3859 apply (unfold eq_z eq_code) |
|
3860 apply (hstep Suc(1)) |
|
3861 apply (unfold eq_len[OF `Suc n \<noteq> 0`]) |
|
3862 apply hstep |
|
3863 apply (auto simp:sep_conj_ac)[1] |
|
3864 apply (sep_cancel+, prune) |
|
3865 apply (fwd abs_ones) |
|
3866 apply ((fwd abs_reps')+, simp add:int_add_ac) |
|
3867 by (metis replicate_append_same) |
|
3868 qed |
|
3869 |
|
3870 lemma hoare_skip_or_sets_set_gen[step]: |
|
3871 assumes h: "p2 = p1" |
|
3872 "p3 = p1 + int (reps_len (replicate (Suc n) 0))" |
|
3873 "p4 = p3 + 1" |
|
3874 shows "\<lbrace>st i \<and>* ps p1 \<and>* zeros p2 p3 \<and>* tm p4 x\<rbrace> |
|
3875 i:[skip_or_sets (Suc n)]:j |
|
3876 \<lbrace>st j \<and>* ps p4 \<and>* reps' p2 p3 (replicate (Suc n) 0) \<and>* tm p4 x\<rbrace>" |
|
3877 apply (unfold h) |
|
3878 by (rule hoare_skip_or_sets_set) |
|
3879 |
|
3880 declare reps.simps[simp del] |
|
3881 |
|
3882 lemma hoare_skip_or_sets_skip: |
|
3883 assumes h: "n < length ks" |
|
3884 shows "\<lbrace>st i \<and>* ps u \<and>* reps' u v (take n ks) \<and>* reps' (v + 1) w [ks!n] \<rbrace> |
|
3885 i:[skip_or_sets (Suc n)]:j |
|
3886 \<lbrace>st j \<and>* ps (w+1) \<and>* reps' u v (take n ks) \<and>* reps' (v + 1) w [ks!n]\<rbrace>" |
|
3887 using h |
|
3888 proof(induct n arbitrary: i j u v w ks) |
|
3889 case 0 |
|
3890 show ?case |
|
3891 apply (subst (1 5) reps'_def, simp add:sep_conj_cond, intro tm.pre_condI, simp) |
|
3892 apply (unfold skip_or_sets_def, simp add:tpg_fold_sg) |
|
3893 apply (unfold reps'_def, simp del:reps.simps) |
|
3894 apply hsteps |
|
3895 by (sep_cancel+, smt+) |
|
3896 next |
|
3897 case (Suc n) |
|
3898 from `Suc n < length ks` have "n < length ks" by auto |
|
3899 note h = Suc(1) [OF this] |
|
3900 show ?case |
|
3901 my_block |
|
3902 from `Suc n < length ks` |
|
3903 have eq_take: "take (Suc n) ks = take n ks @ [ks!n]" |
|
3904 by (metis not_less_eq not_less_iff_gr_or_eq take_Suc_conv_app_nth) |
|
3905 my_block_end |
|
3906 apply (unfold this) |
|
3907 apply (subst reps'_append, simp add:sep_conj_exists, rule tm.precond_exI) |
|
3908 my_block |
|
3909 have "(i :[ skip_or_sets (Suc (Suc n)) ]: j) = |
|
3910 (i :[ (skip_or_sets (Suc n); skip_or_set )]: j)" |
|
3911 proof - |
|
3912 have eq_rep: |
|
3913 "(replicate (Suc (Suc n)) skip_or_set) = ((replicate (Suc n) skip_or_set) @ [skip_or_set])" |
|
3914 by (metis replicate_Suc replicate_append_same) |
|
3915 have "replicate (Suc n) skip_or_set \<noteq> []" "[skip_or_set] \<noteq> []" by auto |
|
3916 from tpg_fold_app[OF this] |
|
3917 show ?thesis |
|
3918 by (unfold skip_or_sets_def eq_rep, simp del:replicate.simps add:tpg_fold_sg) |
|
3919 qed |
|
3920 my_block_end |
|
3921 apply (unfold this) |
|
3922 my_block |
|
3923 fix i j m |
|
3924 have "\<lbrace>st i \<and>* ps u \<and>* (reps' u (m - 1) (take n ks) \<and>* reps' m v [ks ! n])\<rbrace> |
|
3925 i :[ (skip_or_sets (Suc n)) ]: j |
|
3926 \<lbrace>st j \<and>* ps (v + 1) \<and>* (reps' u (m - 1) (take n ks) \<and>* reps' m v [ks ! n])\<rbrace>" |
|
3927 apply (rule h[THEN tm.hoare_adjust]) |
|
3928 by (sep_cancel+, auto) |
|
3929 my_block_end my_note h_c1 = this |
|
3930 my_block |
|
3931 fix j' j m |
|
3932 have "\<lbrace>st j' \<and>* ps (v + 1) \<and>* reps' (v + 1) w [ks ! Suc n]\<rbrace> |
|
3933 j' :[ skip_or_set ]: j |
|
3934 \<lbrace>st j \<and>* ps (w + 1) \<and>* reps' (v + 1) w [ks ! Suc n]\<rbrace>" |
|
3935 apply (unfold reps'_def, simp) |
|
3936 apply (rule hoare_skip_or_set_skip[THEN tm.hoare_adjust]) |
|
3937 by (sep_cancel+, smt)+ |
|
3938 my_block_end |
|
3939 apply (hstep h_c1 this)+ |
|
3940 by ((fwd abs_reps'), simp, sep_cancel+) |
|
3941 qed |
|
3942 |
|
3943 lemma hoare_skip_or_sets_skip_gen[step]: |
|
3944 assumes h: "n < length ks" "u = v" "x = w + 1" |
|
3945 shows "\<lbrace>st i \<and>* ps u \<and>* reps' v w (take n ks) \<and>* reps' x y [ks!n] \<rbrace> |
|
3946 i:[skip_or_sets (Suc n)]:j |
|
3947 \<lbrace>st j \<and>* ps (y+1) \<and>* reps' v w (take n ks) \<and>* reps' x y [ks!n]\<rbrace>" |
|
3948 by (unfold assms, rule hoare_skip_or_sets_skip[OF `n < length ks`]) |
|
3949 |
|
3950 lemma fam_conj_interv_simp: |
|
3951 "(fam_conj {(ia::int)<..} p) = ((p (ia + 1)) \<and>* fam_conj {ia + 1 <..} p)" |
|
3952 by (smt Collect_cong fam_conj_insert_simp greaterThan_def |
|
3953 greaterThan_eq_iff greaterThan_iff insertI1 |
|
3954 insert_compr lessThan_iff mem_Collect_eq) |
|
3955 |
|
3956 lemma zeros_fam_conj: |
|
3957 assumes "u \<le> v" |
|
3958 shows "(zeros u v \<and>* fam_conj {v<..} zero) = fam_conj {u - 1<..} zero" |
|
3959 proof - |
|
3960 have "{u - 1<..v} ## {v <..}" by (auto simp:set_ins_def) |
|
3961 from fam_conj_disj_simp[OF this, symmetric] |
|
3962 have "(fam_conj {u - 1<..v} zero \<and>* fam_conj {v<..} zero) = fam_conj ({u - 1<..v} + {v<..}) zero" . |
|
3963 moreover |
|
3964 from `u \<le> v` have eq_set: "{u - 1 <..} = {u - 1 <..v} + {v <..}" by (auto simp:set_ins_def) |
|
3965 moreover have "fam_conj {u - 1<..v} zero = zeros u v" |
|
3966 proof - |
|
3967 have "({u - 1<..v}) = ({u .. v})" by auto |
|
3968 moreover { |
|
3969 fix u v |
|
3970 assume "u \<le> (v::int)" |
|
3971 hence "fam_conj {u .. v} zero = zeros u v" |
|
3972 proof(induct rule:ones_induct) |
|
3973 case (Base i j) |
|
3974 thus ?case by auto |
|
3975 next |
|
3976 case (Step i j) |
|
3977 thus ?case |
|
3978 proof(cases "i = j") |
|
3979 case True |
|
3980 show ?thesis |
|
3981 by (unfold True, simp add:fam_conj_simps) |
|
3982 next |
|
3983 case False |
|
3984 with `i \<le> j` have hh: "i + 1 \<le> j" by auto |
|
3985 hence eq_set: "{i..j} = (insert i {i + 1 .. j})" |
|
3986 by (smt simp_from_to) |
|
3987 have "i \<notin> {i + 1 .. j}" by simp |
|
3988 from fam_conj_insert_simp[OF this, folded eq_set] |
|
3989 have "fam_conj {i..j} zero = (zero i \<and>* fam_conj {i + 1..j} zero)" . |
|
3990 with Step(2)[OF hh] Step |
|
3991 show ?thesis by simp |
|
3992 qed |
|
3993 qed |
|
3994 } |
|
3995 moreover note this[OF `u \<le> v`] |
|
3996 ultimately show ?thesis by simp |
|
3997 qed |
|
3998 ultimately show ?thesis by smt |
|
3999 qed |
|
4000 |
|
4001 declare replicate.simps [simp del] |
|
4002 |
|
4003 lemma hoare_skip_or_sets_comb: |
|
4004 assumes "length ks \<le> n" |
|
4005 shows "\<lbrace>st i \<and>* ps u \<and>* reps u v ks \<and>* fam_conj {v<..} zero\<rbrace> |
|
4006 i:[skip_or_sets (Suc n)]:j |
|
4007 \<lbrace>st j \<and>* ps ((v + int (reps_len (list_ext n ks)) - int (reps_len ks))+ 2) \<and>* |
|
4008 reps' u (v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1) (list_ext n ks) \<and>* |
|
4009 fam_conj {(v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1)<..} zero \<rbrace>" |
|
4010 proof(cases "ks = []") |
|
4011 case True |
|
4012 show ?thesis |
|
4013 apply (subst True, simp only:reps.simps sep_conj_cond) |
|
4014 apply (rule tm.pre_condI, simp) |
|
4015 apply (rule_tac p = "st i \<and>* ps (v + 1) \<and>* |
|
4016 zeros (v + 1) (v + 1 + int (reps_len (replicate (Suc n) 0))) \<and>* |
|
4017 tm (v + 2 + int (reps_len (replicate (Suc n) 0))) Bk \<and>* |
|
4018 fam_conj {(v + 2 + int (reps_len (replicate (Suc n) 0)))<..} zero |
|
4019 " in tm.pre_stren) |
|
4020 apply hsteps |
|
4021 apply (auto simp:sep_conj_ac)[1] |
|
4022 apply (auto simp:sep_conj_ac)[2] |
|
4023 my_block |
|
4024 from True have "(list_ext n ks) = (replicate (Suc n) 0)" |
|
4025 by (metis append_Nil diff_zero list.size(3) list_ext_def) |
|
4026 my_block_end my_note le_red = this |
|
4027 my_block |
|
4028 from True have "(reps_len ks) = 0" |
|
4029 by (metis reps_len_nil) |
|
4030 my_block_end |
|
4031 apply (unfold this le_red, simp) |
|
4032 my_block |
|
4033 have "v + 2 + int (reps_len (replicate (Suc n) 0)) = |
|
4034 v + int (reps_len (replicate (Suc n) 0)) + 2" by smt |
|
4035 my_block_end my_note eq_len = this |
|
4036 apply (unfold this) |
|
4037 apply (sep_cancel+) |
|
4038 apply (fold zero_def) |
|
4039 apply (subst fam_conj_interv_simp, simp) |
|
4040 apply (simp only:int_add_ac) |
|
4041 apply (simp only:sep_conj_ac, sep_cancel+) |
|
4042 my_block |
|
4043 have "v + 1 \<le> (2 + (v + int (reps_len (replicate (Suc n) 0))))" by simp |
|
4044 from zeros_fam_conj[OF this] |
|
4045 have "(fam_conj {v<..} zero) = (zeros (v + 1) (2 + (v + int (reps_len (replicate (Suc n) 0)))) \<and>* |
|
4046 fam_conj {2 + (v + int (reps_len (replicate (Suc n) 0)))<..} zero)" |
|
4047 by simp |
|
4048 my_block_end |
|
4049 apply (subst (asm) this, simp only:int_add_ac, sep_cancel+) |
|
4050 by (smt cond_true_eq2 sep.mult_assoc sep.mult_commute |
|
4051 sep.mult_left_commute sep_conj_assoc sep_conj_commute |
|
4052 sep_conj_left_commute zeros.simps zeros_rev) |
|
4053 next |
|
4054 case False |
|
4055 show ?thesis |
|
4056 my_block |
|
4057 have "(i:[skip_or_sets (Suc n)]:j) = |
|
4058 (i:[(skip_or_sets (length ks); skip_or_sets (Suc n - length ks))]:j)" |
|
4059 apply (unfold skip_or_sets_def) |
|
4060 my_block |
|
4061 have "(replicate (Suc n) skip_or_set) = |
|
4062 (replicate (length ks) skip_or_set @ (replicate (Suc n - length ks) skip_or_set))" |
|
4063 by (smt assms replicate_add) |
|
4064 my_block_end |
|
4065 apply (unfold this, rule tpg_fold_app, simp add:False) |
|
4066 by (insert `length ks \<le> n`, simp) |
|
4067 my_block_end |
|
4068 apply (unfold this) |
|
4069 my_block |
|
4070 from False have "length ks = (Suc (length ks - 1))" by simp |
|
4071 my_block_end |
|
4072 apply (subst (1) this) |
|
4073 my_block |
|
4074 from False |
|
4075 have "(reps u v ks \<and>* fam_conj {v<..} zero) = |
|
4076 (reps' u (v + 1) ks \<and>* fam_conj {v+1<..} zero)" |
|
4077 apply (unfold reps'_def, simp) |
|
4078 by (subst fam_conj_interv_simp, simp add:sep_conj_ac) |
|
4079 my_block_end |
|
4080 apply (unfold this) |
|
4081 my_block |
|
4082 fix i j |
|
4083 have "\<lbrace>st i \<and>* ps u \<and>* reps' u (v + 1) ks \<rbrace> |
|
4084 i :[ skip_or_sets (Suc (length ks - 1))]: j |
|
4085 \<lbrace>st j \<and>* ps (v + 2) \<and>* reps' u (v + 1) ks \<rbrace>" |
|
4086 my_block |
|
4087 have "ks = take (length ks - 1) ks @ [ks!(length ks - 1)]" |
|
4088 by (smt False drop_0 drop_eq_Nil id_take_nth_drop) |
|
4089 my_block_end my_note eq_ks = this |
|
4090 apply (subst (1) this) |
|
4091 apply (unfold reps'_append, simp add:sep_conj_exists, rule tm.precond_exI) |
|
4092 my_block |
|
4093 from False have "(length ks - Suc 0) < length ks" |
|
4094 by (smt `length ks = Suc (length ks - 1)`) |
|
4095 my_block_end |
|
4096 apply hsteps |
|
4097 apply (subst eq_ks, unfold reps'_append, simp only:sep_conj_exists) |
|
4098 by (rule_tac x = m in EXS_intro, simp add:sep_conj_ac, sep_cancel+, smt) |
|
4099 my_block_end |
|
4100 apply (hstep this) |
|
4101 my_block |
|
4102 fix u n |
|
4103 have "(fam_conj {u <..} zero) = |
|
4104 (zeros (u + 1) (u + int n + 1) \<and>* tm (u + int n + 2) Bk \<and>* fam_conj {(u + int n + 2)<..} zero)" |
|
4105 my_block |
|
4106 have "u + 1 \<le> (u + int n + 2)" by auto |
|
4107 from zeros_fam_conj[OF this, symmetric] |
|
4108 have "fam_conj {u<..} zero = (zeros (u + 1) (u + int n + 2) \<and>* fam_conj {u + int n + 2<..} zero)" |
|
4109 by simp |
|
4110 my_block_end |
|
4111 apply (subst this) |
|
4112 my_block |
|
4113 have "(zeros (u + 1) (u + int n + 2)) = |
|
4114 ((zeros (u + 1) (u + int n + 1) \<and>* tm (u + int n + 2) Bk))" |
|
4115 by (smt zero_def zeros_rev) |
|
4116 my_block_end |
|
4117 by (unfold this, auto simp:sep_conj_ac) |
|
4118 my_block_end |
|
4119 apply (subst (1) this[of _ "(reps_len (replicate (Suc (n - length ks)) 0))"]) |
|
4120 my_block |
|
4121 from `length ks \<le> n` |
|
4122 have "Suc n - length ks = Suc (n - length ks)" by auto |
|
4123 my_block_end my_note eq_suc = this |
|
4124 apply (subst this) |
|
4125 apply hsteps |
|
4126 apply (simp add: sep_conj_ac this, sep_cancel+) |
|
4127 apply (fwd abs_reps')+ |
|
4128 my_block |
|
4129 have "(int (reps_len (replicate (Suc (n - length ks)) 0))) = |
|
4130 (int (reps_len (list_ext n ks)) - int (reps_len ks) - 1)" |
|
4131 apply (unfold list_ext_def eq_suc) |
|
4132 my_block |
|
4133 have "replicate (Suc (n - length ks)) 0 \<noteq> []" by simp |
|
4134 my_block_end |
|
4135 by (unfold reps_len_split[OF False this], simp) |
|
4136 my_block_end |
|
4137 apply (unfold this) |
|
4138 my_block |
|
4139 from `length ks \<le> n` |
|
4140 have "(ks @ replicate (Suc (n - length ks)) 0) = (list_ext n ks)" |
|
4141 by (unfold list_ext_def, simp) |
|
4142 my_block_end |
|
4143 apply (unfold this, simp) |
|
4144 apply (subst fam_conj_interv_simp, unfold zero_def, simp, simp add:int_add_ac sep_conj_ac) |
|
4145 by (sep_cancel+, smt) |
|
4146 qed |
|
4147 |
|
4148 lemma hoare_skip_or_sets_comb_gen: |
|
4149 assumes "length ks \<le> n" "u = v" "w = x" |
|
4150 shows "\<lbrace>st i \<and>* ps u \<and>* reps v w ks \<and>* fam_conj {x<..} zero\<rbrace> |
|
4151 i:[skip_or_sets (Suc n)]:j |
|
4152 \<lbrace>st j \<and>* ps ((x + int (reps_len (list_ext n ks)) - int (reps_len ks))+ 2) \<and>* |
|
4153 reps' u (x + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1) (list_ext n ks) \<and>* |
|
4154 fam_conj {(x + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1)<..} zero \<rbrace>" |
|
4155 by (unfold assms, rule hoare_skip_or_sets_comb[OF `length ks \<le> n`]) |
|
4156 |
|
4157 definition "locate n = (skip_or_sets (Suc n); |
|
4158 move_left; |
|
4159 move_left; |
|
4160 left_until_zero; |
|
4161 move_right |
|
4162 )" |
|
4163 |
|
4164 lemma list_ext_tail_expand: |
|
4165 assumes h: "length ks \<le> a" |
|
4166 shows "list_ext a ks = take a (list_ext a ks) @ [(list_ext a ks)!a]" |
|
4167 proof - |
|
4168 let ?l = "list_ext a ks" |
|
4169 from h have eq_len: "length ?l = Suc a" |
|
4170 by (smt list_ext_len_eq) |
|
4171 hence "?l \<noteq> []" by auto |
|
4172 hence "?l = take (length ?l - 1) ?l @ [?l!(length ?l - 1)]" |
|
4173 by (metis `length (list_ext a ks) = Suc a` diff_Suc_1 le_refl |
|
4174 lessI take_Suc_conv_app_nth take_all) |
|
4175 from this[unfolded eq_len] |
|
4176 show ?thesis by simp |
|
4177 qed |
|
4178 |
|
4179 lemma reps'_nn_expand: |
|
4180 assumes "xs \<noteq> []" |
|
4181 shows "(reps' u v xs) = (reps u (v - 1) xs \<and>* zero v)" |
|
4182 by (metis assms reps'_def) |
|
4183 |
|
4184 lemma sep_conj_st1: "(p \<and>* st t \<and>* q) = (st t \<and>* p \<and>* q)" |
|
4185 by (simp only:sep_conj_ac) |
|
4186 |
|
4187 lemma sep_conj_st2: "(p \<and>* st t) = (st t \<and>* p)" |
|
4188 by (simp only:sep_conj_ac) |
|
4189 |
|
4190 lemma sep_conj_st3: "((st t \<and>* p) \<and>* r) = (st t \<and>* p \<and>* r)" |
|
4191 by (simp only:sep_conj_ac) |
|
4192 |
|
4193 lemma sep_conj_st4: "(EXS x. (st t \<and>* r x)) = ((st t) \<and>* (EXS x. r x))" |
|
4194 apply (unfold pred_ex_def, default+) |
|
4195 apply (safe) |
|
4196 apply (sep_cancel, auto) |
|
4197 by (auto elim!: sep_conjE intro!:sep_conjI) |
|
4198 |
|
4199 lemmas sep_conj_st = sep_conj_st1 sep_conj_st2 sep_conj_st3 sep_conj_st4 |
|
4200 |
|
4201 lemma sep_conj_cond3 : "(<cond1> \<and>* <cond2>) = <(cond1 \<and> cond2)>" |
|
4202 by (smt cond_eqI cond_true_eq sep_conj_commute sep_conj_empty) |
|
4203 |
|
4204 lemma sep_conj_cond4 : "(<cond1> \<and>* <cond2> \<and>* r) = (<(cond1 \<and> cond2)> \<and>* r)" |
|
4205 by (metis Hoare_gen.sep_conj_cond3 Hoare_tm.sep_conj_cond3) |
|
4206 |
|
4207 lemmas sep_conj_cond = sep_conj_cond3 sep_conj_cond4 sep_conj_cond |
|
4208 |
|
4209 lemma hoare_left_until_zero_reps: |
|
4210 "\<lbrace>st i ** ps v ** zero u ** reps (u + 1) v [k]\<rbrace> |
|
4211 i:[left_until_zero]:j |
|
4212 \<lbrace>st j ** ps u ** zero u ** reps (u + 1) v [k]\<rbrace>" |
|
4213 apply (unfold reps.simps, simp only:sep_conj_cond) |
|
4214 apply (rule tm.pre_condI, simp) |
|
4215 by hstep |
|
4216 |
|
4217 lemma hoare_left_until_zero_reps_gen[step]: |
|
4218 assumes "u = x" "w = v + 1" |
|
4219 shows "\<lbrace>st i ** ps u ** zero v ** reps w x [k]\<rbrace> |
|
4220 i:[left_until_zero]:j |
|
4221 \<lbrace>st j ** ps v ** zero v ** reps w x [k]\<rbrace>" |
|
4222 by (unfold assms, rule hoare_left_until_zero_reps) |
|
4223 |
|
4224 lemma reps_lenE: |
|
4225 assumes "reps u v ks s" |
|
4226 shows "( <(v = u + int (reps_len ks) - 1)> \<and>* reps u v ks ) s" |
|
4227 proof(rule condI) |
|
4228 from reps_len_correct[OF assms] show "v = u + int (reps_len ks) - 1" . |
|
4229 next |
|
4230 from assms show "reps u v ks s" . |
|
4231 qed |
|
4232 |
|
4233 lemma condI1: |
|
4234 assumes "p s" "b" |
|
4235 shows "(<b> \<and>* p) s" |
|
4236 proof(rule condI[OF assms(2)]) |
|
4237 from assms(1) show "p s" . |
|
4238 qed |
|
4239 |
|
4240 lemma hoare_locate_set: |
|
4241 assumes "length ks \<le> n" |
|
4242 shows "\<lbrace>st i \<and>* zero (u - 1) \<and>* ps u \<and>* reps u v ks \<and>* fam_conj {v<..} zero\<rbrace> |
|
4243 i:[locate n]:j |
|
4244 \<lbrace>st j \<and>* zero (u - 1) \<and>* |
|
4245 (EXS m w. ps m \<and>* reps' u (m - 1) (take n (list_ext n ks)) \<and>* |
|
4246 reps m w [(list_ext n ks)!n] \<and>* fam_conj {w<..} zero)\<rbrace>" |
|
4247 proof(cases "take n (list_ext n ks) = []") |
|
4248 case False |
|
4249 show ?thesis |
|
4250 apply (unfold locate_def) |
|
4251 apply (hstep hoare_skip_or_sets_comb_gen) |
|
4252 apply (subst (3) list_ext_tail_expand[OF `length ks \<le> n`]) |
|
4253 apply (subst (1) reps'_append, simp add:sep_conj_exists) |
|
4254 apply (rule tm.precond_exI) |
|
4255 apply (subst (1) reps'_nn_expand[OF False]) |
|
4256 apply (rule_tac p = "st j' \<and>* <(m = u + int (reps_len (take n (list_ext n ks))) + 1)> \<and>* |
|
4257 ps (v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 2) \<and>* |
|
4258 ((reps u (m - 1 - 1) (take n (list_ext n ks)) \<and>* zero (m - 1)) \<and>* |
|
4259 reps' m (v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1) |
|
4260 [list_ext n ks ! n]) \<and>* |
|
4261 fam_conj |
|
4262 {v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1<..} |
|
4263 zero \<and>* |
|
4264 zero (u - 1)" |
|
4265 in tm.pre_stren) |
|
4266 my_block |
|
4267 have "[list_ext n ks ! n] \<noteq> []" by simp |
|
4268 from reps'_nn_expand[OF this] |
|
4269 have "(reps' m (v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1) [list_ext n ks ! n]) = |
|
4270 (reps m (v + (int (reps_len (list_ext n ks)) - int (reps_len ks))) [list_ext n ks ! n] \<and>* |
|
4271 zero (v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1))" |
|
4272 by simp |
|
4273 my_block_end |
|
4274 apply (subst this) |
|
4275 my_block |
|
4276 have "(fam_conj {v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 1<..} zero) = |
|
4277 (zero (v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 2) \<and>* |
|
4278 fam_conj {v + int (reps_len (list_ext n ks)) - int (reps_len ks) + 2<..} zero)" |
|
4279 by (subst fam_conj_interv_simp, smt) |
|
4280 my_block_end |
|
4281 apply (unfold this) |
|
4282 apply (simp only:sep_conj_st) |
|
4283 apply hsteps |
|
4284 apply (auto simp:sep_conj_ac)[1] |
|
4285 apply (sep_cancel+) |
|
4286 apply (rule_tac x = m in EXS_intro) |
|
4287 apply (rule_tac x = "m + int (list_ext n ks ! n)" in EXS_intro) |
|
4288 apply (simp add:sep_conj_ac del:ones_simps, sep_cancel+) |
|
4289 apply (subst (asm) sep_conj_cond)+ |
|
4290 apply (erule_tac condE, clarsimp, simp add:sep_conj_ac int_add_ac) |
|
4291 apply (fwd abs_reps') |
|
4292 apply (fwd abs_reps') |
|
4293 apply (simp add:sep_conj_ac int_add_ac) |
|
4294 apply (sep_cancel+) |
|
4295 apply (subst (asm) reps'_def, subst fam_conj_interv_simp, subst fam_conj_interv_simp, |
|
4296 simp add:sep_conj_ac int_add_ac) |
|
4297 my_block |
|
4298 fix s |
|
4299 assume h: "(reps (1 + (u + int (reps_len (take n (list_ext n ks))))) |
|
4300 (v + (- int (reps_len ks) + int (reps_len (list_ext n ks)))) [list_ext n ks ! n]) s" |
|
4301 (is "?P s") |
|
4302 from reps_len_correct[OF this] list_ext_tail_expand[OF `length ks \<le> n`] |
|
4303 have hh: "v + (- int (reps_len ks) + |
|
4304 int (reps_len (take n (list_ext n ks) @ [list_ext n ks ! n]))) = |
|
4305 1 + (u + int (reps_len (take n (list_ext n ks)))) + |
|
4306 int (reps_len [list_ext n ks ! n]) - 1" |
|
4307 by metis |
|
4308 have "[list_ext n ks ! n] \<noteq> []" by simp |
|
4309 from hh[unfolded reps_len_split[OF False this]] |
|
4310 have "v = u + (int (reps_len ks)) - 1" |
|
4311 by simp |
|
4312 from condI1[where p = ?P, OF h this] |
|
4313 have "(<(v = u + int (reps_len ks) - 1)> \<and>* |
|
4314 reps (1 + (u + int (reps_len (take n (list_ext n ks))))) |
|
4315 (v + (- int (reps_len ks) + int (reps_len (list_ext n ks)))) [list_ext n ks ! n]) s" . |
|
4316 my_block_end |
|
4317 apply (fwd this, (subst (asm) sep_conj_cond)+, erule condE, simp add:sep_conj_ac int_add_ac |
|
4318 reps_len_sg) |
|
4319 apply (fwd reps_lenE, (subst (asm) sep_conj_cond)+, erule condE, simp add:sep_conj_ac int_add_ac |
|
4320 reps_len_sg) |
|
4321 by (fwd reps_lenE, (subst (asm) sep_conj_cond)+, erule condE, simp add:sep_conj_ac) |
|
4322 next |
|
4323 case True |
|
4324 show ?thesis |
|
4325 apply (unfold locate_def) |
|
4326 apply (hstep hoare_skip_or_sets_comb) |
|
4327 apply (subst (3) list_ext_tail_expand[OF `length ks \<le> n`]) |
|
4328 apply (subst (1) reps'_append, simp add:sep_conj_exists) |
|
4329 apply (rule tm.precond_exI) |
|
4330 my_block |
|
4331 from True `length ks \<le> n` |
|
4332 have "ks = []" "n = 0" |
|
4333 apply (metis le0 le_antisym length_0_conv less_nat_zero_code list_ext_len take_eq_Nil) |
|
4334 by (smt True length_take list.size(3) list_ext_len) |
|
4335 my_block_end |
|
4336 apply (unfold True this) |
|
4337 apply (simp add:reps'_def list_ext_def reps.simps reps_len_def reps_sep_len_def |
|
4338 reps_ctnt_len_def |
|
4339 del:ones_simps) |
|
4340 apply (subst sep_conj_cond)+ |
|
4341 apply (rule tm.pre_condI, simp del:ones_simps) |
|
4342 apply (subst fam_conj_interv_simp, simp add:sep_conj_st del:ones_simps) |
|
4343 apply (hsteps) |
|
4344 apply (auto simp:sep_conj_ac)[1] |
|
4345 apply (sep_cancel+) |
|
4346 apply (rule_tac x = "(v + int (listsum (replicate (Suc 0) (Suc 0))))" in EXS_intro)+ |
|
4347 apply (simp only:sep_conj_ac, sep_cancel+) |
|
4348 apply (auto) |
|
4349 apply (subst fam_conj_interv_simp) |
|
4350 apply (subst fam_conj_interv_simp) |
|
4351 by smt |
|
4352 qed |
|
4353 |
|
4354 lemma hoare_locate_set_gen[step]: |
|
4355 assumes "length ks \<le> n" |
|
4356 "u = v - 1" "v = w" "x = y" |
|
4357 shows "\<lbrace>st i \<and>* zero u \<and>* ps v \<and>* reps w x ks \<and>* fam_conj {y<..} zero\<rbrace> |
|
4358 i:[locate n]:j |
|
4359 \<lbrace>st j \<and>* zero u \<and>* |
|
4360 (EXS m w. ps m \<and>* reps' v (m - 1) (take n (list_ext n ks)) \<and>* |
|
4361 reps m w [(list_ext n ks)!n] \<and>* fam_conj {w<..} zero)\<rbrace>" |
|
4362 by (unfold assms, rule hoare_locate_set[OF `length ks \<le> n`]) |
|
4363 |
|
4364 lemma hoare_locate_skip: |
|
4365 assumes h: "n < length ks" |
|
4366 shows |
|
4367 "\<lbrace>st i \<and>* ps u \<and>* zero (u - 1) \<and>* reps' u (v - 1) (take n ks) \<and>* reps' v w [ks!n] \<and>* tm (w + 1) x\<rbrace> |
|
4368 i:[locate n]:j |
|
4369 \<lbrace>st j \<and>* ps v \<and>* zero (u - 1) \<and>* reps' u (v - 1) (take n ks) \<and>* reps' v w [ks!n] \<and>* tm (w + 1) x\<rbrace>" |
|
4370 proof - |
|
4371 show ?thesis |
|
4372 apply (unfold locate_def) |
|
4373 apply hsteps |
|
4374 apply (subst (2 4) reps'_def, simp add:reps.simps sep_conj_cond del:ones_simps) |
|
4375 apply (intro tm.pre_condI, simp del:ones_simps) |
|
4376 apply hsteps |
|
4377 apply (case_tac "(take n ks) = []", simp add:reps'_def sep_conj_cond del:ones_simps) |
|
4378 apply (rule tm.pre_condI, simp del:ones_simps) |
|
4379 apply hsteps |
|
4380 apply (simp del:ones_simps add:reps'_def) |
|
4381 by hsteps |
|
4382 qed |
|
4383 |
|
4384 |
|
4385 lemma hoare_locate_skip_gen[step]: |
|
4386 assumes "n < length ks" |
|
4387 "v = u - 1" "w = u" "x = y - 1" "z' = z + 1" |
|
4388 shows |
|
4389 "\<lbrace>st i \<and>* ps u \<and>* tm v Bk \<and>* reps' w x (take n ks) \<and>* reps' y z [ks!n] \<and>* tm z' vx\<rbrace> |
|
4390 i:[locate n]:j |
|
4391 \<lbrace>st j \<and>* ps y \<and>* tm v Bk \<and>* reps' w x (take n ks) \<and>* reps' y z [ks!n] \<and>* tm z' vx\<rbrace>" |
|
4392 by (unfold assms, fold zero_def, rule hoare_locate_skip[OF `n < length ks`]) |
|
4393 |
|
4394 definition "Inc a = locate a; |
|
4395 right_until_zero; |
|
4396 move_right; |
|
4397 shift_right; |
|
4398 move_left; |
|
4399 left_until_double_zero; |
|
4400 write_one; |
|
4401 left_until_double_zero; |
|
4402 move_right; |
|
4403 move_right |
|
4404 " |
|
4405 |
|
4406 lemma ones_int_expand: "(ones m (m + int k)) = (one m \<and>* ones (m + 1) (m + int k))" |
|
4407 by (simp add:ones_simps) |
|
4408 |
|
4409 lemma reps_splitedI: |
|
4410 assumes h1: "(reps u v ks1 \<and>* zero (v + 1) \<and>* reps (v + 2) w ks2) s" |
|
4411 and h2: "ks1 \<noteq> []" |
|
4412 and h3: "ks2 \<noteq> []" |
|
4413 shows "(reps u w (ks1 @ ks2)) s" |
|
4414 proof - |
|
4415 from h2 h3 |
|
4416 have "splited (ks1 @ ks2) ks1 ks2" by (auto simp:splited_def) |
|
4417 from h1 obtain s1 where |
|
4418 "(reps u v ks1) s1" by (auto elim:sep_conjE) |
|
4419 from h1 obtain s2 where |
|
4420 "(reps (v + 2) w ks2) s2" by (auto elim:sep_conjE) |
|
4421 from reps_len_correct[OF `(reps u v ks1) s1`] |
|
4422 have eq_v: "v = u + int (reps_len ks1) - 1" . |
|
4423 from reps_len_correct[OF `(reps (v + 2) w ks2) s2`] |
|
4424 have eq_w: "w = v + 2 + int (reps_len ks2) - 1" . |
|
4425 from h1 |
|
4426 have "(reps u (u + int (reps_len ks1) - 1) ks1 \<and>* |
|
4427 zero (u + int (reps_len ks1)) \<and>* reps (u + int (reps_len ks1) + 1) w ks2) s" |
|
4428 apply (unfold eq_v eq_w[unfolded eq_v]) |
|
4429 by (sep_cancel+, smt) |
|
4430 thus ?thesis |
|
4431 by(unfold reps_splited[OF `splited (ks1 @ ks2) ks1 ks2`], simp) |
|
4432 qed |
|
4433 |
|
4434 lemma reps_sucI: |
|
4435 assumes h: "(reps u v (xs@[x]) \<and>* one (1 + v)) s" |
|
4436 shows "(reps u (1 + v) (xs@[Suc x])) s" |
|
4437 proof(cases "xs = []") |
|
4438 case True |
|
4439 from h obtain s' where "(reps u v (xs@[x])) s'" by (auto elim:sep_conjE) |
|
4440 from reps_len_correct[OF this] have " v = u + int (reps_len (xs @ [x])) - 1" . |
|
4441 with True have eq_v: "v = u + int x" by (simp add:reps_len_sg) |
|
4442 have eq_one1: "(one (1 + (u + int x)) \<and>* ones (u + 1) (u + int x)) = ones (u + 1) (1 + (u + int x))" |
|
4443 by (smt ones_rev sep.mult_commute) |
|
4444 from h show ?thesis |
|
4445 apply (unfold True, simp add:eq_v reps.simps sep_conj_cond sep_conj_ac ones_rev) |
|
4446 by (sep_cancel+, simp add: eq_one1, smt) |
|
4447 next |
|
4448 case False |
|
4449 from h obtain s1 s2 where hh: "(reps u v (xs@[x])) s1" "s = s1 + s2" "s1 ## s2" "one (1 + v) s2" |
|
4450 by (auto elim:sep_conjE) |
|
4451 from hh(1)[unfolded reps_rev[OF False]] |
|
4452 obtain s11 s12 s13 where hhh: |
|
4453 "(reps u (v - int (x + 1) - 1) xs) s11" |
|
4454 "(zero (v - int (x + 1))) s12" "(ones (v - int x) v) s13" |
|
4455 "s11 ## (s12 + s13)" "s12 ## s13" "s1 = s11 + s12 + s13" |
|
4456 apply (atomize_elim) |
|
4457 apply(elim sep_conjE)+ |
|
4458 apply (rule_tac x = xa in exI) |
|
4459 apply (rule_tac x = xaa in exI) |
|
4460 apply (rule_tac x = ya in exI) |
|
4461 apply (intro conjI, assumption+) |
|
4462 by (auto simp:set_ins_def) |
|
4463 show ?thesis |
|
4464 proof(rule reps_splitedI[where v = "(v - int (x + 1) - 1)"]) |
|
4465 show "(reps u (v - int (x + 1) - 1) xs \<and>* zero (v - int (x + 1) - 1 + 1) \<and>* |
|
4466 reps (v - int (x + 1) - 1 + 2) (1 + v) [Suc x]) s" |
|
4467 proof(rule sep_conjI) |
|
4468 from hhh(1) show "reps u (v - int (x + 1) - 1) xs s11" . |
|
4469 next |
|
4470 show "(zero (v - int (x + 1) - 1 + 1) \<and>* reps (v - int (x + 1) - 1 + 2) (1 + v) [Suc x]) (s12 + (s13 + s2))" |
|
4471 proof(rule sep_conjI) |
|
4472 from hhh(2) show "zero (v - int (x + 1) - 1 + 1) s12" by smt |
|
4473 next |
|
4474 from hh(4) hhh(3) |
|
4475 show "reps (v - int (x + 1) - 1 + 2) (1 + v) [Suc x] (s13 + s2)" |
|
4476 apply (simp add:reps.simps del:ones_simps add:ones_rev) |
|
4477 by (smt hh(3) hh(4) hhh(4) hhh(5) hhh(6) sep_add_disjD sep_conjI sep_disj_addI1) |
|
4478 next |
|
4479 show "s12 ## s13 + s2" |
|
4480 by (metis hh(3) hhh(4) hhh(5) hhh(6) sep_add_commute sep_add_disjD |
|
4481 sep_add_disjI2 sep_disj_addD sep_disj_addD1 sep_disj_addI1 sep_disj_commuteI) |
|
4482 next |
|
4483 show "s12 + (s13 + s2) = s12 + (s13 + s2)" by metis |
|
4484 qed |
|
4485 next |
|
4486 show "s11 ## s12 + (s13 + s2)" |
|
4487 by (metis hh(3) hhh(4) hhh(5) hhh(6) sep_disj_addD1 sep_disj_addI1 sep_disj_addI3) |
|
4488 next |
|
4489 show "s = s11 + (s12 + (s13 + s2))" |
|
4490 by (smt hh(2) hh(3) hhh(4) hhh(5) hhh(6) sep_add_assoc sep_add_commute |
|
4491 sep_add_disjD sep_add_disjI2 sep_disj_addD1 sep_disj_addD2 |
|
4492 sep_disj_addI1 sep_disj_addI3 sep_disj_commuteI) |
|
4493 qed |
|
4494 next |
|
4495 from False show "xs \<noteq> []" . |
|
4496 next |
|
4497 show "[Suc x] \<noteq> []" by simp |
|
4498 qed |
|
4499 qed |
|
4500 |
|
4501 lemma cond_expand: "(<cond> \<and>* p) s = (cond \<and> (p s))" |
|
4502 by (metis (full_types) condD pasrt_def sep_conj_commuteI |
|
4503 sep_conj_sep_emptyI sep_empty_def sep_globalise) |
|
4504 |
|
4505 lemma ones_rev1: |
|
4506 assumes "\<not> (1 + n) < m" |
|
4507 shows "(ones m n \<and>* one (1 + n)) = (ones m (1 + n))" |
|
4508 by (insert ones_rev[OF assms, simplified], simp) |
|
4509 |
|
4510 lemma reps_one_abs: |
|
4511 assumes "(reps u v [k] \<and>* one w) s" |
|
4512 "w = v + 1" |
|
4513 shows "(reps u w [Suc k]) s" |
|
4514 using assms unfolding assms |
|
4515 apply (simp add:reps.simps sep_conj_ac) |
|
4516 apply (subst (asm) sep_conj_cond)+ |
|
4517 apply (erule condE, simp) |
|
4518 by (simp add:ones_rev sep_conj_ac, sep_cancel+, smt) |
|
4519 |
|
4520 lemma reps'_reps_abs: |
|
4521 assumes "(reps' u v xs \<and>* reps w x ys) s" |
|
4522 "w = v + 1" "ys \<noteq> []" |
|
4523 shows "(reps u x (xs@ys)) s" |
|
4524 proof(cases "xs = []") |
|
4525 case False |
|
4526 with assms |
|
4527 have h: "splited (xs@ys) xs ys" by (simp add:splited_def) |
|
4528 from assms(1)[unfolded assms(2)] |
|
4529 show ?thesis |
|
4530 apply (unfold reps_splited[OF h]) |
|
4531 apply (insert False, unfold reps'_def, simp) |
|
4532 apply (fwd reps_lenE, (subst (asm) sep_conj_cond)+) |
|
4533 by (erule condE, simp) |
|
4534 next |
|
4535 case True |
|
4536 with assms |
|
4537 show ?thesis |
|
4538 apply (simp add:reps'_def) |
|
4539 by (erule condE, simp) |
|
4540 qed |
|
4541 |
|
4542 lemma reps_one_abs1: |
|
4543 assumes "(reps u v (xs@[k]) \<and>* one w) s" |
|
4544 "w = v + 1" |
|
4545 shows "(reps u w (xs@[Suc k])) s" |
|
4546 proof(cases "xs = []") |
|
4547 case True |
|
4548 with assms reps_one_abs |
|
4549 show ?thesis by simp |
|
4550 next |
|
4551 case False |
|
4552 hence "splited (xs@[k]) xs [k]" by (simp add:splited_def) |
|
4553 from assms(1)[unfolded reps_splited[OF this] assms(2)] |
|
4554 show ?thesis |
|
4555 apply (fwd reps_one_abs) |
|
4556 apply (fwd reps_reps'_abs) |
|
4557 apply (fwd reps'_reps_abs) |
|
4558 by (simp add:assms) |
|
4559 qed |
|
4560 |
|
4561 lemma tm_hoare_inc00: |
|
4562 assumes h: "a < length ks \<and> ks ! a = v" |
|
4563 shows "\<lbrace>st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u ia ks \<and>* fam_conj {ia<..} zero\<rbrace> |
|
4564 i :[ Inc a ]: j |
|
4565 \<lbrace>st j \<and>* |
|
4566 ps u \<and>* |
|
4567 zero (u - 2) \<and>* |
|
4568 zero (u - 1) \<and>* |
|
4569 reps u (ia + int (reps_len (list_ext a ks)) - int (reps_len ks) + 1) (ks[a := Suc v]) \<and>* |
|
4570 fam_conj {(ia + int (reps_len (list_ext a ks)) - int (reps_len ks) + 1)<..} zero\<rbrace>" |
|
4571 (is "\<lbrace> ?P \<rbrace> ?code \<lbrace> ?Q \<rbrace>") |
|
4572 proof - |
|
4573 from h have "a < length ks" "ks ! a = v" by auto |
|
4574 from list_nth_expand[OF `a < length ks`] |
|
4575 have eq_ks: "ks = take a ks @ [ks!a] @ drop (Suc a) ks" . |
|
4576 from `a < length ks` have "ks \<noteq> []" by auto |
|
4577 hence "(reps u ia ks \<and>* zero (ia + 1)) = reps' u (ia + 1) ks" |
|
4578 by (simp add:reps'_def) |
|
4579 also from eq_ks have "\<dots> = reps' u (ia + 1) (take a ks @ [ks!a] @ drop (Suc a) ks)" by simp |
|
4580 also have "\<dots> = (EXS m. reps' u (m - 1) (take a ks) \<and>* |
|
4581 reps' m (ia + 1) (ks ! a # drop (Suc a) ks))" |
|
4582 by (simp add:reps'_append) |
|
4583 also have "\<dots> = (EXS m. reps' u (m - 1) (take a ks) \<and>* |
|
4584 reps' m (ia + 1) ([ks ! a] @ drop (Suc a) ks))" |
|
4585 by simp |
|
4586 also have "\<dots> = (EXS m ma. reps' u (m - 1) (take a ks) \<and>* |
|
4587 reps' m (ma - 1) [ks ! a] \<and>* reps' ma (ia + 1) (drop (Suc a) ks))" |
|
4588 by (simp only:reps'_append sep_conj_exists) |
|
4589 finally have eq_s: "(reps u ia ks \<and>* zero (ia + 1)) = \<dots>" . |
|
4590 { fix m ma |
|
4591 have eq_u: "-1 + u = u - 1" by smt |
|
4592 have " \<lbrace>st i \<and>* |
|
4593 ps u \<and>* |
|
4594 zero (u - 2) \<and>* |
|
4595 zero (u - 1) \<and>* |
|
4596 (reps' u (m - 1) (take a ks) \<and>* |
|
4597 reps' m (ma - 1) [ks ! a] \<and>* reps' ma (ia + 1) (drop (Suc a) ks)) \<and>* |
|
4598 fam_conj {ia + 1<..} zero\<rbrace> |
|
4599 i :[ Inc a ]: j |
|
4600 \<lbrace>st j \<and>* |
|
4601 ps u \<and>* |
|
4602 zero (u - 2) \<and>* |
|
4603 zero (u - 1) \<and>* |
|
4604 reps u (ia + int (reps_len (list_ext a ks)) - int (reps_len ks) + 1) (ks[a := Suc v]) \<and>* |
|
4605 fam_conj {ia + int (reps_len (list_ext a ks)) - int (reps_len ks) + 1<..} zero\<rbrace>" |
|
4606 proof(cases "(drop (Suc a) ks) = []") |
|
4607 case True |
|
4608 { fix hc |
|
4609 have eq_1: "(1 + (m + int (ks ! a))) = (m + int (ks ! a) + 1)" by simp |
|
4610 have eq_2: "take a ks @ [Suc (ks ! a)] = ks[a := Suc v]" |
|
4611 apply (subst (3) eq_ks, unfold True, simp) |
|
4612 by (metis True append_Nil2 eq_ks h upd_conv_take_nth_drop) |
|
4613 assume h: "(fam_conj {1 + (m + int (ks ! a))<..} zero \<and>* |
|
4614 reps u (1 + (m + int (ks ! a))) (take a ks @ [Suc (ks ! a)])) hc" |
|
4615 hence "(fam_conj {m + int (ks ! a) + 1<..} zero \<and>* reps u (m + int (ks ! a) + 1) (ks[a := Suc v])) hc" |
|
4616 by (unfold eq_1 eq_2 , sep_cancel+) |
|
4617 } note eq_fam = this |
|
4618 show ?thesis |
|
4619 apply (unfold Inc_def, subst (3) reps'_def, simp add:True sep_conj_cond) |
|
4620 apply (intro tm.pre_condI, simp) |
|
4621 apply (subst fam_conj_interv_simp, simp, subst (3) zero_def) |
|
4622 apply hsteps |
|
4623 apply (subst reps'_sg, simp add:sep_conj_cond del:ones_simps) |
|
4624 apply (rule tm.pre_condI, simp del:ones_simps) |
|
4625 apply hsteps |
|
4626 apply (rule_tac p = " |
|
4627 st j' \<and>* ps (1 + (m + int (ks ! a))) \<and>* zero (u - 1) \<and>* zero (u - 2) \<and>* |
|
4628 reps u (1 + (m + int (ks ! a))) (take a ks @ [Suc (ks!a)]) |
|
4629 \<and>* fam_conj {1 + (m + int (ks ! a))<..} zero |
|
4630 " in tm.pre_stren) |
|
4631 apply (hsteps) |
|
4632 apply (simp add:sep_conj_ac list_ext_lt[OF `a < length ks`], sep_cancel+) |
|
4633 apply (fwd eq_fam, sep_cancel+) |
|
4634 apply (simp del:ones_simps add:sep_conj_ac) |
|
4635 apply (sep_cancel+, prune) |
|
4636 apply ((fwd abs_reps')+, simp) |
|
4637 apply (fwd reps_one_abs abs_reps')+ |
|
4638 apply (subst (asm) reps'_def, simp) |
|
4639 by (subst fam_conj_interv_simp, simp add:sep_conj_ac) |
|
4640 next |
|
4641 case False |
|
4642 then obtain k' ks' where eq_drop: "drop (Suc a) ks = [k']@ks'" |
|
4643 by (metis append_Cons append_Nil list.exhaust) |
|
4644 from `a < length ks` |
|
4645 have eq_ks: "ks[a := Suc v] = (take a ks @ [Suc (ks ! a)]) @ (drop (Suc a) ks)" |
|
4646 apply (fold `ks!a = v`) |
|
4647 by (metis append_Cons append_Nil append_assoc upd_conv_take_nth_drop) |
|
4648 show ?thesis |
|
4649 apply (unfold Inc_def) |
|
4650 apply (unfold Inc_def eq_drop reps'_append, simp add:sep_conj_exists del:ones_simps) |
|
4651 apply (rule tm.precond_exI, subst (2) reps'_sg) |
|
4652 apply (subst sep_conj_cond)+ |
|
4653 apply (subst (1) ones_int_expand) |
|
4654 apply (rule tm.pre_condI, simp del:ones_simps) |
|
4655 apply hsteps |
|
4656 (* apply (hsteps hoare_locate_skip_gen[OF `a < length ks`]) *) |
|
4657 apply (subst reps'_sg, simp add:sep_conj_cond del:ones_simps) |
|
4658 apply (rule tm.pre_condI, simp del:ones_simps) |
|
4659 apply hsteps |
|
4660 apply (rule_tac p = "st j' \<and>* |
|
4661 ps (2 + (m + int (ks ! a))) \<and>* |
|
4662 reps (2 + (m + int (ks ! a))) ia (drop (Suc a) ks) \<and>* zero (ia + 1) \<and>* zero (ia + 2) \<and>* |
|
4663 reps u (m + int (ks ! a)) (take a ks @ [ks!a]) \<and>* zero (1 + (m + int (ks ! a))) \<and>* |
|
4664 zero (u - 2) \<and>* zero (u - 1) \<and>* fam_conj {ia + 2<..} zero |
|
4665 " in tm.pre_stren) |
|
4666 apply (hsteps hoare_shift_right_cons_gen[OF False] |
|
4667 hoare_left_until_double_zero_gen[OF False]) |
|
4668 apply (rule_tac p = |
|
4669 "st j' \<and>* ps (1 + (m + int (ks ! a))) \<and>* |
|
4670 zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
4671 reps u (1 + (m + int (ks ! a))) (take a ks @ [Suc (ks ! a)]) \<and>* |
|
4672 zero (2 + (m + int (ks ! a))) \<and>* |
|
4673 reps (3 + (m + int (ks ! a))) (ia + 1) (drop (Suc a) ks) \<and>* fam_conj {ia + 1<..} zero |
|
4674 " in tm.pre_stren) |
|
4675 apply (hsteps) |
|
4676 apply (simp add:sep_conj_ac, sep_cancel+) |
|
4677 apply (unfold list_ext_lt[OF `a < length ks`], simp) |
|
4678 apply (fwd abs_reps')+ |
|
4679 apply(fwd reps'_reps_abs) |
|
4680 apply (subst eq_ks, simp) |
|
4681 apply (sep_cancel+) |
|
4682 apply (thin_tac "mb = 4 + (m + (int (ks ! a) + int k'))") |
|
4683 apply (thin_tac "ma = 2 + (m + int (ks ! a))", prune) |
|
4684 apply (simp add: int_add_ac sep_conj_ac, sep_cancel+) |
|
4685 apply (fwd reps_one_abs1, subst fam_conj_interv_simp, simp, sep_cancel+, smt) |
|
4686 apply (fwd abs_ones)+ |
|
4687 apply (fwd abs_reps') |
|
4688 apply (fwd abs_reps') |
|
4689 apply (fwd abs_reps') |
|
4690 apply (fwd abs_reps') |
|
4691 apply (unfold eq_drop, simp add:int_add_ac sep_conj_ac) |
|
4692 apply (sep_cancel+) |
|
4693 apply (fwd reps'_abs[where xs = "take a ks"]) |
|
4694 apply (fwd reps'_abs[where xs = "[k']"]) |
|
4695 apply (unfold reps'_def, simp add:int_add_ac sep_conj_ac) |
|
4696 apply (sep_cancel+) |
|
4697 by (subst (asm) fam_conj_interv_simp, simp, smt) |
|
4698 qed |
|
4699 } note h = this |
|
4700 show ?thesis |
|
4701 apply (subst fam_conj_interv_simp) |
|
4702 apply (rule_tac p = "st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
4703 (reps u ia ks \<and>* zero (ia + 1)) \<and>* fam_conj {ia + 1<..} zero" |
|
4704 in tm.pre_stren) |
|
4705 apply (unfold eq_s, simp only:sep_conj_exists) |
|
4706 apply (intro tm.precond_exI h) |
|
4707 by (sep_cancel+, unfold eq_s, simp) |
|
4708 qed |
|
4709 |
|
4710 declare ones_simps [simp del] |
|
4711 |
|
4712 lemma tm_hoare_inc01: |
|
4713 assumes "length ks \<le> a \<and> v = 0" |
|
4714 shows |
|
4715 "\<lbrace>st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u ia ks \<and>* fam_conj {ia<..} zero\<rbrace> |
|
4716 i :[ Inc a ]: j |
|
4717 \<lbrace>st j \<and>* |
|
4718 ps u \<and>* |
|
4719 zero (u - 2) \<and>* |
|
4720 zero (u - 1) \<and>* |
|
4721 reps u (ia + int (reps_len (list_ext a ks)) - int (reps_len ks) + 1) ((list_ext a ks)[a := Suc v]) \<and>* |
|
4722 fam_conj {(ia + int (reps_len (list_ext a ks)) - int (reps_len ks) + 1)<..} zero\<rbrace>" |
|
4723 proof - |
|
4724 from assms have "length ks \<le> a" "v = 0" by auto |
|
4725 show ?thesis |
|
4726 apply (rule_tac p = " |
|
4727 st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* (reps u ia ks \<and>* <(ia = u + int (reps_len ks) - 1)>) \<and>* |
|
4728 fam_conj {ia<..} zero |
|
4729 " in tm.pre_stren) |
|
4730 apply (subst sep_conj_cond)+ |
|
4731 apply (rule tm.pre_condI, simp) |
|
4732 apply (unfold Inc_def) |
|
4733 apply hstep |
|
4734 (* apply (hstep hoare_locate_set_gen[OF `length ks \<le> a`]) *) |
|
4735 apply (simp only:sep_conj_exists) |
|
4736 apply (intro tm.precond_exI) |
|
4737 my_block |
|
4738 fix m w |
|
4739 have "reps m w [list_ext a ks ! a] = |
|
4740 (ones m (m + int (list_ext a ks ! a)) \<and>* <(w = m + int (list_ext a ks ! a))>)" |
|
4741 by (simp add:reps.simps) |
|
4742 my_block_end |
|
4743 apply (unfold this) |
|
4744 apply (subst sep_conj_cond)+ |
|
4745 apply (rule tm.pre_condI, simp) |
|
4746 apply (subst fam_conj_interv_simp) |
|
4747 apply (hsteps) |
|
4748 apply (subst fam_conj_interv_simp, simp) |
|
4749 apply (hsteps) |
|
4750 apply (rule_tac p = "st j' \<and>* ps (m + int (list_ext a ks ! a) + 1) \<and>* |
|
4751 zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
4752 reps u (m + int (list_ext a ks ! a) + 1) |
|
4753 ((take a (list_ext a ks))@[Suc (list_ext a ks ! a)]) \<and>* |
|
4754 fam_conj {(m + int (list_ext a ks ! a) + 1)<..} zero |
|
4755 " in tm.pre_stren) |
|
4756 apply (hsteps) |
|
4757 apply (simp add:sep_conj_ac, sep_cancel+) |
|
4758 apply (unfold `v = 0`, prune) |
|
4759 my_block |
|
4760 from `length ks \<le> a` have "list_ext a ks ! a = 0" |
|
4761 by (metis le_refl list_ext_tail) |
|
4762 from `length ks \<le> a` have "a < length (list_ext a ks)" |
|
4763 by (metis list_ext_len) |
|
4764 from reps_len_suc[OF this] |
|
4765 have eq_len: "int (reps_len (list_ext a ks)) = |
|
4766 int (reps_len (list_ext a ks[a := Suc (list_ext a ks ! a)])) - 1" |
|
4767 by smt |
|
4768 fix m w hc |
|
4769 assume h: "(fam_conj {m + int (list_ext a ks ! a) + 1<..} zero \<and>* |
|
4770 reps u (m + int (list_ext a ks ! a) + 1) (take a (list_ext a ks) @ [Suc (list_ext a ks ! a)])) |
|
4771 hc" |
|
4772 then obtain s where |
|
4773 "(reps u (m + int (list_ext a ks ! a) + 1) (take a (list_ext a ks) @ [Suc (list_ext a ks ! a)])) s" |
|
4774 by (auto dest!:sep_conjD) |
|
4775 from reps_len_correct[OF this] |
|
4776 have "m = u + int (reps_len (take a (list_ext a ks) @ [Suc (list_ext a ks ! a)])) |
|
4777 - int (list_ext a ks ! a) - 2" by smt |
|
4778 from h [unfolded this] |
|
4779 have "(fam_conj {u + int (reps_len (list_ext a ks))<..} zero \<and>* |
|
4780 reps u (u + int (reps_len (list_ext a ks))) (list_ext a ks[a := Suc 0])) |
|
4781 hc" |
|
4782 apply (unfold eq_len, simp) |
|
4783 my_block |
|
4784 from `a < length (list_ext a ks)` |
|
4785 have "take a (list_ext a ks) @ [Suc (list_ext a ks ! a)] = |
|
4786 list_ext a ks[a := Suc (list_ext a ks ! a)]" |
|
4787 by (smt `list_ext a ks ! a = 0` assms length_take list_ext_tail_expand list_update_length) |
|
4788 my_block_end |
|
4789 apply (unfold this) |
|
4790 my_block |
|
4791 have "-1 + (u + int (reps_len (list_ext a ks[a := Suc (list_ext a ks ! a)]))) = |
|
4792 u + (int (reps_len (list_ext a ks[a := Suc (list_ext a ks ! a)])) - 1)" by simp |
|
4793 my_block_end |
|
4794 apply (unfold this) |
|
4795 apply (sep_cancel+) |
|
4796 by (unfold `(list_ext a ks ! a) = 0`, simp) |
|
4797 my_block_end |
|
4798 apply (rule this, assumption) |
|
4799 apply (simp only:sep_conj_ac, sep_cancel+)+ |
|
4800 apply (fwd abs_reps')+ |
|
4801 apply (fwd reps_one_abs) |
|
4802 apply (fwd reps'_reps_abs) |
|
4803 apply (simp add:int_add_ac sep_conj_ac) |
|
4804 apply (sep_cancel+) |
|
4805 apply (subst fam_conj_interv_simp, simp add:sep_conj_ac, smt) |
|
4806 apply (fwd reps_lenE, (subst (asm) sep_conj_cond)+, erule condE, simp) |
|
4807 by (sep_cancel+) |
|
4808 qed |
|
4809 |
|
4810 definition "Dec a e = (TL continue. |
|
4811 (locate a; |
|
4812 if_reps_nz continue; |
|
4813 left_until_double_zero; |
|
4814 move_right; |
|
4815 move_right; |
|
4816 jmp e); |
|
4817 (TLabel continue; |
|
4818 right_until_zero; |
|
4819 move_left; |
|
4820 write_zero; |
|
4821 move_right; |
|
4822 move_right; |
|
4823 shift_left; |
|
4824 move_left; |
|
4825 move_left; |
|
4826 move_left; |
|
4827 left_until_double_zero; |
|
4828 move_right; |
|
4829 move_right))" |
|
4830 |
|
4831 lemma cond_eq: "((<b> \<and>* p) s) = (b \<and> (p s))" |
|
4832 proof |
|
4833 assume "(<b> \<and>* p) s" |
|
4834 from condD[OF this] show " b \<and> p s" . |
|
4835 next |
|
4836 assume "b \<and> p s" |
|
4837 hence b and "p s" by auto |
|
4838 from `b` have "(<b>) 0" by (auto simp:pasrt_def) |
|
4839 moreover have "s = 0 + s" by auto |
|
4840 moreover have "0 ## s" by auto |
|
4841 moreover note `p s` |
|
4842 ultimately show "(<b> \<and>* p) s" by (auto intro!:sep_conjI) |
|
4843 qed |
|
4844 |
|
4845 lemma tm_hoare_dec_fail00: |
|
4846 assumes "a < length ks \<and> ks ! a = 0" |
|
4847 shows "\<lbrace>st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u ia ks \<and>* fam_conj {ia<..} zero\<rbrace> |
|
4848 i :[ Dec a e ]: j |
|
4849 \<lbrace>st e \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
4850 reps u (ia + int (reps_len (list_ext a ks)) - int (reps_len ks)) (list_ext a ks[a := 0]) \<and>* |
|
4851 fam_conj {ia + int (reps_len (list_ext a ks)) - int (reps_len ks) <..} zero\<rbrace>" |
|
4852 proof - |
|
4853 from assms have "a < length ks" "ks!a = 0" by auto |
|
4854 from list_nth_expand[OF `a < length ks`] |
|
4855 have eq_ks: "ks = take a ks @ [ks ! a] @ drop (Suc a) ks" . |
|
4856 show ?thesis |
|
4857 proof(cases " drop (Suc a) ks = []") |
|
4858 case False |
|
4859 then obtain k' ks' where eq_drop: "drop (Suc a) ks = [k']@ks'" |
|
4860 by (metis append_Cons append_Nil list.exhaust) |
|
4861 show ?thesis |
|
4862 apply (unfold Dec_def, intro t_hoare_local) |
|
4863 apply (subst tassemble_to.simps(2), rule tm.code_exI, rule tm.code_extension) |
|
4864 apply (subst (1) eq_ks) |
|
4865 my_block |
|
4866 have "(reps u ia (take a ks @ [ks ! a] @ drop (Suc a) ks) \<and>* fam_conj {ia<..} zero) = |
|
4867 (reps' u (ia + 1) ((take a ks @ [ks ! a]) @ drop (Suc a) ks) \<and>* fam_conj {ia + 1<..} zero)" |
|
4868 apply (subst fam_conj_interv_simp) |
|
4869 by (unfold reps'_def, simp add:sep_conj_ac) |
|
4870 my_block_end |
|
4871 apply (unfold this) |
|
4872 apply (subst reps'_append) |
|
4873 apply (unfold eq_drop) |
|
4874 apply (subst (2) reps'_append) |
|
4875 apply (simp only:sep_conj_exists, intro tm.precond_exI) |
|
4876 apply (subst (2) reps'_def, simp add:reps.simps ones_simps) |
|
4877 apply (subst reps'_append, simp only:sep_conj_exists, intro tm.precond_exI) |
|
4878 apply hstep |
|
4879 (* apply (hstep hoare_locate_skip_gen[OF `a < length ks`]) *) |
|
4880 my_block |
|
4881 fix m mb |
|
4882 have "(reps' mb (m - 1) [ks ! a]) = (reps mb (m - 2) [ks!a] \<and>* zero (m - 1))" |
|
4883 by (simp add:reps'_def, smt) |
|
4884 my_block_end |
|
4885 apply (unfold this) |
|
4886 apply hstep |
|
4887 (* apply (hstep hoare_if_reps_nz_false_gen[OF `ks!a = 0`]) *) |
|
4888 apply (simp only:reps.simps(2), simp add:`ks!a = 0`) |
|
4889 apply (rule_tac p = "st j'b \<and>* |
|
4890 ps mb \<and>* |
|
4891 reps u mb ((take a ks)@[ks ! a]) \<and>* <(m - 2 = mb)> \<and>* |
|
4892 zero (m - 1) \<and>* |
|
4893 zero (u - 1) \<and>* |
|
4894 one m \<and>* |
|
4895 zero (u - 2) \<and>* |
|
4896 ones (m + 1) (m + int k') \<and>* |
|
4897 <(-2 + ma = m + int k')> \<and>* zero (ma - 1) \<and>* reps' ma (ia + 1) ks' \<and>* fam_conj {ia + 1<..} zero" |
|
4898 in tm.pre_stren) |
|
4899 apply hsteps |
|
4900 apply (simp add:sep_conj_ac, sep_cancel+) |
|
4901 apply (subgoal_tac "m + int k' = ma - 2", simp) |
|
4902 apply (fwd abs_ones)+ |
|
4903 apply (subst (asm) sep_conj_cond)+ |
|
4904 apply (erule condE, auto) |
|
4905 apply (fwd abs_reps')+ |
|
4906 apply (subgoal_tac "ma = m + int k' + 2", simp) |
|
4907 apply (fwd abs_reps')+ |
|
4908 my_block |
|
4909 from `a < length ks` |
|
4910 have "list_ext a ks = ks" by (auto simp:list_ext_def) |
|
4911 my_block_end |
|
4912 apply (simp add:this) |
|
4913 apply (subst eq_ks, simp add:eq_drop `ks!a = 0`) |
|
4914 apply (subst (asm) reps'_def, simp) |
|
4915 apply (subst fam_conj_interv_simp, simp add:sep_conj_ac, sep_cancel+) |
|
4916 apply (metis append_Cons assms eq_Nil_appendI eq_drop eq_ks list_update_id) |
|
4917 apply (clarsimp) |
|
4918 apply (subst (asm) sep_conj_cond)+ |
|
4919 apply (erule condE, clarsimp) |
|
4920 apply (subst (asm) sep_conj_cond)+ |
|
4921 apply (erule condE, clarsimp) |
|
4922 apply (simp add:sep_conj_ac, sep_cancel+) |
|
4923 apply (fwd abs_reps')+ |
|
4924 by (fwd reps'_reps_abs, simp add:`ks!a = 0`) |
|
4925 next |
|
4926 case True |
|
4927 show ?thesis |
|
4928 apply (unfold Dec_def, intro t_hoare_local) |
|
4929 apply (subst tassemble_to.simps(2), rule tm.code_exI, rule tm.code_extension) |
|
4930 apply (subst (1) eq_ks, unfold True, simp) |
|
4931 my_block |
|
4932 have "(reps u ia (take a ks @ [ks ! a]) \<and>* fam_conj {ia<..} zero) = |
|
4933 (reps' u (ia + 1) (take a ks @ [ks ! a]) \<and>* fam_conj {ia + 1<..} zero)" |
|
4934 apply (unfold reps'_def, subst fam_conj_interv_simp) |
|
4935 by (simp add:sep_conj_ac) |
|
4936 my_block_end |
|
4937 apply (subst (1) this) |
|
4938 apply (subst reps'_append) |
|
4939 apply (simp only:sep_conj_exists, intro tm.precond_exI) |
|
4940 apply (subst fam_conj_interv_simp, simp) |
|
4941 my_block |
|
4942 have "(zero (2 + ia)) = (tm (2 + ia) Bk)" |
|
4943 by (simp add:zero_def) |
|
4944 my_block_end my_note eq_z = this |
|
4945 apply hstep |
|
4946 (* apply (hstep hoare_locate_skip_gen[OF `a < length ks`]) *) |
|
4947 my_block |
|
4948 fix m |
|
4949 have "(reps' m (ia + 1) [ks ! a]) = (reps m ia [ks!a] \<and>* zero (ia + 1))" |
|
4950 by (simp add:reps'_def) |
|
4951 my_block_end |
|
4952 apply (unfold this, prune) |
|
4953 apply hstep |
|
4954 (* apply (hstep hoare_if_reps_nz_false_gen[OF `ks!a = 0`]) *) |
|
4955 apply (simp only:reps.simps(2), simp add:`ks!a = 0`) |
|
4956 apply (rule_tac p = "st j'b \<and>* ps m \<and>* (reps u m ((take a ks)@[ks!a]) \<and>* <(ia = m)>) |
|
4957 \<and>* zero (ia + 1) \<and>* zero (u - 1) \<and>* |
|
4958 zero (2 + ia) \<and>* zero (u - 2) \<and>* fam_conj {2 + ia<..} zero" |
|
4959 in tm.pre_stren) |
|
4960 apply hsteps |
|
4961 apply (simp add:sep_conj_ac) |
|
4962 apply ((subst (asm) sep_conj_cond)+, erule condE, simp) |
|
4963 my_block |
|
4964 from `a < length ks` have "list_ext a ks = ks" by (metis list_ext_lt) |
|
4965 my_block_end |
|
4966 apply (unfold this, simp) |
|
4967 apply (subst fam_conj_interv_simp) |
|
4968 apply (subst fam_conj_interv_simp, simp) |
|
4969 apply (simp only:sep_conj_ac, sep_cancel+) |
|
4970 apply (subst eq_ks, unfold True `ks!a = 0`, simp) |
|
4971 apply (metis True append_Nil2 assms eq_ks list_update_same_conv) |
|
4972 apply (simp add:sep_conj_ac) |
|
4973 apply (subst (asm) sep_conj_cond)+ |
|
4974 apply (erule condE, simp, thin_tac "ia = m") |
|
4975 apply (fwd abs_reps')+ |
|
4976 apply (simp add:sep_conj_ac int_add_ac, sep_cancel+) |
|
4977 apply (unfold reps'_def, simp) |
|
4978 by (metis sep.mult_commute) |
|
4979 qed |
|
4980 qed |
|
4981 |
|
4982 lemma tm_hoare_dec_fail01: |
|
4983 assumes "length ks \<le> a" |
|
4984 shows "\<lbrace>st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u ia ks \<and>* fam_conj {ia<..} zero\<rbrace> |
|
4985 i :[ Dec a e ]: j |
|
4986 \<lbrace>st e \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
4987 reps u (ia + int (reps_len (list_ext a ks)) - int (reps_len ks)) (list_ext a ks[a := 0]) \<and>* |
|
4988 fam_conj {ia + int (reps_len (list_ext a ks)) - int (reps_len ks) <..} zero\<rbrace>" |
|
4989 apply (unfold Dec_def, intro t_hoare_local) |
|
4990 apply (subst tassemble_to.simps(2), rule tm.code_exI, rule tm.code_extension) |
|
4991 apply (rule_tac p = "st i \<and>* ps u \<and>* zero (u - 2) \<and>* |
|
4992 zero (u - 1) \<and>* reps u ia ks \<and>* fam_conj {ia<..} zero \<and>* |
|
4993 <(ia = u + int (reps_len ks) - 1)>" in tm.pre_stren) |
|
4994 apply hstep |
|
4995 (* apply (hstep hoare_locate_set_gen[OF `length ks \<le> a`]) *) |
|
4996 apply (simp only:sep_conj_exists, intro tm.precond_exI) |
|
4997 my_block |
|
4998 from assms |
|
4999 have "list_ext a ks ! a = 0" by (metis le_refl list_ext_tail) |
|
5000 my_block_end my_note is_z = this |
|
5001 apply (subst fam_conj_interv_simp) |
|
5002 apply hstep |
|
5003 (* apply (hstep hoare_if_reps_nz_false_gen[OF is_z]) *) |
|
5004 apply (unfold is_z) |
|
5005 apply (subst (1) reps.simps) |
|
5006 apply (rule_tac p = "st j'b \<and>* ps m \<and>* reps u m (take a (list_ext a ks) @ [0]) \<and>* zero (w + 1) \<and>* |
|
5007 <(w = m + int 0)> \<and>* zero (u - 1) \<and>* |
|
5008 fam_conj {w + 1<..} zero \<and>* zero (u - 2) \<and>* |
|
5009 <(ia = u + int (reps_len ks) - 1)>" in tm.pre_stren) |
|
5010 my_block |
|
5011 have "(take a (list_ext a ks)) @ [0] \<noteq> []" by simp |
|
5012 my_block_end |
|
5013 apply hsteps |
|
5014 (* apply (hsteps hoare_left_until_double_zero_gen[OF this]) *) |
|
5015 apply (simp add:sep_conj_ac) |
|
5016 apply prune |
|
5017 apply (subst (asm) sep_conj_cond)+ |
|
5018 apply (elim condE, simp add:sep_conj_ac, prune) |
|
5019 my_block |
|
5020 fix m w ha |
|
5021 assume h1: "w = m \<and> ia = u + int (reps_len ks) - 1" |
|
5022 and h: "(ps u \<and>* |
|
5023 st e \<and>* |
|
5024 zero (u - 1) \<and>* |
|
5025 zero (m + 1) \<and>* |
|
5026 fam_conj {m + 1<..} zero \<and>* zero (u - 2) \<and>* reps u m (take a (list_ext a ks) @ [0])) ha" |
|
5027 from h1 have eq_w: "w = m" and eq_ia: "ia = u + int (reps_len ks) - 1" by auto |
|
5028 from h obtain s' where "reps u m (take a (list_ext a ks) @ [0]) s'" |
|
5029 by (auto dest!:sep_conjD) |
|
5030 from reps_len_correct[OF this] |
|
5031 have eq_m: "m = u + int (reps_len (take a (list_ext a ks) @ [0])) - 1" . |
|
5032 from h[unfolded eq_m, simplified] |
|
5033 have "(ps u \<and>* |
|
5034 st e \<and>* |
|
5035 zero (u - 1) \<and>* |
|
5036 zero (u - 2) \<and>* |
|
5037 fam_conj {u + (-1 + int (reps_len (list_ext a ks)))<..} zero \<and>* |
|
5038 reps u (u + (-1 + int (reps_len (list_ext a ks)))) (list_ext a ks[a := 0])) ha" |
|
5039 apply (sep_cancel+) |
|
5040 apply (subst fam_conj_interv_simp, simp) |
|
5041 my_block |
|
5042 from `length ks \<le> a` have "list_ext a ks[a := 0] = list_ext a ks" |
|
5043 by (metis is_z list_update_id) |
|
5044 my_block_end |
|
5045 apply (unfold this) |
|
5046 my_block |
|
5047 from `length ks \<le> a` is_z |
|
5048 have "take a (list_ext a ks) @ [0] = list_ext a ks" |
|
5049 by (metis list_ext_tail_expand) |
|
5050 my_block_end |
|
5051 apply (unfold this) |
|
5052 by (simp add:sep_conj_ac, sep_cancel+, smt) |
|
5053 my_block_end |
|
5054 apply (rule this, assumption) |
|
5055 apply (sep_cancel+)[1] |
|
5056 apply (subst (asm) sep_conj_cond)+ |
|
5057 apply (erule condE, prune, simp) |
|
5058 my_block |
|
5059 fix s m |
|
5060 assume "(reps' u (m - 1) (take a (list_ext a ks)) \<and>* ones m m \<and>* zero (m + 1)) s" |
|
5061 hence "reps' u (m + 1) (take a (list_ext a ks) @ [0]) s" |
|
5062 apply (unfold reps'_append) |
|
5063 apply (rule_tac x = m in EXS_intro) |
|
5064 by (subst (2) reps'_def, simp add:reps.simps) |
|
5065 my_block_end |
|
5066 apply (rotate_tac 1, fwd this) |
|
5067 apply (subst (asm) reps'_def, simp add:sep_conj_ac) |
|
5068 my_block |
|
5069 fix s |
|
5070 assume h: "(st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
5071 reps u ia ks \<and>* fam_conj {ia<..} zero) s" |
|
5072 then obtain s' where "reps u ia ks s'" by (auto dest!:sep_conjD) |
|
5073 from reps_len_correct[OF this] have eq_ia: "ia = u + int (reps_len ks) - 1" . |
|
5074 from h |
|
5075 have "(st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u ia ks \<and>* |
|
5076 fam_conj {ia<..} zero \<and>* <(ia = u + int (reps_len ks) - 1)>) s" |
|
5077 by (unfold eq_ia, simp) |
|
5078 my_block_end |
|
5079 by (rule this, assumption) |
|
5080 |
|
5081 lemma t_hoare_label1: |
|
5082 "(\<And>l. l = i \<Longrightarrow> \<lbrace>st l \<and>* p\<rbrace> l :[ c l ]: j \<lbrace>st k \<and>* q\<rbrace>) \<Longrightarrow> |
|
5083 \<lbrace>st l \<and>* p \<rbrace> |
|
5084 i:[(TLabel l; c l)]:j |
|
5085 \<lbrace>st k \<and>* q\<rbrace>" |
|
5086 by (unfold tassemble_to.simps, intro tm.code_exI tm.code_condI, clarify, auto) |
|
5087 |
|
5088 lemma tm_hoare_dec_fail1: |
|
5089 assumes "a < length ks \<and> ks ! a = 0 \<or> length ks \<le> a" |
|
5090 shows "\<lbrace>st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u ia ks \<and>* fam_conj {ia<..} zero\<rbrace> |
|
5091 i :[ Dec a e ]: j |
|
5092 \<lbrace>st e \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
5093 reps u (ia + int (reps_len (list_ext a ks)) - int (reps_len ks)) (list_ext a ks[a := 0]) \<and>* |
|
5094 fam_conj {ia + int (reps_len (list_ext a ks)) - int (reps_len ks) <..} zero\<rbrace>" |
|
5095 using assms |
|
5096 proof |
|
5097 assume "a < length ks \<and> ks ! a = 0" |
|
5098 thus ?thesis |
|
5099 by (rule tm_hoare_dec_fail00) |
|
5100 next |
|
5101 assume "length ks \<le> a" |
|
5102 thus ?thesis |
|
5103 by (rule tm_hoare_dec_fail01) |
|
5104 qed |
|
5105 |
|
5106 lemma shift_left_nil_gen[step]: |
|
5107 assumes "u = v" |
|
5108 shows "\<lbrace>st i \<and>* ps u \<and>* zero v\<rbrace> |
|
5109 i :[shift_left]:j |
|
5110 \<lbrace>st j \<and>* ps u \<and>* zero v\<rbrace>" |
|
5111 apply(unfold assms shift_left_def, intro t_hoare_local t_hoare_label, clarify, |
|
5112 rule t_hoare_label_last, simp, clarify, prune, simp) |
|
5113 by hstep |
|
5114 |
|
5115 lemma tm_hoare_dec_suc1: |
|
5116 assumes "a < length ks \<and> ks ! a = Suc v" |
|
5117 shows "\<lbrace>st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u ia ks \<and>* fam_conj {ia<..} zero\<rbrace> |
|
5118 i :[ Dec a e ]: j |
|
5119 \<lbrace>st j \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
5120 reps u (ia - 1) (list_ext a ks[a := v]) \<and>* |
|
5121 fam_conj {ia - 1<..} zero\<rbrace>" |
|
5122 proof - |
|
5123 from assms have "a < length ks" " ks ! a = Suc v" by auto |
|
5124 from list_nth_expand[OF `a < length ks`] |
|
5125 have eq_ks: "ks = take a ks @ [ks ! a] @ drop (Suc a) ks" . |
|
5126 show ?thesis |
|
5127 proof(cases " drop (Suc a) ks = []") |
|
5128 case False |
|
5129 then obtain k' ks' where eq_drop: "drop (Suc a) ks = [k']@ks'" |
|
5130 by (metis append_Cons append_Nil list.exhaust) |
|
5131 show ?thesis |
|
5132 apply (unfold Dec_def, intro t_hoare_local) |
|
5133 apply (subst tassemble_to.simps(2), rule tm.code_exI) |
|
5134 apply (subst (1) eq_ks) |
|
5135 my_block |
|
5136 have "(reps u ia (take a ks @ [ks ! a] @ drop (Suc a) ks) \<and>* fam_conj {ia<..} zero) = |
|
5137 (reps' u (ia + 1) ((take a ks @ [ks ! a]) @ drop (Suc a) ks) \<and>* fam_conj {ia + 1<..} zero)" |
|
5138 apply (subst fam_conj_interv_simp) |
|
5139 by (unfold reps'_def, simp add:sep_conj_ac) |
|
5140 my_block_end |
|
5141 apply (unfold this) |
|
5142 apply (subst reps'_append) |
|
5143 apply (unfold eq_drop) |
|
5144 apply (subst (2) reps'_append) |
|
5145 apply (simp only:sep_conj_exists, intro tm.precond_exI) |
|
5146 apply (subst (2) reps'_def, simp add:reps.simps ones_simps) |
|
5147 apply (subst reps'_append, simp only:sep_conj_exists, intro tm.precond_exI) |
|
5148 apply (rule_tac q = |
|
5149 "st l \<and>* |
|
5150 ps mb \<and>* |
|
5151 zero (u - 1) \<and>* |
|
5152 reps' u (mb - 1) (take a ks) \<and>* |
|
5153 reps' mb (m - 1) [ks ! a] \<and>* |
|
5154 one m \<and>* |
|
5155 zero (u - 2) \<and>* |
|
5156 ones (m + 1) (m + int k') \<and>* |
|
5157 <(-2 + ma = m + int k')> \<and>* zero (ma - 1) \<and>* reps' ma (ia + 1) ks' \<and>* fam_conj {ia + 1<..} zero" |
|
5158 in tm.sequencing) |
|
5159 apply (rule tm.code_extension) |
|
5160 apply hstep |
|
5161 (* apply (hstep hoare_locate_skip_gen[OF `a < length ks`]) *) |
|
5162 apply (subst (2) reps'_def, simp) |
|
5163 my_block |
|
5164 fix i j l m mb |
|
5165 from `ks!a = (Suc v)` have "ks!a \<noteq> 0" by simp |
|
5166 from hoare_if_reps_nz_true[OF this, where u = mb and v = "m - 2"] |
|
5167 have "\<lbrace>st i \<and>* ps mb \<and>* reps mb (-2 + m) [ks ! a]\<rbrace> |
|
5168 i :[ if_reps_nz l ]: j |
|
5169 \<lbrace>st l \<and>* ps mb \<and>* reps mb (-2 + m) [ks ! a]\<rbrace>" |
|
5170 by smt |
|
5171 my_block_end |
|
5172 apply (hgoto this) |
|
5173 apply (simp add:sep_conj_ac, sep_cancel+) |
|
5174 apply (subst reps'_def, simp add:sep_conj_ac) |
|
5175 apply (rule tm.code_extension1) |
|
5176 apply (rule t_hoare_label1, simp, prune) |
|
5177 apply (subst (2) reps'_def, simp add:reps.simps) |
|
5178 apply (rule_tac p = "st j' \<and>* ps mb \<and>* zero (u - 1) \<and>* reps' u (mb - 1) (take a ks) \<and>* |
|
5179 ((ones mb (mb + int (ks ! a)) \<and>* <(-2 + m = mb + int (ks ! a))>) \<and>* zero (mb + int (ks ! a) + 1)) \<and>* |
|
5180 one (mb + int (ks ! a) + 2) \<and>* zero (u - 2) \<and>* |
|
5181 ones (mb + int (ks ! a) + 3) (mb + int (ks ! a) + int k' + 2) \<and>* |
|
5182 <(-2 + ma = m + int k')> \<and>* zero (ma - 1) \<and>* reps' ma (ia + 1) ks' \<and>* fam_conj {ia + 1<..} zero |
|
5183 " in tm.pre_stren) |
|
5184 apply hsteps |
|
5185 (* apply (simp add:sep_conj_ac) *) |
|
5186 apply (unfold `ks!a = Suc v`) |
|
5187 my_block |
|
5188 fix mb |
|
5189 have "(ones mb (mb + int (Suc v))) = (ones mb (mb + int v) \<and>* one (mb + int (Suc v)))" |
|
5190 by (simp add:ones_rev) |
|
5191 my_block_end |
|
5192 apply (unfold this, prune) |
|
5193 apply hsteps |
|
5194 apply (rule_tac p = "st j'a \<and>* |
|
5195 ps (mb + int (Suc v) + 2) \<and>* zero (mb + int (Suc v) + 1) \<and>* |
|
5196 reps (mb + int (Suc v) + 2) ia (drop (Suc a) ks) \<and>* zero (ia + 1) \<and>* zero (ia + 2) \<and>* |
|
5197 zero (mb + int (Suc v)) \<and>* |
|
5198 ones mb (mb + int v) \<and>* |
|
5199 zero (u - 1) \<and>* |
|
5200 reps' u (mb - 1) (take a ks) \<and>* |
|
5201 zero (u - 2) \<and>* fam_conj {ia + 2<..} zero |
|
5202 " in tm.pre_stren) |
|
5203 apply hsteps |
|
5204 (* apply (hsteps hoare_shift_left_cons_gen[OF False]) *) |
|
5205 apply (rule_tac p = "st j'a \<and>* ps (ia - 1) \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
5206 reps u (ia - 1) (take a ks @ [v] @ drop (Suc a) ks) \<and>* |
|
5207 zero ia \<and>* zero (ia + 1) \<and>* zero (ia + 2) \<and>* |
|
5208 fam_conj {ia + 2<..} zero |
|
5209 " in tm.pre_stren) |
|
5210 apply hsteps |
|
5211 apply (simp add:sep_conj_ac) |
|
5212 apply (subst fam_conj_interv_simp) |
|
5213 apply (subst fam_conj_interv_simp) |
|
5214 apply (subst fam_conj_interv_simp) |
|
5215 apply (simp add:sep_conj_ac) |
|
5216 apply (sep_cancel+) |
|
5217 my_block |
|
5218 have "take a ks @ v # drop (Suc a) ks = list_ext a ks[a := v]" |
|
5219 proof - |
|
5220 from `a < length ks` have "list_ext a ks = ks" by (metis list_ext_lt) |
|
5221 hence "list_ext a ks[a:=v] = ks[a:=v]" by simp |
|
5222 moreover from `a < length ks` have "ks[a:=v] = take a ks @ v # drop (Suc a) ks" |
|
5223 by (metis upd_conv_take_nth_drop) |
|
5224 ultimately show ?thesis by metis |
|
5225 qed |
|
5226 my_block_end |
|
5227 apply (unfold this, sep_cancel+, smt) |
|
5228 apply (simp add:sep_conj_ac) |
|
5229 apply (fwd abs_reps')+ |
|
5230 apply (simp add:sep_conj_ac int_add_ac) |
|
5231 apply (sep_cancel+) |
|
5232 apply (subst (asm) reps'_def, simp add:sep_conj_ac) |
|
5233 apply (subst (asm) sep_conj_cond)+ |
|
5234 apply (erule condE, clarsimp) |
|
5235 apply (simp add:sep_conj_ac, sep_cancel+) |
|
5236 apply (fwd abs_ones)+ |
|
5237 apply (fwd abs_reps')+ |
|
5238 apply (subst (asm) reps'_def, simp) |
|
5239 apply (subst (asm) fam_conj_interv_simp) |
|
5240 apply (simp add:sep_conj_ac int_add_ac eq_drop reps'_def) |
|
5241 apply (subst (asm) sep_conj_cond)+ |
|
5242 apply (erule condE, clarsimp) |
|
5243 by (simp add:sep_conj_ac int_add_ac) |
|
5244 next |
|
5245 case True |
|
5246 show ?thesis |
|
5247 apply (unfold Dec_def, intro t_hoare_local) |
|
5248 apply (subst tassemble_to.simps(2), rule tm.code_exI) |
|
5249 apply (subst (1) eq_ks, simp add:True) |
|
5250 my_block |
|
5251 have "(reps u ia (take a ks @ [ks ! a]) \<and>* fam_conj {ia<..} zero) = |
|
5252 (reps' u (ia + 1) (take a ks @ [ks ! a]) \<and>* fam_conj {ia + 1<..} zero)" |
|
5253 apply (subst fam_conj_interv_simp) |
|
5254 by (unfold reps'_def, simp add:sep_conj_ac) |
|
5255 my_block_end |
|
5256 apply (unfold this) |
|
5257 apply (subst reps'_append) |
|
5258 apply (simp only:sep_conj_exists, intro tm.precond_exI) |
|
5259 apply (rule_tac q = "st l \<and>* ps m \<and>* zero (u - 1) \<and>* reps' u (m - 1) (take a ks) \<and>* |
|
5260 reps' m (ia + 1) [ks ! a] \<and>* zero (2 + ia) \<and>* zero (u - 2) \<and>* fam_conj {2 + ia<..} zero" |
|
5261 in tm.sequencing) |
|
5262 apply (rule tm.code_extension) |
|
5263 apply (subst fam_conj_interv_simp, simp) |
|
5264 apply hsteps |
|
5265 (* apply (hstep hoare_locate_skip_gen[OF `a < length ks`]) *) |
|
5266 my_block |
|
5267 fix m |
|
5268 have "(reps' m (ia + 1) [ks ! a]) = |
|
5269 (reps m ia [ks!a] \<and>* zero (ia + 1))" |
|
5270 by (unfold reps'_def, simp) |
|
5271 my_block_end |
|
5272 apply (unfold this) |
|
5273 my_block |
|
5274 fix i j l m |
|
5275 from `ks!a = (Suc v)` have "ks!a \<noteq> 0" by simp |
|
5276 my_block_end |
|
5277 apply (hgoto hoare_if_reps_nz_true_gen) |
|
5278 apply (rule tm.code_extension1) |
|
5279 apply (rule t_hoare_label1, simp) |
|
5280 apply (thin_tac "la = j'", prune) |
|
5281 apply (subst (1) reps.simps) |
|
5282 apply (subst sep_conj_cond)+ |
|
5283 apply (rule tm.pre_condI, simp) |
|
5284 apply hsteps |
|
5285 apply (unfold `ks!a = Suc v`) |
|
5286 my_block |
|
5287 fix m |
|
5288 have "(ones m (m + int (Suc v))) = (ones m (m + int v) \<and>* one (m + int (Suc v)))" |
|
5289 by (simp add:ones_rev) |
|
5290 my_block_end |
|
5291 apply (unfold this) |
|
5292 apply hsteps |
|
5293 apply (rule_tac p = "st j'a \<and>* ps (m + int v) \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
5294 reps u (m + int v) (take a ks @ [v]) \<and>* zero (m + (1 + int v)) \<and>* |
|
5295 zero (2 + (m + int v)) \<and>* zero (3 + (m + int v)) \<and>* |
|
5296 fam_conj {3 + (m + int v)<..} zero |
|
5297 " in tm.pre_stren) |
|
5298 apply hsteps |
|
5299 apply (simp add:sep_conj_ac, sep_cancel+) |
|
5300 my_block |
|
5301 have "take a ks @ [v] = list_ext a ks[a := v]" |
|
5302 proof - |
|
5303 from True `a < length ks` have "ks = take a ks @ [ks!a]" |
|
5304 by (metis append_Nil2 eq_ks) |
|
5305 hence "ks[a:=v] = take a ks @ [v]" |
|
5306 by (metis True `a < length ks` upd_conv_take_nth_drop) |
|
5307 moreover from `a < length ks` have "list_ext a ks = ks" |
|
5308 by (metis list_ext_lt) |
|
5309 ultimately show ?thesis by simp |
|
5310 qed |
|
5311 my_block_end my_note eq_l = this |
|
5312 apply (unfold this) |
|
5313 apply (subst fam_conj_interv_simp) |
|
5314 apply (subst fam_conj_interv_simp) |
|
5315 apply (subst fam_conj_interv_simp) |
|
5316 apply (simp add:sep_conj_ac, sep_cancel, smt) |
|
5317 apply (simp add:sep_conj_ac int_add_ac)+ |
|
5318 apply (sep_cancel+) |
|
5319 apply (fwd abs_reps')+ |
|
5320 apply (fwd reps'_reps_abs) |
|
5321 by (simp add:eq_l) |
|
5322 qed |
|
5323 qed |
|
5324 |
|
5325 definition "cfill_until_one = (TL start exit. |
|
5326 TLabel start; |
|
5327 if_one exit; |
|
5328 write_one; |
|
5329 move_left; |
|
5330 jmp start; |
|
5331 TLabel exit |
|
5332 )" |
|
5333 |
|
5334 lemma hoare_cfill_until_one: |
|
5335 "\<lbrace>st i \<and>* ps v \<and>* one (u - 1) \<and>* zeros u v\<rbrace> |
|
5336 i :[ cfill_until_one ]: j |
|
5337 \<lbrace>st j \<and>* ps (u - 1) \<and>* ones (u - 1) v \<rbrace>" |
|
5338 proof(induct u v rule:zeros_rev_induct) |
|
5339 case (Base x y) |
|
5340 thus ?case |
|
5341 apply (subst sep_conj_cond)+ |
|
5342 apply (rule tm.pre_condI, simp add:ones_simps) |
|
5343 apply (unfold cfill_until_one_def, intro t_hoare_local t_hoare_label, rule t_hoare_label_last, simp+) |
|
5344 by hstep |
|
5345 next |
|
5346 case (Step x y) |
|
5347 show ?case |
|
5348 apply (rule_tac q = "st i \<and>* ps (y - 1) \<and>* one (x - 1) \<and>* zeros x (y - 1) \<and>* one y" in tm.sequencing) |
|
5349 apply (subst cfill_until_one_def, intro t_hoare_local t_hoare_label, rule t_hoare_label_last, simp+) |
|
5350 apply hsteps |
|
5351 my_block |
|
5352 fix i j l |
|
5353 have "\<lbrace>st i \<and>* ps (y - 1) \<and>* one (x - 1) \<and>* zeros x (y - 1)\<rbrace> |
|
5354 i :[ jmp l ]: j |
|
5355 \<lbrace>st l \<and>* ps (y - 1) \<and>* one (x - 1) \<and>* zeros x (y - 1)\<rbrace>" |
|
5356 apply (case_tac "(y - 1) < x", simp add:zeros_simps) |
|
5357 apply (subst sep_conj_cond)+ |
|
5358 apply (rule tm.pre_condI, simp) |
|
5359 apply hstep |
|
5360 apply (drule_tac zeros_rev, simp) |
|
5361 by hstep |
|
5362 my_block_end |
|
5363 apply (hstep this) |
|
5364 (* The next half *) |
|
5365 apply (hstep Step(2), simp add:sep_conj_ac, sep_cancel+) |
|
5366 by (insert Step(1), simp add:ones_rev sep_conj_ac) |
|
5367 qed |
|
5368 |
|
5369 definition "cmove = (TL start exit. |
|
5370 TLabel start; |
|
5371 left_until_zero; |
|
5372 left_until_one; |
|
5373 move_left; |
|
5374 if_zero exit; |
|
5375 move_right; |
|
5376 write_zero; |
|
5377 right_until_one; |
|
5378 right_until_zero; |
|
5379 write_one; |
|
5380 jmp start; |
|
5381 TLabel exit |
|
5382 )" |
|
5383 |
|
5384 declare zeros.simps [simp del] zeros_simps[simp del] |
|
5385 |
|
5386 lemma hoare_cmove: |
|
5387 assumes "w \<le> k" |
|
5388 shows "\<lbrace>st i \<and>* ps (v + 2 + int w) \<and>* zero (u - 1) \<and>* |
|
5389 reps u (v - int w) [k - w] \<and>* zeros (v - int w + 1) (v + 1) \<and>* |
|
5390 one (v + 2) \<and>* ones (v + 3) (v + 2 + int w) \<and>* zeros (v + 3 + int w) (v + int(reps_len [k]) + 1)\<rbrace> |
|
5391 i :[cmove]: j |
|
5392 \<lbrace>st j \<and>* ps (u - 1) \<and>* zero (u - 1) \<and>* reps u u [0] \<and>* zeros (u + 1) (v + 1) \<and>* |
|
5393 reps (v + 2) (v + int (reps_len [k]) + 1) [k]\<rbrace>" |
|
5394 using assms |
|
5395 proof(induct "k - w" arbitrary: w) |
|
5396 case (0 w) |
|
5397 hence "w = k" by auto |
|
5398 show ?case |
|
5399 apply (simp add: `w = k` del:zeros.simps zeros_simps) |
|
5400 apply (unfold cmove_def, intro t_hoare_local t_hoare_label, rule t_hoare_label_last, simp+) |
|
5401 apply (simp add:reps_len_def reps_sep_len_def reps_ctnt_len_def del:zeros_simps zeros.simps) |
|
5402 apply (rule_tac p = "st i \<and>* ps (v + 2 + int k) \<and>* zero (u - 1) \<and>* |
|
5403 reps u u [0] \<and>* zeros (u + 1) (v + 1) \<and>* |
|
5404 ones (v + 2) (v + 2 + int k) \<and>* zeros (v + 3 + int k) (2 + (v + int k)) \<and>* |
|
5405 <(u = v - int k)>" |
|
5406 in tm.pre_stren) |
|
5407 my_block |
|
5408 fix i j |
|
5409 have "\<lbrace>st i \<and>* ps (v + 2 + int k) \<and>* zeros (u + 1) (v + 1) \<and>* ones (v + 2) (v + 2 + int k) |
|
5410 \<and>* <(u = v - int k)>\<rbrace> |
|
5411 i :[ left_until_zero ]: j |
|
5412 \<lbrace>st j \<and>* ps (v + 1) \<and>* zeros (u + 1) (v + 1) \<and>* ones (v + 2) (v + 2 + int k) |
|
5413 \<and>* <(u = v - int k)>\<rbrace>" |
|
5414 apply (subst sep_conj_cond)+ |
|
5415 apply (rule tm.pre_condI, simp) |
|
5416 my_block |
|
5417 have "(zeros (v - int k + 1) (v + 1)) = (zeros (v - int k + 1) v \<and>* zero (v + 1))" |
|
5418 by (simp only:zeros_rev, smt) |
|
5419 my_block_end |
|
5420 apply (unfold this) |
|
5421 by hsteps |
|
5422 my_block_end |
|
5423 apply (hstep this) |
|
5424 my_block |
|
5425 fix i j |
|
5426 have "\<lbrace>st i \<and>* ps (v + 1) \<and>* reps u u [0] \<and>* zeros (u + 1) (v + 1)\<rbrace> |
|
5427 i :[left_until_one]:j |
|
5428 \<lbrace>st j \<and>* ps u \<and>* reps u u [0] \<and>* zeros (u + 1) (v + 1)\<rbrace>" |
|
5429 apply (simp add:reps.simps ones_simps) |
|
5430 by hsteps |
|
5431 my_block_end |
|
5432 apply (hsteps this) |
|
5433 apply ((subst (asm) sep_conj_cond)+, erule condE, clarsimp) |
|
5434 apply (fwd abs_reps')+ |
|
5435 apply (simp only:sep_conj_ac int_add_ac, sep_cancel+) |
|
5436 apply (simp add:int_add_ac sep_conj_ac zeros_simps) |
|
5437 apply (simp add:sep_conj_ac int_add_ac, sep_cancel+) |
|
5438 apply (fwd reps_lenE) |
|
5439 apply (subst (asm) sep_conj_cond)+ |
|
5440 apply (erule condE, clarsimp) |
|
5441 apply (subgoal_tac "v = u + int k + int (reps_len [0]) - 1", clarsimp) |
|
5442 apply (simp add:reps_len_sg) |
|
5443 apply (fwd abs_ones)+ |
|
5444 apply (fwd abs_reps')+ |
|
5445 apply (simp add:sep_conj_ac int_add_ac) |
|
5446 apply (sep_cancel+) |
|
5447 apply (simp add:reps.simps, smt) |
|
5448 by (clarsimp) |
|
5449 next |
|
5450 case (Suc k' w) |
|
5451 from `Suc k' = k - w` `w \<le> k` |
|
5452 have h: "k' = k - (Suc w)" "Suc w \<le> k" by auto |
|
5453 show ?case |
|
5454 apply (rule tm.sequencing[OF _ Suc(1)[OF h(1, 2)]]) |
|
5455 apply (unfold cmove_def, intro t_hoare_local t_hoare_label, rule t_hoare_label_last, simp+) |
|
5456 apply (simp add:reps_len_def reps_sep_len_def reps_ctnt_len_def del:zeros_simps zeros.simps) |
|
5457 my_block |
|
5458 fix i j |
|
5459 have "\<lbrace>st i \<and>* ps (v + 2 + int w) \<and>* zeros (v - int w + 1) (v + 1) \<and>* |
|
5460 one (v + 2) \<and>* ones (v + 3) (v + 2 + int w) \<rbrace> |
|
5461 i :[left_until_zero]: j |
|
5462 \<lbrace>st j \<and>* ps (v + 1) \<and>* zeros (v - int w + 1) (v + 1) \<and>* |
|
5463 one (v + 2) \<and>* ones (v + 3) (v + 2 + int w) \<rbrace>" |
|
5464 my_block |
|
5465 have "(one (v + 2) \<and>* ones (v + 3) (v + 2 + int w)) = |
|
5466 ones (v + 2) (v + 2 + int w)" |
|
5467 by (simp only:ones_simps, smt) |
|
5468 my_block_end |
|
5469 apply (unfold this) |
|
5470 my_block |
|
5471 have "(zeros (v - int w + 1) (v + 1)) = (zeros (v - int w + 1) v \<and>* zero (v + 1))" |
|
5472 by (simp only:zeros_rev, simp) |
|
5473 my_block_end |
|
5474 apply (unfold this) |
|
5475 by hsteps |
|
5476 my_block_end |
|
5477 apply (hstep this) |
|
5478 my_block |
|
5479 fix i j |
|
5480 have "\<lbrace>st i \<and>* ps (v + 1) \<and>* reps u (v - int w) [k - w] \<and>* zeros (v - int w + 1) (v + 1)\<rbrace> |
|
5481 i :[left_until_one]: j |
|
5482 \<lbrace>st j \<and>* ps (v - int w) \<and>* reps u (v - int w) [k - w] \<and>* zeros (v - int w + 1) (v + 1)\<rbrace>" |
|
5483 apply (simp add:reps.simps ones_rev) |
|
5484 apply (subst sep_conj_cond)+ |
|
5485 apply (rule tm.pre_condI, clarsimp) |
|
5486 apply (subgoal_tac "u + int (k - w) = v - int w", simp) |
|
5487 defer |
|
5488 apply simp |
|
5489 by hsteps |
|
5490 my_block_end |
|
5491 apply (hstep this) |
|
5492 my_block |
|
5493 have "(reps u (v - int w) [k - w]) = (reps u (v - (1 + int w)) [k - Suc w] \<and>* one (v - int w))" |
|
5494 apply (subst (1 2) reps.simps) |
|
5495 apply (subst sep_conj_cond)+ |
|
5496 my_block |
|
5497 have "((v - int w = u + int (k - w))) = |
|
5498 (v - (1 + int w) = u + int (k - Suc w))" |
|
5499 apply auto |
|
5500 apply (smt Suc.prems h(2)) |
|
5501 by (smt Suc.prems h(2)) |
|
5502 my_block_end |
|
5503 apply (simp add:this) |
|
5504 my_block |
|
5505 fix b p q |
|
5506 assume "(b \<Longrightarrow> (p::tassert) = q)" |
|
5507 have "(<b> \<and>* p) = (<b> \<and>* q)" |
|
5508 by (metis `b \<Longrightarrow> p = q` cond_eqI) |
|
5509 my_block_end |
|
5510 apply (rule this) |
|
5511 my_block |
|
5512 assume "v - (1 + int w) = u + int (k - Suc w)" |
|
5513 hence "v = 1 + int w + u + int (k - Suc w)" by auto |
|
5514 my_block_end |
|
5515 apply (simp add:this) |
|
5516 my_block |
|
5517 have "\<not> (u + int (k - w)) < u" by auto |
|
5518 my_block_end |
|
5519 apply (unfold ones_rev[OF this]) |
|
5520 my_block |
|
5521 from Suc (2, 3) have "(u + int (k - w) - 1) = (u + int (k - Suc w))" |
|
5522 by auto |
|
5523 my_block_end |
|
5524 apply (unfold this) |
|
5525 my_block |
|
5526 from Suc (2, 3) have "(u + int (k - w)) = (1 + (u + int (k - Suc w)))" |
|
5527 by auto |
|
5528 my_block_end |
|
5529 by (unfold this, simp) |
|
5530 my_block_end |
|
5531 apply (unfold this) |
|
5532 my_block |
|
5533 fix i j |
|
5534 have "\<lbrace>st i \<and>* ps (v - int w) \<and>* |
|
5535 (reps u (v - (1 + int w)) [k - Suc w] \<and>* one (v - int w))\<rbrace> |
|
5536 i :[ move_left]: j |
|
5537 \<lbrace>st j \<and>* ps (v - (1 + int w)) \<and>* |
|
5538 (reps u (v - (1 + int w)) [k - Suc w] \<and>* one (v - int w))\<rbrace>" |
|
5539 apply (simp add:reps.simps ones_rev) |
|
5540 apply (subst sep_conj_cond)+ |
|
5541 apply (rule tm.pre_condI, clarsimp) |
|
5542 apply (subgoal_tac " u + int (k - Suc w) = v - (1 + int w)", simp) |
|
5543 defer |
|
5544 apply simp |
|
5545 apply hsteps |
|
5546 by (simp add:sep_conj_ac, sep_cancel+, smt) |
|
5547 my_block_end |
|
5548 apply (hstep this) |
|
5549 my_block |
|
5550 fix i' j' |
|
5551 have "\<lbrace>st i' \<and>* ps (v - (1 + int w)) \<and>* reps u (v - (1 + int w)) [k - Suc w]\<rbrace> |
|
5552 i' :[ if_zero j ]: j' |
|
5553 \<lbrace>st j' \<and>* ps (v - (1 + int w)) \<and>* reps u (v - (1 + int w)) [k - Suc w]\<rbrace>" |
|
5554 apply (simp add:reps.simps ones_rev) |
|
5555 apply (subst sep_conj_cond)+ |
|
5556 apply (rule tm.pre_condI, clarsimp) |
|
5557 apply (subgoal_tac " u + int (k - Suc w) = v - (1 + int w)", simp) |
|
5558 defer |
|
5559 apply simp |
|
5560 by hstep |
|
5561 my_block_end |
|
5562 apply (hstep this) |
|
5563 my_block |
|
5564 fix i j |
|
5565 have "\<lbrace>st i \<and>* ps (v - (1 + int w)) \<and>* reps u (v - (1 + int w)) [k - Suc w]\<rbrace> |
|
5566 i :[ move_right ]: j |
|
5567 \<lbrace>st j \<and>* ps (v - int w) \<and>* reps u (v - (1 + int w)) [k - Suc w] \<rbrace>" |
|
5568 apply (simp add:reps.simps ones_rev) |
|
5569 apply (subst sep_conj_cond)+ |
|
5570 apply (rule tm.pre_condI, clarsimp) |
|
5571 apply (subgoal_tac " u + int (k - Suc w) = v - (1 + int w)", simp) |
|
5572 defer |
|
5573 apply simp |
|
5574 by hstep |
|
5575 my_block_end |
|
5576 apply (hsteps this) |
|
5577 my_block |
|
5578 fix i j |
|
5579 have "\<lbrace>st i \<and>* ps (v - int w) \<and>* one (v + 2) \<and>* |
|
5580 zero (v - int w) \<and>* zeros (v - int w + 1) (v + 1)\<rbrace> |
|
5581 i :[right_until_one]: j |
|
5582 \<lbrace>st j \<and>* ps (v + 2) \<and>* one (v + 2) \<and>* zero (v - int w) \<and>* zeros (v - int w + 1) (v + 1)\<rbrace>" |
|
5583 my_block |
|
5584 have "(zero (v - int w) \<and>* zeros (v - int w + 1) (v + 1)) = |
|
5585 (zeros (v - int w) (v + 1))" |
|
5586 by (simp add:zeros_simps) |
|
5587 my_block_end |
|
5588 apply (unfold this) |
|
5589 by hsteps |
|
5590 my_block_end |
|
5591 apply (hstep this) |
|
5592 my_block |
|
5593 from Suc(2, 3) have "w < k" by auto |
|
5594 hence "(zeros (v + 3 + int w) (2 + (v + int k))) = |
|
5595 (zero (v + 3 + int w) \<and>* zeros (4 + (v + int w)) (2 + (v + int k)))" |
|
5596 by (simp add:zeros_simps) |
|
5597 my_block_end |
|
5598 apply (unfold this) |
|
5599 my_block |
|
5600 fix i j |
|
5601 have "\<lbrace>st i \<and>* ps (v + 2) \<and>* zero (v + 3 + int w) \<and>* zeros (4 + (v + int w)) (2 + (v + int k)) \<and>* |
|
5602 one (v + 2) \<and>* ones (v + 3) (v + 2 + int w)\<rbrace> |
|
5603 i :[right_until_zero]: j |
|
5604 \<lbrace>st j \<and>* ps (v + 3 + int w) \<and>* zero (v + 3 + int w) \<and>* zeros (4 + (v + int w)) (2 + (v + int k)) \<and>* |
|
5605 one (v + 2) \<and>* ones (v + 3) (v + 2 + int w)\<rbrace>" |
|
5606 my_block |
|
5607 have "(one (v + 2) \<and>* ones (v + 3) (v + 2 + int w)) = |
|
5608 (ones (v + 2) (v + 2 + int w))" |
|
5609 by (simp add:ones_simps, smt) |
|
5610 my_block_end |
|
5611 apply (unfold this) |
|
5612 by hsteps |
|
5613 my_block_end |
|
5614 apply (hsteps this, simp only:sep_conj_ac) |
|
5615 apply (sep_cancel+, simp add:sep_conj_ac) |
|
5616 my_block |
|
5617 fix s |
|
5618 assume "(zero (v - int w) \<and>* zeros (v - int w + 1) (v + 1)) s" |
|
5619 hence "zeros (v - int w) (v + 1) s" |
|
5620 by (simp add:zeros_simps) |
|
5621 my_block_end |
|
5622 apply (fwd this) |
|
5623 my_block |
|
5624 fix s |
|
5625 assume "(one (v + 3 + int w) \<and>* ones (v + 3) (v + 2 + int w)) s" |
|
5626 hence "ones (v + 3) (3 + (v + int w)) s" |
|
5627 by (simp add:ones_rev sep_conj_ac, smt) |
|
5628 my_block_end |
|
5629 apply (fwd this) |
|
5630 by (simp add:sep_conj_ac, smt) |
|
5631 qed |
|
5632 |
|
5633 definition "cinit = (right_until_zero; move_right; write_one)" |
|
5634 |
|
5635 definition "copy = (cinit; cmove; move_right; move_right; right_until_one; move_left; move_left; cfill_until_one)" |
|
5636 |
|
5637 lemma hoare_copy: |
|
5638 shows |
|
5639 "\<lbrace>st i \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u v [k] \<and>* zero (v + 1) \<and>* |
|
5640 zeros (v + 2) (v + int(reps_len [k]) + 1)\<rbrace> |
|
5641 i :[copy]: j |
|
5642 \<lbrace>st j \<and>* ps u \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* reps u v [k] \<and>* zero (v + 1) \<and>* |
|
5643 reps (v + 2) (v + int (reps_len [k]) + 1) [k]\<rbrace>" |
|
5644 apply (unfold copy_def) |
|
5645 my_block |
|
5646 fix i j |
|
5647 have |
|
5648 "\<lbrace>st i \<and>* ps u \<and>* reps u v [k] \<and>* zero (v + 1) \<and>* zeros (v + 2) (v + int(reps_len [k]) + 1)\<rbrace> |
|
5649 i :[cinit]: j |
|
5650 \<lbrace>st j \<and>* ps (v + 2) \<and>* reps u v [k] \<and>* zero (v + 1) \<and>* |
|
5651 one (v + 2) \<and>* zeros (v + 3) (v + int(reps_len [k]) + 1)\<rbrace>" |
|
5652 apply (unfold cinit_def) |
|
5653 apply (simp add:reps.simps) |
|
5654 apply (subst sep_conj_cond)+ |
|
5655 apply (rule tm.pre_condI, simp) |
|
5656 apply hsteps |
|
5657 apply (simp add:sep_conj_ac) |
|
5658 my_block |
|
5659 have "(zeros (u + int k + 2) (u + int k + int (reps_len [k]) + 1)) = |
|
5660 (zero (u + int k + 2) \<and>* zeros (u + int k + 3) (u + int k + int (reps_len [k]) + 1))" |
|
5661 by (smt reps_len_sg zeros_step_simp) |
|
5662 my_block_end |
|
5663 apply (unfold this) |
|
5664 apply hstep |
|
5665 by (simp add:sep_conj_ac, sep_cancel+, smt) |
|
5666 my_block_end |
|
5667 apply (hstep this) |
|
5668 apply (rule_tac p = "st j' \<and>* ps (v + 2) \<and>* reps u v [k] \<and>* zero (v + 1) \<and>* |
|
5669 one (v + 2) \<and>* zeros (v + 3) (v + int (reps_len [k]) + 1) \<and>* zero (u - 2) \<and>* zero (u - 1) \<and>* |
|
5670 <(v = u + int (reps_len [k]) - 1)> |
|
5671 " in tm.pre_stren) |
|
5672 my_block |
|
5673 fix i j |
|
5674 from hoare_cmove[where w = 0 and k = k and i = i and j = j and v = v and u = u] |
|
5675 have "\<lbrace>st i \<and>* ps (v + 2) \<and>* zero (u - 1) \<and>* reps u v [k] \<and>* zero (v + 1) \<and>* |
|
5676 one (v + 2) \<and>* zeros (v + 3) (v + int(reps_len [k]) + 1)\<rbrace> |
|
5677 i :[cmove]: j |
|
5678 \<lbrace>st j \<and>* ps (u - 1) \<and>* zero (u - 1) \<and>* reps u u [0] \<and>* zeros (u + 1) (v + 1) \<and>* |
|
5679 reps (v + 2) (v + int (reps_len [k]) + 1) [k]\<rbrace>" |
|
5680 by (auto simp:ones_simps zeros_simps) |
|
5681 my_block_end |
|
5682 apply (hstep this) |
|
5683 apply (hstep, simp) |
|
5684 my_block |
|
5685 have "reps u u [0] = one u" by (simp add:reps.simps ones_simps) |
|
5686 my_block_end my_note eq_repsz = this |
|
5687 apply (unfold this) |
|
5688 apply (hstep) |
|
5689 apply (subst reps.simps, simp add: ones_simps) |
|
5690 apply hsteps |
|
5691 apply (subst sep_conj_cond)+ |
|
5692 apply (rule tm.pre_condI, simp del:zeros.simps zeros_simps) |
|
5693 apply (thin_tac "int (reps_len [k]) = 1 + int k \<and> v = u + int (reps_len [k]) - 1") |
|
5694 my_block |
|
5695 have "(zeros (u + 1) (u + int k + 1)) = (zeros (u + 1) (u + int k) \<and>* zero (u + int k + 1))" |
|
5696 by (simp only:zeros_rev, smt) |
|
5697 my_block_end |
|
5698 apply (unfold this) |
|
5699 apply (hstep, simp) |
|
5700 my_block |
|
5701 fix i j |
|
5702 from hoare_cfill_until_one[where v = "u + int k" and u = "u + 1"] |
|
5703 have "\<lbrace>st i \<and>* ps (u + int k) \<and>* one u \<and>* zeros (u + 1) (u + int k)\<rbrace> |
|
5704 i :[ cfill_until_one ]: j |
|
5705 \<lbrace>st j \<and>* ps u \<and>* ones u (u + int k) \<rbrace>" |
|
5706 by simp |
|
5707 my_block_end |
|
5708 apply (hstep this, simp add:sep_conj_ac reps.simps ones_simps) |
|
5709 apply (simp add:sep_conj_ac reps.simps ones_simps) |
|
5710 apply (subst sep_conj_cond)+ |
|
5711 apply (subst (asm) sep_conj_cond)+ |
|
5712 apply (rule condI) |
|
5713 apply (erule condE, simp) |
|
5714 apply (simp add: reps_len_def reps_sep_len_def reps_ctnt_len_def) |
|
5715 apply (sep_cancel+) |
|
5716 by (erule condE, simp) |
|
5717 |
|
5718 end |