progs/Matcher2.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 01 Dec 2013 10:08:53 +0000
changeset 216 f5ec7c597c5b
parent 198 f54972b0f641
child 227 93bd75031ced
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory Matcher2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  imports "Main" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
section {* Regular Expressions *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
datatype rexp =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
  NULL
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
| EMPTY
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
| CHAR char
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
| SEQ rexp rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
| ALT rexp rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
| STAR rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
| NOT rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
| PLUS rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
| OPT rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
| NTIMES rexp nat
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
| NMTIMES rexp nat nat
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
fun M :: "rexp \<Rightarrow> nat"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  "M (NULL) = 0"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
| "M (EMPTY) = 0"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
| "M (CHAR char) = 0"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
| "M (SEQ r1 r2) = Suc ((M r1) + (M r2))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
| "M (ALT r1 r2) = Suc ((M r1) + (M r2))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
| "M (STAR r) = Suc (M r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
| "M (NOT r) = Suc (M r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
| "M (PLUS r) = Suc (M r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
| "M (OPT r) = Suc (M r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
| "M (NTIMES r n) = Suc (M r) * 2 * (Suc n)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
| "M (NMTIMES r n m) = Suc (M r) * 2 * (Suc n + Suc m)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
section {* Sequential Composition of Sets *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
definition
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  Seq :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
where 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
text {* Two Simple Properties about Sequential Composition *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
lemma seq_empty [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  shows "A ;; {[]} = A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  and   "{[]} ;; A = A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
by (simp_all add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
lemma seq_null [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  shows "A ;; {} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  and   "{} ;; A = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
by (simp_all add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
lemma seq_union:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  shows "A ;; (B \<union> C) = A ;; B \<union> A ;; C"
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    55
  and   "(B \<union> C) ;; A = B ;; A \<union> C ;; A"
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
by (auto simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
lemma seq_Union:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  shows "A ;; (\<Union>x\<in>B. C x) = (\<Union>x\<in>B. A ;; C x)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
by (auto simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
lemma seq_empty_in [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  "[] \<in> A ;; B \<longleftrightarrow> ([] \<in> A \<and> [] \<in> B)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
by (simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    66
lemma seq_assoc: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    67
  shows "A ;; (B ;; C) = (A ;; B) ;; C" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    68
apply(auto simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    69
apply(metis append_assoc)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    70
apply(metis)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    71
done
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    72
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    73
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    74
section {* Power for Sets *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    76
fun 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    77
  pow :: "string set \<Rightarrow> nat \<Rightarrow> string set" ("_ \<up> _" [101, 102] 101)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    78
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    79
   "A \<up> 0 = {[]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    80
|  "A \<up> (Suc n) = A ;; (A \<up> n)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    81
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    82
lemma pow_empty [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    83
  shows "[] \<in> A \<up> n \<longleftrightarrow> (n = 0 \<or> [] \<in> A)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    84
by (induct n) (auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    85
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    86
lemma pow_plus:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    87
  "A \<up> (n + m) = A \<up> n ;; A \<up> m"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    88
by (induct n) (simp_all add: seq_assoc)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    89
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
section {* Kleene Star for Sets *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
inductive_set
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  for A :: "string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  start[intro]: "[] \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
| step[intro]:  "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
text {* A Standard Property of Star *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
lemma star_decomp: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  assumes a: "c # x \<in> A\<star>" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  shows "\<exists>a b. x = a @ b \<and> c # a \<in> A \<and> b \<in> A\<star>"
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   104
using a 
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
using a
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
by (induct x\<equiv>"c # x" rule: Star.induct) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
   (auto simp add: append_eq_Cons_conv)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   109
lemma star_cases:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   110
  shows "A\<star> = {[]} \<union> A ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   111
unfolding Seq_def 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   112
by (auto) (metis Star.simps)
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   114
lemma Star_in_Pow:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   115
  assumes a: "s \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   116
  shows "\<exists>n. s \<in> A \<up> n"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   117
using a
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   118
apply(induct)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   119
apply(auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   120
apply(rule_tac x="Suc n" in exI)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   121
apply(auto simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   122
done
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   124
lemma Pow_in_Star:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   125
  assumes a: "s \<in> A \<up> n"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   126
  shows "s \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   127
using a
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   128
by (induct n arbitrary: s) (auto simp add: Seq_def)
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   131
lemma Star_def2: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   132
  shows "A\<star> = (\<Union>n. A \<up> n)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   133
using Star_in_Pow Pow_in_Star
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   134
by (auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
   135
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
section {* Semantics of Regular Expressions *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  L :: "rexp \<Rightarrow> string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
  "L (NULL) = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
| "L (EMPTY) = {[]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
| "L (CHAR c) = {[c]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
| "L (STAR r) = (L r)\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
| "L (NOT r) = UNIV - (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
| "L (PLUS r) = (L r) ;; ((L r)\<star>)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
| "L (OPT r) = (L r) \<union> {[]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
| "L (NTIMES r n) = (L r) \<up> n"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
| "L (NMTIMES r n m) = (\<Union>i\<in> {n..n+m} . ((L r) \<up> i))" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
section {* The Matcher *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
 nullable :: "rexp \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
  "nullable (NULL) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
| "nullable (EMPTY) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
| "nullable (CHAR c) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
| "nullable (STAR r) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
| "nullable (NOT r) = (\<not>(nullable r))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
| "nullable (PLUS r) = (nullable r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
| "nullable (OPT r) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
| "nullable (NTIMES r n) = (if n = 0 then True else nullable r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
| "nullable (NMTIMES r n m) = (if n = 0 then True else nullable r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
 der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
  "der c (NULL) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
| "der c (EMPTY) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
| "der c (CHAR d) = (if c = d then EMPTY else NULL)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
| "der c (SEQ r1 r2) = ALT (SEQ (der c r1) r2) (if nullable r1 then der c r2 else NULL)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
| "der c (STAR r) = SEQ (der c r) (STAR r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
| "der c (NOT r) = NOT(der c r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
| "der c (PLUS r) = SEQ (der c r) (STAR r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
| "der c (OPT r) = der c r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
| "der c (NTIMES r 0) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
| "der c (NTIMES r (Suc n)) = der c (SEQ r (NTIMES r n))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
| "der c (NMTIMES r 0 0) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
| "der c (NMTIMES r 0 (Suc m)) = ALT (der c (NTIMES r (Suc m))) (der c (NMTIMES r 0 m))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
| "der c (NMTIMES r (Suc n) m) = der c  (SEQ r (NMTIMES r n m))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
by pat_completeness auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
termination der 
193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   193
by (relation "measure (\<lambda>(c, r). M r)") (simp_all)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   194
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
fun 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
  "ders [] r = r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
| "ders (c # s) r = ders s (der c r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
  matcher :: "rexp \<Rightarrow> string \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
  "matcher r s = nullable (ders s r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
section {* Correctness Proof of the Matcher *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
lemma nullable_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
by(induct r) (auto simp add: Seq_def) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
section {* Left-Quotient of a Set *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
definition
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
  Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
  "Der c A \<equiv> {s. [c] @ s \<in> A}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
lemma Der_null [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
  shows "Der c {} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
lemma Der_empty [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
  shows "Der c {[]} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
lemma Der_char [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
  shows "Der c {[d]} = (if c = d then {[]} else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
lemma Der_union [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
  shows "Der c (A \<union> B) = Der c A \<union> Der c B"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
lemma Der_insert_nil [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
  shows "Der c (insert [] A) = Der c A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
unfolding Der_def 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
by auto 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
lemma Der_seq [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
  shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
unfolding Der_def Seq_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
by (auto simp add: Cons_eq_append_conv)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
lemma Der_star [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
  shows "Der c (A\<star>) = (Der c A) ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
proof -    
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
  have "Der c (A\<star>) = Der c ({[]} \<union> A ;; A\<star>)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
    by (simp only: star_cases[symmetric])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
  also have "... = Der c (A ;; A\<star>)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
    by (simp only: Der_union Der_empty) (simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
  also have "... = (Der c A) ;; A\<star> \<union> (if [] \<in> A then Der c (A\<star>) else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
    by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
  also have "... =  (Der c A) ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
    unfolding Seq_def Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
    by (auto dest: star_decomp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
  finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
lemma Der_UNIV [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
  "Der c (UNIV - A) = UNIV - Der c A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
by (auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
lemma Der_pow [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
  shows "Der c (A \<up> (Suc n)) = (Der c A) ;; (A \<up> n) \<union> (if [] \<in> A then Der c (A \<up> n) else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
unfolding Der_def 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
by(auto simp add: Cons_eq_append_conv Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
lemma Der_UNION [simp]: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
  shows "Der c (\<Union>x\<in>A. B x) = (\<Union>x\<in>A. Der c (B x))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
by (auto simp add: Der_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   281
lemma Suc_Union:
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
  "(\<Union> x\<le>Suc m. B x) = (B (Suc m) \<union> (\<Union> x\<le>m. B x))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
by (metis UN_insert atMost_Suc)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   285
lemma Suc_reduce_Union:
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
  "(\<Union>x\<in>{Suc n..Suc m}. B x) = (\<Union>x\<in>{n..m}. B (Suc x))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
by (metis UN_extend_simps(10) image_Suc_atLeastAtMost)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
lemma der_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
  shows "L (der c r) = Der c (L r)"
193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   292
by (induct rule: der.induct) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   293
   (simp_all add: nullable_correctness 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   294
    Suc_Union Suc_reduce_Union seq_Union atLeast0AtMost)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   295
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
lemma matcher_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
  shows "matcher r s \<longleftrightarrow> s \<in> L r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
by (induct s arbitrary: r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
   (simp_all add: nullable_correctness der_correctness Der_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
198
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 196
diff changeset
   302
`
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   303
end