progs/MatcherNot.thy
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 21 Oct 2020 14:55:20 +0100
changeset 786 da2488db453e
parent 209 ad9b08267fa4
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
209
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory MatcherNot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  imports "Main" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
section {* Regular Expressions *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
datatype rexp =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  NULL
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
| EMPTY
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
| CHAR char
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
| SEQ rexp rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
| ALT rexp rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
| STAR rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
| NOT rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
section {* Sequential Composition of Sets *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
definition
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  Seq :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
where 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
text {* Two Simple Properties about Sequential Composition *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
lemma seq_empty [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  shows "A ;; {[]} = A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  and   "{[]} ;; A = A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
by (simp_all add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
lemma seq_null [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  shows "A ;; {} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  and   "{} ;; A = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
by (simp_all add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
section {* Kleene Star for Sets *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
inductive_set
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  for A :: "string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  start[intro]: "[] \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
| step[intro]:  "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
text {* A Standard Property of Star *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
lemma star_cases:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  shows "A\<star> = {[]} \<union> A ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
unfolding Seq_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
by (auto) (metis Star.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
lemma star_decomp: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  assumes a: "c # x \<in> A\<star>" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  shows "\<exists>a b. x = a @ b \<and> c # a \<in> A \<and> b \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
using a
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
by (induct x\<equiv>"c # x" rule: Star.induct) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
   (auto simp add: append_eq_Cons_conv)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
section {* Semantics of Regular Expressions *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  L :: "rexp \<Rightarrow> string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  "L (NULL) = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
| "L (EMPTY) = {[]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
| "L (CHAR c) = {[c]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
| "L (STAR r) = (L r)\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
| "L (NOT r) = UNIV - (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
section {* The Matcher *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
 nullable :: "rexp \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  "nullable (NULL) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
| "nullable (EMPTY) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
| "nullable (CHAR c) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
| "nullable (STAR r) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
| "nullable (NOT r) = (\<not> nullable r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
 noccurs :: "rexp \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  "noccurs (NULL) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
| "noccurs (EMPTY) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
| "noccurs (CHAR c) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
| "noccurs (ALT r1 r2) = (noccurs r1 \<or> noccurs r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
| "noccurs (SEQ r1 r2) = (noccurs r1 \<or> noccurs r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
| "noccurs (STAR r) = (noccurs r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
| "noccurs (NOT r) = (\<not>noccurs r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
lemma
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  "L r = {} \<Longrightarrow> noccurs r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
apply(induct r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
apply(auto simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
oops
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
lemma
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
  "\<not> noccurs r \<Longrightarrow> L r \<noteq> {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
apply(induct r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
apply(auto simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
oops
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
 der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  "der c (NULL) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
| "der c (EMPTY) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
| "der c (CHAR c') = (if c = c' then EMPTY else NULL)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
| "der c (SEQ r1 r2) = ALT (SEQ (der c r1) r2) (if nullable r1 then der c r2 else NULL)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
| "der c (STAR r) = SEQ (der c r) (STAR r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
| "der c (NOT r) = NOT (der c r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
fun 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
  "ders [] r = r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
| "ders (c # s) r = ders s (der c r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  matcher :: "rexp \<Rightarrow> string \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
  "matcher r s = nullable (ders s r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
section {* Correctness Proof of the Matcher *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
lemma nullable_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
by (induct r) (auto simp add: Seq_def) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
section {* Left-Quotient of a Set *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
 zeroable :: "rexp \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
  "zeroable (NULL) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
| "zeroable (EMPTY) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
| "zeroable (CHAR c) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
| "zeroable (ALT r1 r2) = (zeroable r1 \<and> zeroable r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
| "zeroable (SEQ r1 r2) = (zeroable r1 \<or> zeroable r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
| "zeroable (STAR r) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
| "zeroable (NOT r) = ((nullable r))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
lemma zeroable_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
  shows "zeroable r  \<longleftrightarrow>  (L r = {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
apply(induct r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
apply(auto simp add: Seq_def)[6]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
apply(simp add: nullable_correctness)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
apply(auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
by (induct r) (auto simp add: Seq_def) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
section {* Left-Quotient of a Set *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
definition
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
  Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
  "Der c A \<equiv> {s. [c] @ s \<in> A}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
lemma Der_null [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
  shows "Der c {} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
lemma Der_empty [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
  shows "Der c {[]} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
lemma Der_char [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
  shows "Der c {[d]} = (if c = d then {[]} else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
lemma Der_union [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
  shows "Der c (A \<union> B) = Der c A \<union> Der c B"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
lemma Der_seq [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
  shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
unfolding Der_def Seq_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
by (auto simp add: Cons_eq_append_conv)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
lemma Der_star [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
  shows "Der c (A\<star>) = (Der c A) ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
proof -    
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
  have "Der c (A\<star>) = Der c ({[]} \<union> A ;; A\<star>)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
    by (simp only: star_cases[symmetric])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
  also have "... = Der c (A ;; A\<star>)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
    by (simp only: Der_union Der_empty) (simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
  also have "... = (Der c A) ;; A\<star> \<union> (if [] \<in> A then Der c (A\<star>) else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
    by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
  also have "... =  (Der c A) ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
    unfolding Seq_def Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
    by (auto dest: star_decomp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
  finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
lemma der_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
  shows "L (der c r) = Der c (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
by (induct r) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
   (simp_all add: nullable_correctness)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
lemma matcher_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
  shows "matcher r s \<longleftrightarrow> s \<in> L r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
by (induct s arbitrary: r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
   (simp_all add: nullable_correctness der_correctness Der_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
section {* Examples *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
definition 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
  "CHRA \<equiv> CHAR (CHR ''a'')"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
definition 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
  "ALT1 \<equiv> ALT CHRA EMPTY"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
definition 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
  "SEQ3 \<equiv> SEQ (SEQ ALT1 ALT1) ALT1"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
value "matcher SEQ3 ''aaa''"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
value "matcher NULL []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
value "matcher (CHAR (CHR ''a'')) [CHR ''a'']"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
value "matcher (CHAR a) [a,a]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
value "matcher (STAR (CHAR a)) []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
value "matcher (STAR (CHAR a))  [a,a]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
value "matcher (SEQ (CHAR (CHR ''a'')) (SEQ (STAR (CHAR (CHR ''b''))) (CHAR (CHR ''c'')))) ''abbbbc''"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
value "matcher (SEQ (CHAR (CHR ''a'')) (SEQ (STAR (CHAR (CHR ''b''))) (CHAR (CHR ''c'')))) ''abbcbbc''"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
section {* Incorrect Matcher - fun-definition rejected *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
  match :: "rexp list \<Rightarrow> string \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
  "match [] [] = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
| "match [] (c # s) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
| "match (NULL # rs) s = False"  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
| "match (EMPTY # rs) s = match rs s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
| "match (CHAR c # rs) [] = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
| "match (CHAR c # rs) (d # s) = (if c = d then match rs s else False)"         
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
| "match (ALT r1 r2 # rs) s = (match (r1 # rs) s \<or> match (r2 # rs) s)" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
| "match (SEQ r1 r2 # rs) s = match (r1 # r2 # rs) s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
| "match (STAR r # rs) s = (match rs s \<or> match (r # (STAR r) # rs) s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
end