thys4/posix/Blexer.thy
author Chengsong
Mon, 29 Aug 2022 23:16:28 +0100
changeset 587 3198605ac648
permissions -rw-r--r--
bsimp idempotency
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     1
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     2
theory Blexer
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     3
  imports "Lexer"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     4
begin
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     5
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     6
section \<open>Bit-Encodings\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     7
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     8
datatype bit = Z | S
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     9
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    10
fun code :: "val \<Rightarrow> bit list"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    11
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    12
  "code Void = []"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    13
| "code (Char c) = []"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    14
| "code (Left v) = Z # (code v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    15
| "code (Right v) = S # (code v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    16
| "code (Seq v1 v2) = (code v1) @ (code v2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    17
| "code (Stars []) = [S]"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    18
| "code (Stars (v # vs)) =  (Z # code v) @ code (Stars vs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    19
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    20
fun sz where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    21
  "sz ZERO = 0"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    22
| "sz ONE = 0"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    23
| "sz (CH _) = 0"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    24
| "sz (SEQ r1 r2) = 1 + sz r1 + sz r2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    25
| "sz (ALT r1 r2) = 1 + sz r1 + sz r2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    26
| "sz (STAR r) = 1 + sz r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    27
| "sz (NTIMES r n) = 1 + n + sz r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    28
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    29
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    30
fun 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    31
  Stars_add :: "val \<Rightarrow> val \<Rightarrow> val"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    32
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    33
  "Stars_add v (Stars vs) = Stars (v # vs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    34
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    35
function (sequential)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    36
  decode' :: "bit list \<Rightarrow> rexp \<Rightarrow> (val * bit list)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    37
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    38
  "decode' bs ZERO = (undefined, bs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    39
| "decode' bs ONE = (Void, bs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    40
| "decode' bs (CH d) = (Char d, bs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    41
| "decode' [] (ALT r1 r2) = (Void, [])"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    42
| "decode' (Z # bs) (ALT r1 r2) = (let (v, bs') = decode' bs r1 in (Left v, bs'))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    43
| "decode' (S # bs) (ALT r1 r2) = (let (v, bs') = decode' bs r2 in (Right v, bs'))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    44
| "decode' bs (SEQ r1 r2) = (let (v1, bs') = decode' bs r1 in
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    45
                             let (v2, bs'') = decode' bs' r2 in (Seq v1 v2, bs''))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    46
| "decode' [] (STAR r) = (Void, [])"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    47
| "decode' (S # bs) (STAR r) = (Stars [], bs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    48
| "decode' (Z # bs) (STAR r) = (let (v, bs') = decode' bs r in
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    49
                                    let (vs, bs'') = decode' bs' (STAR r) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    50
                                    in (Stars_add v vs, bs''))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    51
| "decode' [] (NTIMES r n) = (Void, [])"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    52
| "decode' (S # bs) (NTIMES r n) = (Stars [], bs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    53
(*| "decode' (Z # bs) (NTIMES r 0) = (undefined, bs)"*)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    54
| "decode' (Z # bs) (NTIMES r n) = (let (v, bs') = decode' bs r in
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    55
                                    let (vs, bs'') = decode' bs' (NTIMES r (n - 1)) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    56
                                    in (Stars_add v vs, bs''))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    57
