thys2/Sulzmann.thy
author Chengsong
Fri, 30 Dec 2022 23:41:44 +0000
changeset 640 bd1354127574
parent 365 ec5e4fe4cc70
permissions -rw-r--r--
more proofreading done, last version before submission
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
365
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     1
   
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     2
theory Sulzmann
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     3
  imports "Lexer" 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     4
begin
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     5
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     6
section {* Bit-Encodings *}
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     7
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     8
datatype bit = Z | S
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
     9
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    10
fun 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    11
  code :: "val \<Rightarrow> bit list"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    12
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    13
  "code Void = []"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    14
| "code (Char c) = []"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    15
| "code (Left v) = Z # (code v)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    16
| "code (Right v) = S # (code v)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    17
| "code (Seq v1 v2) = (code v1) @ (code v2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    18
| "code (Stars []) = [S]"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    19
| "code (Stars (v # vs)) =  (Z # code v) @ code (Stars vs)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    20
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    21
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    22
fun 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    23
  Stars_add :: "val \<Rightarrow> val \<Rightarrow> val"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    24
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    25
  "Stars_add v (Stars vs) = Stars (v # vs)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    26
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    27
function
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    28
  decode' :: "bit list \<Rightarrow> rexp \<Rightarrow> (val * bit list)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    29
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    30
  "decode' ds ZERO = (Void, [])"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    31
| "decode' ds ONE = (Void, ds)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    32
| "decode' ds (CH d) = (Char d, ds)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    33
| "decode' [] (ALT r1 r2) = (Void, [])"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    34
| "decode' (Z # ds) (ALT r1 r2) = (let (v, ds') = decode' ds r1 in (Left v, ds'))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    35
| "decode' (S # ds) (ALT r1 r2) = (let (v, ds') = decode' ds r2 in (Right v, ds'))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    36
| "decode' ds (SEQ r1 r2) = (let (v1, ds') = decode' ds r1 in
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    37
                             let (v2, ds'') = decode' ds' r2 in (Seq v1 v2, ds''))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    38
| "decode' [] (STAR r) = (Void, [])"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    39
| "decode' (S # ds) (STAR r) = (Stars [], ds)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    40
| "decode' (Z # ds) (STAR r) = (let (v, ds') = decode' ds r in
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    41
                                    let (vs, ds'') = decode' ds' (STAR r) 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    42
                                    in (Stars_add v vs, ds''))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    43
by pat_completeness auto
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    44
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    45
lemma decode'_smaller:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    46
  assumes "decode'_dom (ds, r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    47
  shows "length (snd (decode' ds r)) \<le> length ds"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    48
