thys/RegLangs.thy
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 10 Sep 2022 22:30:22 +0100
changeset 599 a5f666410101
parent 365 ec5e4fe4cc70
permissions -rw-r--r--
updated paper
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
311
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
     1
theory RegLangs
362
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
     2
  imports Main "HOL-Library.Sublist"
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
     5
section \<open>Sequential Composition of Languages\<close>
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
definition
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
where 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    12
text \<open>Two Simple Properties about Sequential Composition\<close>
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
lemma Sequ_empty_string [simp]:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  shows "A ;; {[]} = A"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  and   "{[]} ;; A = A"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
by (simp_all add: Sequ_def)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
lemma Sequ_empty [simp]:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  shows "A ;; {} = {}"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  and   "{} ;; A = {}"
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 314
diff changeset
    22
  by (simp_all add: Sequ_def)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 314
diff changeset
    23
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    25
section \<open>Semantic Derivative (Left Quotient) of Languages\<close>
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
definition
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
where
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  "Der c A \<equiv> {s. c # s \<in> A}"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
definition
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  Ders :: "string \<Rightarrow> string set \<Rightarrow> string set"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
where
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  "Ders s A \<equiv> {s'. s @ s' \<in> A}"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
lemma Der_null [simp]:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  shows "Der c {} = {}"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
unfolding Der_def
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
by auto
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
lemma Der_empty [simp]:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  shows "Der c {[]} = {}"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
unfolding Der_def
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
by auto
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
lemma Der_char [simp]:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  shows "Der c {[d]} = (if c = d then {[]} else {})"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
unfolding Der_def
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
by auto
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
lemma Der_union [simp]:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  shows "Der c (A \<union> B) = Der c A \<union> Der c B"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
unfolding Der_def
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
by auto
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
lemma Der_Sequ [simp]:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
unfolding Der_def Sequ_def
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
by (auto simp add: Cons_eq_append_conv)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    63
section \<open>Kleene Star for Languages\<close>
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
inductive_set
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  for A :: "string set"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
where
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  start[intro]: "[] \<in> A\<star>"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
| step[intro]:  "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
(* Arden's lemma *)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
lemma Star_cases:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  shows "A\<star> = {[]} \<union> A ;; A\<star>"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
unfolding Sequ_def
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
by (auto) (metis Star.simps)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
lemma Star_decomp: 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  assumes "c # x \<in> A\<star>" 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  shows "\<exists>s1 s2. x = s1 @ s2 \<and> c # s1 \<in> A \<and> s2 \<in> A\<star>"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
using assms
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
by (induct x\<equiv>"c # x" rule: Star.induct) 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
   (auto simp add: append_eq_Cons_conv)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
lemma Star_Der_Sequ: 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  shows "Der c (A\<star>) \<subseteq> (Der c A) ;; A\<star>"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
unfolding Der_def Sequ_def
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
by(auto simp add: Star_decomp)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    92
lemma Der_star[simp]:
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  shows "Der c (A\<star>) = (Der c A) ;; A\<star>"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
proof -    
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  have "Der c (A\<star>) = Der c ({[]} \<union> A ;; A\<star>)"  
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
    by (simp only: Star_cases[symmetric])
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  also have "... = Der c (A ;; A\<star>)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
    by (simp only: Der_union Der_empty) (simp)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  also have "... = (Der c A) ;; A\<star> \<union> (if [] \<in> A then Der c (A\<star>) else {})"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
    by simp
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  also have "... =  (Der c A) ;; A\<star>"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    using Star_Der_Sequ by auto
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" .
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
qed
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
311
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   106
lemma Star_concat:
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   107
  assumes "\<forall>s \<in> set ss. s \<in> A"  
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   108
  shows "concat ss \<in> A\<star>"
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   109
using assms by (induct ss) (auto)
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   110
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   111
lemma Star_split:
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   112
  assumes "s \<in> A\<star>"
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   113
  shows "\<exists>ss. concat ss = s \<and> (\<forall>s \<in> set ss. s \<in> A \<and> s \<noteq> [])"
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   114
using assms
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   115
  apply(induct rule: Star.induct)
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   116
  using concat.simps(1) apply fastforce
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   117
  apply(clarify)
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   118
  by (metis append_Nil concat.simps(2) set_ConsD)
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   119
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   120
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   122
section \<open>Regular Expressions\<close>
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
datatype rexp =
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  ZERO
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
| ONE
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   127
| CH char
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
| SEQ rexp rexp
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
| ALT rexp rexp
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
| STAR rexp
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   132
section \<open>Semantics of Regular Expressions\<close>
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
fun
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  L :: "rexp \<Rightarrow> string set"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
where
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  "L (ZERO) = {}"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
| "L (ONE) = {[]}"
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   139
| "L (CH c) = {[c]}"
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
| "L (STAR r) = (L r)\<star>"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   145
section \<open>Nullable, Derivatives\<close>
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
fun
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
 nullable :: "rexp \<Rightarrow> bool"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
where
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  "nullable (ZERO) = False"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
| "nullable (ONE) = True"
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   152
| "nullable (CH c) = False"
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
| "nullable (STAR r) = True"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
fun
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
 der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
where
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
  "der c (ZERO) = ZERO"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
| "der c (ONE) = ZERO"
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   163
| "der c (CH d) = (if c = d then ONE else ZERO)"
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
| "der c (SEQ r1 r2) = 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
     (if nullable r1
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
      then ALT (SEQ (der c r1) r2) (der c r2)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
      else SEQ (der c r1) r2)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
| "der c (STAR r) = SEQ (der c r) (STAR r)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
fun 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
where
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  "ders [] r = r"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
| "ders (c # s) r = ders s (der c r)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
lemma nullable_correctness:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
by (induct r) (auto simp add: Sequ_def) 
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
lemma der_correctness:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  shows "L (der c r) = Der c (L r)"
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
by (induct r) (simp_all add: nullable_correctness)
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
lemma ders_correctness:
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  shows "L (ders s r) = Ders s (L r)"
311
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   188
  by (induct s arbitrary: r)
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   189
     (simp_all add: Ders_def der_correctness Der_def)
267
32b222d77fa0 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   190
287
95b3880d428f updated
Christian Urban <urbanc@in.tum.de>
parents: 286
diff changeset
   191
lemma ders_append:
95b3880d428f updated
Christian Urban <urbanc@in.tum.de>
parents: 286
diff changeset
   192
  shows "ders (s1 @ s2) r = ders s2 (ders s1 r)"
311
8b8db9558ecf updated
Christian Urban <urbanc@in.tum.de>
parents: 295
diff changeset
   193
  by (induct s1 arbitrary: s2 r) (auto)
268
6746f5e1f1f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 267
diff changeset
   194
314
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents: 311
diff changeset
   195
lemma ders_snoc:
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents: 311
diff changeset
   196
  shows "ders (s @ [c]) r = der c (ders s r)"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents: 311
diff changeset
   197
  by (simp add: ders_append)
268
6746f5e1f1f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 267
diff changeset
   198
362
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   199
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   200
(*
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   201
datatype ctxt = 
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   202
    SeqC rexp bool
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   203
  | AltCL rexp
365
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents: 362
diff changeset
   204
  | AltCH rexp 
362
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   205
  | StarC rexp 
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   206
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   207
function
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   208
     down :: "char \<Rightarrow> rexp \<Rightarrow> ctxt list \<Rightarrow> rexp * ctxt list"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   209
and  up :: "char \<Rightarrow> rexp \<Rightarrow> ctxt list \<Rightarrow> rexp * ctxt list"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   210
where
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   211
  "down c (SEQ r1 r2) ctxts =
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   212
     (if (nullable r1) then down c r1 (SeqC r2 True # ctxts) 
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   213
      else down c r1 (SeqC r2 False # ctxts))"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   214
| "down c (CH d) ctxts = 
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   215
     (if c = d then up c ONE ctxts else up c ZERO ctxts)"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   216
| "down c ONE ctxts = up c ZERO ctxts"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   217
| "down c ZERO ctxts = up c ZERO ctxts"
365
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents: 362
diff changeset
   218
| "down c (ALT r1 r2) ctxts = down c r1 (AltCH r2 # ctxts)"
362
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   219
| "down c (STAR r1) ctxts = down c r1 (StarC r1 # ctxts)"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   220
| "up c r [] = (r, [])"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   221
| "up c r (SeqC r2 False # ctxts) = up c (SEQ r r2) ctxts"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   222
| "up c r (SeqC r2 True # ctxts) = down c r2 (AltCL (SEQ r r2) # ctxts)"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   223
| "up c r (AltCL r1 # ctxts) = up c (ALT r1 r) ctxts"
365
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents: 362
diff changeset
   224
| "up c r (AltCH r2 # ctxts) = down c r2 (AltCL r # ctxts)"
362
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   225
| "up c r (StarC r1 # ctxts) = up c (SEQ r (STAR r1)) ctxts"
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   226
  apply(pat_completeness)
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   227
  apply(auto)
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   228
  done
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   229
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   230
termination
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   231
  sorry
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   232
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   233
*)
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   234
e51c9a67a68d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 361
diff changeset
   235
266
fff2e1b40dfc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
end