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