thys4/posix/RegLangs.thy
author Chengsong
Wed, 23 Aug 2023 03:02:31 +0100
changeset 668 3831621d7b14
parent 587 3198605ac648
permissions -rw-r--r--
added technical Overview section, almost done introduction
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     1
theory RegLangs
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     2
  imports Main "HOL-Library.Sublist"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     3
begin
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     4
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     5
section \<open>Sequential Composition of Languages\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     6
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     7
definition
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     8
  Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     9
where 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    10
  "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    11
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    12
text \<open>Two Simple Properties about Sequential Composition\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    13
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    14
lemma Sequ_empty_string [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    15
  shows "A ;; {[]} = A"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    16
  and   "{[]} ;; A = A"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    17
by (simp_all add: Sequ_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    18
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    19
lemma Sequ_empty [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    20
  shows "A ;; {} = {}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    21
  and   "{} ;; A = {}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    22
  by (simp_all add: Sequ_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    23
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    24
lemma concI[simp,intro]: "u : A \<Longrightarrow> v : B \<Longrightarrow> u@v : A ;; B"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    25
by (auto simp add: Sequ_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    26
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    27
lemma concE[elim]: 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    28
assumes "w \<in> A ;; B"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    29
obtains u v where "u \<in> A" "v \<in> B" "w = u@v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    30
using assms by (auto simp: Sequ_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    31
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    32
lemma concI_if_Nil2: "[] \<in> B \<Longrightarrow> xs : A \<Longrightarrow> xs \<in> A ;; B"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    33
by (metis append_Nil2 concI)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    34
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    35
lemma conc_assoc: "(A ;; B) ;; C = A ;; (B ;; C)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    36
by (auto elim!: concE) (simp only: append_assoc[symmetric] concI)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    37
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    38
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    39
text \<open>Language power operations\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    40
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    41
overloading lang_pow == "compow :: nat \<Rightarrow> string set \<Rightarrow> string set"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    42
begin
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    43
  primrec lang_pow :: "nat \<Rightarrow> string set \<Rightarrow> string set" where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    44
  "lang_pow 0 A = {[]}" |
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    45
  "lang_pow (Suc n) A = A ;; (lang_pow n A)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    46
end
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    47
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    48
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    49
lemma conc_pow_comm:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    50
  shows "A ;; (A ^^ n) = (A ^^ n) ;; A"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    51
by (induct n) (simp_all add: conc_assoc[symmetric])
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    52
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    53
lemma lang_pow_add: "A ^^ (n + m) = (A ^^ n) ;; (A ^^ m)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    54
  by (induct n) (auto simp: conc_assoc)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    55
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    56
lemma lang_empty: 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    57
  fixes A::"string set"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    58
  shows "A ^^ 0 = {[]}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    59
  by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    60
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    61
section \<open>Semantic Derivative (Left Quotient) of Languages\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    62
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    63
definition
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    64
  Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    65
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    66
  "Der c A \<equiv> {s. c # s \<in> A}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    67
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    68
definition
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    69
  Ders :: "string \<Rightarrow> string set \<Rightarrow> string set"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    70
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    71
  "Ders s A \<equiv> {s'. s @ s' \<in> A}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    72
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    73
lemma Der_null [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    74
  shows "Der c {} = {}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    75
unfolding Der_def
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    76
by auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    77
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    78
lemma Der_empty [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    79
  shows "Der c {[]} = {}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    80
unfolding Der_def
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    81
by auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    82
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    83
lemma Der_char [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    84
  shows "Der c {[d]} = (if c = d then {[]} else {})"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    85
unfolding Der_def
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    86
by auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    87
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    88
lemma Der_union [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    89
  shows "Der c (A \<union> B) = Der c A \<union> Der c B"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    90
unfolding Der_def
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    91
by auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    92
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    93
lemma Der_Sequ [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    94
  shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    95
unfolding Der_def Sequ_def
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    96
by (auto simp add: Cons_eq_append_conv)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    97
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    98
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    99
section \<open>Kleene Star for Languages\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   100
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   101
inductive_set
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   102
  Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   103
  for A :: "string set"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   104
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   105
  start[intro]: "[] \<in> A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   106
| step[intro]:  "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   107
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   108
(* Arden's lemma *)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   109
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   110
lemma Star_cases:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   111
  shows "A\<star> = {[]} \<union> A ;; A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   112
unfolding Sequ_def
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   113
by (auto) (metis Star.simps)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   114
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   115
lemma Star_decomp: 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   116
  assumes "c # x \<in> A\<star>" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   117
  shows "\<exists>s1 s2. x = s1 @ s2 \<and> c # s1 \<in> A \<and> s2 \<in> A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   118
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   119
by (induct x\<equiv>"c # x" rule: Star.induct) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   120
   (auto simp add: append_eq_Cons_conv)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   121
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   122
lemma Star_Der_Sequ: 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   123
  shows "Der c (A\<star>) \<subseteq> (Der c A) ;; A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   124
unfolding Der_def Sequ_def
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   125
by(auto simp add: Star_decomp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   126
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   127
lemma Der_inter[simp]:   "Der a (A \<inter> B) = Der a A \<inter> Der a B"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   128
  and Der_compl[simp]:   "Der a (-A) = - Der a A"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   129
  and Der_Union[simp]:   "Der a (Union M) = Union(Der a ` M)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   130
  and Der_UN[simp]:      "Der a (UN x:I. S x) = (UN x:I. Der a (S x))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   131
by (auto simp: Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   132
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   133
lemma Der_star[simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   134
  shows "Der c (A\<star>) = (Der c A) ;; A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   135
proof -    
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   136
  have "Der c (A\<star>) = Der c ({[]} \<union> A ;; A\<star>)"  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   137
    by (simp only: Star_cases[symmetric])
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   138
  also have "... = Der c (A ;; A\<star>)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   139
    by (simp only: Der_union Der_empty) (simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   140
  also have "... = (Der c A) ;; A\<star> \<union> (if [] \<in> A then Der c (A\<star>) else {})"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   141
    by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   142
  also have "... =  (Der c A) ;; A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   143
    using Star_Der_Sequ by auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   144
  finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" .
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   145
qed
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   146
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   147
lemma Der_pow[simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   148
  shows "Der c (A ^^ n) = (if n = 0 then {} else (Der c A) ;; (A ^^ (n - 1)))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   149
  apply(induct n arbitrary: A)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   150
   apply(auto simp add: Cons_eq_append_conv)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   151
  by (metis Suc_pred concI_if_Nil2 conc_assoc conc_pow_comm lang_pow.simps(2))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   152
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   153
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   154
lemma Star_concat:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   155
  assumes "\<forall>s \<in> set ss. s \<in> A"  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   156
  shows "concat ss \<in> A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   157
using assms by (induct ss) (auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   158
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   159
lemma Star_split:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   160
  assumes "s \<in> A\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   161
  shows "\<exists>ss. concat ss = s \<and> (\<forall>s \<in> set ss. s \<in> A \<and> s \<noteq> [])"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   162
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   163
  apply(induct rule: Star.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   164
  using concat.simps(1) apply fastforce
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   165
  apply(clarify)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   166
  by (metis append_Nil concat.simps(2) set_ConsD)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   167
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   168
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   169
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   170
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   171
section \<open>Regular Expressions\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   172
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   173
datatype rexp =
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   174
  ZERO
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   175
| ONE
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   176
| CH char
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   177
| SEQ rexp rexp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   178
| ALT rexp rexp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   179
| STAR rexp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   180
| NTIMES rexp nat
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   181
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   182
section \<open>Semantics of Regular Expressions\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   183
 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   184
fun
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   185
  L :: "rexp \<Rightarrow> string set"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   186
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   187
  "L (ZERO) = {}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   188
| "L (ONE) = {[]}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   189
| "L (CH c) = {[c]}"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   190
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   191
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   192
| "L (STAR r) = (L r)\<star>"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   193
| "L (NTIMES r n) = (L r) ^^ n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   194
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   195
section \<open>Nullable, Derivatives\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   196
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   197
fun
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   198
 nullable :: "rexp \<Rightarrow> bool"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   199
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   200
  "nullable (ZERO) = False"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   201
| "nullable (ONE) = True"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   202
| "nullable (CH c) = False"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   203
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   204
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   205
| "nullable (STAR r) = True"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   206
| "nullable (NTIMES r n) = (if n = 0 then True else nullable r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   207
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   208
fun
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   209
 der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   210
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   211
  "der c (ZERO) = ZERO"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   212
| "der c (ONE) = ZERO"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   213
| "der c (CH d) = (if c = d then ONE else ZERO)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   214
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   215
| "der c (SEQ r1 r2) = 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   216
     (if nullable r1
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   217
      then ALT (SEQ (der c r1) r2) (der c r2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   218
      else SEQ (der c r1) r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   219
| "der c (STAR r) = SEQ (der c r) (STAR r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   220
| "der c (NTIMES r n) = (if n = 0 then ZERO else SEQ (der c r) (NTIMES r (n - 1)))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   221
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   222
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   223
fun 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   224
 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   225
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   226
  "ders [] r = r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   227
| "ders (c # s) r = ders s (der c r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   228
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   229
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   230
lemma pow_empty_iff:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   231
  shows "[] \<in> (L r) ^^ n \<longleftrightarrow> (if n = 0 then True else [] \<in> (L r))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   232
  by (induct n) (auto simp add: Sequ_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   233
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   234
lemma nullable_correctness:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   235
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   236
  by (induct r) (auto simp add: Sequ_def pow_empty_iff) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   237
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   238
lemma der_correctness:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   239
  shows "L (der c r) = Der c (L r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   240
  apply (induct r) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   241
        apply(auto simp add: nullable_correctness Sequ_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   242
  using Der_def apply force
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   243
  using Der_def apply auto[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   244
  apply (smt (verit, ccfv_SIG) Der_def append_eq_Cons_conv mem_Collect_eq)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   245
  using Der_def apply force
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   246
  using Der_Sequ Sequ_def by auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   247
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   248
lemma ders_correctness:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   249
  shows "L (ders s r) = Ders s (L r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   250
  by (induct s arbitrary: r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   251
     (simp_all add: Ders_def der_correctness Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   252
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   253
lemma ders_append:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   254
  shows "ders (s1 @ s2) r = ders s2 (ders s1 r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   255
  by (induct s1 arbitrary: s2 r) (auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   256
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   257
lemma ders_snoc:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   258
  shows "ders (s @ [c]) r = der c (ders s r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   259
  by (simp add: ders_append)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   260
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   261
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   262
end