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