progs/Matcher2.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 15 Nov 2013 10:29:04 +0000
changeset 193 6518475020fc
parent 191 ff6665581ced
child 194 90796ee3c17a
permissions -rw-r--r--
added
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"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
by (auto simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
lemma seq_Union:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  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
    59
by (auto simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
lemma seq_empty_in [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  "[] \<in> A ;; B \<longleftrightarrow> ([] \<in> A \<and> [] \<in> B)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
by (simp add: Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
section {* Kleene Star for Sets *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
inductive_set
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  for A :: "string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  start[intro]: "[] \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
| 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
    73
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
text {* A Standard Property of Star *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
lemma star_cases:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
  shows "A\<star> = {[]} \<union> A ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
unfolding Seq_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
by (auto) (metis Star.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
lemma star_decomp: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  assumes a: "c # x \<in> A\<star>" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  shows "\<exists>a b. x = a @ b \<and> c # a \<in> A \<and> b \<in> A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
using a
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
by (induct x\<equiv>"c # x" rule: Star.induct) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
   (auto simp add: append_eq_Cons_conv)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
section {* Power for Sets *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
fun 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
  pow :: "string set \<Rightarrow> nat \<Rightarrow> string set" ("_ \<up> _" [101, 102] 101)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
   "A \<up> 0 = {[]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
|  "A \<up> (Suc n) = A ;; (A \<up> n)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
lemma pow_empty [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  shows "[] \<in> A \<up> n \<longleftrightarrow> (n = 0 \<or> [] \<in> A)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
by (induct n) (auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
section {* Semantics of Regular Expressions *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
  L :: "rexp \<Rightarrow> string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  "L (NULL) = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
| "L (EMPTY) = {[]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
| "L (CHAR c) = {[c]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
| "L (STAR r) = (L r)\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
| "L (NOT r) = UNIV - (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
| "L (PLUS r) = (L r) ;; ((L r)\<star>)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
| "L (OPT r) = (L r) \<union> {[]}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
| "L (NTIMES r n) = (L r) \<up> n"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
| "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
   119
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
section {* The Matcher *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
 nullable :: "rexp \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
  "nullable (NULL) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
| "nullable (EMPTY) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
| "nullable (CHAR c) = False"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
| "nullable (STAR r) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
| "nullable (NOT r) = (\<not>(nullable r))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
| "nullable (PLUS r) = (nullable r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
| "nullable (OPT r) = True"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
| "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
   136
| "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
   137
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
 der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  "der c (NULL) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
| "der c (EMPTY) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
| "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
   144
| "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
   145
| "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
   146
| "der c (STAR r) = SEQ (der c r) (STAR r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
| "der c (NOT r) = NOT(der c r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
| "der c (PLUS r) = SEQ (der c r) (STAR r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
| "der c (OPT r) = der c r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
| "der c (NTIMES r 0) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
| "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
   152
| "der c (NMTIMES r 0 0) = NULL"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
| "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
   154
| "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
   155
by pat_completeness auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
termination der 
193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   158
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
   159
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
fun 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
  "ders [] r = r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
| "ders (c # s) r = ders s (der c r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
  matcher :: "rexp \<Rightarrow> string \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
  "matcher r s = nullable (ders s r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
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
section {* Correctness Proof of the Matcher *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
lemma nullable_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
by(induct r) (auto simp add: Seq_def) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
section {* Left-Quotient of a Set *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
definition
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
  Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
  "Der c A \<equiv> {s. [c] @ s \<in> A}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
lemma Der_null [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
  shows "Der c {} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
by 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
lemma Der_empty [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
  shows "Der c {[]} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
lemma Der_char [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
  shows "Der c {[d]} = (if c = d then {[]} else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
by auto
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
lemma Der_union [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
  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
   204
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
by auto
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
lemma Der_insert_nil [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
  shows "Der c (insert [] A) = Der c A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
unfolding Der_def 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
by auto 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
lemma Der_seq [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
  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
   214
unfolding Der_def Seq_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
by (auto simp add: Cons_eq_append_conv)
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
lemma Der_star [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
  shows "Der c (A\<star>) = (Der c A) ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
proof -    
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
  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
   221
    by (simp only: star_cases[symmetric])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
  also have "... = Der c (A ;; A\<star>)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
    by (simp only: Der_union Der_empty) (simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
  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
   225
    by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
  also have "... =  (Der c A) ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
    unfolding Seq_def Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
    by (auto dest: star_decomp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
  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
   230
qed
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_UNIV [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
  "Der c (UNIV - A) = UNIV - Der c A"
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_pow [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
  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
   239
unfolding Der_def 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
by(auto simp add: Cons_eq_append_conv Seq_def)
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_UNION [simp]: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
  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
   244
by (auto simp add: Der_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   246
lemma Suc_Union:
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
  "(\<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
   248
by (metis UN_insert atMost_Suc)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   250
lemma Suc_reduce_Union:
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
  "(\<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
   252
by (metis UN_extend_simps(10) image_Suc_atLeastAtMost)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
lemma der_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
  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
   257
by (induct rule: der.induct) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   258
   (simp_all add: nullable_correctness 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   259
    Suc_Union Suc_reduce_Union seq_Union atLeast0AtMost)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   260
191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
lemma matcher_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
  shows "matcher r s \<longleftrightarrow> s \<in> L r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
by (induct s arbitrary: r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
   (simp_all add: nullable_correctness der_correctness Der_def)
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
end