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