progs/Matcher.thy
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 10 Sep 2023 12:24:55 +0100
changeset 917 89e05a230d2d
parent 882 5fcad75ade92
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory Matcher
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  imports "Main" 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 167
diff changeset
     5
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
     6
section \<open>Regular Expressions\<close>
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
datatype rexp =
495
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     9
  ZERO
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    10
| ONE
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    11
| CH char
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
| SEQ rexp rexp
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
| ALT rexp rexp
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
| STAR rexp
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    17
section \<open>Sequential Composition of Sets of Strings\<close>
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
definition
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  Seq :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
where 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    24
text \<open>Two Simple Properties about Sequential Composition\<close>
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
lemma seq_empty [simp]:
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  shows "A ;; {[]} = A"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  and   "{[]} ;; A = A"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
by (simp_all add: Seq_def)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
lemma seq_null [simp]:
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  shows "A ;; {} = {}"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  and   "{} ;; A = {}"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
by (simp_all add: Seq_def)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    36
section \<open>Kleene Star for Sets\<close>
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
inductive_set
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  for A :: "string set"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
where
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  start[intro]: "[] \<in> A\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
| step[intro]:  "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    46
text \<open>A Standard Property of Star\<close>
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
lemma star_cases:
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  shows "A\<star> = {[]} \<union> A ;; A\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
unfolding Seq_def
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
by (auto) (metis Star.simps)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
lemma star_decomp: 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  assumes a: "c # x \<in> A\<star>" 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  shows "\<exists>a b. x = a @ b \<and> c # a \<in> A \<and> b \<in> A\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
using a
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
by (induct x\<equiv>"c # x" rule: Star.induct) 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
   (auto simp add: append_eq_Cons_conv)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    61
section \<open>Meaning of Regular Expressions\<close>
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
fun
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  L :: "rexp \<Rightarrow> string set"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
where
495
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    66
  "L (ZERO) = {}"
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    67
| "L (ONE) = {[]}"
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    68
| "L (CH c) = {[c]}"
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
| "L (STAR r) = (L r)\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    73
section \<open>The Matcher\<close>
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
fun
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
 nullable :: "rexp \<Rightarrow> bool"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
where
495
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    78
  "nullable (ZERO) = False"
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    79
| "nullable (ONE) = True"
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    80
| "nullable (CH c) = False"
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
| "nullable (STAR r) = True"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 167
diff changeset
    85
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
    86
section \<open>Correctness Proof for Nullable\<close>
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
lemma nullable_correctness:
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 167
diff changeset
    90
apply(induct r)
495
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    91
(* ZERO case *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    92
apply(simp only: nullable.simps)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    93
apply(simp only: L.simps)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    94
apply(simp)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    95
(* ONE case *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    96
apply(simp only: nullable.simps)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    97
apply(simp only: L.simps)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    98
apply(simp)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    99
(* CHAR case *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   100
apply(simp only: nullable.simps)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   101
apply(simp only: L.simps)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   102
apply(simp)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   103
prefer 2
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   104
(* ALT case *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   105
apply(simp (no_asm) only: nullable.simps)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   106
apply(simp only:)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   107
apply(simp only: L.simps)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   108
apply(simp)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   109
(* SEQ case *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   110
oops
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
495
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   112
lemma nullable_correctness:
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   113
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   114
apply(induct r)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   115
apply(simp_all)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   116
(* all easy subgoals are proved except the last 2 *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   117
(* where the definition of Seq needs to be unfolded. *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   118
oops
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
495
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   120
lemma nullable_correctness:
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   121
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   122
apply(induct r)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   123
apply(simp_all add: Seq_def)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   124
(* except the star case every thing is proved *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   125
(* we need to use the rule for Star.start *)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   126
oops
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
495
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   128
lemma nullable_correctness:
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   129
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   130
apply(induct r)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   131
apply(simp_all add: Seq_def Star.start)
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   132
done
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   134
section \<open>Derivative Operation\<close>
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   135
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   136
fun der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   137
where
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   138
  "der c (ZERO) = ZERO"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   139
| "der c (ONE) = ZERO"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   140
| "der c (CH d) = (if c = d then ONE else ZERO)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   141
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   142
| "der c (SEQ r1 r2) = (if nullable r1 then ALT (SEQ (der c r1) r2) (der c r2)
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   143
                                       else SEQ (der c r1) r2)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   144
| "der c (STAR r) = SEQ (der c r) (STAR r)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   145
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   146
fun 
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   147
 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   148
where
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   149
  "ders [] r = r"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   150
| "ders (c # s) r = ders s (der c r)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   151
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   152
fun
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   153
  matcher :: "rexp \<Rightarrow> string \<Rightarrow> bool"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   154
where
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   155
  "matcher r s = nullable (ders s r)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   156
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   157
definition
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   158
  Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   159
where
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   160
  "Der c A \<equiv> {s. [c] @ s \<in> A}"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   161
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   162
lemma Der_null [simp]:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   163
  shows "Der c {} = {}"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   164
unfolding Der_def
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   165
by auto
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   166
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   167
lemma Der_empty [simp]:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   168
  shows "Der c {[]} = {}"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   169
unfolding Der_def
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   170
by auto
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   171
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   172
lemma Der_char [simp]:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   173
  shows "Der c {[d]} = (if c = d then {[]} else {})"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   174
unfolding Der_def
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   175
by auto
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   176
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   177
lemma Der_union [simp]:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   178
  shows "Der c (A \<union> B) = Der c A \<union> Der c B"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   179
unfolding Der_def
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   180
by auto
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   181
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   182
lemma Der_insert_nil [simp]:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   183
  shows "Der c (insert [] A) = Der c A"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   184
unfolding Der_def 
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   185
by auto 
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   186
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   187
lemma Der_seq [simp]:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   188
  shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   189
unfolding Der_def Seq_def
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   190
by (auto simp add: Cons_eq_append_conv)
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   191
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   192
lemma Der_star [simp]:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   193
  shows "Der c (A\<star>) = (Der c A) ;; A\<star>"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   194
proof -    
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   195
  have "Der c (A\<star>) = Der c ({[]} \<union> A ;; A\<star>)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   196
    by (simp only: star_cases[symmetric])
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   197
  also have "... = Der c (A ;; A\<star>)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   198
    by (simp only: Der_union Der_empty) (simp)
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   199
  also have "... = (Der c A) ;; A\<star> \<union> (if [] \<in> A then Der c (A\<star>) else {})"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   200
    by simp
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   201
  also have "... =  (Der c A) ;; A\<star>"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   202
    unfolding Seq_def Der_def
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   203
    by (auto dest: star_decomp)
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   204
  finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" .
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   205
qed
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   206
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   207
lemma der_correctness:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   208
  shows "L (der c r) = Der c (L r)"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   209
  apply(induct rule: der.induct) 
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   210
  apply(auto simp add: nullable_correctness)
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   211
  done
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   212
  
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   213
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   214
lemma matcher_correctness:
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   215
  shows "matcher r s \<longleftrightarrow> s \<in> L r"
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   216
by (induct s arbitrary: r)
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   217
   (simp_all add: nullable_correctness der_correctness Der_def)
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   218
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 495
diff changeset
   219
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
end