thys2/SizeBound4CT.thy
author Chengsong
Fri, 04 Feb 2022 00:05:12 +0000
changeset 409 f71df68776bb
parent 406 4511cc1bf1f0
permissions -rw-r--r--
5ct
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
406
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     1
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     2
theory SizeBound4CT
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     3
  imports 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     4
  "Lexer"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     5
  "PDerivs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     6
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     7
begin
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     8
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
     9
section \<open>Bit-Encodings\<close>
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    10
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    11
datatype bit = Z | S
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    12
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    13
fun code :: "val \<Rightarrow> bit list"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    14
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    15
  "code Void = []"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    16
| "code (Char c) = []"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    17
| "code (Left v) = Z # (code v)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    18
| "code (Right v) = S # (code v)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    19
| "code (Seq v1 v2) = (code v1) @ (code v2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    20
| "code (Stars []) = [S]"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    21
| "code (Stars (v # vs)) =  (Z # code v) @ code (Stars vs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    22
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    23
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    24
fun 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    25
  Stars_add :: "val \<Rightarrow> val \<Rightarrow> val"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    26
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    27
  "Stars_add v (Stars vs) = Stars (v # vs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    28
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    29
function
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    30
  decode' :: "bit list \<Rightarrow> rexp \<Rightarrow> (val * bit list)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    31
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    32
  "decode' ds ZERO = (Void, [])"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    33
| "decode' ds ONE = (Void, ds)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    34
| "decode' ds (CH d) = (Char d, ds)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    35
| "decode' [] (ALT r1 r2) = (Void, [])"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    36
| "decode' (Z # ds) (ALT r1 r2) = (let (v, ds') = decode' ds r1 in (Left v, ds'))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    37
| "decode' (S # ds) (ALT r1 r2) = (let (v, ds') = decode' ds r2 in (Right v, ds'))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    38
| "decode' ds (SEQ r1 r2) = (let (v1, ds') = decode' ds r1 in
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    39
                             let (v2, ds'') = decode' ds' r2 in (Seq v1 v2, ds''))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    40
| "decode' [] (STAR r) = (Void, [])"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    41
| "decode' (S # ds) (STAR r) = (Stars [], ds)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    42
| "decode' (Z # ds) (STAR r) = (let (v, ds') = decode' ds r in
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    43
                                    let (vs, ds'') = decode' ds' (STAR r) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    44
                                    in (Stars_add v vs, ds''))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    45
by pat_completeness auto
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    46
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    47
lemma decode'_smaller:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    48
  assumes "decode'_dom (ds, r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    49
  shows "length (snd (decode' ds r)) \<le> length ds"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    50
