556
|
1 |
theory RegLangs
|
|
2 |
imports Main "HOL-Library.Sublist"
|
|
3 |
begin
|
|
4 |
|
|
5 |
section \<open>Sequential Composition of Languages\<close>
|
|
6 |
|
|
7 |
definition
|
|
8 |
Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
|
|
9 |
where
|
|
10 |
"A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
|
|
11 |
|
|
12 |
text \<open>Two Simple Properties about Sequential Composition\<close>
|
|
13 |
|
|
14 |
lemma Sequ_empty_string [simp]:
|
|
15 |
shows "A ;; {[]} = A"
|
|
16 |
and "{[]} ;; A = A"
|
|
17 |
by (simp_all add: Sequ_def)
|
|
18 |
|
|
19 |
lemma Sequ_empty [simp]:
|
|
20 |
shows "A ;; {} = {}"
|
|
21 |
and "{} ;; A = {}"
|
|
22 |
by (simp_all add: Sequ_def)
|
|
23 |
|
642
|
24 |
lemma concI[simp,intro]: "u : A \<Longrightarrow> v : B \<Longrightarrow> u@v : A ;; B"
|
|
25 |
by (auto simp add: Sequ_def)
|
|
26 |
|
|
27 |
lemma concE[elim]:
|
|
28 |
assumes "w \<in> A ;; B"
|
|
29 |
obtains u v where "u \<in> A" "v \<in> B" "w = u@v"
|
|
30 |
using assms by (auto simp: Sequ_def)
|
|
31 |
|
|
32 |
lemma concI_if_Nil2: "[] \<in> B \<Longrightarrow> xs : A \<Longrightarrow> xs \<in> A ;; B"
|
|
33 |
by (metis append_Nil2 concI)
|
|
34 |
|
|
35 |
lemma conc_assoc: "(A ;; B) ;; C = A ;; (B ;; C)"
|
|
36 |
by (auto elim!: concE) (simp only: append_assoc[symmetric] concI)
|
|
37 |
|
|
38 |
|
|
39 |
text \<open>Language power operations\<close>
|
|
40 |
|
|
41 |
overloading lang_pow == "compow :: nat \<Rightarrow> string set \<Rightarrow> string set"
|
|
42 |
begin
|
|
43 |
primrec lang_pow :: "nat \<Rightarrow> string set \<Rightarrow> string set" where
|
|
44 |
"lang_pow 0 A = {[]}" |
|
|
45 |
"lang_pow (Suc n) A = A ;; (lang_pow n A)"
|
|
46 |
end
|
|
47 |
|
|
48 |
|
|
49 |
lemma conc_pow_comm:
|
|
50 |
shows "A ;; (A ^^ n) = (A ^^ n) ;; A"
|
|
51 |
by (induct n) (simp_all add: conc_assoc[symmetric])
|
|
52 |
|
|
53 |
lemma lang_pow_add: "A ^^ (n + m) = (A ^^ n) ;; (A ^^ m)"
|
|
54 |
by (induct n) (auto simp: conc_assoc)
|
|
55 |
|
|
56 |
lemma lang_empty:
|
|
57 |
fixes A::"string set"
|
|
58 |
shows "A ^^ 0 = {[]}"
|
|
59 |
by simp
|
556
|
60 |
|
|
61 |
section \<open>Semantic Derivative (Left Quotient) of Languages\<close>
|
|
62 |
|
|
63 |
definition
|
|
64 |
Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
|
|
65 |
where
|
|
66 |
"Der c A \<equiv> {s. c # s \<in> A}"
|
|
67 |
|
|
68 |
definition
|
|
69 |
Ders :: "string \<Rightarrow> string set \<Rightarrow> string set"
|
|
70 |
where
|
|
71 |
"Ders s A \<equiv> {s'. s @ s' \<in> A}"
|
|
72 |
|
|
73 |
lemma Der_null [simp]:
|
|
74 |
shows "Der c {} = {}"
|
|
75 |
unfolding Der_def
|
|
76 |
by auto
|
|
77 |
|
|
78 |
lemma Der_empty [simp]:
|
|
79 |
shows "Der c {[]} = {}"
|
|
80 |
unfolding Der_def
|
|
81 |
by auto
|
|
82 |
|
|
83 |
lemma Der_char [simp]:
|
|
84 |
shows "Der c {[d]} = (if c = d then {[]} else {})"
|
|
85 |
unfolding Der_def
|
|
86 |
by auto
|
|
87 |
|
|
88 |
lemma Der_union [simp]:
|
|
89 |
shows "Der c (A \<union> B) = Der c A \<union> Der c B"
|
|
90 |
unfolding Der_def
|
|
91 |
by auto
|
|
92 |
|
|
93 |
lemma Der_Sequ [simp]:
|
|
94 |
shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})"
|
|
95 |
unfolding Der_def Sequ_def
|
|
96 |
by (auto simp add: Cons_eq_append_conv)
|
|
97 |
|
|
98 |
|
|
99 |
section \<open>Kleene Star for Languages\<close>
|
|
100 |
|
|
101 |
inductive_set
|
|
102 |
Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
|
|
103 |
for A :: "string set"
|
|
104 |
where
|
|
105 |
start[intro]: "[] \<in> A\<star>"
|
|
106 |
| step[intro]: "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>"
|
|
107 |
|
|
108 |
(* Arden's lemma *)
|
|
109 |
|
|
110 |
lemma Star_cases:
|
|
111 |
shows "A\<star> = {[]} \<union> A ;; A\<star>"
|
|
112 |
unfolding Sequ_def
|
|
113 |
by (auto) (metis Star.simps)
|
|
114 |
|
|
115 |
lemma Star_decomp:
|
|
116 |
assumes "c # x \<in> A\<star>"
|
|
117 |
shows "\<exists>s1 s2. x = s1 @ s2 \<and> c # s1 \<in> A \<and> s2 \<in> A\<star>"
|
|
118 |
using assms
|
|
119 |
by (induct x\<equiv>"c # x" rule: Star.induct)
|
|
120 |
(auto simp add: append_eq_Cons_conv)
|
|
121 |
|
|
122 |
lemma Star_Der_Sequ:
|
|
123 |
shows "Der c (A\<star>) \<subseteq> (Der c A) ;; A\<star>"
|
|
124 |
unfolding Der_def Sequ_def
|
|
125 |
by(auto simp add: Star_decomp)
|
|
126 |
|
642
|
127 |
lemma Der_inter[simp]: "Der a (A \<inter> B) = Der a A \<inter> Der a B"
|
|
128 |
and Der_compl[simp]: "Der a (-A) = - Der a A"
|
|
129 |
and Der_Union[simp]: "Der a (Union M) = Union(Der a ` M)"
|
|
130 |
and Der_UN[simp]: "Der a (UN x:I. S x) = (UN x:I. Der a (S x))"
|
|
131 |
by (auto simp: Der_def)
|
556
|
132 |
|
|
133 |
lemma Der_star[simp]:
|
|
134 |
shows "Der c (A\<star>) = (Der c A) ;; A\<star>"
|
|
135 |
proof -
|
|
136 |
have "Der c (A\<star>) = Der c ({[]} \<union> A ;; A\<star>)"
|
|
137 |
by (simp only: Star_cases[symmetric])
|
|
138 |
also have "... = Der c (A ;; A\<star>)"
|
|
139 |
by (simp only: Der_union Der_empty) (simp)
|
|
140 |
also have "... = (Der c A) ;; A\<star> \<union> (if [] \<in> A then Der c (A\<star>) else {})"
|
|
141 |
by simp
|
|
142 |
also have "... = (Der c A) ;; A\<star>"
|
|
143 |
using Star_Der_Sequ by auto
|
|
144 |
finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" .
|
|
145 |
qed
|
|
146 |
|
642
|
147 |
lemma Der_pow[simp]:
|
|
148 |
shows "Der c (A ^^ n) = (if n = 0 then {} else (Der c A) ;; (A ^^ (n - 1)))"
|
|
149 |
apply(induct n arbitrary: A)
|
|
150 |
apply(auto simp add: Cons_eq_append_conv)
|
|
151 |
by (metis Suc_pred concI_if_Nil2 conc_assoc conc_pow_comm lang_pow.simps(2))
|
|
152 |
|
|
153 |
|
556
|
154 |
lemma Star_concat:
|
|
155 |
assumes "\<forall>s \<in> set ss. s \<in> A"
|
|
156 |
shows "concat ss \<in> A\<star>"
|
|
157 |
using assms by (induct ss) (auto)
|
|
158 |
|
|
159 |
lemma Star_split:
|
|
160 |
assumes "s \<in> A\<star>"
|
|
161 |
shows "\<exists>ss. concat ss = s \<and> (\<forall>s \<in> set ss. s \<in> A \<and> s \<noteq> [])"
|
|
162 |
using assms
|
|
163 |
apply(induct rule: Star.induct)
|
|
164 |
using concat.simps(1) apply fastforce
|
|
165 |
apply(clarify)
|
|
166 |
by (metis append_Nil concat.simps(2) set_ConsD)
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
section \<open>Regular Expressions\<close>
|
|
171 |
|
|
172 |
datatype rexp =
|
|
173 |
ZERO
|
|
174 |
| ONE
|
|
175 |
| CH char
|
|
176 |
| SEQ rexp rexp
|
|
177 |
| ALT rexp rexp
|
|
178 |
| STAR rexp
|
642
|
179 |
| NTIMES rexp nat
|
556
|
180 |
|
|
181 |
section \<open>Semantics of Regular Expressions\<close>
|
|
182 |
|
|
183 |
fun
|
|
184 |
L :: "rexp \<Rightarrow> string set"
|
|
185 |
where
|
|
186 |
"L (ZERO) = {}"
|
|
187 |
| "L (ONE) = {[]}"
|
|
188 |
| "L (CH c) = {[c]}"
|
|
189 |
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
|
|
190 |
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
|
|
191 |
| "L (STAR r) = (L r)\<star>"
|
642
|
192 |
| "L (NTIMES r n) = (L r) ^^ n"
|
556
|
193 |
|
|
194 |
section \<open>Nullable, Derivatives\<close>
|
|
195 |
|
|
196 |
fun
|
|
197 |
nullable :: "rexp \<Rightarrow> bool"
|
|
198 |
where
|
|
199 |
"nullable (ZERO) = False"
|
|
200 |
| "nullable (ONE) = True"
|
|
201 |
| "nullable (CH c) = False"
|
|
202 |
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
|
|
203 |
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
|
|
204 |
| "nullable (STAR r) = True"
|
642
|
205 |
| "nullable (NTIMES r n) = (if n = 0 then True else nullable r)"
|
556
|
206 |
|
|
207 |
fun
|
|
208 |
der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
|
|
209 |
where
|
|
210 |
"der c (ZERO) = ZERO"
|
|
211 |
| "der c (ONE) = ZERO"
|
|
212 |
| "der c (CH d) = (if c = d then ONE else ZERO)"
|
|
213 |
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
|
|
214 |
| "der c (SEQ r1 r2) =
|
|
215 |
(if nullable r1
|
|
216 |
then ALT (SEQ (der c r1) r2) (der c r2)
|
|
217 |
else SEQ (der c r1) r2)"
|
|
218 |
| "der c (STAR r) = SEQ (der c r) (STAR r)"
|
642
|
219 |
| "der c (NTIMES r n) = (if n = 0 then ZERO else SEQ (der c r) (NTIMES r (n - 1)))"
|
|
220 |
|
556
|
221 |
|
|
222 |
fun
|
|
223 |
ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
|
|
224 |
where
|
|
225 |
"ders [] r = r"
|
|
226 |
| "ders (c # s) r = ders s (der c r)"
|
|
227 |
|
|
228 |
|
642
|
229 |
lemma pow_empty_iff:
|
|
230 |
shows "[] \<in> (L r) ^^ n \<longleftrightarrow> (if n = 0 then True else [] \<in> (L r))"
|
|
231 |
by (induct n) (auto simp add: Sequ_def)
|
|
232 |
|
556
|
233 |
lemma nullable_correctness:
|
|
234 |
shows "nullable r \<longleftrightarrow> [] \<in> (L r)"
|
642
|
235 |
by (induct r) (auto simp add: Sequ_def pow_empty_iff)
|
556
|
236 |
|
|
237 |
lemma der_correctness:
|
|
238 |
shows "L (der c r) = Der c (L r)"
|
642
|
239 |
apply (induct r)
|
|
240 |
apply(auto simp add: nullable_correctness Sequ_def)
|
|
241 |
using Der_def apply force
|
|
242 |
using Der_def apply auto[1]
|
|
243 |
apply (smt (verit, ccfv_SIG) Der_def append_eq_Cons_conv mem_Collect_eq)
|
|
244 |
using Der_def apply force
|
|
245 |
using Der_Sequ Sequ_def by auto
|
556
|
246 |
|
|
247 |
lemma ders_correctness:
|
|
248 |
shows "L (ders s r) = Ders s (L r)"
|
|
249 |
by (induct s arbitrary: r)
|
|
250 |
(simp_all add: Ders_def der_correctness Der_def)
|
|
251 |
|
|
252 |
lemma ders_append:
|
|
253 |
shows "ders (s1 @ s2) r = ders s2 (ders s1 r)"
|
|
254 |
by (induct s1 arbitrary: s2 r) (auto)
|
|
255 |
|
|
256 |
lemma ders_snoc:
|
|
257 |
shows "ders (s @ [c]) r = der c (ders s r)"
|
|
258 |
by (simp add: ders_append)
|
|
259 |
|
|
260 |
|
|
261 |
end |