thys2/BitCoded2CT.thy
changeset 365 ec5e4fe4cc70
equal deleted inserted replaced
364:232aa2f19a75 365:ec5e4fe4cc70
       
     1 
       
     2 theory BitCoded2CT
       
     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 
       
   645 inductive contains2 :: "arexp \<Rightarrow> bit list \<Rightarrow> bool" ("_ >>2 _" [51, 50] 50)
       
   646   where
       
   647   "AONE bs >>2 bs"
       
   648 | "ACHAR bs c >>2 bs"
       
   649 | "\<lbrakk>a1 >>2 bs1; a2 >>2 bs2\<rbrakk> \<Longrightarrow> ASEQ bs a1 a2 >>2 bs @ bs1 @ bs2"
       
   650 | "r >>2 bs1 \<Longrightarrow> AALTs bs (r#rs) >>2 bs @ bs1"
       
   651 | "AALTs bs rs >>2 bs @ bs1 \<Longrightarrow> AALTs bs (r#rs) >>2 bs @ bs1"
       
   652 | "ASTAR bs r >>2 bs @ [S]"
       
   653 | "\<lbrakk>r >>2 bs1; ASTAR [] r >>2 bs2\<rbrakk> \<Longrightarrow> ASTAR bs r >>2 bs @ Z # bs1 @ bs2"
       
   654 | "r >>2 bs \<Longrightarrow> (bsimp r) >>2 bs"
       
   655 
       
   656 
       
   657 inductive contains :: "arexp \<Rightarrow> bit list \<Rightarrow> bool" ("_ >> _" [51, 50] 50)
       
   658   where
       
   659   "AONE bs >> bs"
       
   660 | "ACHAR bs c >> bs"
       
   661 | "\<lbrakk>a1 >> bs1; a2 >> bs2\<rbrakk> \<Longrightarrow> ASEQ bs a1 a2 >> bs @ bs1 @ bs2"
       
   662 | "r >> bs1 \<Longrightarrow> AALTs bs (r#rs) >> bs @ bs1"
       
   663 | "AALTs bs rs >> bs @ bs1 \<Longrightarrow> AALTs bs (r#rs) >> bs @ bs1"
       
   664 | "ASTAR bs r >> bs @ [S]"
       
   665 | "\<lbrakk>r >> bs1; ASTAR [] r >> bs2\<rbrakk> \<Longrightarrow> ASTAR bs r >> bs @ Z # bs1 @ bs2"
       
   666 
       
   667 lemma contains0:
       
   668   assumes "a >> bs"
       
   669   shows "(fuse bs1 a) >> bs1 @ bs"
       
   670   using assms
       
   671   apply(induct arbitrary: bs1)
       
   672   apply(auto intro: contains.intros)
       
   673        apply (metis append.assoc contains.intros(3))
       
   674      apply (metis append.assoc contains.intros(4))
       
   675   apply (metis append.assoc contains.intros(5))
       
   676     apply (metis append.assoc contains.intros(6))
       
   677    apply (metis append_assoc contains.intros(7))
       
   678   done
       
   679 
       
   680 lemma contains1:
       
   681   assumes "\<forall>v\<in>set vs. \<Turnstile> v : r \<and> intern r >> code v"
       
   682   shows "ASTAR [] (intern r) >> code (Stars vs)"
       
   683   using assms
       
   684   apply(induct vs)
       
   685    apply(simp)
       
   686   using contains.simps apply blast
       
   687   apply(simp)
       
   688    apply(subst (2) append_Nil[symmetric])
       
   689   apply(rule contains.intros)
       
   690    apply(auto)
       
   691   done
       
   692 
       
   693 
       
   694 
       
   695 
       
   696 
       
   697 lemma contains2:
       
   698   assumes "\<Turnstile> v : r"
       
   699   shows "(intern r) >> code v"
       
   700   using assms
       
   701   apply(induct)
       
   702        prefer 4
       
   703        apply(simp)
       
   704        apply(rule contains.intros)
       
   705    prefer 4
       
   706        apply(simp)
       
   707       apply(rule contains.intros)
       
   708      apply(simp)
       
   709   apply(subst (3) append_Nil[symmetric])
       
   710   apply(rule contains.intros)
       
   711       apply(simp)
       
   712   apply(simp)
       
   713     apply(simp)
       
   714   apply(subst (9) append_Nil[symmetric])
       
   715     apply(rule contains.intros)
       
   716     apply (metis append_Cons append_self_conv2 contains0)
       
   717     apply(simp)
       
   718      apply(subst (9) append_Nil[symmetric])
       
   719    apply(rule contains.intros)
       
   720    back
       
   721    apply(rule contains.intros)
       
   722   apply(drule_tac ?bs1.0="[S]" in contains0)
       
   723    apply(simp)
       
   724   apply(simp)
       
   725   apply(case_tac vs)
       
   726    apply(simp)
       
   727   apply (metis append_Nil contains.intros(6))
       
   728   using contains1 by blast
       
   729 
       
   730 lemma qq1:
       
   731   assumes "\<exists>r \<in> set rs. bnullable r"
       
   732   shows "bmkeps (AALTs bs (rs @ rs1)) = bmkeps (AALTs bs rs)"
       
   733   using assms
       
   734   apply(induct rs arbitrary: rs1 bs)
       
   735   apply(simp)
       
   736   apply(simp)
       
   737   by (metis Nil_is_append_conv bmkeps.simps(4) neq_Nil_conv r0 split_list_last)
       
   738 
       
   739 lemma qq2:
       
   740   assumes "\<forall>r \<in> set rs. \<not> bnullable r" "\<exists>r \<in> set rs1. bnullable r"
       
   741   shows "bmkeps (AALTs bs (rs @ rs1)) = bmkeps (AALTs bs rs1)"
       
   742   using assms
       
   743   apply(induct rs arbitrary: rs1 bs)
       
   744   apply(simp)
       
   745   apply(simp)
       
   746   by (metis append_assoc in_set_conv_decomp r1 r2)
       
   747 
       
   748 lemma qq2a:
       
   749   assumes "\<not> bnullable r" "\<exists>r \<in> set rs1. bnullable r"
       
   750   shows "bmkeps (AALTs bs (r # rs1)) = bmkeps (AALTs bs rs1)"
       
   751   using assms
       
   752   by (simp add: r1)
       
   753   
       
   754 lemma qq3:
       
   755   shows "bnullable (AALTs bs rs) = (\<exists>r \<in> set rs. bnullable r)"
       
   756   apply(induct rs arbitrary: bs)
       
   757   apply(simp)
       
   758   apply(simp)
       
   759   done
       
   760 
       
   761 lemma qq4:
       
   762   assumes "bnullable (AALTs bs rs)"
       
   763   shows "bmkeps (AALTs bs rs) = bs @ bmkeps (AALTs [] rs)"
       
   764   by (metis append_Nil2 assms bmkeps_retrieve bnullable_correctness erase_fuse fuse.simps(4) mkeps_nullable retrieve_fuse2)
       
   765 
       
   766 
       
   767 lemma contains3a:
       
   768   assumes "AALTs bs lst >> bs @ bs1"
       
   769   shows "AALTs bs (a # lst) >> bs @ bs1"
       
   770   using assms
       
   771   apply -
       
   772   by (simp add: contains.intros(5))
       
   773 
       
   774   
       
   775 lemma contains3b:
       
   776   assumes "a >> bs1"
       
   777   shows "AALTs bs (a # lst) >> bs @ bs1"
       
   778   using assms
       
   779   apply -
       
   780   apply(rule contains.intros)
       
   781   apply(simp)
       
   782   done   
       
   783 
       
   784 
       
   785 lemma contains3:
       
   786   assumes "\<And>x. \<lbrakk>x \<in> set rs; bnullable x\<rbrakk> \<Longrightarrow> x >> bmkeps x" "x \<in> set rs" "bnullable x"
       
   787   shows "AALTs bs rs >> bmkeps (AALTs bs rs)"
       
   788   using assms
       
   789   apply(induct rs arbitrary: bs x)
       
   790    apply simp
       
   791   by (metis contains.intros(4) contains.intros(5) list.set_intros(1) list.set_intros(2) qq3 qq4 r r0 r1)
       
   792 
       
   793 lemma cont1:
       
   794   assumes "\<And>v. \<Turnstile> v : erase r \<Longrightarrow> r >> retrieve r v" 
       
   795           "\<forall>v\<in>set vs. \<Turnstile> v : erase r \<and> flat v \<noteq> []" 
       
   796   shows "ASTAR bs r >> retrieve (ASTAR bs r) (Stars vs)"
       
   797   using assms 
       
   798   apply(induct vs arbitrary: bs r)
       
   799    apply(simp)
       
   800   using contains.intros(6) apply auto[1]
       
   801   by (simp add: contains.intros(7))
       
   802   
       
   803 lemma contains4:
       
   804   assumes "bnullable a"
       
   805   shows "a >> bmkeps a"
       
   806   using assms
       
   807   apply(induct a rule: bnullable.induct)
       
   808        apply(auto intro: contains.intros)
       
   809   using contains3 by blast
       
   810 
       
   811 lemma contains5:
       
   812   assumes "\<Turnstile> v : r"
       
   813   shows "(intern r) >> retrieve (intern r) v"
       
   814   using contains2[OF assms] retrieve_code[OF assms]
       
   815   by (simp)
       
   816 
       
   817 
       
   818 lemma contains6:
       
   819   assumes "\<Turnstile> v : (erase r)"
       
   820   shows "r >> retrieve r v"
       
   821   using assms
       
   822   apply(induct r arbitrary: v rule: erase.induct)
       
   823   apply(auto)[1]
       
   824   using Prf_elims(1) apply blast
       
   825   using Prf_elims(4) contains.intros(1) apply force
       
   826   using Prf_elims(5) contains.intros(2) apply force
       
   827   apply(auto)[1]
       
   828   using Prf_elims(1) apply blast
       
   829   apply(auto)[1]
       
   830   using contains3b contains3a apply blast
       
   831     prefer 2
       
   832   apply(auto)[1]
       
   833     apply (metis Prf_elims(2) contains.intros(3) retrieve.simps(6))
       
   834    prefer 2
       
   835   apply(auto)[1]
       
   836    apply (metis Prf_elims(6) cont1)
       
   837   apply(simp)
       
   838   apply(erule Prf_elims)
       
   839    apply(auto)
       
   840    apply (simp add: contains3b)
       
   841   using retrieve_fuse2 contains3b contains3a
       
   842   apply(subst retrieve_fuse2[symmetric])
       
   843   apply (metis append_Nil2 erase_fuse fuse.simps(4))
       
   844   apply(simp)
       
   845   by (metis append_Nil2 erase_fuse fuse.simps(4))
       
   846 
       
   847 lemma contains7:
       
   848   assumes "\<Turnstile> v : der c (erase r)"
       
   849   shows "(bder c r) >> retrieve r (injval (erase r) c v)"
       
   850   using bder_retrieve[OF assms(1)] retrieve_code[OF assms(1)]
       
   851   by (metis assms contains6 erase_bder)
       
   852 
       
   853 
       
   854 lemma contains7a:
       
   855   assumes "\<Turnstile> v : der c (erase r)"
       
   856   shows "r >> retrieve r (injval (erase r) c v)"
       
   857   using assms
       
   858   apply -
       
   859   apply(drule Prf_injval)
       
   860   apply(drule contains6)
       
   861   apply(simp)
       
   862   done
       
   863 
       
   864 lemma contains7b:
       
   865   assumes "\<Turnstile> v : ders s (erase r)"
       
   866   shows "(bders r s) >> retrieve r (flex (erase r) id s v)"
       
   867   using assms
       
   868   apply(induct s arbitrary: r v)
       
   869    apply(simp)
       
   870    apply (simp add: contains6)
       
   871   apply(simp add: bders_append flex_append ders_append)
       
   872   apply(drule_tac x="bder a r" in meta_spec)
       
   873   apply(drule meta_spec)
       
   874   apply(drule meta_mp)
       
   875    apply(simp)
       
   876   apply(simp)
       
   877   apply(subst (asm) bder_retrieve)
       
   878    defer
       
   879   apply (simp add: flex_injval)
       
   880   by (simp add: Prf_flex)
       
   881 
       
   882 lemma contains7_iff:
       
   883   assumes "\<Turnstile> v : der c (erase r)"
       
   884   shows "(bder c r) >> retrieve r (injval (erase r) c v) \<longleftrightarrow>
       
   885                   r >> retrieve r (injval (erase r) c v)"
       
   886   by (simp add: assms contains7 contains7a)
       
   887 
       
   888 lemma contains8_iff:
       
   889   assumes "\<Turnstile> v : ders s (erase r)"
       
   890   shows "(bders r s) >> retrieve r (flex (erase r) id s v) \<longleftrightarrow>
       
   891                   r >> retrieve r (flex (erase r) id s v)"
       
   892   using Prf_flex assms contains6 contains7b by blast
       
   893 
       
   894 
       
   895 fun 
       
   896   bders_simp :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
       
   897 where
       
   898   "bders_simp r [] = r"
       
   899 | "bders_simp r (c # s) = bders_simp (bsimp (bder c r)) s"
       
   900 
       
   901 definition blexer_simp where
       
   902  "blexer_simp r s \<equiv> if bnullable (bders_simp (intern r) s) then 
       
   903                 decode (bmkeps (bders_simp (intern r) s)) r else None"
       
   904 
       
   905 
       
   906 
       
   907 
       
   908 
       
   909 lemma bders_simp_append:
       
   910   shows "bders_simp r (s1 @ s2) = bders_simp (bders_simp r s1) s2"
       
   911   apply(induct s1 arbitrary: r s2)
       
   912    apply(simp)
       
   913   apply(simp)
       
   914   done
       
   915 
       
   916 lemma bsimp_ASEQ_size:
       
   917   shows "asize (bsimp_ASEQ bs r1 r2) \<le> Suc (asize r1 + asize r2)"
       
   918   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
       
   919   apply(auto)
       
   920   done
       
   921 
       
   922 
       
   923 
       
   924 lemma flts_size:
       
   925   shows "sum_list (map asize (flts rs)) \<le> sum_list (map asize rs)"
       
   926   apply(induct rs rule: flts.induct)
       
   927         apply(simp_all)
       
   928   by (simp add: asize_fuse comp_def)
       
   929   
       
   930 
       
   931 lemma bsimp_AALTs_size:
       
   932   shows "asize (bsimp_AALTs bs rs) \<le> Suc (sum_list (map asize rs))"
       
   933   apply(induct rs rule: bsimp_AALTs.induct)
       
   934   apply(auto simp add: asize_fuse)
       
   935   done
       
   936 
       
   937 
       
   938 lemma bsimp_size:
       
   939   shows "asize (bsimp r) \<le> asize r"
       
   940   apply(induct r)
       
   941        apply(simp_all)
       
   942    apply (meson Suc_le_mono add_mono_thms_linordered_semiring(1) bsimp_ASEQ_size le_trans)
       
   943   apply(rule le_trans)
       
   944    apply(rule bsimp_AALTs_size)
       
   945   apply(simp)
       
   946    apply(rule le_trans)
       
   947    apply(rule flts_size)
       
   948   by (simp add: sum_list_mono)
       
   949 
       
   950 lemma bsimp_asize0:
       
   951   shows "(\<Sum>x\<leftarrow>rs. asize (bsimp x)) \<le> sum_list (map asize rs)"
       
   952   apply(induct rs)
       
   953    apply(auto)
       
   954   by (simp add: add_mono bsimp_size)
       
   955 
       
   956 lemma bsimp_AALTs_size2:
       
   957   assumes "\<forall>r \<in> set  rs. nonalt r"
       
   958   shows "asize (bsimp_AALTs bs rs) \<ge> sum_list (map asize rs)"
       
   959   using assms
       
   960   apply(induct rs rule: bsimp_AALTs.induct)
       
   961     apply(simp_all add: asize_fuse)
       
   962   done
       
   963 
       
   964 
       
   965 lemma qq:
       
   966   shows "map (asize \<circ> fuse bs) rs = map asize rs"
       
   967   apply(induct rs)
       
   968    apply(auto simp add: asize_fuse)
       
   969   done
       
   970 
       
   971 lemma flts_size2:
       
   972   assumes "\<exists>bs rs'. AALTs bs  rs' \<in> set rs"
       
   973   shows "sum_list (map asize (flts rs)) < sum_list (map asize rs)"
       
   974   using assms
       
   975   apply(induct rs)
       
   976    apply(auto simp add: qq)
       
   977    apply (simp add: flts_size less_Suc_eq_le)
       
   978   apply(case_tac a)
       
   979        apply(auto simp add: qq)
       
   980    prefer 2
       
   981    apply (simp add: flts_size le_imp_less_Suc)
       
   982   using less_Suc_eq by auto
       
   983 
       
   984 lemma bsimp_AALTs_size3:
       
   985   assumes "\<exists>r \<in> set  (map bsimp rs). \<not>nonalt r"
       
   986   shows "asize (bsimp (AALTs bs rs)) < asize (AALTs bs rs)"
       
   987   using assms flts_size2
       
   988   apply  -
       
   989   apply(clarify)
       
   990   apply(simp)
       
   991   apply(drule_tac x="map bsimp rs" in meta_spec)
       
   992   apply(drule meta_mp)
       
   993   apply (metis list.set_map nonalt.elims(3))
       
   994   apply(simp)
       
   995   apply(rule order_class.order.strict_trans1)
       
   996    apply(rule bsimp_AALTs_size)
       
   997   apply(simp)
       
   998   by (smt Suc_leI bsimp_asize0 comp_def le_imp_less_Suc le_trans map_eq_conv not_less_eq)
       
   999 
       
  1000 
       
  1001 
       
  1002 
       
  1003 lemma L_bsimp_ASEQ:
       
  1004   "L (SEQ (erase r1) (erase r2)) = L (erase (bsimp_ASEQ bs r1 r2))"
       
  1005   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
       
  1006   apply(simp_all)
       
  1007   by (metis erase_fuse fuse.simps(4))
       
  1008 
       
  1009 lemma L_bsimp_AALTs:
       
  1010   "L (erase (AALTs bs rs)) = L (erase (bsimp_AALTs bs rs))"
       
  1011   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  1012   apply(simp_all add: erase_fuse)
       
  1013   done
       
  1014 
       
  1015 lemma L_erase_AALTs:
       
  1016   shows "L (erase (AALTs bs rs)) = \<Union> (L ` erase ` (set rs))"
       
  1017   apply(induct rs)
       
  1018    apply(simp)
       
  1019   apply(simp)
       
  1020   apply(case_tac rs)
       
  1021    apply(simp)
       
  1022   apply(simp)
       
  1023   done
       
  1024 
       
  1025 lemma L_erase_flts:
       
  1026   shows "\<Union> (L ` erase ` (set (flts rs))) = \<Union> (L ` erase ` (set rs))"
       
  1027   apply(induct rs rule: flts.induct)
       
  1028         apply(simp_all)
       
  1029   apply(auto)
       
  1030   using L_erase_AALTs erase_fuse apply auto[1]
       
  1031   by (simp add: L_erase_AALTs erase_fuse)
       
  1032 
       
  1033 
       
  1034 lemma L_bsimp_erase:
       
  1035   shows "L (erase r) = L (erase (bsimp r))"
       
  1036   apply(induct r)
       
  1037   apply(simp)
       
  1038   apply(simp)
       
  1039   apply(simp)
       
  1040   apply(auto simp add: Sequ_def)[1]
       
  1041   apply(subst L_bsimp_ASEQ[symmetric])
       
  1042   apply(auto simp add: Sequ_def)[1]
       
  1043   apply(subst (asm)  L_bsimp_ASEQ[symmetric])
       
  1044   apply(auto simp add: Sequ_def)[1]
       
  1045    apply(simp)
       
  1046    apply(subst L_bsimp_AALTs[symmetric])
       
  1047    defer
       
  1048    apply(simp)
       
  1049   apply(subst (2)L_erase_AALTs)
       
  1050   apply(subst L_erase_flts)
       
  1051   apply(auto)
       
  1052    apply (simp add: L_erase_AALTs)
       
  1053   using L_erase_AALTs by blast
       
  1054 
       
  1055 lemma bsimp_ASEQ0:
       
  1056   shows "bsimp_ASEQ bs r1 AZERO = AZERO"
       
  1057   apply(induct r1)
       
  1058   apply(auto)
       
  1059   done
       
  1060 
       
  1061 
       
  1062 
       
  1063 lemma bsimp_ASEQ1:
       
  1064   assumes "r1 \<noteq> AZERO" "r2 \<noteq> AZERO" "\<forall>bs. r1 \<noteq> AONE bs"
       
  1065   shows "bsimp_ASEQ bs r1 r2 = ASEQ bs r1 r2"
       
  1066   using assms
       
  1067   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
       
  1068   apply(auto)
       
  1069   done
       
  1070 
       
  1071 lemma bsimp_ASEQ2:
       
  1072   shows "bsimp_ASEQ bs (AONE bs1) r2 = fuse (bs @ bs1) r2"
       
  1073   apply(induct r2)
       
  1074   apply(auto)
       
  1075   done
       
  1076 
       
  1077 
       
  1078 lemma L_bders_simp:
       
  1079   shows "L (erase (bders_simp r s)) = L (erase (bders r s))"
       
  1080   apply(induct s arbitrary: r rule: rev_induct)
       
  1081    apply(simp)
       
  1082   apply(simp)
       
  1083   apply(simp add: ders_append)
       
  1084   apply(simp add: bders_simp_append)
       
  1085   apply(simp add: L_bsimp_erase[symmetric])
       
  1086   by (simp add: der_correctness)
       
  1087 
       
  1088 lemma b1:
       
  1089   "bsimp_ASEQ bs1 (AONE bs) r =  fuse (bs1 @ bs) r" 
       
  1090   apply(induct r)
       
  1091        apply(auto)
       
  1092   done
       
  1093 
       
  1094 lemma b2:
       
  1095   assumes "bnullable r"
       
  1096   shows "bmkeps (fuse bs r) = bs @ bmkeps r"
       
  1097   by (simp add: assms bmkeps_retrieve bnullable_correctness erase_fuse mkeps_nullable retrieve_fuse2)
       
  1098 
       
  1099 lemma b3:
       
  1100   shows "bnullable r = bnullable (bsimp r)"
       
  1101   using L_bsimp_erase bnullable_correctness nullable_correctness by auto
       
  1102 
       
  1103 
       
  1104 lemma b4:
       
  1105   shows "bnullable (bders_simp r s) = bnullable (bders r s)"
       
  1106   by (metis L_bders_simp bnullable_correctness lexer.simps(1) lexer_correct_None option.distinct(1))
       
  1107 
       
  1108 lemma q1:
       
  1109   assumes "\<forall>r \<in> set rs. bmkeps(bsimp r) = bmkeps r"
       
  1110   shows "map (\<lambda>r. bmkeps(bsimp r)) rs = map bmkeps rs"
       
  1111   using assms
       
  1112   apply(induct rs)
       
  1113   apply(simp)
       
  1114   apply(simp)
       
  1115   done
       
  1116 
       
  1117 lemma q3:
       
  1118   assumes "\<exists>r \<in> set rs. bnullable r"
       
  1119   shows "bmkeps (AALTs bs rs) = bmkeps (bsimp_AALTs bs rs)"
       
  1120   using assms
       
  1121   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  1122     apply(simp)
       
  1123    apply(simp)
       
  1124   apply (simp add: b2)
       
  1125   apply(simp)
       
  1126   done
       
  1127 
       
  1128 
       
  1129 lemma fuse_empty:
       
  1130   shows "fuse [] r = r"
       
  1131   apply(induct r)
       
  1132        apply(auto)
       
  1133   done
       
  1134 
       
  1135 lemma flts_fuse:
       
  1136   shows "map (fuse bs) (flts rs) = flts (map (fuse bs) rs)"
       
  1137   apply(induct rs arbitrary: bs rule: flts.induct)
       
  1138         apply(auto simp add: fuse_append)
       
  1139   done
       
  1140 
       
  1141 lemma bsimp_ASEQ_fuse:
       
  1142   shows "fuse bs1 (bsimp_ASEQ bs2 r1 r2) = bsimp_ASEQ (bs1 @ bs2) r1 r2"
       
  1143   apply(induct r1 r2 arbitrary: bs1 bs2 rule: bsimp_ASEQ.induct)
       
  1144   apply(auto)
       
  1145   done
       
  1146 
       
  1147 lemma bsimp_AALTs_fuse:
       
  1148   assumes "\<forall>r \<in> set rs. fuse bs1 (fuse bs2 r) = fuse (bs1 @ bs2) r"
       
  1149   shows "fuse bs1 (bsimp_AALTs bs2 rs) = bsimp_AALTs (bs1 @ bs2) rs"
       
  1150   using assms
       
  1151   apply(induct bs2 rs arbitrary: bs1 rule: bsimp_AALTs.induct)
       
  1152   apply(auto)
       
  1153   done
       
  1154 
       
  1155 
       
  1156 
       
  1157 lemma bsimp_fuse:
       
  1158   shows "fuse bs (bsimp r) = bsimp (fuse bs r)"
       
  1159 apply(induct r arbitrary: bs)
       
  1160        apply(simp)
       
  1161       apply(simp)
       
  1162      apply(simp)
       
  1163     prefer 3
       
  1164     apply(simp)
       
  1165    apply(simp)
       
  1166    apply (simp add: bsimp_ASEQ_fuse)
       
  1167   apply(simp)
       
  1168   by (simp add: bsimp_AALTs_fuse fuse_append)
       
  1169 
       
  1170 lemma bsimp_fuse_AALTs:
       
  1171   shows "fuse bs (bsimp (AALTs [] rs)) = bsimp (AALTs bs rs)"
       
  1172   apply(subst bsimp_fuse) 
       
  1173   apply(simp)
       
  1174   done
       
  1175 
       
  1176 lemma bsimp_fuse_AALTs2:
       
  1177   shows "fuse bs (bsimp_AALTs [] rs) = bsimp_AALTs bs rs"
       
  1178   using bsimp_AALTs_fuse fuse_append by auto
       
  1179   
       
  1180 
       
  1181 lemma bsimp_ASEQ_idem:
       
  1182   assumes "bsimp (bsimp r1) = bsimp r1" "bsimp (bsimp r2) = bsimp r2"
       
  1183   shows "bsimp (bsimp_ASEQ x1 (bsimp r1) (bsimp r2)) = bsimp_ASEQ x1 (bsimp r1) (bsimp r2)"
       
  1184   using assms
       
  1185   apply(case_tac "bsimp r1 = AZERO")
       
  1186     apply(simp)
       
  1187  apply(case_tac "bsimp r2 = AZERO")
       
  1188     apply(simp)
       
  1189   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))  
       
  1190   apply(case_tac "\<exists>bs. bsimp r1 = AONE bs")
       
  1191     apply(auto)[1]
       
  1192     apply(subst bsimp_ASEQ2)
       
  1193    apply(subst bsimp_ASEQ2)
       
  1194   apply (metis assms(2) bsimp_fuse)
       
  1195       apply(subst bsimp_ASEQ1)
       
  1196       apply(auto)
       
  1197   done
       
  1198 
       
  1199 
       
  1200 
       
  1201 lemma  k0:
       
  1202   shows "flts (r # rs1) = flts [r] @ flts rs1"
       
  1203   apply(induct r arbitrary: rs1)
       
  1204    apply(auto)
       
  1205   done
       
  1206 
       
  1207 lemma  k00:
       
  1208   shows "flts (rs1 @ rs2) = flts rs1 @ flts rs2"
       
  1209   apply(induct rs1 arbitrary: rs2)
       
  1210    apply(auto)
       
  1211   by (metis append.assoc k0)
       
  1212 
       
  1213 lemma  k0a:
       
  1214   shows "flts [AALTs bs rs] = map (fuse bs)  rs"
       
  1215   apply(simp)
       
  1216   done
       
  1217 
       
  1218 
       
  1219 lemma  k0b:
       
  1220   assumes "nonalt r" "r \<noteq> AZERO"
       
  1221   shows "flts [r] = [r]"
       
  1222   using assms
       
  1223   apply(case_tac  r)
       
  1224   apply(simp_all)
       
  1225   done
       
  1226 
       
  1227 lemma nn1:
       
  1228   assumes "nonnested (AALTs bs rs)"
       
  1229   shows "\<nexists>bs1 rs1. flts rs = [AALTs bs1 rs1]"
       
  1230   using assms
       
  1231   apply(induct rs rule: flts.induct)
       
  1232   apply(auto)
       
  1233   done
       
  1234 
       
  1235 lemma nn1q:
       
  1236   assumes "nonnested (AALTs bs rs)"
       
  1237   shows "\<nexists>bs1 rs1. AALTs bs1 rs1 \<in> set (flts rs)"
       
  1238   using assms
       
  1239   apply(induct rs rule: flts.induct)
       
  1240   apply(auto)
       
  1241   done
       
  1242 
       
  1243 lemma nn1qq:
       
  1244   assumes "nonnested (AALTs bs rs)"
       
  1245   shows "\<nexists>bs1 rs1. AALTs bs1 rs1 \<in> set rs"
       
  1246   using assms
       
  1247   apply(induct rs rule: flts.induct)
       
  1248   apply(auto)
       
  1249   done
       
  1250 
       
  1251 lemma nn10:
       
  1252   assumes "nonnested (AALTs cs rs)" 
       
  1253   shows "nonnested (AALTs (bs @ cs) rs)"
       
  1254   using assms
       
  1255   apply(induct rs arbitrary: cs bs)
       
  1256    apply(simp_all)
       
  1257   apply(case_tac a)
       
  1258        apply(simp_all)
       
  1259   done
       
  1260 
       
  1261 lemma nn11a:
       
  1262   assumes "nonalt r"
       
  1263   shows "nonalt (fuse bs r)"
       
  1264   using assms
       
  1265   apply(induct r)
       
  1266        apply(auto)
       
  1267   done
       
  1268 
       
  1269 
       
  1270 lemma nn1a:
       
  1271   assumes "nonnested r"
       
  1272   shows "nonnested (fuse bs r)"
       
  1273   using assms
       
  1274   apply(induct bs r arbitrary: rule: fuse.induct)
       
  1275        apply(simp_all add: nn10)
       
  1276   done  
       
  1277 
       
  1278 lemma n0:
       
  1279   shows "nonnested (AALTs bs rs) \<longleftrightarrow> (\<forall>r \<in> set rs. nonalt r)"
       
  1280   apply(induct rs  arbitrary: bs)
       
  1281    apply(auto)
       
  1282     apply (metis list.set_intros(1) nn1qq nonalt.elims(3))
       
  1283    apply (metis list.set_intros(2) nn1qq nonalt.elims(3))
       
  1284   by (metis nonalt.elims(2) nonnested.simps(3) nonnested.simps(4) nonnested.simps(5) nonnested.simps(6) nonnested.simps(7))
       
  1285 
       
  1286   
       
  1287   
       
  1288 
       
  1289 lemma nn1c:
       
  1290   assumes "\<forall>r \<in> set rs. nonnested r"
       
  1291   shows "\<forall>r \<in> set (flts rs). nonalt r"
       
  1292   using assms
       
  1293   apply(induct rs rule: flts.induct)
       
  1294         apply(auto)
       
  1295   apply(rule nn11a)
       
  1296   by (metis nn1qq nonalt.elims(3))
       
  1297 
       
  1298 lemma nn1bb:
       
  1299   assumes "\<forall>r \<in> set rs. nonalt r"
       
  1300   shows "nonnested (bsimp_AALTs bs rs)"
       
  1301   using assms
       
  1302   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  1303     apply(auto)
       
  1304    apply (metis nn11a nonalt.simps(1) nonnested.elims(3))
       
  1305   using n0 by auto
       
  1306     
       
  1307 lemma nn1b:
       
  1308   shows "nonnested (bsimp r)"
       
  1309   apply(induct r)
       
  1310        apply(simp_all)
       
  1311   apply(case_tac "bsimp r1 = AZERO")
       
  1312     apply(simp)
       
  1313  apply(case_tac "bsimp r2 = AZERO")
       
  1314    apply(simp)
       
  1315     apply(subst bsimp_ASEQ0)
       
  1316   apply(simp)
       
  1317   apply(case_tac "\<exists>bs. bsimp r1 = AONE bs")
       
  1318     apply(auto)[1]
       
  1319     apply(subst bsimp_ASEQ2)
       
  1320   apply (simp add: nn1a)    
       
  1321    apply(subst bsimp_ASEQ1)
       
  1322       apply(auto)
       
  1323   apply(rule nn1bb)
       
  1324   apply(auto)
       
  1325   by (metis (mono_tags, hide_lams) imageE nn1c set_map)
       
  1326 
       
  1327 lemma nn1d:
       
  1328   assumes "bsimp r = AALTs bs rs"
       
  1329   shows "\<forall>r1 \<in> set rs. \<forall>  bs. r1 \<noteq> AALTs bs  rs2"
       
  1330   using nn1b assms
       
  1331   by (metis nn1qq)
       
  1332 
       
  1333 lemma nn_flts:
       
  1334   assumes "nonnested (AALTs bs rs)"
       
  1335   shows "\<forall>r \<in>  set (flts rs). nonalt r"
       
  1336   using assms
       
  1337   apply(induct rs arbitrary: bs rule: flts.induct)
       
  1338         apply(auto)
       
  1339   done
       
  1340 
       
  1341 
       
  1342 
       
  1343 lemma rt:
       
  1344   shows "sum_list (map asize (flts (map bsimp rs))) \<le> sum_list (map asize rs)"
       
  1345   apply(induct rs)
       
  1346    apply(simp)
       
  1347   apply(simp)
       
  1348   apply(subst  k0)
       
  1349   apply(simp)
       
  1350   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)
       
  1351 
       
  1352 lemma bsimp_AALTs_qq:
       
  1353   assumes "1 < length rs"
       
  1354   shows "bsimp_AALTs bs rs = AALTs bs  rs"
       
  1355   using  assms
       
  1356   apply(case_tac rs)
       
  1357    apply(simp)
       
  1358   apply(case_tac list)
       
  1359    apply(simp_all)
       
  1360   done
       
  1361 
       
  1362 
       
  1363 lemma bsimp_AALTs1:
       
  1364   assumes "nonalt r"
       
  1365   shows "bsimp_AALTs bs (flts [r]) = fuse bs r"
       
  1366   using  assms
       
  1367   apply(case_tac r)
       
  1368    apply(simp_all)
       
  1369   done
       
  1370 
       
  1371 lemma bbbbs:
       
  1372   assumes "good r" "r = AALTs bs1 rs"
       
  1373   shows "bsimp_AALTs bs (flts [r]) = AALTs bs (map (fuse bs1) rs)"
       
  1374   using  assms
       
  1375   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)
       
  1376 
       
  1377 lemma bbbbs1:
       
  1378   shows "nonalt r \<or> (\<exists>bs rs. r  = AALTs bs rs)"
       
  1379   using nonalt.elims(3) by auto
       
  1380   
       
  1381 
       
  1382 lemma good_fuse:
       
  1383   shows "good (fuse bs r) = good r"
       
  1384   apply(induct r arbitrary: bs)
       
  1385        apply(auto)
       
  1386      apply(case_tac r1)
       
  1387           apply(simp_all)
       
  1388   apply(case_tac r2)
       
  1389           apply(simp_all)
       
  1390   apply(case_tac r2)
       
  1391             apply(simp_all)
       
  1392   apply(case_tac r2)
       
  1393            apply(simp_all)
       
  1394   apply(case_tac r2)
       
  1395           apply(simp_all)
       
  1396   apply(case_tac r1)
       
  1397           apply(simp_all)
       
  1398   apply(case_tac r2)
       
  1399            apply(simp_all)
       
  1400   apply(case_tac r2)
       
  1401            apply(simp_all)
       
  1402   apply(case_tac r2)
       
  1403            apply(simp_all)
       
  1404   apply(case_tac r2)
       
  1405          apply(simp_all)
       
  1406   apply(case_tac x2a)
       
  1407     apply(simp_all)
       
  1408   apply(case_tac list)
       
  1409     apply(simp_all)
       
  1410   apply(case_tac x2a)
       
  1411     apply(simp_all)
       
  1412   apply(case_tac list)
       
  1413     apply(simp_all)
       
  1414   done
       
  1415 
       
  1416 lemma good0:
       
  1417   assumes "rs \<noteq> Nil" "\<forall>r \<in> set rs. nonalt r"
       
  1418   shows "good (bsimp_AALTs bs rs) \<longleftrightarrow> (\<forall>r \<in> set rs. good r)"
       
  1419   using  assms
       
  1420   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  1421   apply(auto simp add: good_fuse)
       
  1422   done
       
  1423 
       
  1424 lemma good0a:
       
  1425   assumes "flts (map bsimp rs) \<noteq> Nil" "\<forall>r \<in> set (flts (map bsimp rs)). nonalt r"
       
  1426   shows "good (bsimp (AALTs bs rs)) \<longleftrightarrow> (\<forall>r \<in> set (flts (map bsimp rs)). good r)"
       
  1427   using  assms
       
  1428   apply(simp)
       
  1429   apply(auto)
       
  1430   apply(subst (asm) good0)
       
  1431    apply(simp)
       
  1432     apply(auto)
       
  1433    apply(subst good0)
       
  1434    apply(simp)
       
  1435     apply(auto)
       
  1436   done
       
  1437 
       
  1438 lemma flts0:
       
  1439   assumes "r \<noteq> AZERO" "nonalt r"
       
  1440   shows "flts [r] \<noteq> []"
       
  1441   using  assms
       
  1442   apply(induct r)
       
  1443        apply(simp_all)
       
  1444   done
       
  1445 
       
  1446 lemma flts1:
       
  1447   assumes "good r" 
       
  1448   shows "flts [r] \<noteq> []"
       
  1449   using  assms
       
  1450   apply(induct r)
       
  1451        apply(simp_all)
       
  1452   apply(case_tac x2a)
       
  1453    apply(simp)
       
  1454   apply(simp)
       
  1455   done
       
  1456 
       
  1457 lemma flts2:
       
  1458   assumes "good r" 
       
  1459   shows "\<forall>r' \<in> set (flts [r]). good r' \<and> nonalt r'"
       
  1460   using  assms
       
  1461   apply(induct r)
       
  1462        apply(simp)
       
  1463       apply(simp)
       
  1464      apply(simp)
       
  1465     prefer 2
       
  1466     apply(simp)
       
  1467     apply(auto)[1]
       
  1468      apply (metis bsimp_AALTs.elims good.simps(4) good.simps(5) good.simps(6) good_fuse)
       
  1469   apply (metis bsimp_AALTs.elims good.simps(4) good.simps(5) good.simps(6) nn11a)
       
  1470    apply fastforce
       
  1471   apply(simp)
       
  1472   done  
       
  1473 
       
  1474 
       
  1475 lemma flts3:
       
  1476   assumes "\<forall>r \<in> set rs. good r \<or> r = AZERO" 
       
  1477   shows "\<forall>r \<in> set (flts rs). good r"
       
  1478   using  assms
       
  1479   apply(induct rs arbitrary: rule: flts.induct)
       
  1480         apply(simp_all)
       
  1481   by (metis UnE flts2 k0a set_map)
       
  1482 
       
  1483 lemma flts3b:
       
  1484   assumes "\<exists>r\<in>set rs. good r"
       
  1485   shows "flts rs \<noteq> []"
       
  1486   using  assms
       
  1487   apply(induct rs arbitrary: rule: flts.induct)
       
  1488         apply(simp)
       
  1489        apply(simp)
       
  1490       apply(simp)
       
  1491       apply(auto)
       
  1492   done
       
  1493 
       
  1494 lemma flts4:
       
  1495   assumes "bsimp_AALTs bs (flts rs) = AZERO"
       
  1496   shows "\<forall>r \<in> set rs. \<not> good r"
       
  1497   using assms
       
  1498   apply(induct rs arbitrary: bs rule: flts.induct)
       
  1499         apply(auto)
       
  1500         defer
       
  1501   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))
       
  1502   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))
       
  1503   apply (metis arexp.distinct(3) arexp.distinct(7) bsimp_AALTs.elims fuse.simps(3) list.distinct(1) list.inject)
       
  1504   apply (metis arexp.distinct(7) bsimp_AALTs.elims good.simps(1) good_fuse list.distinct(1) list.inject)
       
  1505     apply (metis arexp.distinct(7) bsimp_AALTs.elims list.distinct(1) list.inject)
       
  1506   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))
       
  1507   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)
       
  1508 
       
  1509 
       
  1510 lemma flts_nil:
       
  1511   assumes "\<forall>y. asize y < Suc (sum_list (map asize rs)) \<longrightarrow>
       
  1512             good (bsimp y) \<or> bsimp y = AZERO"
       
  1513   and "\<forall>r\<in>set rs. \<not> good (bsimp r)"
       
  1514   shows "flts (map bsimp rs) = []"
       
  1515   using assms
       
  1516   apply(induct rs)
       
  1517    apply(simp)
       
  1518   apply(simp)
       
  1519   apply(subst k0)
       
  1520   apply(simp)
       
  1521   by force
       
  1522 
       
  1523 lemma flts_nil2:
       
  1524   assumes "\<forall>y. asize y < Suc (sum_list (map asize rs)) \<longrightarrow>
       
  1525             good (bsimp y) \<or> bsimp y = AZERO"
       
  1526   and "bsimp_AALTs bs (flts (map bsimp rs)) = AZERO"
       
  1527   shows "flts (map bsimp rs) = []"
       
  1528   using assms
       
  1529   apply(induct rs arbitrary: bs)
       
  1530    apply(simp)
       
  1531   apply(simp)
       
  1532   apply(subst k0)
       
  1533   apply(simp)
       
  1534   apply(subst (asm) k0)
       
  1535   apply(auto)
       
  1536   apply (metis flts.simps(1) flts.simps(2) flts4 k0 less_add_Suc1 list.set_intros(1))
       
  1537   by (metis flts.simps(2) flts4 k0 less_add_Suc1 list.set_intros(1))
       
  1538   
       
  1539   
       
  1540 
       
  1541 lemma good_SEQ:
       
  1542   assumes "r1 \<noteq> AZERO" "r2 \<noteq> AZERO" "\<forall>bs. r1 \<noteq> AONE bs"
       
  1543   shows "good (ASEQ bs r1 r2) = (good r1 \<and> good r2)"
       
  1544   using assms
       
  1545   apply(case_tac r1)
       
  1546        apply(simp_all)
       
  1547   apply(case_tac r2)
       
  1548           apply(simp_all)
       
  1549   apply(case_tac r2)
       
  1550          apply(simp_all)
       
  1551   apply(case_tac r2)
       
  1552         apply(simp_all)
       
  1553   apply(case_tac r2)
       
  1554        apply(simp_all)
       
  1555   done
       
  1556 
       
  1557 lemma good1:
       
  1558   shows "good (bsimp a) \<or> bsimp a = AZERO"
       
  1559   apply(induct a taking: asize rule: measure_induct)
       
  1560   apply(case_tac x)
       
  1561   apply(simp)
       
  1562   apply(simp)
       
  1563   apply(simp)
       
  1564   prefer 3
       
  1565     apply(simp)
       
  1566    prefer 2
       
  1567   (*  AALTs case  *)
       
  1568   apply(simp only:)
       
  1569    apply(case_tac "x52")
       
  1570     apply(simp)
       
  1571   thm good0a
       
  1572    (*  AALTs list at least one - case *)
       
  1573    apply(simp only: )
       
  1574   apply(frule_tac x="a" in spec)
       
  1575    apply(drule mp)
       
  1576     apply(simp)
       
  1577    (* either first element is good, or AZERO *)
       
  1578     apply(erule disjE)
       
  1579      prefer 2
       
  1580     apply(simp)
       
  1581    (* in  the AZERO case, the size  is smaller *)
       
  1582    apply(drule_tac x="AALTs x51 list" in spec)
       
  1583    apply(drule mp)
       
  1584      apply(simp add: asize0)
       
  1585     apply(subst (asm) bsimp.simps)
       
  1586   apply(subst (asm) bsimp.simps)
       
  1587     apply(assumption)
       
  1588    (* in the good case *)
       
  1589   apply(frule_tac x="AALTs x51 list" in spec)
       
  1590    apply(drule mp)
       
  1591     apply(simp add: asize0)
       
  1592    apply(erule disjE)
       
  1593     apply(rule disjI1)
       
  1594   apply(simp add: good0)
       
  1595     apply(subst good0)
       
  1596       apply (metis Nil_is_append_conv flts1 k0)
       
  1597   apply (metis ex_map_conv list.simps(9) nn1b nn1c)
       
  1598   apply(simp)
       
  1599     apply(subst k0)
       
  1600     apply(simp)
       
  1601     apply(auto)[1]
       
  1602   using flts2 apply blast
       
  1603     apply(subst  (asm) good0)
       
  1604       prefer 3
       
  1605       apply(auto)[1]
       
  1606      apply auto[1]
       
  1607     apply (metis ex_map_conv nn1b nn1c)
       
  1608   (* in  the AZERO case *)
       
  1609    apply(simp)
       
  1610    apply(frule_tac x="a" in spec)
       
  1611    apply(drule mp)
       
  1612   apply(simp)
       
  1613    apply(erule disjE)
       
  1614     apply(rule disjI1)
       
  1615     apply(subst good0)
       
  1616   apply(subst k0)
       
  1617   using flts1 apply blast
       
  1618      apply(auto)[1]
       
  1619   apply (metis (no_types, hide_lams) ex_map_conv list.simps(9) nn1b nn1c)
       
  1620     apply(auto)[1]
       
  1621   apply(subst (asm) k0)
       
  1622   apply(auto)[1]
       
  1623   using flts2 apply blast
       
  1624   apply(frule_tac x="AALTs x51 list" in spec)
       
  1625    apply(drule mp)
       
  1626      apply(simp add: asize0)
       
  1627     apply(erule disjE)
       
  1628      apply(simp)
       
  1629     apply(simp)
       
  1630   apply (metis add.left_commute flts_nil2 less_add_Suc1 less_imp_Suc_add list.distinct(1) list.set_cases nat.inject)
       
  1631    apply(subst (2) k0)
       
  1632   apply(simp)
       
  1633   (* SEQ case *)
       
  1634   apply(simp)
       
  1635   apply(case_tac "bsimp x42 = AZERO")
       
  1636     apply(simp)
       
  1637  apply(case_tac "bsimp x43 = AZERO")
       
  1638    apply(simp)
       
  1639     apply(subst (2) bsimp_ASEQ0)
       
  1640   apply(simp)
       
  1641   apply(case_tac "\<exists>bs. bsimp x42 = AONE bs")
       
  1642     apply(auto)[1]
       
  1643    apply(subst bsimp_ASEQ2)
       
  1644   using good_fuse apply force
       
  1645    apply(subst bsimp_ASEQ1)
       
  1646      apply(auto)
       
  1647   apply(subst  good_SEQ)
       
  1648   apply(simp)
       
  1649     apply(simp)
       
  1650    apply(simp)
       
  1651   using less_add_Suc1 less_add_Suc2 by blast
       
  1652 
       
  1653 lemma good1a:
       
  1654   assumes "L(erase a) \<noteq> {}"
       
  1655   shows "good (bsimp a)"
       
  1656   using good1 assms
       
  1657   using L_bsimp_erase by force
       
  1658   
       
  1659 
       
  1660 
       
  1661 lemma flts_append:
       
  1662   "flts (xs1 @ xs2) = flts xs1 @ flts xs2"
       
  1663   apply(induct xs1  arbitrary: xs2  rule: rev_induct)
       
  1664    apply(auto)
       
  1665   apply(case_tac xs)
       
  1666    apply(auto)
       
  1667    apply(case_tac x)
       
  1668         apply(auto)
       
  1669   apply(case_tac x)
       
  1670         apply(auto)
       
  1671   done
       
  1672 
       
  1673 lemma g1:
       
  1674   assumes "good (bsimp_AALTs bs rs)"
       
  1675   shows "bsimp_AALTs bs rs = AALTs bs rs \<or> (\<exists>r. rs = [r] \<and> bsimp_AALTs bs [r] = fuse bs r)"
       
  1676 using assms
       
  1677     apply(induct rs arbitrary: bs)
       
  1678   apply(simp)
       
  1679   apply(case_tac rs)
       
  1680   apply(simp only:)
       
  1681   apply(simp)
       
  1682   apply(case_tac  list)
       
  1683   apply(simp)
       
  1684   by simp
       
  1685 
       
  1686 lemma flts_0:
       
  1687   assumes "nonnested (AALTs bs  rs)"
       
  1688   shows "\<forall>r \<in> set (flts rs). r \<noteq> AZERO"
       
  1689   using assms
       
  1690   apply(induct rs arbitrary: bs rule: flts.induct)
       
  1691         apply(simp) 
       
  1692        apply(simp) 
       
  1693       defer
       
  1694       apply(simp) 
       
  1695      apply(simp) 
       
  1696     apply(simp) 
       
  1697 apply(simp) 
       
  1698   apply(rule ballI)
       
  1699   apply(simp)
       
  1700   done
       
  1701 
       
  1702 lemma flts_0a:
       
  1703   assumes "nonnested (AALTs bs  rs)"
       
  1704   shows "AZERO \<notin> set (flts rs)"
       
  1705   using assms
       
  1706   using flts_0 by blast 
       
  1707   
       
  1708 lemma qqq1:
       
  1709   shows "AZERO \<notin> set (flts (map bsimp rs))"
       
  1710   by (metis ex_map_conv flts3 good.simps(1) good1)
       
  1711 
       
  1712 
       
  1713 fun nonazero :: "arexp \<Rightarrow> bool"
       
  1714   where
       
  1715   "nonazero AZERO = False"
       
  1716 | "nonazero r = True"
       
  1717 
       
  1718 lemma flts_concat:
       
  1719   shows "flts rs = concat (map (\<lambda>r. flts [r]) rs)"
       
  1720   apply(induct rs)
       
  1721    apply(auto)
       
  1722   apply(subst k0)
       
  1723   apply(simp)
       
  1724   done
       
  1725 
       
  1726 lemma flts_single1:
       
  1727   assumes "nonalt r" "nonazero r"
       
  1728   shows "flts [r] = [r]"
       
  1729   using assms
       
  1730   apply(induct r)
       
  1731   apply(auto)
       
  1732   done
       
  1733 
       
  1734 lemma flts_qq:
       
  1735   assumes "\<forall>y. asize y < Suc (sum_list (map asize rs)) \<longrightarrow> good y \<longrightarrow> bsimp y = y" 
       
  1736           "\<forall>r'\<in>set rs. good r' \<and> nonalt r'"
       
  1737   shows "flts (map bsimp rs) = rs"
       
  1738   using assms
       
  1739   apply(induct rs)
       
  1740    apply(simp)
       
  1741   apply(simp)
       
  1742   apply(subst k0)
       
  1743   apply(subgoal_tac "flts [bsimp a] =  [a]")
       
  1744    prefer 2
       
  1745    apply(drule_tac x="a" in spec)
       
  1746    apply(drule mp)
       
  1747     apply(simp)
       
  1748    apply(auto)[1]
       
  1749   using good.simps(1) k0b apply blast
       
  1750   apply(auto)[1]  
       
  1751   done
       
  1752   
       
  1753 lemma test:
       
  1754   assumes "good r"
       
  1755   shows "bsimp r = r"
       
  1756   using assms
       
  1757   apply(induct r taking: "asize" rule: measure_induct)
       
  1758   apply(erule good.elims)
       
  1759   apply(simp_all)
       
  1760   apply(subst k0)
       
  1761   apply(subst (2) k0)
       
  1762                 apply(subst flts_qq)
       
  1763                   apply(auto)[1]
       
  1764                  apply(auto)[1]
       
  1765                 apply (metis append_Cons append_Nil bsimp_AALTs.simps(3) good.simps(1) k0b)
       
  1766                apply force+
       
  1767   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)
       
  1768   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)
       
  1769          apply force+
       
  1770   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)
       
  1771   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)
       
  1772     apply force+
       
  1773   done
       
  1774 
       
  1775 lemma test2:
       
  1776   assumes "good r"
       
  1777   shows "bsimp r = r"
       
  1778   using assms
       
  1779   apply(induct r taking: "asize" rule: measure_induct)
       
  1780   apply(case_tac x)
       
  1781        apply(simp_all)
       
  1782    defer  
       
  1783   (* AALT case *)
       
  1784    apply(subgoal_tac "1 < length x52")
       
  1785     prefer 2
       
  1786     apply(case_tac x52)
       
  1787      apply(simp)
       
  1788     apply(simp)
       
  1789     apply(case_tac list)
       
  1790      apply(simp)
       
  1791   apply(simp)
       
  1792     apply(subst bsimp_AALTs_qq)
       
  1793     prefer 2
       
  1794     apply(subst flts_qq)
       
  1795       apply(auto)[1]
       
  1796      apply(auto)[1]
       
  1797    apply(case_tac x52)
       
  1798      apply(simp)
       
  1799     apply(simp)
       
  1800     apply(case_tac list)
       
  1801      apply(simp)
       
  1802       apply(simp)
       
  1803       apply(auto)[1]
       
  1804   apply (metis (no_types, lifting) bsimp_AALTs.elims good.simps(6) length_Cons length_pos_if_in_set list.size(3) nat_neq_iff)
       
  1805   apply(simp)  
       
  1806   apply(case_tac x52)
       
  1807      apply(simp)
       
  1808     apply(simp)
       
  1809     apply(case_tac list)
       
  1810      apply(simp)
       
  1811    apply(simp)
       
  1812    apply(subst k0)
       
  1813    apply(simp)
       
  1814    apply(subst (2) k0)
       
  1815    apply(simp)
       
  1816   apply (simp add: Suc_lessI flts1 one_is_add)
       
  1817   (* SEQ case *)
       
  1818   apply(case_tac "bsimp x42 = AZERO")
       
  1819    apply simp
       
  1820   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)  
       
  1821    apply(case_tac "\<exists>bs'. bsimp x42 = AONE bs'")
       
  1822    apply(auto)[1]
       
  1823   defer
       
  1824   apply(case_tac "bsimp x43 = AZERO")
       
  1825     apply(simp)
       
  1826   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)
       
  1827   apply(auto)  
       
  1828    apply (subst bsimp_ASEQ1)
       
  1829       apply(auto)[3]
       
  1830    apply(auto)[1]
       
  1831     apply (metis bsimp.simps(3) good.simps(2) good_SEQ less_add_Suc1)
       
  1832    apply (metis bsimp.simps(3) good.simps(2) good_SEQ less_add_Suc1 less_add_Suc2)
       
  1833   apply (subst bsimp_ASEQ2)
       
  1834   apply(drule_tac x="x42" in spec)
       
  1835   apply(drule mp)
       
  1836    apply(simp)
       
  1837   apply(drule mp)
       
  1838    apply (metis bsimp.elims bsimp.simps(3) good.simps(10) good.simps(11) good.simps(2) good_SEQ)
       
  1839   apply(simp)
       
  1840   done
       
  1841 
       
  1842 
       
  1843 lemma bsimp_idem:
       
  1844   shows "bsimp (bsimp r) = bsimp r"
       
  1845   using test good1
       
  1846   by force
       
  1847 
       
  1848 
       
  1849 lemma contains_ex1:
       
  1850   assumes "a = AALTs bs1 [AZERO, AONE bs2]" "a >> bs" 
       
  1851   shows "bsimp a >> bs"
       
  1852   using assms
       
  1853   apply(simp)
       
  1854   apply(erule contains.cases)
       
  1855         apply(auto)
       
  1856   using contains.simps apply blast
       
  1857   apply(erule contains.cases)
       
  1858         apply(auto)
       
  1859   using contains0 apply fastforce
       
  1860   using contains.simps by blast
       
  1861     
       
  1862 lemma contains_ex2:
       
  1863   assumes "a = AALTs bs1 [AZERO, AONE bs2, AALTs bs5 [AONE bs3, AZERO, AONE bs4]]" "a >> bs" 
       
  1864   shows "bsimp a >> bs"
       
  1865   using assms
       
  1866   apply(simp)
       
  1867   apply(erule contains.cases)
       
  1868         apply(auto)
       
  1869   using contains.simps apply blast
       
  1870   apply(erule contains.cases)
       
  1871         apply(auto)
       
  1872   using contains3b apply blast
       
  1873    apply(erule contains.cases)
       
  1874         apply(auto)
       
  1875   apply(erule contains.cases)
       
  1876          apply(auto)
       
  1877   apply (metis contains.intros(4) contains.intros(5) contains0 fuse.simps(2))
       
  1878   apply(erule contains.cases)
       
  1879          apply(auto)
       
  1880   using contains.simps apply blast
       
  1881   apply(erule contains.cases)
       
  1882          apply(auto)
       
  1883   apply (metis contains.intros(4) contains.intros(5) contains0 fuse.simps(2))
       
  1884       apply(erule contains.cases)
       
  1885          apply(auto)
       
  1886 apply(erule contains.cases)
       
  1887          apply(auto)
       
  1888   done
       
  1889 
       
  1890 lemma contains48:
       
  1891   assumes "\<And>x2aa bs bs1. \<lbrakk>x2aa \<in> set x2a; fuse bs x2aa >> bs @ bs1\<rbrakk> \<Longrightarrow> x2aa >> bs1" 
       
  1892           "AALTs (bs @ x1) x2a >> bs @ bs1"
       
  1893         shows "AALTs x1 x2a >> bs1"
       
  1894   using assms
       
  1895   apply(induct x2a arbitrary: bs x1 bs1)
       
  1896    apply(auto)
       
  1897    apply(erule contains.cases)
       
  1898          apply(auto)
       
  1899   apply(erule contains.cases)
       
  1900         apply(auto)
       
  1901   apply (simp add: contains.intros(4))
       
  1902   using contains.intros(5) by blast
       
  1903 
       
  1904 
       
  1905 lemma contains49:
       
  1906   assumes "fuse bs a >> bs @ bs1"
       
  1907   shows "a >> bs1"
       
  1908   using assms
       
  1909   apply(induct a arbitrary: bs bs1)
       
  1910        apply(auto)
       
  1911   using contains.simps apply blast
       
  1912       apply(erule contains.cases)
       
  1913             apply(auto)
       
  1914   apply(rule contains.intros)
       
  1915     apply(erule contains.cases)
       
  1916             apply(auto)
       
  1917      apply(rule contains.intros)
       
  1918   apply(erule contains.cases)
       
  1919             apply(auto)
       
  1920   apply(rule contains.intros)
       
  1921      apply(auto)[2]
       
  1922   prefer 2
       
  1923   apply(erule contains.cases)
       
  1924          apply(auto)
       
  1925   apply (simp add: contains.intros(6))
       
  1926   using contains.intros(7) apply blast
       
  1927   using contains48 by blast
       
  1928 
       
  1929 lemma contains50:
       
  1930   assumes "bsimp_AALTs bs rs2 >> bs @ bs1"
       
  1931   shows "bsimp_AALTs bs (rs1 @ rs2) >> bs @ bs1"
       
  1932   using assms
       
  1933   apply(induct rs1 arbitrary: bs rs2 bs1)
       
  1934    apply(simp)
       
  1935   apply(auto)
       
  1936   apply(case_tac rs1)
       
  1937    apply(simp)
       
  1938    apply(case_tac rs2)
       
  1939     apply(simp)
       
  1940   using contains.simps apply blast
       
  1941   apply(simp)
       
  1942   apply(case_tac list)
       
  1943     apply(simp)
       
  1944     apply(rule contains.intros)
       
  1945     back
       
  1946     apply(rule contains.intros)
       
  1947   using contains49 apply blast
       
  1948    apply(simp)
       
  1949   using contains.intros(5) apply blast
       
  1950   apply(simp)
       
  1951   by (metis bsimp_AALTs.elims contains.intros(4) contains.intros(5) contains49 list.distinct(1))
       
  1952 
       
  1953 lemma contains51:
       
  1954   assumes "bsimp_AALTs bs [r] >> bs @ bs1"
       
  1955   shows "bsimp_AALTs bs ([r] @ rs2) >> bs @ bs1"
       
  1956   using assms
       
  1957   apply(induct rs2 arbitrary: bs r bs1)
       
  1958    apply(simp)
       
  1959   apply(auto)
       
  1960   using contains.intros(4) contains49 by blast
       
  1961 
       
  1962 lemma contains51a:
       
  1963   assumes "bsimp_AALTs bs rs2 >> bs @ bs1"
       
  1964   shows "bsimp_AALTs bs (rs2 @ [r]) >> bs @ bs1"
       
  1965   using assms
       
  1966   apply(induct rs2 arbitrary: bs r bs1)
       
  1967    apply(simp)
       
  1968    apply(auto)
       
  1969   using contains.simps apply blast
       
  1970   apply(case_tac rs2)
       
  1971    apply(auto)
       
  1972   using contains3b contains49 apply blast
       
  1973   apply(case_tac list)
       
  1974    apply(auto)
       
  1975   apply(erule contains.cases)
       
  1976          apply(auto)
       
  1977   using contains.intros(4) apply auto[1]
       
  1978    apply(erule contains.cases)
       
  1979          apply(auto)
       
  1980     apply (simp add: contains.intros(4) contains.intros(5))
       
  1981    apply (simp add: contains.intros(5))
       
  1982   apply(erule contains.cases)
       
  1983         apply(auto)
       
  1984    apply (simp add: contains.intros(4))
       
  1985    apply(erule contains.cases)
       
  1986         apply(auto)
       
  1987   using contains.intros(4) contains.intros(5) apply blast
       
  1988   using contains.intros(5) by blast  
       
  1989   
       
  1990 lemma contains51b:
       
  1991   assumes "bsimp_AALTs bs rs >> bs @ bs1"
       
  1992   shows "bsimp_AALTs bs (rs @ rs2) >> bs @ bs1"
       
  1993   using assms
       
  1994   apply(induct rs2 arbitrary: bs rs bs1)
       
  1995    apply(simp)
       
  1996   using contains51a by fastforce
       
  1997 
       
  1998 
       
  1999 lemma contains51c:
       
  2000   assumes "AALTs (bs @ bs2) rs >> bs @ bs1"
       
  2001   shows "bsimp_AALTs bs (map (fuse bs2) rs) >> bs @ bs1"
       
  2002   using assms
       
  2003   apply(induct rs arbitrary: bs bs1 bs2)
       
  2004        apply(auto)
       
  2005   apply(erule contains.cases)
       
  2006         apply(auto)
       
  2007   apply(erule contains.cases)
       
  2008         apply(auto)
       
  2009   using contains0 contains51 apply auto[1]
       
  2010   by (metis append.left_neutral append_Cons contains50 list.simps(9))
       
  2011   
       
  2012 
       
  2013 lemma contains51d:
       
  2014   assumes "fuse bs r >> bs @ bs1"
       
  2015   shows "bsimp_AALTs bs (flts [r]) >> bs @ bs1"
       
  2016   using assms
       
  2017   apply(induct r arbitrary: bs bs1)
       
  2018        apply(auto)
       
  2019   by (simp add: contains51c)
       
  2020 
       
  2021 lemma contains52:
       
  2022   assumes "\<exists>r \<in> set rs. (fuse bs r) >> bs @ bs1"
       
  2023   shows "bsimp_AALTs bs (flts rs) >> bs @ bs1"
       
  2024   using assms
       
  2025   apply(induct rs arbitrary: bs bs1)
       
  2026    apply(simp)
       
  2027   apply(auto)
       
  2028    defer
       
  2029    apply (metis contains50 k0)
       
  2030   apply(subst k0)
       
  2031   apply(rule contains51b)
       
  2032   using contains51d by blast
       
  2033 
       
  2034 lemma contains55:
       
  2035   assumes "a >> bs" 
       
  2036   shows "bsimp a >> bs"
       
  2037   using assms
       
  2038   apply(induct a bs arbitrary:)
       
  2039         apply(auto intro: contains.intros)
       
  2040     apply(case_tac "bsimp a1 = AZERO")
       
  2041      apply(simp)
       
  2042   using contains.simps apply blast
       
  2043   apply(case_tac "bsimp a2 = AZERO")
       
  2044      apply(simp)
       
  2045   using contains.simps apply blast
       
  2046   apply(case_tac "\<exists>bs. bsimp a1 = AONE bs")
       
  2047      apply(auto)[1]
       
  2048      apply(rotate_tac 1)
       
  2049      apply(erule contains.cases)
       
  2050            apply(auto)
       
  2051      apply (simp add: b1 contains0 fuse_append)
       
  2052     apply (simp add: bsimp_ASEQ1 contains.intros(3))
       
  2053    prefer 2
       
  2054    apply(case_tac rs)
       
  2055     apply(simp)
       
  2056   using contains.simps apply blast
       
  2057    apply (metis contains50 k0)
       
  2058   (* AALTS case *)
       
  2059   apply(rule contains52)
       
  2060   apply(rule_tac x="bsimp r" in bexI)
       
  2061    apply(auto)
       
  2062   using contains0 by blast
       
  2063 
       
  2064 
       
  2065 lemma q3a:
       
  2066   assumes "\<exists>r \<in> set rs. bnullable r"
       
  2067   shows "bmkeps (AALTs bs (map (fuse bs1) rs)) = bmkeps (AALTs (bs@bs1) rs)"
       
  2068   using assms
       
  2069   apply(induct rs arbitrary: bs bs1)
       
  2070    apply(simp)
       
  2071   apply(simp)
       
  2072   apply(auto)
       
  2073    apply (metis append_assoc b2 bnullable_correctness erase_fuse r0)
       
  2074   apply(case_tac "bnullable a")
       
  2075    apply (metis append.assoc b2 bnullable_correctness erase_fuse r0)
       
  2076   apply(case_tac rs)
       
  2077   apply(simp)
       
  2078   apply(simp)
       
  2079   apply(auto)[1]
       
  2080    apply (metis bnullable_correctness erase_fuse)+
       
  2081   done
       
  2082 
       
  2083 
       
  2084 
       
  2085 lemma qq4a:
       
  2086   assumes "\<exists>x\<in>set list. bnullable x"
       
  2087   shows "\<exists>x\<in>set (flts list). bnullable x"
       
  2088   using assms
       
  2089   apply(induct list rule: flts.induct)
       
  2090         apply(auto)
       
  2091   by (metis UnCI bnullable_correctness erase_fuse imageI)
       
  2092   
       
  2093 
       
  2094 lemma qs3:
       
  2095   assumes "\<exists>r \<in> set rs. bnullable r"
       
  2096   shows "bmkeps (AALTs bs rs) = bmkeps (AALTs bs (flts rs))"
       
  2097   using assms
       
  2098   apply(induct rs arbitrary: bs taking: size rule: measure_induct)
       
  2099   apply(case_tac x)
       
  2100   apply(simp)
       
  2101   apply(simp)
       
  2102   apply(case_tac a)
       
  2103        apply(simp)
       
  2104        apply (simp add: r1)
       
  2105       apply(simp)
       
  2106       apply (simp add: r0)
       
  2107      apply(simp)
       
  2108      apply(case_tac "flts list")
       
  2109       apply(simp)
       
  2110   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)
       
  2111      apply(simp)
       
  2112      apply (simp add: r1)
       
  2113     prefer 3
       
  2114     apply(simp)
       
  2115     apply (simp add: r0)
       
  2116    prefer 2
       
  2117    apply(simp)
       
  2118   apply(case_tac "\<exists>x\<in>set x52. bnullable x")
       
  2119   apply(case_tac "list")
       
  2120     apply(simp)
       
  2121     apply (metis b2 fuse.simps(4) q3a r2)
       
  2122    apply(erule disjE)
       
  2123     apply(subst qq1)
       
  2124      apply(auto)[1]
       
  2125      apply (metis bnullable_correctness erase_fuse)
       
  2126     apply(simp)
       
  2127      apply (metis b2 fuse.simps(4) q3a r2)
       
  2128     apply(simp)
       
  2129     apply(auto)[1]
       
  2130      apply(subst qq1)
       
  2131       apply (metis bnullable_correctness erase_fuse image_eqI set_map)
       
  2132      apply (metis b2 fuse.simps(4) q3a r2)
       
  2133   apply(subst qq1)
       
  2134       apply (metis bnullable_correctness erase_fuse image_eqI set_map)
       
  2135     apply (metis b2 fuse.simps(4) q3a r2)
       
  2136    apply(simp)
       
  2137    apply(subst qq2)
       
  2138      apply (metis bnullable_correctness erase_fuse imageE set_map)
       
  2139   prefer 2
       
  2140   apply(case_tac "list")
       
  2141      apply(simp)
       
  2142     apply(simp)
       
  2143    apply (simp add: qq4a)
       
  2144   apply(simp)
       
  2145   apply(auto)
       
  2146    apply(case_tac list)
       
  2147     apply(simp)
       
  2148    apply(simp)
       
  2149    apply (simp add: r0)
       
  2150   apply(case_tac "bnullable (ASEQ x41 x42 x43)")
       
  2151    apply(case_tac list)
       
  2152     apply(simp)
       
  2153    apply(simp)
       
  2154    apply (simp add: r0)
       
  2155   apply(simp)
       
  2156   using qq4a r1 r2 by auto
       
  2157 
       
  2158 
       
  2159 
       
  2160 lemma k1:
       
  2161   assumes "\<And>x2aa. \<lbrakk>x2aa \<in> set x2a; bnullable x2aa\<rbrakk> \<Longrightarrow> bmkeps x2aa = bmkeps (bsimp x2aa)"
       
  2162           "\<exists>x\<in>set x2a. bnullable x"
       
  2163         shows "bmkeps (AALTs x1 (flts x2a)) = bmkeps (AALTs x1 (flts (map bsimp x2a)))"
       
  2164   using assms
       
  2165   apply(induct x2a)
       
  2166   apply fastforce
       
  2167   apply(simp)
       
  2168   apply(subst k0)
       
  2169   apply(subst (2) k0)
       
  2170   apply(auto)[1]
       
  2171   apply (metis b3 k0 list.set_intros(1) qs3 r0)
       
  2172   by (smt b3 imageI insert_iff k0 list.set(2) qq3 qs3 r0 r1 set_map)
       
  2173   
       
  2174   
       
  2175   
       
  2176 lemma bmkeps_simp:
       
  2177   assumes "bnullable r"
       
  2178   shows "bmkeps r = bmkeps (bsimp r)"
       
  2179   using  assms
       
  2180   apply(induct r)
       
  2181        apply(simp)
       
  2182       apply(simp)
       
  2183      apply(simp)
       
  2184     apply(simp)
       
  2185     prefer 3
       
  2186   apply(simp)
       
  2187    apply(case_tac "bsimp r1 = AZERO")
       
  2188     apply(simp)
       
  2189     apply(auto)[1]
       
  2190   apply (metis L_bsimp_erase L_flat_Prf1 L_flat_Prf2 Prf_elims(1) bnullable_correctness erase.simps(1) mkeps_nullable)
       
  2191  apply(case_tac "bsimp r2 = AZERO")
       
  2192     apply(simp)  
       
  2193     apply(auto)[1]
       
  2194   apply (metis L_bsimp_erase L_flat_Prf1 L_flat_Prf2 Prf_elims(1) bnullable_correctness erase.simps(1) mkeps_nullable)
       
  2195   apply(case_tac "\<exists>bs. bsimp r1 = AONE bs")
       
  2196     apply(auto)[1]
       
  2197     apply(subst b1)
       
  2198     apply(subst b2)
       
  2199   apply(simp add: b3[symmetric])
       
  2200     apply(simp)
       
  2201    apply(subgoal_tac "bsimp_ASEQ x1 (bsimp r1) (bsimp r2) = ASEQ x1 (bsimp r1) (bsimp r2)")
       
  2202     prefer 2
       
  2203   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))
       
  2204    apply(simp)
       
  2205   apply(simp)
       
  2206   thm q3
       
  2207   apply(subst q3[symmetric])
       
  2208    apply simp
       
  2209   using b3 qq4a apply auto[1]
       
  2210   apply(subst qs3)
       
  2211    apply simp
       
  2212   using k1 by blast
       
  2213 
       
  2214 thm bmkeps_retrieve bmkeps_simp bder_retrieve
       
  2215 
       
  2216 lemma bmkeps_bder_AALTs:
       
  2217   assumes "\<exists>r \<in> set rs. bnullable (bder c r)" 
       
  2218   shows "bmkeps (bder c (bsimp_AALTs bs rs)) = bmkeps (bsimp_AALTs bs (map (bder c) rs))"
       
  2219   using assms
       
  2220   apply(induct rs)
       
  2221    apply(simp)
       
  2222   apply(simp)
       
  2223   apply(auto)
       
  2224   apply(case_tac rs)
       
  2225     apply(simp)
       
  2226   apply (metis (full_types) Prf_injval bder_retrieve bmkeps_retrieve bnullable_correctness erase_bder erase_fuse mkeps_nullable retrieve_fuse2)
       
  2227    apply(simp)
       
  2228   apply(case_tac  rs)
       
  2229    apply(simp_all)
       
  2230   done
       
  2231 
       
  2232 lemma bbs0:
       
  2233   shows "blexer_simp r [] = blexer r []"
       
  2234   apply(simp add: blexer_def blexer_simp_def)
       
  2235   done
       
  2236 
       
  2237 lemma bbs1:
       
  2238   shows "blexer_simp r [c] = blexer r [c]"
       
  2239   apply(simp add: blexer_def blexer_simp_def)
       
  2240   apply(auto)
       
  2241     defer
       
  2242   using b3 apply auto[1]
       
  2243   using b3 apply auto[1]  
       
  2244   apply(subst bmkeps_simp[symmetric])
       
  2245    apply(simp)
       
  2246   apply(simp)
       
  2247   done
       
  2248 
       
  2249 lemma oo:
       
  2250   shows "(case (blexer (der c r) s) of None \<Rightarrow> None | Some v \<Rightarrow> Some (injval r c v)) = blexer r (c # s)"
       
  2251   apply(simp add: blexer_correctness)
       
  2252   done
       
  2253 
       
  2254 lemma XXX2_helper:
       
  2255   assumes "\<forall>y. asize y < Suc (sum_list (map asize rs)) \<longrightarrow> good y \<longrightarrow> bsimp y = y" 
       
  2256           "\<forall>r'\<in>set rs. good r' \<and> nonalt r'"
       
  2257   shows "flts (map (bsimp \<circ> bder c) (flts (map bsimp rs))) = flts (map (bsimp \<circ> bder c) rs)"
       
  2258   using assms
       
  2259   apply(induct rs arbitrary: c)
       
  2260    apply(simp)
       
  2261   apply(simp)
       
  2262   apply(subst k0)
       
  2263   apply(simp add: flts_append)
       
  2264   apply(subst (2) k0)
       
  2265   apply(simp add: flts_append)
       
  2266   apply(subgoal_tac "flts [a] =  [a]")
       
  2267    prefer 2
       
  2268   using good.simps(1) k0b apply blast
       
  2269   apply(simp)
       
  2270   done
       
  2271 
       
  2272 lemma bmkeps_good:
       
  2273   assumes "good a"
       
  2274   shows "bmkeps (bsimp a) = bmkeps a"
       
  2275   using assms
       
  2276   using test2 by auto
       
  2277 
       
  2278 
       
  2279 lemma xxx_bder:
       
  2280   assumes "good r"
       
  2281   shows "L (erase r) \<noteq> {}"
       
  2282   using assms
       
  2283   apply(induct r rule: good.induct)
       
  2284   apply(auto simp add: Sequ_def)
       
  2285   done
       
  2286 
       
  2287 lemma xxx_bder2:
       
  2288   assumes "L (erase (bsimp r)) = {}"
       
  2289   shows "bsimp r = AZERO"
       
  2290   using assms xxx_bder test2 good1
       
  2291   by blast
       
  2292 
       
  2293 lemma XXX2aa:
       
  2294   assumes "good a"
       
  2295   shows "bsimp (bder c (bsimp a)) = bsimp (bder c a)"
       
  2296   using  assms
       
  2297   by (simp add: test2)
       
  2298 
       
  2299 lemma XXX2aa_ders:
       
  2300   assumes "good a"
       
  2301   shows "bsimp (bders (bsimp a) s) = bsimp (bders a s)"
       
  2302   using  assms
       
  2303   by (simp add: test2)
       
  2304 
       
  2305 lemma XXX4a:
       
  2306   shows "good (bders_simp (bsimp r) s)  \<or> bders_simp (bsimp r) s = AZERO"
       
  2307   apply(induct s arbitrary: r rule:  rev_induct)
       
  2308    apply(simp)
       
  2309   apply (simp add: good1)
       
  2310   apply(simp add: bders_simp_append)
       
  2311   apply (simp add: good1)
       
  2312   done
       
  2313 
       
  2314 lemma XXX4a_good:
       
  2315   assumes "good a"
       
  2316   shows "good (bders_simp a s) \<or> bders_simp a s = AZERO"
       
  2317   using assms
       
  2318   apply(induct s arbitrary: a rule:  rev_induct)
       
  2319    apply(simp)
       
  2320   apply(simp add: bders_simp_append)
       
  2321   apply (simp add: good1)
       
  2322   done
       
  2323 
       
  2324 lemma XXX4a_good_cons:
       
  2325   assumes "s \<noteq> []"
       
  2326   shows "good (bders_simp a s) \<or> bders_simp a s = AZERO"
       
  2327   using assms
       
  2328   apply(case_tac s)
       
  2329    apply(auto)
       
  2330   using XXX4a by blast
       
  2331 
       
  2332 lemma XXX4b:
       
  2333   assumes "good a" "L (erase (bders_simp a s)) \<noteq> {}"
       
  2334   shows "good (bders_simp a s)"
       
  2335   using assms
       
  2336   apply(induct s arbitrary: a)
       
  2337    apply(simp)
       
  2338   apply(simp)
       
  2339   apply(subgoal_tac "L (erase (bder a aa)) = {} \<or> L (erase (bder a aa)) \<noteq> {}")
       
  2340    prefer 2
       
  2341    apply(auto)[1]
       
  2342   apply(erule disjE)
       
  2343    apply(subgoal_tac "bsimp (bder a aa) = AZERO")
       
  2344     prefer 2
       
  2345   using L_bsimp_erase xxx_bder2 apply auto[1]
       
  2346    apply(simp)
       
  2347   apply (metis L.simps(1) XXX4a erase.simps(1))  
       
  2348   apply(drule_tac x="bsimp (bder a aa)" in meta_spec)
       
  2349   apply(drule meta_mp)
       
  2350   apply simp
       
  2351   apply(rule good1a)
       
  2352   apply(auto)
       
  2353   done
       
  2354 
       
  2355 lemma bders_AZERO:
       
  2356   shows "bders AZERO s = AZERO"
       
  2357   and   "bders_simp AZERO s = AZERO"
       
  2358    apply (induct s)
       
  2359      apply(auto)
       
  2360   done
       
  2361 
       
  2362 lemma LA:
       
  2363   assumes "\<Turnstile> v : ders s (erase r)"
       
  2364   shows "retrieve (bders r s) v = retrieve r (flex (erase r) id s v)"
       
  2365   using assms
       
  2366   apply(induct s arbitrary: r v rule: rev_induct)
       
  2367    apply(simp)
       
  2368   apply(simp add: bders_append ders_append)
       
  2369   apply(subst bder_retrieve)
       
  2370    apply(simp)
       
  2371   apply(drule Prf_injval)
       
  2372   by (simp add: flex_append)
       
  2373 
       
  2374 
       
  2375 lemma LB:
       
  2376   assumes "s \<in> (erase r) \<rightarrow> v" 
       
  2377   shows "retrieve r v = retrieve r (flex (erase r) id s (mkeps (ders s (erase r))))"
       
  2378   using assms
       
  2379   apply(induct s arbitrary: r v rule: rev_induct)
       
  2380    apply(simp)
       
  2381    apply(subgoal_tac "v = mkeps (erase r)")
       
  2382     prefer 2
       
  2383   apply (simp add: Posix1(1) Posix_determ Posix_mkeps nullable_correctness)
       
  2384    apply(simp)
       
  2385   apply(simp add: flex_append ders_append)
       
  2386   by (metis Posix_determ Posix_flex Posix_injval Posix_mkeps ders_snoc lexer_correctness(2) lexer_flex)
       
  2387 
       
  2388 lemma LB_sym:
       
  2389   assumes "s \<in> (erase r) \<rightarrow> v" 
       
  2390   shows "retrieve r v = retrieve r (flex (erase r) id s (mkeps (erase (bders r s))))"
       
  2391   using assms
       
  2392   by (simp add: LB)
       
  2393 
       
  2394 
       
  2395 lemma LC:
       
  2396   assumes "s \<in> (erase r) \<rightarrow> v" 
       
  2397   shows "retrieve r v = retrieve (bders r s) (mkeps (erase (bders r s)))"
       
  2398   apply(simp)
       
  2399   by (metis LA LB Posix1(1) assms lexer_correct_None lexer_flex mkeps_nullable)
       
  2400 
       
  2401 
       
  2402 lemma L0:
       
  2403   assumes "bnullable a"
       
  2404   shows "retrieve (bsimp a) (mkeps (erase (bsimp a))) = retrieve a (mkeps (erase a))"
       
  2405   using assms b3 bmkeps_retrieve bmkeps_simp bnullable_correctness
       
  2406   by (metis b3 bmkeps_retrieve bmkeps_simp bnullable_correctness)
       
  2407 
       
  2408 thm bmkeps_retrieve
       
  2409 
       
  2410 lemma L0a:
       
  2411   assumes "s \<in> L(erase a)"
       
  2412   shows "retrieve (bsimp (bders a s)) (mkeps (erase (bsimp (bders a s)))) = 
       
  2413          retrieve (bders a s) (mkeps (erase (bders a s)))"
       
  2414   using assms
       
  2415   by (metis L0 bnullable_correctness erase_bders lexer_correct_None lexer_flex)
       
  2416   
       
  2417 lemma L0aa:
       
  2418   assumes "s \<in> L (erase a)"
       
  2419   shows "[] \<in> erase (bsimp (bders a s)) \<rightarrow> mkeps (erase (bsimp (bders a s)))"
       
  2420   using assms
       
  2421   by (metis Posix_mkeps b3 bnullable_correctness erase_bders lexer_correct_None lexer_flex)
       
  2422 
       
  2423 lemma L0aaa:
       
  2424   assumes "[c] \<in> L (erase a)"
       
  2425   shows "[c] \<in> (erase a) \<rightarrow> flex (erase a) id [c] (mkeps (erase (bder c a)))"
       
  2426   using assms
       
  2427   by (metis bders.simps(1) bders.simps(2) erase_bders lexer_correct_None lexer_correct_Some lexer_flex option.inject)
       
  2428 
       
  2429 lemma L0aaaa:
       
  2430   assumes "[c] \<in> L (erase a)"
       
  2431   shows "[c] \<in> (erase a) \<rightarrow> flex (erase a) id [c] (mkeps (erase (bders a [c])))"
       
  2432   using assms
       
  2433   using L0aaa by auto
       
  2434     
       
  2435 
       
  2436 lemma L02:
       
  2437   assumes "bnullable (bder c a)"
       
  2438   shows "retrieve (bsimp a) (flex (erase (bsimp a)) id [c] (mkeps (erase (bder c (bsimp a))))) = 
       
  2439          retrieve (bder c (bsimp a)) (mkeps (erase (bder c (bsimp a))))"
       
  2440   using assms
       
  2441   apply(simp)
       
  2442   using bder_retrieve L0 bmkeps_simp bmkeps_retrieve L0  LA LB
       
  2443   apply(subst bder_retrieve[symmetric])
       
  2444   apply (metis L_bsimp_erase bnullable_correctness der_correctness erase_bder mkeps_nullable nullable_correctness)
       
  2445   apply(simp)
       
  2446   done
       
  2447 
       
  2448 lemma L02_bders:
       
  2449   assumes "bnullable (bders a s)"
       
  2450   shows "retrieve (bsimp a) (flex (erase (bsimp a)) id s (mkeps (erase (bders (bsimp a) s)))) = 
       
  2451          retrieve (bders (bsimp a) s) (mkeps (erase (bders (bsimp a) s)))"
       
  2452   using assms
       
  2453   by (metis LA L_bsimp_erase bnullable_correctness ders_correctness erase_bders mkeps_nullable nullable_correctness)
       
  2454 
       
  2455 
       
  2456   
       
  2457 
       
  2458 lemma L03:
       
  2459   assumes "bnullable (bder c a)"
       
  2460   shows "retrieve (bder c (bsimp a)) (mkeps (erase (bder c (bsimp a)))) =
       
  2461          bmkeps (bsimp (bder c (bsimp a)))"
       
  2462   using assms
       
  2463   by (metis L0 L_bsimp_erase bmkeps_retrieve bnullable_correctness der_correctness erase_bder nullable_correctness)
       
  2464 
       
  2465 lemma L04:
       
  2466   assumes "bnullable (bder c a)"
       
  2467   shows "retrieve (bder c (bsimp a)) (mkeps (erase (bder c (bsimp a)))) =
       
  2468          retrieve (bsimp (bder c (bsimp a))) (mkeps (erase (bsimp (bder c (bsimp a)))))"     
       
  2469   using assms
       
  2470   by (metis L0 L_bsimp_erase bnullable_correctness der_correctness erase_bder nullable_correctness)
       
  2471     
       
  2472 lemma L05:
       
  2473   assumes "bnullable (bder c a)"
       
  2474   shows "retrieve (bder c (bsimp a)) (mkeps (erase (bder c (bsimp a)))) =
       
  2475          retrieve (bsimp (bder c (bsimp a))) (mkeps (erase (bsimp (bder c (bsimp a)))))" 
       
  2476   using assms
       
  2477   using L04 by auto 
       
  2478 
       
  2479 lemma L06:
       
  2480   assumes "bnullable (bder c a)"
       
  2481   shows "bmkeps (bder c (bsimp a)) = bmkeps (bsimp (bder c (bsimp a)))"
       
  2482   using assms
       
  2483   by (metis L03 L_bsimp_erase bmkeps_retrieve bnullable_correctness der_correctness erase_bder nullable_correctness) 
       
  2484 
       
  2485 lemma L07:
       
  2486   assumes "s \<in> L (erase r)"
       
  2487   shows "retrieve r (flex (erase r) id s (mkeps (ders s (erase r)))) 
       
  2488             = retrieve (bders r s) (mkeps (erase (bders r s)))"
       
  2489   using assms
       
  2490   using LB LC lexer_correct_Some by auto
       
  2491 
       
  2492 lemma L06_2:
       
  2493   assumes "bnullable (bders a [c,d])"
       
  2494   shows "bmkeps (bders (bsimp a) [c,d]) = bmkeps (bsimp (bders (bsimp a) [c,d]))"
       
  2495   using assms
       
  2496   apply(simp)
       
  2497   by (metis L_bsimp_erase bmkeps_simp bnullable_correctness der_correctness erase_bder nullable_correctness)
       
  2498   
       
  2499 lemma L06_bders:
       
  2500   assumes "bnullable (bders a s)"
       
  2501   shows "bmkeps (bders (bsimp a) s) = bmkeps (bsimp (bders (bsimp a) s))"
       
  2502   using assms
       
  2503   by (metis L_bsimp_erase bmkeps_simp bnullable_correctness ders_correctness erase_bders nullable_correctness)
       
  2504 
       
  2505 lemma LLLL:
       
  2506   shows "L (erase a) =  L (erase (bsimp a))"
       
  2507   and "L (erase a) = {flat v | v. \<Turnstile> v: (erase a)}"
       
  2508   and "L (erase a) = {flat v | v. \<Turnstile> v: (erase (bsimp a))}"
       
  2509   using L_bsimp_erase apply(blast)
       
  2510   apply (simp add: L_flat_Prf)
       
  2511   using L_bsimp_erase L_flat_Prf apply(auto)[1]
       
  2512   done  
       
  2513     
       
  2514 
       
  2515 
       
  2516 lemma L07XX:
       
  2517   assumes "s \<in> L (erase a)"
       
  2518   shows "s \<in> erase a \<rightarrow> flex (erase a) id s (mkeps (ders s (erase a)))"
       
  2519   using assms
       
  2520   by (meson lexer_correct_None lexer_correctness(1) lexer_flex)
       
  2521 
       
  2522 lemma LX0:
       
  2523   assumes "s \<in> L r"
       
  2524   shows "decode (bmkeps (bders (intern r) s)) r = Some(flex r id s (mkeps (ders s r)))"
       
  2525   by (metis assms blexer_correctness blexer_def lexer_correct_None lexer_flex)
       
  2526 
       
  2527 lemma L1:
       
  2528   assumes "s \<in> r \<rightarrow> v" 
       
  2529   shows "decode (bmkeps (bders (intern r) s)) r = Some v"
       
  2530   using assms
       
  2531   by (metis blexer_correctness blexer_def lexer_correctness(1) option.distinct(1))
       
  2532 
       
  2533 lemma L2:
       
  2534   assumes "s \<in> (der c r) \<rightarrow> v" 
       
  2535   shows "decode (bmkeps (bders (intern r) (c # s))) r = Some (injval r c v)"
       
  2536   using assms
       
  2537   apply(subst bmkeps_retrieve)
       
  2538   using Posix1(1) lexer_correct_None lexer_flex apply fastforce
       
  2539   using MAIN_decode
       
  2540   apply(subst MAIN_decode[symmetric])
       
  2541    apply(simp)
       
  2542    apply (meson Posix1(1) lexer_correct_None lexer_flex mkeps_nullable)
       
  2543   apply(simp)
       
  2544   apply(subgoal_tac "v = flex (der c r) id s (mkeps (ders s (der c r)))")
       
  2545    prefer 2
       
  2546    apply (metis Posix_determ lexer_correctness(1) lexer_flex option.distinct(1))
       
  2547   apply(simp)
       
  2548   apply(subgoal_tac "injval r c (flex (der c r) id s (mkeps (ders s (der c r)))) =
       
  2549     (flex (der c r) ((\<lambda>v. injval r c v) o id) s (mkeps (ders s (der c r))))")
       
  2550    apply(simp)
       
  2551   using flex_fun_apply by blast
       
  2552   
       
  2553 lemma L3:
       
  2554   assumes "s2 \<in> (ders s1 r) \<rightarrow> v" 
       
  2555   shows "decode (bmkeps (bders (intern r) (s1 @ s2))) r = Some (flex r id s1 v)"
       
  2556   using assms
       
  2557   apply(induct s1 arbitrary: r s2 v rule: rev_induct)
       
  2558    apply(simp)
       
  2559   using L1 apply blast
       
  2560   apply(simp add: ders_append)
       
  2561   apply(drule_tac x="r" in meta_spec)
       
  2562   apply(drule_tac x="x # s2" in meta_spec)
       
  2563   apply(drule_tac x="injval (ders xs r) x v" in meta_spec)
       
  2564   apply(drule meta_mp)
       
  2565    defer
       
  2566    apply(simp)
       
  2567    apply(simp add:  flex_append)
       
  2568   by (simp add: Posix_injval)
       
  2569 
       
  2570 
       
  2571 
       
  2572 lemma bders_snoc:
       
  2573   "bder c (bders a s) = bders a (s @ [c])"
       
  2574   apply(simp add: bders_append)
       
  2575   done
       
  2576 
       
  2577 
       
  2578 lemma QQ1:
       
  2579   shows "bsimp (bders (bsimp a) []) = bders_simp (bsimp a) []"
       
  2580   apply(simp)
       
  2581   apply(simp add: bsimp_idem)
       
  2582   done
       
  2583 
       
  2584 lemma QQ2:
       
  2585   shows "bsimp (bders (bsimp a) [c]) = bders_simp (bsimp a) [c]"
       
  2586   apply(simp)
       
  2587   done
       
  2588 
       
  2589 lemma XXX2a_long:
       
  2590   assumes "good a"
       
  2591   shows "bsimp (bder c (bsimp a)) = bsimp (bder c a)"
       
  2592   using  assms
       
  2593   apply(induct a arbitrary: c taking: asize rule: measure_induct)
       
  2594   apply(case_tac x)
       
  2595        apply(simp)
       
  2596       apply(simp)
       
  2597      apply(simp)
       
  2598   prefer 3
       
  2599     apply(simp)
       
  2600    apply(simp)
       
  2601    apply(auto)[1]
       
  2602 apply(case_tac "x42 = AZERO")
       
  2603      apply(simp)
       
  2604    apply(case_tac "x43 = AZERO")
       
  2605      apply(simp)
       
  2606   using test2 apply force  
       
  2607   apply(case_tac "\<exists>bs. x42 = AONE bs")
       
  2608      apply(clarify)
       
  2609      apply(simp)
       
  2610     apply(subst bsimp_ASEQ1)
       
  2611        apply(simp)
       
  2612   using b3 apply force
       
  2613   using bsimp_ASEQ0 test2 apply force
       
  2614   thm good_SEQ test2
       
  2615      apply (simp add: good_SEQ test2)
       
  2616     apply (simp add: good_SEQ test2)
       
  2617   apply(case_tac "x42 = AZERO")
       
  2618      apply(simp)
       
  2619    apply(case_tac "x43 = AZERO")
       
  2620     apply(simp)
       
  2621   apply (simp add: bsimp_ASEQ0)
       
  2622   apply(case_tac "\<exists>bs. x42 = AONE bs")
       
  2623      apply(clarify)
       
  2624      apply(simp)
       
  2625     apply(subst bsimp_ASEQ1)
       
  2626       apply(simp)
       
  2627   using bsimp_ASEQ0 test2 apply force
       
  2628      apply (simp add: good_SEQ test2)
       
  2629     apply (simp add: good_SEQ test2)
       
  2630   apply (simp add: good_SEQ test2)
       
  2631   (* AALTs case *)
       
  2632   apply(simp)
       
  2633   using test2 by fastforce
       
  2634 
       
  2635 
       
  2636 lemma bder_bsimp_AALTs:
       
  2637   shows "bder c (bsimp_AALTs bs rs) = bsimp_AALTs bs (map (bder c) rs)"
       
  2638   apply(induct bs rs rule: bsimp_AALTs.induct)
       
  2639     apply(simp)
       
  2640    apply(simp)
       
  2641    apply (simp add: bder_fuse)
       
  2642   apply(simp)
       
  2643   done
       
  2644 
       
  2645 lemma flts_nothing:
       
  2646   assumes "\<forall>r \<in> set rs. r \<noteq> AZERO" "\<forall>r \<in> set rs. nonalt r"
       
  2647   shows "flts rs = rs"
       
  2648   using assms
       
  2649   apply(induct rs rule: flts.induct)
       
  2650         apply(auto)
       
  2651   done
       
  2652 
       
  2653 lemma flts_flts:
       
  2654   assumes "\<forall>r \<in> set rs. good r"
       
  2655   shows "flts (flts rs) = flts rs"
       
  2656   using assms
       
  2657   apply(induct rs taking: "\<lambda>rs. sum_list  (map asize rs)" rule: measure_induct)
       
  2658   apply(case_tac x)
       
  2659    apply(simp)
       
  2660   apply(simp)
       
  2661   apply(case_tac a)
       
  2662        apply(simp_all  add: bder_fuse flts_append)
       
  2663   apply(subgoal_tac "\<forall>r \<in> set x52. r \<noteq> AZERO")
       
  2664    prefer 2
       
  2665   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)
       
  2666   apply(subgoal_tac "\<forall>r \<in> set x52. nonalt r")
       
  2667    prefer 2
       
  2668    apply (metis n0 nn1b test2)
       
  2669   by (metis flts_fuse flts_nothing)
       
  2670 
       
  2671 
       
  2672 lemma iii:
       
  2673   assumes "bsimp_AALTs bs rs \<noteq> AZERO"
       
  2674   shows "rs \<noteq> []"
       
  2675   using assms
       
  2676   apply(induct bs  rs rule: bsimp_AALTs.induct)
       
  2677     apply(auto)
       
  2678   done
       
  2679 
       
  2680 lemma CT1_SEQ:
       
  2681   shows "bsimp (ASEQ bs a1 a2) = bsimp (ASEQ bs (bsimp a1) (bsimp a2))"
       
  2682   apply(simp add: bsimp_idem)
       
  2683   done
       
  2684 
       
  2685 lemma CT1:
       
  2686   shows "bsimp (AALTs bs as) = bsimp (AALTs bs (map  bsimp as))"
       
  2687   apply(induct as arbitrary: bs)
       
  2688    apply(simp)
       
  2689   apply(simp)
       
  2690   by (simp add: bsimp_idem comp_def)
       
  2691   
       
  2692 lemma CT1a:
       
  2693   shows "bsimp (AALT bs a1 a2) = bsimp(AALT bs (bsimp a1) (bsimp a2))"
       
  2694   by (metis CT1 list.simps(8) list.simps(9))
       
  2695 
       
  2696 lemma WWW2:
       
  2697   shows "bsimp (bsimp_AALTs bs1 (flts (map bsimp as1))) =
       
  2698          bsimp_AALTs bs1 (flts (map bsimp as1))"
       
  2699   by (metis bsimp.simps(2) bsimp_idem)
       
  2700 
       
  2701 lemma CT1b:
       
  2702   shows "bsimp (bsimp_AALTs bs as) = bsimp (bsimp_AALTs bs (map bsimp as))"
       
  2703   apply(induct bs as rule: bsimp_AALTs.induct)
       
  2704     apply(auto simp add: bsimp_idem)
       
  2705   apply (simp add: bsimp_fuse bsimp_idem)
       
  2706   by (metis bsimp_idem comp_apply)
       
  2707   
       
  2708   
       
  2709 
       
  2710 
       
  2711 (* CT *)
       
  2712 
       
  2713 lemma CTa:
       
  2714   assumes "\<forall>r \<in> set as. nonalt r \<and> r \<noteq> AZERO"
       
  2715   shows  "flts as = as"
       
  2716   using assms
       
  2717   apply(induct as)
       
  2718    apply(simp)
       
  2719   apply(case_tac as)
       
  2720    apply(simp)
       
  2721   apply (simp add: k0b)
       
  2722   using flts_nothing by auto
       
  2723 
       
  2724 lemma CT0:
       
  2725   assumes "\<forall>r \<in> set as1. nonalt r \<and> r \<noteq> AZERO" 
       
  2726   shows "flts [bsimp_AALTs bs1 as1] =  flts (map (fuse bs1) as1)"
       
  2727   using assms CTa
       
  2728   apply(induct as1 arbitrary: bs1)
       
  2729     apply(simp)
       
  2730    apply(simp)
       
  2731   apply(case_tac as1)
       
  2732    apply(simp)
       
  2733   apply(simp)
       
  2734 proof -
       
  2735 fix a :: arexp and as1a :: "arexp list" and bs1a :: "bit list" and aa :: arexp and list :: "arexp list"
       
  2736   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)"
       
  2737   assume a2: "\<And>as. \<forall>r\<in>set as. nonalt r \<and> r \<noteq> AZERO \<Longrightarrow> flts as = as"
       
  2738   assume a3: "as1a = aa # list"
       
  2739   have "flts [a] = [a]"
       
  2740 using a1 k0b by blast
       
  2741 then show "fuse bs1a a # fuse bs1a aa # map (fuse bs1a) list = flts (fuse bs1a a # fuse bs1a aa # map (fuse bs1a) list)"
       
  2742   using a3 a2 a1 by (metis (no_types) append.left_neutral append_Cons flts_fuse k00 k0b list.simps(9))
       
  2743 qed
       
  2744   
       
  2745   
       
  2746 lemma CT01:
       
  2747   assumes "\<forall>r \<in> set as1. nonalt r \<and> r \<noteq> AZERO" "\<forall>r \<in> set as2. nonalt r \<and> r \<noteq> AZERO" 
       
  2748   shows "flts [bsimp_AALTs bs1 as1, bsimp_AALTs bs2 as2] =  flts ((map (fuse bs1) as1) @ (map (fuse bs2) as2))"
       
  2749   using assms CT0
       
  2750   by (metis k0 k00)
       
  2751   
       
  2752 
       
  2753 
       
  2754 lemma CT_exp:
       
  2755   assumes "\<forall>a \<in> set as. bsimp (bder c (bsimp a)) = bsimp (bder c a)"
       
  2756   shows "map bsimp (map (bder c) as) = map bsimp (map (bder c) (map bsimp as))"
       
  2757   using assms
       
  2758   apply(induct as)
       
  2759    apply(auto)
       
  2760   done
       
  2761 
       
  2762 lemma asize_set:
       
  2763   assumes "a \<in> set as"
       
  2764   shows "asize a < Suc (sum_list (map asize as))"
       
  2765   using assms
       
  2766   apply(induct as arbitrary: a)
       
  2767    apply(auto)
       
  2768   using le_add2 le_less_trans not_less_eq by blast
       
  2769 
       
  2770 lemma L_erase_bder_simp:
       
  2771   shows "L (erase (bsimp (bder a r))) = L (der a (erase (bsimp r)))"
       
  2772   using L_bsimp_erase der_correctness by auto
       
  2773 
       
  2774 lemma PPP0: 
       
  2775   assumes "s \<in> r \<rightarrow> v"
       
  2776   shows "(bders (intern r) s) >> code v"
       
  2777   using assms
       
  2778   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)
       
  2779 
       
  2780 thm L07 L1 LX0 Posix1(1) Posix_Prf contains6 erase_bders erase_intern lexer_correct_None lexer_flex mkeps_nullable option.inject retrieve_code
       
  2781 
       
  2782 
       
  2783 lemma PPP0_isar: 
       
  2784   assumes "s \<in> r \<rightarrow> v"
       
  2785   shows "(bders (intern r) s) >> code v"
       
  2786 proof -
       
  2787   from assms have a1: "\<Turnstile> v : r" using Posix_Prf by simp
       
  2788   
       
  2789   from assms have "s \<in> L r" using Posix1(1) by auto
       
  2790   then have "[] \<in> L (ders s r)" by (simp add: ders_correctness Ders_def)
       
  2791   then have a2: "\<Turnstile> mkeps (ders s r) : ders s r"
       
  2792     by (simp add: mkeps_nullable nullable_correctness) 
       
  2793 
       
  2794   have "retrieve (bders (intern r) s) (mkeps (ders s r)) =  
       
  2795         retrieve (intern r) (flex r id s (mkeps (ders s r)))" using a2 LA LB bder_retrieve  by simp
       
  2796   also have "... = retrieve (intern r) v"
       
  2797     using LB assms by auto 
       
  2798   also have "... = code v" using a1 by (simp add: retrieve_code) 
       
  2799   finally have "retrieve (bders (intern r) s) (mkeps (ders s r)) = code v" by simp
       
  2800   moreover
       
  2801   have "\<Turnstile> mkeps (ders s r) : erase (bders (intern r) s)" using a2 by simp 
       
  2802   then have "bders (intern r) s >> retrieve (bders (intern r) s) (mkeps (ders s r))"
       
  2803     by (rule contains6)  
       
  2804   ultimately
       
  2805   show "(bders (intern r) s) >> code v" by simp
       
  2806 qed
       
  2807 
       
  2808 lemma PPP0b: 
       
  2809   assumes "s \<in> r \<rightarrow> v"
       
  2810   shows "(intern r) >> code v"
       
  2811   using assms
       
  2812   using Posix_Prf contains2 by auto
       
  2813   
       
  2814 lemma PPP0_eq:
       
  2815   assumes "s \<in> r \<rightarrow> v"
       
  2816   shows "(intern r >> code v) = (bders (intern r) s >> code v)"
       
  2817   using assms
       
  2818   using PPP0_isar PPP0b by blast
       
  2819 
       
  2820 lemma f_cont1:
       
  2821   assumes "fuse bs1 a >> bs"
       
  2822   shows "\<exists>bs2. bs = bs1 @ bs2"
       
  2823   using assms
       
  2824   apply(induct a arbitrary: bs1 bs)
       
  2825        apply(auto elim: contains.cases)
       
  2826   done
       
  2827 
       
  2828 
       
  2829 lemma f_cont2:
       
  2830   assumes "bsimp_AALTs bs1 as >> bs"
       
  2831   shows "\<exists>bs2. bs = bs1 @ bs2"
       
  2832   using assms
       
  2833   apply(induct bs1 as arbitrary: bs rule: bsimp_AALTs.induct)
       
  2834     apply(auto elim: contains.cases f_cont1)
       
  2835   done
       
  2836 
       
  2837 lemma contains_SEQ1:
       
  2838   assumes "bsimp_ASEQ bs r1 r2 >> bsX"
       
  2839   shows "\<exists>bs1 bs2. r1 >> bs1 \<and> r2 >> bs2 \<and> bsX = bs @ bs1 @ bs2"
       
  2840   using assms
       
  2841   apply(auto)
       
  2842   apply(case_tac "r1 = AZERO")
       
  2843    apply(auto)
       
  2844   using contains.simps apply blast
       
  2845   apply(case_tac "r2 = AZERO")
       
  2846    apply(auto)
       
  2847    apply(simp add: bsimp_ASEQ0)
       
  2848   using contains.simps apply blast
       
  2849   apply(case_tac "\<exists>bsX. r1 = AONE bsX")
       
  2850    apply(auto)
       
  2851    apply(simp add: bsimp_ASEQ2)
       
  2852    apply (metis append_assoc contains.intros(1) contains49 f_cont1)
       
  2853   apply(simp add: bsimp_ASEQ1)
       
  2854   apply(erule contains.cases)
       
  2855         apply(auto)
       
  2856   done
       
  2857 
       
  2858 lemma contains59:
       
  2859   assumes "AALTs bs rs >> bs2"
       
  2860   shows "\<exists>r \<in> set rs. (fuse bs r) >> bs2"
       
  2861  using assms
       
  2862   apply(induct rs arbitrary: bs bs2)
       
  2863   apply(auto)
       
  2864    apply(erule contains.cases)
       
  2865         apply(auto)
       
  2866   apply(erule contains.cases)
       
  2867        apply(auto)
       
  2868   using contains0 by blast
       
  2869 
       
  2870 lemma contains60:
       
  2871   assumes "\<exists>r \<in> set rs. fuse bs r >> bs2"
       
  2872   shows "AALTs bs rs >> bs2"
       
  2873   using assms
       
  2874   apply(induct rs arbitrary: bs bs2)
       
  2875    apply(auto)
       
  2876   apply (metis contains3b contains49 f_cont1)
       
  2877   using contains.intros(5) f_cont1 by blast
       
  2878   
       
  2879   
       
  2880 
       
  2881 lemma contains61:
       
  2882   assumes "bsimp_AALTs bs rs >> bs2"
       
  2883   shows "\<exists>r \<in> set rs. (fuse bs r) >> bs2"
       
  2884   using assms
       
  2885   apply(induct arbitrary: bs2 rule: bsimp_AALTs.induct)
       
  2886     apply(auto)
       
  2887   using contains.simps apply blast
       
  2888   using contains59 by fastforce
       
  2889 
       
  2890 lemma contains61b:
       
  2891   assumes "bsimp_AALTs bs rs >> bs2"
       
  2892   shows "\<exists>r \<in> set (flts rs). (fuse bs r) >> bs2"
       
  2893   using assms
       
  2894   apply(induct bs rs arbitrary: bs2 rule: bsimp_AALTs.induct)
       
  2895     apply(auto)
       
  2896   using contains.simps apply blast
       
  2897   using contains51d contains61 f_cont1 apply blast
       
  2898   by (metis bsimp_AALTs.simps(3) contains52 contains61 f_cont2)
       
  2899   
       
  2900   
       
  2901 
       
  2902 lemma contains61a:
       
  2903   assumes "\<exists>r \<in> set rs. (fuse bs r) >> bs2"
       
  2904   shows "bsimp_AALTs bs rs >> bs2" 
       
  2905   using assms
       
  2906   apply(induct rs arbitrary: bs2 bs)
       
  2907    apply(auto)
       
  2908    apply (metis bsimp_AALTs.elims contains60 list.distinct(1) list.inject list.set_intros(1))
       
  2909   by (metis append_Cons append_Nil contains50 f_cont2)
       
  2910 
       
  2911 lemma contains62:
       
  2912   assumes "bsimp_AALTs bs (rs1 @ rs2) >> bs2"
       
  2913   shows "bsimp_AALTs bs rs1 >> bs2 \<or> bsimp_AALTs bs rs2 >> bs2"
       
  2914   using assms
       
  2915   apply -
       
  2916   apply(drule contains61)
       
  2917   apply(auto)
       
  2918    apply(case_tac rs1)
       
  2919     apply(auto)
       
  2920   apply(case_tac list)
       
  2921      apply(auto)
       
  2922   apply (simp add: contains60)
       
  2923    apply(case_tac list)
       
  2924     apply(auto)
       
  2925   apply (simp add: contains60)
       
  2926   apply (meson contains60 list.set_intros(2))
       
  2927    apply(case_tac rs2)
       
  2928     apply(auto)
       
  2929   apply(case_tac list)
       
  2930      apply(auto)
       
  2931   apply (simp add: contains60)
       
  2932    apply(case_tac list)
       
  2933     apply(auto)
       
  2934   apply (simp add: contains60)
       
  2935   apply (meson contains60 list.set_intros(2))
       
  2936   done
       
  2937 
       
  2938 lemma contains63:
       
  2939   assumes "AALTs bs (map (fuse bs1) rs) >> bs3"
       
  2940   shows "AALTs (bs @ bs1) rs >> bs3"
       
  2941   using assms
       
  2942   apply(induct rs arbitrary: bs bs1 bs3)
       
  2943    apply(auto elim: contains.cases)
       
  2944     apply(erule contains.cases)
       
  2945         apply(auto)
       
  2946   apply (simp add: contains0 contains60 fuse_append)
       
  2947   by (metis contains.intros(5) contains59 f_cont1)
       
  2948     
       
  2949 lemma contains64:
       
  2950   assumes "bsimp_AALTs bs (flts rs1 @ flts rs2) >> bs2" "\<forall>r \<in> set rs2. \<not> fuse bs r >> bs2"
       
  2951   shows "bsimp_AALTs bs (flts rs1) >> bs2"
       
  2952   using assms
       
  2953   apply(induct rs2 arbitrary: rs1 bs bs2)
       
  2954    apply(auto)
       
  2955   apply(drule_tac x="rs1" in meta_spec)
       
  2956     apply(drule_tac x="bs" in meta_spec)
       
  2957   apply(drule_tac x="bs2" in meta_spec)
       
  2958   apply(drule meta_mp)
       
  2959    apply(drule contains61)
       
  2960    apply(auto)
       
  2961   using contains51b contains61a f_cont1 apply blast
       
  2962   apply(subst (asm) k0)
       
  2963   apply(auto)
       
  2964    prefer 2
       
  2965   using contains50 contains61a f_cont1 apply blast
       
  2966   apply(case_tac a)
       
  2967        apply(auto)
       
  2968   by (metis contains60 fuse_append)
       
  2969   
       
  2970   
       
  2971 
       
  2972 lemma contains65:
       
  2973   assumes "bsimp_AALTs bs (flts rs) >> bs2"
       
  2974   shows "\<exists>r \<in> set rs. (fuse bs r) >> bs2"
       
  2975   using assms
       
  2976   apply(induct rs arbitrary: bs bs2 taking: "\<lambda>rs. sum_list (map asize rs)" rule: measure_induct)
       
  2977   apply(case_tac x)
       
  2978         apply(auto elim: contains.cases)
       
  2979   apply(case_tac list)
       
  2980    apply(auto elim: contains.cases)
       
  2981    apply(case_tac a)
       
  2982         apply(auto elim: contains.cases)
       
  2983    apply(drule contains61)
       
  2984    apply(auto)
       
  2985    apply (metis contains60 fuse_append)
       
  2986   apply(case_tac lista)
       
  2987    apply(auto elim: contains.cases)
       
  2988    apply(subst (asm) k0)
       
  2989    apply(drule contains62)
       
  2990    apply(auto)
       
  2991    apply(case_tac a)
       
  2992          apply(auto elim: contains.cases)
       
  2993    apply(case_tac x52)
       
  2994    apply(auto elim: contains.cases)
       
  2995   apply(case_tac list)
       
  2996    apply(auto elim: contains.cases)
       
  2997   apply (simp add: contains60 fuse_append)
       
  2998    apply(erule contains.cases)
       
  2999           apply(auto)
       
  3000      apply (metis append.left_neutral contains0 contains60 fuse.simps(4) in_set_conv_decomp)
       
  3001   apply(erule contains.cases)
       
  3002           apply(auto)
       
  3003      apply (metis contains0 contains60 fuse.simps(4) list.set_intros(1) list.set_intros(2))
       
  3004   apply (simp add: contains.intros(5) contains63)
       
  3005    apply(case_tac aa)
       
  3006         apply(auto)
       
  3007   apply (meson contains60 contains61 contains63)
       
  3008   apply(subst (asm) k0)
       
  3009   apply(drule contains64)
       
  3010    apply(auto)[1]
       
  3011   by (metis append_Nil2 bsimp_AALTs.simps(2) contains50 contains61a contains64 f_cont2 flts.simps(1))
       
  3012 
       
  3013 
       
  3014 lemma contains55a:
       
  3015   assumes "bsimp r >> bs"
       
  3016   shows "r >> bs"
       
  3017   using assms
       
  3018   apply(induct r arbitrary: bs)
       
  3019        apply(auto)
       
  3020    apply(frule contains_SEQ1)
       
  3021   apply(auto)
       
  3022    apply (simp add: contains.intros(3))
       
  3023   apply(frule f_cont2)
       
  3024   apply(auto) 
       
  3025   apply(drule contains65)
       
  3026   apply(auto)
       
  3027   using contains0 contains49 contains60 by blast
       
  3028 
       
  3029 
       
  3030 lemma PPP1_eq:
       
  3031   shows "bsimp r >> bs \<longleftrightarrow> r >> bs"
       
  3032   using contains55 contains55a by blast
       
  3033 
       
  3034 lemma retrieve_code_bder:
       
  3035   assumes "\<Turnstile> v : der c r"
       
  3036   shows "code (injval r c v) = retrieve (bder c (intern r)) v"
       
  3037   using assms
       
  3038   by (simp add: Prf_injval bder_retrieve retrieve_code)
       
  3039 
       
  3040 lemma Etrans:
       
  3041   assumes "a >> s" "s = t" 
       
  3042   shows "a >> t"
       
  3043   using assms by simp
       
  3044 
       
  3045 
       
  3046 
       
  3047 lemma retrieve_code_bders:
       
  3048   assumes "\<Turnstile> v : ders s r"
       
  3049   shows "code (flex r id s v) = retrieve (bders (intern r) s) v"
       
  3050   using assms
       
  3051   apply(induct s arbitrary: v r rule: rev_induct)
       
  3052    apply(auto simp add: ders_append flex_append bders_append)
       
  3053   apply (simp add: retrieve_code)
       
  3054   apply(frule Prf_injval)
       
  3055   apply(drule_tac meta_spec)+
       
  3056   apply(drule meta_mp)
       
  3057    apply(assumption)
       
  3058   apply(simp)
       
  3059   apply(subst bder_retrieve)
       
  3060    apply(simp)
       
  3061   apply(simp)
       
  3062   done
       
  3063 
       
  3064 lemma contains70:
       
  3065  assumes "s \<in> L(r)"
       
  3066  shows "bders (intern r) s >> code (flex r id s (mkeps (ders s r)))"
       
  3067   apply(subst PPP0_eq[symmetric])
       
  3068    apply (meson assms lexer_correct_None lexer_correctness(1) lexer_flex)
       
  3069   by (metis L07XX PPP0b assms erase_intern)
       
  3070 
       
  3071 
       
  3072 lemma contains_equiv_def:
       
  3073   shows " (AALTs bs as >> bs@bs1) \<longleftrightarrow> (\<exists>a\<in>set as. a >> bs1)"
       
  3074   by (meson contains0 contains49 contains59 contains60)
       
  3075 
       
  3076 lemma i_know_it_must_be_a_theorem_but_dunno_its_name:
       
  3077   assumes "a \<and> (a=b) "
       
  3078   shows"b"
       
  3079   using assms
       
  3080   apply -
       
  3081   by auto
       
  3082 
       
  3083 lemma concat_def:
       
  3084   shows"[]@lst=lst"
       
  3085   apply auto
       
  3086   done
       
  3087 
       
  3088 lemma derc_alt00:
       
  3089   assumes " bder c a >> bs" and "bder c (bsimp a) >> bs"
       
  3090   shows "bder c (bsimp_AALTs [] (flts [bsimp a])) >> bs"
       
  3091   using assms
       
  3092   apply -
       
  3093   apply(case_tac "bsimp a")
       
  3094   prefer 6
       
  3095        apply(simp)+
       
  3096   apply(subst bder_bsimp_AALTs)
       
  3097   by (metis append_Nil contains51c map_bder_fuse map_map)
       
  3098 lemma derc_alt01:
       
  3099   shows "\<And>a list1 list2.
       
  3100        \<lbrakk> bder c (bsimp a) >> bs ;
       
  3101         bder c a >> bs; as = [a] @ list2; flts (map bsimp list1) = [];
       
  3102         flts (map bsimp list2) \<noteq> []\<rbrakk>
       
  3103        \<Longrightarrow> bder c (bsimp_AALTs [] (flts [bsimp a] @ flts (map bsimp list2))) >> bs"
       
  3104   using bder_bsimp_AALTs contains51b derc_alt00 f_cont2 by fastforce
       
  3105 
       
  3106 lemma derc_alt10:
       
  3107   shows "\<And>a list1 list2.
       
  3108        \<lbrakk>  a \<in> set as; bder c (bsimp a) >> bs;
       
  3109         bder c a >> bs; as = list1 @ [a] @ list2; flts (map bsimp list1) \<noteq> [];
       
  3110 flts(map bsimp list2) = []\<rbrakk>
       
  3111        \<Longrightarrow> bder c (bsimp_AALTs []
       
  3112               (flts (map bsimp list1) @
       
  3113                flts (map bsimp [a]) @ flts (map bsimp list2))) >> bs"
       
  3114   apply(subst bder_bsimp_AALTs)
       
  3115   apply simp
       
  3116   using bder_bsimp_AALTs contains50 derc_alt00 f_cont2 by fastforce
       
  3117 
       
  3118 
       
  3119 (*QUESTION*)
       
  3120 lemma derc_alt:
       
  3121   assumes "bder c (AALTs [] as) >> bs" 
       
  3122    and "\<forall>a \<in> set as. ((bder c a >> bs) \<longrightarrow> (bder c (bsimp a) >> bs))"
       
  3123   shows "bder c (bsimp(AALTs [] as)) >> bs"
       
  3124   using assms
       
  3125   apply -
       
  3126   using contains_equiv_def
       
  3127   apply -
       
  3128   apply(simp add: bder.simps)
       
  3129   apply(drule_tac  x="[]" in meta_spec)
       
  3130   apply(drule_tac x="map (bder c) as" in meta_spec)
       
  3131   apply(drule_tac x="bs" in meta_spec)
       
  3132   
       
  3133   apply(simp add:List.append.simps(1))
       
  3134   apply(erule bexE)
       
  3135   apply(subgoal_tac "\<exists>list1 list2. as = list1 @ [a] @ list2")
       
  3136    prefer 2
       
  3137   using split_list_last apply fastforce
       
  3138   apply(erule exE)+
       
  3139   apply(rule_tac t="as" and  s="list1@[a]@list2" in subst)
       
  3140    apply simp
       
  3141   (*find_theorems "map _ _ = _"*)
       
  3142   apply(subst map_append)+
       
  3143   apply(subst k00)+
       
  3144   apply(case_tac "flts (map bsimp list1) = Nil")
       
  3145   apply(case_tac "flts (map bsimp list2) = Nil")
       
  3146   apply simp
       
  3147   using derc_alt00 apply blast
       
  3148   apply simp
       
  3149   using derc_alt01 apply blast
       
  3150   apply(case_tac "flts (map bsimp list2) = Nil")
       
  3151   using derc_alt10 apply blast
       
  3152   by (smt bder_bsimp_AALTs contains50 contains51b derc_alt00 f_cont2 list.simps(8) list.simps(9) map_append)
       
  3153 
       
  3154   (*find_theorems "flts _ = _ "*)
       
  3155 (*  (*HERE*)
       
  3156   apply(drule  i_know_it_must_be_a_theorem_but_dunno_its_name)
       
  3157 *)
       
  3158 
       
  3159 lemma transfer:
       
  3160   assumes " (a \<Rightarrow> c) \<and> (c \<Rightarrow> b)"
       
  3161   shows "a \<Rightarrow> b"
       
  3162   
       
  3163 (*if we have proved that a can prove c, to prove a can prove b, we
       
  3164 then have the option to just show that c can prove b *)
       
  3165 (*how to express the above using drule+mp and a lemma*)
       
  3166 
       
  3167 definition FC where
       
  3168  "FC a s v = retrieve a (flex (erase a) id s v)"
       
  3169 
       
  3170 definition FE where
       
  3171  "FE a s = retrieve a (flex (erase a) id s (mkeps (ders s (erase a))))"
       
  3172 
       
  3173 definition PV where
       
  3174   "PV r s v = flex r id s v"
       
  3175 
       
  3176 definition PX where
       
  3177   "PX r s = PV r s (mkeps (ders s r))"
       
  3178 
       
  3179 lemma FE_PX:
       
  3180   shows "FE r s = retrieve r (PX (erase r) s)"
       
  3181   unfolding FE_def PX_def PV_def by(simp)
       
  3182 
       
  3183 lemma FE_PX_code:
       
  3184   assumes "s \<in> L r"
       
  3185   shows "FE (intern r) s = code (PX r s)"
       
  3186   unfolding FE_def PX_def PV_def 
       
  3187   using assms
       
  3188   by (metis L07XX Posix_Prf erase_intern retrieve_code)
       
  3189   
       
  3190 
       
  3191 lemma PV_id[simp]:
       
  3192   shows "PV r [] v = v"
       
  3193   by (simp add: PV_def)
       
  3194 
       
  3195 lemma PX_id[simp]:
       
  3196   shows "PX r [] = mkeps r"
       
  3197   by (simp add: PX_def)
       
  3198 
       
  3199 lemma PV_cons:
       
  3200   shows "PV r (c # s) v = injval r c (PV (der c r) s v)"
       
  3201   apply(simp add: PV_def flex_fun_apply)
       
  3202   done
       
  3203 
       
  3204 lemma PX_cons:
       
  3205   shows "PX r (c # s) = injval r c (PX (der c r) s)"
       
  3206   apply(simp add: PX_def PV_cons)
       
  3207   done
       
  3208 
       
  3209 lemma PV_append:
       
  3210   shows "PV r (s1 @ s2) v = PV r s1 (PV (ders s1 r) s2 v)"
       
  3211   apply(simp add: PV_def flex_append)
       
  3212   by (simp add: flex_fun_apply2)
       
  3213   
       
  3214 lemma PX_append:
       
  3215   shows "PX r (s1 @ s2) = PV r s1 (PX (ders s1 r) s2)"
       
  3216   by (simp add: PV_append PX_def ders_append)
       
  3217 
       
  3218 lemma code_PV0: 
       
  3219   shows "PV r (c # s) v = injval r c (PV (der c r) s v)"
       
  3220   unfolding PX_def PV_def
       
  3221   apply(simp)
       
  3222   by (simp add: flex_injval)
       
  3223 
       
  3224 lemma code_PX0: 
       
  3225   shows "PX r (c # s) = injval r c (PX (der c r) s)"
       
  3226   unfolding PX_def
       
  3227   apply(simp add: code_PV0)
       
  3228   done  
       
  3229 
       
  3230 lemma Prf_PV:
       
  3231   assumes "\<Turnstile> v : ders s r"
       
  3232   shows "\<Turnstile> PV r s v : r"
       
  3233   using assms unfolding PX_def PV_def
       
  3234   apply(induct s arbitrary: v r)
       
  3235    apply(simp)
       
  3236   apply(simp)
       
  3237   by (simp add: Prf_injval flex_injval)
       
  3238   
       
  3239 
       
  3240 lemma Prf_PX:
       
  3241   assumes "s \<in> L r"
       
  3242   shows "\<Turnstile> PX r s : r"
       
  3243   using assms unfolding PX_def PV_def
       
  3244   using L1 LX0 Posix_Prf lexer_correct_Some by fastforce
       
  3245 
       
  3246 lemma PV1: 
       
  3247   assumes "\<Turnstile> v : ders s r"
       
  3248   shows "(intern r) >> code (PV r s v)"
       
  3249   using assms
       
  3250   by (simp add: Prf_PV contains2)
       
  3251 
       
  3252 lemma PX1: 
       
  3253   assumes "s \<in> L r"
       
  3254   shows "(intern r) >> code (PX r s)"
       
  3255   using assms
       
  3256   by (simp add: Prf_PX contains2)
       
  3257 
       
  3258 lemma PX2: 
       
  3259   assumes "s \<in> L (der c r)"
       
  3260   shows "bder c (intern r) >> code (injval r c (PX (der c r) s))"
       
  3261   using assms
       
  3262   by (simp add: Prf_PX contains6 retrieve_code_bder)
       
  3263 
       
  3264 lemma PX2a: 
       
  3265   assumes "c # s \<in> L r"
       
  3266   shows "bder c (intern r) >> code (injval r c (PX (der c r) s))"
       
  3267   using assms
       
  3268   using PX2 lexer_correct_None by force
       
  3269 
       
  3270 lemma PX2b: 
       
  3271   assumes "c # s \<in> L r"
       
  3272   shows "bder c (intern r) >> code (PX r (c # s))"
       
  3273   using assms unfolding PX_def PV_def
       
  3274   by (metis Der_def L07XX PV_def PX2a PX_def Posix_determ Posix_injval der_correctness erase_intern mem_Collect_eq)
       
  3275     
       
  3276 lemma PV3: 
       
  3277   assumes "\<Turnstile> v : ders s r"
       
  3278   shows "bders (intern r) s >> code (PV r s v)"
       
  3279   using assms
       
  3280   using PX_def PV_def contains70
       
  3281   by (simp add: contains6 retrieve_code_bders)
       
  3282   
       
  3283 lemma PX3: 
       
  3284   assumes "s \<in> L r"
       
  3285   shows "bders (intern r) s >> code (PX r s)"
       
  3286   using assms
       
  3287   using PX_def PV_def contains70 by auto
       
  3288 
       
  3289 
       
  3290 lemma PV_bders_iff:
       
  3291   assumes "\<Turnstile> v : ders s r"
       
  3292   shows "bders (intern r) s >> code (PV r s v) \<longleftrightarrow> (intern r) >> code (PV r s v)"
       
  3293   by (simp add: PV1 PV3 assms)
       
  3294   
       
  3295 lemma PX_bders_iff:
       
  3296   assumes "s \<in> L r"
       
  3297   shows "bders (intern r) s >> code (PX r s) \<longleftrightarrow> (intern r) >> code (PX r s)"
       
  3298   by (simp add: PX1 PX3 assms)
       
  3299 
       
  3300 lemma PX4: 
       
  3301   assumes "(s1 @ s2) \<in> L r"
       
  3302   shows "bders (intern r) (s1 @ s2) >> code (PX r (s1 @ s2))"
       
  3303   using assms
       
  3304   by (simp add: PX3)
       
  3305 
       
  3306 lemma PX_bders_iff2: 
       
  3307   assumes "(s1 @ s2) \<in> L r"
       
  3308   shows "bders (intern r) (s1 @ s2) >> code (PX r (s1 @ s2)) \<longleftrightarrow>
       
  3309          (intern r) >> code (PX r (s1 @ s2))"
       
  3310   by (simp add: PX1 PX3 assms)
       
  3311 
       
  3312 lemma PV_bders_iff3: 
       
  3313   assumes "\<Turnstile> v : ders (s1 @ s2) r"
       
  3314   shows "bders (intern r) (s1 @ s2) >> code (PV r (s1 @ s2) v) \<longleftrightarrow>
       
  3315          bders (intern r) s1 >> code (PV r (s1 @ s2) v)"
       
  3316   by (metis PV3 PV_append Prf_PV assms ders_append)
       
  3317 
       
  3318 
       
  3319 
       
  3320 lemma PX_bders_iff3: 
       
  3321   assumes "(s1 @ s2) \<in> L r"
       
  3322   shows "bders (intern r) (s1 @ s2) >> code (PX r (s1 @ s2)) \<longleftrightarrow>
       
  3323          bders (intern r) s1 >> code (PX r (s1 @ s2))"
       
  3324   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)
       
  3325 
       
  3326 lemma PV_bder_iff: 
       
  3327   assumes "\<Turnstile> v : ders (s1 @ [c]) r"
       
  3328   shows "bder c (bders (intern r) s1) >> code (PV r (s1 @ [c]) v) \<longleftrightarrow>
       
  3329          bders (intern r) s1 >> code (PV r (s1 @ [c]) v)"
       
  3330   by (simp add: PV_bders_iff3 assms bders_snoc)
       
  3331   
       
  3332 lemma PV_bder_IFF: 
       
  3333   assumes "\<Turnstile> v : ders (s1 @ c # s2) r"
       
  3334   shows "bder c (bders (intern r) s1) >> code (PV r (s1 @ c # s2) v) \<longleftrightarrow>
       
  3335          bders (intern r) s1 >> code (PV r (s1 @ c # s2) v)"
       
  3336   by (metis LA PV3 PV_def Prf_PV assms bders_append code_PV0 contains7 ders.simps(2) erase_bders erase_intern retrieve_code_bders)
       
  3337     
       
  3338 
       
  3339 lemma PX_bder_iff: 
       
  3340   assumes "(s1 @ [c]) \<in> L r"
       
  3341   shows "bder c (bders (intern r) s1) >> code (PX r (s1 @ [c])) \<longleftrightarrow>
       
  3342          bders (intern r) s1 >> code (PX r (s1 @ [c]))"
       
  3343   by (simp add: PX_bders_iff3 assms bders_snoc)
       
  3344 
       
  3345 lemma PV_bder_iff2: 
       
  3346   assumes "\<Turnstile> v : ders (c # s1) r"
       
  3347   shows "bders (bder c (intern r)) s1 >> code (PV r (c # s1) v) \<longleftrightarrow>
       
  3348          bder c (intern r) >> code (PV r (c # s1) v)"
       
  3349   by (metis PV3 Prf_PV assms bders.simps(2) code_PV0 contains7 ders.simps(2) erase_intern retrieve_code)
       
  3350   
       
  3351 
       
  3352 lemma PX_bder_iff2: 
       
  3353   assumes "(c # s1) \<in> L r"
       
  3354   shows "bders (bder c (intern r)) s1 >> code (PX r (c # s1)) \<longleftrightarrow>
       
  3355          bder c (intern r) >> code (PX r (c # s1))"
       
  3356   using PX2b PX3 assms by force
       
  3357 
       
  3358 
       
  3359 lemma FC_id:
       
  3360   shows "FC r [] v = retrieve r v"
       
  3361   by (simp add: FC_def)
       
  3362 
       
  3363 lemma FC_char:
       
  3364   shows "FC r [c] v = retrieve r (injval (erase r) c v)"
       
  3365   by (simp add: FC_def)
       
  3366 
       
  3367 lemma FC_char2:
       
  3368   assumes "\<Turnstile> v : der c (erase r)"
       
  3369   shows "FC r [c] v = FC (bder c r) [] v"
       
  3370   using assms
       
  3371   by (simp add: FC_char FC_id bder_retrieve)
       
  3372   
       
  3373 
       
  3374 lemma FC_bders_iff:
       
  3375   assumes "\<Turnstile> v : ders s (erase r)"
       
  3376   shows "bders r s >> FC r s v \<longleftrightarrow> r >> FC r s v"
       
  3377   unfolding FC_def
       
  3378   by (simp add: assms contains8_iff)
       
  3379 
       
  3380 
       
  3381 lemma FC_bder_iff:
       
  3382   assumes "\<Turnstile> v : der c (erase r)"
       
  3383   shows "bder c r >> FC r [c] v \<longleftrightarrow> r >> FC r [c] v"
       
  3384   apply(subst FC_bders_iff[symmetric])
       
  3385    apply(simp add: assms)
       
  3386   apply(simp)
       
  3387   done
       
  3388 
       
  3389 lemma FC_bnullable0:
       
  3390   assumes "bnullable r"
       
  3391   shows "FC r [] (mkeps (erase r)) = FC (bsimp r) [] (mkeps (erase (bsimp r)))"
       
  3392   unfolding FC_def 
       
  3393   by (simp add: L0 assms)
       
  3394 
       
  3395 
       
  3396 lemma FC_nullable2:
       
  3397   assumes "bnullable (bders a s)"
       
  3398   shows "FC (bsimp a) s (mkeps (erase (bders (bsimp a) s))) = 
       
  3399          FC (bders (bsimp a) s) [] (mkeps (erase (bders (bsimp a) s)))"
       
  3400   unfolding FC_def
       
  3401   using L02_bders assms by auto
       
  3402 
       
  3403 lemma FC_nullable3:
       
  3404   assumes "bnullable (bders a s)"
       
  3405   shows "FC a s (mkeps (erase (bders a s))) = 
       
  3406          FC (bders a s) [] (mkeps (erase (bders a s)))"
       
  3407   unfolding FC_def
       
  3408   using LA assms bnullable_correctness mkeps_nullable by fastforce
       
  3409 
       
  3410 
       
  3411 lemma FE_contains0:
       
  3412   assumes "bnullable r"
       
  3413   shows "r >> FE r []"
       
  3414   by (simp add: FE_def assms bnullable_correctness contains6 mkeps_nullable)
       
  3415 
       
  3416 lemma FE_contains1:
       
  3417   assumes "bnullable (bders r s)"
       
  3418   shows "r >> FE r s"
       
  3419   by (metis FE_def Prf_flex assms bnullable_correctness contains6 erase_bders mkeps_nullable)
       
  3420 
       
  3421 lemma FE_bnullable0:
       
  3422   assumes "bnullable r"
       
  3423   shows "FE r [] = FE (bsimp r) []"
       
  3424   unfolding FE_def 
       
  3425   by (simp add: L0 assms)
       
  3426 
       
  3427 
       
  3428 lemma FE_nullable1:
       
  3429   assumes "bnullable (bders r s)"
       
  3430   shows "FE r s = FE (bders r s) []"
       
  3431   unfolding FE_def
       
  3432   using LA assms bnullable_correctness mkeps_nullable by fastforce
       
  3433 
       
  3434 lemma FE_contains2:
       
  3435   assumes "bnullable (bders r s)"
       
  3436   shows "r >> FE (bders r s) []"
       
  3437   by (metis FE_contains1 FE_nullable1 assms)
       
  3438 
       
  3439 lemma FE_contains3:
       
  3440   assumes "bnullable (bder c r)"
       
  3441   shows "r >> FE (bsimp (bder c r)) []"
       
  3442   by (metis FE_def L0 assms bder_retrieve bders.simps(1) bnullable_correctness contains7a erase_bder erase_bders flex.simps(1) id_apply mkeps_nullable)
       
  3443 
       
  3444 lemma FE_contains4:
       
  3445   assumes "bnullable (bders r s)"
       
  3446   shows "r >> FE (bsimp (bders r s)) []"
       
  3447   using FE_bnullable0 FE_contains2 assms by auto
       
  3448   
       
  3449 
       
  3450 (*
       
  3451 lemma FE_bnullable2:
       
  3452   assumes "bnullable (bder c r)"
       
  3453   shows "FE r [c] = FE (bsimp r) [c]"
       
  3454   unfolding FE_def
       
  3455   apply(simp)
       
  3456   using L0
       
  3457 
       
  3458   apply(subst FE_nullable1)
       
  3459   using assms apply(simp)
       
  3460   apply(subst FE_bnullable0)
       
  3461   using assms apply(simp)
       
  3462   unfolding FE_def
       
  3463   apply(simp)
       
  3464   apply(subst L0)
       
  3465   using assms apply(simp)
       
  3466   apply(subst bder_retrieve[symmetric])
       
  3467   using LLLL(1) L_erase_bder_simp assms bnullable_correctness mkeps_nullable nullable_correctness apply b last
       
  3468   apply(simp)
       
  3469   find_theorems "retrieve _ (injval _ _ _)"
       
  3470   find_theorems "retrieve (bsimp _) _"
       
  3471 
       
  3472   lemma FE_nullable3:
       
  3473   assumes "bnullable (bders a s)"
       
  3474   shows "FE (bsimp a) s = FE a s"
       
  3475   
       
  3476   unfolding FE_def
       
  3477   using LA assms bnullable_correctness mkeps_nullable by fas tforce
       
  3478 *)
       
  3479 
       
  3480 
       
  3481 lemma FC4:
       
  3482   assumes "\<Turnstile> v : ders s (erase a)"
       
  3483   shows "FC a s v = FC (bders a s) [] v"
       
  3484   unfolding FC_def by (simp add: LA assms)
       
  3485 
       
  3486 lemma FC5:
       
  3487   assumes "nullable (erase a)"
       
  3488   shows "FC a [] (mkeps (erase a)) = FC (bsimp a) [] (mkeps (erase (bsimp a)))"
       
  3489   unfolding FC_def
       
  3490   using L0 assms bnullable_correctness by auto 
       
  3491 
       
  3492 
       
  3493 lemma FC6:
       
  3494   assumes "nullable (erase (bders a s))"
       
  3495   shows "FC (bsimp a) s (mkeps (erase (bders (bsimp a) s))) = FC a s (mkeps (erase (bders a s)))"
       
  3496   apply(subst (2) FC4)
       
  3497   using assms mkeps_nullable apply auto[1]
       
  3498   apply(subst FC_nullable2)
       
  3499   using assms bnullable_correctness apply blast
       
  3500   oops
       
  3501 (*
       
  3502 lemma FC_bnullable:
       
  3503   assumes "bnullable (bders r s)"
       
  3504   shows "FC r s (mkeps (erase r)) = FC (bsimp r) s (mkeps (erase (bsimp r)))"
       
  3505   using assms
       
  3506   unfolding FC_def
       
  3507   using L0 L0a bder_retrieve L02_bders L04
       
  3508 
       
  3509   apply(induct s arbitrary: r)
       
  3510    apply(simp add: FC_id)
       
  3511   apply (simp add: L0 assms)
       
  3512   apply(simp add: bders_append)
       
  3513   apply(drule_tac x="bder a r" in meta_spec)
       
  3514   apply(drule meta_mp)
       
  3515    apply(simp)
       
  3516 
       
  3517   apply(subst bder_retrieve[symmetric])
       
  3518   apply(simp)
       
  3519 *)
       
  3520 
       
  3521 
       
  3522 lemma FC_bnullable:
       
  3523   assumes "bnullable (bders r s)"
       
  3524   shows "FC r s (mkeps (ders s (erase r))) = FC (bsimp r) s (mkeps (ders s (erase (bsimp r))))"
       
  3525   unfolding FC_def
       
  3526   oops
       
  3527 
       
  3528 lemma AA0:
       
  3529   assumes "bnullable (bders r s)"
       
  3530   assumes "bders r s >> FC r s (mkeps (erase (bders r s)))"
       
  3531   shows "bders (bsimp r) s >> FC (bsimp r) s (mkeps (erase (bders (bsimp r) s)))"
       
  3532   using assms
       
  3533   apply(subst (asm) FC_bders_iff)
       
  3534    apply(simp)
       
  3535   using bnullable_correctness mkeps_nullable apply fastforce
       
  3536   apply(subst FC_bders_iff)
       
  3537    apply(simp)
       
  3538   apply (metis LLLL(1) bnullable_correctness ders_correctness erase_bders mkeps_nullable nullable_correctness)
       
  3539   apply(simp add: PPP1_eq)
       
  3540   unfolding FC_def
       
  3541   find_theorems "retrieve (bsimp _) _"
       
  3542   using contains7b
       
  3543   oops
       
  3544 
       
  3545 
       
  3546 lemma AA1:
       
  3547   
       
  3548   assumes "\<Turnstile> v : der c (erase r)" "\<Turnstile> v : der c (erase (bsimp r))" 
       
  3549   assumes "bder c r >> FC r [c] v"
       
  3550   shows "bder c (bsimp r) >> FC (bsimp r) [c] v"
       
  3551   using assms
       
  3552   apply(subst (asm) FC_bder_iff)
       
  3553   apply(rule assms)
       
  3554   apply(subst FC_bder_iff)
       
  3555    apply(rule assms)
       
  3556   apply(simp add: PPP1_eq)
       
  3557   unfolding FC_def
       
  3558   find_theorems "retrieve (bsimp _) _"
       
  3559   using contains7b
       
  3560   oops
       
  3561 
       
  3562   
       
  3563 lemma PX_bder_simp_iff: 
       
  3564   assumes "\<Turnstile> v: ders (s1 @ s2) r"
       
  3565   shows "bders (bsimp (bders (intern r) s1)) s2 >> code (PV r (s1 @ s2) v) \<longleftrightarrow>
       
  3566          bders (intern r) s1 >> code (PV r (s1 @ s2) v)"
       
  3567   using assms 
       
  3568   apply(induct s2 arbitrary: r s1 v)
       
  3569    apply(simp)
       
  3570   apply (simp add: PV3 contains55)
       
  3571   apply(drule_tac x="r" in meta_spec)
       
  3572   apply(drule_tac x="s1 @ [a]" in meta_spec)
       
  3573   apply(drule_tac x="v" in meta_spec)
       
  3574   apply(simp)
       
  3575   apply(simp add: bders_append)
       
  3576   apply(subst (asm) PV_bder_IFF)
       
  3577   oops
       
  3578 
       
  3579 lemma in1:
       
  3580   assumes "AALTs bsX rsX \<in> set rs"
       
  3581   shows "\<forall>r \<in> set rsX. fuse bsX r \<in> set (flts rs)"
       
  3582   using assms
       
  3583   apply(induct rs arbitrary: bsX rsX)
       
  3584    apply(auto)
       
  3585   by (metis append_assoc in_set_conv_decomp k0)
       
  3586 
       
  3587 lemma in2a:
       
  3588   assumes "nonnested (bsimp r)" "\<not>nonalt(bsimp r)" 
       
  3589   shows "(\<exists>bsX rsX. r = AALTs bsX rsX) \<or> (\<exists>bsX rX1 rX2. r = ASEQ bsX rX1 rX2 \<and> bnullable rX1)"
       
  3590   using assms
       
  3591   apply(induct r)
       
  3592        apply(auto)
       
  3593   by (metis arexp.distinct(25) b3 bnullable.simps(2) bsimp_ASEQ.simps(1) bsimp_ASEQ0 bsimp_ASEQ1 nonalt.elims(3) nonalt.simps(2))
       
  3594   
       
  3595 
       
  3596 lemma in2:
       
  3597   assumes "bder c r >> bs2" and
       
  3598           "AALTs bsX rsX = bsimp r" and
       
  3599           "XX \<in> set rsX" "nonnested (bsimp r)"
       
  3600         shows "bder c (fuse bsX XX) >> bs2"
       
  3601 
       
  3602   sorry  
       
  3603 
       
  3604 
       
  3605 lemma
       
  3606   assumes "bder c a >> bs"
       
  3607   shows "bder c (bsimp a) >> bs"
       
  3608   using assms
       
  3609   apply(induct a arbitrary: c bs)
       
  3610        apply(auto elim: contains.cases)
       
  3611    apply(case_tac "bnullable a1")
       
  3612     apply(simp)
       
  3613   prefer 2
       
  3614     apply(simp)
       
  3615     apply(erule contains.cases)
       
  3616           apply(auto)
       
  3617     apply(case_tac "(bsimp a1) = AZERO")
       
  3618      apply(simp)
       
  3619      apply (metis append_Nil2 contains0 contains49 fuse.simps(1))
       
  3620    apply(case_tac "(bsimp a2a) = AZERO")
       
  3621      apply(simp)
       
  3622   apply (metis bder.simps(1) bsimp.simps(1) bsimp_ASEQ0 contains.intros(3) contains55)
       
  3623     apply(case_tac "\<exists>bsX. (bsimp a1) = AONE bsX")
       
  3624      apply(auto)[1]
       
  3625   using b3 apply fastforce
       
  3626     apply(subst bsimp_ASEQ1)
       
  3627   apply(auto)[3]
       
  3628     apply(simp)
       
  3629     apply(subgoal_tac  "\<not> bnullable (bsimp a1)")
       
  3630      prefer 2
       
  3631   using b3 apply blast
       
  3632     apply(simp)
       
  3633     apply (simp add: contains.intros(3) contains55)
       
  3634   (* SEQ nullable case *)
       
  3635    apply(erule contains.cases)
       
  3636          apply(auto)
       
  3637    apply(erule contains.cases)
       
  3638           apply(auto)
       
  3639    apply(case_tac "(bsimp a1) = AZERO")
       
  3640      apply(simp)
       
  3641      apply (metis append_Nil2 contains0 contains49 fuse.simps(1))
       
  3642    apply(case_tac "(bsimp a2a) = AZERO")
       
  3643      apply(simp)
       
  3644   apply (metis bder.simps(1) bsimp.simps(1) bsimp_ASEQ0 contains.intros(3) contains55)
       
  3645     apply(case_tac "\<exists>bsX. (bsimp a1) = AONE bsX")
       
  3646      apply(auto)[1]
       
  3647   using contains.simps apply blast
       
  3648     apply(subst bsimp_ASEQ1)
       
  3649   apply(auto)[3]
       
  3650     apply(simp)
       
  3651   apply(subgoal_tac  "bnullable (bsimp a1)")
       
  3652      prefer 2
       
  3653   using b3 apply blast
       
  3654     apply(simp)
       
  3655   apply (metis contains.intros(3) contains.intros(4) contains55 self_append_conv2)
       
  3656    apply(erule contains.cases)
       
  3657          apply(auto)
       
  3658   apply(case_tac "(bsimp a1) = AZERO")
       
  3659      apply(simp)
       
  3660   using b3 apply force
       
  3661    apply(case_tac "(bsimp a2) = AZERO")
       
  3662      apply(simp)
       
  3663   apply (metis bder.simps(1) bsimp_ASEQ0 bsimp_ASEQ_fuse contains0 contains49 f_cont1)    
       
  3664   apply(case_tac "\<exists>bsX. (bsimp a1) = AONE bsX")
       
  3665      apply(auto)[1]
       
  3666   apply (metis append_assoc bder_fuse bmkeps.simps(1) bmkeps_simp bsimp_ASEQ2 contains0 contains49 f_cont1)
       
  3667    apply(subst bsimp_ASEQ1)
       
  3668        apply(auto)[3]
       
  3669     apply(simp)
       
  3670    apply(subgoal_tac  "bnullable (bsimp a1)")
       
  3671      prefer 2
       
  3672   using b3 apply blast
       
  3673     apply(simp)
       
  3674   apply (metis bmkeps_simp contains.intros(4) contains.intros(5) contains0 contains49 f_cont1)
       
  3675        apply(erule contains.cases)
       
  3676          apply(auto)
       
  3677   (* ALT case *)
       
  3678   apply(drule contains59)
       
  3679   apply(auto)
       
  3680   apply(subst bder_bsimp_AALTs)
       
  3681   apply(rule contains61a)
       
  3682   apply(auto)
       
  3683   apply(subgoal_tac "bsimp r \<in> set (map bsimp x2a)")
       
  3684    prefer 2
       
  3685    apply simp
       
  3686   apply(case_tac "bsimp r = AZERO")
       
  3687   apply (metis Nil_is_append_conv bder.simps(1) bsimp_AALTs.elims bsimp_AALTs.simps(2) contains49 contains61 f_cont2 list.distinct(1) split_list_last)
       
  3688   apply(subgoal_tac "nonnested (bsimp r)")  
       
  3689    prefer 2
       
  3690   using nn1b apply blast
       
  3691   apply(case_tac "nonalt (bsimp r)")
       
  3692    apply(rule_tac x="bsimp r" in bexI)
       
  3693     apply (metis contains0 contains49 f_cont1)
       
  3694    apply (metis append_Cons flts_append in_set_conv_decomp k0 k0b)
       
  3695   (* AALTS case *)
       
  3696   apply(subgoal_tac "\<exists>rsX bsX. (bsimp r) = AALTs bsX rsX \<and> (\<forall>r \<in> set rsX. nonalt r)")
       
  3697    prefer 2
       
  3698   apply (metis n0 nonalt.elims(3))
       
  3699   apply(auto)
       
  3700  apply(subgoal_tac "bsimp r \<in> set (map bsimp x2a)")
       
  3701    prefer 2
       
  3702   apply (metis imageI list.set_map)
       
  3703   apply(simp)
       
  3704   apply(simp add: image_def)
       
  3705   apply(erule bexE)
       
  3706   apply(subgoal_tac "AALTs bsX rsX \<in> set (map bsimp x2a)")
       
  3707    prefer 2
       
  3708   apply simp
       
  3709   apply(drule in1)
       
  3710   apply(subgoal_tac "rsX \<noteq> []")
       
  3711    prefer 2
       
  3712    apply (metis arexp.distinct(7) good.simps(4) good1)
       
  3713 
       
  3714   by (metis contains0 contains49 f_cont1 in2 list.exhaust list.set_intros(1))
       
  3715 
       
  3716 lemma CONTAINS1:
       
  3717   assumes "a >> bs"
       
  3718   shows "a >>2 bs"
       
  3719   using assms
       
  3720   apply(induct a bs)
       
  3721   apply(auto intro: contains2.intros)
       
  3722   done
       
  3723 
       
  3724 lemma CONTAINS2:
       
  3725   assumes "a >>2 bs"
       
  3726   shows "a >> bs"
       
  3727   using assms
       
  3728   apply(induct a bs)
       
  3729   apply(auto intro: contains.intros)
       
  3730   using contains55 by auto
       
  3731 
       
  3732 lemma CONTAINS2_IFF:
       
  3733   shows "a >> bs \<longleftrightarrow> a >>2 bs"
       
  3734   using CONTAINS1 CONTAINS2 by blast
       
  3735 
       
  3736 lemma
       
  3737   assumes "bders (intern r) s >>2 bs"
       
  3738   shows   "bders_simp (intern r) s >>2 bs"
       
  3739   using assms
       
  3740   apply(induct s arbitrary: r bs)
       
  3741    apply(simp)
       
  3742   apply(simp)
       
  3743   oops
       
  3744 
       
  3745 
       
  3746 lemma
       
  3747   assumes "s \<in> L r"
       
  3748   shows "(bders_simp (intern r) s >> code (PX r s)) \<longleftrightarrow> ((intern r) >> code (PX r s))"
       
  3749   using assms
       
  3750   apply(induct s arbitrary: r rule: rev_induct)
       
  3751    apply(simp)
       
  3752   apply(simp add: bders_simp_append)
       
  3753   apply(simp add: PPP1_eq)
       
  3754   
       
  3755   
       
  3756 find_theorems "retrieve (bders _ _) _"
       
  3757 find_theorems "_ >> retrieve _ _"
       
  3758 find_theorems "bsimp _ >> _"
       
  3759   oops
       
  3760 
       
  3761 
       
  3762 lemma PX4a: 
       
  3763   assumes "(s1 @ s2) \<in> L r"
       
  3764   shows "bders (intern r) (s1 @ s2) >> code (PV r s1 (PX (ders s1 r) s2))"
       
  3765   using PX4[OF assms]
       
  3766   apply(simp add: PX_append)
       
  3767   done
       
  3768 
       
  3769 lemma PV5: 
       
  3770   assumes "s2 \<in> (ders s1 r) \<rightarrow> v"
       
  3771   shows "bders (intern r) (s1 @ s2) >> code (PV r s1 v)"
       
  3772   by (simp add: PPP0_isar PV_def Posix_flex assms)
       
  3773 
       
  3774 lemma PV6: 
       
  3775   assumes "s2 \<in> (ders s1 r) \<rightarrow> v"
       
  3776   shows "bders (bders (intern r) s1) s2 >> code (PV r s1 v)"
       
  3777   using PV5 assms bders_append by auto
       
  3778 
       
  3779 find_theorems "retrieve (bders _ _) _"
       
  3780 find_theorems "_ >> retrieve _ _"
       
  3781 find_theorems "bder _ _ >> _"
       
  3782 
       
  3783 
       
  3784 lemma OO0_PX:
       
  3785   assumes "s \<in> L r"
       
  3786   shows "bders (intern r) s >> code (PX r s)"
       
  3787   using assms
       
  3788   by (simp add: PX3)
       
  3789   
       
  3790 
       
  3791 lemma OO1:
       
  3792   assumes "[c] \<in> r \<rightarrow> v"
       
  3793   shows "bder c (intern r) >> code v"
       
  3794   using assms
       
  3795   using PPP0_isar by force
       
  3796 
       
  3797 lemma OO1a:
       
  3798   assumes "[c] \<in> L r"
       
  3799   shows "bder c (intern r) >> code (PX r [c])"
       
  3800   using assms unfolding PX_def PV_def
       
  3801   using contains70 by fastforce
       
  3802   
       
  3803 lemma OO12:
       
  3804   assumes "[c1, c2] \<in> L r"
       
  3805   shows "bders (intern r) [c1, c2] >> code (PX r [c1, c2])"
       
  3806   using assms
       
  3807   using PX_def PV_def contains70 by presburger
       
  3808 
       
  3809 lemma OO2:
       
  3810   assumes "[c] \<in> L r"
       
  3811   shows "bders_simp (intern r) [c] >> code (PX r [c])"
       
  3812   using assms
       
  3813   using OO1a Posix1(1) contains55 by auto
       
  3814   
       
  3815 
       
  3816 lemma OO22:
       
  3817   assumes "[c1, c2] \<in> L r"
       
  3818   shows "bders_simp (intern r) [c1, c2] >> code (PX r [c1, c2])"
       
  3819   using assms
       
  3820   apply(simp)
       
  3821   apply(rule contains55)
       
  3822   apply(rule Etrans)
       
  3823   thm contains7
       
  3824   apply(rule contains7)
       
  3825   oops
       
  3826 
       
  3827 
       
  3828 lemma contains70:
       
  3829  assumes "s \<in> L(r)"
       
  3830  shows "bders_simp (intern r) s >> code (flex r id s (mkeps (ders s r)))"
       
  3831   using assms
       
  3832   apply(induct s arbitrary: r rule: rev_induct)
       
  3833    apply(simp)
       
  3834   apply (simp add: contains2 mkeps_nullable nullable_correctness)
       
  3835   apply(simp add: bders_simp_append flex_append)
       
  3836   apply(simp add: PPP1_eq)
       
  3837   apply(rule Etrans)
       
  3838   apply(rule_tac v="flex r id xs (mkeps (ders (xs @ [x]) r))" in contains7)
       
  3839   oops
       
  3840 
       
  3841 
       
  3842 thm L07XX PPP0b erase_intern
       
  3843 
       
  3844 find_theorems "retrieve (bders _ _) _"
       
  3845 find_theorems "_ >> retrieve _ _"
       
  3846 find_theorems "bder _ _ >> _"
       
  3847 
       
  3848 
       
  3849 lemma PPP3:
       
  3850   assumes "\<Turnstile> v : ders s (erase a)"
       
  3851   shows "bders a s >> retrieve a (flex (erase a) id s v)"
       
  3852   using LA[OF assms] contains6 erase_bders assms by metis
       
  3853 
       
  3854 
       
  3855 find_theorems "bder _ _ >> _"
       
  3856 
       
  3857 
       
  3858 lemma
       
  3859   fixes n :: nat
       
  3860   shows "(\<Sum>i \<in> {0..n}. i) = n * (n + 1) div 2"
       
  3861   apply(induct n)
       
  3862   apply(simp)
       
  3863   apply(simp)
       
  3864   done
       
  3865 
       
  3866 lemma COUNTEREXAMPLE:
       
  3867   assumes "r = AALTs [S] [ASEQ [S] (AALTs [S] [AONE [S], ACHAR [S] c]) (ACHAR [S] c)]"
       
  3868   shows "bsimp (bder c (bsimp r)) = bsimp (bder c r)"
       
  3869   apply(simp_all add: assms)
       
  3870   oops
       
  3871 
       
  3872 lemma COUNTEREXAMPLE:
       
  3873   assumes "r = AALTs [S] [ASEQ [S] (AALTs [S] [AONE [S], ACHAR [S] c]) (ACHAR [S] c)]"
       
  3874   shows "bsimp r = r"
       
  3875   apply(simp_all add: assms)
       
  3876   oops
       
  3877 
       
  3878 lemma COUNTEREXAMPLE:
       
  3879   assumes "r = AALTs [S] [ASEQ [S] (AALTs [S] [AONE [S], ACHAR [S] c]) (ACHAR [S] c)]"
       
  3880   shows "bsimp r = XXX"
       
  3881   and   "bder c r = XXX"
       
  3882   and   "bder c (bsimp r) = XXX"
       
  3883   and   "bsimp (bder c (bsimp r)) = XXX"
       
  3884   and   "bsimp (bder c r) = XXX"
       
  3885   apply(simp_all add: assms)
       
  3886   oops
       
  3887 
       
  3888 lemma COUNTEREXAMPLE_contains1:
       
  3889   assumes "r = AALTs [S] [ASEQ [S] (AALTs [S] [AONE [S], ACHAR [S] c]) (ACHAR [S] c)]"
       
  3890   and   "bsimp (bder c r) >> bs"
       
  3891   shows "bsimp (bder c (bsimp r)) >> bs"
       
  3892   using assms 
       
  3893   apply(auto elim!: contains.cases)
       
  3894    apply(rule Etrans)
       
  3895     apply(rule contains.intros)
       
  3896     apply(rule contains.intros)
       
  3897    apply(simp)
       
  3898   apply(rule Etrans)
       
  3899     apply(rule contains.intros)
       
  3900     apply(rule contains.intros)
       
  3901   apply(simp)
       
  3902   done
       
  3903 
       
  3904 lemma COUNTEREXAMPLE_contains2:
       
  3905   assumes "r = AALTs [S] [ASEQ [S] (AALTs [S] [AONE [S], ACHAR [S] c]) (ACHAR [S] c)]"
       
  3906   and   "bsimp (bder c (bsimp r)) >> bs"
       
  3907   shows "bsimp (bder c r) >> bs" 
       
  3908   using assms 
       
  3909   apply(auto elim!: contains.cases)
       
  3910    apply(rule Etrans)
       
  3911     apply(rule contains.intros)
       
  3912     apply(rule contains.intros)
       
  3913    apply(simp)
       
  3914   apply(rule Etrans)
       
  3915     apply(rule contains.intros)
       
  3916     apply(rule contains.intros)
       
  3917   apply(simp)
       
  3918   done
       
  3919 
       
  3920 
       
  3921 end