using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    51
apply(induct ds r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    52
apply(auto simp add: decode'.psimps split: prod.split)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    53
using dual_order.trans apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    54
by (meson dual_order.trans le_SucI)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    55
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    56
termination "decode'"  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    57
apply(relation "inv_image (measure(%cs. size cs) <*lex*> measure(%s. size s)) (%(ds,r). (r,ds))") 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    58
apply(auto dest!: decode'_smaller)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    59
by (metis less_Suc_eq_le snd_conv)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    60
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    61
definition
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    62
  decode :: "bit list \<Rightarrow> rexp \<Rightarrow> val option"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    63
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    64
  "decode ds r \<equiv> (let (v, ds') = decode' ds r 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    65
                  in (if ds' = [] then Some v else None))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    66
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    67
lemma decode'_code_Stars:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    68
  assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> (\<forall>x. decode' (code v @ x) r = (v, x)) \<and> flat v \<noteq> []" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    69
  shows "decode' (code (Stars vs) @ ds) (STAR r) = (Stars vs, ds)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    70
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    71
  apply(induct vs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    72
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    73
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    74
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    75
lemma decode'_code:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    76
  assumes "\<Turnstile> v : r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    77
  shows "decode' ((code v) @ ds) r = (v, ds)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    78
using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    79
  apply(induct v r arbitrary: ds) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    80
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    81
  using decode'_code_Stars by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    82
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    83
lemma decode_code:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    84
  assumes "\<Turnstile> v : r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    85
  shows "decode (code v) r = Some v"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    86
  using assms unfolding decode_def
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    87
  by (smt append_Nil2 decode'_code old.prod.case)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    88
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    89
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    90
section {* Annotated Regular Expressions *}
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    91
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    92
datatype arexp = 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    93
  AZERO
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    94
| AONE "bit list"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    95
| ACHAR "bit list" char
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    96
| ASEQ "bit list" arexp arexp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    97
| AALTs "bit list" "arexp list"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    98
| ASTAR "bit list" arexp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
    99
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   100
abbreviation
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   101
  "AALT bs r1 r2 \<equiv> AALTs bs [r1, r2]"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   102
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   103
fun asize :: "arexp \<Rightarrow> nat" where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   104
  "asize AZERO = 1"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   105
| "asize (AONE cs) = 1" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   106
| "asize (ACHAR cs c) = 1"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   107
| "asize (AALTs cs rs) = Suc (sum_list (map asize rs))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   108
| "asize (ASEQ cs r1 r2) = Suc (asize r1 + asize r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   109
| "asize (ASTAR cs r) = Suc (asize r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   110
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   111
fun 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   112
  erase :: "arexp \<Rightarrow> rexp"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   113
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   114
  "erase AZERO = ZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   115
| "erase (AONE _) = ONE"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   116
| "erase (ACHAR _ c) = CH c"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   117
| "erase (AALTs _ []) = ZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   118
| "erase (AALTs _ [r]) = (erase r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   119
| "erase (AALTs bs (r#rs)) = ALT (erase r) (erase (AALTs bs rs))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   120
| "erase (ASEQ _ r1 r2) = SEQ (erase r1) (erase r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   121
| "erase (ASTAR _ r) = STAR (erase r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   122
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   123
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   124
fun fuse :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp" where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   125
  "fuse bs AZERO = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   126
| "fuse bs (AONE cs) = AONE (bs @ cs)" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   127
| "fuse bs (ACHAR cs c) = ACHAR (bs @ cs) c"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   128
| "fuse bs (AALTs cs rs) = AALTs (bs @ cs) rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   129
| "fuse bs (ASEQ cs r1 r2) = ASEQ (bs @ cs) r1 r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   130
| "fuse bs (ASTAR cs r) = ASTAR (bs @ cs) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   131
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   132
lemma fuse_append:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   133
  shows "fuse (bs1 @ bs2) r = fuse bs1 (fuse bs2 r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   134
  apply(induct r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   135
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   136
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   137
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   138
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   139
fun intern :: "rexp \<Rightarrow> arexp" where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   140
  "intern ZERO = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   141
| "intern ONE = AONE []"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   142
| "intern (CH c) = ACHAR [] c"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   143
| "intern (ALT r1 r2) = AALT [] (fuse [Z] (intern r1)) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   144
                                (fuse [S]  (intern r2))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   145
| "intern (SEQ r1 r2) = ASEQ [] (intern r1) (intern r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   146
| "intern (STAR r) = ASTAR [] (intern r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   147
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   148
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   149
fun retrieve :: "arexp \<Rightarrow> val \<Rightarrow> bit list" where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   150
  "retrieve (AONE bs) Void = bs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   151
| "retrieve (ACHAR bs c) (Char d) = bs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   152
| "retrieve (AALTs bs [r]) v = bs @ retrieve r v"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   153
| "retrieve (AALTs bs (r#rs)) (Left v) = bs @ retrieve r v"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   154
| "retrieve (AALTs bs (r#rs)) (Right v) = bs @ retrieve (AALTs [] rs) v"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   155
| "retrieve (ASEQ bs r1 r2) (Seq v1 v2) = bs @ retrieve r1 v1 @ retrieve r2 v2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   156
| "retrieve (ASTAR bs r) (Stars []) = bs @ [S]"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   157
| "retrieve (ASTAR bs r) (Stars (v#vs)) = 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   158
     bs @ [Z] @ retrieve r v @ retrieve (ASTAR [] r) (Stars vs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   159
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   160
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   161
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   162
fun
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   163
 bnullable :: "arexp \<Rightarrow> bool"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   164
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   165
  "bnullable (AZERO) = False"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   166
| "bnullable (AONE bs) = True"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   167
| "bnullable (ACHAR bs c) = False"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   168
| "bnullable (AALTs bs rs) = (\<exists>r \<in> set rs. bnullable r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   169
| "bnullable (ASEQ bs r1 r2) = (bnullable r1 \<and> bnullable r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   170
| "bnullable (ASTAR bs r) = True"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   171
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   172
abbreviation
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   173
  bnullables :: "arexp list \<Rightarrow> bool"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   174
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   175
  "bnullables rs \<equiv> (\<exists>r \<in> set rs. bnullable r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   176
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   177
fun 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   178
  bmkeps :: "arexp \<Rightarrow> bit list" and
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   179
  bmkepss :: "arexp list \<Rightarrow> bit list"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   180
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   181
  "bmkeps(AONE bs) = bs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   182
| "bmkeps(ASEQ bs r1 r2) = bs @ (bmkeps r1) @ (bmkeps r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   183
| "bmkeps(AALTs bs rs) = bs @ (bmkepss rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   184
| "bmkeps(ASTAR bs r) = bs @ [S]"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   185
| "bmkepss [] = []"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   186
| "bmkepss (r # rs) = (if bnullable(r) then (bmkeps r) else (bmkepss rs))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   187
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   188
lemma bmkepss1:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   189
  assumes "\<not> bnullables rs1"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   190
  shows "bmkepss (rs1 @ rs2) = bmkepss rs2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   191
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   192
  by (induct rs1) (auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   193
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   194
lemma bmkepss2:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   195
  assumes "bnullables rs1"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   196
  shows "bmkepss (rs1 @ rs2) = bmkepss rs1"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   197
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   198
  by (induct rs1) (auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   199
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   200
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   201
fun
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   202
 bder :: "char \<Rightarrow> arexp \<Rightarrow> arexp"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   203
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   204
  "bder c (AZERO) = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   205
| "bder c (AONE bs) = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   206
| "bder c (ACHAR bs d) = (if c = d then AONE bs else AZERO)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   207
| "bder c (AALTs bs rs) = AALTs bs (map (bder c) rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   208
| "bder c (ASEQ bs r1 r2) = 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   209
     (if bnullable r1
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   210
      then AALT bs (ASEQ [] (bder c r1) r2) (fuse (bmkeps r1) (bder c r2))
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   211
      else ASEQ bs (bder c r1) r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   212
| "bder c (ASTAR bs r) = ASEQ bs (fuse [Z] (bder c r)) (ASTAR [] r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   213
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   214
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   215
fun 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   216
  bders :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   217
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   218
  "bders r [] = r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   219
| "bders r (c#s) = bders (bder c r) s"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   220
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   221
lemma bders_append:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   222
  "bders c (s1 @ s2) = bders (bders c s1) s2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   223
  apply(induct s1 arbitrary: c s2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   224
  apply(simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   225
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   226
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   227
lemma bnullable_correctness:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   228
  shows "nullable (erase r) = bnullable r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   229
  apply(induct r rule: erase.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   230
  apply(simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   231
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   232
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   233
lemma erase_fuse:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   234
  shows "erase (fuse bs r) = erase r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   235
  apply(induct r rule: erase.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   236
  apply(simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   237
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   238
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   239
lemma erase_intern [simp]:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   240
  shows "erase (intern r) = r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   241
  apply(induct r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   242
  apply(simp_all add: erase_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   243
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   244
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   245
lemma erase_bder [simp]:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   246
  shows "erase (bder a r) = der a (erase r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   247
  apply(induct r rule: erase.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   248
  apply(simp_all add: erase_fuse bnullable_correctness)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   249
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   250
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   251
lemma erase_bders [simp]:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   252
  shows "erase (bders r s) = ders s (erase r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   253
  apply(induct s arbitrary: r )
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   254
  apply(simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   255
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   256
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   257
lemma bnullable_fuse:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   258
  shows "bnullable (fuse bs r) = bnullable r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   259
  apply(induct r arbitrary: bs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   260
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   261
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   262
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   263
lemma retrieve_encode_STARS:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   264
  assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> code v = retrieve (intern r) v"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   265
  shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   266
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   267
  apply(induct vs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   268
  apply(simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   269
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   270
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   271
lemma retrieve_fuse2:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   272
  assumes "\<Turnstile> v : (erase r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   273
  shows "retrieve (fuse bs r) v = bs @ retrieve r v"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   274
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   275
  apply(induct r arbitrary: v bs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   276
  apply(auto elim: Prf_elims)[4]
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   277
  apply(case_tac x2a)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   278
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   279
  using Prf_elims(1) apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   280
  apply(case_tac x2a)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   281
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   282
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   283
  apply(case_tac list)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   284
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   285
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   286
  apply (smt (verit, best) Prf_elims(3) append_assoc retrieve.simps(4) retrieve.simps(5))
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   287
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   288
  using retrieve_encode_STARS
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   289
  apply(auto elim!: Prf_elims)[1]
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   290
  apply(case_tac vs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   291
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   292
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   293
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   294
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   295
lemma retrieve_fuse:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   296
  assumes "\<Turnstile> v : r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   297
  shows "retrieve (fuse bs (intern r)) v = bs @ retrieve (intern r) v"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   298
  using assms 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   299
  by (simp_all add: retrieve_fuse2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   300
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   301
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   302
lemma retrieve_code:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   303
  assumes "\<Turnstile> v : r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   304
  shows "code v = retrieve (intern r) v"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   305
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   306
  apply(induct v r )
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   307
  apply(simp_all add: retrieve_fuse retrieve_encode_STARS)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   308
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   309
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   310
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   311
lemma retrieve_AALTs_bnullable1:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   312
  assumes "bnullable r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   313
  shows "retrieve (AALTs bs (r # rs)) (mkeps (erase (AALTs bs (r # rs))))
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   314
         = bs @ retrieve r (mkeps (erase r))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   315
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   316
  apply(case_tac rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   317
  apply(auto simp add: bnullable_correctness)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   318
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   319
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   320
lemma retrieve_AALTs_bnullable2:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   321
  assumes "\<not>bnullable r" "bnullables rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   322
  shows "retrieve (AALTs bs (r # rs)) (mkeps (erase (AALTs bs (r # rs))))
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   323
         = retrieve (AALTs bs rs) (mkeps (erase (AALTs bs rs)))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   324
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   325
  apply(induct rs arbitrary: r bs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   326
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   327
  using bnullable_correctness apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   328
  apply(case_tac rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   329
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   330
  using bnullable_correctness apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   331
  apply(case_tac rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   332
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   333
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   334
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   335
lemma bmkeps_retrieve_AALTs: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   336
  assumes "\<forall>r \<in> set rs. bnullable r \<longrightarrow> bmkeps r = retrieve r (mkeps (erase r))" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   337
          "bnullables rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   338
  shows "bs @ bmkepss rs = retrieve (AALTs bs rs) (mkeps (erase (AALTs bs rs)))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   339
 using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   340
  apply(induct rs arbitrary: bs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   341
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   342
  using retrieve_AALTs_bnullable1 apply presburger
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   343
  apply (metis retrieve_AALTs_bnullable2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   344
  apply (simp add: retrieve_AALTs_bnullable1)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   345
  by (metis retrieve_AALTs_bnullable2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   346
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   347
    
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   348
lemma bmkeps_retrieve:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   349
  assumes "bnullable r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   350
  shows "bmkeps r = retrieve r (mkeps (erase r))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   351
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   352
  apply(induct r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   353
  apply(auto)  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   354
  using bmkeps_retrieve_AALTs by auto
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   355
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   356
lemma bder_retrieve:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   357
  assumes "\<Turnstile> v : der c (erase r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   358
  shows "retrieve (bder c r) v = retrieve r (injval (erase r) c v)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   359
  using assms  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   360
  apply(induct r arbitrary: v rule: erase.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   361
  using Prf_elims(1) apply auto[1]
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   362
  using Prf_elims(1) apply auto[1]
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   363
  apply(auto)[1]
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   364
  apply (metis Prf_elims(4) injval.simps(1) retrieve.simps(1) retrieve.simps(2))
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   365
  using Prf_elims(1) apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   366
  (* AALTs case *)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   367
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   368
  apply(erule Prf_elims)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   369
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   370
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   371
  apply(rename_tac "r\<^sub>1" "r\<^sub>2" rs v)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   372
  apply(erule Prf_elims)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   373
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   374
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   375
  apply(case_tac rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   376
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   377
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   378
  using Prf_elims(3) apply fastforce
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   379
  (* ASEQ case *) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   380
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   381
  apply(case_tac "nullable (erase r1)")
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   382
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   383
  apply(erule Prf_elims)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   384
  using Prf_elims(2) bnullable_correctness apply force
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   385
  apply (simp add: bmkeps_retrieve bnullable_correctness retrieve_fuse2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   386
  apply (simp add: bmkeps_retrieve bnullable_correctness retrieve_fuse2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   387
  using Prf_elims(2) apply force
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   388
  (* ASTAR case *)  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   389
  apply(rename_tac bs r v)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   390
  apply(simp)  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   391
  apply(erule Prf_elims)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   392
  apply(clarify)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   393
  apply(erule Prf_elims)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   394
  apply(clarify)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   395
  by (simp add: retrieve_fuse2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   396
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   397
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   398
lemma MAIN_decode:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   399
  assumes "\<Turnstile> v : ders s r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   400
  shows "Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   401
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   402
proof (induct s arbitrary: v rule: rev_induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   403
  case Nil
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   404
  have "\<Turnstile> v : ders [] r" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   405
  then have "\<Turnstile> v : r" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   406
  then have "Some v = decode (retrieve (intern r) v) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   407
    using decode_code retrieve_code by auto
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   408
  then show "Some (flex r id [] v) = decode (retrieve (bders (intern r) []) v) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   409
    by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   410
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   411
  case (snoc c s v)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   412
  have IH: "\<And>v. \<Turnstile> v : ders s r \<Longrightarrow> 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   413
     Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   414
  have asm: "\<Turnstile> v : ders (s @ [c]) r" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   415
  then have asm2: "\<Turnstile> injval (ders s r) c v : ders s r" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   416
    by (simp add: Prf_injval ders_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   417
  have "Some (flex r id (s @ [c]) v) = Some (flex r id s (injval (ders s r) c v))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   418
    by (simp add: flex_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   419
  also have "... = decode (retrieve (bders (intern r) s) (injval (ders s r) c v)) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   420
    using asm2 IH by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   421
  also have "... = decode (retrieve (bder c (bders (intern r) s)) v) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   422
    using asm by (simp_all add: bder_retrieve ders_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   423
  finally show "Some (flex r id (s @ [c]) v) = 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   424
                 decode (retrieve (bders (intern r) (s @ [c])) v) r" by (simp add: bders_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   425
qed
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   426
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   427
definition blexer where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   428
 "blexer r s \<equiv> if bnullable (bders (intern r) s) then 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   429
                decode (bmkeps (bders (intern r) s)) r else None"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   430
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   431
lemma blexer_correctness:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   432
  shows "blexer r s = lexer r s"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   433
proof -
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   434
  { define bds where "bds \<equiv> bders (intern r) s"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   435
    define ds  where "ds \<equiv> ders s r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   436
    assume asm: "nullable ds"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   437
    have era: "erase bds = ds" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   438
      unfolding ds_def bds_def by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   439
    have mke: "\<Turnstile> mkeps ds : ds"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   440
      using asm by (simp add: mkeps_nullable)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   441
    have "decode (bmkeps bds) r = decode (retrieve bds (mkeps ds)) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   442
      using bmkeps_retrieve
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   443
      using asm era
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   444
      using bnullable_correctness by force 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   445
    also have "... =  Some (flex r id s (mkeps ds))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   446
      using mke by (simp_all add: MAIN_decode ds_def bds_def)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   447
    finally have "decode (bmkeps bds) r = Some (flex r id s (mkeps ds))" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   448
      unfolding bds_def ds_def .
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   449
  }
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   450
  then show "blexer r s = lexer r s"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   451
    unfolding blexer_def lexer_flex
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   452
    by (auto simp add: bnullable_correctness[symmetric])
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   453
qed
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   454
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   455
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   456
fun distinctBy :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b set \<Rightarrow> 'a list"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   457
  where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   458
  "distinctBy [] f acc = []"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   459
| "distinctBy (x#xs) f acc = 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   460
     (if (f x) \<in> acc then distinctBy xs f acc 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   461
      else x # (distinctBy xs f ({f x} \<union> acc)))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   462
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   463
  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   464
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   465
fun flts :: "arexp list \<Rightarrow> arexp list"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   466
  where 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   467
  "flts [] = []"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   468
| "flts (AZERO # rs) = flts rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   469
| "flts ((AALTs bs  rs1) # rs) = (map (fuse bs) rs1) @ flts rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   470
| "flts (r1 # rs) = r1 # flts rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   471
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   472
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   473
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   474
fun bsimp_ASEQ :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp \<Rightarrow> arexp"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   475
  where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   476
  "bsimp_ASEQ _ AZERO _ = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   477
| "bsimp_ASEQ _ _ AZERO = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   478
| "bsimp_ASEQ bs1 (AONE bs2) r2 = fuse (bs1 @ bs2) r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   479
| "bsimp_ASEQ bs1 r1 r2 = ASEQ  bs1 r1 r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   480
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   481
lemma bsimp_ASEQ0[simp]:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   482
  shows "bsimp_ASEQ bs r1 AZERO = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   483
  by (case_tac r1)(simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   484
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   485
lemma bsimp_ASEQ1:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   486
  assumes "r1 \<noteq> AZERO" "r2 \<noteq> AZERO" "\<nexists>bs. r1 = AONE bs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   487
  shows "bsimp_ASEQ bs r1 r2 = ASEQ bs r1 r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   488
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   489
  apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   490
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   491
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   492
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   493
lemma bsimp_ASEQ2[simp]:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   494
  shows "bsimp_ASEQ bs1 (AONE bs2) r2 = fuse (bs1 @ bs2) r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   495
  by (case_tac r2) (simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   496
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   497
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   498
fun bsimp_AALTs :: "bit list \<Rightarrow> arexp list \<Rightarrow> arexp"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   499
  where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   500
  "bsimp_AALTs _ [] = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   501
| "bsimp_AALTs bs1 [r] = fuse bs1 r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   502
| "bsimp_AALTs bs1 rs = AALTs bs1 rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   503
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   504
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   505
fun bsimp :: "arexp \<Rightarrow> arexp" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   506
  where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   507
  "bsimp (ASEQ bs1 r1 r2) = bsimp_ASEQ bs1 (bsimp r1) (bsimp r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   508
| "bsimp (AALTs bs1 rs) = bsimp_AALTs bs1 (distinctBy (flts (map bsimp rs)) erase {}) "
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   509
| "bsimp r = r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   510
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   511
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   512
fun 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   513
  bders_simp :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   514
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   515
  "bders_simp r [] = r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   516
| "bders_simp r (c # s) = bders_simp (bsimp (bder c r)) s"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   517
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   518
definition blexer_simp where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   519
 "blexer_simp r s \<equiv> if bnullable (bders_simp (intern r) s) then 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   520
                    decode (bmkeps (bders_simp (intern r) s)) r else None"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   521
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   522
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   523
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   524
lemma bders_simp_append:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   525
  shows "bders_simp r (s1 @ s2) = bders_simp (bders_simp r s1) s2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   526
  apply(induct s1 arbitrary: r s2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   527
  apply(simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   528
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   529
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   530
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   531
lemma bmkeps_fuse:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   532
  assumes "bnullable r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   533
  shows "bmkeps (fuse bs r) = bs @ bmkeps r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   534
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   535
  by (metis bmkeps_retrieve bnullable_correctness erase_fuse mkeps_nullable retrieve_fuse2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   536
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   537
lemma bmkepss_fuse: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   538
  assumes "bnullables rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   539
  shows "bmkepss (map (fuse bs) rs) = bs @ bmkepss rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   540
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   541
  apply(induct rs arbitrary: bs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   542
  apply(auto simp add: bmkeps_fuse bnullable_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   543
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   544
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   545
lemma bder_fuse:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   546
  shows "bder c (fuse bs a) = fuse bs  (bder c a)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   547
  apply(induct a arbitrary: bs c)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   548
  apply(simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   549
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   550
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   551
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   552
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   553
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   554
inductive 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   555
  rrewrite:: "arexp \<Rightarrow> arexp \<Rightarrow> bool" ("_ \<leadsto> _" [99, 99] 99)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   556
and 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   557
  srewrite:: "arexp list \<Rightarrow> arexp list \<Rightarrow> bool" (" _ s\<leadsto> _" [100, 100] 100)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   558
where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   559
  bs1: "ASEQ bs AZERO r2 \<leadsto> AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   560
| bs2: "ASEQ bs r1 AZERO \<leadsto> AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   561
| bs3: "ASEQ bs1 (AONE bs2) r \<leadsto> fuse (bs1@bs2) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   562
| bs4: "r1 \<leadsto> r2 \<Longrightarrow> ASEQ bs r1 r3 \<leadsto> ASEQ bs r2 r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   563
| bs5: "r3 \<leadsto> r4 \<Longrightarrow> ASEQ bs r1 r3 \<leadsto> ASEQ bs r1 r4"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   564
| bs6: "AALTs bs [] \<leadsto> AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   565
| bs7: "AALTs bs [r] \<leadsto> fuse bs r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   566
| bs8: "rs1 s\<leadsto> rs2 \<Longrightarrow> AALTs bs rs1 \<leadsto> AALTs bs rs2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   567
| ss1:  "[] s\<leadsto> []"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   568
| ss2:  "rs1 s\<leadsto> rs2 \<Longrightarrow> (r # rs1) s\<leadsto> (r # rs2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   569
| ss3:  "r1 \<leadsto> r2 \<Longrightarrow> (r1 # rs) s\<leadsto> (r2 # rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   570
| ss4:  "(AZERO # rs) s\<leadsto> rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   571
| ss5:  "(AALTs bs1 rs1 # rsb) s\<leadsto> ((map (fuse bs1) rs1) @ rsb)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   572
| ss6:  "erase a1 = erase a2 \<Longrightarrow> (rsa@[a1]@rsb@[a2]@rsc) s\<leadsto> (rsa@[a1]@rsb@rsc)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   573
| ss7:  "erase a01 = erase a02 \<and> (distinctBy as2 erase (set (map erase as1)) = as2p)  \<Longrightarrow> 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   574
        (rsa@[ASEQ bs (AALTs bs1 as1) (ASTAR bs01 a01)]@rsb@[ASEQ bs (AALTs bs2 as2) (ASTAR bs02 a02)]@rsc)  s\<leadsto>
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   575
        (rsa@[ASEQ bs (AALTs bs1 as1) (ASTAR bs01 a01)]@rsb@[ASEQ bs (AALTs bs2 as2p) (ASTAR bs02 a02)]@rsc)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   576
| ss8:  "erase a01 = erase a02 \<and> (distinctBy [a2] erase (set (map erase as1)) = [])  \<Longrightarrow> 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   577
        (rsa@[ASEQ bs (AALTs bs1 as1) (ASTAR bs01 a01)]@rsb@[ASEQ bs a2 (ASTAR bs02 a02)]@rsc) s\<leadsto>                                                
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   578
        (rsa@[ASEQ bs (AALTs bs1 as1) (ASTAR bs01 a01)]@rsb@rsc)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   579
| ss9:  "erase a01 = erase a02 \<and> (distinctBy as2 erase {erase a1} = as2p)  \<Longrightarrow> 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   580
        (rsa@[ASEQ bs a1 (ASTAR bs01 a01)]@rsb@[ASEQ bs (AALTs bs2 as2) (ASTAR bs02 a02)]@rsc) s\<leadsto> 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   581
        (rsa@[ASEQ bs a1 (ASTAR bs01 a01)]@rsb@[ASEQ bs (AALTs bs2 as2p) (ASTAR bs02 a02)]@rsc)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   582
 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   583
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   584
inductive 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   585
  rrewrites:: "arexp \<Rightarrow> arexp \<Rightarrow> bool" ("_ \<leadsto>* _" [100, 100] 100)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   586
where 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   587
  rs1[intro, simp]:"r \<leadsto>* r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   588
| rs2[intro]: "\<lbrakk>r1 \<leadsto>* r2; r2 \<leadsto> r3\<rbrakk> \<Longrightarrow> r1 \<leadsto>* r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   589
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   590
inductive 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   591
  srewrites:: "arexp list \<Rightarrow> arexp list \<Rightarrow> bool" ("_ s\<leadsto>* _" [100, 100] 100)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   592
where 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   593
  sss1[intro, simp]:"rs s\<leadsto>* rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   594
| sss2[intro]: "\<lbrakk>rs1 s\<leadsto> rs2; rs2 s\<leadsto>* rs3\<rbrakk> \<Longrightarrow> rs1 s\<leadsto>* rs3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   595
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   596
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   597
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   598
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   599
fun fmap :: "arexp \<Rightarrow> rexp set"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   600
  where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   601
"fmap AALTs bs rs = flatten (map  fmap rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   602
| "fmap ASEQ bs (AALTs bs1 rs1) r2 = (flatten (map fmap rs1)) ` (ASEQ bs _ r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   603
| "fmap (ASTAR bs r0) = {ASTAR bs r0}"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   604
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   605
(*r1~r2 --\c>  r1c~r2 \<longrightarrow> r1\s~r2 + r2\s1 ... \<longrightarrow> (r1\s ~ r2) +r2\s'+r2\s''+........ *)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   606
(* r* \<longrightarrow>r\c ~ r* ---> r\s1 ~ r* + r\s1' ~ r* \<longrightarrow> s1, s1' \<in> suffix s  *)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   607
(*                    {r1, r2', r3} {r2,r3,r4} pders\<longrightarrow> {r1-r*, r2-r*, r3-r*, r4-r*} *)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   608
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   609
lemma iso_pder: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   610
"fmap (bders_simp r s) \<subseteq> pderss UNIV r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   611
  apply(induction r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   612
  prefer 4
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   613
  oops
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   614
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   615
lemma shape_of_star_after_derssimp:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   616
  shows "\<forall>bs r s. \<exists>bs1 r1 r2 rs bs2 bsp. (bders_simp (ASTAR bs r) s = (ASEQ bs1 r1 r2)) \<or> 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   617
         (bders_simp (ASTAR bs r) s = (AALTs bs2 rs)) \<or>
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   618
         (bders_simp (ASTAR bs r) s = (ASTAR bsp r))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   619
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   620
  oops
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   621
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   622
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   623
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   624
lemma notManyTerms:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   625
  shows "card {(bders_simp r (Suffix s))| r s. s \<in> L (erase r) } < (size r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   626
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   627
  oops
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   628
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   629
lemma rtozero: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   630
  shows "\<lbrakk>s \<notin> L (erase r); s \<noteq> Nil\<rbrakk> \<Longrightarrow> bders_simp r s = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   631
  apply(induct r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   632
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   633
  oops
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   634
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   635
lemma r_in_rstar: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   636
  shows "r1 \<leadsto> r2 \<Longrightarrow> r1 \<leadsto>* r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   637
  using rrewrites.intros(1) rrewrites.intros(2) by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   638
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   639
lemma rrewrites_trans[trans]: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   640
  assumes a1: "r1 \<leadsto>* r2"  and a2: "r2 \<leadsto>* r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   641
  shows "r1 \<leadsto>* r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   642
  using a2 a1
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   643
  apply(induct r2 r3 arbitrary: r1 rule: rrewrites.induct) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   644
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   645
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   646
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   647
lemma srewrites_trans[trans]: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   648
  assumes a1: "r1 s\<leadsto>* r2"  and a2: "r2 s\<leadsto>* r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   649
  shows "r1 s\<leadsto>* r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   650
  using a1 a2
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   651
  apply(induct r1 r2 arbitrary: r3 rule: srewrites.induct) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   652
   apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   653
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   654
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   655
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   656
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   657
lemma contextrewrites0: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   658
  "rs1 s\<leadsto>* rs2 \<Longrightarrow> AALTs bs rs1 \<leadsto>* AALTs bs rs2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   659
  apply(induct rs1 rs2 rule: srewrites.inducts)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   660
   apply simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   661
  using bs8 r_in_rstar rrewrites_trans by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   662
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   663
lemma contextrewrites1: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   664
  "r \<leadsto>* r' \<Longrightarrow> AALTs bs (r # rs) \<leadsto>* AALTs bs (r' # rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   665
  apply(induct r r' rule: rrewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   666
   apply simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   667
  using bs8 ss3 by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   668
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   669
lemma srewrite1: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   670
  shows "rs1 s\<leadsto> rs2 \<Longrightarrow> (rs @ rs1) s\<leadsto> (rs @ rs2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   671
  apply(induct rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   672
   apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   673
  using ss2 by auto
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   674
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   675
lemma srewrites1: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   676
  shows "rs1 s\<leadsto>* rs2 \<Longrightarrow> (rs @ rs1) s\<leadsto>* (rs @ rs2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   677
  apply(induct rs1 rs2 rule: srewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   678
   apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   679
  using srewrite1 by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   680
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   681
lemma srewrite2: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   682
  shows  "r1 \<leadsto> r2 \<Longrightarrow> True"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   683
  and "rs1 s\<leadsto> rs2 \<Longrightarrow> (rs1 @ rs) s\<leadsto>* (rs2 @ rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   684
  apply(induct rule: rrewrite_srewrite.inducts)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   685
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   686
  apply (metis append_Cons append_Nil srewrites1)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   687
  apply(meson srewrites.simps ss3)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   688
  apply (meson srewrites.simps ss4)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   689
  apply (meson srewrites.simps ss5)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   690
  by (metis append_Cons append_Nil srewrites.simps ss6)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   691
  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   692
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   693
lemma srewrites3: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   694
  shows "rs1 s\<leadsto>* rs2 \<Longrightarrow> (rs1 @ rs) s\<leadsto>* (rs2 @ rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   695
  apply(induct rs1 rs2 arbitrary: rs rule: srewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   696
   apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   697
  by (meson srewrite2(2) srewrites_trans)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   698
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   699
(*
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   700
lemma srewrites4:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   701
  assumes "rs3 s\<leadsto>* rs4" "rs1 s\<leadsto>* rs2" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   702
  shows "(rs1 @ rs3) s\<leadsto>* (rs2 @ rs4)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   703
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   704
  apply(induct rs3 rs4 arbitrary: rs1 rs2 rule: srewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   705
  apply (simp add: srewrites3)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   706
  using srewrite1 by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   707
*)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   708
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   709
lemma srewrites6:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   710
  assumes "r1 \<leadsto>* r2" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   711
  shows "[r1] s\<leadsto>* [r2]"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   712
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   713
  apply(induct r1 r2 rule: rrewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   714
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   715
  by (meson srewrites.simps srewrites_trans ss3)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   716
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   717
lemma srewrites7:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   718
  assumes "rs3 s\<leadsto>* rs4" "r1 \<leadsto>* r2" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   719
  shows "(r1 # rs3) s\<leadsto>* (r2 # rs4)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   720
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   721
  by (smt (verit, del_insts) append.simps srewrites1 srewrites3 srewrites6 srewrites_trans)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   722
  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   723
lemma ss6_stronger_aux:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   724
  shows "(rs1 @ rs2) s\<leadsto>* (rs1 @ distinctBy rs2 erase (set (map erase rs1)))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   725
  apply(induct rs2 arbitrary: rs1)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   726
   apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   727
  apply (smt (verit, best) append.assoc append.right_neutral append_Cons append_Nil split_list srewrite2(2) srewrites_trans ss6)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   728
  apply(drule_tac x="rs1 @ [a]" in meta_spec)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   729
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   730
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   731
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   732
lemma ss6_stronger:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   733
  shows "rs1 s\<leadsto>* distinctBy rs1 erase {}"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   734
  using ss6_stronger_aux[of "[]" _] by auto
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   735
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   736
lemma rewrite_preserves_fuse: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   737
  shows "r2 \<leadsto> r3 \<Longrightarrow> fuse bs r2 \<leadsto> fuse bs r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   738
  and   "rs2 s\<leadsto> rs3 \<Longrightarrow> map (fuse bs) rs2 s\<leadsto> map (fuse bs) rs3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   739
proof(induct rule: rrewrite_srewrite.inducts)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   740
  case (bs3 bs1 bs2 r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   741
  then show "fuse bs (ASEQ bs1 (AONE bs2) r) \<leadsto> fuse bs (fuse (bs1 @ bs2) r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   742
    by (metis fuse.simps(5) fuse_append rrewrite_srewrite.bs3) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   743
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   744
  case (bs7 bs1 r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   745
  then show "fuse bs (AALTs bs1 [r]) \<leadsto> fuse bs (fuse bs1 r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   746
    by (metis fuse.simps(4) fuse_append rrewrite_srewrite.bs7) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   747
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   748
  case (ss2 rs1 rs2 r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   749
  then show "map (fuse bs) (r # rs1) s\<leadsto> map (fuse bs) (r # rs2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   750
    by (simp add: rrewrite_srewrite.ss2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   751
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   752
  case (ss3 r1 r2 rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   753
  then show "map (fuse bs) (r1 # rs) s\<leadsto> map (fuse bs) (r2 # rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   754
    by (simp add: rrewrite_srewrite.ss3)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   755
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   756
  case (ss5 bs1 rs1 rsb)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   757
  have "map (fuse bs) (AALTs bs1 rs1 # rsb) = AALTs (bs @ bs1) rs1 # (map (fuse bs) rsb)" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   758
  also have "... s\<leadsto> ((map (fuse (bs @ bs1)) rs1) @ (map (fuse bs) rsb))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   759
    by (simp add: rrewrite_srewrite.ss5)  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   760
  finally show "map (fuse bs) (AALTs bs1 rs1 # rsb) s\<leadsto> map (fuse bs) (map (fuse bs1) rs1 @ rsb)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   761
    by (simp add: comp_def fuse_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   762
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   763
  case (ss6 a1 a2 rsa rsb rsc)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   764
  then show "map (fuse bs) (rsa @ [a1] @ rsb @ [a2] @ rsc) s\<leadsto> map (fuse bs) (rsa @ [a1] @ rsb @ rsc)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   765
    apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   766
    apply(rule rrewrite_srewrite.ss6[simplified])
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   767
    apply(simp add: erase_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   768
    done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   769
qed (auto intro: rrewrite_srewrite.intros)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   770
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   771
lemma rewrites_fuse:  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   772
  assumes "r1 \<leadsto>* r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   773
  shows "fuse bs r1 \<leadsto>* fuse bs r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   774
using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   775
apply(induction r1 r2 arbitrary: bs rule: rrewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   776
apply(auto intro: rewrite_preserves_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   777
done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   778
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   779
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   780
lemma star_seq:  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   781
  assumes "r1 \<leadsto>* r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   782
  shows "ASEQ bs r1 r3 \<leadsto>* ASEQ bs r2 r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   783
using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   784
apply(induct r1 r2 arbitrary: r3 rule: rrewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   785
apply(auto intro: rrewrite_srewrite.intros)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   786
done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   787
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   788
lemma star_seq2:  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   789
  assumes "r3 \<leadsto>* r4"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   790
  shows "ASEQ bs r1 r3 \<leadsto>* ASEQ bs r1 r4"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   791
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   792
apply(induct r3 r4 arbitrary: r1 rule: rrewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   793
apply(auto intro: rrewrite_srewrite.intros)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   794
done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   795
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   796
lemma continuous_rewrite: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   797
  assumes "r1 \<leadsto>* AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   798
  shows "ASEQ bs1 r1 r2 \<leadsto>* AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   799
using assms bs1 star_seq by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   800
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   801
(*
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   802
lemma continuous_rewrite2: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   803
  assumes "r1 \<leadsto>* AONE bs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   804
  shows "ASEQ bs1 r1 r2 \<leadsto>* (fuse (bs1 @ bs) r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   805
  using assms  by (meson bs3 rrewrites.simps star_seq)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   806
*)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   807
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   808
lemma bsimp_aalts_simpcases: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   809
  shows "AONE bs \<leadsto>* bsimp (AONE bs)"  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   810
  and   "AZERO \<leadsto>* bsimp AZERO" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   811
  and   "ACHAR bs c \<leadsto>* bsimp (ACHAR bs c)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   812
  by (simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   813
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   814
lemma bsimp_AALTs_rewrites: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   815
  shows "AALTs bs1 rs \<leadsto>* bsimp_AALTs bs1 rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   816
  by (smt (verit) bs6 bs7 bsimp_AALTs.elims rrewrites.simps)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   817
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   818
lemma trivialbsimp_srewrites: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   819
  assumes "\<And>x. x \<in> set rs \<Longrightarrow> x \<leadsto>* f x"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   820
  shows "rs s\<leadsto>* (map f rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   821
using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   822
  apply(induction rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   823
  apply(simp_all add: srewrites7)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   824
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   825
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   826
lemma fltsfrewrites: "rs s\<leadsto>* flts rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   827
  apply(induction rs rule: flts.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   828
  apply(auto intro: rrewrite_srewrite.intros)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   829
  apply (meson srewrites.simps srewrites1 ss5)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   830
  using rs1 srewrites7 apply presburger
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   831
  using srewrites7 apply force
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   832
  apply (simp add: srewrites7)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   833
  by (simp add: srewrites7)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   834
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   835
lemma bnullable0:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   836
shows "r1 \<leadsto> r2 \<Longrightarrow> bnullable r1 = bnullable r2" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   837
  and "rs1 s\<leadsto> rs2 \<Longrightarrow> bnullables rs1 = bnullables rs2" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   838
  apply(induct rule: rrewrite_srewrite.inducts)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   839
  apply(auto simp add:  bnullable_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   840
  apply (meson UnCI bnullable_fuse imageI)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   841
  by (metis bnullable_correctness)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   842
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   843
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   844
lemma rewrites_bnullable_eq: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   845
  assumes "r1 \<leadsto>* r2" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   846
  shows "bnullable r1 = bnullable r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   847
using assms 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   848
  apply(induction r1 r2 rule: rrewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   849
  apply simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   850
  using bnullable0(1) by auto
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   851
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   852
lemma rewrite_bmkeps_aux: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   853
  shows "r1 \<leadsto> r2 \<Longrightarrow> bnullable r1 \<Longrightarrow> bmkeps r1 = bmkeps r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   854
  and   "rs1 s\<leadsto> rs2 \<Longrightarrow> bnullables rs1 \<Longrightarrow> bmkepss rs1 = bmkepss rs2" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   855
proof (induct rule: rrewrite_srewrite.inducts)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   856
  case (bs3 bs1 bs2 r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   857
  have IH2: "bnullable (ASEQ bs1 (AONE bs2) r)" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   858
  then show "bmkeps (ASEQ bs1 (AONE bs2) r) = bmkeps (fuse (bs1 @ bs2) r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   859
    by (simp add: bmkeps_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   860
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   861
  case (bs7 bs r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   862
  have IH2: "bnullable (AALTs bs [r])" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   863
  then show "bmkeps (AALTs bs [r]) = bmkeps (fuse bs r)" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   864
    by (simp add: bmkeps_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   865
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   866
  case (ss3 r1 r2 rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   867
  have IH1: "bnullable r1 \<Longrightarrow> bmkeps r1 = bmkeps r2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   868
  have as: "r1 \<leadsto> r2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   869
  from IH1 as show "bmkepss (r1 # rs) = bmkepss (r2 # rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   870
    by (simp add: bnullable0)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   871
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   872
  case (ss5 bs1 rs1 rsb)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   873
  have "bnullables (AALTs bs1 rs1 # rsb)" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   874
  then show "bmkepss (AALTs bs1 rs1 # rsb) = bmkepss (map (fuse bs1) rs1 @ rsb)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   875
    by (simp add: bmkepss1 bmkepss2 bmkepss_fuse bnullable_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   876
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   877
  case (ss6 a1 a2 rsa rsb rsc)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   878
  have as1: "erase a1 = erase a2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   879
  have as3: "bnullables (rsa @ [a1] @ rsb @ [a2] @ rsc)" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   880
  show "bmkepss (rsa @ [a1] @ rsb @ [a2] @ rsc) = bmkepss (rsa @ [a1] @ rsb @ rsc)" using as1 as3
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   881
    by (smt (verit, best) append_Cons bmkeps.simps(3) bmkepss.simps(2) bmkepss1 bmkepss2 bnullable_correctness)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   882
qed (auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   883
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   884
lemma rewrites_bmkeps: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   885
  assumes "r1 \<leadsto>* r2" "bnullable r1" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   886
  shows "bmkeps r1 = bmkeps r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   887
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   888
proof(induction r1 r2 rule: rrewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   889
  case (rs1 r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   890
  then show "bmkeps r = bmkeps r" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   891
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   892
  case (rs2 r1 r2 r3)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   893
  then have IH: "bmkeps r1 = bmkeps r2" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   894
  have a1: "bnullable r1" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   895
  have a2: "r1 \<leadsto>* r2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   896
  have a3: "r2 \<leadsto> r3" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   897
  have a4: "bnullable r2" using a1 a2 by (simp add: rewrites_bnullable_eq) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   898
  then have "bmkeps r2 = bmkeps r3"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   899
    using a3 bnullable0(1) rewrite_bmkeps_aux(1) by blast 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   900
  then show "bmkeps r1 = bmkeps r3" using IH by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   901
qed
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   902
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   903
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   904
lemma rewrites_to_bsimp: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   905
  shows "r \<leadsto>* bsimp r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   906
proof (induction r rule: bsimp.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   907
  case (1 bs1 r1 r2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   908
  have IH1: "r1 \<leadsto>* bsimp r1" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   909
  have IH2: "r2 \<leadsto>* bsimp r2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   910
  { assume as: "bsimp r1 = AZERO \<or> bsimp r2 = AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   911
    with IH1 IH2 have "r1 \<leadsto>* AZERO \<or> r2 \<leadsto>* AZERO" by auto
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   912
    then have "ASEQ bs1 r1 r2 \<leadsto>* AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   913
      by (metis bs2 continuous_rewrite rrewrites.simps star_seq2)  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   914
    then have "ASEQ bs1 r1 r2 \<leadsto>* bsimp (ASEQ bs1 r1 r2)" using as by auto
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   915
  }
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   916
  moreover
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   917
  { assume "\<exists>bs. bsimp r1 = AONE bs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   918
    then obtain bs where as: "bsimp r1 = AONE bs" by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   919
    with IH1 have "r1 \<leadsto>* AONE bs" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   920
    then have "ASEQ bs1 r1 r2 \<leadsto>* fuse (bs1 @ bs) r2" using bs3 star_seq by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   921
    with IH2 have "ASEQ bs1 r1 r2 \<leadsto>* fuse (bs1 @ bs) (bsimp r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   922
      using rewrites_fuse by (meson rrewrites_trans) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   923
    then have "ASEQ bs1 r1 r2 \<leadsto>* bsimp (ASEQ bs1 (AONE bs) r2)" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   924
    then have "ASEQ bs1 r1 r2 \<leadsto>* bsimp (ASEQ bs1 r1 r2)" by (simp add: as) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   925
  } 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   926
  moreover
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   927
  { assume as1: "bsimp r1 \<noteq> AZERO" "bsimp r2 \<noteq> AZERO" and as2: "(\<nexists>bs. bsimp r1 = AONE bs)" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   928
    then have "bsimp_ASEQ bs1 (bsimp r1) (bsimp r2) = ASEQ bs1 (bsimp r1) (bsimp r2)" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   929
      by (simp add: bsimp_ASEQ1) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   930
    then have "ASEQ bs1 r1 r2 \<leadsto>* bsimp_ASEQ bs1 (bsimp r1) (bsimp r2)" using as1 as2 IH1 IH2
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   931
      by (metis rrewrites_trans star_seq star_seq2) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   932
    then have "ASEQ bs1 r1 r2 \<leadsto>* bsimp (ASEQ bs1 r1 r2)" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   933
  } 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   934
  ultimately show "ASEQ bs1 r1 r2 \<leadsto>* bsimp (ASEQ bs1 r1 r2)" by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   935
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   936
  case (2 bs1 rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   937
  have IH: "\<And>x. x \<in> set rs \<Longrightarrow> x \<leadsto>* bsimp x" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   938
  then have "rs s\<leadsto>* (map bsimp rs)" by (simp add: trivialbsimp_srewrites)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   939
  also have "... s\<leadsto>* flts (map bsimp rs)" by (simp add: fltsfrewrites) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   940
  also have "... s\<leadsto>* distinctBy (flts (map bsimp rs)) erase {}" by (simp add: ss6_stronger) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   941
  finally have "AALTs bs1 rs \<leadsto>* AALTs bs1 (distinctBy (flts (map bsimp rs)) erase {})"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   942
    using contextrewrites0 by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   943
  also have "... \<leadsto>* bsimp_AALTs  bs1 (distinctBy (flts (map bsimp rs)) erase {})"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   944
    by (simp add: bsimp_AALTs_rewrites)     
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   945
  finally show "AALTs bs1 rs \<leadsto>* bsimp (AALTs bs1 rs)" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   946
qed (simp_all)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   947
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   948
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   949
lemma to_zero_in_alt: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   950
  shows "AALT bs (ASEQ [] AZERO r) r2 \<leadsto> AALT bs AZERO r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   951
  by (simp add: bs1 bs8 ss3)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   952
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   953
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   954
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   955
lemma  bder_fuse_list: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   956
  shows "map (bder c \<circ> fuse bs1) rs1 = map (fuse bs1 \<circ> bder c) rs1"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   957
  apply(induction rs1)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   958
  apply(simp_all add: bder_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   959
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   960
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   961
lemma rewrite_preserves_bder: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   962
  shows "r1 \<leadsto> r2 \<Longrightarrow> bder c r1 \<leadsto>* bder c r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   963
  and   "rs1 s\<leadsto> rs2 \<Longrightarrow> map (bder c) rs1 s\<leadsto>* map (bder c) rs2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   964
proof(induction rule: rrewrite_srewrite.inducts)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   965
  case (bs1 bs r2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   966
  show "bder c (ASEQ bs AZERO r2) \<leadsto>* bder c AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   967
    by (simp add: continuous_rewrite) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   968
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   969
  case (bs2 bs r1)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   970
  show "bder c (ASEQ bs r1 AZERO) \<leadsto>* bder c AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   971
    apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   972
    apply (meson bs6 contextrewrites0 rrewrite_srewrite.bs2 rs2 ss3 ss4 sss1 sss2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   973
    by (simp add: r_in_rstar rrewrite_srewrite.bs2)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   974
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   975
  case (bs3 bs1 bs2 r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   976
  show "bder c (ASEQ bs1 (AONE bs2) r) \<leadsto>* bder c (fuse (bs1 @ bs2) r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   977
    apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   978
    by (metis (no_types, lifting) bder_fuse bs8 bs7 fuse_append rrewrites.simps ss4 to_zero_in_alt)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   979
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   980
  case (bs4 r1 r2 bs r3)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   981
  have as: "r1 \<leadsto> r2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   982
  have IH: "bder c r1 \<leadsto>* bder c r2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   983
  from as IH show "bder c (ASEQ bs r1 r3) \<leadsto>* bder c (ASEQ bs r2 r3)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   984
    by (metis bder.simps(5) bnullable0(1) contextrewrites1 rewrite_bmkeps_aux(1) star_seq)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   985
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   986
  case (bs5 r3 r4 bs r1)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   987
  have as: "r3 \<leadsto> r4" by fact 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   988
  have IH: "bder c r3 \<leadsto>* bder c r4" by fact 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   989
  from as IH show "bder c (ASEQ bs r1 r3) \<leadsto>* bder c (ASEQ bs r1 r4)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   990
    apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   991
    apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   992
    using contextrewrites0 r_in_rstar rewrites_fuse srewrites6 srewrites7 star_seq2 apply presburger
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   993
    using star_seq2 by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   994
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   995
  case (bs6 bs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   996
  show "bder c (AALTs bs []) \<leadsto>* bder c AZERO"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   997
    using rrewrite_srewrite.bs6 by force 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   998
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
   999
  case (bs7 bs r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1000
  show "bder c (AALTs bs [r]) \<leadsto>* bder c (fuse bs r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1001
    by (simp add: bder_fuse r_in_rstar rrewrite_srewrite.bs7) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1002
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1003
  case (bs8 rs1 rs2 bs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1004
  have IH1: "map (bder c) rs1 s\<leadsto>* map (bder c) rs2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1005
  then show "bder c (AALTs bs rs1) \<leadsto>* bder c (AALTs bs rs2)" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1006
    using contextrewrites0 by force    
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1007
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1008
  case ss1
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1009
  show "map (bder c) [] s\<leadsto>* map (bder c) []" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1010
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1011
  case (ss2 rs1 rs2 r)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1012
  have IH1: "map (bder c) rs1 s\<leadsto>* map (bder c) rs2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1013
  then show "map (bder c) (r # rs1) s\<leadsto>* map (bder c) (r # rs2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1014
    by (simp add: srewrites7) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1015
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1016
  case (ss3 r1 r2 rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1017
  have IH: "bder c r1 \<leadsto>* bder c r2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1018
  then show "map (bder c) (r1 # rs) s\<leadsto>* map (bder c) (r2 # rs)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1019
    by (simp add: srewrites7) 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1020
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1021
  case (ss4 rs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1022
  show "map (bder c) (AZERO # rs) s\<leadsto>* map (bder c) rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1023
    using rrewrite_srewrite.ss4 by fastforce 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1024
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1025
  case (ss5 bs1 rs1 rsb)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1026
  show "map (bder c) (AALTs bs1 rs1 # rsb) s\<leadsto>* map (bder c) (map (fuse bs1) rs1 @ rsb)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1027
    apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1028
    using bder_fuse_list map_map rrewrite_srewrite.ss5 srewrites.simps by blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1029
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1030
  case (ss6 a1 a2 bs rsa rsb)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1031
  have as: "erase a1 = erase a2" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1032
  show "map (bder c) (bs @ [a1] @ rsa @ [a2] @ rsb) s\<leadsto>* map (bder c) (bs @ [a1] @ rsa @ rsb)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1033
    apply(simp only: map_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1034
    by (smt (verit, best) erase_bder list.simps(8) list.simps(9) as rrewrite_srewrite.ss6 srewrites.simps)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1035
qed
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1036
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1037
lemma rewrites_preserves_bder: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1038
  assumes "r1 \<leadsto>* r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1039
  shows "bder c r1 \<leadsto>* bder c r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1040
using assms  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1041
apply(induction r1 r2 rule: rrewrites.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1042
apply(simp_all add: rewrite_preserves_bder rrewrites_trans)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1043
done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1044
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1045
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1046
lemma central:  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1047
  shows "bders r s \<leadsto>* bders_simp r s"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1048
proof(induct s arbitrary: r rule: rev_induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1049
  case Nil
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1050
  then show "bders r [] \<leadsto>* bders_simp r []" by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1051
next
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1052
  case (snoc x xs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1053
  have IH: "\<And>r. bders r xs \<leadsto>* bders_simp r xs" by fact
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1054
  have "bders r (xs @ [x]) = bders (bders r xs) [x]" by (simp add: bders_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1055
  also have "... \<leadsto>* bders (bders_simp r xs) [x]" using IH
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1056
    by (simp add: rewrites_preserves_bder)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1057
  also have "... \<leadsto>* bders_simp (bders_simp r xs) [x]" using IH
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1058
    by (simp add: rewrites_to_bsimp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1059
  finally show "bders r (xs @ [x]) \<leadsto>* bders_simp r (xs @ [x])" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1060
    by (simp add: bders_simp_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1061
qed
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1062
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1063
lemma main_aux: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1064
  assumes "bnullable (bders r s)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1065
  shows "bmkeps (bders r s) = bmkeps (bders_simp r s)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1066
proof -
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1067
  have "bders r s \<leadsto>* bders_simp r s" by (rule central)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1068
  then 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1069
  show "bmkeps (bders r s) = bmkeps (bders_simp r s)" using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1070
    by (rule rewrites_bmkeps)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1071
qed  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1072
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1073
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1074
theorem main_blexer_simp: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1075
  shows "blexer r s = blexer_simp r s"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1076
  unfolding blexer_def blexer_simp_def
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1077
  by (metis central main_aux rewrites_bnullable_eq)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1078
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1079
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1080
theorem blexersimp_correctness: 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1081
  shows "lexer r s = blexer_simp r s"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1082
  using blexer_correctness main_blexer_simp by simp
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1083
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1084
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1085
(* below is the idempotency of bsimp *)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1086
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1087
lemma bsimp_ASEQ_fuse:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1088
  shows "fuse bs1 (bsimp_ASEQ bs2 r1 r2) = bsimp_ASEQ (bs1 @ bs2) r1 r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1089
  apply(induct r1 r2 arbitrary: bs1 bs2 rule: bsimp_ASEQ.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1090
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1091
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1092
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1093
lemma bsimp_AALTs_fuse:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1094
  assumes "\<forall>r \<in> set rs. fuse bs1 (fuse bs2 r) = fuse (bs1 @ bs2) r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1095
  shows "fuse bs1 (bsimp_AALTs bs2 rs) = bsimp_AALTs (bs1 @ bs2) rs"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1096
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1097
  apply(induct bs2 rs arbitrary: bs1 rule: bsimp_AALTs.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1098
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1099
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1100
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1101
lemma bsimp_fuse:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1102
  shows "fuse bs (bsimp r) = bsimp (fuse bs r)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1103
  apply(induct r arbitrary: bs)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1104
  apply(simp_all add: bsimp_ASEQ_fuse bsimp_AALTs_fuse fuse_append)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1105
  done
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1106
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1107
lemma bsimp_ASEQ_idem:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1108
  assumes "bsimp (bsimp r1) = bsimp r1" "bsimp (bsimp r2) = bsimp r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1109
  shows "bsimp (bsimp_ASEQ x1 (bsimp r1) (bsimp r2)) = bsimp_ASEQ x1 (bsimp r1) (bsimp r2)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1110
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1111
  apply(case_tac "bsimp r1 = AZERO")
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1112
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1113
  apply(case_tac "bsimp r2 = AZERO")
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1114
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1115
  apply(case_tac "\<exists>bs. bsimp r1 = AONE bs")
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1116
  apply(auto)[1]
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1117
  apply (metis bsimp_fuse)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1118
  apply(simp add: bsimp_ASEQ1)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1119
  done  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1120
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1121
lemma bsimp_AALTs_idem:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1122
  assumes "\<forall>r \<in> set rs. bsimp (bsimp r) = bsimp r" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1123
  shows "bsimp (bsimp_AALTs bs rs) = bsimp_AALTs bs (map bsimp rs)" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1124
  using assms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1125
  apply(induct bs rs rule: bsimp_AALTs.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1126
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1127
   apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1128
  using bsimp_fuse apply presburger
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1129
  oops   
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1130
  
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1131
lemma bsimp_idem_rev:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1132
  shows "\<nexists>r2. bsimp r1 \<leadsto> r2"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1133
  apply(induct r1 rule: bsimp.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1134
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1135
  defer
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1136
  defer
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1137
  using rrewrite.simps apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1138
  using rrewrite.cases apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1139
  using rrewrite.simps apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1140
  using rrewrite.cases apply blast
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1141
  apply(case_tac "bsimp r1 = AZERO")
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1142
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1143
  apply(case_tac "bsimp r2 = AZERO")
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1144
  apply(simp)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1145
  apply(case_tac "\<exists>bs. bsimp r1 = AONE bs")
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1146
  apply(auto)[1]
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1147
  prefer 2
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1148
  apply (smt (verit, best) arexp.distinct(25) arexp.inject(3) bsimp_ASEQ1 rrewrite.simps)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1149
  defer
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1150
  oops
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1151
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1152
lemma bsimp_idem:
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1153
  shows "bsimp (bsimp r) = bsimp r"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1154
  apply(induct r rule: bsimp.induct)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1155
  apply(auto)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1156
  using bsimp_ASEQ_idem apply presburger
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1157
  oops
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1158
409
Chengsong
parents: 406
diff changeset
  1159
Chengsong
parents: 406
diff changeset
  1160
lemma normal_form: 
Chengsong
parents: 406
diff changeset
  1161
  shows "\<forall>r. \<nexists> r'. bsimp r \<leadsto> r'"
Chengsong
parents: 406
diff changeset
  1162
Chengsong
parents: 406
diff changeset
  1163
  oops
Chengsong
parents: 406
diff changeset
  1164
Chengsong
parents: 406
diff changeset
  1165
lemma another_normal:
Chengsong
parents: 406
diff changeset
  1166
  shows "\<nexists>r'. bsimp r \<leadsto> r'"
Chengsong
parents: 406
diff changeset
  1167
  oops
Chengsong
parents: 406
diff changeset
  1168
406
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1169
export_code blexer_simp blexer lexer bders bders_simp in Scala module_name VerifiedLexers
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1170
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1171
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1172
unused_thms
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1173
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1174
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1175
fun concatLen:: "arexp \<Rightarrow> int" 
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1176
  where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1177
"concatLen AZERO = 0"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1178
| "concatLen (AONE bs) = 0"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1179
| "concatLen (ACHAR bs a) = 0"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1180
| "concatLen (ASEQ bs a1 a2) = 1 + (max (concatLen a1) ( concatLen a2))"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1181
| "concatLen (ASTAR bs r0) = 1 + (concatLen r0)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1182
| "concatLen (AALTS bs as) =  foldl max (map concatLen as)"
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1183
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1184
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1185
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1186
inductive aggressive:: "arexp \<Rightarrow> arexp \<Rightarrow> bool" ("_ \<leadsto>? _" [99, 99] 99)
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1187
  where
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1188
 "ASEQ bs (AALTs bs1 rs) r \<leadsto>? AALTs (bs@bs1) (map (\<lambda>r'. ASEQ [] r' r) rs) "
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1189
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1190
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1191
4511cc1bf1f0 bound4CT
Chengsong
parents:
diff changeset
  1192
end