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