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