thys/BitCoded2.thy
changeset 342 f0e876ed43fa
parent 339 9d466b27b054
child 343 f139bdc0dcd5
equal deleted inserted replaced
341:abf178a5db58 342:f0e876ed43fa
       
     1 
       
     2 theory BitCoded2
       
     3   imports "Lexer" 
       
     4 begin
       
     5 
       
     6 section \<open>Bit-Encodings\<close>
       
     7 
       
     8 datatype bit = Z | S
       
     9 
       
    10 fun 
       
    11   code :: "val \<Rightarrow> bit list"
       
    12 where
       
    13   "code Void = []"
       
    14 | "code (Char c) = []"
       
    15 | "code (Left v) = Z # (code v)"
       
    16 | "code (Right v) = S # (code v)"
       
    17 | "code (Seq v1 v2) = (code v1) @ (code v2)"
       
    18 | "code (Stars []) = [S]"
       
    19 | "code (Stars (v # vs)) =  (Z # code v) @ code (Stars vs)"
       
    20 
       
    21 
       
    22 fun 
       
    23   Stars_add :: "val \<Rightarrow> val \<Rightarrow> val"
       
    24 where
       
    25   "Stars_add v (Stars vs) = Stars (v # vs)"
       
    26 | "Stars_add v _ = Stars [v]" 
       
    27 
       
    28 function
       
    29   decode' :: "bit list \<Rightarrow> rexp \<Rightarrow> (val * bit list)"
       
    30 where
       
    31   "decode' ds ZERO = (Void, [])"
       
    32 | "decode' ds ONE = (Void, ds)"
       
    33 | "decode' ds (CHAR d) = (Char d, ds)"
       
    34 | "decode' [] (ALT r1 r2) = (Void, [])"
       
    35 | "decode' (Z # ds) (ALT r1 r2) = (let (v, ds') = decode' ds r1 in (Left v, ds'))"
       
    36 | "decode' (S # ds) (ALT r1 r2) = (let (v, ds') = decode' ds r2 in (Right v, ds'))"
       
    37 | "decode' ds (SEQ r1 r2) = (let (v1, ds') = decode' ds r1 in
       
    38                              let (v2, ds'') = decode' ds' r2 in (Seq v1 v2, ds''))"
       
    39 | "decode' [] (STAR r) = (Void, [])"
       
    40 | "decode' (S # ds) (STAR r) = (Stars [], ds)"
       
    41 | "decode' (Z # ds) (STAR r) = (let (v, ds') = decode' ds r in
       
    42                                     let (vs, ds'') = decode' ds' (STAR r) 
       
    43                                     in (Stars_add v vs, ds''))"
       
    44 by pat_completeness auto
       
    45 
       
    46 lemma decode'_smaller:
       
    47   assumes "decode'_dom (ds, r)"
       
    48   shows "length (snd (decode' ds r)) \<le> length ds"
       
    49 using assms
       
    50 apply(induct ds r)
       
    51 apply(auto simp add: decode'.psimps split: prod.split)
       
    52 using dual_order.trans apply blast
       
    53 by (meson dual_order.trans le_SucI)
       
    54 
       
    55 termination "decode'"  
       
    56 apply(relation "inv_image (measure(%cs. size cs) <*lex*> measure(%s. size s)) (%(ds,r). (r,ds))") 
       
    57 apply(auto dest!: decode'_smaller)
       
    58 by (metis less_Suc_eq_le snd_conv)
       
    59 
       
    60 definition
       
    61   decode :: "bit list \<Rightarrow> rexp \<Rightarrow> val option"
       
    62 where
       
    63   "decode ds r \<equiv> (let (v, ds') = decode' ds r 
       
    64                   in (if ds' = [] then Some v else None))"
       
    65 
       
    66 lemma decode'_code_Stars:
       
    67   assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> (\<forall>x. decode' (code v @ x) r = (v, x)) \<and> flat v \<noteq> []" 
       
    68   shows "decode' (code (Stars vs) @ ds) (STAR r) = (Stars vs, ds)"
       
    69   using assms
       
    70   apply(induct vs)
       
    71   apply(auto)
       
    72   done
       
    73 
       
    74 lemma decode'_code:
       
    75   assumes "\<Turnstile> v : r"
       
    76   shows "decode' ((code v) @ ds) r = (v, ds)"
       
    77 using assms
       
    78   apply(induct v r arbitrary: ds) 
       
    79   apply(auto)
       
    80   using decode'_code_Stars by blast
       
    81 
       
    82 lemma decode_code:
       
    83   assumes "\<Turnstile> v : r"
       
    84   shows "decode (code v) r = Some v"
       
    85   using assms unfolding decode_def
       
    86   by (smt append_Nil2 decode'_code old.prod.case)
       
    87 
       
    88 
       
    89 section {* Annotated Regular Expressions *}
       
    90 
       
    91 datatype arexp = 
       
    92   AZERO
       
    93 | AONE "bit list"
       
    94 | ACHAR "bit list" char
       
    95 | ASEQ "bit list" arexp arexp
       
    96 | AALTs "bit list" "arexp list"
       
    97 | ASTAR "bit list" arexp
       
    98 
       
    99 abbreviation
       
   100   "AALT bs r1 r2 \<equiv> AALTs bs [r1, r2]"
       
   101 
       
   102 fun asize :: "arexp \<Rightarrow> nat" where
       
   103   "asize AZERO = 1"
       
   104 | "asize (AONE cs) = 1" 
       
   105 | "asize (ACHAR cs c) = 1"
       
   106 | "asize (AALTs cs rs) = Suc (sum_list (map asize rs))"
       
   107 | "asize (ASEQ cs r1 r2) = Suc (asize r1 + asize r2)"
       
   108 | "asize (ASTAR cs r) = Suc (asize r)"
       
   109 
       
   110 fun 
       
   111   erase :: "arexp \<Rightarrow> rexp"
       
   112 where
       
   113   "erase AZERO = ZERO"
       
   114 | "erase (AONE _) = ONE"
       
   115 | "erase (ACHAR _ c) = CHAR c"
       
   116 | "erase (AALTs _ []) = ZERO"
       
   117 | "erase (AALTs _ [r]) = (erase r)"
       
   118 | "erase (AALTs bs (r#rs)) = ALT (erase r) (erase (AALTs bs rs))"
       
   119 | "erase (ASEQ _ r1 r2) = SEQ (erase r1) (erase r2)"
       
   120 | "erase (ASTAR _ r) = STAR (erase r)"
       
   121 
       
   122 lemma decode_code_erase:
       
   123   assumes "\<Turnstile> v : (erase  a)"
       
   124   shows "decode (code v) (erase a) = Some v"
       
   125   using assms
       
   126   by (simp add: decode_code) 
       
   127 
       
   128 
       
   129 fun nonalt :: "arexp \<Rightarrow> bool"
       
   130   where
       
   131   "nonalt (AALTs bs2 rs) = False"
       
   132 | "nonalt r = True"
       
   133 
       
   134 
       
   135 fun good :: "arexp \<Rightarrow> bool" where
       
   136   "good AZERO = False"
       
   137 | "good (AONE cs) = True" 
       
   138 | "good (ACHAR cs c) = True"
       
   139 | "good (AALTs cs []) = False"
       
   140 | "good (AALTs cs [r]) = False"
       
   141 | "good (AALTs cs (r1#r2#rs)) = (\<forall>r' \<in> set (r1#r2#rs). good r' \<and> nonalt r')"
       
   142 | "good (ASEQ _ AZERO _) = False"
       
   143 | "good (ASEQ _ (AONE _) _) = False"
       
   144 | "good (ASEQ _ _ AZERO) = False"
       
   145 | "good (ASEQ cs r1 r2) = (good r1 \<and> good r2)"
       
   146 | "good (ASTAR cs r) = True"
       
   147 
       
   148 
       
   149 
       
   150 
       
   151 fun fuse :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp" where
       
   152   "fuse bs AZERO = AZERO"
       
   153 | "fuse bs (AONE cs) = AONE (bs @ cs)" 
       
   154 | "fuse bs (ACHAR cs c) = ACHAR (bs @ cs) c"
       
   155 | "fuse bs (AALTs cs rs) = AALTs (bs @ cs) rs"
       
   156 | "fuse bs (ASEQ cs r1 r2) = ASEQ (bs @ cs) r1 r2"
       
   157 | "fuse bs (ASTAR cs r) = ASTAR (bs @ cs) r"
       
   158 
       
   159 lemma fuse_append:
       
   160   shows "fuse (bs1 @ bs2) r = fuse bs1 (fuse bs2 r)"
       
   161   apply(induct r)
       
   162   apply(auto)
       
   163   done
       
   164 
       
   165 
       
   166 fun intern :: "rexp \<Rightarrow> arexp" where
       
   167   "intern ZERO = AZERO"
       
   168 | "intern ONE = AONE []"
       
   169 | "intern (CHAR c) = ACHAR [] c"
       
   170 | "intern (ALT r1 r2) = AALT [] (fuse [Z] (intern r1)) 
       
   171                                 (fuse [S]  (intern r2))"
       
   172 | "intern (SEQ r1 r2) = ASEQ [] (intern r1) (intern r2)"
       
   173 | "intern (STAR r) = ASTAR [] (intern r)"
       
   174 
       
   175 
       
   176 fun retrieve :: "arexp \<Rightarrow> val \<Rightarrow> bit list" where
       
   177   "retrieve (AONE bs) Void = bs"
       
   178 | "retrieve (ACHAR bs c) (Char d) = bs"
       
   179 | "retrieve (AALTs bs [r]) v = bs @ retrieve r v" 
       
   180 | "retrieve (AALTs bs (r#rs)) (Left v) = bs @ retrieve r v"
       
   181 | "retrieve (AALTs bs (r#rs)) (Right v) = bs @ retrieve (AALTs [] rs) v"
       
   182 | "retrieve (ASEQ bs r1 r2) (Seq v1 v2) = bs @ retrieve r1 v1 @ retrieve r2 v2"
       
   183 | "retrieve (ASTAR bs r) (Stars []) = bs @ [S]"
       
   184 | "retrieve (ASTAR bs r) (Stars (v#vs)) = 
       
   185      bs @ [Z] @ retrieve r v @ retrieve (ASTAR [] r) (Stars vs)"
       
   186 
       
   187 
       
   188 
       
   189 fun
       
   190  bnullable :: "arexp \<Rightarrow> bool"
       
   191 where
       
   192   "bnullable (AZERO) = False"
       
   193 | "bnullable (AONE bs) = True"
       
   194 | "bnullable (ACHAR bs c) = False"
       
   195 | "bnullable (AALTs bs rs) = (\<exists>r \<in> set rs. bnullable r)"
       
   196 | "bnullable (ASEQ bs r1 r2) = (bnullable r1 \<and> bnullable r2)"
       
   197 | "bnullable (ASTAR bs r) = True"
       
   198 
       
   199 fun 
       
   200   bmkeps :: "arexp \<Rightarrow> bit list"
       
   201 where
       
   202   "bmkeps(AONE bs) = bs"
       
   203 | "bmkeps(ASEQ bs r1 r2) = bs @ (bmkeps r1) @ (bmkeps r2)"
       
   204 | "bmkeps(AALTs bs [r]) = bs @ (bmkeps r)"
       
   205 | "bmkeps(AALTs bs (r#rs)) = (if bnullable(r) then bs @ (bmkeps r) else (bmkeps (AALTs bs rs)))"
       
   206 | "bmkeps(ASTAR bs r) = bs @ [S]"
       
   207 
       
   208 
       
   209 fun
       
   210  bder :: "char \<Rightarrow> arexp \<Rightarrow> arexp"
       
   211 where
       
   212   "bder c (AZERO) = AZERO"
       
   213 | "bder c (AONE bs) = AZERO"
       
   214 | "bder c (ACHAR bs d) = (if c = d then AONE bs else AZERO)"
       
   215 | "bder c (AALTs bs rs) = AALTs bs (map (bder c) rs)"
       
   216 | "bder c (ASEQ bs r1 r2) = 
       
   217      (if bnullable r1
       
   218       then AALT bs (ASEQ [] (bder c r1) r2) (fuse (bmkeps r1) (bder c r2))
       
   219       else ASEQ bs (bder c r1) r2)"
       
   220 | "bder c (ASTAR bs r) = ASEQ bs (fuse [Z] (bder c r)) (ASTAR [] r)"
       
   221 
       
   222 
       
   223 fun 
       
   224   bders :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
       
   225 where
       
   226   "bders r [] = r"
       
   227 | "bders r (c#s) = bders (bder c r) s"
       
   228 
       
   229 lemma bders_append:
       
   230   "bders r (s1 @ s2) = bders (bders r s1) s2"
       
   231   apply(induct s1 arbitrary: r s2)
       
   232   apply(simp_all)
       
   233   done
       
   234 
       
   235 lemma bnullable_correctness:
       
   236   shows "nullable (erase r) = bnullable r"
       
   237   apply(induct r rule: erase.induct)
       
   238   apply(simp_all)
       
   239   done
       
   240 
       
   241 lemma erase_fuse:
       
   242   shows "erase (fuse bs r) = erase r"
       
   243   apply(induct r rule: erase.induct)
       
   244   apply(simp_all)
       
   245   done
       
   246 
       
   247 lemma erase_intern [simp]:
       
   248   shows "erase (intern r) = r"
       
   249   apply(induct r)
       
   250   apply(simp_all add: erase_fuse)
       
   251   done
       
   252 
       
   253 lemma erase_bder [simp]:
       
   254   shows "erase (bder a r) = der a (erase r)"
       
   255   apply(induct r rule: erase.induct)
       
   256   apply(simp_all add: erase_fuse bnullable_correctness)
       
   257   done
       
   258 
       
   259 lemma erase_bders [simp]:
       
   260   shows "erase (bders r s) = ders s (erase r)"
       
   261   apply(induct s arbitrary: r )
       
   262   apply(simp_all)
       
   263   done
       
   264 
       
   265 lemma retrieve_encode_STARS:
       
   266   assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> code v = retrieve (intern r) v"
       
   267   shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)"
       
   268   using assms
       
   269   apply(induct vs)
       
   270   apply(simp_all)
       
   271   done
       
   272 
       
   273 lemma retrieve_fuse2:
       
   274   assumes "\<Turnstile> v : (erase r)"
       
   275   shows "retrieve (fuse bs r) v = bs @ retrieve r v"
       
   276   using assms
       
   277   apply(induct r arbitrary: v bs)
       
   278          apply(auto elim: Prf_elims)[4]
       
   279    defer
       
   280   using retrieve_encode_STARS
       
   281    apply(auto elim!: Prf_elims)[1]
       
   282    apply(case_tac vs)
       
   283     apply(simp)
       
   284    apply(simp)
       
   285   (* AALTs  case *)
       
   286   apply(simp)
       
   287   apply(case_tac x2a)
       
   288    apply(simp)
       
   289    apply(auto elim!: Prf_elims)[1]
       
   290   apply(simp)
       
   291    apply(case_tac list)
       
   292    apply(simp)
       
   293   apply(auto)
       
   294   apply(auto elim!: Prf_elims)[1]
       
   295   done
       
   296 
       
   297 lemma retrieve_fuse:
       
   298   assumes "\<Turnstile> v : r"
       
   299   shows "retrieve (fuse bs (intern r)) v = bs @ retrieve (intern r) v"
       
   300   using assms 
       
   301   by (simp_all add: retrieve_fuse2)
       
   302 
       
   303 
       
   304 lemma retrieve_code:
       
   305   assumes "\<Turnstile> v : r"
       
   306   shows "code v = retrieve (intern r) v"
       
   307   using assms
       
   308   apply(induct v r )
       
   309   apply(simp_all add: retrieve_fuse retrieve_encode_STARS)
       
   310   done
       
   311 
       
   312 lemma r:
       
   313   assumes "bnullable (AALTs bs (a # rs))"
       
   314   shows "bnullable a \<or> (\<not> bnullable a \<and> bnullable (AALTs bs rs))"
       
   315   using assms
       
   316   apply(induct rs)
       
   317    apply(auto)
       
   318   done
       
   319 
       
   320 lemma r0:
       
   321   assumes "bnullable a" 
       
   322   shows  "bmkeps (AALTs bs (a # rs)) = bs @ (bmkeps a)"
       
   323   using assms
       
   324   by (metis bmkeps.simps(3) bmkeps.simps(4) list.exhaust)
       
   325 
       
   326 lemma r1:
       
   327   assumes "\<not> bnullable a" "bnullable (AALTs bs rs)"
       
   328   shows  "bmkeps (AALTs bs (a # rs)) = bmkeps (AALTs bs rs)"
       
   329   using assms
       
   330   apply(induct rs)
       
   331    apply(auto)
       
   332   done
       
   333 
       
   334 lemma r2:
       
   335   assumes "x \<in> set rs" "bnullable x"
       
   336   shows "bnullable (AALTs bs rs)"
       
   337   using assms
       
   338   apply(induct rs)
       
   339    apply(auto)
       
   340   done
       
   341 
       
   342 lemma  r3:
       
   343   assumes "\<not> bnullable r" 
       
   344           " \<exists> x \<in> set rs. bnullable x"
       
   345   shows "retrieve (AALTs bs rs) (mkeps (erase (AALTs bs rs))) =
       
   346          retrieve (AALTs bs (r # rs)) (mkeps (erase (AALTs bs (r # rs))))"
       
   347   using assms
       
   348   apply(induct rs arbitrary: r bs)
       
   349    apply(auto)[1]
       
   350   apply(auto)
       
   351   using bnullable_correctness apply blast
       
   352     apply(auto simp add: bnullable_correctness mkeps_nullable retrieve_fuse2)
       
   353    apply(subst retrieve_fuse2[symmetric])
       
   354   apply (smt bnullable.simps(4) bnullable_correctness erase.simps(5) erase.simps(6) insert_iff list.exhaust list.set(2) mkeps.simps(3) mkeps_nullable)
       
   355    apply(simp)
       
   356   apply(case_tac "bnullable a")
       
   357   apply (smt append_Nil2 bnullable.simps(4) bnullable_correctness erase.simps(5) erase.simps(6) fuse.simps(4) insert_iff list.exhaust list.set(2) mkeps.simps(3) mkeps_nullable retrieve_fuse2)
       
   358   apply(drule_tac x="a" in meta_spec)
       
   359   apply(drule_tac x="bs" in meta_spec)
       
   360   apply(drule meta_mp)
       
   361    apply(simp)
       
   362   apply(drule meta_mp)
       
   363    apply(auto)
       
   364   apply(subst retrieve_fuse2[symmetric])
       
   365   apply(case_tac rs)
       
   366     apply(simp)
       
   367    apply(auto)[1]
       
   368       apply (simp add: bnullable_correctness)
       
   369   apply (metis append_Nil2 bnullable_correctness erase_fuse fuse.simps(4) list.set_intros(1) mkeps.simps(3) mkeps_nullable nullable.simps(4) r2)
       
   370     apply (simp add: bnullable_correctness)
       
   371   apply (metis append_Nil2 bnullable_correctness erase.simps(6) erase_fuse fuse.simps(4) list.set_intros(2) mkeps.simps(3) mkeps_nullable r2)
       
   372   apply(simp)
       
   373   done
       
   374 
       
   375 
       
   376 lemma t: 
       
   377   assumes "\<forall>r \<in> set rs. nullable (erase r) \<longrightarrow> bmkeps r = retrieve r (mkeps (erase r))" 
       
   378           "nullable (erase (AALTs bs rs))"
       
   379   shows " bmkeps (AALTs bs rs) = retrieve (AALTs bs rs) (mkeps (erase (AALTs bs rs)))"
       
   380   using assms
       
   381   apply(induct rs arbitrary: bs)
       
   382    apply(simp)
       
   383   apply(auto simp add: bnullable_correctness)
       
   384    apply(case_tac rs)
       
   385      apply(auto simp add: bnullable_correctness)[2]
       
   386    apply(subst r1)
       
   387      apply(simp)
       
   388     apply(rule r2)
       
   389      apply(assumption)
       
   390     apply(simp)
       
   391    apply(drule_tac x="bs" in meta_spec)
       
   392    apply(drule meta_mp)
       
   393     apply(auto)[1]
       
   394    prefer 2
       
   395   apply(case_tac "bnullable a")
       
   396     apply(subst r0)
       
   397      apply blast
       
   398     apply(subgoal_tac "nullable (erase a)")
       
   399   prefer 2
       
   400   using bnullable_correctness apply blast
       
   401   apply (metis (no_types, lifting) erase.simps(5) erase.simps(6) list.exhaust mkeps.simps(3) retrieve.simps(3) retrieve.simps(4))
       
   402   apply(subst r1)
       
   403      apply(simp)
       
   404   using r2 apply blast
       
   405   apply(drule_tac x="bs" in meta_spec)
       
   406    apply(drule meta_mp)
       
   407     apply(auto)[1]
       
   408    apply(simp)
       
   409   using r3 apply blast
       
   410   apply(auto)
       
   411   using r3 by blast
       
   412 
       
   413 lemma bmkeps_retrieve:
       
   414   assumes "nullable (erase r)"
       
   415   shows "bmkeps r = retrieve r (mkeps (erase r))"
       
   416   using assms
       
   417   apply(induct r)
       
   418          apply(simp)
       
   419         apply(simp)
       
   420        apply(simp)
       
   421     apply(simp)
       
   422    defer
       
   423    apply(simp)
       
   424   apply(rule t)
       
   425    apply(auto)
       
   426   done
       
   427 
       
   428 lemma bder_retrieve:
       
   429   assumes "\<Turnstile> v : der c (erase r)"
       
   430   shows "retrieve (bder c r) v = retrieve r (injval (erase r) c v)"
       
   431   using assms
       
   432   apply(induct r arbitrary: v rule: erase.induct)
       
   433          apply(simp)
       
   434          apply(erule Prf_elims)
       
   435         apply(simp)
       
   436         apply(erule Prf_elims) 
       
   437         apply(simp)
       
   438       apply(case_tac "c = ca")
       
   439        apply(simp)
       
   440        apply(erule Prf_elims)
       
   441        apply(simp)
       
   442       apply(simp)
       
   443        apply(erule Prf_elims)
       
   444   apply(simp)
       
   445       apply(erule Prf_elims)
       
   446      apply(simp)
       
   447     apply(simp)
       
   448   apply(rename_tac "r\<^sub>1" "r\<^sub>2" rs v)
       
   449     apply(erule Prf_elims)
       
   450      apply(simp)
       
   451     apply(simp)
       
   452     apply(case_tac rs)
       
   453      apply(simp)
       
   454     apply(simp)
       
   455   apply (smt Prf_elims(3) injval.simps(2) injval.simps(3) retrieve.simps(4) retrieve.simps(5) same_append_eq)
       
   456    apply(simp)
       
   457    apply(case_tac "nullable (erase r1)")
       
   458     apply(simp)
       
   459   apply(erule Prf_elims)
       
   460      apply(subgoal_tac "bnullable r1")
       
   461   prefer 2
       
   462   using bnullable_correctness apply blast
       
   463     apply(simp)
       
   464      apply(erule Prf_elims)
       
   465      apply(simp)
       
   466    apply(subgoal_tac "bnullable r1")
       
   467   prefer 2
       
   468   using bnullable_correctness apply blast
       
   469     apply(simp)
       
   470     apply(simp add: retrieve_fuse2)
       
   471     apply(simp add: bmkeps_retrieve)
       
   472    apply(simp)
       
   473    apply(erule Prf_elims)
       
   474    apply(simp)
       
   475   using bnullable_correctness apply blast
       
   476   apply(rename_tac bs r v)
       
   477   apply(simp)
       
   478   apply(erule Prf_elims)
       
   479      apply(clarify)
       
   480   apply(erule Prf_elims)
       
   481   apply(clarify)
       
   482   apply(subst injval.simps)
       
   483   apply(simp del: retrieve.simps)
       
   484   apply(subst retrieve.simps)
       
   485   apply(subst retrieve.simps)
       
   486   apply(simp)
       
   487   apply(simp add: retrieve_fuse2)
       
   488   done
       
   489   
       
   490 
       
   491 
       
   492 lemma MAIN_decode:
       
   493   assumes "\<Turnstile> v : ders s r"
       
   494   shows "Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r"
       
   495   using assms
       
   496 proof (induct s arbitrary: v rule: rev_induct)
       
   497   case Nil
       
   498   have "\<Turnstile> v : ders [] r" by fact
       
   499   then have "\<Turnstile> v : r" by simp
       
   500   then have "Some v = decode (retrieve (intern r) v) r"
       
   501     using decode_code retrieve_code by auto
       
   502   then show "Some (flex r id [] v) = decode (retrieve (bders (intern r) []) v) r"
       
   503     by simp
       
   504 next
       
   505   case (snoc c s v)
       
   506   have IH: "\<And>v. \<Turnstile> v : ders s r \<Longrightarrow> 
       
   507      Some (flex r id s v) = decode (retrieve (bders (intern r) s) v) r" by fact
       
   508   have asm: "\<Turnstile> v : ders (s @ [c]) r" by fact
       
   509   then have asm2: "\<Turnstile> injval (ders s r) c v : ders s r" 
       
   510     by (simp add: Prf_injval ders_append)
       
   511   have "Some (flex r id (s @ [c]) v) = Some (flex r id s (injval (ders s r) c v))"
       
   512     by (simp add: flex_append)
       
   513   also have "... = decode (retrieve (bders (intern r) s) (injval (ders s r) c v)) r"
       
   514     using asm2 IH by simp
       
   515   also have "... = decode (retrieve (bder c (bders (intern r) s)) v) r"
       
   516     using asm by (simp_all add: bder_retrieve ders_append)
       
   517   finally show "Some (flex r id (s @ [c]) v) = 
       
   518                  decode (retrieve (bders (intern r) (s @ [c])) v) r" by (simp add: bders_append)
       
   519 qed
       
   520 
       
   521 
       
   522 definition blex where
       
   523  "blex a s \<equiv> if bnullable (bders a s) then Some (bmkeps (bders a s)) else None"
       
   524 
       
   525 
       
   526 
       
   527 definition blexer where
       
   528  "blexer r s \<equiv> if bnullable (bders (intern r) s) then 
       
   529                 decode (bmkeps (bders (intern r) s)) r else None"
       
   530 
       
   531 lemma blexer_correctness:
       
   532   shows "blexer r s = lexer r s"
       
   533 proof -
       
   534   { define bds where "bds \<equiv> bders (intern r) s"
       
   535     define ds  where "ds \<equiv> ders s r"
       
   536     assume asm: "nullable ds"
       
   537     have era: "erase bds = ds" 
       
   538       unfolding ds_def bds_def by simp
       
   539     have mke: "\<Turnstile> mkeps ds : ds"
       
   540       using asm by (simp add: mkeps_nullable)
       
   541     have "decode (bmkeps bds) r = decode (retrieve bds (mkeps ds)) r"
       
   542       using bmkeps_retrieve
       
   543       using asm era by (simp add: bmkeps_retrieve)
       
   544     also have "... =  Some (flex r id s (mkeps ds))"
       
   545       using mke by (simp_all add: MAIN_decode ds_def bds_def)
       
   546     finally have "decode (bmkeps bds) r = Some (flex r id s (mkeps ds))" 
       
   547       unfolding bds_def ds_def .
       
   548   }
       
   549   then show "blexer r s = lexer r s"
       
   550     unfolding blexer_def lexer_flex
       
   551     apply(subst bnullable_correctness[symmetric])
       
   552     apply(simp)
       
   553     done
       
   554 qed
       
   555 
       
   556 lemma asize0:
       
   557   shows "0 < asize r"
       
   558   apply(induct  r)
       
   559   apply(auto)
       
   560   done
       
   561 
       
   562 lemma asize_fuse:
       
   563   shows "asize (fuse bs r) = asize r"
       
   564   apply(induct r)
       
   565   apply(auto)
       
   566   done
       
   567 
       
   568 lemma bder_fuse:
       
   569   shows "bder c (fuse bs a) = fuse bs  (bder c a)"
       
   570   apply(induct a arbitrary: bs c)
       
   571   apply(simp_all)
       
   572   done
       
   573 
       
   574 lemma map_bder_fuse:
       
   575   shows "map (bder c \<circ> fuse bs1) as1 = map (fuse bs1) (map (bder c) as1)"
       
   576   apply(induct as1)
       
   577   apply(auto simp add: bder_fuse)
       
   578   done
       
   579 
       
   580 
       
   581 fun nonnested :: "arexp \<Rightarrow> bool"
       
   582   where
       
   583   "nonnested (AALTs bs2 []) = True"
       
   584 | "nonnested (AALTs bs2 ((AALTs bs1 rs1) # rs2)) = False"
       
   585 | "nonnested (AALTs bs2 (r # rs2)) = nonnested (AALTs bs2 rs2)"
       
   586 | "nonnested r = True"
       
   587 
       
   588 
       
   589 
       
   590 fun distinctBy :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b set \<Rightarrow> 'a list"
       
   591   where
       
   592   "distinctBy [] f acc = []"
       
   593 | "distinctBy (x#xs) f acc = 
       
   594      (if (f x) \<in> acc then distinctBy xs f acc 
       
   595       else x # (distinctBy xs f ({f x} \<union> acc)))"
       
   596 
       
   597 fun flts :: "arexp list \<Rightarrow> arexp list"
       
   598   where 
       
   599   "flts [] = []"
       
   600 | "flts (AZERO # rs) = flts rs"
       
   601 | "flts ((AALTs bs  rs1) # rs) = (map (fuse bs) rs1) @ flts rs"
       
   602 | "flts (r1 # rs) = r1 # flts rs"
       
   603 
       
   604 
       
   605 fun spill :: "arexp list \<Rightarrow> arexp list"
       
   606   where 
       
   607   "spill [] = []"
       
   608 | "spill ((AALTs bs rs1) # rs) = (map (fuse bs) rs1) @ spill rs"
       
   609 | "spill (r1 # rs) = r1 # spill rs"
       
   610 
       
   611 lemma  spill_Cons:
       
   612   shows "spill (r # rs1) = spill [r] @ spill rs1"
       
   613   apply(induct r arbitrary: rs1)
       
   614    apply(auto)
       
   615   done
       
   616 
       
   617 lemma  spill_append:
       
   618   shows "spill (rs1 @ rs2) = spill rs1 @ spill rs2"
       
   619   apply(induct rs1 arbitrary: rs2)
       
   620    apply(auto)
       
   621   by (metis append.assoc spill_Cons)
       
   622 
       
   623 fun bsimp_ASEQ :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp \<Rightarrow> arexp"
       
   624   where
       
   625   "bsimp_ASEQ _ AZERO _ = AZERO"
       
   626 | "bsimp_ASEQ _ _ AZERO = AZERO"
       
   627 | "bsimp_ASEQ bs1 (AONE bs2) r2 = fuse (bs1 @ bs2) r2"
       
   628 | "bsimp_ASEQ bs1 r1 r2 = ASEQ  bs1 r1 r2"
       
   629 
       
   630 
       
   631 fun bsimp_AALTs :: "bit list \<Rightarrow> arexp list \<Rightarrow> arexp"
       
   632   where
       
   633   "bsimp_AALTs _ [] = AZERO"
       
   634 | "bsimp_AALTs bs1 [r] = fuse bs1 r"
       
   635 | "bsimp_AALTs bs1 rs = AALTs bs1 rs"
       
   636 
       
   637 
       
   638 fun bsimp :: "arexp \<Rightarrow> arexp" 
       
   639   where
       
   640   "bsimp (ASEQ bs1 r1 r2) = bsimp_ASEQ bs1 (bsimp r1) (bsimp r2)"
       
   641 | "bsimp (AALTs bs1 rs) = bsimp_AALTs bs1 (flts (map bsimp rs))"
       
   642 | "bsimp r = r"
       
   643 
       
   644 inductive contains :: "arexp \<Rightarrow> bit list \<Rightarrow> bool" ("_ >> _" [51, 50] 50)
       
   645   where
       
   646   "AONE bs >> bs"
       
   647 | "ACHAR bs c >> bs"
       
   648 | "\<lbrakk>a1 >> bs1; a2 >> bs2\<rbrakk> \<Longrightarrow> ASEQ bs a1 a2 >> bs @ bs1 @ bs2"
       
   649 | "r >> bs1 \<Longrightarrow> AALTs bs (r#rs) >> bs @ bs1"
       
   650 | "AALTs bs rs >> bs @ bs1 \<Longrightarrow> AALTs bs (r#rs) >> bs @ bs1"
       
   651 | "ASTAR bs r >> bs @ [S]"
       
   652 | "\<lbrakk>r >> bs1; ASTAR [] r >> bs2\<rbrakk> \<Longrightarrow> ASTAR bs r >> bs @ Z # bs1 @ bs2"
       
   653 
       
   654 lemma contains0:
       
   655   assumes "a >> bs"
       
   656   shows "(fuse bs1 a) >> bs1 @ bs"
       
   657   using assms
       
   658   apply(induct arbitrary: bs1)
       
   659   apply(auto intro: contains.intros)
       
   660        apply (metis append.assoc contains.intros(3))
       
   661      apply (metis append.assoc contains.intros(4))
       
   662   apply (metis append.assoc contains.intros(5))
       
   663     apply (metis append.assoc contains.intros(6))
       
   664    apply (metis append_assoc contains.intros(7))
       
   665   done
       
   666 
       
   667 lemma contains1:
       
   668   assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> intern r >> code v"
       
   669   shows "ASTAR [] (intern r) >> code (Stars vs)"
       
   670   using assms
       
   671   apply(induct vs)
       
   672    apply(simp)
       
   673   using contains.simps apply blast
       
   674   apply(simp)
       
   675    apply(subst (2) append_Nil[symmetric])
       
   676   apply(rule contains.intros)
       
   677    apply(auto)
       
   678   done
       
   679 
       
   680 
       
   681 lemma contains2:
       
   682   assumes "\<Turnstile> v : r"
       
   683   shows "(intern r) >> code v"
       
   684   using assms
       
   685   apply(induct)
       
   686        prefer 4
       
   687        apply(simp)
       
   688        apply(rule contains.intros)
       
   689    prefer 4
       
   690        apply(simp)
       
   691       apply(rule contains.intros)
       
   692      apply(simp)
       
   693   apply(subst (3) append_Nil[symmetric])
       
   694   apply(rule contains.intros)
       
   695       apply(simp)
       
   696   apply(simp)
       
   697     apply(simp)
       
   698   apply(subst (9) append_Nil[symmetric])
       
   699     apply(rule contains.intros)
       
   700     apply (metis append_Cons append_self_conv2 contains0)
       
   701     apply(simp)
       
   702      apply(subst (9) append_Nil[symmetric])
       
   703    apply(rule contains.intros)
       
   704    back
       
   705    apply(rule contains.intros)
       
   706   apply(drule_tac ?bs1.0="[S]" in contains0)
       
   707    apply(simp)
       
   708   apply(simp)
       
   709   apply(case_tac vs)
       
   710    apply(simp)
       
   711   apply (metis append_Nil contains.intros(6))
       
   712   using contains1 by blast
       
   713 
       
   714 lemma qq1:
       
   715   assumes "\<exists>r \<in> set rs. bnullable r"
       
   716   shows "bmkeps (AALTs bs (rs @ rs1)) = bmkeps (AALTs bs rs)"
       
   717   using assms
       
   718   apply(induct rs arbitrary: rs1 bs)
       
   719   apply(simp)
       
   720   apply(simp)
       
   721   by (metis Nil_is_append_conv bmkeps.simps(4) neq_Nil_conv r0 split_list_last)
       
   722 
       
   723 lemma qq2:
       
   724   assumes "\<forall>r \<in> set rs. \<not> bnullable r" "\<exists>r \<in> set rs1. bnullable r"
       
   725   shows "bmkeps (AALTs bs (rs @ rs1)) = bmkeps (AALTs bs rs1)"
       
   726   using assms
       
   727   apply(induct rs arbitrary: rs1 bs)
       
   728   apply(simp)
       
   729   apply(simp)
       
   730   by (metis append_assoc in_set_conv_decomp r1 r2)
       
   731 
       
   732 lemma qq2a:
       
   733   assumes "\<not> bnullable r" "\<exists>r \<in> set rs1. bnullable r"
       
   734   shows "bmkeps (AALTs bs (r # rs1)) = bmkeps (AALTs bs rs1)"
       
   735   using assms
       
   736   by (simp add: r1)
       
   737   
       
   738 lemma qq3:
       
   739   shows "bnullable (AALTs bs rs) = (\<exists>r \<in> set rs. bnullable r)"
       
   740   apply(induct rs arbitrary: bs)
       
   741   apply(simp)
       
   742   apply(simp)
       
   743   done
       
   744 
       
   745 lemma qq4:
       
   746   assumes "bnullable (AALTs bs rs)"
       
   747   shows "bmkeps (AALTs bs rs) = bs @ bmkeps (AALTs [] rs)"
       
   748   by (metis append_Nil2 assms bmkeps_retrieve bnullable_correctness erase_fuse fuse.simps(4) mkeps_nullable retrieve_fuse2)
       
   749 
       
   750 
       
   751 lemma contains3a:
       
   752   assumes "AALTs bs lst >> bs @ bs1"
       
   753   shows "AALTs bs (a # lst) >> bs @ bs1"
       
   754   using assms
       
   755   apply -
       
   756   by (simp add: contains.intros(5))
       
   757 
       
   758   
       
   759 lemma contains3b:
       
   760   assumes "a >> bs1"
       
   761   shows "AALTs bs (a # lst) >> bs @ bs1"
       
   762   using assms
       
   763   apply -
       
   764   apply(rule contains.intros)
       
   765   apply(simp)
       
   766   done   
       
   767 
       
   768 
       
   769 lemma contains3:
       
   770   assumes "\<And>x. \<lbrakk>x \<in> set rs; bnullable x\<rbrakk> \<Longrightarrow> x >> bmkeps x" "x \<in> set rs" "bnullable x"
       
   771   shows "AALTs bs rs >> bmkeps (AALTs bs rs)"
       
   772   using assms
       
   773   apply(induct rs arbitrary: bs x)
       
   774    apply simp
       
   775   by (metis contains.intros(4) contains.intros(5) list.set_intros(1) list.set_intros(2) qq3 qq4 r r0 r1)
       
   776 
       
   777 lemma cont1:
       
   778   assumes "\<And>v. \<Turnstile> v : erase r \<Longrightarrow> r >> retrieve r v" 
       
   779           "\<forall>v\<in>set vs. \<Turnstile> v : erase r \<and> flat v \<noteq> []" 
       
   780   shows "ASTAR bs r >> retrieve (ASTAR bs r) (Stars vs)"
       
   781   using assms 
       
   782   apply(induct vs arbitrary: bs r)
       
   783    apply(simp)
       
   784   using contains.intros(6) apply auto[1]
       
   785   by (simp add: contains.intros(7))
       
   786   
       
   787 lemma contains4:
       
   788   assumes "bnullable a"
       
   789   shows "a >> bmkeps a"
       
   790   using assms
       
   791   apply(induct a rule: bnullable.induct)
       
   792        apply(auto intro: contains.intros)
       
   793   using contains3 by blast
       
   794 
       
   795 lemma contains5:
       
   796   assumes "\<Turnstile> v : r"
       
   797   shows "(intern r) >> retrieve (intern r) v"
       
   798   using contains2[OF assms] retrieve_code[OF assms]
       
   799   by (simp)
       
   800 
       
   801 lemma contains6:
       
   802   assumes "\<Turnstile> v : (erase r)"
       
   803   shows "r >> retrieve r v"
       
   804   using assms
       
   805   apply(induct r arbitrary: v rule: erase.induct)
       
   806   apply(auto)[1]
       
   807   using Prf_elims(1) apply blast
       
   808   using Prf_elims(4) contains.intros(1) apply force
       
   809   using Prf_elims(5) contains.intros(2) apply force
       
   810   apply(auto)[1]
       
   811   using Prf_elims(1) apply blast
       
   812   apply(auto)[1]
       
   813   using contains3b contains3a apply blast
       
   814     prefer 2
       
   815   apply(auto)[1]
       
   816     apply (metis Prf_elims(2) contains.intros(3) retrieve.simps(6))
       
   817    prefer 2
       
   818   apply(auto)[1]
       
   819    apply (metis Prf_elims(6) cont1)
       
   820   apply(simp)
       
   821   apply(erule Prf_elims)
       
   822    apply(auto)
       
   823    apply (simp add: contains3b)
       
   824   using retrieve_fuse2 contains3b contains3a
       
   825   apply(subst retrieve_fuse2[symmetric])
       
   826   apply (metis append_Nil2 erase_fuse fuse.simps(4))
       
   827   apply(simp)
       
   828   by (metis append_Nil2 erase_fuse fuse.simps(4))
       
   829 
       
   830 lemma contains7:
       
   831   assumes "\<Turnstile> v : der c (erase r)"
       
   832   shows "(bder c r) >> retrieve r (injval (erase r) c v)"
       
   833   using bder_retrieve[OF assms(1)] retrieve_code[OF assms(1)]
       
   834   by (metis assms contains6 erase_bder)
       
   835 
       
   836 
       
   837 lemma contains7a:
       
   838   assumes "\<Turnstile> v : der c (erase r)"
       
   839   shows "r >> retrieve r (injval (erase r) c v)"
       
   840   using assms
       
   841   apply -
       
   842   apply(drule Prf_injval)
       
   843   apply(drule contains6)
       
   844   apply(simp)
       
   845   done
       
   846 
       
   847 fun 
       
   848   bders_simp :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
       
   849 where
       
   850   "bders_simp r [] = r"
       
   851 | "bders_simp r (c # s) = bders_simp (bsimp (bder c r)) s"
       
   852 
       
   853 definition blexer_simp where
       
   854  "blexer_simp r s \<equiv> if bnullable (bders_simp (intern r) s) then 
       
   855                 decode (bmkeps (bders_simp (intern r) s)) r else None"
       
   856 
       
   857 
       
   858 
       
   859 
       
   860 
       
   861 lemma bders_simp_append:
       
   862   shows "bders_simp r (s1 @ s2) = bders_simp (bders_simp r s1) s2"
       
   863   apply(induct s1 arbitrary: r s2)
       
   864    apply(simp)
       
   865   apply(simp)
       
   866   done
       
   867 
       
   868 lemma bsimp_ASEQ_size:
       
   869   shows "asize (bsimp_ASEQ bs r1 r2) \<le> Suc (asize r1 + asize r2)"
       
   870   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
       
   871   apply(auto)
       
   872   done
       
   873 
       
   874 
       
   875 
       
   876 lemma flts_size:
       
   877   shows "sum_list (map asize (flts rs)) \<le> sum_list (map asize rs)"
       
   878   apply(induct rs rule: flts.induct)
       
   879         apply(simp_all)
       
   880   by (simp add: asize_fuse comp_def)
       
   881   
       
   882 
       
   883 lemma bsimp_AALTs_size:
       
   884   shows "asize (bsimp_AALTs bs rs) \<le> Suc (sum_list (map asize rs))"
       
   885   apply(induct rs rule: bsimp_AALTs.induct)
       
   886   apply(auto simp add: asize_fuse)
       
   887   done
       
   888 
       
   889 
       
   890 lemma bsimp_size:
       
   891   shows "asize (bsimp r) \<le> asize r"
       
   892   apply(induct r)
       
   893        apply(simp_all)
       
   894    apply (meson Suc_le_mono add_mono_thms_linordered_semiring(1) bsimp_ASEQ_size le_trans)
       
   895   apply(rule le_trans)
       
   896    apply(rule bsimp_AALTs_size)
       
   897   apply(simp)
       
   898    apply(rule le_trans)
       
   899    apply(rule flts_size)
       
   900   by (simp add: sum_list_mono)
       
   901 
       
   902 lemma bsimp_asize0:
       
   903   shows "(\<Sum>x\<leftarrow>rs. asize (bsimp x)) \<le> sum_list (map asize rs)"
       
   904   apply(induct rs)
       
   905    apply(auto)
       
   906   by (simp add: add_mono bsimp_size)
       
   907 
       
   908 lemma bsimp_AALTs_size2:
       
   909   assumes "\<forall>r \<in> set  rs. nonalt r"
       
   910   shows "asize (bsimp_AALTs bs rs) \<ge> sum_list (map asize rs)"
       
   911   using assms
       
   912   apply(induct rs rule: bsimp_AALTs.induct)
       
   913     apply(simp_all add: asize_fuse)
       
   914   done
       
   915 
       
   916 
       
   917 lemma qq:
       
   918   shows "map (asize \<circ> fuse bs) rs = map asize rs"
       
   919   apply(induct rs)
       
   920    apply(auto simp add: asize_fuse)
       
   921   done
       
   922 
       
   923 lemma flts_size2:
       
   924   assumes "\<exists>bs rs'. AALTs bs  rs' \<in> set rs"
       
   925   shows "sum_list (map asize (flts rs)) < sum_list (map asize rs)"
       
   926   using assms
       
   927   apply(induct rs)
       
   928    apply(auto simp add: qq)
       
   929    apply (simp add: flts_size less_Suc_eq_le)
       
   930   apply(case_tac a)
       
   931        apply(auto simp add: qq)
       
   932    prefer 2
       
   933    apply (simp add: flts_size le_imp_less_Suc)
       
   934   using less_Suc_eq by auto
       
   935 
       
   936 lemma bsimp_AALTs_size3:
       
   937   assumes "\<exists>r \<in> set  (map bsimp rs). \<not>nonalt r"
       
   938   shows "asize (bsimp (AALTs bs rs)) < asize (AALTs bs rs)"
       
   939   using assms flts_size2
       
   940   apply  -
       
   941   apply(clarify)
       
   942   apply(simp)
       
   943   apply(drule_tac x="map bsimp rs" in meta_spec)
       
   944   apply(drule meta_mp)
       
   945   apply (metis list.set_map nonalt.elims(3))
       
   946   apply(simp)
       
   947   apply(rule order_class.order.strict_trans1)
       
   948    apply(rule bsimp_AALTs_size)
       
   949   apply(simp)
       
   950   by (smt Suc_leI bsimp_asize0 comp_def le_imp_less_Suc le_trans map_eq_conv not_less_eq)
       
   951 
       
   952 
       
   953 
       
   954 
       
   955 lemma L_bsimp_ASEQ:
       
   956   "L (SEQ (erase r1) (erase r2)) = L (erase (bsimp_ASEQ bs r1 r2))"
       
   957   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
       
   958   apply(simp_all)
       
   959   by (metis erase_fuse fuse.simps(4))
       
   960 
       
   961 lemma L_bsimp_AALTs:
       
   962   "L (erase (AALTs bs rs)) = L (erase (bsimp_AALTs bs rs))"
       
   963   apply(induct bs rs rule: bsimp_AALTs.induct)
       
   964   apply(simp_all add: erase_fuse)
       
   965   done
       
   966 
       
   967 lemma L_erase_AALTs:
       
   968   shows "L (erase (AALTs bs rs)) = \<Union> (L ` erase ` (set rs))"
       
   969   apply(induct rs)
       
   970    apply(simp)
       
   971   apply(simp)
       
   972   apply(case_tac rs)
       
   973    apply(simp)
       
   974   apply(simp)
       
   975   done
       
   976 
       
   977 lemma L_erase_flts:
       
   978   shows "\<Union> (L ` erase ` (set (flts rs))) = \<Union> (L ` erase ` (set rs))"
       
   979   apply(induct rs rule: flts.induct)
       
   980         apply(simp_all)
       
   981   apply(auto)
       
   982   using L_erase_AALTs erase_fuse apply auto[1]
       
   983   by (simp add: L_erase_AALTs erase_fuse)
       
   984 
       
   985 
       
   986 lemma L_bsimp_erase:
       
   987   shows "L (erase r) = L (erase (bsimp r))"
       
   988   apply(induct r)
       
   989   apply(simp)
       
   990   apply(simp)
       
   991   apply(simp)
       
   992   apply(auto simp add: Sequ_def)[1]
       
   993   apply(subst L_bsimp_ASEQ[symmetric])
       
   994   apply(auto simp add: Sequ_def)[1]
       
   995   apply(subst (asm)  L_bsimp_ASEQ[symmetric])
       
   996   apply(auto simp add: Sequ_def)[1]
       
   997    apply(simp)
       
   998    apply(subst L_bsimp_AALTs[symmetric])
       
   999    defer
       
  1000    apply(simp)
       
  1001   apply(subst (2)L_erase_AALTs)
       
  1002   apply(subst L_erase_flts)
       
  1003   apply(auto)
       
  1004    apply (simp add: L_erase_AALTs)
       
  1005   using L_erase_AALTs by blast
       
  1006 
       
  1007 lemma bsimp_ASEQ0:
       
  1008   shows "bsimp_ASEQ bs r1 AZERO = AZERO"
       
  1009   apply(induct r1)
       
  1010   apply(auto)
       
  1011   done
       
  1012 
       
  1013 
       
  1014 
       
  1015 lemma bsimp_ASEQ1:
       
  1016   assumes "r1 \<noteq> AZERO" "r2 \<noteq> AZERO" "\<forall>bs. r1 \<noteq> AONE bs"
       
  1017   shows "bsimp_ASEQ bs r1 r2 = ASEQ bs r1 r2"
       
  1018   using assms
       
  1019   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
       
  1020   apply(auto)
       
  1021   done
       
  1022 
       
  1023 lemma bsimp_ASEQ2:
       
  1024   shows "bsimp_ASEQ bs (AONE bs1) r2 = fuse (bs @ bs1) r2"
       
  1025   apply(induct r2)
       
  1026   apply(auto)
       
  1027   done
       
  1028 
       
  1029 
       
  1030 lemma L_bders_simp:
       
  1031   shows "L (erase (bders_simp r s)) = L (erase (bders r s))"
       
  1032   apply(induct s arbitrary: r rule: rev_induct)
       
  1033    apply(simp)
       
  1034   apply(simp)
       
  1035   apply(simp add: ders_append)
       
  1036   apply(simp add: bders_simp_append)
       
  1037   apply(simp add: L_bsimp_erase[symmetric])
       
  1038   by (simp add: der_correctness)
       
  1039 
       
  1040 lemma b1:
       
  1041   "bsimp_ASEQ bs1 (AONE bs) r =  fuse (bs1 @ bs) r" 
       
  1042   apply(induct r)
       
  1043        apply(auto)
       
  1044   done
       
  1045 
       
  1046 lemma b2:
       
  1047   assumes "bnullable r"
       
  1048   shows "bmkeps (fuse bs r) = bs @ bmkeps r"
       
  1049   by (simp add: assms bmkeps_retrieve bnullable_correctness erase_fuse mkeps_nullable retrieve_fuse2)
       
  1050 
       
  1051 lemma b3:
       
  1052   shows "bnullable r = bnullable (bsimp r)"
       
  1053   using L_bsimp_erase bnullable_correctness nullable_correctness by auto
       
  1054 
       
  1055 
       
  1056 lemma b4:
       
  1057   shows "bnullable (bders_simp r s) = bnullable (bders r s)"
       
  1058   by (metis L_bders_simp bnullable_correctness lexer.simps(1) lexer_correct_None option.distinct(1))
       
  1059 
       
  1060 lemma q1:
       
  1061   assumes "\<forall>r \<in> set rs. bmkeps(bsimp r) = bmkeps r"
       
  1062   shows "map (\<lambda>r. bmkeps(bsimp r)) rs = map bmkeps rs"
       
  1063   using assms
       
  1064   apply(induct rs)
       
  1065   apply(simp)
       
  1066   apply(simp)
       
  1067   done
       
  1068 
       
  1069 lemma q3:
       
  1070   assumes "\<exists>r \<in> set rs. bnullable r"
       
  1071   shows "bmkeps (AALTs bs rs) = bmkeps (bsimp_AALTs bs rs)"
       
  1072   using assms
       
  1073   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  1074     apply(simp)
       
  1075    apply(simp)
       
  1076   apply (simp add: b2)
       
  1077   apply(simp)
       
  1078   done
       
  1079 
       
  1080 
       
  1081 lemma fuse_empty:
       
  1082   shows "fuse [] r = r"
       
  1083   apply(induct r)
       
  1084        apply(auto)
       
  1085   done
       
  1086 
       
  1087 lemma flts_fuse:
       
  1088   shows "map (fuse bs) (flts rs) = flts (map (fuse bs) rs)"
       
  1089   apply(induct rs arbitrary: bs rule: flts.induct)
       
  1090         apply(auto simp add: fuse_append)
       
  1091   done
       
  1092 
       
  1093 lemma bsimp_ASEQ_fuse:
       
  1094   shows "fuse bs1 (bsimp_ASEQ bs2 r1 r2) = bsimp_ASEQ (bs1 @ bs2) r1 r2"
       
  1095   apply(induct r1 r2 arbitrary: bs1 bs2 rule: bsimp_ASEQ.induct)
       
  1096   apply(auto)
       
  1097   done
       
  1098 
       
  1099 lemma bsimp_AALTs_fuse:
       
  1100   assumes "\<forall>r \<in> set rs. fuse bs1 (fuse bs2 r) = fuse (bs1 @ bs2) r"
       
  1101   shows "fuse bs1 (bsimp_AALTs bs2 rs) = bsimp_AALTs (bs1 @ bs2) rs"
       
  1102   using assms
       
  1103   apply(induct bs2 rs arbitrary: bs1 rule: bsimp_AALTs.induct)
       
  1104   apply(auto)
       
  1105   done
       
  1106 
       
  1107 
       
  1108 
       
  1109 lemma bsimp_fuse:
       
  1110   shows "fuse bs (bsimp r) = bsimp (fuse bs r)"
       
  1111 apply(induct r arbitrary: bs)
       
  1112        apply(simp)
       
  1113       apply(simp)
       
  1114      apply(simp)
       
  1115     prefer 3
       
  1116     apply(simp)
       
  1117    apply(simp)
       
  1118    apply (simp add: bsimp_ASEQ_fuse)
       
  1119   apply(simp)
       
  1120   by (simp add: bsimp_AALTs_fuse fuse_append)
       
  1121 
       
  1122 lemma bsimp_fuse_AALTs:
       
  1123   shows "fuse bs (bsimp (AALTs [] rs)) = bsimp (AALTs bs rs)"
       
  1124   apply(subst bsimp_fuse) 
       
  1125   apply(simp)
       
  1126   done
       
  1127 
       
  1128 lemma bsimp_fuse_AALTs2:
       
  1129   shows "fuse bs (bsimp_AALTs [] rs) = bsimp_AALTs bs rs"
       
  1130   using bsimp_AALTs_fuse fuse_append by auto
       
  1131   
       
  1132 
       
  1133 lemma bsimp_ASEQ_idem:
       
  1134   assumes "bsimp (bsimp r1) = bsimp r1" "bsimp (bsimp r2) = bsimp r2"
       
  1135   shows "bsimp (bsimp_ASEQ x1 (bsimp r1) (bsimp r2)) = bsimp_ASEQ x1 (bsimp r1) (bsimp r2)"
       
  1136   using assms
       
  1137   apply(case_tac "bsimp r1 = AZERO")
       
  1138     apply(simp)
       
  1139  apply(case_tac "bsimp r2 = AZERO")
       
  1140     apply(simp)
       
  1141   apply (metis bnullable.elims(2) bnullable.elims(3) bsimp.simps(3) bsimp_ASEQ.simps(2) bsimp_ASEQ.simps(3) bsimp_ASEQ.simps(4) bsimp_ASEQ.simps(5) bsimp_ASEQ.simps(6))  
       
  1142   apply(case_tac "\<exists>bs. bsimp r1 = AONE bs")
       
  1143     apply(auto)[1]
       
  1144     apply(subst bsimp_ASEQ2)
       
  1145    apply(subst bsimp_ASEQ2)
       
  1146   apply (metis assms(2) bsimp_fuse)
       
  1147       apply(subst bsimp_ASEQ1)
       
  1148       apply(auto)
       
  1149   done
       
  1150 
       
  1151 
       
  1152 
       
  1153 lemma  k0:
       
  1154   shows "flts (r # rs1) = flts [r] @ flts rs1"
       
  1155   apply(induct r arbitrary: rs1)
       
  1156    apply(auto)
       
  1157   done
       
  1158 
       
  1159 lemma  k00:
       
  1160   shows "flts (rs1 @ rs2) = flts rs1 @ flts rs2"
       
  1161   apply(induct rs1 arbitrary: rs2)
       
  1162    apply(auto)
       
  1163   by (metis append.assoc k0)
       
  1164 
       
  1165 lemma  k0a:
       
  1166   shows "flts [AALTs bs rs] = map (fuse bs)  rs"
       
  1167   apply(simp)
       
  1168   done
       
  1169 
       
  1170 
       
  1171 lemma  k0b:
       
  1172   assumes "nonalt r" "r \<noteq> AZERO"
       
  1173   shows "flts [r] = [r]"
       
  1174   using assms
       
  1175   apply(case_tac  r)
       
  1176   apply(simp_all)
       
  1177   done
       
  1178 
       
  1179 lemma nn1:
       
  1180   assumes "nonnested (AALTs bs rs)"
       
  1181   shows "\<nexists>bs1 rs1. flts rs = [AALTs bs1 rs1]"
       
  1182   using assms
       
  1183   apply(induct rs rule: flts.induct)
       
  1184   apply(auto)
       
  1185   done
       
  1186 
       
  1187 lemma nn1q:
       
  1188   assumes "nonnested (AALTs bs rs)"
       
  1189   shows "\<nexists>bs1 rs1. AALTs bs1 rs1 \<in> set (flts rs)"
       
  1190   using assms
       
  1191   apply(induct rs rule: flts.induct)
       
  1192   apply(auto)
       
  1193   done
       
  1194 
       
  1195 lemma nn1qq:
       
  1196   assumes "nonnested (AALTs bs rs)"
       
  1197   shows "\<nexists>bs1 rs1. AALTs bs1 rs1 \<in> set rs"
       
  1198   using assms
       
  1199   apply(induct rs rule: flts.induct)
       
  1200   apply(auto)
       
  1201   done
       
  1202 
       
  1203 lemma nn10:
       
  1204   assumes "nonnested (AALTs cs rs)" 
       
  1205   shows "nonnested (AALTs (bs @ cs) rs)"
       
  1206   using assms
       
  1207   apply(induct rs arbitrary: cs bs)
       
  1208    apply(simp_all)
       
  1209   apply(case_tac a)
       
  1210        apply(simp_all)
       
  1211   done
       
  1212 
       
  1213 lemma nn11a:
       
  1214   assumes "nonalt r"
       
  1215   shows "nonalt (fuse bs r)"
       
  1216   using assms
       
  1217   apply(induct r)
       
  1218        apply(auto)
       
  1219   done
       
  1220 
       
  1221 
       
  1222 lemma nn1a:
       
  1223   assumes "nonnested r"
       
  1224   shows "nonnested (fuse bs r)"
       
  1225   using assms
       
  1226   apply(induct bs r arbitrary: rule: fuse.induct)
       
  1227        apply(simp_all add: nn10)
       
  1228   done  
       
  1229 
       
  1230 lemma n0:
       
  1231   shows "nonnested (AALTs bs rs) \<longleftrightarrow> (\<forall>r \<in> set rs. nonalt r)"
       
  1232   apply(induct rs  arbitrary: bs)
       
  1233    apply(auto)
       
  1234     apply (metis list.set_intros(1) nn1qq nonalt.elims(3))
       
  1235    apply (metis list.set_intros(2) nn1qq nonalt.elims(3))
       
  1236   by (metis nonalt.elims(2) nonnested.simps(3) nonnested.simps(4) nonnested.simps(5) nonnested.simps(6) nonnested.simps(7))
       
  1237 
       
  1238   
       
  1239   
       
  1240 
       
  1241 lemma nn1c:
       
  1242   assumes "\<forall>r \<in> set rs. nonnested r"
       
  1243   shows "\<forall>r \<in> set (flts rs). nonalt r"
       
  1244   using assms
       
  1245   apply(induct rs rule: flts.induct)
       
  1246         apply(auto)
       
  1247   apply(rule nn11a)
       
  1248   by (metis nn1qq nonalt.elims(3))
       
  1249 
       
  1250 lemma nn1bb:
       
  1251   assumes "\<forall>r \<in> set rs. nonalt r"
       
  1252   shows "nonnested (bsimp_AALTs bs rs)"
       
  1253   using assms
       
  1254   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  1255     apply(auto)
       
  1256    apply (metis nn11a nonalt.simps(1) nonnested.elims(3))
       
  1257   using n0 by auto
       
  1258     
       
  1259 lemma nn1b:
       
  1260   shows "nonnested (bsimp r)"
       
  1261   apply(induct r)
       
  1262        apply(simp_all)
       
  1263   apply(case_tac "bsimp r1 = AZERO")
       
  1264     apply(simp)
       
  1265  apply(case_tac "bsimp r2 = AZERO")
       
  1266    apply(simp)
       
  1267     apply(subst bsimp_ASEQ0)
       
  1268   apply(simp)
       
  1269   apply(case_tac "\<exists>bs. bsimp r1 = AONE bs")
       
  1270     apply(auto)[1]
       
  1271     apply(subst bsimp_ASEQ2)
       
  1272   apply (simp add: nn1a)    
       
  1273    apply(subst bsimp_ASEQ1)
       
  1274       apply(auto)
       
  1275   apply(rule nn1bb)
       
  1276   apply(auto)
       
  1277   by (metis (mono_tags, hide_lams) imageE nn1c set_map)
       
  1278 
       
  1279 lemma nn1d:
       
  1280   assumes "bsimp r = AALTs bs rs"
       
  1281   shows "\<forall>r1 \<in> set rs. \<forall>  bs. r1 \<noteq> AALTs bs  rs2"
       
  1282   using nn1b assms
       
  1283   by (metis nn1qq)
       
  1284 
       
  1285 lemma nn_flts:
       
  1286   assumes "nonnested (AALTs bs rs)"
       
  1287   shows "\<forall>r \<in>  set (flts rs). nonalt r"
       
  1288   using assms
       
  1289   apply(induct rs arbitrary: bs rule: flts.induct)
       
  1290         apply(auto)
       
  1291   done
       
  1292 
       
  1293 
       
  1294 
       
  1295 lemma rt:
       
  1296   shows "sum_list (map asize (flts (map bsimp rs))) \<le> sum_list (map asize rs)"
       
  1297   apply(induct rs)
       
  1298    apply(simp)
       
  1299   apply(simp)
       
  1300   apply(subst  k0)
       
  1301   apply(simp)
       
  1302   by (smt add_le_cancel_right add_mono bsimp_size flts.simps(1) flts_size k0 le_iff_add list.simps(9) map_append sum_list.Cons sum_list.append trans_le_add1)
       
  1303 
       
  1304 lemma bsimp_AALTs_qq:
       
  1305   assumes "1 < length rs"
       
  1306   shows "bsimp_AALTs bs rs = AALTs bs  rs"
       
  1307   using  assms
       
  1308   apply(case_tac rs)
       
  1309    apply(simp)
       
  1310   apply(case_tac list)
       
  1311    apply(simp_all)
       
  1312   done
       
  1313 
       
  1314 
       
  1315 lemma bsimp_AALTs1:
       
  1316   assumes "nonalt r"
       
  1317   shows "bsimp_AALTs bs (flts [r]) = fuse bs r"
       
  1318   using  assms
       
  1319   apply(case_tac r)
       
  1320    apply(simp_all)
       
  1321   done
       
  1322 
       
  1323 lemma bbbbs:
       
  1324   assumes "good r" "r = AALTs bs1 rs"
       
  1325   shows "bsimp_AALTs bs (flts [r]) = AALTs bs (map (fuse bs1) rs)"
       
  1326   using  assms
       
  1327   by (metis (no_types, lifting) Nil_is_map_conv append.left_neutral append_butlast_last_id bsimp_AALTs.elims butlast.simps(2) good.simps(4) good.simps(5) k0a map_butlast)
       
  1328 
       
  1329 lemma bbbbs1:
       
  1330   shows "nonalt r \<or> (\<exists>bs rs. r  = AALTs bs rs)"
       
  1331   using nonalt.elims(3) by auto
       
  1332   
       
  1333 
       
  1334 lemma good_fuse:
       
  1335   shows "good (fuse bs r) = good r"
       
  1336   apply(induct r arbitrary: bs)
       
  1337        apply(auto)
       
  1338      apply(case_tac r1)
       
  1339           apply(simp_all)
       
  1340   apply(case_tac r2)
       
  1341           apply(simp_all)
       
  1342   apply(case_tac r2)
       
  1343             apply(simp_all)
       
  1344   apply(case_tac r2)
       
  1345            apply(simp_all)
       
  1346   apply(case_tac r2)
       
  1347           apply(simp_all)
       
  1348   apply(case_tac r1)
       
  1349           apply(simp_all)
       
  1350   apply(case_tac r2)
       
  1351            apply(simp_all)
       
  1352   apply(case_tac r2)
       
  1353            apply(simp_all)
       
  1354   apply(case_tac r2)
       
  1355            apply(simp_all)
       
  1356   apply(case_tac r2)
       
  1357          apply(simp_all)
       
  1358   apply(case_tac x2a)
       
  1359     apply(simp_all)
       
  1360   apply(case_tac list)
       
  1361     apply(simp_all)
       
  1362   apply(case_tac x2a)
       
  1363     apply(simp_all)
       
  1364   apply(case_tac list)
       
  1365     apply(simp_all)
       
  1366   done
       
  1367 
       
  1368 lemma good0:
       
  1369   assumes "rs \<noteq> Nil" "\<forall>r \<in> set rs. nonalt r"
       
  1370   shows "good (bsimp_AALTs bs rs) \<longleftrightarrow> (\<forall>r \<in> set rs. good r)"
       
  1371   using  assms
       
  1372   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  1373   apply(auto simp add: good_fuse)
       
  1374   done
       
  1375 
       
  1376 lemma good0a:
       
  1377   assumes "flts (map bsimp rs) \<noteq> Nil" "\<forall>r \<in> set (flts (map bsimp rs)). nonalt r"
       
  1378   shows "good (bsimp (AALTs bs rs)) \<longleftrightarrow> (\<forall>r \<in> set (flts (map bsimp rs)). good r)"
       
  1379   using  assms
       
  1380   apply(simp)
       
  1381   apply(auto)
       
  1382   apply(subst (asm) good0)
       
  1383    apply(simp)
       
  1384     apply(auto)
       
  1385    apply(subst good0)
       
  1386    apply(simp)
       
  1387     apply(auto)
       
  1388   done
       
  1389 
       
  1390 lemma flts0:
       
  1391   assumes "r \<noteq> AZERO" "nonalt r"
       
  1392   shows "flts [r] \<noteq> []"
       
  1393   using  assms
       
  1394   apply(induct r)
       
  1395        apply(simp_all)
       
  1396   done
       
  1397 
       
  1398 lemma flts1:
       
  1399   assumes "good r" 
       
  1400   shows "flts [r] \<noteq> []"
       
  1401   using  assms
       
  1402   apply(induct r)
       
  1403        apply(simp_all)
       
  1404   apply(case_tac x2a)
       
  1405    apply(simp)
       
  1406   apply(simp)
       
  1407   done
       
  1408 
       
  1409 lemma flts2:
       
  1410   assumes "good r" 
       
  1411   shows "\<forall>r' \<in> set (flts [r]). good r' \<and> nonalt r'"
       
  1412   using  assms
       
  1413   apply(induct r)
       
  1414        apply(simp)
       
  1415       apply(simp)
       
  1416      apply(simp)
       
  1417     prefer 2
       
  1418     apply(simp)
       
  1419     apply(auto)[1]
       
  1420      apply (metis bsimp_AALTs.elims good.simps(4) good.simps(5) good.simps(6) good_fuse)
       
  1421   apply (metis bsimp_AALTs.elims good.simps(4) good.simps(5) good.simps(6) nn11a)
       
  1422    apply fastforce
       
  1423   apply(simp)
       
  1424   done  
       
  1425 
       
  1426 
       
  1427 lemma flts3:
       
  1428   assumes "\<forall>r \<in> set rs. good r \<or> r = AZERO" 
       
  1429   shows "\<forall>r \<in> set (flts rs). good r"
       
  1430   using  assms
       
  1431   apply(induct rs arbitrary: rule: flts.induct)
       
  1432         apply(simp_all)
       
  1433   by (metis UnE flts2 k0a set_map)
       
  1434 
       
  1435 lemma flts3b:
       
  1436   assumes "\<exists>r\<in>set rs. good r"
       
  1437   shows "flts rs \<noteq> []"
       
  1438   using  assms
       
  1439   apply(induct rs arbitrary: rule: flts.induct)
       
  1440         apply(simp)
       
  1441        apply(simp)
       
  1442       apply(simp)
       
  1443       apply(auto)
       
  1444   done
       
  1445 
       
  1446 lemma flts4:
       
  1447   assumes "bsimp_AALTs bs (flts rs) = AZERO"
       
  1448   shows "\<forall>r \<in> set rs. \<not> good r"
       
  1449   using assms
       
  1450   apply(induct rs arbitrary: bs rule: flts.induct)
       
  1451         apply(auto)
       
  1452         defer
       
  1453   apply (metis (no_types, lifting) Nil_is_append_conv append_self_conv2 bsimp_AALTs.elims butlast.simps(2) butlast_append flts3b nonalt.simps(1) nonalt.simps(2))
       
  1454   apply (metis arexp.distinct(7) bsimp_AALTs.elims flts2 good.simps(1) good.simps(2) good0 k0b list.distinct(1) list.inject nonalt.simps(3))
       
  1455   apply (metis arexp.distinct(3) arexp.distinct(7) bsimp_AALTs.elims fuse.simps(3) list.distinct(1) list.inject)
       
  1456   apply (metis arexp.distinct(7) bsimp_AALTs.elims good.simps(1) good_fuse list.distinct(1) list.inject)
       
  1457     apply (metis arexp.distinct(7) bsimp_AALTs.elims list.distinct(1) list.inject)
       
  1458   apply (metis arexp.distinct(7) bsimp_AALTs.elims flts2 good.simps(1) good.simps(33) good0 k0b list.distinct(1) list.inject nonalt.simps(6))
       
  1459   by (metis (no_types, lifting) Nil_is_append_conv append_Nil2 arexp.distinct(7) bsimp_AALTs.elims butlast.simps(2) butlast_append flts1 flts2 good.simps(1) good0 k0a)
       
  1460 
       
  1461 
       
  1462 lemma flts_nil:
       
  1463   assumes "\<forall>y. asize y < Suc (sum_list (map asize rs)) \<longrightarrow>
       
  1464             good (bsimp y) \<or> bsimp y = AZERO"
       
  1465   and "\<forall>r\<in>set rs. \<not> good (bsimp r)"
       
  1466   shows "flts (map bsimp rs) = []"
       
  1467   using assms
       
  1468   apply(induct rs)
       
  1469    apply(simp)
       
  1470   apply(simp)
       
  1471   apply(subst k0)
       
  1472   apply(simp)
       
  1473   by force
       
  1474 
       
  1475 lemma flts_nil2:
       
  1476   assumes "\<forall>y. asize y < Suc (sum_list (map asize rs)) \<longrightarrow>
       
  1477             good (bsimp y) \<or> bsimp y = AZERO"
       
  1478   and "bsimp_AALTs bs (flts (map bsimp rs)) = AZERO"
       
  1479   shows "flts (map bsimp rs) = []"
       
  1480   using assms
       
  1481   apply(induct rs arbitrary: bs)
       
  1482    apply(simp)
       
  1483   apply(simp)
       
  1484   apply(subst k0)
       
  1485   apply(simp)
       
  1486   apply(subst (asm) k0)
       
  1487   apply(auto)
       
  1488   apply (metis flts.simps(1) flts.simps(2) flts4 k0 less_add_Suc1 list.set_intros(1))
       
  1489   by (metis flts.simps(2) flts4 k0 less_add_Suc1 list.set_intros(1))
       
  1490   
       
  1491   
       
  1492 
       
  1493 lemma good_SEQ:
       
  1494   assumes "r1 \<noteq> AZERO" "r2 \<noteq> AZERO" "\<forall>bs. r1 \<noteq> AONE bs"
       
  1495   shows "good (ASEQ bs r1 r2) = (good r1 \<and> good r2)"
       
  1496   using assms
       
  1497   apply(case_tac r1)
       
  1498        apply(simp_all)
       
  1499   apply(case_tac r2)
       
  1500           apply(simp_all)
       
  1501   apply(case_tac r2)
       
  1502          apply(simp_all)
       
  1503   apply(case_tac r2)
       
  1504         apply(simp_all)
       
  1505   apply(case_tac r2)
       
  1506        apply(simp_all)
       
  1507   done
       
  1508 
       
  1509 lemma good1:
       
  1510   shows "good (bsimp a) \<or> bsimp a = AZERO"
       
  1511   apply(induct a taking: asize rule: measure_induct)
       
  1512   apply(case_tac x)
       
  1513   apply(simp)
       
  1514   apply(simp)
       
  1515   apply(simp)
       
  1516   prefer 3
       
  1517     apply(simp)
       
  1518    prefer 2
       
  1519   (*  AALTs case  *)
       
  1520   apply(simp only:)
       
  1521    apply(case_tac "x52")
       
  1522     apply(simp)
       
  1523   thm good0a
       
  1524    (*  AALTs list at least one - case *)
       
  1525    apply(simp only: )
       
  1526   apply(frule_tac x="a" in spec)
       
  1527    apply(drule mp)
       
  1528     apply(simp)
       
  1529    (* either first element is good, or AZERO *)
       
  1530     apply(erule disjE)
       
  1531      prefer 2
       
  1532     apply(simp)
       
  1533    (* in  the AZERO case, the size  is smaller *)
       
  1534    apply(drule_tac x="AALTs x51 list" in spec)
       
  1535    apply(drule mp)
       
  1536      apply(simp add: asize0)
       
  1537     apply(subst (asm) bsimp.simps)
       
  1538   apply(subst (asm) bsimp.simps)
       
  1539     apply(assumption)
       
  1540    (* in the good case *)
       
  1541   apply(frule_tac x="AALTs x51 list" in spec)
       
  1542    apply(drule mp)
       
  1543     apply(simp add: asize0)
       
  1544    apply(erule disjE)
       
  1545     apply(rule disjI1)
       
  1546   apply(simp add: good0)
       
  1547     apply(subst good0)
       
  1548       apply (metis Nil_is_append_conv flts1 k0)
       
  1549   apply (metis ex_map_conv list.simps(9) nn1b nn1c)
       
  1550   apply(simp)
       
  1551     apply(subst k0)
       
  1552     apply(simp)
       
  1553     apply(auto)[1]
       
  1554   using flts2 apply blast
       
  1555     apply(subst  (asm) good0)
       
  1556       prefer 3
       
  1557       apply(auto)[1]
       
  1558      apply auto[1]
       
  1559     apply (metis ex_map_conv nn1b nn1c)
       
  1560   (* in  the AZERO case *)
       
  1561    apply(simp)
       
  1562    apply(frule_tac x="a" in spec)
       
  1563    apply(drule mp)
       
  1564   apply(simp)
       
  1565    apply(erule disjE)
       
  1566     apply(rule disjI1)
       
  1567     apply(subst good0)
       
  1568   apply(subst k0)
       
  1569   using flts1 apply blast
       
  1570      apply(auto)[1]
       
  1571   apply (metis (no_types, hide_lams) ex_map_conv list.simps(9) nn1b nn1c)
       
  1572     apply(auto)[1]
       
  1573   apply(subst (asm) k0)
       
  1574   apply(auto)[1]
       
  1575   using flts2 apply blast
       
  1576   apply(frule_tac x="AALTs x51 list" in spec)
       
  1577    apply(drule mp)
       
  1578      apply(simp add: asize0)
       
  1579     apply(erule disjE)
       
  1580      apply(simp)
       
  1581     apply(simp)
       
  1582   apply (metis add.left_commute flts_nil2 less_add_Suc1 less_imp_Suc_add list.distinct(1) list.set_cases nat.inject)
       
  1583    apply(subst (2) k0)
       
  1584   apply(simp)
       
  1585   (* SEQ case *)
       
  1586   apply(simp)
       
  1587   apply(case_tac "bsimp x42 = AZERO")
       
  1588     apply(simp)
       
  1589  apply(case_tac "bsimp x43 = AZERO")
       
  1590    apply(simp)
       
  1591     apply(subst (2) bsimp_ASEQ0)
       
  1592   apply(simp)
       
  1593   apply(case_tac "\<exists>bs. bsimp x42 = AONE bs")
       
  1594     apply(auto)[1]
       
  1595    apply(subst bsimp_ASEQ2)
       
  1596   using good_fuse apply force
       
  1597    apply(subst bsimp_ASEQ1)
       
  1598      apply(auto)
       
  1599   apply(subst  good_SEQ)
       
  1600   apply(simp)
       
  1601     apply(simp)
       
  1602    apply(simp)
       
  1603   using less_add_Suc1 less_add_Suc2 by blast
       
  1604 
       
  1605 lemma good1a:
       
  1606   assumes "L(erase a) \<noteq> {}"
       
  1607   shows "good (bsimp a)"
       
  1608   using good1 assms
       
  1609   using L_bsimp_erase by force
       
  1610   
       
  1611 
       
  1612 
       
  1613 lemma flts_append:
       
  1614   "flts (xs1 @ xs2) = flts xs1 @ flts xs2"
       
  1615   apply(induct xs1  arbitrary: xs2  rule: rev_induct)
       
  1616    apply(auto)
       
  1617   apply(case_tac xs)
       
  1618    apply(auto)
       
  1619    apply(case_tac x)
       
  1620         apply(auto)
       
  1621   apply(case_tac x)
       
  1622         apply(auto)
       
  1623   done
       
  1624 
       
  1625 lemma g1:
       
  1626   assumes "good (bsimp_AALTs bs rs)"
       
  1627   shows "bsimp_AALTs bs rs = AALTs bs rs \<or> (\<exists>r. rs = [r] \<and> bsimp_AALTs bs [r] = fuse bs r)"
       
  1628 using assms
       
  1629     apply(induct rs arbitrary: bs)
       
  1630   apply(simp)
       
  1631   apply(case_tac rs)
       
  1632   apply(simp only:)
       
  1633   apply(simp)
       
  1634   apply(case_tac  list)
       
  1635   apply(simp)
       
  1636   by simp
       
  1637 
       
  1638 lemma flts_0:
       
  1639   assumes "nonnested (AALTs bs  rs)"
       
  1640   shows "\<forall>r \<in> set (flts rs). r \<noteq> AZERO"
       
  1641   using assms
       
  1642   apply(induct rs arbitrary: bs rule: flts.induct)
       
  1643         apply(simp) 
       
  1644        apply(simp) 
       
  1645       defer
       
  1646       apply(simp) 
       
  1647      apply(simp) 
       
  1648     apply(simp) 
       
  1649 apply(simp) 
       
  1650   apply(rule ballI)
       
  1651   apply(simp)
       
  1652   done
       
  1653 
       
  1654 lemma flts_0a:
       
  1655   assumes "nonnested (AALTs bs  rs)"
       
  1656   shows "AZERO \<notin> set (flts rs)"
       
  1657   using assms
       
  1658   using flts_0 by blast 
       
  1659   
       
  1660 lemma qqq1:
       
  1661   shows "AZERO \<notin> set (flts (map bsimp rs))"
       
  1662   by (metis ex_map_conv flts3 good.simps(1) good1)
       
  1663 
       
  1664 
       
  1665 fun nonazero :: "arexp \<Rightarrow> bool"
       
  1666   where
       
  1667   "nonazero AZERO = False"
       
  1668 | "nonazero r = True"
       
  1669 
       
  1670 lemma flts_concat:
       
  1671   shows "flts rs = concat (map (\<lambda>r. flts [r]) rs)"
       
  1672   apply(induct rs)
       
  1673    apply(auto)
       
  1674   apply(subst k0)
       
  1675   apply(simp)
       
  1676   done
       
  1677 
       
  1678 lemma flts_single1:
       
  1679   assumes "nonalt r" "nonazero r"
       
  1680   shows "flts [r] = [r]"
       
  1681   using assms
       
  1682   apply(induct r)
       
  1683   apply(auto)
       
  1684   done
       
  1685 
       
  1686 lemma flts_qq:
       
  1687   assumes "\<forall>y. asize y < Suc (sum_list (map asize rs)) \<longrightarrow> good y \<longrightarrow> bsimp y = y" 
       
  1688           "\<forall>r'\<in>set rs. good r' \<and> nonalt r'"
       
  1689   shows "flts (map bsimp rs) = rs"
       
  1690   using assms
       
  1691   apply(induct rs)
       
  1692    apply(simp)
       
  1693   apply(simp)
       
  1694   apply(subst k0)
       
  1695   apply(subgoal_tac "flts [bsimp a] =  [a]")
       
  1696    prefer 2
       
  1697    apply(drule_tac x="a" in spec)
       
  1698    apply(drule mp)
       
  1699     apply(simp)
       
  1700    apply(auto)[1]
       
  1701   using good.simps(1) k0b apply blast
       
  1702   apply(auto)[1]  
       
  1703   done
       
  1704   
       
  1705 lemma test:
       
  1706   assumes "good r"
       
  1707   shows "bsimp r = r"
       
  1708   using assms
       
  1709   apply(induct r taking: "asize" rule: measure_induct)
       
  1710   apply(erule good.elims)
       
  1711   apply(simp_all)
       
  1712   apply(subst k0)
       
  1713   apply(subst (2) k0)
       
  1714                 apply(subst flts_qq)
       
  1715                   apply(auto)[1]
       
  1716                  apply(auto)[1]
       
  1717                 apply (metis append_Cons append_Nil bsimp_AALTs.simps(3) good.simps(1) k0b)
       
  1718                apply force+
       
  1719   apply (metis (no_types, lifting) add_Suc add_Suc_right asize.simps(5) bsimp.simps(1) bsimp_ASEQ.simps(19) less_add_Suc1 less_add_Suc2)
       
  1720   apply (metis add_Suc add_Suc_right arexp.distinct(5) arexp.distinct(7) asize.simps(4) asize.simps(5) bsimp.simps(1) bsimp.simps(2) bsimp_ASEQ1 good.simps(21) good.simps(8) less_add_Suc1 less_add_Suc2)
       
  1721          apply force+
       
  1722   apply (metis (no_types, lifting) add_Suc add_Suc_right arexp.distinct(5) arexp.distinct(7) asize.simps(4) asize.simps(5) bsimp.simps(1) bsimp.simps(2) bsimp_ASEQ1 good.simps(25) good.simps(8) less_add_Suc1 less_add_Suc2)
       
  1723   apply (metis add_Suc add_Suc_right arexp.distinct(7) asize.simps(4) bsimp.simps(2) bsimp_ASEQ1 good.simps(26) good.simps(8) less_add_Suc1 less_add_Suc2)
       
  1724     apply force+
       
  1725   done
       
  1726 
       
  1727 lemma test2:
       
  1728   assumes "good r"
       
  1729   shows "bsimp r = r"
       
  1730   using assms
       
  1731   apply(induct r taking: "asize" rule: measure_induct)
       
  1732   apply(case_tac x)
       
  1733        apply(simp_all)
       
  1734    defer  
       
  1735   (* AALT case *)
       
  1736    apply(subgoal_tac "1 < length x52")
       
  1737     prefer 2
       
  1738     apply(case_tac x52)
       
  1739      apply(simp)
       
  1740     apply(simp)
       
  1741     apply(case_tac list)
       
  1742      apply(simp)
       
  1743   apply(simp)
       
  1744     apply(subst bsimp_AALTs_qq)
       
  1745     prefer 2
       
  1746     apply(subst flts_qq)
       
  1747       apply(auto)[1]
       
  1748      apply(auto)[1]
       
  1749    apply(case_tac x52)
       
  1750      apply(simp)
       
  1751     apply(simp)
       
  1752     apply(case_tac list)
       
  1753      apply(simp)
       
  1754       apply(simp)
       
  1755       apply(auto)[1]
       
  1756   apply (metis (no_types, lifting) bsimp_AALTs.elims good.simps(6) length_Cons length_pos_if_in_set list.size(3) nat_neq_iff)
       
  1757   apply(simp)  
       
  1758   apply(case_tac x52)
       
  1759      apply(simp)
       
  1760     apply(simp)
       
  1761     apply(case_tac list)
       
  1762      apply(simp)
       
  1763    apply(simp)
       
  1764    apply(subst k0)
       
  1765    apply(simp)
       
  1766    apply(subst (2) k0)
       
  1767    apply(simp)
       
  1768   apply (simp add: Suc_lessI flts1 one_is_add)
       
  1769   (* SEQ case *)
       
  1770   apply(case_tac "bsimp x42 = AZERO")
       
  1771    apply simp
       
  1772   apply (metis asize.elims good.simps(10) good.simps(11) good.simps(12) good.simps(2) good.simps(7) good.simps(9) good_SEQ less_add_Suc1)  
       
  1773    apply(case_tac "\<exists>bs'. bsimp x42 = AONE bs'")
       
  1774    apply(auto)[1]
       
  1775   defer
       
  1776   apply(case_tac "bsimp x43 = AZERO")
       
  1777     apply(simp)
       
  1778   apply (metis bsimp.elims bsimp.simps(3) good.simps(10) good.simps(11) good.simps(12) good.simps(8) good.simps(9) good_SEQ less_add_Suc2)
       
  1779   apply(auto)  
       
  1780    apply (subst bsimp_ASEQ1)
       
  1781       apply(auto)[3]
       
  1782    apply(auto)[1]
       
  1783     apply (metis bsimp.simps(3) good.simps(2) good_SEQ less_add_Suc1)
       
  1784    apply (metis bsimp.simps(3) good.simps(2) good_SEQ less_add_Suc1 less_add_Suc2)
       
  1785   apply (subst bsimp_ASEQ2)
       
  1786   apply(drule_tac x="x42" in spec)
       
  1787   apply(drule mp)
       
  1788    apply(simp)
       
  1789   apply(drule mp)
       
  1790    apply (metis bsimp.elims bsimp.simps(3) good.simps(10) good.simps(11) good.simps(2) good_SEQ)
       
  1791   apply(simp)
       
  1792   done
       
  1793 
       
  1794 
       
  1795 lemma bsimp_idem:
       
  1796   shows "bsimp (bsimp r) = bsimp r"
       
  1797   using test good1
       
  1798   by force
       
  1799 
       
  1800 
       
  1801 lemma contains_ex1:
       
  1802   assumes "a = AALTs bs1 [AZERO, AONE bs2]" "a >> bs" 
       
  1803   shows "bsimp a >> bs"
       
  1804   using assms
       
  1805   apply(simp)
       
  1806   apply(erule contains.cases)
       
  1807         apply(auto)
       
  1808   using contains.simps apply blast
       
  1809   apply(erule contains.cases)
       
  1810         apply(auto)
       
  1811   using contains0 apply fastforce
       
  1812   using contains.simps by blast
       
  1813     
       
  1814 lemma contains_ex2:
       
  1815   assumes "a = AALTs bs1 [AZERO, AONE bs2, AALTs bs5 [AONE bs3, AZERO, AONE bs4]]" "a >> bs" 
       
  1816   shows "bsimp a >> bs"
       
  1817   using assms
       
  1818   apply(simp)
       
  1819   apply(erule contains.cases)
       
  1820         apply(auto)
       
  1821   using contains.simps apply blast
       
  1822   apply(erule contains.cases)
       
  1823         apply(auto)
       
  1824   using contains3b apply blast
       
  1825    apply(erule contains.cases)
       
  1826         apply(auto)
       
  1827   apply(erule contains.cases)
       
  1828          apply(auto)
       
  1829   apply (metis append.left_neutral contains.intros(4) contains.intros(5) contains0 fuse.simps(2))
       
  1830   apply(erule contains.cases)
       
  1831          apply(auto)
       
  1832   using contains.simps apply blast
       
  1833   apply(erule contains.cases)
       
  1834          apply(auto)
       
  1835   apply (metis append.left_neutral contains.intros(4) contains.intros(5) contains0 fuse.simps(2))
       
  1836       apply(erule contains.cases)
       
  1837          apply(auto)
       
  1838 apply(erule contains.cases)
       
  1839          apply(auto)
       
  1840   done
       
  1841 
       
  1842 lemma contains48:
       
  1843   assumes "\<And>x2aa bs bs1. \<lbrakk>x2aa \<in> set x2a; fuse bs x2aa >> bs @ bs1\<rbrakk> \<Longrightarrow> x2aa >> bs1" 
       
  1844           "AALTs (bs @ x1) x2a >> bs @ bs1"
       
  1845         shows "AALTs x1 x2a >> bs1"
       
  1846   using assms
       
  1847   apply(induct x2a arbitrary: bs x1 bs1)
       
  1848    apply(auto)
       
  1849    apply(erule contains.cases)
       
  1850          apply(auto)
       
  1851   apply(erule contains.cases)
       
  1852         apply(auto)
       
  1853   apply (simp add: contains.intros(4))
       
  1854   using contains.intros(5) by blast
       
  1855 
       
  1856 
       
  1857 lemma contains49:
       
  1858   assumes "fuse bs a >> bs @ bs1"
       
  1859   shows "a >> bs1"
       
  1860   using assms
       
  1861   apply(induct a arbitrary: bs bs1)
       
  1862        apply(auto)
       
  1863   using contains.simps apply blast
       
  1864       apply(erule contains.cases)
       
  1865             apply(auto)
       
  1866   apply(rule contains.intros)
       
  1867     apply(erule contains.cases)
       
  1868             apply(auto)
       
  1869      apply(rule contains.intros)
       
  1870   apply(erule contains.cases)
       
  1871             apply(auto)
       
  1872   apply(rule contains.intros)
       
  1873      apply(auto)[2]
       
  1874   prefer 2
       
  1875   apply(erule contains.cases)
       
  1876          apply(auto)
       
  1877   apply (simp add: contains.intros(6))
       
  1878   using contains.intros(7) apply blast
       
  1879   using contains48 by blast
       
  1880 
       
  1881 lemma contains50:
       
  1882   assumes "bsimp_AALTs bs rs2 >> bs @ bs1"
       
  1883   shows "bsimp_AALTs bs (rs1 @ rs2) >> bs @ bs1"
       
  1884   using assms
       
  1885   apply(induct rs1 arbitrary: bs rs2 bs1)
       
  1886    apply(simp)
       
  1887   apply(auto)
       
  1888   apply(case_tac rs1)
       
  1889    apply(simp)
       
  1890    apply(case_tac rs2)
       
  1891     apply(simp)
       
  1892   using contains.simps apply blast
       
  1893   apply(simp)
       
  1894   apply(case_tac list)
       
  1895     apply(simp)
       
  1896     apply(rule contains.intros)
       
  1897     back
       
  1898     apply(rule contains.intros)
       
  1899   using contains49 apply blast
       
  1900    apply(simp)
       
  1901   using contains.intros(5) apply blast
       
  1902   apply(simp)
       
  1903   by (metis bsimp_AALTs.elims contains.intros(4) contains.intros(5) contains49 list.distinct(1))
       
  1904 
       
  1905 lemma contains51:
       
  1906   assumes "bsimp_AALTs bs [r] >> bs @ bs1"
       
  1907   shows "bsimp_AALTs bs ([r] @ rs2) >> bs @ bs1"
       
  1908   using assms
       
  1909   apply(induct rs2 arbitrary: bs r bs1)
       
  1910    apply(simp)
       
  1911   apply(auto)
       
  1912   using contains.intros(4) contains49 by blast
       
  1913 
       
  1914 lemma contains51a:
       
  1915   assumes "bsimp_AALTs bs rs2 >> bs @ bs1"
       
  1916   shows "bsimp_AALTs bs (rs2 @ [r]) >> bs @ bs1"
       
  1917   using assms
       
  1918   apply(induct rs2 arbitrary: bs r bs1)
       
  1919    apply(simp)
       
  1920    apply(auto)
       
  1921   using contains.simps apply blast
       
  1922   apply(case_tac rs2)
       
  1923    apply(auto)
       
  1924   using contains3b contains49 apply blast
       
  1925   apply(case_tac list)
       
  1926    apply(auto)
       
  1927   apply(erule contains.cases)
       
  1928          apply(auto)
       
  1929   using contains.intros(4) apply auto[1]
       
  1930    apply(erule contains.cases)
       
  1931          apply(auto)
       
  1932     apply (simp add: contains.intros(4) contains.intros(5))
       
  1933    apply (simp add: contains.intros(5))
       
  1934   apply(erule contains.cases)
       
  1935         apply(auto)
       
  1936    apply (simp add: contains.intros(4))
       
  1937    apply(erule contains.cases)
       
  1938         apply(auto)
       
  1939   using contains.intros(4) contains.intros(5) apply blast
       
  1940   using contains.intros(5) by blast  
       
  1941   
       
  1942 lemma contains51b:
       
  1943   assumes "bsimp_AALTs bs rs >> bs @ bs1"
       
  1944   shows "bsimp_AALTs bs (rs @ rs2) >> bs @ bs1"
       
  1945   using assms
       
  1946   apply(induct rs2 arbitrary: bs rs bs1)
       
  1947    apply(simp)
       
  1948   using contains51a by fastforce
       
  1949 
       
  1950 
       
  1951 lemma contains51c:
       
  1952   assumes "AALTs (bs @ bs2) rs >> bs @ bs1"
       
  1953   shows "bsimp_AALTs bs (map (fuse bs2) rs) >> bs @ bs1"
       
  1954   using assms
       
  1955   apply(induct rs arbitrary: bs bs1 bs2)
       
  1956        apply(auto)
       
  1957   apply(erule contains.cases)
       
  1958         apply(auto)
       
  1959   apply(erule contains.cases)
       
  1960         apply(auto)
       
  1961   using contains0 contains51 apply auto[1]
       
  1962   by (metis append.left_neutral append_Cons contains50 list.simps(9))
       
  1963   
       
  1964 
       
  1965 lemma contains51d:
       
  1966   assumes "fuse bs r >> bs @ bs1"
       
  1967   shows "bsimp_AALTs bs (flts [r]) >> bs @ bs1"
       
  1968   using assms
       
  1969   apply(induct r arbitrary: bs bs1)
       
  1970        apply(auto)
       
  1971   by (simp add: contains51c)
       
  1972 
       
  1973 lemma contains52:
       
  1974   assumes "\<exists>r \<in> set rs. (fuse bs r) >> bs @ bs1"
       
  1975   shows "bsimp_AALTs bs (flts rs) >> bs @ bs1"
       
  1976   using assms
       
  1977   apply(induct rs arbitrary: bs bs1)
       
  1978    apply(simp)
       
  1979   apply(auto)
       
  1980    defer
       
  1981    apply (metis contains50 k0)
       
  1982   apply(subst k0)
       
  1983   apply(rule contains51b)
       
  1984   using contains51d by blast
       
  1985 
       
  1986 lemma contains55:
       
  1987   assumes "a >> bs" 
       
  1988   shows "bsimp a >> bs"
       
  1989   using assms
       
  1990   apply(induct a bs arbitrary:)
       
  1991         apply(auto intro: contains.intros)
       
  1992     apply(case_tac "bsimp a1 = AZERO")
       
  1993      apply(simp)
       
  1994   using contains.simps apply blast
       
  1995   apply(case_tac "bsimp a2 = AZERO")
       
  1996      apply(simp)
       
  1997   using contains.simps apply blast
       
  1998   apply(case_tac "\<exists>bs. bsimp a1 = AONE bs")
       
  1999      apply(auto)[1]
       
  2000      apply(rotate_tac 1)
       
  2001      apply(erule contains.cases)
       
  2002            apply(auto)
       
  2003      apply (simp add: b1 contains0 fuse_append)
       
  2004     apply (simp add: bsimp_ASEQ1 contains.intros(3))
       
  2005    prefer 2
       
  2006    apply(case_tac rs)
       
  2007     apply(simp)
       
  2008   using contains.simps apply blast
       
  2009    apply (metis contains50 k0)
       
  2010   (* AALTS case *)
       
  2011   apply(rule contains52)
       
  2012   apply(rule_tac x="bsimp r" in bexI)
       
  2013    apply(auto)
       
  2014   using contains0 by blast
       
  2015 
       
  2016 
       
  2017 lemma q3a:
       
  2018   assumes "\<exists>r \<in> set rs. bnullable r"
       
  2019   shows "bmkeps (AALTs bs (map (fuse bs1) rs)) = bmkeps (AALTs (bs@bs1) rs)"
       
  2020   using assms
       
  2021   apply(induct rs arbitrary: bs bs1)
       
  2022    apply(simp)
       
  2023   apply(simp)
       
  2024   apply(auto)
       
  2025    apply (metis append_assoc b2 bnullable_correctness erase_fuse r0)
       
  2026   apply(case_tac "bnullable a")
       
  2027    apply (metis append.assoc b2 bnullable_correctness erase_fuse r0)
       
  2028   apply(case_tac rs)
       
  2029   apply(simp)
       
  2030   apply(simp)
       
  2031   apply(auto)[1]
       
  2032    apply (metis bnullable_correctness erase_fuse)+
       
  2033   done
       
  2034 
       
  2035 
       
  2036 
       
  2037 lemma qq4a:
       
  2038   assumes "\<exists>x\<in>set list. bnullable x"
       
  2039   shows "\<exists>x\<in>set (flts list). bnullable x"
       
  2040   using assms
       
  2041   apply(induct list rule: flts.induct)
       
  2042         apply(auto)
       
  2043   by (metis UnCI bnullable_correctness erase_fuse imageI)
       
  2044   
       
  2045 
       
  2046 lemma qs3:
       
  2047   assumes "\<exists>r \<in> set rs. bnullable r"
       
  2048   shows "bmkeps (AALTs bs rs) = bmkeps (AALTs bs (flts rs))"
       
  2049   using assms
       
  2050   apply(induct rs arbitrary: bs taking: size rule: measure_induct)
       
  2051   apply(case_tac x)
       
  2052   apply(simp)
       
  2053   apply(simp)
       
  2054   apply(case_tac a)
       
  2055        apply(simp)
       
  2056        apply (simp add: r1)
       
  2057       apply(simp)
       
  2058       apply (simp add: r0)
       
  2059      apply(simp)
       
  2060      apply(case_tac "flts list")
       
  2061       apply(simp)
       
  2062   apply (metis L_erase_AALTs L_erase_flts L_flat_Prf1 L_flat_Prf2 Prf_elims(1) bnullable_correctness erase.simps(4) mkeps_nullable r2)
       
  2063      apply(simp)
       
  2064      apply (simp add: r1)
       
  2065     prefer 3
       
  2066     apply(simp)
       
  2067     apply (simp add: r0)
       
  2068    prefer 2
       
  2069    apply(simp)
       
  2070   apply(case_tac "\<exists>x\<in>set x52. bnullable x")
       
  2071   apply(case_tac "list")
       
  2072     apply(simp)
       
  2073     apply (metis b2 fuse.simps(4) q3a r2)
       
  2074    apply(erule disjE)
       
  2075     apply(subst qq1)
       
  2076      apply(auto)[1]
       
  2077      apply (metis bnullable_correctness erase_fuse)
       
  2078     apply(simp)
       
  2079      apply (metis b2 fuse.simps(4) q3a r2)
       
  2080     apply(simp)
       
  2081     apply(auto)[1]
       
  2082      apply(subst qq1)
       
  2083       apply (metis bnullable_correctness erase_fuse image_eqI set_map)
       
  2084      apply (metis b2 fuse.simps(4) q3a r2)
       
  2085   apply(subst qq1)
       
  2086       apply (metis bnullable_correctness erase_fuse image_eqI set_map)
       
  2087     apply (metis b2 fuse.simps(4) q3a r2)
       
  2088    apply(simp)
       
  2089    apply(subst qq2)
       
  2090      apply (metis bnullable_correctness erase_fuse imageE set_map)
       
  2091   prefer 2
       
  2092   apply(case_tac "list")
       
  2093      apply(simp)
       
  2094     apply(simp)
       
  2095    apply (simp add: qq4a)
       
  2096   apply(simp)
       
  2097   apply(auto)
       
  2098    apply(case_tac list)
       
  2099     apply(simp)
       
  2100    apply(simp)
       
  2101    apply (simp add: r0)
       
  2102   apply(case_tac "bnullable (ASEQ x41 x42 x43)")
       
  2103    apply(case_tac list)
       
  2104     apply(simp)
       
  2105    apply(simp)
       
  2106    apply (simp add: r0)
       
  2107   apply(simp)
       
  2108   using qq4a r1 r2 by auto
       
  2109 
       
  2110 
       
  2111 
       
  2112 lemma k1:
       
  2113   assumes "\<And>x2aa. \<lbrakk>x2aa \<in> set x2a; bnullable x2aa\<rbrakk> \<Longrightarrow> bmkeps x2aa = bmkeps (bsimp x2aa)"
       
  2114           "\<exists>x\<in>set x2a. bnullable x"
       
  2115         shows "bmkeps (AALTs x1 (flts x2a)) = bmkeps (AALTs x1 (flts (map bsimp x2a)))"
       
  2116   using assms
       
  2117   apply(induct x2a)
       
  2118   apply fastforce
       
  2119   apply(simp)
       
  2120   apply(subst k0)
       
  2121   apply(subst (2) k0)
       
  2122   apply(auto)[1]
       
  2123   apply (metis b3 k0 list.set_intros(1) qs3 r0)
       
  2124   by (smt b3 imageI insert_iff k0 list.set(2) qq3 qs3 r0 r1 set_map)
       
  2125   
       
  2126   
       
  2127   
       
  2128 lemma bmkeps_simp:
       
  2129   assumes "bnullable r"
       
  2130   shows "bmkeps r = bmkeps (bsimp r)"
       
  2131   using  assms
       
  2132   apply(induct r)
       
  2133        apply(simp)
       
  2134       apply(simp)
       
  2135      apply(simp)
       
  2136     apply(simp)
       
  2137     prefer 3
       
  2138   apply(simp)
       
  2139    apply(case_tac "bsimp r1 = AZERO")
       
  2140     apply(simp)
       
  2141     apply(auto)[1]
       
  2142   apply (metis L_bsimp_erase L_flat_Prf1 L_flat_Prf2 Prf_elims(1) bnullable_correctness erase.simps(1) mkeps_nullable)
       
  2143  apply(case_tac "bsimp r2 = AZERO")
       
  2144     apply(simp)  
       
  2145     apply(auto)[1]
       
  2146   apply (metis L_bsimp_erase L_flat_Prf1 L_flat_Prf2 Prf_elims(1) bnullable_correctness erase.simps(1) mkeps_nullable)
       
  2147   apply(case_tac "\<exists>bs. bsimp r1 = AONE bs")
       
  2148     apply(auto)[1]
       
  2149     apply(subst b1)
       
  2150     apply(subst b2)
       
  2151   apply(simp add: b3[symmetric])
       
  2152     apply(simp)
       
  2153    apply(subgoal_tac "bsimp_ASEQ x1 (bsimp r1) (bsimp r2) = ASEQ x1 (bsimp r1) (bsimp r2)")
       
  2154     prefer 2
       
  2155   apply (smt b3 bnullable.elims(2) bsimp_ASEQ.simps(17) bsimp_ASEQ.simps(19) bsimp_ASEQ.simps(20) bsimp_ASEQ.simps(21) bsimp_ASEQ.simps(22) bsimp_ASEQ.simps(24) bsimp_ASEQ.simps(25) bsimp_ASEQ.simps(26) bsimp_ASEQ.simps(27) bsimp_ASEQ.simps(29) bsimp_ASEQ.simps(30) bsimp_ASEQ.simps(31))
       
  2156    apply(simp)
       
  2157   apply(simp)
       
  2158   thm q3
       
  2159   apply(subst q3[symmetric])
       
  2160    apply simp
       
  2161   using b3 qq4a apply auto[1]
       
  2162   apply(subst qs3)
       
  2163    apply simp
       
  2164   using k1 by blast
       
  2165 
       
  2166 thm bmkeps_retrieve bmkeps_simp bder_retrieve
       
  2167 
       
  2168 lemma bmkeps_bder_AALTs:
       
  2169   assumes "\<exists>r \<in> set rs. bnullable (bder c r)" 
       
  2170   shows "bmkeps (bder c (bsimp_AALTs bs rs)) = bmkeps (bsimp_AALTs bs (map (bder c) rs))"
       
  2171   using assms
       
  2172   apply(induct rs)
       
  2173    apply(simp)
       
  2174   apply(simp)
       
  2175   apply(auto)
       
  2176   apply(case_tac rs)
       
  2177     apply(simp)
       
  2178   apply (metis (full_types) Prf_injval bder_retrieve bmkeps_retrieve bnullable_correctness erase_bder erase_fuse mkeps_nullable retrieve_fuse2)
       
  2179    apply(simp)
       
  2180   apply(case_tac  rs)
       
  2181    apply(simp_all)
       
  2182   done
       
  2183 
       
  2184 lemma bbs0:
       
  2185   shows "blexer_simp r [] = blexer r []"
       
  2186   apply(simp add: blexer_def blexer_simp_def)
       
  2187   done
       
  2188 
       
  2189 lemma bbs1:
       
  2190   shows "blexer_simp r [c] = blexer r [c]"
       
  2191   apply(simp add: blexer_def blexer_simp_def)
       
  2192   apply(auto)
       
  2193     defer
       
  2194   using b3 apply auto[1]
       
  2195   using b3 apply auto[1]  
       
  2196   apply(subst bmkeps_simp[symmetric])
       
  2197    apply(simp)
       
  2198   apply(simp)
       
  2199   done
       
  2200 
       
  2201 lemma oo:
       
  2202   shows "(case (blexer (der c r) s) of None \<Rightarrow> None | Some v \<Rightarrow> Some (injval r c v)) = blexer r (c # s)"
       
  2203   apply(simp add: blexer_correctness)
       
  2204   done
       
  2205 
       
  2206 lemma XXX2_helper:
       
  2207   assumes "\<forall>y. asize y < Suc (sum_list (map asize rs)) \<longrightarrow> good y \<longrightarrow> bsimp y = y" 
       
  2208           "\<forall>r'\<in>set rs. good r' \<and> nonalt r'"
       
  2209   shows "flts (map (bsimp \<circ> bder c) (flts (map bsimp rs))) = flts (map (bsimp \<circ> bder c) rs)"
       
  2210   using assms
       
  2211   apply(induct rs arbitrary: c)
       
  2212    apply(simp)
       
  2213   apply(simp)
       
  2214   apply(subst k0)
       
  2215   apply(simp add: flts_append)
       
  2216   apply(subst (2) k0)
       
  2217   apply(simp add: flts_append)
       
  2218   apply(subgoal_tac "flts [a] =  [a]")
       
  2219    prefer 2
       
  2220   using good.simps(1) k0b apply blast
       
  2221   apply(simp)
       
  2222   done
       
  2223 
       
  2224 lemma bmkeps_good:
       
  2225   assumes "good a"
       
  2226   shows "bmkeps (bsimp a) = bmkeps a"
       
  2227   using assms
       
  2228   using test2 by auto
       
  2229 
       
  2230 
       
  2231 lemma xxx_bder:
       
  2232   assumes "good r"
       
  2233   shows "L (erase r) \<noteq> {}"
       
  2234   using assms
       
  2235   apply(induct r rule: good.induct)
       
  2236   apply(auto simp add: Sequ_def)
       
  2237   done
       
  2238 
       
  2239 lemma xxx_bder2:
       
  2240   assumes "L (erase (bsimp r)) = {}"
       
  2241   shows "bsimp r = AZERO"
       
  2242   using assms xxx_bder test2 good1
       
  2243   by blast
       
  2244 
       
  2245 lemma XXX2aa:
       
  2246   assumes "good a"
       
  2247   shows "bsimp (bder c (bsimp a)) = bsimp (bder c a)"
       
  2248   using  assms
       
  2249   by (simp add: test2)
       
  2250 
       
  2251 lemma XXX2aa_ders:
       
  2252   assumes "good a"
       
  2253   shows "bsimp (bders (bsimp a) s) = bsimp (bders a s)"
       
  2254   using  assms
       
  2255   by (simp add: test2)
       
  2256 
       
  2257 lemma XXX4a:
       
  2258   shows "good (bders_simp (bsimp r) s)  \<or> bders_simp (bsimp r) s = AZERO"
       
  2259   apply(induct s arbitrary: r rule:  rev_induct)
       
  2260    apply(simp)
       
  2261   apply (simp add: good1)
       
  2262   apply(simp add: bders_simp_append)
       
  2263   apply (simp add: good1)
       
  2264   done
       
  2265 
       
  2266 lemma XXX4a_good:
       
  2267   assumes "good a"
       
  2268   shows "good (bders_simp a s) \<or> bders_simp a s = AZERO"
       
  2269   using assms
       
  2270   apply(induct s arbitrary: a rule:  rev_induct)
       
  2271    apply(simp)
       
  2272   apply(simp add: bders_simp_append)
       
  2273   apply (simp add: good1)
       
  2274   done
       
  2275 
       
  2276 lemma XXX4a_good_cons:
       
  2277   assumes "s \<noteq> []"
       
  2278   shows "good (bders_simp a s) \<or> bders_simp a s = AZERO"
       
  2279   using assms
       
  2280   apply(case_tac s)
       
  2281    apply(auto)
       
  2282   using XXX4a by blast
       
  2283 
       
  2284 lemma XXX4b:
       
  2285   assumes "good a" "L (erase (bders_simp a s)) \<noteq> {}"
       
  2286   shows "good (bders_simp a s)"
       
  2287   using assms
       
  2288   apply(induct s arbitrary: a)
       
  2289    apply(simp)
       
  2290   apply(simp)
       
  2291   apply(subgoal_tac "L (erase (bder a aa)) = {} \<or> L (erase (bder a aa)) \<noteq> {}")
       
  2292    prefer 2
       
  2293    apply(auto)[1]
       
  2294   apply(erule disjE)
       
  2295    apply(subgoal_tac "bsimp (bder a aa) = AZERO")
       
  2296     prefer 2
       
  2297   using L_bsimp_erase xxx_bder2 apply auto[1]
       
  2298    apply(simp)
       
  2299   apply (metis L.simps(1) XXX4a erase.simps(1))  
       
  2300   apply(drule_tac x="bsimp (bder a aa)" in meta_spec)
       
  2301   apply(drule meta_mp)
       
  2302   apply simp
       
  2303   apply(rule good1a)
       
  2304   apply(auto)
       
  2305   done
       
  2306 
       
  2307 lemma bders_AZERO:
       
  2308   shows "bders AZERO s = AZERO"
       
  2309   and   "bders_simp AZERO s = AZERO"
       
  2310    apply (induct s)
       
  2311      apply(auto)
       
  2312   done
       
  2313 
       
  2314 lemma LA:
       
  2315   assumes "\<Turnstile> v : ders s (erase r)"
       
  2316   shows "retrieve (bders r s) v = retrieve r (flex (erase r) id s v)"
       
  2317   using assms
       
  2318   apply(induct s arbitrary: r v rule: rev_induct)
       
  2319    apply(simp)
       
  2320   apply(simp add: bders_append ders_append)
       
  2321   apply(subst bder_retrieve)
       
  2322    apply(simp)
       
  2323   apply(drule Prf_injval)
       
  2324   by (simp add: flex_append)
       
  2325 
       
  2326 
       
  2327 lemma LB:
       
  2328   assumes "s \<in> (erase r) \<rightarrow> v" 
       
  2329   shows "retrieve r v = retrieve r (flex (erase r) id s (mkeps (ders s (erase r))))"
       
  2330   using assms
       
  2331   apply(induct s arbitrary: r v rule: rev_induct)
       
  2332    apply(simp)
       
  2333    apply(subgoal_tac "v = mkeps (erase r)")
       
  2334     prefer 2
       
  2335   apply (simp add: Posix1(1) Posix_determ Posix_mkeps nullable_correctness)
       
  2336    apply(simp)
       
  2337   apply(simp add: flex_append ders_append)
       
  2338   by (metis Posix_determ Posix_flex Posix_injval Posix_mkeps ders_snoc lexer_correctness(2) lexer_flex)
       
  2339 
       
  2340 lemma LB_sym:
       
  2341   assumes "s \<in> (erase r) \<rightarrow> v" 
       
  2342   shows "retrieve r v = retrieve r (flex (erase r) id s (mkeps (erase (bders r s))))"
       
  2343   using assms
       
  2344   by (simp add: LB)
       
  2345 
       
  2346 
       
  2347 lemma LC:
       
  2348   assumes "s \<in> (erase r) \<rightarrow> v" 
       
  2349   shows "retrieve r v = retrieve (bders r s) (mkeps (erase (bders r s)))"
       
  2350   apply(simp)
       
  2351   by (metis LA LB Posix1(1) assms lexer_correct_None lexer_flex mkeps_nullable)
       
  2352 
       
  2353 
       
  2354 lemma L0:
       
  2355   assumes "bnullable a"
       
  2356   shows "retrieve (bsimp a) (mkeps (erase (bsimp a))) = retrieve a (mkeps (erase a))"
       
  2357   using assms
       
  2358   by (metis b3 bmkeps_retrieve bmkeps_simp bnullable_correctness)
       
  2359 
       
  2360 thm bmkeps_retrieve
       
  2361 
       
  2362 lemma L0a:
       
  2363   assumes "s \<in> L(erase a)"
       
  2364   shows "retrieve (bsimp (bders a s)) (mkeps (erase (bsimp (bders a s)))) = 
       
  2365          retrieve (bders a s) (mkeps (erase (bders a s)))"
       
  2366   using assms
       
  2367   by (metis L0 bnullable_correctness erase_bders lexer_correct_None lexer_flex)
       
  2368   
       
  2369 lemma L0aa:
       
  2370   assumes "s \<in> L (erase a)"
       
  2371   shows "[] \<in> erase (bsimp (bders a s)) \<rightarrow> mkeps (erase (bsimp (bders a s)))"
       
  2372   using assms
       
  2373   by (metis Posix_mkeps b3 bnullable_correctness erase_bders lexer_correct_None lexer_flex)
       
  2374 
       
  2375 lemma L0aaa:
       
  2376   assumes "[c] \<in> L (erase a)"
       
  2377   shows "[c] \<in> (erase a) \<rightarrow> flex (erase a) id [c] (mkeps (erase (bder c a)))"
       
  2378   using assms
       
  2379   by (metis bders.simps(1) bders.simps(2) erase_bders lexer_correct_None lexer_correct_Some lexer_flex option.inject)
       
  2380 
       
  2381 lemma L0aaaa:
       
  2382   assumes "[c] \<in> L (erase a)"
       
  2383   shows "[c] \<in> (erase a) \<rightarrow> flex (erase a) id [c] (mkeps (erase (bders a [c])))"
       
  2384   using assms
       
  2385   using L0aaa by auto
       
  2386     
       
  2387 
       
  2388 lemma L02:
       
  2389   assumes "bnullable (bder c a)"
       
  2390   shows "retrieve (bsimp a) (flex (erase (bsimp a)) id [c] (mkeps (erase (bder c (bsimp a))))) = 
       
  2391          retrieve (bder c (bsimp a)) (mkeps (erase (bder c (bsimp a))))"
       
  2392   using assms
       
  2393   apply(simp)
       
  2394   using bder_retrieve L0 bmkeps_simp bmkeps_retrieve L0  LA LB
       
  2395   apply(subst bder_retrieve[symmetric])
       
  2396   apply (metis L_bsimp_erase bnullable_correctness der_correctness erase_bder mkeps_nullable nullable_correctness)
       
  2397   apply(simp)
       
  2398   done
       
  2399 
       
  2400 lemma L02_bders:
       
  2401   assumes "bnullable (bders a s)"
       
  2402   shows "retrieve (bsimp a) (flex (erase (bsimp a)) id s (mkeps (erase (bders (bsimp a) s)))) = 
       
  2403          retrieve (bders (bsimp a) s) (mkeps (erase (bders (bsimp a) s)))"
       
  2404   using assms
       
  2405   by (metis LA L_bsimp_erase bnullable_correctness ders_correctness erase_bders mkeps_nullable nullable_correctness)
       
  2406 
       
  2407 
       
  2408   
       
  2409 
       
  2410 lemma L03:
       
  2411   assumes "bnullable (bder c a)"
       
  2412   shows "retrieve (bder c (bsimp a)) (mkeps (erase (bder c (bsimp a)))) =
       
  2413          bmkeps (bsimp (bder c (bsimp a)))"
       
  2414   using assms
       
  2415   by (metis L0 L_bsimp_erase bmkeps_retrieve bnullable_correctness der_correctness erase_bder nullable_correctness)
       
  2416 
       
  2417 lemma L04:
       
  2418   assumes "bnullable (bder c a)"
       
  2419   shows "retrieve (bder c (bsimp a)) (mkeps (erase (bder c (bsimp a)))) =
       
  2420          retrieve (bsimp (bder c (bsimp a))) (mkeps (erase (bsimp (bder c (bsimp a)))))"     
       
  2421   using assms
       
  2422   by (metis L0 L_bsimp_erase bnullable_correctness der_correctness erase_bder nullable_correctness)
       
  2423     
       
  2424 lemma L05:
       
  2425   assumes "bnullable (bder c a)"
       
  2426   shows "retrieve (bder c (bsimp a)) (mkeps (erase (bder c (bsimp a)))) =
       
  2427          retrieve (bsimp (bder c (bsimp a))) (mkeps (erase (bsimp (bder c (bsimp a)))))" 
       
  2428   using assms
       
  2429   using L04 by auto 
       
  2430 
       
  2431 lemma L06:
       
  2432   assumes "bnullable (bder c a)"
       
  2433   shows "bmkeps (bder c (bsimp a)) = bmkeps (bsimp (bder c (bsimp a)))"
       
  2434   using assms
       
  2435   by (metis L03 L_bsimp_erase bmkeps_retrieve bnullable_correctness der_correctness erase_bder nullable_correctness) 
       
  2436 
       
  2437 lemma L07:
       
  2438   assumes "s \<in> L (erase r)"
       
  2439   shows "retrieve r (flex (erase r) id s (mkeps (ders s (erase r)))) 
       
  2440             = retrieve (bders r s) (mkeps (erase (bders r s)))"
       
  2441   using assms
       
  2442   using LB LC lexer_correct_Some by auto
       
  2443 
       
  2444 lemma L06_2:
       
  2445   assumes "bnullable (bders a [c,d])"
       
  2446   shows "bmkeps (bders (bsimp a) [c,d]) = bmkeps (bsimp (bders (bsimp a) [c,d]))"
       
  2447   using assms
       
  2448   apply(simp)
       
  2449   by (metis L_bsimp_erase bmkeps_simp bnullable_correctness der_correctness erase_bder nullable_correctness)
       
  2450   
       
  2451 lemma L06_bders:
       
  2452   assumes "bnullable (bders a s)"
       
  2453   shows "bmkeps (bders (bsimp a) s) = bmkeps (bsimp (bders (bsimp a) s))"
       
  2454   using assms
       
  2455   by (metis L_bsimp_erase bmkeps_simp bnullable_correctness ders_correctness erase_bders nullable_correctness)
       
  2456 
       
  2457 lemma LLLL:
       
  2458   shows "L (erase a) =  L (erase (bsimp a))"
       
  2459   and "L (erase a) = {flat v | v. \<Turnstile> v: (erase a)}"
       
  2460   and "L (erase a) = {flat v | v. \<Turnstile> v: (erase (bsimp a))}"
       
  2461   using L_bsimp_erase apply(blast)
       
  2462   apply (simp add: L_flat_Prf)
       
  2463   using L_bsimp_erase L_flat_Prf apply(auto)[1]
       
  2464   done  
       
  2465     
       
  2466 
       
  2467 
       
  2468 lemma L07XX:
       
  2469   assumes "s \<in> L (erase a)"
       
  2470   shows "s \<in> erase a \<rightarrow> flex (erase a) id s (mkeps (ders s (erase a)))"
       
  2471   using assms
       
  2472   by (meson lexer_correct_None lexer_correctness(1) lexer_flex)
       
  2473 
       
  2474 lemma LX0:
       
  2475   assumes "s \<in> L r"
       
  2476   shows "decode (bmkeps (bders (intern r) s)) r = Some(flex r id s (mkeps (ders s r)))"
       
  2477   by (metis assms blexer_correctness blexer_def lexer_correct_None lexer_flex)
       
  2478 
       
  2479 lemma L1:
       
  2480   assumes "s \<in> r \<rightarrow> v" 
       
  2481   shows "decode (bmkeps (bders (intern r) s)) r = Some v"
       
  2482   using assms
       
  2483   by (metis blexer_correctness blexer_def lexer_correctness(1) option.distinct(1))
       
  2484 
       
  2485 lemma L2:
       
  2486   assumes "s \<in> (der c r) \<rightarrow> v" 
       
  2487   shows "decode (bmkeps (bders (intern r) (c # s))) r = Some (injval r c v)"
       
  2488   using assms
       
  2489   apply(subst bmkeps_retrieve)
       
  2490   using Posix1(1) lexer_correct_None lexer_flex apply fastforce
       
  2491   using MAIN_decode
       
  2492   apply(subst MAIN_decode[symmetric])
       
  2493    apply(simp)
       
  2494    apply (meson Posix1(1) lexer_correct_None lexer_flex mkeps_nullable)
       
  2495   apply(simp)
       
  2496   apply(subgoal_tac "v = flex (der c r) id s (mkeps (ders s (der c r)))")
       
  2497    prefer 2
       
  2498    apply (metis Posix_determ lexer_correctness(1) lexer_flex option.distinct(1))
       
  2499   apply(simp)
       
  2500   apply(subgoal_tac "injval r c (flex (der c r) id s (mkeps (ders s (der c r)))) =
       
  2501     (flex (der c r) ((\<lambda>v. injval r c v) o id) s (mkeps (ders s (der c r))))")
       
  2502    apply(simp)
       
  2503   using flex_fun_apply by blast
       
  2504   
       
  2505 lemma L3:
       
  2506   assumes "s2 \<in> (ders s1 r) \<rightarrow> v" 
       
  2507   shows "decode (bmkeps (bders (intern r) (s1 @ s2))) r = Some (flex r id s1 v)"
       
  2508   using assms
       
  2509   apply(induct s1 arbitrary: r s2 v rule: rev_induct)
       
  2510    apply(simp)
       
  2511   using L1 apply blast
       
  2512   apply(simp add: ders_append)
       
  2513   apply(drule_tac x="r" in meta_spec)
       
  2514   apply(drule_tac x="x # s2" in meta_spec)
       
  2515   apply(drule_tac x="injval (ders xs r) x v" in meta_spec)
       
  2516   apply(drule meta_mp)
       
  2517    defer
       
  2518    apply(simp)
       
  2519    apply(simp add:  flex_append)
       
  2520   by (simp add: Posix_injval)
       
  2521 
       
  2522 
       
  2523 
       
  2524 lemma bders_snoc:
       
  2525   "bder c (bders a s) = bders a (s @ [c])"
       
  2526   apply(simp add: bders_append)
       
  2527   done
       
  2528 
       
  2529 
       
  2530 lemma QQ1:
       
  2531   shows "bsimp (bders (bsimp a) []) = bders_simp (bsimp a) []"
       
  2532   apply(simp)
       
  2533   apply(simp add: bsimp_idem)
       
  2534   done
       
  2535 
       
  2536 lemma QQ2:
       
  2537   shows "bsimp (bders (bsimp a) [c]) = bders_simp (bsimp a) [c]"
       
  2538   apply(simp)
       
  2539   done
       
  2540 
       
  2541 lemma XXX2a_long:
       
  2542   assumes "good a"
       
  2543   shows "bsimp (bder c (bsimp a)) = bsimp (bder c a)"
       
  2544   using  assms
       
  2545   apply(induct a arbitrary: c taking: asize rule: measure_induct)
       
  2546   apply(case_tac x)
       
  2547        apply(simp)
       
  2548       apply(simp)
       
  2549      apply(simp)
       
  2550   prefer 3
       
  2551     apply(simp)
       
  2552    apply(simp)
       
  2553    apply(auto)[1]
       
  2554 apply(case_tac "x42 = AZERO")
       
  2555      apply(simp)
       
  2556    apply(case_tac "x43 = AZERO")
       
  2557      apply(simp)
       
  2558   using test2 apply force  
       
  2559   apply(case_tac "\<exists>bs. x42 = AONE bs")
       
  2560      apply(clarify)
       
  2561      apply(simp)
       
  2562     apply(subst bsimp_ASEQ1)
       
  2563        apply(simp)
       
  2564   using b3 apply force
       
  2565   using bsimp_ASEQ0 test2 apply force
       
  2566   thm good_SEQ test2
       
  2567      apply (simp add: good_SEQ test2)
       
  2568     apply (simp add: good_SEQ test2)
       
  2569   apply(case_tac "x42 = AZERO")
       
  2570      apply(simp)
       
  2571    apply(case_tac "x43 = AZERO")
       
  2572     apply(simp)
       
  2573   apply (simp add: bsimp_ASEQ0)
       
  2574   apply(case_tac "\<exists>bs. x42 = AONE bs")
       
  2575      apply(clarify)
       
  2576      apply(simp)
       
  2577     apply(subst bsimp_ASEQ1)
       
  2578       apply(simp)
       
  2579   using bsimp_ASEQ0 test2 apply force
       
  2580      apply (simp add: good_SEQ test2)
       
  2581     apply (simp add: good_SEQ test2)
       
  2582   apply (simp add: good_SEQ test2)
       
  2583   (* AALTs case *)
       
  2584   apply(simp)
       
  2585   using test2 by fastforce
       
  2586 
       
  2587 
       
  2588 lemma bder_bsimp_AALTs:
       
  2589   shows "bder c (bsimp_AALTs bs rs) = bsimp_AALTs bs (map (bder c) rs)"
       
  2590   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  2591     apply(simp)
       
  2592    apply(simp)
       
  2593    apply (simp add: bder_fuse)
       
  2594   apply(simp)
       
  2595   done
       
  2596 
       
  2597 lemma flts_nothing:
       
  2598   assumes "\<forall>r \<in> set rs. r \<noteq> AZERO" "\<forall>r \<in> set rs. nonalt r"
       
  2599   shows "flts rs = rs"
       
  2600   using assms
       
  2601   apply(induct rs rule: flts.induct)
       
  2602         apply(auto)
       
  2603   done
       
  2604 
       
  2605 lemma flts_flts:
       
  2606   assumes "\<forall>r \<in> set rs. good r"
       
  2607   shows "flts (flts rs) = flts rs"
       
  2608   using assms
       
  2609   apply(induct rs taking: "\<lambda>rs. sum_list  (map asize rs)" rule: measure_induct)
       
  2610   apply(case_tac x)
       
  2611    apply(simp)
       
  2612   apply(simp)
       
  2613   apply(case_tac a)
       
  2614        apply(simp_all  add: bder_fuse flts_append)
       
  2615   apply(subgoal_tac "\<forall>r \<in> set x52. r \<noteq> AZERO")
       
  2616    prefer 2
       
  2617   apply (metis Nil_is_append_conv bsimp_AALTs.elims good.simps(1) good.simps(5) good0 list.distinct(1) n0 nn1b split_list_last test2)
       
  2618   apply(subgoal_tac "\<forall>r \<in> set x52. nonalt r")
       
  2619    prefer 2
       
  2620    apply (metis n0 nn1b test2)
       
  2621   by (metis flts_fuse flts_nothing)
       
  2622 
       
  2623 
       
  2624 lemma iii:
       
  2625   assumes "bsimp_AALTs bs rs \<noteq> AZERO"
       
  2626   shows "rs \<noteq> []"
       
  2627   using assms
       
  2628   apply(induct bs  rs rule: bsimp_AALTs.induct)
       
  2629     apply(auto)
       
  2630   done
       
  2631 
       
  2632 lemma CT1_SEQ:
       
  2633   shows "bsimp (ASEQ bs a1 a2) = bsimp (ASEQ bs (bsimp a1) (bsimp a2))"
       
  2634   apply(simp add: bsimp_idem)
       
  2635   done
       
  2636 
       
  2637 lemma CT1:
       
  2638   shows "bsimp (AALTs bs as) = bsimp (AALTs bs (map  bsimp as))"
       
  2639   apply(induct as arbitrary: bs)
       
  2640    apply(simp)
       
  2641   apply(simp)
       
  2642   by (simp add: bsimp_idem comp_def)
       
  2643   
       
  2644 lemma CT1a:
       
  2645   shows "bsimp (AALT bs a1 a2) = bsimp(AALT bs (bsimp a1) (bsimp a2))"
       
  2646   by (metis CT1 list.simps(8) list.simps(9))
       
  2647 
       
  2648 lemma WWW2:
       
  2649   shows "bsimp (bsimp_AALTs bs1 (flts (map bsimp as1))) =
       
  2650          bsimp_AALTs bs1 (flts (map bsimp as1))"
       
  2651   by (metis bsimp.simps(2) bsimp_idem)
       
  2652 
       
  2653 lemma CT1b:
       
  2654   shows "bsimp (bsimp_AALTs bs as) = bsimp (bsimp_AALTs bs (map bsimp as))"
       
  2655   apply(induct bs as rule: bsimp_AALTs.induct)
       
  2656     apply(auto simp add: bsimp_idem)
       
  2657   apply (simp add: bsimp_fuse bsimp_idem)
       
  2658   by (metis bsimp_idem comp_apply)
       
  2659   
       
  2660   
       
  2661 
       
  2662 
       
  2663 (* CT *)
       
  2664 
       
  2665 lemma CTa:
       
  2666   assumes "\<forall>r \<in> set as. nonalt r \<and> r \<noteq> AZERO"
       
  2667   shows  "flts as = as"
       
  2668   using assms
       
  2669   apply(induct as)
       
  2670    apply(simp)
       
  2671   apply(case_tac as)
       
  2672    apply(simp)
       
  2673   apply (simp add: k0b)
       
  2674   using flts_nothing by auto
       
  2675 
       
  2676 lemma CT0:
       
  2677   assumes "\<forall>r \<in> set as1. nonalt r \<and> r \<noteq> AZERO" 
       
  2678   shows "flts [bsimp_AALTs bs1 as1] =  flts (map (fuse bs1) as1)"
       
  2679   using assms CTa
       
  2680   apply(induct as1 arbitrary: bs1)
       
  2681     apply(simp)
       
  2682    apply(simp)
       
  2683   apply(case_tac as1)
       
  2684    apply(simp)
       
  2685   apply(simp)
       
  2686 proof -
       
  2687 fix a :: arexp and as1a :: "arexp list" and bs1a :: "bit list" and aa :: arexp and list :: "arexp list"
       
  2688   assume a1: "nonalt a \<and> a \<noteq> AZERO \<and> nonalt aa \<and> aa \<noteq> AZERO \<and> (\<forall>r\<in>set list. nonalt r \<and> r \<noteq> AZERO)"
       
  2689   assume a2: "\<And>as. \<forall>r\<in>set as. nonalt r \<and> r \<noteq> AZERO \<Longrightarrow> flts as = as"
       
  2690   assume a3: "as1a = aa # list"
       
  2691   have "flts [a] = [a]"
       
  2692 using a1 k0b by blast
       
  2693 then show "fuse bs1a a # fuse bs1a aa # map (fuse bs1a) list = flts (fuse bs1a a # fuse bs1a aa # map (fuse bs1a) list)"
       
  2694   using a3 a2 a1 by (metis (no_types) append.left_neutral append_Cons flts_fuse k00 k0b list.simps(9))
       
  2695 qed
       
  2696   
       
  2697   
       
  2698 lemma CT01:
       
  2699   assumes "\<forall>r \<in> set as1. nonalt r \<and> r \<noteq> AZERO" "\<forall>r \<in> set as2. nonalt r \<and> r \<noteq> AZERO" 
       
  2700   shows "flts [bsimp_AALTs bs1 as1, bsimp_AALTs bs2 as2] =  flts ((map (fuse bs1) as1) @ (map (fuse bs2) as2))"
       
  2701   using assms CT0
       
  2702   by (metis k0 k00)
       
  2703   
       
  2704 
       
  2705 
       
  2706 lemma CT_exp:
       
  2707   assumes "\<forall>a \<in> set as. bsimp (bder c (bsimp a)) = bsimp (bder c a)"
       
  2708   shows "map bsimp (map (bder c) as) = map bsimp (map (bder c) (map bsimp as))"
       
  2709   using assms
       
  2710   apply(induct as)
       
  2711    apply(auto)
       
  2712   done
       
  2713 
       
  2714 lemma asize_set:
       
  2715   assumes "a \<in> set as"
       
  2716   shows "asize a < Suc (sum_list (map asize as))"
       
  2717   using assms
       
  2718   apply(induct as arbitrary: a)
       
  2719    apply(auto)
       
  2720   using le_add2 le_less_trans not_less_eq by blast
       
  2721 
       
  2722 lemma L_erase_bder_simp:
       
  2723   shows "L (erase (bsimp (bder a r))) = L (der a (erase (bsimp r)))"
       
  2724   using L_bsimp_erase der_correctness by auto
       
  2725 
       
  2726 lemma PPP0: 
       
  2727   assumes "s \<in> r \<rightarrow> v"
       
  2728   shows "(bders (intern r) s) >> code v"
       
  2729   using assms
       
  2730   by (smt L07 L1 LX0 Posix1(1) Posix_Prf contains6 erase_bders erase_intern lexer_correct_None lexer_flex mkeps_nullable option.inject retrieve_code)
       
  2731 
       
  2732 thm L07 L1 LX0 Posix1(1) Posix_Prf contains6 erase_bders erase_intern lexer_correct_None lexer_flex mkeps_nullable option.inject retrieve_code
       
  2733 
       
  2734 
       
  2735 lemma PPP0_isar: 
       
  2736   assumes "s \<in> r \<rightarrow> v"
       
  2737   shows "(bders (intern r) s) >> code v"
       
  2738 proof -
       
  2739   from assms have a1: "\<Turnstile> v : r" using Posix_Prf by simp
       
  2740   
       
  2741   from assms have "s \<in> L r" using Posix1(1) by auto
       
  2742   then have "[] \<in> L (ders s r)" by (simp add: ders_correctness Ders_def)
       
  2743   then have a2: "\<Turnstile> mkeps (ders s r) : ders s r"
       
  2744     by (simp add: mkeps_nullable nullable_correctness) 
       
  2745 
       
  2746   have "retrieve (bders (intern r) s) (mkeps (ders s r)) =  
       
  2747         retrieve (intern r) (flex r id s (mkeps (ders s r)))" using a2 LA LB bder_retrieve  by simp
       
  2748   also have "... = retrieve (intern r) v"
       
  2749     using LB assms by auto 
       
  2750   also have "... = code v" using a1 by (simp add: retrieve_code) 
       
  2751   finally have "retrieve (bders (intern r) s) (mkeps (ders s r)) = code v" by simp
       
  2752   moreover
       
  2753   have "\<Turnstile> mkeps (ders s r) : erase (bders (intern r) s)" using a2 by simp 
       
  2754   then have "bders (intern r) s >> retrieve (bders (intern r) s) (mkeps (ders s r))"
       
  2755     by (rule contains6)  
       
  2756   ultimately
       
  2757   show "(bders (intern r) s) >> code v" by simp
       
  2758 qed
       
  2759 
       
  2760 lemma PPP0b: 
       
  2761   assumes "s \<in> r \<rightarrow> v"
       
  2762   shows "(intern r) >> code v"
       
  2763   using assms
       
  2764   using Posix_Prf contains2 by auto
       
  2765   
       
  2766 lemma PPP0_eq:
       
  2767   assumes "s \<in> r \<rightarrow> v"
       
  2768   shows "(intern r >> code v) = (bders (intern r) s >> code v)"
       
  2769   using assms
       
  2770   using PPP0_isar PPP0b by blast
       
  2771 
       
  2772 lemma f_cont1:
       
  2773   assumes "fuse bs1 a >> bs"
       
  2774   shows "\<exists>bs2. bs = bs1 @ bs2"
       
  2775   using assms
       
  2776   apply(induct a arbitrary: bs1 bs)
       
  2777        apply(auto elim: contains.cases)
       
  2778   done
       
  2779 
       
  2780 
       
  2781 lemma f_cont2:
       
  2782   assumes "bsimp_AALTs bs1 as >> bs"
       
  2783   shows "\<exists>bs2. bs = bs1 @ bs2"
       
  2784   using assms
       
  2785   apply(induct bs1 as arbitrary: bs rule: bsimp_AALTs.induct)
       
  2786     apply(auto elim: contains.cases f_cont1)
       
  2787   done
       
  2788 
       
  2789 lemma contains_SEQ1:
       
  2790   assumes "bsimp_ASEQ bs r1 r2 >> bsX"
       
  2791   shows "\<exists>bs1 bs2. r1 >> bs1 \<and> r2 >> bs2 \<and> bsX = bs @ bs1 @ bs2"
       
  2792   using assms
       
  2793   apply(auto)
       
  2794   apply(case_tac "r1 = AZERO")
       
  2795    apply(auto)
       
  2796   using contains.simps apply blast
       
  2797   apply(case_tac "r2 = AZERO")
       
  2798    apply(auto)
       
  2799    apply(simp add: bsimp_ASEQ0)
       
  2800   using contains.simps apply blast
       
  2801   apply(case_tac "\<exists>bsX. r1 = AONE bsX")
       
  2802    apply(auto)
       
  2803    apply(simp add: bsimp_ASEQ2)
       
  2804    apply (metis append_assoc contains.intros(1) contains49 f_cont1)
       
  2805   apply(simp add: bsimp_ASEQ1)
       
  2806   apply(erule contains.cases)
       
  2807         apply(auto)
       
  2808   done
       
  2809 
       
  2810 lemma contains59:
       
  2811   assumes "AALTs bs rs >> bs2"
       
  2812   shows "\<exists>r \<in> set rs. (fuse bs r) >> bs2"
       
  2813  using assms
       
  2814   apply(induct rs arbitrary: bs bs2)
       
  2815   apply(auto)
       
  2816    apply(erule contains.cases)
       
  2817         apply(auto)
       
  2818   apply(erule contains.cases)
       
  2819        apply(auto)
       
  2820   using contains0 by blast
       
  2821 
       
  2822 lemma contains60:
       
  2823   assumes "\<exists>r \<in> set rs. fuse bs r >> bs2"
       
  2824   shows "AALTs bs rs >> bs2"
       
  2825   using assms
       
  2826   apply(induct rs arbitrary: bs bs2)
       
  2827    apply(auto)
       
  2828   apply (metis contains3b contains49 f_cont1)
       
  2829   using contains.intros(5) f_cont1 by blast
       
  2830   
       
  2831   
       
  2832 
       
  2833 lemma contains61:
       
  2834   assumes "bsimp_AALTs bs rs >> bs2"
       
  2835   shows "\<exists>r \<in> set rs. (fuse bs r) >> bs2"
       
  2836   using assms
       
  2837   apply(induct arbitrary: bs2 rule: bsimp_AALTs.induct)
       
  2838     apply(auto)
       
  2839   using contains.simps apply blast
       
  2840   using contains59 by fastforce
       
  2841 
       
  2842 lemma contains61b:
       
  2843   assumes "bsimp_AALTs bs rs >> bs2"
       
  2844   shows "\<exists>r \<in> set (flts rs). (fuse bs r) >> bs2"
       
  2845   using assms
       
  2846   apply(induct bs rs arbitrary: bs2 rule: bsimp_AALTs.induct)
       
  2847     apply(auto)
       
  2848   using contains.simps apply blast
       
  2849   using contains51d contains61 f_cont1 apply blast
       
  2850   by (metis bsimp_AALTs.simps(3) contains52 contains61 f_cont2)
       
  2851   
       
  2852   
       
  2853 
       
  2854 lemma contains61a:
       
  2855   assumes "\<exists>r \<in> set rs. (fuse bs r) >> bs2"
       
  2856   shows "bsimp_AALTs bs rs >> bs2" 
       
  2857   using assms
       
  2858   apply(induct rs arbitrary: bs2 bs)
       
  2859    apply(auto)
       
  2860    apply (metis bsimp_AALTs.elims contains60 list.distinct(1) list.inject list.set_intros(1))
       
  2861   by (metis append_Cons append_Nil contains50 f_cont2)
       
  2862 
       
  2863 lemma contains62:
       
  2864   assumes "bsimp_AALTs bs (rs1 @ rs2) >> bs2"
       
  2865   shows "bsimp_AALTs bs rs1 >> bs2 \<or> bsimp_AALTs bs rs2 >> bs2"
       
  2866   using assms
       
  2867   apply -
       
  2868   apply(drule contains61)
       
  2869   apply(auto)
       
  2870    apply(case_tac rs1)
       
  2871     apply(auto)
       
  2872   apply(case_tac list)
       
  2873      apply(auto)
       
  2874   apply (simp add: contains60)
       
  2875    apply(case_tac list)
       
  2876     apply(auto)
       
  2877   apply (simp add: contains60)
       
  2878   apply (meson contains60 list.set_intros(2))
       
  2879    apply(case_tac rs2)
       
  2880     apply(auto)
       
  2881   apply(case_tac list)
       
  2882      apply(auto)
       
  2883   apply (simp add: contains60)
       
  2884    apply(case_tac list)
       
  2885     apply(auto)
       
  2886   apply (simp add: contains60)
       
  2887   apply (meson contains60 list.set_intros(2))
       
  2888   done
       
  2889 
       
  2890 lemma contains63:
       
  2891   assumes "AALTs bs (map (fuse bs1) rs) >> bs3"
       
  2892   shows "AALTs (bs @ bs1) rs >> bs3"
       
  2893   using assms
       
  2894   apply(induct rs arbitrary: bs bs1 bs3)
       
  2895    apply(auto elim: contains.cases)
       
  2896     apply(erule contains.cases)
       
  2897         apply(auto)
       
  2898   apply (simp add: contains0 contains60 fuse_append)
       
  2899   by (metis contains.intros(5) contains59 f_cont1)
       
  2900     
       
  2901 lemma contains64:
       
  2902   assumes "bsimp_AALTs bs (flts rs1 @ flts rs2) >> bs2" "\<forall>r \<in> set rs2. \<not> fuse bs r >> bs2"
       
  2903   shows "bsimp_AALTs bs (flts rs1) >> bs2"
       
  2904   using assms
       
  2905   apply(induct rs2 arbitrary: rs1 bs bs2)
       
  2906    apply(auto)
       
  2907   apply(drule_tac x="rs1" in meta_spec)
       
  2908     apply(drule_tac x="bs" in meta_spec)
       
  2909   apply(drule_tac x="bs2" in meta_spec)
       
  2910   apply(drule meta_mp)
       
  2911    apply(drule contains61)
       
  2912    apply(auto)
       
  2913   using contains51b contains61a f_cont1 apply blast
       
  2914   apply(subst (asm) k0)
       
  2915   apply(auto)
       
  2916    prefer 2
       
  2917   using contains50 contains61a f_cont1 apply blast
       
  2918   apply(case_tac a)
       
  2919        apply(auto)
       
  2920   by (metis contains60 fuse_append)
       
  2921   
       
  2922   
       
  2923 
       
  2924 lemma contains65:
       
  2925   assumes "bsimp_AALTs bs (flts rs) >> bs2"
       
  2926   shows "\<exists>r \<in> set rs. (fuse bs r) >> bs2"
       
  2927   using assms
       
  2928   apply(induct rs arbitrary: bs bs2 taking: "\<lambda>rs. sum_list (map asize rs)" rule: measure_induct)
       
  2929   apply(case_tac x)
       
  2930         apply(auto elim: contains.cases)
       
  2931   apply(case_tac list)
       
  2932    apply(auto elim: contains.cases)
       
  2933    apply(case_tac a)
       
  2934         apply(auto elim: contains.cases)
       
  2935    apply(drule contains61)
       
  2936    apply(auto)
       
  2937    apply (metis contains60 fuse_append)
       
  2938   apply(case_tac lista)
       
  2939    apply(auto elim: contains.cases)
       
  2940    apply(subst (asm) k0)
       
  2941    apply(drule contains62)
       
  2942    apply(auto)
       
  2943    apply(case_tac a)
       
  2944          apply(auto elim: contains.cases)
       
  2945    apply(case_tac x52)
       
  2946    apply(auto elim: contains.cases)
       
  2947   apply(case_tac list)
       
  2948    apply(auto elim: contains.cases)
       
  2949   apply (simp add: contains60 fuse_append)
       
  2950    apply(erule contains.cases)
       
  2951           apply(auto)
       
  2952      apply (metis append.left_neutral contains0 contains60 fuse.simps(4) in_set_conv_decomp)
       
  2953   apply(erule contains.cases)
       
  2954           apply(auto)
       
  2955      apply (metis contains0 contains60 fuse.simps(4) list.set_intros(1) list.set_intros(2))
       
  2956   apply (simp add: contains.intros(5) contains63)
       
  2957    apply(case_tac aa)
       
  2958         apply(auto)
       
  2959   apply (meson contains60 contains61 contains63)
       
  2960   apply(subst (asm) k0)
       
  2961   apply(drule contains64)
       
  2962    apply(auto)[1]
       
  2963   by (metis append_Nil2 bsimp_AALTs.simps(2) contains50 contains61a contains64 f_cont2 flts.simps(1))
       
  2964 
       
  2965 
       
  2966 lemma contains55a:
       
  2967   assumes "bsimp r >> bs"
       
  2968   shows "r >> bs"
       
  2969   using assms
       
  2970   apply(induct r arbitrary: bs)
       
  2971        apply(auto)
       
  2972    apply(frule contains_SEQ1)
       
  2973   apply(auto)
       
  2974    apply (simp add: contains.intros(3))
       
  2975   apply(frule f_cont2)
       
  2976   apply(auto) 
       
  2977   apply(drule contains65)
       
  2978   apply(auto)
       
  2979   using contains0 contains49 contains60 by blast
       
  2980 
       
  2981 
       
  2982 lemma PPP1_eq:
       
  2983   shows "bsimp r >> bs \<longleftrightarrow> r >> bs"
       
  2984   using contains55 contains55a by blast
       
  2985 
       
  2986 lemma retrieve_code_bder:
       
  2987   assumes "\<Turnstile> v : der c r"
       
  2988   shows "code (injval r c v) = retrieve (bder c (intern r)) v"
       
  2989   using assms
       
  2990   by (simp add: Prf_injval bder_retrieve retrieve_code)
       
  2991 
       
  2992 lemma Etrans:
       
  2993   assumes "a >> s" "s = t" 
       
  2994   shows "a >> t"
       
  2995   using assms by simp
       
  2996 
       
  2997 
       
  2998 
       
  2999 lemma retrieve_code_bders:
       
  3000   assumes "\<Turnstile> v : ders s r"
       
  3001   shows "code (flex r id s v) = retrieve (bders (intern r) s) v"
       
  3002   using assms
       
  3003   apply(induct s arbitrary: v r rule: rev_induct)
       
  3004    apply(auto simp add: ders_append flex_append bders_append)
       
  3005   apply (simp add: retrieve_code)
       
  3006   apply(frule Prf_injval)
       
  3007   apply(drule_tac meta_spec)+
       
  3008   apply(drule meta_mp)
       
  3009    apply(assumption)
       
  3010   apply(simp)
       
  3011   apply(subst bder_retrieve)
       
  3012    apply(simp)
       
  3013   apply(simp)
       
  3014   done
       
  3015 
       
  3016 thm LA LB
       
  3017 
       
  3018 lemma contains70:
       
  3019  assumes "s \<in> L(r)"
       
  3020  shows "bders (intern r) s >> code (flex r id s (mkeps (ders s r)))"
       
  3021   apply(subst PPP0_eq[symmetric])
       
  3022    apply (meson assms lexer_correct_None lexer_correctness(1) lexer_flex)
       
  3023   by (metis L07XX PPP0b assms erase_intern)
       
  3024 
       
  3025 
       
  3026 
       
  3027 
       
  3028 definition PV where
       
  3029   "PV r s v = flex r id s v"
       
  3030 
       
  3031 definition PX where
       
  3032   "PX r s = PV r s (mkeps (ders s r))"
       
  3033 
       
  3034 lemma PV_id[simp]:
       
  3035   shows "PV r [] v = v"
       
  3036   by (simp add: PV_def)
       
  3037 
       
  3038 lemma PX_id[simp]:
       
  3039   shows "PX r [] = mkeps r"
       
  3040   by (simp add: PX_def)
       
  3041 
       
  3042 lemma PV_cons:
       
  3043   shows "PV r (c # s) v = injval r c (PV (der c r) s v)"
       
  3044   apply(simp add: PV_def flex_fun_apply)
       
  3045   done
       
  3046 
       
  3047 lemma PX_cons:
       
  3048   shows "PX r (c # s) = injval r c (PX (der c r) s)"
       
  3049   apply(simp add: PX_def PV_cons)
       
  3050   done
       
  3051 
       
  3052 lemma PV_append:
       
  3053   shows "PV r (s1 @ s2) v = PV r s1 (PV (ders s1 r) s2 v)"
       
  3054   apply(simp add: PV_def flex_append)
       
  3055   by (simp add: flex_fun_apply2)
       
  3056   
       
  3057 lemma PX_append:
       
  3058   shows "PX r (s1 @ s2) = PV r s1 (PX (ders s1 r) s2)"
       
  3059   by (simp add: PV_append PX_def ders_append)
       
  3060 
       
  3061 lemma code_PV0: 
       
  3062   shows "PV r (c # s) v = injval r c (PV (der c r) s v)"
       
  3063   unfolding PX_def PV_def
       
  3064   apply(simp)
       
  3065   by (simp add: flex_injval)
       
  3066 
       
  3067 lemma code_PX0: 
       
  3068   shows "PX r (c # s) = injval r c (PX (der c r) s)"
       
  3069   unfolding PX_def
       
  3070   apply(simp add: code_PV0)
       
  3071   done  
       
  3072 
       
  3073 lemma Prf_PV:
       
  3074   assumes "\<Turnstile> v : ders s r"
       
  3075   shows "\<Turnstile> PV r s v : r"
       
  3076   using assms unfolding PX_def PV_def
       
  3077   apply(induct s arbitrary: v r)
       
  3078    apply(simp)
       
  3079   apply(simp)
       
  3080   by (simp add: Prf_injval flex_injval)
       
  3081   
       
  3082 
       
  3083 lemma Prf_PX:
       
  3084   assumes "s \<in> L r"
       
  3085   shows "\<Turnstile> PX r s : r"
       
  3086   using assms unfolding PX_def PV_def
       
  3087   using L1 LX0 Posix_Prf lexer_correct_Some by fastforce
       
  3088 
       
  3089 lemma PV1: 
       
  3090   assumes "\<Turnstile> v : ders s r"
       
  3091   shows "(intern r) >> code (PV r s v)"
       
  3092   using assms
       
  3093   by (simp add: Prf_PV contains2)
       
  3094 
       
  3095 lemma PX1: 
       
  3096   assumes "s \<in> L r"
       
  3097   shows "(intern r) >> code (PX r s)"
       
  3098   using assms
       
  3099   by (simp add: Prf_PX contains2)
       
  3100 
       
  3101 lemma PX2: 
       
  3102   assumes "s \<in> L (der c r)"
       
  3103   shows "bder c (intern r) >> code (injval r c (PX (der c r) s))"
       
  3104   using assms
       
  3105   by (simp add: Prf_PX contains6 retrieve_code_bder)
       
  3106 
       
  3107 lemma PX2a: 
       
  3108   assumes "c # s \<in> L r"
       
  3109   shows "bder c (intern r) >> code (injval r c (PX (der c r) s))"
       
  3110   using assms
       
  3111   using PX2 lexer_correct_None by force
       
  3112 
       
  3113 lemma PX2b: 
       
  3114   assumes "c # s \<in> L r"
       
  3115   shows "bder c (intern r) >> code (PX r (c # s))"
       
  3116   using assms unfolding PX_def PV_def
       
  3117   by (metis Der_def L07XX PV_def PX2a PX_def Posix_determ Posix_injval der_correctness erase_intern mem_Collect_eq)
       
  3118     
       
  3119 lemma PV3: 
       
  3120   assumes "\<Turnstile> v : ders s r"
       
  3121   shows "bders (intern r) s >> code (PV r s v)"
       
  3122   using assms
       
  3123   using PX_def PV_def contains70
       
  3124   by (simp add: contains6 retrieve_code_bders)
       
  3125   
       
  3126 lemma PX3: 
       
  3127   assumes "s \<in> L r"
       
  3128   shows "bders (intern r) s >> code (PX r s)"
       
  3129   using assms
       
  3130   using PX_def PV_def contains70 by auto
       
  3131 
       
  3132 lemma PV_bders_iff:
       
  3133   assumes "\<Turnstile> v : ders s r"
       
  3134   shows "bders (intern r) s >> code (PV r s v) \<longleftrightarrow> (intern r) >> code (PV r s v)"
       
  3135   by (simp add: PV1 PV3 assms)
       
  3136   
       
  3137 lemma PX_bders_iff:
       
  3138   assumes "s \<in> L r"
       
  3139   shows "bders (intern r) s >> code (PX r s) \<longleftrightarrow> (intern r) >> code (PX r s)"
       
  3140   by (simp add: PX1 PX3 assms)
       
  3141 
       
  3142 lemma PX4: 
       
  3143   assumes "(s1 @ s2) \<in> L r"
       
  3144   shows "bders (intern r) (s1 @ s2) >> code (PX r (s1 @ s2))"
       
  3145   using assms
       
  3146   by (simp add: PX3)
       
  3147 
       
  3148 lemma PX_bders_iff2: 
       
  3149   assumes "(s1 @ s2) \<in> L r"
       
  3150   shows "bders (intern r) (s1 @ s2) >> code (PX r (s1 @ s2)) \<longleftrightarrow>
       
  3151          (intern r) >> code (PX r (s1 @ s2))"
       
  3152   by (simp add: PX1 PX3 assms)
       
  3153 
       
  3154 lemma PV_bders_iff3: 
       
  3155   assumes "\<Turnstile> v : ders (s1 @ s2) r"
       
  3156   shows "bders (intern r) (s1 @ s2) >> code (PV r (s1 @ s2) v) \<longleftrightarrow>
       
  3157          bders (intern r) s1 >> code (PV r (s1 @ s2) v)"
       
  3158   by (metis PV3 PV_append Prf_PV assms ders_append)
       
  3159 
       
  3160 
       
  3161 
       
  3162 lemma PX_bders_iff3: 
       
  3163   assumes "(s1 @ s2) \<in> L r"
       
  3164   shows "bders (intern r) (s1 @ s2) >> code (PX r (s1 @ s2)) \<longleftrightarrow>
       
  3165          bders (intern r) s1 >> code (PX r (s1 @ s2))"
       
  3166   by (metis Ders_def L07XX PV_append PV_def PX4 PX_def Posix_Prf assms contains6 ders_append ders_correctness erase_bders erase_intern mem_Collect_eq retrieve_code_bders)
       
  3167 
       
  3168 lemma PV_bder_iff: 
       
  3169   assumes "\<Turnstile> v : ders (s1 @ [c]) r"
       
  3170   shows "bder c (bders (intern r) s1) >> code (PV r (s1 @ [c]) v) \<longleftrightarrow>
       
  3171          bders (intern r) s1 >> code (PV r (s1 @ [c]) v)"
       
  3172   by (simp add: PV_bders_iff3 assms bders_snoc)
       
  3173   
       
  3174 lemma PV_bder_IFF: 
       
  3175   assumes "\<Turnstile> v : ders (s1 @ c # s2) r"
       
  3176   shows "bder c (bders (intern r) s1) >> code (PV r (s1 @ c # s2) v) \<longleftrightarrow>
       
  3177          bders (intern r) s1 >> code (PV r (s1 @ c # s2) v)"
       
  3178   by (metis LA PV3 PV_def Prf_PV assms bders_append code_PV0 contains7 ders.simps(2) erase_bders erase_intern retrieve_code_bders)
       
  3179     
       
  3180 
       
  3181 lemma PX_bder_iff: 
       
  3182   assumes "(s1 @ [c]) \<in> L r"
       
  3183   shows "bder c (bders (intern r) s1) >> code (PX r (s1 @ [c])) \<longleftrightarrow>
       
  3184          bders (intern r) s1 >> code (PX r (s1 @ [c]))"
       
  3185   by (simp add: PX_bders_iff3 assms bders_snoc)
       
  3186 
       
  3187 lemma PV_bder_iff2: 
       
  3188   assumes "\<Turnstile> v : ders (c # s1) r"
       
  3189   shows "bders (bder c (intern r)) s1 >> code (PV r (c # s1) v) \<longleftrightarrow>
       
  3190          bder c (intern r) >> code (PV r (c # s1) v)"
       
  3191   by (metis PV3 Prf_PV assms bders.simps(2) code_PV0 contains7 ders.simps(2) erase_intern retrieve_code)
       
  3192   
       
  3193 
       
  3194 lemma PX_bder_iff2: 
       
  3195   assumes "(c # s1) \<in> L r"
       
  3196   shows "bders (bder c (intern r)) s1 >> code (PX r (c # s1)) \<longleftrightarrow>
       
  3197          bder c (intern r) >> code (PX r (c # s1))"
       
  3198   using PX2b PX3 assms by force
       
  3199 
       
  3200   
       
  3201   
       
  3202   
       
  3203 
       
  3204 
       
  3205 definition EQ where
       
  3206   "EQ a1 a2 \<equiv> (\<forall>bs.  a1 >> bs \<longleftrightarrow> a2 >> bs)"   
       
  3207 
       
  3208 lemma EQ1:
       
  3209   assumes "EQ (intern r1) (intern r2)" 
       
  3210           "bders (intern r1) s >> code (PX r1 s)" 
       
  3211            "s \<in> L r1" "s \<in> L r2"
       
  3212   shows "bders (intern r2) s >> code (PX r1 s)"
       
  3213   using assms unfolding EQ_def
       
  3214   thm PX_bders_iff
       
  3215   apply(subst (asm) PX_bders_iff)
       
  3216    apply(assumption)
       
  3217   apply(subgoal_tac "intern r2 >> code (PX r1 s)")
       
  3218   prefer 2
       
  3219   apply(auto)
       
  3220 
       
  3221 
       
  3222 lemma AA1:
       
  3223   assumes "[c] \<in> L r"
       
  3224   assumes "bder c (intern r) >> code (PX r [c])"
       
  3225   shows "bder c (bsimp (intern r)) >> code (PX r [c])"
       
  3226   using assms
       
  3227   
       
  3228   apply(induct a arbitrary: c bs1 bs2 rs)
       
  3229        apply(auto elim: contains.cases)
       
  3230      apply(case_tac "c = x2a")
       
  3231       apply(simp)
       
  3232       apply(case_tac rs)
       
  3233        apply(auto)
       
  3234   using contains0 apply fastforce
       
  3235   apply(case_tac list)
       
  3236        apply(auto)
       
  3237   
       
  3238   prefer 2
       
  3239   apply(erule contains.cases)
       
  3240   apply(auto)
       
  3241 
       
  3242 
       
  3243 
       
  3244 lemma TEST:
       
  3245   assumes "bder c a >> bs"
       
  3246   shows   "bder c (bsimp a) >> bs"
       
  3247   using assms
       
  3248   apply(induct a arbitrary: c bs)
       
  3249        apply(auto elim: contains.cases)
       
  3250    prefer 2
       
  3251    apply(erule contains.cases)
       
  3252          apply(auto)
       
  3253   
       
  3254   
       
  3255 
       
  3256 
       
  3257 
       
  3258 lemma PX_bder_simp_iff: 
       
  3259   assumes "\<Turnstile> v: ders (s1 @ s2) r"
       
  3260   shows "bders (bsimp (bders (intern r) s1)) s2 >> code (PV r (s1 @ s2) v) \<longleftrightarrow>
       
  3261          bders (intern r) s1 >> code (PV r (s1 @ s2) v)"
       
  3262   using assms 
       
  3263   apply(induct s2 arbitrary: r s1 v)
       
  3264    apply(simp)
       
  3265   apply (simp add: PV3 contains55)
       
  3266   apply(drule_tac x="r" in meta_spec)
       
  3267   apply(drule_tac x="s1 @ [a]" in meta_spec)
       
  3268   apply(drule_tac x="v" in meta_spec)
       
  3269   apply(simp)
       
  3270   apply(simp add: bders_append)
       
  3271   apply(subst (asm) PV_bder_IFF)
       
  3272 
       
  3273 definition EXs where
       
  3274   "EXs a s \<equiv> \<forall>v \<in> \<lbrace>= v : ders s (erase a).   
       
  3275 
       
  3276 lemma
       
  3277   assumes "s \<in> L r"
       
  3278   shows "(bders_simp (intern r) s >> code (PX r s)) \<longleftrightarrow> ((intern r) >> code (PX r s))"
       
  3279   using assms
       
  3280   apply(induct s arbitrary: r rule: rev_induct)
       
  3281    apply(simp)
       
  3282   apply(simp add: bders_simp_append)
       
  3283   apply(simp add: PPP1_eq)
       
  3284   
       
  3285   
       
  3286 find_theorems "retrieve (bders _ _) _"
       
  3287 find_theorems "_ >> retrieve _ _"
       
  3288 find_theorems "bsimp _ >> _"
       
  3289 
       
  3290 
       
  3291 
       
  3292 lemma PX4a: 
       
  3293   assumes "(s1 @ s2) \<in> L r"
       
  3294   shows "bders (intern r) (s1 @ s2) >> code (PV r s1 (PX (ders s1 r) s2))"
       
  3295   using PX4[OF assms]
       
  3296   apply(simp add: PX_append)
       
  3297   done
       
  3298 
       
  3299 lemma PV5: 
       
  3300   assumes "s2 \<in> (ders s1 r) \<rightarrow> v"
       
  3301   shows "bders (intern r) (s1 @ s2) >> code (PV r s1 v)"
       
  3302   by (simp add: PPP0_isar PV_def Posix_flex assms)
       
  3303 
       
  3304 lemma PV6: 
       
  3305   assumes "s2 \<in> (ders s1 r) \<rightarrow> v"
       
  3306   shows "bders (bders (intern r) s1) s2 >> code (PV r s1 v)"
       
  3307   using PV5 assms bders_append by auto
       
  3308 
       
  3309 find_theorems "retrieve (bders _ _) _"
       
  3310 find_theorems "_ >> retrieve _ _"
       
  3311 find_theorems "bder _ _ >> _"
       
  3312 
       
  3313 
       
  3314 
       
  3315 lemma PV6: 
       
  3316   assumes "s @[c] \<in> L r"
       
  3317   shows"bder s1 (bders (intern r) s2) >> code (PX r (c # s))"
       
  3318   apply(subst PX_bders_iff)
       
  3319    apply(rule contains7)
       
  3320    apply(simp)
       
  3321    apply(rule assms)
       
  3322   apply(subst retrieve_code)
       
  3323   
       
  3324     apply(simp add: PV_def)
       
  3325   apply(simp)
       
  3326   apply(drule_tac x="r" in meta_spec)
       
  3327   apply(drule_tac x="s1 @ [a]" in meta_spec)
       
  3328   apply(simp add: bders_append)
       
  3329   apply(subst PV_cons)
       
  3330   apply(drule_tac x="injval r a v" in meta_spec)
       
  3331   apply(drule meta_mp)
       
  3332   
       
  3333 
       
  3334 lemma PV8:
       
  3335   assumes "(s1 @ s2) \<in> L r"
       
  3336   shows "bders (bders_simp (intern r) s1) s2 >> code (PX r (s1 @ s2))" 
       
  3337   using assms
       
  3338   apply(induct s1 arbitrary: r s2 rule: rev_induct)
       
  3339   apply(simp add: PX3)
       
  3340   apply(simp)
       
  3341   apply(simp add: bders_simp_append)
       
  3342   apply(drule_tac x="r" in meta_spec)
       
  3343   apply(drule_tac x="x # s2" in meta_spec)
       
  3344   apply(simp add: bders_simp_append)
       
  3345   apply(rule iffI)
       
  3346    defer
       
  3347   
       
  3348   apply(simp add: PX_append)
       
  3349   apply(simp add: bders_append)
       
  3350 
       
  3351 lemma PV6:
       
  3352   assumes "\<Turnstile> v : ders s r"
       
  3353   shows "bders (intern r) s >> code (PV r s v)"
       
  3354   using assms
       
  3355   by (simp add: PV_def contains6 retrieve_code_bders)
       
  3356   
       
  3357 lemma OO0_PX:
       
  3358   assumes "s \<in> L r"
       
  3359   shows "bders (intern r) s >> code (PX r s)"
       
  3360   using assms
       
  3361   by (simp add: PX3)
       
  3362   
       
  3363 
       
  3364 lemma OO1:
       
  3365   assumes "[c] \<in> r \<rightarrow> v"
       
  3366   shows "bder c (intern r) >> code v"
       
  3367   using assms
       
  3368   using PPP0_isar by force
       
  3369 
       
  3370 lemma OO1a:
       
  3371   assumes "[c] \<in> L r"
       
  3372   shows "bder c (intern r) >> code (PX r [c])"
       
  3373   using assms unfolding PX_def PV_def
       
  3374   using contains70 by fastforce
       
  3375   
       
  3376 lemma OO12:
       
  3377   assumes "[c1, c2] \<in> L r"
       
  3378   shows "bders (intern r) [c1, c2] >> code (PX r [c1, c2])"
       
  3379   using assms
       
  3380   using PX_def PV_def contains70 by presburger
       
  3381 
       
  3382 lemma OO2:
       
  3383   assumes "[c] \<in> L r"
       
  3384   shows "bders_simp (intern r) [c] >> code (PX r [c])"
       
  3385   using assms
       
  3386   using OO1a Posix1(1) contains55 by auto
       
  3387   
       
  3388 
       
  3389 lemma OO22:
       
  3390   assumes "[c1, c2] \<in> L r"
       
  3391   shows "bders_simp (intern r) [c1, c2] >> code (PX r [c1, c2])"
       
  3392   using assms
       
  3393   apply(simp)
       
  3394   apply(rule contains55)
       
  3395   apply(rule Etrans)
       
  3396   thm contains7
       
  3397   apply(rule contains7)
       
  3398 
       
  3399 
       
  3400 
       
  3401 lemma contains70:
       
  3402  assumes "s \<in> L(r)"
       
  3403  shows "bders_simp (intern r) s >> code (flex r id s (mkeps (ders s r)))"
       
  3404   using assms
       
  3405   apply(induct s arbitrary: r rule: rev_induct)
       
  3406    apply(simp)
       
  3407   apply (simp add: contains2 mkeps_nullable nullable_correctness)
       
  3408   apply(simp add: bders_simp_append flex_append)
       
  3409   apply(simp add: PPP1_eq)
       
  3410   apply(rule Etrans)
       
  3411   apply(rule_tac v="flex r id xs (mkeps (ders (xs @ [x]) r))" in contains7)
       
  3412   
       
  3413 
       
  3414 
       
  3415 thm L07XX PPP0b erase_intern
       
  3416 
       
  3417 find_theorems "retrieve (bders _ _) _"
       
  3418 find_theorems "_ >> retrieve _ _"
       
  3419 find_theorems "bder _ _ >> _"
       
  3420 
       
  3421 
       
  3422 proof -
       
  3423   from assms have "\<Turnstile> v : erase (bder c r)" by simp
       
  3424   then have "bder c r >> retrieve (bder c r) v"
       
  3425     by (simp add: contains6)
       
  3426   moreover have "retrieve (bder c r) v = retrieve r (injval (erase r) c v)"
       
  3427     using assms bder_retrieve by blast
       
  3428   ultimately have "bder c r >> code (injval (erase r) c v)"
       
  3429     apply -
       
  3430     apply(subst retrieve_code_bder)
       
  3431     apply(simp add: assms)
       
  3432     oops
       
  3433     
       
  3434 find_theorems "code _ = retrieve _ _"
       
  3435 find_theorems "_ >> retrieve _ _"
       
  3436 find_theorems "bder _ _ >> _"
       
  3437 
       
  3438 lemma 
       
  3439   assumes "s \<in> r \<rightarrow> v" "s = [c1, c2]"
       
  3440   shows "bders_simp (intern r) s >> bs \<longleftrightarrow> bders (intern r) s >> bs"
       
  3441   using assms
       
  3442   apply(simp add: PPP1_eq)
       
  3443   
       
  3444 
       
  3445 
       
  3446 lemma PPP10:
       
  3447   assumes "s \<in> r \<rightarrow> v"
       
  3448   shows "bders_simp (intern r) s >> retrieve (intern r) v \<longleftrightarrow> bders (intern r) s >> retrieve (intern r) v"
       
  3449   using assms
       
  3450   apply(induct s arbitrary: r v rule: rev_induct)
       
  3451    apply(auto)
       
  3452   apply(simp_all add: PPP1_eq bders_append bders_simp_append)
       
  3453 
       
  3454   find_theorems "bder _ _ >> _"
       
  3455 
       
  3456 lemma
       
  3457   shows "bder 
       
  3458 
       
  3459 
       
  3460 find_theorems "bsimp _ >> _"
       
  3461 
       
  3462 fun get where
       
  3463  "get (Some v) = v"
       
  3464 
       
  3465 
       
  3466 lemma decode9:
       
  3467   assumes "decode' bs (STAR r) = (v, bsX)" "bs \<noteq> []"
       
  3468   shows "\<exists>vs. v = Stars vs"
       
  3469   using assms
       
  3470   apply(induct bs\<equiv>"bs" r\<equiv>"STAR r" arbitrary: bs r v bsX rule: decode'.induct)
       
  3471     apply(auto)
       
  3472   apply(case_tac "decode' ds r")
       
  3473   apply(auto)
       
  3474   apply(case_tac "decode' b (STAR r)")
       
  3475   apply(auto)
       
  3476   apply(case_tac aa)
       
  3477    apply(auto)
       
  3478   done  
       
  3479 
       
  3480 lemma decode10_Stars:
       
  3481   assumes "decode' bs (STAR r) = (Stars vs, bs1)" "\<Turnstile> Stars vs : (STAR r)" "vs \<noteq> []"
       
  3482   shows "decode' (bs @ bsX) (STAR r) = (Stars vs, bs1 @ bsX)"
       
  3483   using assms
       
  3484   apply(induct vs arbitrary: bs r bs1 bsX)
       
  3485    apply(auto elim!: Prf_elims)
       
  3486    apply(case_tac vs)
       
  3487     apply(auto)
       
  3488    apply(case_tac bs)
       
  3489     apply(auto)
       
  3490    apply(case_tac aa)
       
  3491     apply(auto)
       
  3492    apply(case_tac "decode' list r")
       
  3493    apply(auto)
       
  3494    apply(case_tac "decode' b (STAR r)")
       
  3495    apply(auto)
       
  3496    apply(case_tac "decode' (list @ bsX) r")
       
  3497   apply(auto)
       
  3498   apply(case_tac "decode' ba (STAR r)")
       
  3499    apply(auto)
       
  3500   apply(case_tac ba)
       
  3501      apply(auto)
       
  3502   oops
       
  3503 
       
  3504 lemma decode10:
       
  3505   assumes "decode' bs r = (v, bs1)" "\<Turnstile> v : r"
       
  3506   shows "decode' (bs @ bsX) r = (v, bs1 @ bsX)"
       
  3507   using assms
       
  3508   apply(induct bs r arbitrary: v bs1 bsX rule: decode'.induct)
       
  3509            apply(auto elim: Prf_elims)[7]
       
  3510      apply(case_tac "decode' ds r1")
       
  3511      apply(auto)[3]
       
  3512      apply(case_tac "decode' (ds @ bsX) r1")
       
  3513      apply(auto)[3]
       
  3514       apply(auto elim: Prf_elims)[4]
       
  3515    apply(case_tac "decode' ds r2")
       
  3516      apply(auto)[1]
       
  3517      apply(case_tac "decode' (ds @ bsX) r2")
       
  3518      apply(auto)[1]
       
  3519      apply(auto elim: Prf_elims)[2]
       
  3520  apply(case_tac "decode' ds r1")
       
  3521      apply(auto)[1]
       
  3522      apply(case_tac "decode' b r2")
       
  3523      apply(auto)[1]
       
  3524      apply(auto elim: Prf_elims)[1]
       
  3525     apply(auto elim: Prf_elims)[1]
       
  3526    apply(auto elim: Prf_elims)[1]
       
  3527   apply(erule Prf_elims)
       
  3528 (* STAR case *)
       
  3529   apply(auto)
       
  3530    apply(case_tac "decode' ds r")
       
  3531      apply(auto)
       
  3532      apply(case_tac "decode' b (STAR r)")
       
  3533   apply(auto)
       
  3534   apply(case_tac aa)
       
  3535        apply(auto)
       
  3536   apply(case_tac "decode' (b @ bsX) (STAR r)")
       
  3537        apply(auto)
       
  3538   oops
       
  3539   
       
  3540 
       
  3541 lemma contains100:
       
  3542   assumes "(intern r) >> bs"
       
  3543   shows "\<exists>v bsV. decode' bs r = (v, bsV) \<and> \<Turnstile> v : r"
       
  3544   using assms
       
  3545   apply(induct r arbitrary: bs)
       
  3546        apply(auto)
       
  3547 apply(erule contains.cases)
       
  3548                 apply(auto)
       
  3549   apply(erule contains.cases)
       
  3550                apply(auto intro: Prf.intros)
       
  3551 apply(erule contains.cases)
       
  3552           apply(auto)
       
  3553     apply(drule_tac x="bs1" in meta_spec)
       
  3554     apply(drule_tac x="bs2" in meta_spec)
       
  3555   apply(auto)[1]
       
  3556     apply(rule_tac x="Seq v va" in exI)
       
  3557     apply(auto)
       
  3558   apply(case_tac "decode' (bs1 @ bs2) r1")
       
  3559     apply(auto)
       
  3560   apply(case_tac "decode' b r2")
       
  3561      apply(auto)
       
  3562   oops
       
  3563 
       
  3564 lemma contains101:
       
  3565   assumes "(intern r) >> code v"
       
  3566   shows "\<Turnstile> v : r"
       
  3567   using assms
       
  3568   apply(induct r arbitrary: v)
       
  3569        apply(auto elim: contains.cases)
       
  3570   apply(erule contains.cases)
       
  3571             apply(auto)
       
  3572       apply(case_tac v)
       
  3573   apply(auto intro: Prf.intros)
       
  3574   apply(erule contains.cases)
       
  3575             apply(auto)
       
  3576       apply(case_tac v)
       
  3577   apply(auto intro: Prf.intros)
       
  3578 
       
  3579 (*
       
  3580   using contains.simps apply blast
       
  3581       apply(erule contains.cases)
       
  3582             apply(auto)
       
  3583   using L1 Posix_ONE Prf.intros(4) apply force
       
  3584    apply(erule contains.cases)
       
  3585            apply(auto)
       
  3586   apply (metis Prf.intros(5) code.simps(2) decode_code get.simps)
       
  3587     apply(erule contains.cases)
       
  3588           apply(auto)
       
  3589     prefer 2
       
  3590   apply(erule contains.cases)
       
  3591           apply(auto)
       
  3592      apply(frule f_cont1)
       
  3593      apply(auto)
       
  3594      apply(case_tac "decode' bs2 r1")
       
  3595      apply(auto)
       
  3596      apply(rule Prf.intros)
       
  3597   apply (metis Cons_eq_append_conv contains49 f_cont1 fst_conv list.inject self_append_conv2)
       
  3598     apply(erule contains.cases)
       
  3599           apply(auto)
       
  3600      apply(frule f_cont1)
       
  3601      apply(auto)
       
  3602      apply(case_tac "decode' bs2 r2")
       
  3603      apply(auto)
       
  3604      apply(rule Prf.intros)
       
  3605   apply (metis (full_types) append_Cons contains49 append_Nil fst_conv)
       
  3606     apply(erule contains.cases)
       
  3607           apply(auto)
       
  3608   apply(case_tac "decode' (bs1 @ bs2) r1")
       
  3609    apply(auto)
       
  3610   apply(case_tac "decode' b r2")
       
  3611    apply(auto)
       
  3612    apply(rule Prf.intros)
       
  3613   
       
  3614     apply (metis fst_conv)
       
  3615    apply(subgoal_tac "b = bs2 @ bsX")
       
  3616     apply(auto)
       
  3617     apply (metis fst_conv)
       
  3618    apply(subgoal_tac "decode' (bs1 @ bs2 @ bsX) r1 = (a, bs2 @ bsX)")
       
  3619     apply simp
       
  3620 *)
       
  3621   
       
  3622   
       
  3623    apply(case_tac ba)
       
  3624   apply(auto)
       
  3625     apply(drule meta_spec)
       
  3626     apply(drule meta_mp)
       
  3627   apply(assumption)
       
  3628     prefer 2
       
  3629   
       
  3630 
       
  3631       apply(case_tac v)
       
  3632   apply(auto)
       
  3633   
       
  3634 
       
  3635 
       
  3636 find_theorems "bder _ _ >> _"
       
  3637 
       
  3638 lemma PPP0_isar: 
       
  3639   assumes "bders r s >> code v" 
       
  3640   shows "bders_simp r s >> code v"
       
  3641   using assms
       
  3642   apply(induct s arbitrary: r v)
       
  3643    apply(simp)
       
  3644   apply(auto)
       
  3645   apply(drule_tac x="bsimp (bder a r)" in meta_spec)
       
  3646   apply(drule_tac x="v" in meta_spec)
       
  3647   apply(drule_tac meta_mp)
       
  3648   
       
  3649    prefer 2
       
  3650    apply(simp)
       
  3651   
       
  3652   using bnullable_correctness nullable_correctness apply fastforce
       
  3653   apply(simp add: bders_append)
       
  3654   
       
  3655   
       
  3656   
       
  3657 
       
  3658 
       
  3659 lemma PPP0_isar: 
       
  3660   assumes "s \<in> r \<rightarrow> v"
       
  3661   shows "(bders (intern r) s) >> code v"
       
  3662 proof -
       
  3663   from assms have a1: "\<Turnstile> v : r" using Posix_Prf by simp
       
  3664   
       
  3665   from assms have "s \<in> L r" using Posix1(1) by auto
       
  3666   then have "[] \<in> L (ders s r)" by (simp add: ders_correctness Ders_def)
       
  3667   then have a2: "\<Turnstile> mkeps (ders s r) : ders s r"
       
  3668     by (simp add: mkeps_nullable nullable_correctness) 
       
  3669 
       
  3670   have "retrieve (bders (intern r) s) (mkeps (ders s r)) =  
       
  3671         retrieve (intern r) (flex r id s (mkeps (ders s r)))" using a2 LA by simp
       
  3672   also have "... = retrieve (intern r) v"
       
  3673     using LB assms by auto 
       
  3674   also have "... = code v" using a1 by (simp add: retrieve_code) 
       
  3675   finally have "retrieve (bders (intern r) s) (mkeps (ders s r)) = code v" by simp
       
  3676   moreover
       
  3677   have "\<Turnstile> mkeps (ders s r) : erase (bders (intern r) s)" using a2 by simp 
       
  3678   then have "bders (intern r) s >> retrieve (bders (intern r) s) (mkeps (ders s r))"
       
  3679     by (rule contains6)  
       
  3680   ultimately
       
  3681   show "(bders (intern r) s) >> code v" by simp
       
  3682 qed
       
  3683 
       
  3684 
       
  3685 
       
  3686 
       
  3687 
       
  3688 
       
  3689 
       
  3690 
       
  3691 
       
  3692 lemma A0:
       
  3693   assumes "r \<in> set (flts rs)"
       
  3694   shows "r \<in> set rs"
       
  3695  using assms
       
  3696   apply(induct rs arbitrary: r rule: flts.induct)
       
  3697        apply(auto)
       
  3698   oops
       
  3699 
       
  3700 lemma A1:
       
  3701   assumes "r \<in> set (flts (map (bder c) (flts rs)))" "\<forall>r \<in> set rs. nonnested r \<and> good r"
       
  3702   shows "r \<in> set (flts (map (bder c) rs))"
       
  3703   using assms
       
  3704   apply(induct rs arbitrary: r c rule: flts.induct)
       
  3705         apply(auto)
       
  3706   apply(subst (asm) map_bder_fuse)
       
  3707   apply(simp add: flts_append)
       
  3708   apply(auto)
       
  3709   apply(auto simp add: comp_def)
       
  3710   apply(subgoal_tac "\<forall>r \<in> set rs1. nonalt r \<and> good r")
       
  3711    prefer 2
       
  3712   apply (metis Nil_is_append_conv good.simps(5) good.simps(6) in_set_conv_decomp neq_Nil_conv)
       
  3713   apply(case_tac rs1)
       
  3714    apply(auto)
       
  3715   apply(subst (asm) k0)
       
  3716   apply(auto)
       
  3717   
       
  3718   oops
       
  3719 
       
  3720 
       
  3721 lemma bsimp_comm2:
       
  3722   assumes "bder c a >> bs" 
       
  3723   shows "bder c (bsimp a) >> bs"
       
  3724   using assms
       
  3725   apply(induct a arbitrary: bs c taking: "asize" rule: measure_induct)
       
  3726   apply(case_tac x)
       
  3727        apply(auto)
       
  3728      prefer 2
       
  3729   apply(erule contains.cases)
       
  3730            apply(auto)
       
  3731   apply(subst bder_bsimp_AALTs)
       
  3732   apply(rule contains61a)
       
  3733     apply(rule bexI)
       
  3734      apply(rule contains0)
       
  3735      apply(assumption)
       
  3736   
       
  3737 
       
  3738 lemma bsimp_comm:
       
  3739   assumes "bder c (bsimp a) >> bs" 
       
  3740   shows "bsimp (bder c a) >> bs"
       
  3741   using assms
       
  3742   apply(induct a arbitrary: bs c taking: "asize" rule: measure_induct)
       
  3743   apply(case_tac x)
       
  3744        apply(auto)
       
  3745      prefer 4
       
  3746   apply(erule contains.cases)
       
  3747            apply(auto)
       
  3748   using contains.intros(3) contains55 apply fastforce
       
  3749     prefer 3
       
  3750     apply(subst (asm) bder_bsimp_AALTs)
       
  3751     apply(drule contains61b)
       
  3752     apply(auto)
       
  3753     apply(rule contains61a)
       
  3754     apply(rule bexI)
       
  3755      apply(assumption)
       
  3756     apply(rule_tac t="set (flts (map (bsimp \<circ> bder c) x52))" 
       
  3757               and  s="set (flts (map (bder c \<circ> bsimp) x52))" in subst)
       
  3758      prefer 2
       
  3759   find_theorems "map (_ \<circ> _) _ = _"
       
  3760   apply(simp add: comp_def)
       
  3761   
       
  3762 
       
  3763   find_theorems "bder _ (bsimp_AALTs _ _) = _"
       
  3764   apply(drule contains_SEQ1)
       
  3765   apply(auto)[1]
       
  3766   apply(rule contains.intros)
       
  3767      prefer 2
       
  3768   apply(assumption)
       
  3769   
       
  3770 
       
  3771    apply(case_tac "bnullable x42")
       
  3772     apply(simp)
       
  3773     prefer 2
       
  3774     apply(simp)
       
  3775     apply(case_tac "bsimp x42 = AZERO")
       
  3776   apply (me tis L_erase_bder_simp bder.simps(1) bsimp.simps(3) bsimp_ASEQ.simps(1) good.simps(1) good1a xxx_bder2)
       
  3777     apply(case_tac "bsimp x43 = AZERO")
       
  3778   apply (simp add: bsimp_ASEQ0)
       
  3779     apply(case_tac "\<exists>bs1. bsimp x42 = AONE bs1")
       
  3780   using b3 apply force
       
  3781     apply(subst bsimp_ASEQ1)
       
  3782         apply(auto)[3]
       
  3783      apply(auto)[1]
       
  3784   using b3 apply blast
       
  3785      apply(case_tac "bsimp (bder c x42) = AZERO")
       
  3786       apply(simp)
       
  3787   using contains.simps apply blast
       
  3788   apply(case_tac "\<exists>bs2. bsimp (bder c x42) = AONE bs2")
       
  3789       apply(auto)[1]
       
  3790       apply(subst (asm) bsimp_ASEQ2)
       
  3791       apply(subgoal_tac "\<exists>bsX. bs = x41 @ bs2 @ bsX")
       
  3792        apply(auto)[1]
       
  3793        apply(rule contains.intros)
       
  3794         apply (simp add: contains.intros(1))
       
  3795        apply (metis append_assoc contains49)
       
  3796   using append_assoc f_cont1 apply blast
       
  3797   apply(subst (asm) bsimp_ASEQ1)
       
  3798         apply(auto)[3]
       
  3799    apply(erule contains.cases)
       
  3800            apply(auto)
       
  3801   using contains.intros(3) less_add_Suc1 apply blast
       
  3802    apply(case_tac "bsimp x42 = AZERO")
       
  3803   using b3 apply force     
       
  3804     apply(case_tac "bsimp x43 = AZERO")
       
  3805   apply (metis LLLL(1) L_erase_bder_simp bder.simps(1) bsimp_AALTs.simps(1) bsimp_ASEQ0 bsimp_fuse flts.simps(1) flts.simps(2) fuse.simps(1) good.simps(1) good1a xxx_bder2)
       
  3806     apply(case_tac "\<exists>bs1. bsimp x42 = AONE bs1")
       
  3807      apply(auto)[1]
       
  3808      apply(subst bsimp_ASEQ2)
       
  3809      apply(drule_tac x="fuse (x41 @ bs1) x43" in spec)
       
  3810      apply(drule mp)
       
  3811   apply (simp add: asize_fuse)
       
  3812   apply(drule_tac x="bs" in spec)
       
  3813      apply(drule_tac x="c" in spec)
       
  3814      apply(drule mp)
       
  3815   prefer 2
       
  3816       apply (simp add: bsimp_fuse)
       
  3817      apply(subst (asm) k0)
       
  3818      apply(subgoal_tac "\<exists>bsX. bs = x41 @ bsX")
       
  3819       prefer 2
       
  3820   using f_cont2 apply blast
       
  3821      apply(clarify)
       
  3822      apply(drule  contains62)
       
  3823      apply(auto)[1]
       
  3824       apply(case_tac "bsimp (bder c x42) = AZERO")
       
  3825   apply (metis append_is_Nil_conv bsimp_ASEQ.simps(1) contains61 flts.simps(1) flts.simps(2) in_set_conv_decomp list.distinct(1))
       
  3826       apply(case_tac "\<exists>bsX. bsimp (bder c x42) = AONE bsX")
       
  3827        apply(clarify)
       
  3828   apply (simp add: L_erase_bder_simp xxx_bder2)
       
  3829   using L_erase_bder_simp xxx_bder2 apply auto[1]
       
  3830      apply(drule contains65)
       
  3831      apply(auto)[1]
       
  3832   apply (simp add: bder_fuse bmkeps_simp bsimp_fuse fuse_append)
       
  3833     apply(subst bsimp_ASEQ1)
       
  3834        apply(auto)[3]
       
  3835     apply(auto)[1]
       
  3836      apply(case_tac "bsimp (bder c x42) = AZERO")
       
  3837       apply(simp add: bsimp_ASEQ0)
       
  3838       apply(drule contains65)
       
  3839       apply(auto)[1]
       
  3840   apply (metis asize_fuse bder_fuse bmkeps_simp bsimp_fuse contains.intros(4) contains.intros(5) contains49 f_cont1 less_add_Suc2)
       
  3841   
       
  3842      apply(frule f_cont1)
       
  3843        apply(auto)
       
  3844      
       
  3845      apply(case_tac "\<exists>bsX. bsimp (bder c x42) = AONE bsX")
       
  3846       apply(auto)[1]
       
  3847       apply(subst (asm) bsimp_ASEQ2)
       
  3848       apply(auto)
       
  3849    apply(drule contains65)
       
  3850       apply(auto)[1]
       
  3851        apply(frule f_cont1)
       
  3852        apply(auto)
       
  3853        apply(rule contains.intros)
       
  3854   apply (metis (no_types, lifting) append_Nil2 append_eq_append_conv2 contains.intros(1) contains.intros(3) contains49 f_cont1 less_add_Suc1 same_append_eq)
       
  3855    apply(frule f_cont1)
       
  3856       apply(auto)
       
  3857       apply(rule contains.intros)
       
  3858       apply(drule contains49)
       
  3859       apply(subst (asm) bsimp_fuse[symmetric])
       
  3860   apply(frule f_cont1)
       
  3861       apply(auto)
       
  3862   apply(subst (3) append_Nil[symmetric])
       
  3863       apply(rule contains.intros)
       
  3864        apply(drule contains49)
       
  3865   
       
  3866        prefer 2
       
  3867   
       
  3868   apply(simp)
       
  3869   find_theorems "fuse _ _ >> _"
       
  3870   
       
  3871 
       
  3872   apply(erule contains.cases)
       
  3873            apply(auto)
       
  3874   
       
  3875   
       
  3876   
       
  3877   
       
  3878   
       
  3879     
       
  3880 
       
  3881 
       
  3882 
       
  3883 thm bder_retrieve
       
  3884 find_theorems "_ >> retrieve _ _"
       
  3885 
       
  3886 lemma TEST:
       
  3887   assumes "\<Turnstile> v : ders s (erase r)"
       
  3888   shows "bders r s >> retrieve r (flex (erase r) id s v)"
       
  3889   using assms
       
  3890   apply(induct s arbitrary: v r rule: rev_induct)
       
  3891    apply(simp)
       
  3892   apply (simp add: contains6)
       
  3893   apply(simp add: bders_append ders_append)
       
  3894   apply(rule Etrans)
       
  3895    apply(rule contains7)  
       
  3896    apply(simp)
       
  3897   by (metis LA bder_retrieve bders_snoc ders_snoc erase_bders)
       
  3898 
       
  3899 
       
  3900 lemma TEST1:
       
  3901   assumes "bder c r >> retrieve r (injval (erase r) c v)"
       
  3902   shows "r >> retrieve r v"
       
  3903   oops
       
  3904 
       
  3905 lemma TEST2:
       
  3906   assumes "bders (intern r) s >> retrieve (intern r) (flex r id s (mkeps (ders s r)))" "s = [c1, c2]"
       
  3907   shows "bders_simp (intern r) s >> retrieve (intern r) (flex r id s (mkeps (ders s r)))"
       
  3908   using assms
       
  3909   apply(simp)
       
  3910   
       
  3911   
       
  3912     apply(induct s arbitrary: r rule: rev_induct)
       
  3913    apply(simp)
       
  3914   apply(simp add: bders_simp_append ders_append flex_append bders_append)
       
  3915   apply(rule contains55)
       
  3916   
       
  3917   apply(drule_tac x="bsimp (bder a r)" in meta_spec)
       
  3918   thm L02_bders
       
  3919   apply(subst L02_bders)
       
  3920   find_theorems "retrieve (bsimp _) _ = _"
       
  3921   apply(drule_tac "" in  Etrans)
       
  3922 
       
  3923 lemma TEST2:
       
  3924   assumes "bders r s >> retrieve r (flex (erase r) id s (mkeps (ders s (erase r))))"
       
  3925   shows "bders_simp r s >> retrieve r (flex (erase r) id s (mkeps (ders s (erase r))))"
       
  3926   using assms
       
  3927   apply(induct s arbitrary: r rule: rev_induct)
       
  3928    apply(simp)
       
  3929   apply(simp add: bders_simp_append ders_append flex_append bders_append)
       
  3930   apply(subgoal_tac "bder x (bders r xs) >> retrieve r (flex (erase r) id xs (injval (ders xs (erase r)) x (mkeps (ders xs (erase r)))))")
       
  3931   find_theorems "bders _ _ >> _"
       
  3932   apply(drule_tac x="bsimp (bder a r)" in meta_spec)
       
  3933   thm L02_bders
       
  3934   apply(subst L02_bders)
       
  3935   find_theorems "retrieve (bsimp _) _ = _"
       
  3936   apply(drule_tac "" in  Etrans)
       
  3937   apply(rule contains55)
       
  3938   apply(rule Etrans)
       
  3939    apply(rule contains7)
       
  3940    apply(subgoal_tac "\<Turnstile> v : der x (erase (bders_simp r xs))")
       
  3941     apply(assumption)
       
  3942    prefer 2
       
  3943   
       
  3944   
       
  3945    apply(simp)
       
  3946   by (m etis LA bder_retrieve bders_snoc ders_snoc erase_bders)
       
  3947   
       
  3948 
       
  3949 
       
  3950 
       
  3951 lemma PPP0A: 
       
  3952   assumes "s \<in> L (r)"
       
  3953   shows "(bders (intern r) s) >> code (flex r id s (mkeps (ders s r)))"
       
  3954   using assms
       
  3955   by (metis L07XX PPP0 erase_intern)
       
  3956   
       
  3957 
       
  3958 
       
  3959 
       
  3960 lemma PPP1: 
       
  3961   assumes "bder c (intern r) >> code v" "\<Turnstile> v : der c r"
       
  3962   shows "(intern r) >> code (injval r c v)"
       
  3963   using assms
       
  3964   by (simp add: Prf_injval contains2)
       
  3965 
       
  3966 
       
  3967 (*
       
  3968 lemma PPP1: 
       
  3969   assumes "bder c r >> code v" "\<Turnstile> v : der c (erase r)"
       
  3970   shows "r >> code (injval (erase r) c v)"
       
  3971   using assms contains7[OF assms(2)] retrieve_code[OF assms(2)]
       
  3972   find_theorems "bder _ _ >> _"
       
  3973   by (simp add: Prf_injval contains2)
       
  3974 *)
       
  3975 
       
  3976 lemma PPP3:
       
  3977   assumes "\<Turnstile> v : ders s (erase a)"
       
  3978   shows "bders a s >> retrieve a (flex (erase a) id s v)"
       
  3979   using LA[OF assms] contains6 erase_bders assms by metis
       
  3980 
       
  3981 
       
  3982 find_theorems "bder _ _ >> _"
       
  3983 
       
  3984 lemma QQQ0:
       
  3985   assumes "bder c a >> code v"
       
  3986   shows "a >> code (injval (erase a) c v)"
       
  3987   using assms
       
  3988   apply(induct a arbitrary: c v)
       
  3989        apply(auto)
       
  3990   using contains.simps apply blast
       
  3991   using contains.simps apply blast
       
  3992   apply(case_tac "c = x2a")
       
  3993     apply(simp)
       
  3994       apply(erule contains.cases)
       
  3995             apply(auto)
       
  3996   
       
  3997 
       
  3998 lemma PPP4:
       
  3999   assumes "bders (intern a) [c1, c2] >> bs"
       
  4000   shows "bders_simp (intern a) [c1, c2] >> bs"
       
  4001   using assms 
       
  4002   apply(simp)
       
  4003   apply(rule contains55)
       
  4004   
       
  4005   find_theorems "bder _ _ >> _"
       
  4006 
       
  4007 
       
  4008    apply(induct s arbitrary: a v rule: rev_induct)
       
  4009    apply(simp)
       
  4010   apply (simp add: contains6)  
       
  4011   apply(simp add: bders_append bders_simp_append ders_append flex_append)
       
  4012   (*apply(rule contains55)*)
       
  4013   apply(drule Prf_injval)
       
  4014   apply(drule_tac x="a" in meta_spec)
       
  4015   apply(drule_tac x="injval (ders xs (erase a)) x v" in meta_spec)
       
  4016   apply(drule meta_mp)
       
  4017    apply(assumption)
       
  4018   
       
  4019   apply(thin_tac "\<Turnstile> injval (ders xs (erase a)) x v : ders xs (erase a)")
       
  4020   
       
  4021   apply(thin_tac "bders a xs >> retrieve a (flex (erase a) id xs (injval (ders xs (erase a)) x v))")
       
  4022   
       
  4023   apply(rule Etrans)
       
  4024   apply(rule contains7)
       
  4025 
       
  4026 lemma PPP4: 
       
  4027   assumes "bders a s >> code v" "\<Turnstile> v : ders s (erase a)"
       
  4028   shows "bders_simp a s >> code v"
       
  4029   using assms
       
  4030   apply(induct s arbitrary: a v rule: rev_induct)
       
  4031    apply(simp)
       
  4032   apply(simp add: bders_append bders_simp_append ders_append)
       
  4033   apply(rule contains55)
       
  4034   find_theorems "bder _ _ >> _"
       
  4035 
       
  4036 
       
  4037 lemma PPP0: 
       
  4038   assumes "s \<in> L (r)"
       
  4039   shows "(bders (intern r) s) >> code (flex r id s (mkeps (ders s r)))"
       
  4040   using assms
       
  4041   apply(induct s arbitrary: r rule: rev_induct)
       
  4042    apply(simp)
       
  4043   apply (simp add: contains2 mkeps_nullable nullable_correctness)
       
  4044   apply(simp add: bders_simp_append flex_append)
       
  4045   apply(rule contains55)
       
  4046   apply(rule Etrans)
       
  4047    apply(rule contains7)
       
  4048    defer
       
  4049   
       
  4050   find_theorems "_ >> _" 
       
  4051   apply(drule_tac x="der a r" in meta_spec)
       
  4052   apply(drule meta_mp)
       
  4053   find_theorems "bder _ _ >> _" 
       
  4054   apply(subgoal_tac "s \<in> L(der a r)")
       
  4055    prefer 2
       
  4056   
       
  4057    apply (simp add: Posix_Prf contains2)
       
  4058   apply(simp add: bders_simp_append)
       
  4059   apply(rule contains55)
       
  4060   apply(frule PPP0)
       
  4061   apply(simp add: bders_append)
       
  4062   using Posix_injval contains7
       
  4063   apply(subgoal_tac "retrieve r (injval (erase r) x v)")
       
  4064   find_theorems "bders _ _ >> _" 
       
  4065   
       
  4066 
       
  4067 
       
  4068 lemma PPP1:
       
  4069   assumes "\<Turnstile> v : ders s r"
       
  4070   shows "bders (intern r) s >> code v"
       
  4071   using  assms
       
  4072   apply(induct s arbitrary: r v rule: rev_induct)
       
  4073    apply(simp)
       
  4074    apply (simp add: Posix_Prf contains2)
       
  4075   apply(simp add: bders_append ders_append flex_append)
       
  4076   apply(frule Prf_injval)
       
  4077   apply(drule meta_spec)
       
  4078   apply(drule meta_spec)
       
  4079   apply(drule meta_mp)
       
  4080    apply(assumption)
       
  4081   apply(subst retrieve_code)
       
  4082    apply(assumption)
       
  4083   apply(subst (asm) retrieve_code)
       
  4084    apply(assumption)
       
  4085 
       
  4086   using contains7 contains7a contains6 retrieve_code
       
  4087   apply(rule contains7)
       
  4088   
       
  4089   find_theorems "bder _ _ >> _"
       
  4090   find_theorems "code _ = _"
       
  4091   find_theorems "\<Turnstile> _ : der _ _"
       
  4092   
       
  4093   
       
  4094 
       
  4095   find_theorems "_ >> (code _)"
       
  4096   apply(induct s arbitrary: a bs rule: rev_induct)
       
  4097    apply(simp)
       
  4098   apply(simp add: bders_simp_append bders_append)
       
  4099   apply(rule contains55)
       
  4100   find_theorems "bder _ _ >> _"
       
  4101   apply(drule_tac x="bder a aa" in meta_spec)
       
  4102   apply(drule_tac x="bs" in meta_spec)
       
  4103   apply(simp)
       
  4104   apply(rule contains55)
       
  4105   find_theorems "bsimp _ >> _"
       
  4106 
       
  4107 lemma XXX4:
       
  4108   assumes "good a"
       
  4109   shows "bders_simp a s = bsimp (bders a s)"
       
  4110   using  assms
       
  4111   apply(induct s arbitrary: a rule: rev_induct)
       
  4112    apply(simp)
       
  4113    apply (simp add: test2)
       
  4114   apply(simp add: bders_append bders_simp_append)
       
  4115   oops
       
  4116 
       
  4117 
       
  4118 lemma MAINMAIN:
       
  4119   "blexer r s = blexer_simp r s"
       
  4120   apply(induct s arbitrary: r)
       
  4121   apply(simp add: blexer_def blexer_simp_def)
       
  4122   apply(simp add: blexer_def blexer_simp_def del: bders.simps bders_simp.simps)
       
  4123   apply(auto simp del: bders.simps bders_simp.simps)
       
  4124     prefer 2
       
  4125   apply (metis b4 bders.simps(2) bders_simp.simps(2))
       
  4126    prefer 2
       
  4127   apply (metis b4 bders.simps(2))
       
  4128   apply(subst bmkeps_simp)
       
  4129    apply(simp)
       
  4130   apply(case_tac s)
       
  4131    apply(simp only: bders.simps)
       
  4132    apply(subst bders_simp.simps)
       
  4133   apply(simp)
       
  4134   oops   
       
  4135 
       
  4136 
       
  4137 lemma
       
  4138   fixes n :: nat
       
  4139   shows "(\<Sum>i \<in> {0..n}. i) = n * (n + 1) div 2"
       
  4140   apply(induct n)
       
  4141   apply(simp)
       
  4142   apply(simp)
       
  4143   done
       
  4144 
       
  4145 
       
  4146 
       
  4147 
       
  4148 
       
  4149 end