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