|
1 |
|
2 theory BitCoded |
|
3 imports "Lexer" |
|
4 begin |
|
5 |
|
6 section {* Bit-Encodings *} |
|
7 |
|
8 datatype bit = Z | S |
|
9 |
|
10 fun |
|
11 code :: "val \<Rightarrow> bit list" |
|
12 where |
|
13 "code Void = []" |
|
14 | "code (Char c) = []" |
|
15 | "code (Left v) = Z # (code v)" |
|
16 | "code (Right v) = S # (code v)" |
|
17 | "code (Seq v1 v2) = (code v1) @ (code v2)" |
|
18 | "code (Stars []) = [S]" |
|
19 | "code (Stars (v # vs)) = (Z # code v) @ code (Stars vs)" |
|
20 |
|
21 |
|
22 fun |
|
23 Stars_add :: "val \<Rightarrow> val \<Rightarrow> val" |
|
24 where |
|
25 "Stars_add v (Stars vs) = Stars (v # vs)" |
|
26 |
|
27 function |
|
28 decode' :: "bit list \<Rightarrow> rexp \<Rightarrow> (val * bit list)" |
|
29 where |
|
30 "decode' ds ZERO = (Void, [])" |
|
31 | "decode' ds ONE = (Void, ds)" |
|
32 | "decode' ds (CHAR d) = (Char d, ds)" |
|
33 | "decode' [] (ALT r1 r2) = (Void, [])" |
|
34 | "decode' (Z # ds) (ALT r1 r2) = (let (v, ds') = decode' ds r1 in (Left v, ds'))" |
|
35 | "decode' (S # ds) (ALT r1 r2) = (let (v, ds') = decode' ds r2 in (Right v, ds'))" |
|
36 | "decode' ds (SEQ r1 r2) = (let (v1, ds') = decode' ds r1 in |
|
37 let (v2, ds'') = decode' ds' r2 in (Seq v1 v2, ds''))" |
|
38 | "decode' [] (STAR r) = (Void, [])" |
|
39 | "decode' (S # ds) (STAR r) = (Stars [], ds)" |
|
40 | "decode' (Z # ds) (STAR r) = (let (v, ds') = decode' ds r in |
|
41 let (vs, ds'') = decode' ds' (STAR r) |
|
42 in (Stars_add v vs, ds''))" |
|
43 by pat_completeness auto |
|
44 |
|
45 lemma decode'_smaller: |
|
46 assumes "decode'_dom (ds, r)" |
|
47 shows "length (snd (decode' ds r)) \<le> length ds" |
|
48 using assms |
|
49 apply(induct ds r) |
|
50 apply(auto simp add: decode'.psimps split: prod.split) |
|
51 using dual_order.trans apply blast |
|
52 by (meson dual_order.trans le_SucI) |
|
53 |
|
54 termination "decode'" |
|
55 apply(relation "inv_image (measure(%cs. size cs) <*lex*> measure(%s. size s)) (%(ds,r). (r,ds))") |
|
56 apply(auto dest!: decode'_smaller) |
|
57 by (metis less_Suc_eq_le snd_conv) |
|
58 |
|
59 definition |
|
60 decode :: "bit list \<Rightarrow> rexp \<Rightarrow> val option" |
|
61 where |
|
62 "decode ds r \<equiv> (let (v, ds') = decode' ds r |
|
63 in (if ds' = [] then Some v else None))" |
|
64 |
|
65 lemma decode'_code_Stars: |
|
66 assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> (\<forall>x. decode' (code v @ x) r = (v, x)) \<and> flat v \<noteq> []" |
|
67 shows "decode' (code (Stars vs) @ ds) (STAR r) = (Stars vs, ds)" |
|
68 using assms |
|
69 apply(induct vs) |
|
70 apply(auto) |
|
71 done |
|
72 |
|
73 lemma decode'_code: |
|
74 assumes "\<Turnstile> v : r" |
|
75 shows "decode' ((code v) @ ds) r = (v, ds)" |
|
76 using assms |
|
77 apply(induct v r arbitrary: ds) |
|
78 apply(auto) |
|
79 using decode'_code_Stars by blast |
|
80 |
|
81 lemma decode_code: |
|
82 assumes "\<Turnstile> v : r" |
|
83 shows "decode (code v) r = Some v" |
|
84 using assms unfolding decode_def |
|
85 by (smt append_Nil2 decode'_code old.prod.case) |
|
86 |
|
87 |
|
88 section {* Annotated Regular Expressions *} |
|
89 |
|
90 datatype arexp = |
|
91 AZERO |
|
92 | AONE "bit list" |
|
93 | ACHAR "bit list" char |
|
94 | ASEQ "bit list" arexp arexp |
|
95 | AALTs "bit list" "arexp list" |
|
96 | ASTAR "bit list" arexp |
|
97 |
|
98 abbreviation |
|
99 "AALT bs r1 r2 \<equiv> AALTs bs [r1, r2]" |
|
100 |
|
101 |
|
102 fun fuse :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp" where |
|
103 "fuse bs AZERO = AZERO" |
|
104 | "fuse bs (AONE cs) = AONE (bs @ cs)" |
|
105 | "fuse bs (ACHAR cs c) = ACHAR (bs @ cs) c" |
|
106 | "fuse bs (AALTs cs rs) = AALTs (bs @ cs) rs" |
|
107 | "fuse bs (ASEQ cs r1 r2) = ASEQ (bs @ cs) r1 r2" |
|
108 | "fuse bs (ASTAR cs r) = ASTAR (bs @ cs) r" |
|
109 |
|
110 fun intern :: "rexp \<Rightarrow> arexp" where |
|
111 "intern ZERO = AZERO" |
|
112 | "intern ONE = AONE []" |
|
113 | "intern (CHAR c) = ACHAR [] c" |
|
114 | "intern (ALT r1 r2) = AALT [] (fuse [Z] (intern r1)) |
|
115 (fuse [S] (intern r2))" |
|
116 | "intern (SEQ r1 r2) = ASEQ [] (intern r1) (intern r2)" |
|
117 | "intern (STAR r) = ASTAR [] (intern r)" |
|
118 |
|
119 |
|
120 fun retrieve :: "arexp \<Rightarrow> val \<Rightarrow> bit list" where |
|
121 "retrieve (AONE bs) Void = bs" |
|
122 | "retrieve (ACHAR bs c) (Char d) = bs" |
|
123 | "retrieve (AALTs bs (r#rs)) (Left v) = bs @ retrieve r v" |
|
124 | "retrieve (AALTs bs (r#rs)) (Right v) = bs @ retrieve (AALTs [] rs) v" |
|
125 | "retrieve (ASEQ bs r1 r2) (Seq v1 v2) = bs @ retrieve r1 v1 @ retrieve r2 v2" |
|
126 | "retrieve (ASTAR bs r) (Stars []) = bs @ [S]" |
|
127 | "retrieve (ASTAR bs r) (Stars (v#vs)) = |
|
128 bs @ [Z] @ retrieve r v @ retrieve (ASTAR [] r) (Stars vs)" |
|
129 |
|
130 fun |
|
131 erase :: "arexp \<Rightarrow> rexp" |
|
132 where |
|
133 "erase AZERO = ZERO" |
|
134 | "erase (AONE _) = ONE" |
|
135 | "erase (ACHAR _ c) = CHAR c" |
|
136 | "erase (AALTs _ []) = ZERO" |
|
137 | "erase (AALTs _ [r]) = (erase r)" |
|
138 | "erase (AALTs _ (r#rs)) = ALT (erase r) (erase (AALTs [] rs))" |
|
139 | "erase (ASEQ _ r1 r2) = SEQ (erase r1) (erase r2)" |
|
140 | "erase (ASTAR _ r) = STAR (erase r)" |
|
141 |
|
142 fun |
|
143 bnullable :: "arexp \<Rightarrow> bool" |
|
144 where |
|
145 "bnullable (AZERO) = False" |
|
146 | "bnullable (AONE bs) = True" |
|
147 | "bnullable (ACHAR bs c) = False" |
|
148 | "bnullable (AALTs bs rs) = (\<exists>r \<in> set rs. bnullable r)" |
|
149 | "bnullable (ASEQ bs r1 r2) = (bnullable r1 \<and> bnullable r2)" |
|
150 | "bnullable (ASTAR bs r) = True" |
|
151 |
|
152 fun |
|
153 bmkeps :: "arexp \<Rightarrow> bit list" |
|
154 where |
|
155 "bmkeps(AONE bs) = bs" |
|
156 | "bmkeps(ASEQ bs r1 r2) = bs @ (bmkeps r1) @ (bmkeps r2)" |
|
157 | "bmkeps(AALTs bs (r#rs)) = (if bnullable(r) then bs @ (bmkeps r) else (bmkeps (AALTs bs rs)))" |
|
158 | "bmkeps(ASTAR bs r) = bs @ [S]" |
|
159 |
|
160 |
|
161 fun |
|
162 bder :: "char \<Rightarrow> arexp \<Rightarrow> arexp" |
|
163 where |
|
164 "bder c (AZERO) = AZERO" |
|
165 | "bder c (AONE bs) = AZERO" |
|
166 | "bder c (ACHAR bs d) = (if c = d then AONE bs else AZERO)" |
|
167 | "bder c (AALTs bs rs) = AALTs bs (map (bder c) rs)" |
|
168 | "bder c (ASEQ bs r1 r2) = |
|
169 (if bnullable r1 |
|
170 then AALT bs (ASEQ [] (bder c r1) r2) (fuse (bmkeps r1) (bder c r2)) |
|
171 else ASEQ bs (bder c r1) r2)" |
|
172 | "bder c (ASTAR bs r) = ASEQ bs (fuse [Z] (bder c r)) (ASTAR [] r)" |
|
173 |
|
174 |
|
175 fun |
|
176 bders :: "arexp \<Rightarrow> string \<Rightarrow> arexp" |
|
177 where |
|
178 "bders r [] = r" |
|
179 | "bders r (c#s) = bders (bder c r) s" |
|
180 |
|
181 lemma bders_append: |
|
182 "bders r (s1 @ s2) = bders (bders r s1) s2" |
|
183 apply(induct s1 arbitrary: r s2) |
|
184 apply(simp_all) |
|
185 done |
|
186 |
|
187 lemma bnullable_correctness: |
|
188 shows "nullable (erase r) = bnullable r" |
|
189 apply(induct r rule: erase.induct) |
|
190 apply(simp_all) |
|
191 done |
|
192 |
|
193 lemma erase_fuse: |
|
194 shows "erase (fuse bs r) = erase r" |
|
195 apply(induct r rule: erase.induct) |
|
196 apply(simp_all) |
|
197 done |
|
198 |
|
199 lemma erase_intern [simp]: |
|
200 shows "erase (intern r) = r" |
|
201 apply(induct r) |
|
202 apply(simp_all add: erase_fuse) |
|
203 done |
|
204 |
|
205 lemma erase_bder [simp]: |
|
206 shows "erase (bder a r) = der a (erase r)" |
|
207 apply(induct r rule: erase.induct) |
|
208 apply(simp_all add: erase_fuse bnullable_correctness) |
|
209 done |
|
210 |
|
211 lemma erase_bders [simp]: |
|
212 shows "erase (bders r s) = ders s (erase r)" |
|
213 apply(induct s arbitrary: r ) |
|
214 apply(simp_all) |
|
215 done |
|
216 |
|
217 lemma retrieve_encode_STARS: |
|
218 assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> code v = retrieve (intern r) v" |
|
219 shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)" |
|
220 using assms |
|
221 apply(induct vs) |
|
222 apply(simp_all) |
|
223 done |
|
224 |
|
225 lemma retrieve_fuse2: |
|
226 assumes "\<Turnstile> v : (erase r)" |
|
227 shows "retrieve (fuse bs r) v = bs @ retrieve r v" |
|
228 using assms |
|
229 apply(induct r arbitrary: v bs rule: erase.induct) |
|
230 apply(auto elim: Prf_elims)[1] |
|
231 sorry |
|
232 |
|
233 lemma retrieve_fuse: |
|
234 assumes "\<Turnstile> v : r" |
|
235 shows "retrieve (fuse bs (intern r)) v = bs @ retrieve (intern r) v" |
|
236 using assms |
|
237 by (simp_all add: retrieve_fuse2) |
|
238 |
|
239 |
|
240 lemma retrieve_code: |
|
241 assumes "\<Turnstile> v : r" |
|
242 shows "code v = retrieve (intern r) v" |
|
243 using assms |
|
244 apply(induct v r ) |
|
245 apply(simp_all add: retrieve_fuse retrieve_encode_STARS) |
|
246 sorry |
|
247 |
|
248 |
|
249 lemma bmkeps_retrieve: |
|
250 assumes "nullable (erase r)" |
|
251 shows "bmkeps r = retrieve r (mkeps (erase r))" |
|
252 using assms |
|
253 apply(induct r) |
|
254 apply(auto simp add: bnullable_correctness) |
|
255 sorry |
|
256 |
|
257 lemma bder_retrieve: |
|
258 assumes "\<Turnstile> v : der c (erase r)" |
|
259 shows "retrieve (bder c r) v = retrieve r (injval (erase r) c v)" |
|
260 using assms |
|
261 apply(induct r arbitrary: v) |
|
262 apply(auto elim!: Prf_elims simp add: retrieve_fuse2 bnullable_correctness bmkeps_retrieve) |
|
263 sorry |
|
264 |
|
265 lemma MAIN_decode: |
|
266 assumes "\<Turnstile> v : ders s r" |
|
267 shows "Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r" |
|
268 using assms |
|
269 proof (induct s arbitrary: v rule: rev_induct) |
|
270 case Nil |
|
271 have "\<Turnstile> v : ders [] r" by fact |
|
272 then have "\<Turnstile> v : r" by simp |
|
273 then have "Some v = decode (retrieve (intern r) v) r" |
|
274 using decode_code retrieve_code by auto |
|
275 then show "Some (flex r id [] v) = decode (retrieve (bders (intern r) []) v) r" |
|
276 by simp |
|
277 next |
|
278 case (snoc c s v) |
|
279 have IH: "\<And>v. \<Turnstile> v : ders s r \<Longrightarrow> |
|
280 Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r" by fact |
|
281 have asm: "\<Turnstile> v : ders (s @ [c]) r" by fact |
|
282 then have asm2: "\<Turnstile> injval (ders s r) c v : ders s r" |
|
283 by(simp add: Prf_injval ders_append) |
|
284 have "Some (flex r id (s @ [c]) v) = Some (flex r id s (injval (ders s r) c v))" |
|
285 by (simp add: flex_append) |
|
286 also have "... = decode (retrieve (bders (intern r) s) (injval (ders s r) c v)) r" |
|
287 using asm2 IH by simp |
|
288 also have "... = decode (retrieve (bder c (bders (intern r) s)) v) r" |
|
289 using asm by(simp_all add: bder_retrieve ders_append) |
|
290 finally show "Some (flex r id (s @ [c]) v) = |
|
291 decode (retrieve (bders (intern r) (s @ [c])) v) r" by (simp add: bders_append) |
|
292 qed |
|
293 |
|
294 |
|
295 definition blexer where |
|
296 "blexer r s \<equiv> if bnullable (bders (intern r) s) then |
|
297 decode (bmkeps (bders (intern r) s)) r else None" |
|
298 |
|
299 lemma blexer_correctness: |
|
300 shows "blexer r s = lexer r s" |
|
301 proof - |
|
302 { define bds where "bds \<equiv> bders (intern r) s" |
|
303 define ds where "ds \<equiv> ders s r" |
|
304 assume asm: "nullable ds" |
|
305 have era: "erase bds = ds" |
|
306 unfolding ds_def bds_def by simp |
|
307 have mke: "\<Turnstile> mkeps ds : ds" |
|
308 using asm by (simp add: mkeps_nullable) |
|
309 have "decode (bmkeps bds) r = decode (retrieve bds (mkeps ds)) r" |
|
310 using bmkeps_retrieve |
|
311 using asm era by (simp add: bmkeps_retrieve) |
|
312 also have "... = Some (flex r id s (mkeps ds))" |
|
313 using mke by (simp_all add: MAIN_decode ds_def bds_def) |
|
314 finally have "decode (bmkeps bds) r = Some (flex r id s (mkeps ds))" |
|
315 unfolding bds_def ds_def . |
|
316 } |
|
317 then show "blexer r s = lexer r s" |
|
318 unfolding blexer_def lexer_flex |
|
319 apply(subst bnullable_correctness[symmetric]) |
|
320 apply(simp) |
|
321 done |
|
322 qed |
|
323 |
|
324 |
|
325 |
|
326 end |