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