using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    49
apply(induct ds r)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    50
apply(auto simp add: decode'.psimps split: prod.split)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    51
using dual_order.trans apply blast
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    52
by (meson dual_order.trans le_SucI)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    53
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    54
termination "decode'"  
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    55
apply(relation "inv_image (measure(%cs. size cs) <*lex*> measure(%s. size s)) (%(ds,r). (r,ds))") 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    56
apply(auto dest!: decode'_smaller)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    57
by (metis less_Suc_eq_le snd_conv)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    58
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    59
definition
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    60
  decode :: "bit list \<Rightarrow> rexp \<Rightarrow> val option"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    61
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    62
  "decode ds r \<equiv> (let (v, ds') = decode' ds r 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    63
                  in (if ds' = [] then Some v else None))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    64
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    65
lemma decode'_code_Stars:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    66
  assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> (\<forall>x. decode' (code v @ x) r = (v, x)) \<and> flat v \<noteq> []" 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    67
  shows "decode' (code (Stars vs) @ ds) (STAR r) = (Stars vs, ds)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    68
  using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    69
  apply(induct vs)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    70
  apply(auto)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    71
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    72
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    73
lemma decode'_code:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    74
  assumes "\<Turnstile> v : r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    75
  shows "decode' ((code v) @ ds) r = (v, ds)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    76
using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    77
  apply(induct v r arbitrary: ds) 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    78
  apply(auto)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    79
  using decode'_code_Stars by blast
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    80
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    81
lemma decode_code:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    82
  assumes "\<Turnstile> v : r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    83
  shows "decode (code v) r = Some v"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    84
  using assms unfolding decode_def
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    85
  by (smt append_Nil2 decode'_code old.prod.case)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    86
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    87
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    88
datatype arexp =
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    89
  AZERO
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    90
| AONE "bit list"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    91
| ACH "bit list" char
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    92
| ASEQ "bit list" arexp arexp
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    93
| AALT "bit list" arexp arexp
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    94
| ASTAR "bit list" arexp
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    95
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    96
fun fuse :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp" where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    97
  "fuse bs AZERO = AZERO"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    98
| "fuse bs (AONE cs) = AONE (bs @ cs)" 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
    99
| "fuse bs (ACH cs c) = ACH (bs @ cs) c"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   100
| "fuse bs (AALT cs r1 r2) = AALT (bs @ cs) r1 r2"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   101
| "fuse bs (ASEQ cs r1 r2) = ASEQ (bs @ cs) r1 r2"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   102
| "fuse bs (ASTAR cs r) = ASTAR (bs @ cs) r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   103
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   104
fun intern :: "rexp \<Rightarrow> arexp" where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   105
  "intern ZERO = AZERO"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   106
| "intern ONE = AONE []"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   107
| "intern (CH c) = ACH [] c"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   108
| "intern (ALT r1 r2) = AALT [] (fuse [Z] (intern r1)) 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   109
                                (fuse [S]  (intern r2))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   110
| "intern (SEQ r1 r2) = ASEQ [] (intern r1) (intern r2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   111
| "intern (STAR r) = ASTAR [] (intern r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   112
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   113
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   114
fun retrieve :: "arexp \<Rightarrow> val \<Rightarrow> bit list" where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   115
  "retrieve (AONE bs) Void = bs"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   116
| "retrieve (ACH bs c) (Char d) = bs"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   117
| "retrieve (AALT bs r1 r2) (Left v) = bs @ retrieve r1 v"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   118
| "retrieve (AALT bs r1 r2) (Right v) = bs @ retrieve r2 v"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   119
| "retrieve (ASEQ bs r1 r2) (Seq v1 v2) = bs @ retrieve r1 v1 @ retrieve r2 v2"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   120
| "retrieve (ASTAR bs r) (Stars []) = bs @ [S]"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   121
| "retrieve (ASTAR bs r) (Stars (v#vs)) = 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   122
     bs @ [Z] @ retrieve r v @ retrieve (ASTAR [] r) (Stars vs)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   123
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   124
fun 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   125
  erase :: "arexp \<Rightarrow> rexp"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   126
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   127
  "erase AZERO = ZERO"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   128
| "erase (AONE _) = ONE"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   129
| "erase (ACH _ c) = CH c"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   130
| "erase (AALT _ r1 r2) = ALT (erase r1) (erase r2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   131
| "erase (ASEQ _ r1 r2) = SEQ (erase r1) (erase r2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   132
| "erase (ASTAR _ r) = STAR (erase r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   133
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   134
fun
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   135
 bnullable :: "arexp \<Rightarrow> bool"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   136
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   137
  "bnullable (AZERO) = False"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   138
| "bnullable (AONE bs) = True"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   139
| "bnullable (ACH bs c) = False"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   140
| "bnullable (AALT bs r1 r2) = (bnullable r1 \<or> bnullable r2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   141
| "bnullable (ASEQ bs r1 r2) = (bnullable r1 \<and> bnullable r2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   142
| "bnullable (ASTAR bs r) = True"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   143
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   144
fun 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   145
  bmkeps :: "arexp \<Rightarrow> bit list"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   146
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   147
  "bmkeps(AONE bs) = bs"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   148
| "bmkeps(ASEQ bs r1 r2) = bs @ (bmkeps r1) @ (bmkeps r2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   149
| "bmkeps(AALT bs r1 r2) = (if bnullable(r1) then bs @ (bmkeps r1) else bs @ (bmkeps r2))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   150
| "bmkeps(ASTAR bs r) = bs @ [S]"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   151
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   152
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   153
fun
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   154
 bder :: "char \<Rightarrow> arexp \<Rightarrow> arexp"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   155
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   156
  "bder c (AZERO) = AZERO"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   157
| "bder c (AONE bs) = AZERO"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   158
| "bder c (ACH bs d) = (if c = d then AONE bs else AZERO)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   159
| "bder c (AALT bs r1 r2) = AALT bs (bder c r1) (bder c r2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   160
| "bder c (ASEQ bs r1 r2) = 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   161
     (if bnullable r1
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   162
      then AALT bs (ASEQ [] (bder c r1) r2) (fuse (bmkeps r1) (bder c r2))
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   163
      else ASEQ bs (bder c r1) r2)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   164
| "bder c (ASTAR bs r) = ASEQ bs (fuse [Z] (bder c r)) (ASTAR [] r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   165
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   166
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   167
fun 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   168
  bders :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   169
where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   170
  "bders r [] = r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   171
| "bders r (c#s) = bders (bder c r) s"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   172
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   173
lemma bders_append:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   174
  "bders r (s1 @ s2) = bders (bders r s1) s2"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   175
  apply(induct s1 arbitrary: r s2)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   176
  apply(simp_all)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   177
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   178
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   179
lemma bnullable_correctness:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   180
  shows "nullable (erase r) = bnullable r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   181
  apply(induct r)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   182
  apply(simp_all)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   183
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   184
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   185
lemma erase_fuse:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   186
  shows "erase (fuse bs r) = erase r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   187
  apply(induct r)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   188
  apply(simp_all)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   189
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   190
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   191
lemma erase_intern[simp]:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   192
  shows "erase (intern r) = r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   193
  apply(induct r)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   194
  apply(simp_all add: erase_fuse)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   195
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   196
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   197
lemma erase_bder[simp]:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   198
  shows "erase (bder a r) = der a (erase r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   199
  apply(induct r)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   200
  apply(simp_all add: erase_fuse bnullable_correctness)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   201
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   202
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   203
lemma erase_bders[simp]:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   204
  shows "erase (bders r s) = ders s (erase r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   205
  apply(induct s arbitrary: r )
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   206
  apply(simp_all)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   207
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   208
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   209
lemma retrieve_encode_STARS:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   210
  assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> code v = retrieve (intern r) v"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   211
  shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   212
  using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   213
  apply(induct vs)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   214
  apply(simp_all)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   215
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   216
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   217
lemma retrieve_fuse2:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   218
  assumes "\<Turnstile> v : (erase r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   219
  shows "retrieve (fuse bs r) v = bs @ retrieve r v"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   220
  using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   221
  apply(induct r arbitrary: v bs)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   222
  using retrieve_encode_STARS
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   223
  apply(auto elim!: Prf_elims)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   224
  apply(case_tac vs)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   225
  apply(simp)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   226
  apply(simp)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   227
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   228
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   229
lemma retrieve_fuse:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   230
  assumes "\<Turnstile> v : r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   231
  shows "retrieve (fuse bs (intern r)) v = bs @ retrieve (intern r) v"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   232
  using assms 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   233
  by (simp_all add: retrieve_fuse2)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   234
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   235
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   236
lemma retrieve_code:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   237
  assumes "\<Turnstile> v : r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   238
  shows "code v = retrieve (intern r) v"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   239
  using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   240
  apply(induct v r)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   241
  apply(simp_all add: retrieve_fuse retrieve_encode_STARS)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   242
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   243
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   244
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   245
lemma bmkeps_retrieve:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   246
  assumes "nullable (erase r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   247
  shows "bmkeps r = retrieve r (mkeps (erase r))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   248
  using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   249
  apply(induct r)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   250
       apply(simp)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   251
      apply(simp)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   252
     apply(simp)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   253
    apply(simp) 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   254
   apply(simp only: bmkeps.simps bnullable_correctness)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   255
  apply(auto simp only: split: if_split)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   256
  apply(auto simp add: bnullable_correctness)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   257
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   258
  
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   259
lemma bder_retrieve:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   260
  assumes "\<Turnstile> v : der c (erase r)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   261
  shows "retrieve (bder c r) v = retrieve r (injval (erase r) c v)"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   262
  using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   263
  apply(induct r arbitrary: v)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   264
  apply(auto elim!: Prf_elims simp add: retrieve_fuse2 bnullable_correctness bmkeps_retrieve)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   265
  done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   266
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   267
lemma MAIN_decode:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   268
  assumes "\<Turnstile> v : ders s r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   269
  shows "Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   270
  using assms
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   271
proof (induct s arbitrary: v rule: rev_induct)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   272
  case Nil
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   273
  have "\<Turnstile> v : ders [] r" by fact
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   274
  then have "\<Turnstile> v : r" by simp
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   275
  then have "Some v = decode (retrieve (intern r) v) r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   276
    using decode_code retrieve_code by auto
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   277
  then show "Some (flex r id [] v) = decode (retrieve (bders (intern r) []) v) r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   278
    by simp
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   279
next
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   280
  case (snoc c s v)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   281
  have IH: "\<And>v. \<Turnstile> v : ders s r \<Longrightarrow> 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   282
     Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r" by fact
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   283
  have asm: "\<Turnstile> v : ders (s @ [c]) r" by fact
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   284
  then have asm2: "\<Turnstile> injval (ders s r) c v : ders s r" 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   285
    by(simp add: Prf_injval ders_append)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   286
  have "Some (flex r id (s @ [c]) v) = Some (flex r id s (injval (ders s r) c v))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   287
    by (simp add: flex_append)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   288
  also have "... = decode (retrieve (bders (intern r) s) (injval (ders s r) c v)) r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   289
    using asm2 IH by simp
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   290
  also have "... = decode (retrieve (bder c (bders (intern r) s)) v) r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   291
    using asm by(simp_all add: bder_retrieve ders_append)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   292
  finally show "Some (flex r id (s @ [c]) v) = 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   293
                 decode (retrieve (bders (intern r) (s @ [c])) v) r" by (simp add: bders_append)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   294
qed
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   295
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   296
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   297
definition blexer where
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   298
 "blexer r s \<equiv> if bnullable (bders (intern r) s) then 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   299
                decode (bmkeps (bders (intern r) s)) r else None"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   300
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   301
lemma blexer_correctness:
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   302
  shows "blexer r s = lexer r s"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   303
proof -
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   304
  { define bds where "bds \<equiv> bders (intern r) s"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   305
    define ds  where "ds \<equiv> ders s r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   306
    assume asm: "nullable ds"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   307
    have era: "erase bds = ds" 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   308
      unfolding ds_def bds_def by simp
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   309
    have mke: "\<Turnstile> mkeps ds : ds"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   310
      using asm by (simp add: mkeps_nullable)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   311
    have "decode (bmkeps bds) r = decode (retrieve bds (mkeps ds)) r"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   312
      using bmkeps_retrieve
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   313
      using asm era by (simp add: bmkeps_retrieve)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   314
    also have "... =  Some (flex r id s (mkeps ds))"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   315
      using mke by (simp_all add: MAIN_decode ds_def bds_def)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   316
    finally have "decode (bmkeps bds) r = Some (flex r id s (mkeps ds))" 
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   317
      unfolding bds_def ds_def .
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   318
  }
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   319
  then show "blexer r s = lexer r s"
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   320
    unfolding blexer_def lexer_flex
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   321
    apply(subst bnullable_correctness[symmetric])
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   322
    apply(simp)
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   323
    done
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   324
qed
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   325
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   326
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   327
ec5e4fe4cc70 for new journal/conf paper!
Chengsong
parents:
diff changeset
   328
end