author | Christian Urban <christian.urban@kcl.ac.uk> |
Sat, 30 Apr 2022 00:50:08 +0100 | |
changeset 496 | f493a20feeb3 |
parent 495 | thys3/RegLangs.thy@f9cdc295ccf7 |
child 563 | c92a41d9c4da |
permissions | -rw-r--r-- |
495
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
theory RegLangs |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
imports Main "HOL-Library.Sublist" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
begin |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
section \<open>Sequential Composition of Languages\<close> |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
definition |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
"A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
text \<open>Two Simple Properties about Sequential Composition\<close> |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
lemma Sequ_empty_string [simp]: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
shows "A ;; {[]} = A" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
and "{[]} ;; A = A" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
by (simp_all add: Sequ_def) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
lemma Sequ_empty [simp]: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
shows "A ;; {} = {}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
and "{} ;; A = {}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
by (simp_all add: Sequ_def) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
section \<open>Semantic Derivative (Left Quotient) of Languages\<close> |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
definition |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
Der :: "char \<Rightarrow> string set \<Rightarrow> string set" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
"Der c A \<equiv> {s. c # s \<in> A}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
definition |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
Ders :: "string \<Rightarrow> string set \<Rightarrow> string set" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
"Ders s A \<equiv> {s'. s @ s' \<in> A}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
lemma Der_null [simp]: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
shows "Der c {} = {}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
unfolding Der_def |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
by auto |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
lemma Der_empty [simp]: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
shows "Der c {[]} = {}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
unfolding Der_def |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
by auto |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
lemma Der_char [simp]: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
shows "Der c {[d]} = (if c = d then {[]} else {})" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
unfolding Der_def |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
by auto |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
lemma Der_union [simp]: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
shows "Der c (A \<union> B) = Der c A \<union> Der c B" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
unfolding Der_def |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
by auto |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
lemma Der_Sequ [simp]: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
unfolding Der_def Sequ_def |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
by (auto simp add: Cons_eq_append_conv) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
section \<open>Kleene Star for Languages\<close> |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
inductive_set |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
for A :: "string set" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
start[intro]: "[] \<in> A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
| step[intro]: "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
(* Arden's lemma *) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
lemma Star_cases: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
shows "A\<star> = {[]} \<union> A ;; A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
unfolding Sequ_def |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
by (auto) (metis Star.simps) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
lemma Star_decomp: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
assumes "c # x \<in> A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
shows "\<exists>s1 s2. x = s1 @ s2 \<and> c # s1 \<in> A \<and> s2 \<in> A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
using assms |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
by (induct x\<equiv>"c # x" rule: Star.induct) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
(auto simp add: append_eq_Cons_conv) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
lemma Star_Der_Sequ: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
shows "Der c (A\<star>) \<subseteq> (Der c A) ;; A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
unfolding Der_def Sequ_def |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
by(auto simp add: Star_decomp) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
lemma Der_star[simp]: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
shows "Der c (A\<star>) = (Der c A) ;; A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
proof - |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
have "Der c (A\<star>) = Der c ({[]} \<union> A ;; A\<star>)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
by (simp only: Star_cases[symmetric]) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
also have "... = Der c (A ;; A\<star>)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
by (simp only: Der_union Der_empty) (simp) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
also have "... = (Der c A) ;; A\<star> \<union> (if [] \<in> A then Der c (A\<star>) else {})" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
by simp |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
also have "... = (Der c A) ;; A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
using Star_Der_Sequ by auto |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" . |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
qed |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
lemma Star_concat: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
assumes "\<forall>s \<in> set ss. s \<in> A" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
shows "concat ss \<in> A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
using assms by (induct ss) (auto) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
lemma Star_split: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
assumes "s \<in> A\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
shows "\<exists>ss. concat ss = s \<and> (\<forall>s \<in> set ss. s \<in> A \<and> s \<noteq> [])" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
using assms |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
apply(induct rule: Star.induct) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
using concat.simps(1) apply fastforce |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
apply(clarify) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
by (metis append_Nil concat.simps(2) set_ConsD) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
section \<open>Regular Expressions\<close> |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
datatype rexp = |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
ZERO |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
| ONE |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
| CH char |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
| SEQ rexp rexp |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
| ALT rexp rexp |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
| STAR rexp |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
section \<open>Semantics of Regular Expressions\<close> |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
fun |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
L :: "rexp \<Rightarrow> string set" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
"L (ZERO) = {}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
| "L (ONE) = {[]}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
| "L (CH c) = {[c]}" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
| "L (SEQ r1 r2) = (L r1) ;; (L r2)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
| "L (ALT r1 r2) = (L r1) \<union> (L r2)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
| "L (STAR r) = (L r)\<star>" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
section \<open>Nullable, Derivatives\<close> |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
fun |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
nullable :: "rexp \<Rightarrow> bool" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
"nullable (ZERO) = False" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
| "nullable (ONE) = True" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
| "nullable (CH c) = False" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
| "nullable (STAR r) = True" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
fun |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
der :: "char \<Rightarrow> rexp \<Rightarrow> rexp" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
"der c (ZERO) = ZERO" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
| "der c (ONE) = ZERO" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
| "der c (CH d) = (if c = d then ONE else ZERO)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
| "der c (SEQ r1 r2) = |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
166 |
(if nullable r1 |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
then ALT (SEQ (der c r1) r2) (der c r2) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
else SEQ (der c r1) r2)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
| "der c (STAR r) = SEQ (der c r) (STAR r)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
fun |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
172 |
ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
"ders [] r = r" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
| "ders (c # s) r = ders s (der c r)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
176 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
lemma nullable_correctness: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
shows "nullable r \<longleftrightarrow> [] \<in> (L r)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
by (induct r) (auto simp add: Sequ_def) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
lemma der_correctness: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
shows "L (der c r) = Der c (L r)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
184 |
by (induct r) (simp_all add: nullable_correctness) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
lemma ders_correctness: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
187 |
shows "L (ders s r) = Ders s (L r)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
188 |
by (induct s arbitrary: r) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
(simp_all add: Ders_def der_correctness Der_def) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
190 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
lemma ders_append: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
192 |
shows "ders (s1 @ s2) r = ders s2 (ders s1 r)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
by (induct s1 arbitrary: s2 r) (auto) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
194 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
lemma ders_snoc: |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
196 |
shows "ders (s @ [c]) r = der c (ders s r)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
197 |
by (simp add: ders_append) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
198 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
(* |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
201 |
datatype ctxt = |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
202 |
SeqC rexp bool |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
203 |
| AltCL rexp |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
204 |
| AltCH rexp |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
205 |
| StarC rexp |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
206 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
207 |
function |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
208 |
down :: "char \<Rightarrow> rexp \<Rightarrow> ctxt list \<Rightarrow> rexp * ctxt list" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
209 |
and up :: "char \<Rightarrow> rexp \<Rightarrow> ctxt list \<Rightarrow> rexp * ctxt list" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
210 |
where |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
211 |
"down c (SEQ r1 r2) ctxts = |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
212 |
(if (nullable r1) then down c r1 (SeqC r2 True # ctxts) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
213 |
else down c r1 (SeqC r2 False # ctxts))" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
214 |
| "down c (CH d) ctxts = |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
215 |
(if c = d then up c ONE ctxts else up c ZERO ctxts)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
216 |
| "down c ONE ctxts = up c ZERO ctxts" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
217 |
| "down c ZERO ctxts = up c ZERO ctxts" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
218 |
| "down c (ALT r1 r2) ctxts = down c r1 (AltCH r2 # ctxts)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
219 |
| "down c (STAR r1) ctxts = down c r1 (StarC r1 # ctxts)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
220 |
| "up c r [] = (r, [])" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
221 |
| "up c r (SeqC r2 False # ctxts) = up c (SEQ r r2) ctxts" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
222 |
| "up c r (SeqC r2 True # ctxts) = down c r2 (AltCL (SEQ r r2) # ctxts)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
223 |
| "up c r (AltCL r1 # ctxts) = up c (ALT r1 r) ctxts" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
224 |
| "up c r (AltCH r2 # ctxts) = down c r2 (AltCL r # ctxts)" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
225 |
| "up c r (StarC r1 # ctxts) = up c (SEQ r (STAR r1)) ctxts" |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
226 |
apply(pat_completeness) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
227 |
apply(auto) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
228 |
done |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
229 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
230 |
termination |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
231 |
sorry |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
232 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
233 |
*) |
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
234 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
235 |
|
f9cdc295ccf7
a fresh directory with cleaned up code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
236 |
end |