progs/Matcher2.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 14 Nov 2013 20:10:06 +0000
changeset 191 ff6665581ced
child 193 6518475020fc
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 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
apply(relation "measure (\<lambda>(c, r). M r)")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
apply(simp_all)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
done
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
fun 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
  "ders [] r = r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
| "ders (c # s) r = ders s (der c r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
fun
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
  matcher :: "rexp \<Rightarrow> string \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
  "matcher r s = nullable (ders s 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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
section {* Correctness Proof of the Matcher *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
lemma nullable_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
by(induct r) (auto simp add: Seq_def) 
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
section {* Left-Quotient of a Set *}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
definition
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
  Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
  "Der c A \<equiv> {s. [c] @ s \<in> A}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
lemma Der_null [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
  shows "Der c {} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
lemma Der_empty [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
  shows "Der c {[]} = {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
lemma Der_char [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
  shows "Der c {[d]} = (if c = d then {[]} else {})"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
lemma Der_union [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
  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
   205
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
by auto
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
lemma Der_insert_nil [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
  shows "Der c (insert [] A) = Der c A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
unfolding Der_def 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
by auto 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
lemma Der_seq [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
  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
   215
unfolding Der_def Seq_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
by (auto simp add: Cons_eq_append_conv)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
lemma Der_star [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
  shows "Der c (A\<star>) = (Der c A) ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
proof -    
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
  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
   222
    by (simp only: star_cases[symmetric])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
  also have "... = Der c (A ;; A\<star>)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
    by (simp only: Der_union Der_empty) (simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
  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
   226
    by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
  also have "... =  (Der c A) ;; A\<star>"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
    unfolding Seq_def Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
    by (auto dest: star_decomp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
  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
   231
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
lemma Der_UNIV [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
  "Der c (UNIV - A) = UNIV - Der c A"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
unfolding Der_def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
by (auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
lemma Der_pow [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
  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
   240
unfolding Der_def 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
by(auto simp add: Cons_eq_append_conv Seq_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
lemma Der_UNION [simp]: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
  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
   246
by (auto simp add: Der_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
lemma test:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
  "(\<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
   250
by (metis UN_insert atMost_Suc)
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 yy:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
  "(\<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
   254
by (metis UN_extend_simps(10) image_Suc_atLeastAtMost)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
lemma uu:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
  "(Suc n) + m = Suc (n + m)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
lemma der_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
  shows "L (der c r) = Der c (L r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
apply(induct rule: der.induct) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
apply(simp_all add: nullable_correctness)[12]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
apply(simp only: L.simps der.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
apply(simp only: Der_UNION)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
apply(simp del: pow.simps Der_pow)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
apply(simp only: atLeast0AtMost)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
apply(simp only: test)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
apply(simp only: L.simps der.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
apply(simp only: Der_UNION)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
apply(simp only: yy add_Suc)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
apply(simp only: seq_Union)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
apply(simp only: Der_UNION)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
apply(simp only: pow.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
done
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 matcher_correctness:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
  shows "matcher r s \<longleftrightarrow> s \<in> L r"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
by (induct s arbitrary: r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
   (simp_all add: nullable_correctness der_correctness Der_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
end