|
1 theory Derivs |
|
2 imports Closure |
|
3 begin |
|
4 |
|
5 section {* Experiments with Derivatives -- independent of Myhill-Nerode *} |
|
6 |
|
7 subsection {* Left-Quotients *} |
|
8 |
|
9 definition |
|
10 Delta :: "lang \<Rightarrow> lang" |
|
11 where |
|
12 "Delta A = (if [] \<in> A then {[]} else {})" |
|
13 |
|
14 definition |
|
15 Der :: "char \<Rightarrow> lang \<Rightarrow> lang" |
|
16 where |
|
17 "Der c A \<equiv> {s. [c] @ s \<in> A}" |
|
18 |
|
19 definition |
|
20 Ders :: "string \<Rightarrow> lang \<Rightarrow> lang" |
|
21 where |
|
22 "Ders s A \<equiv> {s'. s @ s' \<in> A}" |
|
23 |
|
24 definition |
|
25 Ders_set :: "lang \<Rightarrow> lang \<Rightarrow> lang" |
|
26 where |
|
27 "Ders_set A B \<equiv> {s' | s s'. s @ s' \<in> B \<and> s \<in> A}" |
|
28 |
|
29 lemma Ders_set_Ders: |
|
30 shows "Ders_set A B = (\<Union>s \<in> A. Ders s B)" |
|
31 unfolding Ders_set_def Ders_def |
|
32 by auto |
|
33 |
|
34 lemma Der_null [simp]: |
|
35 shows "Der c {} = {}" |
|
36 unfolding Der_def |
|
37 by auto |
|
38 |
|
39 lemma Der_empty [simp]: |
|
40 shows "Der c {[]} = {}" |
|
41 unfolding Der_def |
|
42 by auto |
|
43 |
|
44 lemma Der_char [simp]: |
|
45 shows "Der c {[d]} = (if c = d then {[]} else {})" |
|
46 unfolding Der_def |
|
47 by auto |
|
48 |
|
49 lemma Der_union [simp]: |
|
50 shows "Der c (A \<union> B) = Der c A \<union> Der c B" |
|
51 unfolding Der_def |
|
52 by auto |
|
53 |
|
54 lemma Der_seq [simp]: |
|
55 shows "Der c (A ;; B) = (Der c A) ;; B \<union> (Delta A ;; Der c B)" |
|
56 unfolding Der_def Delta_def |
|
57 unfolding Seq_def |
|
58 by (auto simp add: Cons_eq_append_conv) |
|
59 |
|
60 lemma Der_star [simp]: |
|
61 shows "Der c (A\<star>) = (Der c A) ;; A\<star>" |
|
62 apply(subst star_cases) |
|
63 apply(simp only: Delta_def Der_union Der_seq Der_empty) |
|
64 apply(simp add: Der_def Seq_def) |
|
65 apply(auto) |
|
66 apply(drule star_decom) |
|
67 apply(auto simp add: Cons_eq_append_conv) |
|
68 done |
|
69 |
|
70 lemma Ders_singleton: |
|
71 shows "Ders [c] A = Der c A" |
|
72 unfolding Der_def Ders_def |
|
73 by simp |
|
74 |
|
75 lemma Ders_append: |
|
76 shows "Ders (s1 @ s2) A = Ders s2 (Ders s1 A)" |
|
77 unfolding Ders_def by simp |
|
78 |
|
79 lemma MN_Rel_Ders: |
|
80 shows "x \<approx>A y \<longleftrightarrow> Ders x A = Ders y A" |
|
81 unfolding Ders_def str_eq_def str_eq_rel_def |
|
82 by auto |
|
83 |
|
84 subsection {* Brozowsky's derivatives of regular expressions *} |
|
85 |
|
86 fun |
|
87 nullable :: "rexp \<Rightarrow> bool" |
|
88 where |
|
89 "nullable (NULL) = False" |
|
90 | "nullable (EMPTY) = True" |
|
91 | "nullable (CHAR c) = False" |
|
92 | "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)" |
|
93 | "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)" |
|
94 | "nullable (STAR r) = True" |
|
95 |
|
96 fun |
|
97 der :: "char \<Rightarrow> rexp \<Rightarrow> rexp" |
|
98 where |
|
99 "der c (NULL) = NULL" |
|
100 | "der c (EMPTY) = NULL" |
|
101 | "der c (CHAR c') = (if c = c' then EMPTY else NULL)" |
|
102 | "der c (ALT r1 r2) = ALT (der c r1) (der c r2)" |
|
103 | "der c (SEQ r1 r2) = ALT (SEQ (der c r1) r2) (if nullable r1 then der c r2 else NULL)" |
|
104 | "der c (STAR r) = SEQ (der c r) (STAR r)" |
|
105 |
|
106 function |
|
107 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp" |
|
108 where |
|
109 "ders [] r = r" |
|
110 | "ders (s @ [c]) r = der c (ders s r)" |
|
111 by (auto) (metis rev_cases) |
|
112 |
|
113 termination |
|
114 by (relation "measure (length o fst)") (auto) |
|
115 |
|
116 lemma Delta_nullable: |
|
117 shows "Delta (L r) = (if nullable r then {[]} else {})" |
|
118 unfolding Delta_def |
|
119 by (induct r) (auto simp add: Seq_def split: if_splits) |
|
120 |
|
121 lemma Der_der: |
|
122 fixes r::rexp |
|
123 shows "Der c (L r) = L (der c r)" |
|
124 by (induct r) (simp_all add: Delta_nullable) |
|
125 |
|
126 lemma Ders_ders: |
|
127 fixes r::rexp |
|
128 shows "Ders s (L r) = L (ders s r)" |
|
129 apply(induct s rule: rev_induct) |
|
130 apply(simp add: Ders_def) |
|
131 apply(simp only: ders.simps) |
|
132 apply(simp only: Ders_append) |
|
133 apply(simp only: Ders_singleton) |
|
134 apply(simp only: Der_der) |
|
135 done |
|
136 |
|
137 |
|
138 subsection {* Antimirov's Partial Derivatives *} |
|
139 |
|
140 abbreviation |
|
141 "SEQS R r \<equiv> {SEQ r' r | r'. r' \<in> R}" |
|
142 |
|
143 fun |
|
144 pder :: "char \<Rightarrow> rexp \<Rightarrow> rexp set" |
|
145 where |
|
146 "pder c NULL = {NULL}" |
|
147 | "pder c EMPTY = {NULL}" |
|
148 | "pder c (CHAR c') = (if c = c' then {EMPTY} else {NULL})" |
|
149 | "pder c (ALT r1 r2) = (pder c r1) \<union> (pder c r2)" |
|
150 | "pder c (SEQ r1 r2) = SEQS (pder c r1) r2 \<union> (if nullable r1 then pder c r2 else {})" |
|
151 | "pder c (STAR r) = SEQS (pder c r) (STAR r)" |
|
152 |
|
153 abbreviation |
|
154 "pder_set c R \<equiv> \<Union>r \<in> R. pder c r" |
|
155 |
|
156 function |
|
157 pders :: "string \<Rightarrow> rexp \<Rightarrow> rexp set" |
|
158 where |
|
159 "pders [] r = {r}" |
|
160 | "pders (s @ [c]) r = pder_set c (pders s r)" |
|
161 by (auto) (metis rev_cases) |
|
162 |
|
163 termination |
|
164 by (relation "measure (length o fst)") (auto) |
|
165 |
|
166 abbreviation |
|
167 "pders_set A r \<equiv> \<Union>s \<in> A. pders s r" |
|
168 |
|
169 lemma pders_append: |
|
170 "pders (s1 @ s2) r = \<Union> (pders s2) ` (pders s1 r)" |
|
171 apply(induct s2 arbitrary: s1 r rule: rev_induct) |
|
172 apply(simp) |
|
173 apply(subst append_assoc[symmetric]) |
|
174 apply(simp only: pders.simps) |
|
175 apply(auto) |
|
176 done |
|
177 |
|
178 lemma pders_singleton: |
|
179 "pders [c] r = pder c r" |
|
180 apply(subst append_Nil[symmetric]) |
|
181 apply(simp only: pders.simps) |
|
182 apply(simp) |
|
183 done |
|
184 |
|
185 lemma pder_set_lang: |
|
186 shows "(\<Union> (L ` pder_set c R)) = (\<Union>r \<in> R. (\<Union>L ` (pder c r)))" |
|
187 unfolding image_def |
|
188 by auto |
|
189 |
|
190 lemma |
|
191 shows seq_UNION_left: "B ;; (\<Union>n\<in>C. A n) = (\<Union>n\<in>C. B ;; A n)" |
|
192 and seq_UNION_right: "(\<Union>n\<in>C. A n) ;; B = (\<Union>n\<in>C. A n ;; B)" |
|
193 unfolding Seq_def by auto |
|
194 |
|
195 lemma Der_pder: |
|
196 fixes r::rexp |
|
197 shows "Der c (L r) = \<Union> L ` (pder c r)" |
|
198 by (induct r) (auto simp add: Delta_nullable seq_UNION_right) |
|
199 |
|
200 lemma Ders_pders: |
|
201 fixes r::rexp |
|
202 shows "Ders s (L r) = \<Union> L ` (pders s r)" |
|
203 proof (induct s rule: rev_induct) |
|
204 case (snoc c s) |
|
205 have ih: "Ders s (L r) = \<Union> L ` (pders s r)" by fact |
|
206 have "Ders (s @ [c]) (L r) = Ders [c] (Ders s (L r))" |
|
207 by (simp add: Ders_append) |
|
208 also have "\<dots> = Der c (\<Union> L ` (pders s r))" using ih |
|
209 by (simp add: Ders_singleton) |
|
210 also have "\<dots> = (\<Union>r\<in>pders s r. Der c (L r))" |
|
211 unfolding Der_def image_def by auto |
|
212 also have "\<dots> = (\<Union>r\<in>pders s r. (\<Union> L ` (pder c r)))" |
|
213 by (simp add: Der_pder) |
|
214 also have "\<dots> = (\<Union>L ` (pder_set c (pders s r)))" |
|
215 by (simp add: pder_set_lang) |
|
216 also have "\<dots> = (\<Union>L ` (pders (s @ [c]) r))" |
|
217 by simp |
|
218 finally show "Ders (s @ [c]) (L r) = \<Union>L ` pders (s @ [c]) r" . |
|
219 qed (simp add: Ders_def) |
|
220 |
|
221 lemma Ders_set_pders_set: |
|
222 fixes r::rexp |
|
223 shows "Ders_set A (L r) = (\<Union> L ` (pders_set A r))" |
|
224 by (simp add: Ders_set_Ders Ders_pders) |
|
225 |
|
226 lemma pders_NULL [simp]: |
|
227 shows "pders s NULL = {NULL}" |
|
228 by (induct s rule: rev_induct) (simp_all) |
|
229 |
|
230 lemma pders_EMPTY [simp]: |
|
231 shows "pders s EMPTY = (if s = [] then {EMPTY} else {NULL})" |
|
232 by (induct s rule: rev_induct) (auto) |
|
233 |
|
234 lemma pders_CHAR [simp]: |
|
235 shows "pders s (CHAR c) = (if s = [] then {CHAR c} else (if s = [c] then {EMPTY} else {NULL}))" |
|
236 by (induct s rule: rev_induct) (auto) |
|
237 |
|
238 lemma pders_ALT [simp]: |
|
239 shows "pders s (ALT r1 r2) = (if s = [] then {ALT r1 r2} else (pders s r1) \<union> (pders s r2))" |
|
240 by (induct s rule: rev_induct) (auto) |
|
241 |
|
242 definition |
|
243 "Suf s \<equiv> {v. v \<noteq> [] \<and> (\<exists>u. u @ v = s)}" |
|
244 |
|
245 lemma Suf: |
|
246 shows "Suf (s @ [c]) = (Suf s) ;; {[c]} \<union> {[c]}" |
|
247 unfolding Suf_def Seq_def |
|
248 by (auto simp add: append_eq_append_conv2 append_eq_Cons_conv) |
|
249 |
|
250 lemma Suf_Union: |
|
251 shows "(\<Union>v \<in> Suf s ;; {[c]}. P v) = (\<Union>v \<in> Suf s. P (v @ [c]))" |
|
252 by (auto simp add: Seq_def) |
|
253 |
|
254 lemma inclusion1: |
|
255 shows "pder_set c (SEQS R r2) \<subseteq> SEQS (pder_set c R) r2 \<union> (pder c r2)" |
|
256 apply(auto simp add: if_splits) |
|
257 apply(blast) |
|
258 done |
|
259 |
|
260 lemma pders_SEQ: |
|
261 shows "pders s (SEQ r1 r2) \<subseteq> SEQS (pders s r1) r2 \<union> (\<Union>v \<in> Suf s. pders v r2)" |
|
262 proof (induct s rule: rev_induct) |
|
263 case (snoc c s) |
|
264 have ih: "pders s (SEQ r1 r2) \<subseteq> SEQS (pders s r1) r2 \<union> (\<Union>v \<in> Suf s. pders v r2)" |
|
265 by fact |
|
266 have "pders (s @ [c]) (SEQ r1 r2) = pder_set c (pders s (SEQ r1 r2))" by simp |
|
267 also have "\<dots> \<subseteq> pder_set c (SEQS (pders s r1) r2 \<union> (\<Union>v \<in> Suf s. pders v r2))" |
|
268 using ih by auto |
|
269 also have "\<dots> = pder_set c (SEQS (pders s r1) r2) \<union> pder_set c (\<Union>v \<in> Suf s. pders v r2)" |
|
270 by (simp) |
|
271 also have "\<dots> = pder_set c (SEQS (pders s r1) r2) \<union> (\<Union>v \<in> Suf s. pder_set c (pders v r2))" |
|
272 by (simp) |
|
273 also have "\<dots> \<subseteq> pder_set c (SEQS (pders s r1) r2) \<union> (pder c r2) \<union> (\<Union>v \<in> Suf s. pders (v @ [c]) r2)" |
|
274 by (auto) |
|
275 also have "\<dots> \<subseteq> SEQS (pder_set c (pders s r1)) r2 \<union> (pder c r2) \<union> (\<Union>v \<in> Suf s. pders (v @ [c]) r2)" |
|
276 using inclusion1 by blast |
|
277 also have "\<dots> = SEQS (pders (s @ [c]) r1) r2 \<union> (\<Union>v \<in> Suf (s @ [c]). pders v r2)" |
|
278 apply(subst (2) pders.simps) |
|
279 apply(simp only: Suf) |
|
280 apply(simp add: Suf_Union pders_singleton) |
|
281 apply(auto) |
|
282 done |
|
283 finally show ?case . |
|
284 qed (simp) |
|
285 |
|
286 lemma pders_STAR: |
|
287 assumes a: "s \<noteq> []" |
|
288 shows "pders s (STAR r) \<subseteq> (\<Union>v \<in> Suf s. SEQS (pders v r) (STAR r))" |
|
289 using a |
|
290 proof (induct s rule: rev_induct) |
|
291 case (snoc c s) |
|
292 have ih: "s \<noteq> [] \<Longrightarrow> pders s (STAR r) \<subseteq> (\<Union>v\<in>Suf s. SEQS (pders v r) (STAR r))" by fact |
|
293 { assume asm: "s \<noteq> []" |
|
294 have "pders (s @ [c]) (STAR r) = pder_set c (pders s (STAR r))" by simp |
|
295 also have "\<dots> \<subseteq> (pder_set c (\<Union>v\<in>Suf s. SEQS (pders v r) (STAR r)))" |
|
296 using ih[OF asm] by blast |
|
297 also have "\<dots> = (\<Union>v\<in>Suf s. pder_set c (SEQS (pders v r) (STAR r)))" |
|
298 by simp |
|
299 also have "\<dots> \<subseteq> (\<Union>v\<in>Suf s. (SEQS (pder_set c (pders v r)) (STAR r) \<union> pder c (STAR r)))" |
|
300 using inclusion1 by blast |
|
301 also have "\<dots> = (\<Union>v\<in>Suf s. (SEQS (pder_set c (pders v r)) (STAR r))) \<union> pder c (STAR r)" |
|
302 using asm by (auto simp add: Suf_def) |
|
303 also have "\<dots> = (\<Union>v\<in>Suf s. (SEQS (pders (v @ [c]) r) (STAR r))) \<union> (SEQS (pder c r) (STAR r))" |
|
304 by simp |
|
305 also have "\<dots> = (\<Union>v\<in>Suf (s @ [c]). (SEQS (pders v r) (STAR r)))" |
|
306 apply(simp only: Suf) |
|
307 apply(simp add: Suf_Union pders_singleton) |
|
308 apply(auto) |
|
309 done |
|
310 finally have ?case . |
|
311 } |
|
312 moreover |
|
313 { assume asm: "s = []" |
|
314 then have ?case |
|
315 apply(simp add: pders_singleton Suf_def) |
|
316 apply(auto) |
|
317 apply(rule_tac x="[c]" in exI) |
|
318 apply(simp add: pders_singleton) |
|
319 done |
|
320 } |
|
321 ultimately show ?case by blast |
|
322 qed (simp) |
|
323 |
|
324 abbreviation |
|
325 "UNIV1 \<equiv> UNIV - {[]}" |
|
326 |
|
327 lemma pders_set_NULL: |
|
328 shows "pders_set UNIV1 NULL = {NULL}" |
|
329 by auto |
|
330 |
|
331 lemma pders_set_EMPTY: |
|
332 shows "pders_set UNIV1 EMPTY = {NULL}" |
|
333 by (auto split: if_splits) |
|
334 |
|
335 lemma pders_set_CHAR: |
|
336 shows "pders_set UNIV1 (CHAR c) \<subseteq> {EMPTY, NULL}" |
|
337 by (auto split: if_splits) |
|
338 |
|
339 lemma pders_set_ALT: |
|
340 shows "pders_set UNIV1 (ALT r1 r2) = pders_set UNIV1 r1 \<union> pders_set UNIV1 r2" |
|
341 by auto |
|
342 |
|
343 lemma pders_set_SEQ_aux: |
|
344 assumes a: "s \<in> UNIV1" |
|
345 shows "pders_set (Suf s) r2 \<subseteq> pders_set UNIV1 r2" |
|
346 using a by (auto simp add: Suf_def) |
|
347 |
|
348 lemma pders_set_SEQ: |
|
349 shows "pders_set UNIV1 (SEQ r1 r2) \<subseteq> SEQS (pders_set UNIV1 r1) r2 \<union> pders_set UNIV1 r2" |
|
350 apply(rule UN_least) |
|
351 apply(rule subset_trans) |
|
352 apply(rule pders_SEQ) |
|
353 apply(simp) |
|
354 apply(rule conjI) |
|
355 apply(auto)[1] |
|
356 apply(rule subset_trans) |
|
357 apply(rule pders_set_SEQ_aux) |
|
358 apply(auto) |
|
359 done |
|
360 |
|
361 lemma pders_set_STAR: |
|
362 shows "pders_set UNIV1 (STAR r) \<subseteq> SEQS (pders_set UNIV1 r) (STAR r)" |
|
363 apply(rule UN_least) |
|
364 apply(rule subset_trans) |
|
365 apply(rule pders_STAR) |
|
366 apply(simp) |
|
367 apply(simp add: Suf_def) |
|
368 apply(auto) |
|
369 done |
|
370 |
|
371 lemma finite_SEQS: |
|
372 assumes a: "finite A" |
|
373 shows "finite (SEQS A r)" |
|
374 using a by (auto) |
|
375 |
|
376 lemma finite_pders_set_UNIV1: |
|
377 shows "finite (pders_set UNIV1 r)" |
|
378 apply(induct r) |
|
379 apply(simp) |
|
380 apply(simp only: pders_set_EMPTY) |
|
381 apply(simp) |
|
382 apply(rule finite_subset) |
|
383 apply(rule pders_set_CHAR) |
|
384 apply(simp) |
|
385 apply(rule finite_subset) |
|
386 apply(rule pders_set_SEQ) |
|
387 apply(simp only: finite_SEQS finite_Un) |
|
388 apply(simp) |
|
389 apply(simp only: pders_set_ALT) |
|
390 apply(simp) |
|
391 apply(rule finite_subset) |
|
392 apply(rule pders_set_STAR) |
|
393 apply(simp only: finite_SEQS) |
|
394 done |
|
395 |
|
396 lemma pders_set_UNIV_UNIV1: |
|
397 shows "pders_set UNIV r = pders [] r \<union> pders_set UNIV1 r" |
|
398 apply(auto) |
|
399 apply(rule_tac x="[]" in exI) |
|
400 apply(simp) |
|
401 done |
|
402 |
|
403 lemma finite_pders_set_UNIV: |
|
404 shows "finite (pders_set UNIV r)" |
|
405 unfolding pders_set_UNIV_UNIV1 |
|
406 by (simp add: finite_pders_set_UNIV1) |
|
407 |
|
408 lemma finite_pders_set: |
|
409 shows "finite (pders_set A r)" |
|
410 apply(rule rev_finite_subset) |
|
411 apply(rule_tac r="r" in finite_pders_set_UNIV) |
|
412 apply(auto) |
|
413 done |
|
414 |
|
415 lemma finite_pders: |
|
416 shows "finite (pders s r)" |
|
417 using finite_pders_set[where A="{s}" and r="r"] |
|
418 by simp |
|
419 |
|
420 lemma closure_left_quotient: |
|
421 assumes "regular A" |
|
422 shows "regular (Ders_set B A)" |
|
423 proof - |
|
424 from assms obtain r::rexp where eq: "L r = A" by auto |
|
425 have fin: "finite (pders_set B r)" by (rule finite_pders_set) |
|
426 |
|
427 have "Ders_set B (L r) = (\<Union> L ` (pders_set B r))" |
|
428 by (simp add: Ders_set_pders_set) |
|
429 also have "\<dots> = L (\<Uplus>(pders_set B r))" using fin by simp |
|
430 finally have "Ders_set B A = L (\<Uplus>(pders_set B r))" using eq |
|
431 by simp |
|
432 then show "regular (Ders_set B A)" by auto |
|
433 qed |
|
434 |
|
435 |
|
436 fun |
|
437 width :: "rexp \<Rightarrow> nat" |
|
438 where |
|
439 "width (NULL) = 0" |
|
440 | "width (EMPTY) = 0" |
|
441 | "width (CHAR c) = 1" |
|
442 | "width (ALT r1 r2) = width r1 + width r2" |
|
443 | "width (SEQ r1 r2) = width r1 + width r2" |
|
444 | "width (STAR r) = width r" |
|
445 |
|
446 |
|
447 |
|
448 end |