progs/Matcher.thy
author Christian Urban <urbanc@in.tum.de>
Thu, 26 Sep 2019 10:59:52 +0100
changeset 632 b7b91158eded
parent 495 7d9d86dc7aa0
child 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
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
section {* Regular Expressions *}
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
datatype rexp =
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
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
| CHAR char
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
| SEQ rexp rexp
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
| ALT rexp rexp
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
| STAR rexp
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
section {* Sequential Composition of Sets *}
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
definition
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  Seq :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
where 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
text {* Two Simple Properties about Sequential Composition *}
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
lemma seq_empty [simp]:
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  shows "A ;; {[]} = A"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  and   "{[]} ;; A = A"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
by (simp_all add: Seq_def)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
lemma seq_null [simp]:
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  shows "A ;; {} = {}"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  and   "{} ;; A = {}"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
by (simp_all add: Seq_def)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
section {* Kleene Star for Sets *}
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
inductive_set
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  for A :: "string set"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
where
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  start[intro]: "[] \<in> A\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
| step[intro]:  "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
text {* A Standard Property of Star *}
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
lemma star_cases:
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  shows "A\<star> = {[]} \<union> A ;; A\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
unfolding Seq_def
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
by (auto) (metis Star.simps)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
lemma star_decomp: 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  assumes a: "c # x \<in> A\<star>" 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  shows "\<exists>a b. x = a @ b \<and> c # a \<in> A \<and> b \<in> A\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
using a
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
by (induct x\<equiv>"c # x" rule: Star.induct) 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
   (auto simp add: append_eq_Cons_conv)
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
section {* Semantics of Regular Expressions *}
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
 
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
fun
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  L :: "rexp \<Rightarrow> string set"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
where
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) = {[]}"
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
| "L (CHAR c) = {[c]}"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
| "L (STAR r) = (L r)\<star>"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
section {* The Matcher *}
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
fun
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
 nullable :: "rexp \<Rightarrow> bool"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
where
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"
167
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
| "nullable (CHAR c) = False"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
| "nullable (STAR r) = True"
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 167
diff changeset
    85
495
7d9d86dc7aa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    86
section {* Correctness Proof for Nullable *}
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
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
cfba674a8fdf added matcher
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
end