|
1 |
|
2 theory RegLangs |
|
3 imports Main "~~/src/HOL/Library/Sublist" |
|
4 begin |
|
5 |
|
6 section {* Sequential Composition of Languages *} |
|
7 |
|
8 definition |
|
9 Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100) |
|
10 where |
|
11 "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}" |
|
12 |
|
13 text {* Two Simple Properties about Sequential Composition *} |
|
14 |
|
15 lemma Sequ_empty_string [simp]: |
|
16 shows "A ;; {[]} = A" |
|
17 and "{[]} ;; A = A" |
|
18 by (simp_all add: Sequ_def) |
|
19 |
|
20 lemma Sequ_empty [simp]: |
|
21 shows "A ;; {} = {}" |
|
22 and "{} ;; A = {}" |
|
23 by (simp_all add: Sequ_def) |
|
24 |
|
25 |
|
26 section {* Semantic Derivative (Left Quotient) of Languages *} |
|
27 |
|
28 definition |
|
29 Der :: "char \<Rightarrow> string set \<Rightarrow> string set" |
|
30 where |
|
31 "Der c A \<equiv> {s. c # s \<in> A}" |
|
32 |
|
33 definition |
|
34 Ders :: "string \<Rightarrow> string set \<Rightarrow> string set" |
|
35 where |
|
36 "Ders s A \<equiv> {s'. s @ s' \<in> A}" |
|
37 |
|
38 lemma Der_null [simp]: |
|
39 shows "Der c {} = {}" |
|
40 unfolding Der_def |
|
41 by auto |
|
42 |
|
43 lemma Der_empty [simp]: |
|
44 shows "Der c {[]} = {}" |
|
45 unfolding Der_def |
|
46 by auto |
|
47 |
|
48 lemma Der_char [simp]: |
|
49 shows "Der c {[d]} = (if c = d then {[]} else {})" |
|
50 unfolding Der_def |
|
51 by auto |
|
52 |
|
53 lemma Der_union [simp]: |
|
54 shows "Der c (A \<union> B) = Der c A \<union> Der c B" |
|
55 unfolding Der_def |
|
56 by auto |
|
57 |
|
58 lemma Der_Sequ [simp]: |
|
59 shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})" |
|
60 unfolding Der_def Sequ_def |
|
61 by (auto simp add: Cons_eq_append_conv) |
|
62 |
|
63 |
|
64 section {* Kleene Star for Languages *} |
|
65 |
|
66 inductive_set |
|
67 Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102) |
|
68 for A :: "string set" |
|
69 where |
|
70 start[intro]: "[] \<in> A\<star>" |
|
71 | step[intro]: "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>" |
|
72 |
|
73 (* Arden's lemma *) |
|
74 |
|
75 lemma Star_cases: |
|
76 shows "A\<star> = {[]} \<union> A ;; A\<star>" |
|
77 unfolding Sequ_def |
|
78 by (auto) (metis Star.simps) |
|
79 |
|
80 lemma Star_decomp: |
|
81 assumes "c # x \<in> A\<star>" |
|
82 shows "\<exists>s1 s2. x = s1 @ s2 \<and> c # s1 \<in> A \<and> s2 \<in> A\<star>" |
|
83 using assms |
|
84 by (induct x\<equiv>"c # x" rule: Star.induct) |
|
85 (auto simp add: append_eq_Cons_conv) |
|
86 |
|
87 lemma Star_Der_Sequ: |
|
88 shows "Der c (A\<star>) \<subseteq> (Der c A) ;; A\<star>" |
|
89 unfolding Der_def Sequ_def |
|
90 by(auto simp add: Star_decomp) |
|
91 |
|
92 |
|
93 lemma Der_star [simp]: |
|
94 shows "Der c (A\<star>) = (Der c A) ;; A\<star>" |
|
95 proof - |
|
96 have "Der c (A\<star>) = Der c ({[]} \<union> A ;; A\<star>)" |
|
97 by (simp only: Star_cases[symmetric]) |
|
98 also have "... = Der c (A ;; A\<star>)" |
|
99 by (simp only: Der_union Der_empty) (simp) |
|
100 also have "... = (Der c A) ;; A\<star> \<union> (if [] \<in> A then Der c (A\<star>) else {})" |
|
101 by simp |
|
102 also have "... = (Der c A) ;; A\<star>" |
|
103 using Star_Der_Sequ by auto |
|
104 finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" . |
|
105 qed |
|
106 |
|
107 lemma Star_concat: |
|
108 assumes "\<forall>s \<in> set ss. s \<in> A" |
|
109 shows "concat ss \<in> A\<star>" |
|
110 using assms by (induct ss) (auto) |
|
111 |
|
112 lemma Star_split: |
|
113 assumes "s \<in> A\<star>" |
|
114 shows "\<exists>ss. concat ss = s \<and> (\<forall>s \<in> set ss. s \<in> A \<and> s \<noteq> [])" |
|
115 using assms |
|
116 apply(induct rule: Star.induct) |
|
117 using concat.simps(1) apply fastforce |
|
118 apply(clarify) |
|
119 by (metis append_Nil concat.simps(2) set_ConsD) |
|
120 |
|
121 |
|
122 |
|
123 section {* Regular Expressions *} |
|
124 |
|
125 datatype rexp = |
|
126 ZERO |
|
127 | ONE |
|
128 | CHAR char |
|
129 | SEQ rexp rexp |
|
130 | ALT rexp rexp |
|
131 | STAR rexp |
|
132 |
|
133 section {* Semantics of Regular Expressions *} |
|
134 |
|
135 fun |
|
136 L :: "rexp \<Rightarrow> string set" |
|
137 where |
|
138 "L (ZERO) = {}" |
|
139 | "L (ONE) = {[]}" |
|
140 | "L (CHAR c) = {[c]}" |
|
141 | "L (SEQ r1 r2) = (L r1) ;; (L r2)" |
|
142 | "L (ALT r1 r2) = (L r1) \<union> (L r2)" |
|
143 | "L (STAR r) = (L r)\<star>" |
|
144 |
|
145 |
|
146 section {* Nullable, Derivatives *} |
|
147 |
|
148 fun |
|
149 nullable :: "rexp \<Rightarrow> bool" |
|
150 where |
|
151 "nullable (ZERO) = False" |
|
152 | "nullable (ONE) = True" |
|
153 | "nullable (CHAR c) = False" |
|
154 | "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)" |
|
155 | "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)" |
|
156 | "nullable (STAR r) = True" |
|
157 |
|
158 |
|
159 fun |
|
160 der :: "char \<Rightarrow> rexp \<Rightarrow> rexp" |
|
161 where |
|
162 "der c (ZERO) = ZERO" |
|
163 | "der c (ONE) = ZERO" |
|
164 | "der c (CHAR d) = (if c = d then ONE else ZERO)" |
|
165 | "der c (ALT r1 r2) = ALT (der c r1) (der c r2)" |
|
166 | "der c (SEQ r1 r2) = |
|
167 (if nullable r1 |
|
168 then ALT (SEQ (der c r1) r2) (der c r2) |
|
169 else SEQ (der c r1) r2)" |
|
170 | "der c (STAR r) = SEQ (der c r) (STAR r)" |
|
171 |
|
172 fun |
|
173 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp" |
|
174 where |
|
175 "ders [] r = r" |
|
176 | "ders (c # s) r = ders s (der c r)" |
|
177 |
|
178 |
|
179 lemma nullable_correctness: |
|
180 shows "nullable r \<longleftrightarrow> [] \<in> (L r)" |
|
181 by (induct r) (auto simp add: Sequ_def) |
|
182 |
|
183 lemma der_correctness: |
|
184 shows "L (der c r) = Der c (L r)" |
|
185 by (induct r) (simp_all add: nullable_correctness) |
|
186 |
|
187 lemma ders_correctness: |
|
188 shows "L (ders s r) = Ders s (L r)" |
|
189 by (induct s arbitrary: r) |
|
190 (simp_all add: Ders_def der_correctness Der_def) |
|
191 |
|
192 lemma ders_append: |
|
193 shows "ders (s1 @ s2) r = ders s2 (ders s1 r)" |
|
194 by (induct s1 arbitrary: s2 r) (auto) |
|
195 |
|
196 |
|
197 |
|
198 end |