by pat_completeness auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    58
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    59
lemma decode'_smaller:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    60
  assumes "decode'_dom (bs, r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    61
  shows "length (snd (decode' bs r)) \<le> length bs"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    62
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    63
apply(induct bs r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    64
apply(auto simp add: decode'.psimps split: prod.split)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    65
using dual_order.trans apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    66
apply (meson dual_order.trans le_SucI)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    67
  apply (meson le_SucI le_trans)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    68
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    69
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    70
termination "decode'"  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    71
apply(relation "inv_image (measure(%cs. sz cs) <*lex*> measure(%s. size s)) (%(ds,r). (r,ds))") 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    72
apply(auto dest!: decode'_smaller)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    73
   apply (metis less_Suc_eq_le snd_conv)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    74
  by (metis less_Suc_eq_le snd_conv)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    75
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    76
definition
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    77
  decode :: "bit list \<Rightarrow> rexp \<Rightarrow> val option"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    78
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    79
  "decode ds r \<equiv> (let (v, ds') = decode' ds r 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    80
                  in (if ds' = [] then Some v else None))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    81
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    82
lemma decode'_code_Stars:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    83
  assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> (\<forall>x. decode' (code v @ x) r = (v, x)) \<and> flat v \<noteq> []" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    84
  shows "decode' (code (Stars vs) @ ds) (STAR r) = (Stars vs, ds)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    85
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    86
  apply(induct vs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    87
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    88
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    89
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    90
lemma decode'_code_NTIMES:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    91
  assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> (\<forall>x. decode' (code v @ x) r = (v, x))" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    92
  shows "decode' (code (Stars vs) @ ds) (NTIMES r n) = (Stars vs, ds)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    93
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    94
  apply(induct vs arbitrary: n r ds)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    95
   apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    96
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    97
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    98
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    99
lemma decode'_code:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   100
  assumes "\<Turnstile> v : r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   101
  shows "decode' ((code v) @ ds) r = (v, ds)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   102
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   103
  apply(induct v r arbitrary: ds) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   104
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   105
  using decode'_code_Stars apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   106
   by (metis Un_iff decode'_code_NTIMES set_append)  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   107
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   108
lemma decode_code:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   109
  assumes "\<Turnstile> v : r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   110
  shows "decode (code v) r = Some v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   111
  using assms unfolding decode_def
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   112
  by (smt append_Nil2 decode'_code old.prod.case)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   113
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   114
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   115
section {* Annotated Regular Expressions *}
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   116
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   117
datatype arexp = 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   118
  AZERO
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   119
| AONE "bit list"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   120
| ACHAR "bit list" char
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   121
| ASEQ "bit list" arexp arexp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   122
| AALTs "bit list" "arexp list"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   123
| ASTAR "bit list" arexp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   124
| ANTIMES "bit list" arexp nat
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   125
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   126
abbreviation
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   127
  "AALT bs r1 r2 \<equiv> AALTs bs [r1, r2]"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   128
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   129
fun asize :: "arexp \<Rightarrow> nat" where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   130
  "asize AZERO = 1"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   131
| "asize (AONE cs) = 1" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   132
| "asize (ACHAR cs c) = 1"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   133
| "asize (AALTs cs rs) = Suc (sum_list (map asize rs))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   134
| "asize (ASEQ cs r1 r2) = Suc (asize r1 + asize r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   135
| "asize (ASTAR cs r) = Suc (asize r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   136
| "asize (ANTIMES cs r n) = Suc (asize r) + n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   137
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   138
fun 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   139
  erase :: "arexp \<Rightarrow> rexp"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   140
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   141
  "erase AZERO = ZERO"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   142
| "erase (AONE _) = ONE"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   143
| "erase (ACHAR _ c) = CH c"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   144
| "erase (AALTs _ []) = ZERO"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   145
| "erase (AALTs _ [r]) = (erase r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   146
| "erase (AALTs bs (r#rs)) = ALT (erase r) (erase (AALTs bs rs))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   147
| "erase (ASEQ _ r1 r2) = SEQ (erase r1) (erase r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   148
| "erase (ASTAR _ r) = STAR (erase r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   149
| "erase (ANTIMES _ r n) = NTIMES (erase r) n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   150
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   151
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   152
fun fuse :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp" where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   153
  "fuse bs AZERO = AZERO"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   154
| "fuse bs (AONE cs) = AONE (bs @ cs)" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   155
| "fuse bs (ACHAR cs c) = ACHAR (bs @ cs) c"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   156
| "fuse bs (AALTs cs rs) = AALTs (bs @ cs) rs"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   157
| "fuse bs (ASEQ cs r1 r2) = ASEQ (bs @ cs) r1 r2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   158
| "fuse bs (ASTAR cs r) = ASTAR (bs @ cs) r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   159
| "fuse bs (ANTIMES cs r n) = ANTIMES (bs @ cs) r n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   160
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   161
lemma fuse_append:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   162
  shows "fuse (bs1 @ bs2) r = fuse bs1 (fuse bs2 r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   163
  apply(induct r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   164
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   165
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   166
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   167
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   168
fun intern :: "rexp \<Rightarrow> arexp" where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   169
  "intern ZERO = AZERO"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   170
| "intern ONE = AONE []"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   171
| "intern (CH c) = ACHAR [] c"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   172
| "intern (ALT r1 r2) = AALT [] (fuse [Z] (intern r1)) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   173
                                (fuse [S]  (intern r2))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   174
| "intern (SEQ r1 r2) = ASEQ [] (intern r1) (intern r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   175
| "intern (STAR r) = ASTAR [] (intern r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   176
| "intern (NTIMES r n) = ANTIMES [] (intern r) n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   177
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   178
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   179
fun retrieve :: "arexp \<Rightarrow> val \<Rightarrow> bit list" where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   180
  "retrieve (AONE bs) Void = bs"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   181
| "retrieve (ACHAR bs c) (Char d) = bs"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   182
| "retrieve (AALTs bs [r]) v = bs @ retrieve r v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   183
| "retrieve (AALTs bs (r#rs)) (Left v) = bs @ retrieve r v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   184
| "retrieve (AALTs bs (r#rs)) (Right v) = bs @ retrieve (AALTs [] rs) v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   185
| "retrieve (ASEQ bs r1 r2) (Seq v1 v2) = bs @ retrieve r1 v1 @ retrieve r2 v2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   186
| "retrieve (ASTAR bs r) (Stars []) = bs @ [S]"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   187
| "retrieve (ASTAR bs r) (Stars (v#vs)) = 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   188
     bs @ [Z] @ retrieve r v @ retrieve (ASTAR [] r) (Stars vs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   189
| "retrieve (ANTIMES bs r 0) (Stars []) = bs @ [S]"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   190
| "retrieve (ANTIMES bs r (Suc n)) (Stars (v#vs)) = 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   191
     bs @ [Z] @ retrieve r v @ retrieve (ANTIMES [] r n) (Stars vs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   192
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   193
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   194
fun
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   195
 bnullable :: "arexp \<Rightarrow> bool"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   196
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   197
  "bnullable (AZERO) = False"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   198
| "bnullable (AONE bs) = True"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   199
| "bnullable (ACHAR bs c) = False"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   200
| "bnullable (AALTs bs rs) = (\<exists>r \<in> set rs. bnullable r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   201
| "bnullable (ASEQ bs r1 r2) = (bnullable r1 \<and> bnullable r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   202
| "bnullable (ASTAR bs r) = True"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   203
| "bnullable (ANTIMES bs r n) = (if n  = 0 then True else bnullable r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   204
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   205
abbreviation
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   206
  bnullables :: "arexp list \<Rightarrow> bool"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   207
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   208
  "bnullables rs \<equiv> (\<exists>r \<in> set rs. bnullable r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   209
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   210
function (sequential)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   211
  bmkeps :: "arexp \<Rightarrow> bit list" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   212
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   213
  "bmkeps(AONE bs) = bs"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   214
| "bmkeps(ASEQ bs r1 r2) = bs @ (bmkeps r1) @ (bmkeps r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   215
| "bmkeps(AALTs bs (r#rs)) = 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   216
    (if bnullable(r) then (bs @ bmkeps r) else (bmkeps (AALTs bs rs)))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   217
| "bmkeps(ASTAR bs r) = bs @ [S]"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   218
| "bmkeps(ANTIMES bs r 0) = bs @ [S]"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   219
| "bmkeps(ANTIMES bs r (Suc n)) = bs @ [Z] @ (bmkeps r) @ bmkeps(ANTIMES [] r n)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   220
apply(pat_completeness)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   221
apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   222
done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   223
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   224
termination "bmkeps"  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   225
apply(relation "measure asize") 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   226
        apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   227
  using asize.elims by force
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   228
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   229
fun
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   230
   bmkepss :: "arexp list \<Rightarrow> bit list"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   231
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   232
  "bmkepss (r # rs) = (if bnullable(r) then (bmkeps r) else (bmkepss rs))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   233
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   234
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   235
lemma bmkepss1:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   236
  assumes "\<not> bnullables rs1"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   237
  shows "bmkepss (rs1 @ rs2) = bmkepss rs2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   238
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   239
  by(induct rs1) (auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   240
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   241
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   242
lemma bmkepss2:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   243
  assumes "bnullables rs1"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   244
  shows "bmkepss (rs1 @ rs2) = bmkepss rs1"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   245
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   246
  by (induct rs1) (auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   247
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   248
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   249
fun
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   250
 bder :: "char \<Rightarrow> arexp \<Rightarrow> arexp"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   251
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   252
  "bder c (AZERO) = AZERO"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   253
| "bder c (AONE bs) = AZERO"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   254
| "bder c (ACHAR bs d) = (if c = d then AONE bs else AZERO)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   255
| "bder c (AALTs bs rs) = AALTs bs (map (bder c) rs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   256
| "bder c (ASEQ bs r1 r2) = 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   257
     (if bnullable r1
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   258
      then AALT bs (ASEQ [] (bder c r1) r2) (fuse (bmkeps r1) (bder c r2))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   259
      else ASEQ bs (bder c r1) r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   260
| "bder c (ASTAR bs r) = ASEQ (bs @ [Z]) (bder c r) (ASTAR [] r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   261
| "bder c (ANTIMES bs r n) = (if n = 0 then AZERO else ASEQ (bs @ [Z]) (bder c r) (ANTIMES [] r (n - 1)))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   262
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   263
fun 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   264
  bders :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   265
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   266
  "bders r [] = r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   267
| "bders r (c#s) = bders (bder c r) s"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   268
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   269
lemma bders_append:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   270
  "bders c (s1 @ s2) = bders (bders c s1) s2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   271
  apply(induct s1 arbitrary: c s2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   272
  apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   273
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   274
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   275
lemma bnullable_correctness:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   276
  shows "nullable (erase r) = bnullable r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   277
  apply(induct r rule: erase.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   278
  apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   279
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   280
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   281
lemma erase_fuse:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   282
  shows "erase (fuse bs r) = erase r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   283
  apply(induct r rule: erase.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   284
  apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   285
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   286
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   287
lemma erase_intern [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   288
  shows "erase (intern r) = r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   289
  apply(induct r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   290
  apply(simp_all add: erase_fuse)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   291
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   292
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   293
lemma erase_bder [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   294
  shows "erase (bder a r) = der a (erase r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   295
  apply(induct r rule: erase.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   296
  apply(simp_all add: erase_fuse bnullable_correctness)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   297
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   298
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   299
lemma erase_bders [simp]:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   300
  shows "erase (bders r s) = ders s (erase r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   301
  apply(induct s arbitrary: r )
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   302
  apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   303
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   304
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   305
lemma bnullable_fuse:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   306
  shows "bnullable (fuse bs r) = bnullable r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   307
  apply(induct r arbitrary: bs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   308
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   309
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   310
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   311
lemma retrieve_encode_STARS:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   312
  assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> code v = retrieve (intern r) v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   313
  shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   314
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   315
  apply(induct vs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   316
  apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   317
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   318
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   319
lemma retrieve_encode_NTIMES:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   320
  assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> code v = retrieve (intern r) v" "length vs = n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   321
  shows "code (Stars vs) = retrieve (ANTIMES [] (intern r) n) (Stars vs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   322
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   323
  apply(induct vs arbitrary: n)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   324
   apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   325
  by force
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   326
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   327
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   328
lemma retrieve_fuse2:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   329
  assumes "\<Turnstile> v : (erase r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   330
  shows "retrieve (fuse bs r) v = bs @ retrieve r v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   331
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   332
  apply(induct r arbitrary: v bs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   333
  apply(auto elim: Prf_elims)[4]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   334
  apply(case_tac x2a)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   335
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   336
  using Prf_elims(1) apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   337
  apply(case_tac x2a)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   338
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   339
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   340
  apply(case_tac list)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   341
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   342
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   343
  apply (smt (verit, best) Prf_elims(3) append_assoc retrieve.simps(4) retrieve.simps(5))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   344
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   345
  using retrieve_encode_STARS
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   346
  apply(auto elim!: Prf_elims)[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   347
  apply(case_tac vs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   348
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   349
   apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   350
  (* NTIMES *)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   351
  apply(auto elim!: Prf_elims)[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   352
  apply(case_tac vs1)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   353
   apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   354
  apply(case_tac vs2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   355
   apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   356
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   357
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   358
lemma retrieve_fuse:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   359
  assumes "\<Turnstile> v : r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   360
  shows "retrieve (fuse bs (intern r)) v = bs @ retrieve (intern r) v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   361
  using assms 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   362
  by (simp_all add: retrieve_fuse2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   363
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   364
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   365
lemma retrieve_code:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   366
  assumes "\<Turnstile> v : r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   367
  shows "code v = retrieve (intern r) v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   368
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   369
  apply(induct v r )
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   370
        apply(simp_all add: retrieve_fuse retrieve_encode_STARS)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   371
  apply(subst retrieve_encode_NTIMES)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   372
    apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   373
  done 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   374
 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   375
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   376
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   377
lemma retrieve_AALTs_bnullable1:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   378
  assumes "bnullable r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   379
  shows "retrieve (AALTs bs (r # rs)) (mkeps (erase (AALTs bs (r # rs))))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   380
         = bs @ retrieve r (mkeps (erase r))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   381
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   382
  apply(case_tac rs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   383
  apply(auto simp add: bnullable_correctness)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   384
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   385
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   386
lemma retrieve_AALTs_bnullable2:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   387
  assumes "\<not>bnullable r" "bnullables rs"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   388
  shows "retrieve (AALTs bs (r # rs)) (mkeps (erase (AALTs bs (r # rs))))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   389
         = retrieve (AALTs bs rs) (mkeps (erase (AALTs bs rs)))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   390
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   391
  apply(induct rs arbitrary: r bs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   392
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   393
  using bnullable_correctness apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   394
  apply(case_tac rs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   395
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   396
  using bnullable_correctness apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   397
  apply(case_tac rs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   398
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   399
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   400
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   401
lemma bmkeps_retrieve_AALTs: 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   402
  assumes "\<forall>r \<in> set rs. bnullable r \<longrightarrow> bmkeps r = retrieve r (mkeps (erase r))" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   403
          "bnullables rs"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   404
  shows "bs @ bmkepss rs = retrieve (AALTs bs rs) (mkeps (erase (AALTs bs rs)))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   405
 using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   406
  apply(induct rs arbitrary: bs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   407
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   408
  using retrieve_AALTs_bnullable1 apply presburger
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   409
  apply (metis retrieve_AALTs_bnullable2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   410
  apply (simp add: retrieve_AALTs_bnullable1)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   411
  by (metis retrieve_AALTs_bnullable2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   412
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   413
lemma bmkeps_retrieve_ANTIMES: 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   414
  assumes "if n = 0 then True else bmkeps r = retrieve r (mkeps (erase r))" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   415
  and     "bnullable (ANTIMES bs r n)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   416
  shows "bmkeps (ANTIMES bs r n) = retrieve (ANTIMES bs r n) (Stars (replicate n (mkeps (erase r))))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   417
 using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   418
  apply(induct n arbitrary: r bs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   419
  apply(auto)[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   420
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   421
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   422
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   423
lemma bmkeps_retrieve:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   424
  assumes "bnullable r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   425
  shows "bmkeps r = retrieve r (mkeps (erase r))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   426
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   427
  apply(induct r rule: bmkeps.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   428
        apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   429
  apply (simp add: retrieve_AALTs_bnullable1)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   430
  using retrieve_AALTs_bnullable1 apply force
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   431
  by (metis retrieve_AALTs_bnullable2)  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   432
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   433
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   434
lemma bder_retrieve:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   435
  assumes "\<Turnstile> v : der c (erase r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   436
  shows "retrieve (bder c r) v = retrieve r (injval (erase r) c v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   437
  using assms  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   438
  apply(induct r arbitrary: v rule: erase.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   439
  using Prf_elims(1) apply auto[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   440
  using Prf_elims(1) apply auto[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   441
  apply(auto)[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   442
  apply (metis Prf_elims(4) injval.simps(1) retrieve.simps(1) retrieve.simps(2))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   443
  using Prf_elims(1) apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   444
  (* AALTs case *)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   445
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   446
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   447
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   448
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   449
  apply(rename_tac "r\<^sub>1" "r\<^sub>2" rs v)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   450
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   451
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   452
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   453
  apply(case_tac rs)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   454
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   455
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   456
  using Prf_elims(3) apply fastforce
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   457
  (* ASEQ case *) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   458
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   459
  apply(case_tac "nullable (erase r1)")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   460
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   461
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   462
  using Prf_elims(2) bnullable_correctness apply force
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   463
  apply (simp add: bmkeps_retrieve bnullable_correctness retrieve_fuse2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   464
  apply (simp add: bmkeps_retrieve bnullable_correctness retrieve_fuse2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   465
  using Prf_elims(2) apply force
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   466
  (* ASTAR case *)  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   467
  apply(rename_tac bs r v)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   468
  apply(simp)  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   469
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   470
  apply(clarify)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   471
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   472
  apply(clarify)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   473
   apply (simp add: retrieve_fuse2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   474
  (* ANTIMES case *)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   475
  apply(auto)  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   476
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   477
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   478
  apply(clarify)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   479
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   480
  apply(clarify)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   481
  by (metis (full_types) Suc_pred append_assoc injval.simps(8) retrieve.simps(10) retrieve.simps(6))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   482
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   483
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   484
lemma MAIN_decode:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   485
  assumes "\<Turnstile> v : ders s r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   486
  shows "Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   487
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   488
proof (induct s arbitrary: v rule: rev_induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   489
  case Nil
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   490
  have "\<Turnstile> v : ders [] r" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   491
  then have "\<Turnstile> v : r" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   492
  then have "Some v = decode (retrieve (intern r) v) r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   493
    using decode_code retrieve_code by auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   494
  then show "Some (flex r id [] v) = decode (retrieve (bders (intern r) []) v) r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   495
    by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   496
next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   497
  case (snoc c s v)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   498
  have IH: "\<And>v. \<Turnstile> v : ders s r \<Longrightarrow> 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   499
     Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   500
  have asm: "\<Turnstile> v : ders (s @ [c]) r" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   501
  then have asm2: "\<Turnstile> injval (ders s r) c v : ders s r" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   502
    by (simp add: Prf_injval ders_append)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   503
  have "Some (flex r id (s @ [c]) v) = Some (flex r id s (injval (ders s r) c v))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   504
    by (simp add: flex_append)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   505
  also have "... = decode (retrieve (bders (intern r) s) (injval (ders s r) c v)) r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   506
    using asm2 IH by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   507
  also have "... = decode (retrieve (bder c (bders (intern r) s)) v) r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   508
    using asm by (simp_all add: bder_retrieve ders_append)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   509
  finally show "Some (flex r id (s @ [c]) v) = 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   510
                 decode (retrieve (bders (intern r) (s @ [c])) v) r" by (simp add: bders_append)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   511
qed
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   512
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   513
definition blexer where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   514
 "blexer r s \<equiv> if bnullable (bders (intern r) s) then 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   515
                decode (bmkeps (bders (intern r) s)) r else None"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   516
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   517
lemma blexer_correctness:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   518
  shows "blexer r s = lexer r s"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   519
proof -
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   520
  { define bds where "bds \<equiv> bders (intern r) s"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   521
    define ds  where "ds \<equiv> ders s r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   522
    assume asm: "nullable ds"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   523
    have era: "erase bds = ds" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   524
      unfolding ds_def bds_def by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   525
    have mke: "\<Turnstile> mkeps ds : ds"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   526
      using asm by (simp add: mkeps_nullable)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   527
    have "decode (bmkeps bds) r = decode (retrieve bds (mkeps ds)) r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   528
      using bmkeps_retrieve
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   529
      using asm era
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   530
      using bnullable_correctness by force 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   531
    also have "... =  Some (flex r id s (mkeps ds))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   532
      using mke by (simp_all add: MAIN_decode ds_def bds_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   533
    finally have "decode (bmkeps bds) r = Some (flex r id s (mkeps ds))" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   534
      unfolding bds_def ds_def .
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   535
  }
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   536
  then show "blexer r s = lexer r s"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   537
    unfolding blexer_def lexer_flex
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   538
    by (auto simp add: bnullable_correctness[symmetric])
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   539
qed
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   540
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   541
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   542
unused_thms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   543
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   544
end