587
|
1 |
|
|
2 |
theory Blexer
|
|
3 |
imports "Lexer"
|
|
4 |
begin
|
|
5 |
|
|
6 |
section \<open>Bit-Encodings\<close>
|
|
7 |
|
|
8 |
datatype bit = Z | S
|
|
9 |
|
|
10 |
fun code :: "val \<Rightarrow> bit list"
|
|
11 |
where
|
|
12 |
"code Void = []"
|
|
13 |
| "code (Char c) = []"
|
|
14 |
| "code (Left v) = Z # (code v)"
|
|
15 |
| "code (Right v) = S # (code v)"
|
|
16 |
| "code (Seq v1 v2) = (code v1) @ (code v2)"
|
|
17 |
| "code (Stars []) = [S]"
|
|
18 |
| "code (Stars (v # vs)) = (Z # code v) @ code (Stars vs)"
|
|
19 |
|
|
20 |
fun sz where
|
|
21 |
"sz ZERO = 0"
|
|
22 |
| "sz ONE = 0"
|
|
23 |
| "sz (CH _) = 0"
|
|
24 |
| "sz (SEQ r1 r2) = 1 + sz r1 + sz r2"
|
|
25 |
| "sz (ALT r1 r2) = 1 + sz r1 + sz r2"
|
|
26 |
| "sz (STAR r) = 1 + sz r"
|
|
27 |
| "sz (NTIMES r n) = 1 + n + sz r"
|
|
28 |
|
|
29 |
|
|
30 |
fun
|
|
31 |
Stars_add :: "val \<Rightarrow> val \<Rightarrow> val"
|
|
32 |
where
|
|
33 |
"Stars_add v (Stars vs) = Stars (v # vs)"
|
|
34 |
|
|
35 |
function (sequential)
|
|
36 |
decode' :: "bit list \<Rightarrow> rexp \<Rightarrow> (val * bit list)"
|
|
37 |
where
|
|
38 |
"decode' bs ZERO = (undefined, bs)"
|
|
39 |
| "decode' bs ONE = (Void, bs)"
|
|
40 |
| "decode' bs (CH d) = (Char d, bs)"
|
|
41 |
| "decode' [] (ALT r1 r2) = (Void, [])"
|
|
42 |
| "decode' (Z # bs) (ALT r1 r2) = (let (v, bs') = decode' bs r1 in (Left v, bs'))"
|
|
43 |
| "decode' (S # bs) (ALT r1 r2) = (let (v, bs') = decode' bs r2 in (Right v, bs'))"
|
|
44 |
| "decode' bs (SEQ r1 r2) = (let (v1, bs') = decode' bs r1 in
|
|
45 |
let (v2, bs'') = decode' bs' r2 in (Seq v1 v2, bs''))"
|
|
46 |
| "decode' [] (STAR r) = (Void, [])"
|
|
47 |
| "decode' (S # bs) (STAR r) = (Stars [], bs)"
|
|
48 |
| "decode' (Z # bs) (STAR r) = (let (v, bs') = decode' bs r in
|
|
49 |
let (vs, bs'') = decode' bs' (STAR r)
|
|
50 |
in (Stars_add v vs, bs''))"
|
|
51 |
| "decode' [] (NTIMES r n) = (Void, [])"
|
|
52 |
| "decode' (S # bs) (NTIMES r n) = (Stars [], bs)"
|
|
53 |
(*| "decode' (Z # bs) (NTIMES r 0) = (undefined, bs)"*)
|
|
54 |
| "decode' (Z # bs) (NTIMES r n) = (let (v, bs') = decode' bs r in
|
|
55 |
let (vs, bs'') = decode' bs' (NTIMES r (n - 1))
|
|
56 |
in (Stars_add v vs, bs''))"
|
|
57 |
by pat_completeness auto
|
|
58 |
|
|
59 |
lemma decode'_smaller:
|
|
60 |
assumes "decode'_dom (bs, r)"
|
|
61 |
shows "length (snd (decode' bs r)) \<le> length bs"
|
|
62 |
using assms
|
|
63 |
apply(induct bs r)
|
|
64 |
apply(auto simp add: decode'.psimps split: prod.split)
|
|
65 |
using dual_order.trans apply blast
|
|
66 |
apply (meson dual_order.trans le_SucI)
|
|
67 |
apply (meson le_SucI le_trans)
|
|
68 |
done
|
|
69 |
|
|
70 |
termination "decode'"
|
|
71 |
apply(relation "inv_image (measure(%cs. sz cs) <*lex*> measure(%s. size s)) (%(ds,r). (r,ds))")
|
|
72 |
apply(auto dest!: decode'_smaller)
|
|
73 |
apply (metis less_Suc_eq_le snd_conv)
|
|
74 |
by (metis less_Suc_eq_le snd_conv)
|
|
75 |
|
|
76 |
definition
|
|
77 |
decode :: "bit list \<Rightarrow> rexp \<Rightarrow> val option"
|
|
78 |
where
|
|
79 |
"decode ds r \<equiv> (let (v, ds') = decode' ds r
|
|
80 |
in (if ds' = [] then Some v else None))"
|
|
81 |
|
|
82 |
lemma decode'_code_Stars:
|
|
83 |
assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> (\<forall>x. decode' (code v @ x) r = (v, x)) \<and> flat v \<noteq> []"
|
|
84 |
shows "decode' (code (Stars vs) @ ds) (STAR r) = (Stars vs, ds)"
|
|
85 |
using assms
|
|
86 |
apply(induct vs)
|
|
87 |
apply(auto)
|
|
88 |
done
|
|
89 |
|
|
90 |
lemma decode'_code_NTIMES:
|
|
91 |
assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> (\<forall>x. decode' (code v @ x) r = (v, x))"
|
|
92 |
shows "decode' (code (Stars vs) @ ds) (NTIMES r n) = (Stars vs, ds)"
|
|
93 |
using assms
|
|
94 |
apply(induct vs arbitrary: n r ds)
|
|
95 |
apply(auto)
|
|
96 |
done
|
|
97 |
|
|
98 |
|
|
99 |
lemma decode'_code:
|
|
100 |
assumes "\<Turnstile> v : r"
|
|
101 |
shows "decode' ((code v) @ ds) r = (v, ds)"
|
|
102 |
using assms
|
|
103 |
apply(induct v r arbitrary: ds)
|
|
104 |
apply(auto)
|
|
105 |
using decode'_code_Stars apply blast
|
|
106 |
by (metis Un_iff decode'_code_NTIMES set_append)
|
|
107 |
|
|
108 |
lemma decode_code:
|
|
109 |
assumes "\<Turnstile> v : r"
|
|
110 |
shows "decode (code v) r = Some v"
|
|
111 |
using assms unfolding decode_def
|
|
112 |
by (smt append_Nil2 decode'_code old.prod.case)
|
|
113 |
|
|
114 |
|
|
115 |
section {* Annotated Regular Expressions *}
|
|
116 |
|
|
117 |
datatype arexp =
|
|
118 |
AZERO
|
|
119 |
| AONE "bit list"
|
|
120 |
| ACHAR "bit list" char
|
|
121 |
| ASEQ "bit list" arexp arexp
|
|
122 |
| AALTs "bit list" "arexp list"
|
|
123 |
| ASTAR "bit list" arexp
|
|
124 |
| ANTIMES "bit list" arexp nat
|
|
125 |
|
|
126 |
abbreviation
|
|
127 |
"AALT bs r1 r2 \<equiv> AALTs bs [r1, r2]"
|
|
128 |
|
|
129 |
fun asize :: "arexp \<Rightarrow> nat" where
|
|
130 |
"asize AZERO = 1"
|
|
131 |
| "asize (AONE cs) = 1"
|
|
132 |
| "asize (ACHAR cs c) = 1"
|
|
133 |
| "asize (AALTs cs rs) = Suc (sum_list (map asize rs))"
|
|
134 |
| "asize (ASEQ cs r1 r2) = Suc (asize r1 + asize r2)"
|
|
135 |
| "asize (ASTAR cs r) = Suc (asize r)"
|
|
136 |
| "asize (ANTIMES cs r n) = Suc (asize r) + n"
|
|
137 |
|
|
138 |
fun
|
|
139 |
erase :: "arexp \<Rightarrow> rexp"
|
|
140 |
where
|
|
141 |
"erase AZERO = ZERO"
|
|
142 |
| "erase (AONE _) = ONE"
|
|
143 |
| "erase (ACHAR _ c) = CH c"
|
|
144 |
| "erase (AALTs _ []) = ZERO"
|
|
145 |
| "erase (AALTs _ [r]) = (erase r)"
|
|
146 |
| "erase (AALTs bs (r#rs)) = ALT (erase r) (erase (AALTs bs rs))"
|
|
147 |
| "erase (ASEQ _ r1 r2) = SEQ (erase r1) (erase r2)"
|
|
148 |
| "erase (ASTAR _ r) = STAR (erase r)"
|
|
149 |
| "erase (ANTIMES _ r n) = NTIMES (erase r) n"
|
|
150 |
|
|
151 |
|
|
152 |
fun fuse :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp" where
|
|
153 |
"fuse bs AZERO = AZERO"
|
|
154 |
| "fuse bs (AONE cs) = AONE (bs @ cs)"
|
|
155 |
| "fuse bs (ACHAR cs c) = ACHAR (bs @ cs) c"
|
|
156 |
| "fuse bs (AALTs cs rs) = AALTs (bs @ cs) rs"
|
|
157 |
| "fuse bs (ASEQ cs r1 r2) = ASEQ (bs @ cs) r1 r2"
|
|
158 |
| "fuse bs (ASTAR cs r) = ASTAR (bs @ cs) r"
|
|
159 |
| "fuse bs (ANTIMES cs r n) = ANTIMES (bs @ cs) r n"
|
|
160 |
|
|
161 |
lemma fuse_append:
|
|
162 |
shows "fuse (bs1 @ bs2) r = fuse bs1 (fuse bs2 r)"
|
|
163 |
apply(induct r)
|
|
164 |
apply(auto)
|
|
165 |
done
|
|
166 |
|
|
167 |
|
|
168 |
fun intern :: "rexp \<Rightarrow> arexp" where
|
|
169 |
"intern ZERO = AZERO"
|
|
170 |
| "intern ONE = AONE []"
|
|
171 |
| "intern (CH c) = ACHAR [] c"
|
|
172 |
| "intern (ALT r1 r2) = AALT [] (fuse [Z] (intern r1))
|
|
173 |
(fuse [S] (intern r2))"
|
|
174 |
| "intern (SEQ r1 r2) = ASEQ [] (intern r1) (intern r2)"
|
|
175 |
| "intern (STAR r) = ASTAR [] (intern r)"
|
|
176 |
| "intern (NTIMES r n) = ANTIMES [] (intern r) n"
|
|
177 |
|
|
178 |
|
|
179 |
fun retrieve :: "arexp \<Rightarrow> val \<Rightarrow> bit list" where
|
|
180 |
"retrieve (AONE bs) Void = bs"
|
|
181 |
| "retrieve (ACHAR bs c) (Char d) = bs"
|
|
182 |
| "retrieve (AALTs bs [r]) v = bs @ retrieve r v"
|
|
183 |
| "retrieve (AALTs bs (r#rs)) (Left v) = bs @ retrieve r v"
|
|
184 |
| "retrieve (AALTs bs (r#rs)) (Right v) = bs @ retrieve (AALTs [] rs) v"
|
|
185 |
| "retrieve (ASEQ bs r1 r2) (Seq v1 v2) = bs @ retrieve r1 v1 @ retrieve r2 v2"
|
|
186 |
| "retrieve (ASTAR bs r) (Stars []) = bs @ [S]"
|
|
187 |
| "retrieve (ASTAR bs r) (Stars (v#vs)) =
|
|
188 |
bs @ [Z] @ retrieve r v @ retrieve (ASTAR [] r) (Stars vs)"
|
|
189 |
| "retrieve (ANTIMES bs r 0) (Stars []) = bs @ [S]"
|
|
190 |
| "retrieve (ANTIMES bs r (Suc n)) (Stars (v#vs)) =
|
|
191 |
bs @ [Z] @ retrieve r v @ retrieve (ANTIMES [] r n) (Stars vs)"
|
|
192 |
|
|
193 |
|
|
194 |
fun
|
|
195 |
bnullable :: "arexp \<Rightarrow> bool"
|
|
196 |
where
|
|
197 |
"bnullable (AZERO) = False"
|
|
198 |
| "bnullable (AONE bs) = True"
|
|
199 |
| "bnullable (ACHAR bs c) = False"
|
|
200 |
| "bnullable (AALTs bs rs) = (\<exists>r \<in> set rs. bnullable r)"
|
|
201 |
| "bnullable (ASEQ bs r1 r2) = (bnullable r1 \<and> bnullable r2)"
|
|
202 |
| "bnullable (ASTAR bs r) = True"
|
|
203 |
| "bnullable (ANTIMES bs r n) = (if n = 0 then True else bnullable r)"
|
|
204 |
|
|
205 |
abbreviation
|
|
206 |
bnullables :: "arexp list \<Rightarrow> bool"
|
|
207 |
where
|
|
208 |
"bnullables rs \<equiv> (\<exists>r \<in> set rs. bnullable r)"
|
|
209 |
|
|
210 |
function (sequential)
|
|
211 |
bmkeps :: "arexp \<Rightarrow> bit list"
|
|
212 |
where
|
|
213 |
"bmkeps(AONE bs) = bs"
|
|
214 |
| "bmkeps(ASEQ bs r1 r2) = bs @ (bmkeps r1) @ (bmkeps r2)"
|
|
215 |
| "bmkeps(AALTs bs (r#rs)) =
|
|
216 |
(if bnullable(r) then (bs @ bmkeps r) else (bmkeps (AALTs bs rs)))"
|
|
217 |
| "bmkeps(ASTAR bs r) = bs @ [S]"
|
|
218 |
| "bmkeps(ANTIMES bs r 0) = bs @ [S]"
|
|
219 |
| "bmkeps(ANTIMES bs r (Suc n)) = bs @ [Z] @ (bmkeps r) @ bmkeps(ANTIMES [] r n)"
|
|
220 |
apply(pat_completeness)
|
|
221 |
apply(auto)
|
|
222 |
done
|
|
223 |
|
|
224 |
termination "bmkeps"
|
|
225 |
apply(relation "measure asize")
|
|
226 |
apply(auto)
|
|
227 |
using asize.elims by force
|
|
228 |
|
|
229 |
fun
|
|
230 |
bmkepss :: "arexp list \<Rightarrow> bit list"
|
|
231 |
where
|
|
232 |
"bmkepss (r # rs) = (if bnullable(r) then (bmkeps r) else (bmkepss rs))"
|
|
233 |
|
|
234 |
|
|
235 |
lemma bmkepss1:
|
|
236 |
assumes "\<not> bnullables rs1"
|
|
237 |
shows "bmkepss (rs1 @ rs2) = bmkepss rs2"
|
|
238 |
using assms
|
|
239 |
by(induct rs1) (auto)
|
|
240 |
|
|
241 |
|
|
242 |
lemma bmkepss2:
|
|
243 |
assumes "bnullables rs1"
|
|
244 |
shows "bmkepss (rs1 @ rs2) = bmkepss rs1"
|
|
245 |
using assms
|
|
246 |
by (induct rs1) (auto)
|
|
247 |
|
|
248 |
|
|
249 |
fun
|
|
250 |
bder :: "char \<Rightarrow> arexp \<Rightarrow> arexp"
|
|
251 |
where
|
|
252 |
"bder c (AZERO) = AZERO"
|
|
253 |
| "bder c (AONE bs) = AZERO"
|
|
254 |
| "bder c (ACHAR bs d) = (if c = d then AONE bs else AZERO)"
|
|
255 |
| "bder c (AALTs bs rs) = AALTs bs (map (bder c) rs)"
|
|
256 |
| "bder c (ASEQ bs r1 r2) =
|
|
257 |
(if bnullable r1
|
|
258 |
then AALT bs (ASEQ [] (bder c r1) r2) (fuse (bmkeps r1) (bder c r2))
|
|
259 |
else ASEQ bs (bder c r1) r2)"
|
|
260 |
| "bder c (ASTAR bs r) = ASEQ (bs @ [Z]) (bder c r) (ASTAR [] r)"
|
|
261 |
| "bder c (ANTIMES bs r n) = (if n = 0 then AZERO else ASEQ (bs @ [Z]) (bder c r) (ANTIMES [] r (n - 1)))"
|
|
262 |
|
|
263 |
fun
|
|
264 |
bders :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
|
|
265 |
where
|
|
266 |
"bders r [] = r"
|
|
267 |
| "bders r (c#s) = bders (bder c r) s"
|
|
268 |
|
|
269 |
lemma bders_append:
|
|
270 |
"bders c (s1 @ s2) = bders (bders c s1) s2"
|
|
271 |
apply(induct s1 arbitrary: c s2)
|
|
272 |
apply(simp_all)
|
|
273 |
done
|
|
274 |
|
|
275 |
lemma bnullable_correctness:
|
|
276 |
shows "nullable (erase r) = bnullable r"
|
|
277 |
apply(induct r rule: erase.induct)
|
|
278 |
apply(simp_all)
|
|
279 |
done
|
|
280 |
|
|
281 |
lemma erase_fuse:
|
|
282 |
shows "erase (fuse bs r) = erase r"
|
|
283 |
apply(induct r rule: erase.induct)
|
|
284 |
apply(simp_all)
|
|
285 |
done
|
|
286 |
|
|
287 |
lemma erase_intern [simp]:
|
|
288 |
shows "erase (intern r) = r"
|
|
289 |
apply(induct r)
|
|
290 |
apply(simp_all add: erase_fuse)
|
|
291 |
done
|
|
292 |
|
|
293 |
lemma erase_bder [simp]:
|
|
294 |
shows "erase (bder a r) = der a (erase r)"
|
|
295 |
apply(induct r rule: erase.induct)
|
|
296 |
apply(simp_all add: erase_fuse bnullable_correctness)
|
|
297 |
done
|
|
298 |
|
|
299 |
lemma erase_bders [simp]:
|
|
300 |
shows "erase (bders r s) = ders s (erase r)"
|
|
301 |
apply(induct s arbitrary: r )
|
|
302 |
apply(simp_all)
|
|
303 |
done
|
|
304 |
|
|
305 |
lemma bnullable_fuse:
|
|
306 |
shows "bnullable (fuse bs r) = bnullable r"
|
|
307 |
apply(induct r arbitrary: bs)
|
|
308 |
apply(auto)
|
|
309 |
done
|
|
310 |
|
|
311 |
lemma retrieve_encode_STARS:
|
|
312 |
assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> code v = retrieve (intern r) v"
|
|
313 |
shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)"
|
|
314 |
using assms
|
|
315 |
apply(induct vs)
|
|
316 |
apply(simp_all)
|
|
317 |
done
|
|
318 |
|
|
319 |
lemma retrieve_encode_NTIMES:
|
|
320 |
assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> code v = retrieve (intern r) v" "length vs = n"
|
|
321 |
shows "code (Stars vs) = retrieve (ANTIMES [] (intern r) n) (Stars vs)"
|
|
322 |
using assms
|
|
323 |
apply(induct vs arbitrary: n)
|
|
324 |
apply(simp_all)
|
|
325 |
by force
|
|
326 |
|
|
327 |
|
|
328 |
lemma retrieve_fuse2:
|
|
329 |
assumes "\<Turnstile> v : (erase r)"
|
|
330 |
shows "retrieve (fuse bs r) v = bs @ retrieve r v"
|
|
331 |
using assms
|
|
332 |
apply(induct r arbitrary: v bs)
|
|
333 |
apply(auto elim: Prf_elims)[4]
|
|
334 |
apply(case_tac x2a)
|
|
335 |
apply(simp)
|
|
336 |
using Prf_elims(1) apply blast
|
|
337 |
apply(case_tac x2a)
|
|
338 |
apply(simp)
|
|
339 |
apply(simp)
|
|
340 |
apply(case_tac list)
|
|
341 |
apply(simp)
|
|
342 |
apply(simp)
|
|
343 |
apply (smt (verit, best) Prf_elims(3) append_assoc retrieve.simps(4) retrieve.simps(5))
|
|
344 |
apply(simp)
|
|
345 |
using retrieve_encode_STARS
|
|
346 |
apply(auto elim!: Prf_elims)[1]
|
|
347 |
apply(case_tac vs)
|
|
348 |
apply(simp)
|
|
349 |
apply(simp)
|
|
350 |
(* NTIMES *)
|
|
351 |
apply(auto elim!: Prf_elims)[1]
|
|
352 |
apply(case_tac vs1)
|
|
353 |
apply(simp_all)
|
|
354 |
apply(case_tac vs2)
|
|
355 |
apply(simp_all)
|
|
356 |
done
|
|
357 |
|
|
358 |
lemma retrieve_fuse:
|
|
359 |
assumes "\<Turnstile> v : r"
|
|
360 |
shows "retrieve (fuse bs (intern r)) v = bs @ retrieve (intern r) v"
|
|
361 |
using assms
|
|
362 |
by (simp_all add: retrieve_fuse2)
|
|
363 |
|
|
364 |
|
|
365 |
lemma retrieve_code:
|
|
366 |
assumes "\<Turnstile> v : r"
|
|
367 |
shows "code v = retrieve (intern r) v"
|
|
368 |
using assms
|
|
369 |
apply(induct v r )
|
|
370 |
apply(simp_all add: retrieve_fuse retrieve_encode_STARS)
|
|
371 |
apply(subst retrieve_encode_NTIMES)
|
|
372 |
apply(auto)
|
|
373 |
done
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
lemma retrieve_AALTs_bnullable1:
|
|
378 |
assumes "bnullable r"
|
|
379 |
shows "retrieve (AALTs bs (r # rs)) (mkeps (erase (AALTs bs (r # rs))))
|
|
380 |
= bs @ retrieve r (mkeps (erase r))"
|
|
381 |
using assms
|
|
382 |
apply(case_tac rs)
|
|
383 |
apply(auto simp add: bnullable_correctness)
|
|
384 |
done
|
|
385 |
|
|
386 |
lemma retrieve_AALTs_bnullable2:
|
|
387 |
assumes "\<not>bnullable r" "bnullables rs"
|
|
388 |
shows "retrieve (AALTs bs (r # rs)) (mkeps (erase (AALTs bs (r # rs))))
|
|
389 |
= retrieve (AALTs bs rs) (mkeps (erase (AALTs bs rs)))"
|
|
390 |
using assms
|
|
391 |
apply(induct rs arbitrary: r bs)
|
|
392 |
apply(auto)
|
|
393 |
using bnullable_correctness apply blast
|
|
394 |
apply(case_tac rs)
|
|
395 |
apply(auto)
|
|
396 |
using bnullable_correctness apply blast
|
|
397 |
apply(case_tac rs)
|
|
398 |
apply(auto)
|
|
399 |
done
|
|
400 |
|
|
401 |
lemma bmkeps_retrieve_AALTs:
|
|
402 |
assumes "\<forall>r \<in> set rs. bnullable r \<longrightarrow> bmkeps r = retrieve r (mkeps (erase r))"
|
|
403 |
"bnullables rs"
|
|
404 |
shows "bs @ bmkepss rs = retrieve (AALTs bs rs) (mkeps (erase (AALTs bs rs)))"
|
|
405 |
using assms
|
|
406 |
apply(induct rs arbitrary: bs)
|
|
407 |
apply(auto)
|
|
408 |
using retrieve_AALTs_bnullable1 apply presburger
|
|
409 |
apply (metis retrieve_AALTs_bnullable2)
|
|
410 |
apply (simp add: retrieve_AALTs_bnullable1)
|
|
411 |
by (metis retrieve_AALTs_bnullable2)
|
|
412 |
|
|
413 |
lemma bmkeps_retrieve_ANTIMES:
|
|
414 |
assumes "if n = 0 then True else bmkeps r = retrieve r (mkeps (erase r))"
|
|
415 |
and "bnullable (ANTIMES bs r n)"
|
|
416 |
shows "bmkeps (ANTIMES bs r n) = retrieve (ANTIMES bs r n) (Stars (replicate n (mkeps (erase r))))"
|
|
417 |
using assms
|
|
418 |
apply(induct n arbitrary: r bs)
|
|
419 |
apply(auto)[1]
|
|
420 |
apply(simp)
|
|
421 |
done
|
|
422 |
|
|
423 |
lemma bmkeps_retrieve:
|
|
424 |
assumes "bnullable r"
|
|
425 |
shows "bmkeps r = retrieve r (mkeps (erase r))"
|
|
426 |
using assms
|
|
427 |
apply(induct r rule: bmkeps.induct)
|
|
428 |
apply(auto)
|
|
429 |
apply (simp add: retrieve_AALTs_bnullable1)
|
|
430 |
using retrieve_AALTs_bnullable1 apply force
|
|
431 |
by (metis retrieve_AALTs_bnullable2)
|
|
432 |
|
|
433 |
|
|
434 |
lemma bder_retrieve:
|
|
435 |
assumes "\<Turnstile> v : der c (erase r)"
|
|
436 |
shows "retrieve (bder c r) v = retrieve r (injval (erase r) c v)"
|
|
437 |
using assms
|
|
438 |
apply(induct r arbitrary: v rule: erase.induct)
|
|
439 |
using Prf_elims(1) apply auto[1]
|
|
440 |
using Prf_elims(1) apply auto[1]
|
|
441 |
apply(auto)[1]
|
|
442 |
apply (metis Prf_elims(4) injval.simps(1) retrieve.simps(1) retrieve.simps(2))
|
|
443 |
using Prf_elims(1) apply blast
|
|
444 |
(* AALTs case *)
|
|
445 |
apply(simp)
|
|
446 |
apply(erule Prf_elims)
|
|
447 |
apply(simp)
|
|
448 |
apply(simp)
|
|
449 |
apply(rename_tac "r\<^sub>1" "r\<^sub>2" rs v)
|
|
450 |
apply(erule Prf_elims)
|
|
451 |
apply(simp)
|
|
452 |
apply(simp)
|
|
453 |
apply(case_tac rs)
|
|
454 |
apply(simp)
|
|
455 |
apply(simp)
|
|
456 |
using Prf_elims(3) apply fastforce
|
|
457 |
(* ASEQ case *)
|
|
458 |
apply(simp)
|
|
459 |
apply(case_tac "nullable (erase r1)")
|
|
460 |
apply(simp)
|
|
461 |
apply(erule Prf_elims)
|
|
462 |
using Prf_elims(2) bnullable_correctness apply force
|
|
463 |
apply (simp add: bmkeps_retrieve bnullable_correctness retrieve_fuse2)
|
|
464 |
apply (simp add: bmkeps_retrieve bnullable_correctness retrieve_fuse2)
|
|
465 |
using Prf_elims(2) apply force
|
|
466 |
(* ASTAR case *)
|
|
467 |
apply(rename_tac bs r v)
|
|
468 |
apply(simp)
|
|
469 |
apply(erule Prf_elims)
|
|
470 |
apply(clarify)
|
|
471 |
apply(erule Prf_elims)
|
|
472 |
apply(clarify)
|
|
473 |
apply (simp add: retrieve_fuse2)
|
|
474 |
(* ANTIMES case *)
|
|
475 |
apply(auto)
|
|
476 |
apply(erule Prf_elims)
|
|
477 |
apply(erule Prf_elims)
|
|
478 |
apply(clarify)
|
|
479 |
apply(erule Prf_elims)
|
|
480 |
apply(clarify)
|
|
481 |
by (metis (full_types) Suc_pred append_assoc injval.simps(8) retrieve.simps(10) retrieve.simps(6))
|
|
482 |
|
|
483 |
|
|
484 |
lemma MAIN_decode:
|
|
485 |
assumes "\<Turnstile> v : ders s r"
|
|
486 |
shows "Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r"
|
|
487 |
using assms
|
|
488 |
proof (induct s arbitrary: v rule: rev_induct)
|
|
489 |
case Nil
|
|
490 |
have "\<Turnstile> v : ders [] r" by fact
|
|
491 |
then have "\<Turnstile> v : r" by simp
|
|
492 |
then have "Some v = decode (retrieve (intern r) v) r"
|
|
493 |
using decode_code retrieve_code by auto
|
|
494 |
then show "Some (flex r id [] v) = decode (retrieve (bders (intern r) []) v) r"
|
|
495 |
by simp
|
|
496 |
next
|
|
497 |
case (snoc c s v)
|
|
498 |
have IH: "\<And>v. \<Turnstile> v : ders s r \<Longrightarrow>
|
|
499 |
Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r" by fact
|
|
500 |
have asm: "\<Turnstile> v : ders (s @ [c]) r" by fact
|
|
501 |
then have asm2: "\<Turnstile> injval (ders s r) c v : ders s r"
|
|
502 |
by (simp add: Prf_injval ders_append)
|
|
503 |
have "Some (flex r id (s @ [c]) v) = Some (flex r id s (injval (ders s r) c v))"
|
|
504 |
by (simp add: flex_append)
|
|
505 |
also have "... = decode (retrieve (bders (intern r) s) (injval (ders s r) c v)) r"
|
|
506 |
using asm2 IH by simp
|
|
507 |
also have "... = decode (retrieve (bder c (bders (intern r) s)) v) r"
|
|
508 |
using asm by (simp_all add: bder_retrieve ders_append)
|
|
509 |
finally show "Some (flex r id (s @ [c]) v) =
|
|
510 |
decode (retrieve (bders (intern r) (s @ [c])) v) r" by (simp add: bders_append)
|
|
511 |
qed
|
|
512 |
|
|
513 |
definition blexer where
|
|
514 |
"blexer r s \<equiv> if bnullable (bders (intern r) s) then
|
|
515 |
decode (bmkeps (bders (intern r) s)) r else None"
|
|
516 |
|
|
517 |
lemma blexer_correctness:
|
|
518 |
shows "blexer r s = lexer r s"
|
|
519 |
proof -
|
|
520 |
{ define bds where "bds \<equiv> bders (intern r) s"
|
|
521 |
define ds where "ds \<equiv> ders s r"
|
|
522 |
assume asm: "nullable ds"
|
|
523 |
have era: "erase bds = ds"
|
|
524 |
unfolding ds_def bds_def by simp
|
|
525 |
have mke: "\<Turnstile> mkeps ds : ds"
|
|
526 |
using asm by (simp add: mkeps_nullable)
|
|
527 |
have "decode (bmkeps bds) r = decode (retrieve bds (mkeps ds)) r"
|
|
528 |
using bmkeps_retrieve
|
|
529 |
using asm era
|
|
530 |
using bnullable_correctness by force
|
|
531 |
also have "... = Some (flex r id s (mkeps ds))"
|
|
532 |
using mke by (simp_all add: MAIN_decode ds_def bds_def)
|
|
533 |
finally have "decode (bmkeps bds) r = Some (flex r id s (mkeps ds))"
|
|
534 |
unfolding bds_def ds_def .
|
|
535 |
}
|
|
536 |
then show "blexer r s = lexer r s"
|
|
537 |
unfolding blexer_def lexer_flex
|
|
538 |
by (auto simp add: bnullable_correctness[symmetric])
|
|
539 |
qed
|
|
540 |
|
|
541 |
|
|
542 |
unused_thms
|
|
543 |
|
|
544 |
end
|