266
|
1 |
|
|
2 |
theory Spec
|
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 = {}"
|
|
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 |
|
|
108 |
section {* Regular Expressions *}
|
|
109 |
|
|
110 |
datatype rexp =
|
|
111 |
ZERO
|
|
112 |
| ONE
|
|
113 |
| CHAR char
|
|
114 |
| SEQ rexp rexp
|
|
115 |
| ALT rexp rexp
|
|
116 |
| STAR rexp
|
|
117 |
|
|
118 |
section {* Semantics of Regular Expressions *}
|
|
119 |
|
|
120 |
fun
|
|
121 |
L :: "rexp \<Rightarrow> string set"
|
|
122 |
where
|
|
123 |
"L (ZERO) = {}"
|
|
124 |
| "L (ONE) = {[]}"
|
|
125 |
| "L (CHAR c) = {[c]}"
|
|
126 |
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
|
|
127 |
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
|
|
128 |
| "L (STAR r) = (L r)\<star>"
|
|
129 |
|
|
130 |
|
|
131 |
section {* Nullable, Derivatives *}
|
|
132 |
|
|
133 |
fun
|
|
134 |
nullable :: "rexp \<Rightarrow> bool"
|
|
135 |
where
|
|
136 |
"nullable (ZERO) = False"
|
|
137 |
| "nullable (ONE) = True"
|
|
138 |
| "nullable (CHAR c) = False"
|
|
139 |
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
|
|
140 |
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
|
|
141 |
| "nullable (STAR r) = True"
|
|
142 |
|
|
143 |
|
|
144 |
fun
|
|
145 |
der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
|
|
146 |
where
|
|
147 |
"der c (ZERO) = ZERO"
|
|
148 |
| "der c (ONE) = ZERO"
|
|
149 |
| "der c (CHAR d) = (if c = d then ONE else ZERO)"
|
|
150 |
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
|
|
151 |
| "der c (SEQ r1 r2) =
|
|
152 |
(if nullable r1
|
|
153 |
then ALT (SEQ (der c r1) r2) (der c r2)
|
|
154 |
else SEQ (der c r1) r2)"
|
|
155 |
| "der c (STAR r) = SEQ (der c r) (STAR r)"
|
|
156 |
|
|
157 |
fun
|
|
158 |
ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
|
|
159 |
where
|
|
160 |
"ders [] r = r"
|
|
161 |
| "ders (c # s) r = ders s (der c r)"
|
|
162 |
|
|
163 |
|
|
164 |
lemma nullable_correctness:
|
|
165 |
shows "nullable r \<longleftrightarrow> [] \<in> (L r)"
|
|
166 |
by (induct r) (auto simp add: Sequ_def)
|
|
167 |
|
|
168 |
lemma der_correctness:
|
|
169 |
shows "L (der c r) = Der c (L r)"
|
|
170 |
by (induct r) (simp_all add: nullable_correctness)
|
|
171 |
|
|
172 |
lemma ders_correctness:
|
|
173 |
shows "L (ders s r) = Ders s (L r)"
|
267
|
174 |
by (induct s arbitrary: r)
|
|
175 |
(simp_all add: Ders_def der_correctness Der_def)
|
|
176 |
|
266
|
177 |
|
|
178 |
|
|
179 |
section {* Values *}
|
|
180 |
|
|
181 |
datatype val =
|
|
182 |
Void
|
|
183 |
| Char char
|
|
184 |
| Seq val val
|
|
185 |
| Right val
|
|
186 |
| Left val
|
|
187 |
| Stars "val list"
|
|
188 |
|
|
189 |
|
|
190 |
section {* The string behind a value *}
|
|
191 |
|
|
192 |
fun
|
|
193 |
flat :: "val \<Rightarrow> string"
|
|
194 |
where
|
|
195 |
"flat (Void) = []"
|
|
196 |
| "flat (Char c) = [c]"
|
|
197 |
| "flat (Left v) = flat v"
|
|
198 |
| "flat (Right v) = flat v"
|
|
199 |
| "flat (Seq v1 v2) = (flat v1) @ (flat v2)"
|
|
200 |
| "flat (Stars []) = []"
|
|
201 |
| "flat (Stars (v#vs)) = (flat v) @ (flat (Stars vs))"
|
|
202 |
|
267
|
203 |
abbreviation
|
|
204 |
"flats vs \<equiv> concat (map flat vs)"
|
|
205 |
|
266
|
206 |
lemma flat_Stars [simp]:
|
267
|
207 |
"flat (Stars vs) = flats vs"
|
266
|
208 |
by (induct vs) (auto)
|
|
209 |
|
|
210 |
lemma Star_concat:
|
|
211 |
assumes "\<forall>s \<in> set ss. s \<in> A"
|
|
212 |
shows "concat ss \<in> A\<star>"
|
|
213 |
using assms by (induct ss) (auto)
|
|
214 |
|
268
|
215 |
lemma Star_cstring:
|
266
|
216 |
assumes "s \<in> A\<star>"
|
268
|
217 |
shows "\<exists>ss. concat ss = s \<and> (\<forall>s \<in> set ss. s \<in> A \<and> s \<noteq> [])"
|
266
|
218 |
using assms
|
|
219 |
apply(induct rule: Star.induct)
|
268
|
220 |
apply(auto)[1]
|
266
|
221 |
apply(rule_tac x="[]" in exI)
|
|
222 |
apply(simp)
|
268
|
223 |
apply(erule exE)
|
|
224 |
apply(clarify)
|
|
225 |
apply(case_tac "s1 = []")
|
|
226 |
apply(rule_tac x="ss" in exI)
|
|
227 |
apply(simp)
|
266
|
228 |
apply(rule_tac x="s1#ss" in exI)
|
|
229 |
apply(simp)
|
|
230 |
done
|
|
231 |
|
|
232 |
|
268
|
233 |
section {* Lexical Values *}
|
266
|
234 |
|
|
235 |
inductive
|
268
|
236 |
Prf :: "val \<Rightarrow> rexp \<Rightarrow> bool" ("\<Turnstile> _ : _" [100, 100] 100)
|
266
|
237 |
where
|
|
238 |
"\<lbrakk>\<Turnstile> v1 : r1; \<Turnstile> v2 : r2\<rbrakk> \<Longrightarrow> \<Turnstile> Seq v1 v2 : SEQ r1 r2"
|
|
239 |
| "\<Turnstile> v1 : r1 \<Longrightarrow> \<Turnstile> Left v1 : ALT r1 r2"
|
|
240 |
| "\<Turnstile> v2 : r2 \<Longrightarrow> \<Turnstile> Right v2 : ALT r1 r2"
|
|
241 |
| "\<Turnstile> Void : ONE"
|
|
242 |
| "\<Turnstile> Char c : CHAR c"
|
|
243 |
| "\<forall>v \<in> set vs. \<Turnstile> v : r \<and> flat v \<noteq> [] \<Longrightarrow> \<Turnstile> Stars vs : STAR r"
|
|
244 |
|
268
|
245 |
inductive_cases Prf_elims:
|
|
246 |
"\<Turnstile> v : ZERO"
|
|
247 |
"\<Turnstile> v : SEQ r1 r2"
|
|
248 |
"\<Turnstile> v : ALT r1 r2"
|
|
249 |
"\<Turnstile> v : ONE"
|
|
250 |
"\<Turnstile> v : CHAR c"
|
|
251 |
"\<Turnstile> vs : STAR r"
|
266
|
252 |
|
268
|
253 |
lemma Prf_Stars_appendE:
|
266
|
254 |
assumes "\<Turnstile> Stars (vs1 @ vs2) : STAR r"
|
|
255 |
shows "\<Turnstile> Stars vs1 : STAR r \<and> \<Turnstile> Stars vs2 : STAR r"
|
|
256 |
using assms
|
268
|
257 |
by (auto intro: Prf.intros elim!: Prf_elims)
|
|
258 |
|
|
259 |
|
|
260 |
lemma Star_cval:
|
|
261 |
assumes "\<forall>s\<in>set ss. \<exists>v. s = flat v \<and> \<Turnstile> v : r"
|
|
262 |
shows "\<exists>vs. flats vs = concat ss \<and> (\<forall>v\<in>set vs. \<Turnstile> v : r \<and> flat v \<noteq> [])"
|
|
263 |
using assms
|
|
264 |
apply(induct ss)
|
|
265 |
apply(auto)
|
|
266 |
apply(rule_tac x="[]" in exI)
|
|
267 |
apply(simp)
|
|
268 |
apply(case_tac "flat v = []")
|
|
269 |
apply(rule_tac x="vs" in exI)
|
|
270 |
apply(simp)
|
|
271 |
apply(rule_tac x="v#vs" in exI)
|
|
272 |
apply(simp)
|
266
|
273 |
done
|
|
274 |
|
267
|
275 |
|
268
|
276 |
lemma L_flat_Prf1:
|
|
277 |
assumes "\<Turnstile> v : r"
|
|
278 |
shows "flat v \<in> L r"
|
|
279 |
using assms
|
|
280 |
by (induct) (auto simp add: Sequ_def Star_concat)
|
266
|
281 |
|
268
|
282 |
lemma L_flat_Prf2:
|
|
283 |
assumes "s \<in> L r"
|
|
284 |
shows "\<exists>v. \<Turnstile> v : r \<and> flat v = s"
|
|
285 |
using assms
|
|
286 |
proof(induct r arbitrary: s)
|
|
287 |
case (STAR r s)
|
|
288 |
have IH: "\<And>s. s \<in> L r \<Longrightarrow> \<exists>v. \<Turnstile> v : r \<and> flat v = s" by fact
|
|
289 |
have "s \<in> L (STAR r)" by fact
|
|
290 |
then obtain ss where "concat ss = s" "\<forall>s \<in> set ss. s \<in> L r \<and> s \<noteq> []"
|
|
291 |
using Star_cstring by auto
|
|
292 |
then obtain vs where "flats vs = s" "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> flat v \<noteq> []"
|
|
293 |
using IH Star_cval by metis
|
|
294 |
then show "\<exists>v. \<Turnstile> v : STAR r \<and> flat v = s"
|
|
295 |
using Prf.intros(6) flat_Stars by blast
|
|
296 |
next
|
|
297 |
case (SEQ r1 r2 s)
|
|
298 |
then show "\<exists>v. \<Turnstile> v : SEQ r1 r2 \<and> flat v = s"
|
|
299 |
unfolding Sequ_def L.simps by (fastforce intro: Prf.intros)
|
|
300 |
next
|
|
301 |
case (ALT r1 r2 s)
|
|
302 |
then show "\<exists>v. \<Turnstile> v : ALT r1 r2 \<and> flat v = s"
|
|
303 |
unfolding L.simps by (fastforce intro: Prf.intros)
|
|
304 |
qed (auto intro: Prf.intros)
|
|
305 |
|
|
306 |
|
|
307 |
lemma L_flat_Prf:
|
|
308 |
shows "L(r) = {flat v | v. \<Turnstile> v : r}"
|
|
309 |
using L_flat_Prf1 L_flat_Prf2 by blast
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
section {* Sets of Lexical Values *}
|
|
314 |
|
|
315 |
text {*
|
|
316 |
Shows that lexical values are finite for a given regex and string.
|
|
317 |
*}
|
266
|
318 |
|
|
319 |
definition
|
268
|
320 |
LV :: "rexp \<Rightarrow> string \<Rightarrow> val set"
|
|
321 |
where "LV r s \<equiv> {v. \<Turnstile> v : r \<and> flat v = s}"
|
267
|
322 |
|
268
|
323 |
lemma LV_simps:
|
|
324 |
shows "LV ZERO s = {}"
|
|
325 |
and "LV ONE s = (if s = [] then {Void} else {})"
|
|
326 |
and "LV (CHAR c) s = (if s = [c] then {Char c} else {})"
|
|
327 |
and "LV (ALT r1 r2) s = Left ` LV r1 s \<union> Right ` LV r2 s"
|
|
328 |
unfolding LV_def
|
|
329 |
by (auto intro: Prf.intros elim: Prf.cases)
|
|
330 |
|
267
|
331 |
|
|
332 |
abbreviation
|
|
333 |
"Prefixes s \<equiv> {s'. prefixeq s' s}"
|
|
334 |
|
|
335 |
abbreviation
|
|
336 |
"Suffixes s \<equiv> {s'. suffixeq s' s}"
|
|
337 |
|
|
338 |
abbreviation
|
|
339 |
"SSuffixes s \<equiv> {s'. suffix s' s}"
|
266
|
340 |
|
267
|
341 |
lemma Suffixes_cons [simp]:
|
|
342 |
shows "Suffixes (c # s) = Suffixes s \<union> {c # s}"
|
|
343 |
by (auto simp add: suffixeq_def Cons_eq_append_conv)
|
|
344 |
|
|
345 |
|
|
346 |
lemma finite_Suffixes:
|
|
347 |
shows "finite (Suffixes s)"
|
|
348 |
by (induct s) (simp_all)
|
266
|
349 |
|
267
|
350 |
lemma finite_SSuffixes:
|
|
351 |
shows "finite (SSuffixes s)"
|
|
352 |
proof -
|
|
353 |
have "SSuffixes s \<subseteq> Suffixes s"
|
|
354 |
unfolding suffix_def suffixeq_def by auto
|
|
355 |
then show "finite (SSuffixes s)"
|
|
356 |
using finite_Suffixes finite_subset by blast
|
|
357 |
qed
|
|
358 |
|
|
359 |
lemma finite_Prefixes:
|
|
360 |
shows "finite (Prefixes s)"
|
|
361 |
proof -
|
|
362 |
have "finite (Suffixes (rev s))"
|
|
363 |
by (rule finite_Suffixes)
|
|
364 |
then have "finite (rev ` Suffixes (rev s))" by simp
|
|
365 |
moreover
|
|
366 |
have "rev ` (Suffixes (rev s)) = Prefixes s"
|
|
367 |
unfolding suffixeq_def prefixeq_def image_def
|
|
368 |
by (auto)(metis rev_append rev_rev_ident)+
|
|
369 |
ultimately show "finite (Prefixes s)" by simp
|
|
370 |
qed
|
|
371 |
|
268
|
372 |
lemma LV_STAR_finite:
|
|
373 |
assumes "\<forall>s. finite (LV r s)"
|
|
374 |
shows "finite (LV (STAR r) s)"
|
267
|
375 |
proof(induct s rule: length_induct)
|
|
376 |
fix s::"char list"
|
268
|
377 |
assume "\<forall>s'. length s' < length s \<longrightarrow> finite (LV (STAR r) s')"
|
|
378 |
then have IH: "\<forall>s' \<in> SSuffixes s. finite (LV (STAR r) s')"
|
267
|
379 |
by (auto simp add: suffix_def)
|
|
380 |
def f \<equiv> "\<lambda>(v, vs). Stars (v # vs)"
|
268
|
381 |
def S1 \<equiv> "\<Union>s' \<in> Prefixes s. LV r s'"
|
|
382 |
def S2 \<equiv> "\<Union>s2 \<in> SSuffixes s. Stars -` (LV (STAR r) s2)"
|
267
|
383 |
have "finite S1" using assms
|
|
384 |
unfolding S1_def by (simp_all add: finite_Prefixes)
|
|
385 |
moreover
|
|
386 |
with IH have "finite S2" unfolding S2_def
|
|
387 |
by (auto simp add: finite_SSuffixes inj_on_def finite_vimageI)
|
|
388 |
ultimately
|
|
389 |
have "finite ({Stars []} \<union> f ` (S1 \<times> S2))" by simp
|
|
390 |
moreover
|
268
|
391 |
have "LV (STAR r) s \<subseteq> {Stars []} \<union> f ` (S1 \<times> S2)"
|
|
392 |
unfolding S1_def S2_def f_def
|
|
393 |
unfolding LV_def image_def prefixeq_def suffix_def
|
|
394 |
apply(auto elim: Prf_elims)
|
|
395 |
apply(erule Prf_elims)
|
|
396 |
apply(auto)
|
|
397 |
apply(case_tac vs)
|
|
398 |
apply(auto intro: Prf.intros)
|
|
399 |
done
|
267
|
400 |
ultimately
|
268
|
401 |
show "finite (LV (STAR r) s)" by (simp add: finite_subset)
|
267
|
402 |
qed
|
|
403 |
|
|
404 |
|
268
|
405 |
lemma LV_finite:
|
|
406 |
shows "finite (LV r s)"
|
267
|
407 |
proof(induct r arbitrary: s)
|
|
408 |
case (ZERO s)
|
268
|
409 |
show "finite (LV ZERO s)" by (simp add: LV_simps)
|
267
|
410 |
next
|
|
411 |
case (ONE s)
|
268
|
412 |
show "finite (LV ONE s)" by (simp add: LV_simps)
|
267
|
413 |
next
|
|
414 |
case (CHAR c s)
|
268
|
415 |
show "finite (LV (CHAR c) s)" by (simp add: LV_simps)
|
267
|
416 |
next
|
|
417 |
case (ALT r1 r2 s)
|
268
|
418 |
then show "finite (LV (ALT r1 r2) s)" by (simp add: LV_simps)
|
267
|
419 |
next
|
|
420 |
case (SEQ r1 r2 s)
|
|
421 |
def f \<equiv> "\<lambda>(v1, v2). Seq v1 v2"
|
268
|
422 |
def S1 \<equiv> "\<Union>s' \<in> Prefixes s. LV r1 s'"
|
|
423 |
def S2 \<equiv> "\<Union>s' \<in> Suffixes s. LV r2 s'"
|
|
424 |
have IHs: "\<And>s. finite (LV r1 s)" "\<And>s. finite (LV r2 s)" by fact+
|
267
|
425 |
then have "finite S1" "finite S2" unfolding S1_def S2_def
|
|
426 |
by (simp_all add: finite_Prefixes finite_Suffixes)
|
|
427 |
moreover
|
268
|
428 |
have "LV (SEQ r1 r2) s \<subseteq> f ` (S1 \<times> S2)"
|
|
429 |
unfolding f_def S1_def S2_def
|
|
430 |
unfolding LV_def image_def prefixeq_def suffixeq_def
|
|
431 |
by (auto elim: Prf.cases)
|
267
|
432 |
ultimately
|
268
|
433 |
show "finite (LV (SEQ r1 r2) s)"
|
267
|
434 |
by (simp add: finite_subset)
|
|
435 |
next
|
|
436 |
case (STAR r s)
|
268
|
437 |
then show "finite (LV (STAR r) s)" by (simp add: LV_STAR_finite)
|
267
|
438 |
qed
|
|
439 |
|
|
440 |
|
266
|
441 |
|
|
442 |
section {* Our POSIX Definition *}
|
|
443 |
|
|
444 |
inductive
|
|
445 |
Posix :: "string \<Rightarrow> rexp \<Rightarrow> val \<Rightarrow> bool" ("_ \<in> _ \<rightarrow> _" [100, 100, 100] 100)
|
|
446 |
where
|
|
447 |
Posix_ONE: "[] \<in> ONE \<rightarrow> Void"
|
|
448 |
| Posix_CHAR: "[c] \<in> (CHAR c) \<rightarrow> (Char c)"
|
|
449 |
| Posix_ALT1: "s \<in> r1 \<rightarrow> v \<Longrightarrow> s \<in> (ALT r1 r2) \<rightarrow> (Left v)"
|
|
450 |
| Posix_ALT2: "\<lbrakk>s \<in> r2 \<rightarrow> v; s \<notin> L(r1)\<rbrakk> \<Longrightarrow> s \<in> (ALT r1 r2) \<rightarrow> (Right v)"
|
|
451 |
| Posix_SEQ: "\<lbrakk>s1 \<in> r1 \<rightarrow> v1; s2 \<in> r2 \<rightarrow> v2;
|
|
452 |
\<not>(\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (s1 @ s\<^sub>3) \<in> L r1 \<and> s\<^sub>4 \<in> L r2)\<rbrakk> \<Longrightarrow>
|
|
453 |
(s1 @ s2) \<in> (SEQ r1 r2) \<rightarrow> (Seq v1 v2)"
|
|
454 |
| Posix_STAR1: "\<lbrakk>s1 \<in> r \<rightarrow> v; s2 \<in> STAR r \<rightarrow> Stars vs; flat v \<noteq> [];
|
|
455 |
\<not>(\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (s1 @ s\<^sub>3) \<in> L r \<and> s\<^sub>4 \<in> L (STAR r))\<rbrakk>
|
|
456 |
\<Longrightarrow> (s1 @ s2) \<in> STAR r \<rightarrow> Stars (v # vs)"
|
|
457 |
| Posix_STAR2: "[] \<in> STAR r \<rightarrow> Stars []"
|
|
458 |
|
|
459 |
inductive_cases Posix_elims:
|
|
460 |
"s \<in> ZERO \<rightarrow> v"
|
|
461 |
"s \<in> ONE \<rightarrow> v"
|
|
462 |
"s \<in> CHAR c \<rightarrow> v"
|
|
463 |
"s \<in> ALT r1 r2 \<rightarrow> v"
|
|
464 |
"s \<in> SEQ r1 r2 \<rightarrow> v"
|
|
465 |
"s \<in> STAR r \<rightarrow> v"
|
|
466 |
|
|
467 |
lemma Posix1:
|
|
468 |
assumes "s \<in> r \<rightarrow> v"
|
|
469 |
shows "s \<in> L r" "flat v = s"
|
|
470 |
using assms
|
|
471 |
by (induct s r v rule: Posix.induct)
|
|
472 |
(auto simp add: Sequ_def)
|
|
473 |
|
|
474 |
text {*
|
|
475 |
Our Posix definition determines a unique value.
|
|
476 |
*}
|
|
477 |
|
|
478 |
lemma Posix_determ:
|
|
479 |
assumes "s \<in> r \<rightarrow> v1" "s \<in> r \<rightarrow> v2"
|
|
480 |
shows "v1 = v2"
|
|
481 |
using assms
|
|
482 |
proof (induct s r v1 arbitrary: v2 rule: Posix.induct)
|
|
483 |
case (Posix_ONE v2)
|
|
484 |
have "[] \<in> ONE \<rightarrow> v2" by fact
|
|
485 |
then show "Void = v2" by cases auto
|
|
486 |
next
|
|
487 |
case (Posix_CHAR c v2)
|
|
488 |
have "[c] \<in> CHAR c \<rightarrow> v2" by fact
|
|
489 |
then show "Char c = v2" by cases auto
|
|
490 |
next
|
|
491 |
case (Posix_ALT1 s r1 v r2 v2)
|
|
492 |
have "s \<in> ALT r1 r2 \<rightarrow> v2" by fact
|
|
493 |
moreover
|
|
494 |
have "s \<in> r1 \<rightarrow> v" by fact
|
|
495 |
then have "s \<in> L r1" by (simp add: Posix1)
|
|
496 |
ultimately obtain v' where eq: "v2 = Left v'" "s \<in> r1 \<rightarrow> v'" by cases auto
|
|
497 |
moreover
|
|
498 |
have IH: "\<And>v2. s \<in> r1 \<rightarrow> v2 \<Longrightarrow> v = v2" by fact
|
|
499 |
ultimately have "v = v'" by simp
|
|
500 |
then show "Left v = v2" using eq by simp
|
|
501 |
next
|
|
502 |
case (Posix_ALT2 s r2 v r1 v2)
|
|
503 |
have "s \<in> ALT r1 r2 \<rightarrow> v2" by fact
|
|
504 |
moreover
|
|
505 |
have "s \<notin> L r1" by fact
|
|
506 |
ultimately obtain v' where eq: "v2 = Right v'" "s \<in> r2 \<rightarrow> v'"
|
|
507 |
by cases (auto simp add: Posix1)
|
|
508 |
moreover
|
|
509 |
have IH: "\<And>v2. s \<in> r2 \<rightarrow> v2 \<Longrightarrow> v = v2" by fact
|
|
510 |
ultimately have "v = v'" by simp
|
|
511 |
then show "Right v = v2" using eq by simp
|
|
512 |
next
|
|
513 |
case (Posix_SEQ s1 r1 v1 s2 r2 v2 v')
|
|
514 |
have "(s1 @ s2) \<in> SEQ r1 r2 \<rightarrow> v'"
|
|
515 |
"s1 \<in> r1 \<rightarrow> v1" "s2 \<in> r2 \<rightarrow> v2"
|
|
516 |
"\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> L r1 \<and> s\<^sub>4 \<in> L r2)" by fact+
|
|
517 |
then obtain v1' v2' where "v' = Seq v1' v2'" "s1 \<in> r1 \<rightarrow> v1'" "s2 \<in> r2 \<rightarrow> v2'"
|
|
518 |
apply(cases) apply (auto simp add: append_eq_append_conv2)
|
|
519 |
using Posix1(1) by fastforce+
|
|
520 |
moreover
|
|
521 |
have IHs: "\<And>v1'. s1 \<in> r1 \<rightarrow> v1' \<Longrightarrow> v1 = v1'"
|
|
522 |
"\<And>v2'. s2 \<in> r2 \<rightarrow> v2' \<Longrightarrow> v2 = v2'" by fact+
|
|
523 |
ultimately show "Seq v1 v2 = v'" by simp
|
|
524 |
next
|
|
525 |
case (Posix_STAR1 s1 r v s2 vs v2)
|
|
526 |
have "(s1 @ s2) \<in> STAR r \<rightarrow> v2"
|
|
527 |
"s1 \<in> r \<rightarrow> v" "s2 \<in> STAR r \<rightarrow> Stars vs" "flat v \<noteq> []"
|
|
528 |
"\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> L r \<and> s\<^sub>4 \<in> L (STAR r))" by fact+
|
|
529 |
then obtain v' vs' where "v2 = Stars (v' # vs')" "s1 \<in> r \<rightarrow> v'" "s2 \<in> (STAR r) \<rightarrow> (Stars vs')"
|
|
530 |
apply(cases) apply (auto simp add: append_eq_append_conv2)
|
|
531 |
using Posix1(1) apply fastforce
|
|
532 |
apply (metis Posix1(1) Posix_STAR1.hyps(6) append_Nil append_Nil2)
|
|
533 |
using Posix1(2) by blast
|
|
534 |
moreover
|
|
535 |
have IHs: "\<And>v2. s1 \<in> r \<rightarrow> v2 \<Longrightarrow> v = v2"
|
|
536 |
"\<And>v2. s2 \<in> STAR r \<rightarrow> v2 \<Longrightarrow> Stars vs = v2" by fact+
|
|
537 |
ultimately show "Stars (v # vs) = v2" by auto
|
|
538 |
next
|
|
539 |
case (Posix_STAR2 r v2)
|
|
540 |
have "[] \<in> STAR r \<rightarrow> v2" by fact
|
|
541 |
then show "Stars [] = v2" by cases (auto simp add: Posix1)
|
|
542 |
qed
|
|
543 |
|
|
544 |
|
|
545 |
text {*
|
272
|
546 |
Our POSIX value is a lexical value.
|
266
|
547 |
*}
|
|
548 |
|
268
|
549 |
lemma Posix_LV:
|
266
|
550 |
assumes "s \<in> r \<rightarrow> v"
|
268
|
551 |
shows "v \<in> LV r s"
|
272
|
552 |
using assms unfolding LV_def
|
266
|
553 |
apply(induct rule: Posix.induct)
|
272
|
554 |
apply(auto simp add: intro!: Prf.intros elim!: Prf_elims)
|
266
|
555 |
done
|
|
556 |
|
|
557 |
end |