author | urbanc |
Mon, 14 Feb 2011 11:12:01 +0000 | |
changeset 102 | 5fed809d0fc1 |
parent 24 | f72c82bf59e5 |
child 103 | f460d5f75cb5 |
permissions | -rw-r--r-- |
3 | 1 |
theory Matcher |
2 |
imports "Main" |
|
3 |
begin |
|
4 |
||
5 |
||
6 |
section {* Sequential Composition of Sets *} |
|
7 |
||
8 |
fun |
|
9 |
lang_seq :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ; _" [100,100] 100) |
|
10 |
where |
|
11 |
"L1 ; L2 = {s1 @ s2 | s1 s2. s1 \<in> L1 \<and> s2 \<in> L2}" |
|
12 |
||
13 |
||
14 |
section {* Kleene Star for Sets *} |
|
15 |
||
16 |
inductive_set |
|
17 |
Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102) |
|
18 |
for L :: "string set" |
|
19 |
where |
|
20 |
start[intro]: "[] \<in> L\<star>" |
|
21 |
| step[intro]: "\<lbrakk>s1 \<in> L; s2 \<in> L\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> L\<star>" |
|
22 |
||
23 |
||
24 | 24 |
text {* A standard property of Star *} |
3 | 25 |
|
26 |
lemma lang_star_cases: |
|
27 |
shows "L\<star> = {[]} \<union> L ; L\<star>" |
|
28 |
by (auto) (metis Star.simps) |
|
29 |
||
30 |
section {* Regular Expressions *} |
|
31 |
||
32 |
datatype rexp = |
|
33 |
NULL |
|
34 |
| EMPTY |
|
35 |
| CHAR char |
|
36 |
| SEQ rexp rexp |
|
37 |
| ALT rexp rexp |
|
38 |
| STAR rexp |
|
39 |
||
40 |
||
102 | 41 |
definition |
42 |
"DERIV s A \<equiv> {s'. s @ s' \<in> A}" |
|
43 |
||
44 |
definition |
|
45 |
"delta A = (if [] \<in> A then {[]} else {})" |
|
46 |
||
47 |
||
48 |
||
3 | 49 |
section {* Semantics of Regular Expressions *} |
50 |
||
51 |
fun |
|
52 |
L :: "rexp \<Rightarrow> string set" |
|
53 |
where |
|
54 |
"L (NULL) = {}" |
|
55 |
| "L (EMPTY) = {[]}" |
|
56 |
| "L (CHAR c) = {[c]}" |
|
57 |
| "L (SEQ r1 r2) = (L r1) ; (L r2)" |
|
58 |
| "L (ALT r1 r2) = (L r1) \<union> (L r2)" |
|
59 |
| "L (STAR r) = (L r)\<star>" |
|
60 |
||
61 |
||
62 |
section {* The Matcher *} |
|
63 |
||
64 |
fun |
|
65 |
nullable :: "rexp \<Rightarrow> bool" |
|
66 |
where |
|
67 |
"nullable (NULL) = False" |
|
68 |
| "nullable (EMPTY) = True" |
|
69 |
| "nullable (CHAR c) = False" |
|
70 |
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)" |
|
71 |
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)" |
|
72 |
| "nullable (STAR r) = True" |
|
73 |
||
74 |
fun |
|
75 |
der :: "char \<Rightarrow> rexp \<Rightarrow> rexp" |
|
76 |
where |
|
77 |
"der c (NULL) = NULL" |
|
78 |
| "der c (EMPTY) = NULL" |
|
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
79 |
| "der c (CHAR c') = (if c = c' then EMPTY else NULL)" |
3 | 80 |
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)" |
81 |
| "der c (SEQ r1 r2) = ALT (SEQ (der c r1) r2) (if nullable r1 then der c r2 else NULL)" |
|
82 |
| "der c (STAR r) = SEQ (der c r) (STAR r)" |
|
83 |
||
84 |
fun |
|
85 |
derivative :: "string \<Rightarrow> rexp \<Rightarrow> rexp" |
|
86 |
where |
|
87 |
"derivative [] r = r" |
|
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
88 |
| "derivative (c # s) r = derivative s (der c r)" |
3 | 89 |
|
90 |
fun |
|
91 |
matches :: "rexp \<Rightarrow> string \<Rightarrow> bool" |
|
92 |
where |
|
93 |
"matches r s = nullable (derivative s r)" |
|
94 |
||
95 |
||
96 |
section {* Correctness Proof of the Matcher *} |
|
97 |
||
98 |
lemma nullable_correctness: |
|
99 |
shows "nullable r \<longleftrightarrow> [] \<in> L r" |
|
100 |
by (induct r) (auto) |
|
101 |
||
102 |
lemma der_correctness: |
|
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
103 |
shows "s \<in> L (der c r) \<longleftrightarrow> c # s \<in> L r" |
3 | 104 |
proof (induct r arbitrary: s) |
105 |
case (SEQ r1 r2 s) |
|
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
106 |
have ih1: "\<And>s. s \<in> L (der c r1) \<longleftrightarrow> c # s \<in> L r1" by fact |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
107 |
have ih2: "\<And>s. s \<in> L (der c r2) \<longleftrightarrow> c # s \<in> L r2" by fact |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
108 |
show "s \<in> L (der c (SEQ r1 r2)) \<longleftrightarrow> c # s \<in> L (SEQ r1 r2)" |
3 | 109 |
using ih1 ih2 by (auto simp add: nullable_correctness Cons_eq_append_conv) |
110 |
next |
|
111 |
case (STAR r s) |
|
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
112 |
have ih: "\<And>s. s \<in> L (der c r) \<longleftrightarrow> c # s \<in> L r" by fact |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
113 |
show "s \<in> L (der c (STAR r)) \<longleftrightarrow> c # s \<in> L (STAR r)" |
3 | 114 |
proof |
115 |
assume "s \<in> L (der c (STAR r))" |
|
116 |
then have "s \<in> L (der c r) ; L r\<star>" by simp |
|
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
117 |
then have "c # s \<in> L r ; (L r)\<star>" |
3 | 118 |
by (auto simp add: ih Cons_eq_append_conv) |
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
119 |
then have "c # s \<in> (L r)\<star>" using lang_star_cases by auto |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
120 |
then show "c # s \<in> L (STAR r)" by simp |
3 | 121 |
next |
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
122 |
assume "c # s \<in> L (STAR r)" |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
123 |
then have "c # s \<in> (L r)\<star>" by simp |
3 | 124 |
then have "s \<in> L (der c r); (L r)\<star>" |
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
125 |
by (induct x\<equiv>"c # s" rule: Star.induct) |
3 | 126 |
(auto simp add: ih append_eq_Cons_conv) |
127 |
then show "s \<in> L (der c (STAR r))" by simp |
|
128 |
qed |
|
129 |
qed (simp_all) |
|
130 |
||
131 |
lemma matches_correctness: |
|
132 |
shows "matches r s \<longleftrightarrow> s \<in> L r" |
|
133 |
by (induct s arbitrary: r) |
|
134 |
(simp_all add: nullable_correctness der_correctness) |
|
135 |
||
136 |
section {* Examples *} |
|
137 |
||
138 |
value "matches NULL []" |
|
139 |
value "matches (CHAR (CHR ''a'')) [CHR ''a'']" |
|
140 |
value "matches (CHAR a) [a,a]" |
|
141 |
value "matches (STAR (CHAR a)) []" |
|
142 |
value "matches (STAR (CHAR a)) [a,a]" |
|
143 |
value "matches (SEQ (CHAR (CHR ''a'')) (SEQ (STAR (CHAR (CHR ''b''))) (CHAR (CHR ''c'')))) ''abbbbc''" |
|
144 |
value "matches (SEQ (CHAR (CHR ''a'')) (SEQ (STAR (CHAR (CHR ''b''))) (CHAR (CHR ''c'')))) ''abbcbbc''" |
|
145 |
||
146 |
section {* Incorrect Matcher - fun-definition rejected *} |
|
147 |
||
148 |
function |
|
149 |
match :: "rexp list \<Rightarrow> string \<Rightarrow> bool" |
|
150 |
where |
|
151 |
"match [] [] = True" |
|
5
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
152 |
| "match [] (c # s) = False" |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
153 |
| "match (NULL # rs) s = False" |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
154 |
| "match (EMPTY # rs) s = match rs s" |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
155 |
| "match (CHAR c # rs) [] = False" |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
156 |
| "match (CHAR c # rs) (d # s) = (if c = d then match rs s else False)" |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
157 |
| "match (ALT r1 r2 # rs) s = (match (r1 # rs) s \<or> match (r2 # rs) s)" |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
158 |
| "match (SEQ r1 r2 # rs) s = match (r1 # r2 # rs) s" |
074d9a4b2bc9
added a file about the easy closure properties of regular sets (the difficult parts, like complement, are missing)
urbanc
parents:
3
diff
changeset
|
159 |
| "match (STAR r # rs) s = (match rs s \<or> match (r # (STAR r) # rs) s)" |
3 | 160 |
apply(pat_completeness) |
161 |
apply(auto) |
|
162 |
done |
|
163 |
||
164 |
end |