thys2/SizeBound.thy
changeset 374 98362002c818
parent 373 320f923c77b9
child 375 f83271c585d2
equal deleted inserted replaced
373:320f923c77b9 374:98362002c818
   116 | "erase (AALTs bs (r#rs)) = ALT (erase r) (erase (AALTs bs rs))"
   116 | "erase (AALTs bs (r#rs)) = ALT (erase r) (erase (AALTs bs rs))"
   117 | "erase (ASEQ _ r1 r2) = SEQ (erase r1) (erase r2)"
   117 | "erase (ASEQ _ r1 r2) = SEQ (erase r1) (erase r2)"
   118 | "erase (ASTAR _ r) = STAR (erase r)"
   118 | "erase (ASTAR _ r) = STAR (erase r)"
   119 
   119 
   120 
   120 
   121 
       
   122 
       
   123 fun nonalt :: "arexp \<Rightarrow> bool"
   121 fun nonalt :: "arexp \<Rightarrow> bool"
   124   where
   122   where
   125   "nonalt (AALTs bs2 rs) = False"
   123   "nonalt (AALTs bs2 rs) = False"
   126 | "nonalt r = True"
   124 | "nonalt r = True"
   127 
       
   128 
       
   129 fun good :: "arexp \<Rightarrow> bool" where
       
   130   "good AZERO = False"
       
   131 | "good (AONE cs) = True" 
       
   132 | "good (ACHAR cs c) = True"
       
   133 | "good (AALTs cs []) = False"
       
   134 | "good (AALTs cs [r]) = False"
       
   135 | "good (AALTs cs (r1#r2#rs)) = (\<forall>r' \<in> set (r1#r2#rs). good r' \<and> nonalt r')"
       
   136 | "good (ASEQ _ AZERO _) = False"
       
   137 | "good (ASEQ _ (AONE _) _) = False"
       
   138 | "good (ASEQ _ _ AZERO) = False"
       
   139 | "good (ASEQ cs r1 r2) = (good r1 \<and> good r2)"
       
   140 | "good (ASTAR cs r) = True"
       
   141 
       
   142 
       
   143 
   125 
   144 
   126 
   145 fun fuse :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp" where
   127 fun fuse :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp" where
   146   "fuse bs AZERO = AZERO"
   128   "fuse bs AZERO = AZERO"
   147 | "fuse bs (AONE cs) = AONE (bs @ cs)" 
   129 | "fuse bs (AONE cs) = AONE (bs @ cs)" 
   235 lemma erase_fuse:
   217 lemma erase_fuse:
   236   shows "erase (fuse bs r) = erase r"
   218   shows "erase (fuse bs r) = erase r"
   237   apply(induct r rule: erase.induct)
   219   apply(induct r rule: erase.induct)
   238   apply(simp_all)
   220   apply(simp_all)
   239   done
   221   done
   240 
       
   241 thm Posix.induct
       
   242 
   222 
   243 lemma erase_intern [simp]:
   223 lemma erase_intern [simp]:
   244   shows "erase (intern r) = r"
   224   shows "erase (intern r) = r"
   245   apply(induct r)
   225   apply(induct r)
   246   apply(simp_all add: erase_fuse)
   226   apply(simp_all add: erase_fuse)
   549   "distinctBy [] f acc = []"
   529   "distinctBy [] f acc = []"
   550 | "distinctBy (x#xs) f acc = 
   530 | "distinctBy (x#xs) f acc = 
   551      (if (f x) \<in> acc then distinctBy xs f acc 
   531      (if (f x) \<in> acc then distinctBy xs f acc 
   552       else x # (distinctBy xs f ({f x} \<union> acc)))"
   532       else x # (distinctBy xs f ({f x} \<union> acc)))"
   553 
   533 
   554 
   534 lemma dB_single_step: 
   555 
   535   shows "distinctBy (a#rs) f {} = a # distinctBy rs f {f a}"
       
   536   by simp 
   556 
   537 
   557 fun flts :: "arexp list \<Rightarrow> arexp list"
   538 fun flts :: "arexp list \<Rightarrow> arexp list"
   558   where 
   539   where 
   559   "flts [] = []"
   540   "flts [] = []"
   560 | "flts (AZERO # rs) = flts rs"
   541 | "flts (AZERO # rs) = flts rs"
   561 | "flts ((AALTs bs  rs1) # rs) = (map (fuse bs) rs1) @ flts rs"
   542 | "flts ((AALTs bs  rs1) # rs) = (map (fuse bs) rs1) @ flts rs"
   562 | "flts (r1 # rs) = r1 # flts rs"
   543 | "flts (r1 # rs) = r1 # flts rs"
   563 
   544 
   564 
   545 
   565 
   546 
   566 
       
   567 fun li :: "bit list \<Rightarrow> arexp list \<Rightarrow> arexp"
       
   568   where
       
   569   "li _ [] = AZERO"
       
   570 | "li bs [a] = fuse bs a"
       
   571 | "li bs as = AALTs bs as"
       
   572 
       
   573 
       
   574 
       
   575 
       
   576 fun bsimp_ASEQ :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp \<Rightarrow> arexp"
   547 fun bsimp_ASEQ :: "bit list \<Rightarrow> arexp \<Rightarrow> arexp \<Rightarrow> arexp"
   577   where
   548   where
   578   "bsimp_ASEQ _ AZERO _ = AZERO"
   549   "bsimp_ASEQ _ AZERO _ = AZERO"
   579 | "bsimp_ASEQ _ _ AZERO = AZERO"
   550 | "bsimp_ASEQ _ _ AZERO = AZERO"
   580 | "bsimp_ASEQ bs1 (AONE bs2) r2 = fuse (bs1 @ bs2) r2"
   551 | "bsimp_ASEQ bs1 (AONE bs2) r2 = fuse (bs1 @ bs2) r2"
   589 
   560 
   590 
   561 
   591 fun bsimp :: "arexp \<Rightarrow> arexp" 
   562 fun bsimp :: "arexp \<Rightarrow> arexp" 
   592   where
   563   where
   593   "bsimp (ASEQ bs1 r1 r2) = bsimp_ASEQ bs1 (bsimp r1) (bsimp r2)"
   564   "bsimp (ASEQ bs1 r1 r2) = bsimp_ASEQ bs1 (bsimp r1) (bsimp r2)"
   594 | "bsimp (AALTs bs1 rs) = bsimp_AALTs bs1 (distinctBy  (flts (map bsimp rs)) erase {} ) "
   565 | "bsimp (AALTs bs1 rs) = bsimp_AALTs bs1 (distinctBy (flts (map bsimp rs)) erase {}) "
   595 | "bsimp r = r"
   566 | "bsimp r = r"
   596 
       
   597 
       
   598 
   567 
   599 
   568 
   600 fun 
   569 fun 
   601   bders_simp :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
   570   bders_simp :: "arexp \<Rightarrow> string \<Rightarrow> arexp"
   602 where
   571 where
   603   "bders_simp r [] = r"
   572   "bders_simp r [] = r"
   604 | "bders_simp r (c # s) = bders_simp (bsimp (bder c r)) s"
   573 | "bders_simp r (c # s) = bders_simp (bsimp (bder c r)) s"
   605 
   574 
   606 definition blexer_simp where
   575 definition blexer_simp where
   607  "blexer_simp r s \<equiv> if bnullable (bders_simp (intern r) s) then 
   576  "blexer_simp r s \<equiv> if bnullable (bders_simp (intern r) s) then 
   608                 decode (bmkeps (bders_simp (intern r) s)) r else None"
   577                     decode (bmkeps (bders_simp (intern r) s)) r else None"
   609 
   578 
   610 export_code bders_simp in Scala module_name Example
   579 export_code bders_simp in Scala module_name Example
   611 
   580 
   612 lemma bders_simp_append:
   581 lemma bders_simp_append:
   613   shows "bders_simp r (s1 @ s2) = bders_simp (bders_simp r s1) s2"
   582   shows "bders_simp r (s1 @ s2) = bders_simp (bders_simp r s1) s2"
   614   apply(induct s1 arbitrary: r s2)
   583   apply(induct s1 arbitrary: r s2)
   615    apply(simp)
   584   apply(simp_all)
   616   apply(simp)
   585   done
   617   done
       
   618 
       
   619 
       
   620 
       
   621 
       
   622 
       
   623 
       
   624 
   586 
   625 lemma L_bsimp_ASEQ:
   587 lemma L_bsimp_ASEQ:
   626   "L (SEQ (erase r1) (erase r2)) = L (erase (bsimp_ASEQ bs r1 r2))"
   588   "L (SEQ (erase r1) (erase r2)) = L (erase (bsimp_ASEQ bs r1 r2))"
   627   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
   589   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
   628   apply(simp_all)
   590   apply(simp_all)
   672   apply(auto simp add: Sequ_def)[1]
   634   apply(auto simp add: Sequ_def)[1]
   673   apply(subst L_bsimp_ASEQ[symmetric])
   635   apply(subst L_bsimp_ASEQ[symmetric])
   674   apply(auto simp add: Sequ_def)[1]
   636   apply(auto simp add: Sequ_def)[1]
   675   apply(subst (asm)  L_bsimp_ASEQ[symmetric])
   637   apply(subst (asm)  L_bsimp_ASEQ[symmetric])
   676   apply(auto simp add: Sequ_def)[1]
   638   apply(auto simp add: Sequ_def)[1]
   677    apply(simp)
   639   apply(simp)
   678    apply(subst L_bsimp_AALTs[symmetric])
   640   apply(subst L_bsimp_AALTs[symmetric])
   679    defer
   641   defer
   680    apply(simp)
   642   apply(simp)
   681   apply(subst (2)L_erase_AALTs)
   643   apply(subst (2)L_erase_AALTs)
   682   apply(subst L_erase_dB)
   644   apply(subst L_erase_dB)
   683   apply(subst L_erase_flts)
   645   apply(subst L_erase_flts)
   684   apply(auto)
   646   apply(auto)
   685    apply (simp add: L_erase_AALTs)
   647   apply (simp add: L_erase_AALTs)
   686   using L_erase_AALTs by blast
   648   using L_erase_AALTs by blast
       
   649 
       
   650 
   687 
   651 
   688 lemma bsimp_ASEQ0:
   652 lemma bsimp_ASEQ0:
   689   shows "bsimp_ASEQ bs r1 AZERO = AZERO"
   653   shows "bsimp_ASEQ bs r1 AZERO = AZERO"
   690   apply(induct r1)
   654   apply(induct r1)
   691   apply(auto)
   655   apply(auto)
   692   done
   656   done
   693 
   657 
   694 
       
   695 
       
   696 lemma bsimp_ASEQ1:
   658 lemma bsimp_ASEQ1:
   697   assumes "r1 \<noteq> AZERO" "r2 \<noteq> AZERO" "\<forall>bs. r1 \<noteq> AONE bs"
   659   assumes "r1 \<noteq> AZERO" "r2 \<noteq> AZERO" "\<forall>bs. r1 \<noteq> AONE bs"
   698   shows "bsimp_ASEQ bs r1 r2 = ASEQ bs r1 r2"
   660   shows "bsimp_ASEQ bs r1 r2 = ASEQ bs r1 r2"
   699   using assms
   661   using assms
   700   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
   662   apply(induct bs r1 r2 rule: bsimp_ASEQ.induct)
   709 
   671 
   710 
   672 
   711 lemma L_bders_simp:
   673 lemma L_bders_simp:
   712   shows "L (erase (bders_simp r s)) = L (erase (bders r s))"
   674   shows "L (erase (bders_simp r s)) = L (erase (bders r s))"
   713   apply(induct s arbitrary: r rule: rev_induct)
   675   apply(induct s arbitrary: r rule: rev_induct)
   714    apply(simp)
   676   apply(simp)
   715   apply(simp)
   677   apply(simp)
   716   apply(simp add: ders_append)
   678   apply(simp add: ders_append)
   717   apply(simp add: bders_simp_append)
   679   apply(simp add: bders_simp_append)
   718   apply(simp add: L_bsimp_erase[symmetric])
   680   apply(simp add: L_bsimp_erase[symmetric])
   719   by (simp add: der_correctness)
   681   by (simp add: der_correctness)
   746   apply(induct rs arbitrary: rs1 bs)
   708   apply(induct rs arbitrary: rs1 bs)
   747   apply(simp)
   709   apply(simp)
   748   apply(simp)
   710   apply(simp)
   749   by (metis append_assoc in_set_conv_decomp r1 r2)
   711   by (metis append_assoc in_set_conv_decomp r1 r2)
   750   
   712   
   751 lemma qq3:
       
   752   shows "bnullable (AALTs bs rs) = (\<exists>r \<in> set rs. bnullable r)"
       
   753   apply(induct rs arbitrary: bs)
       
   754   apply(simp)
       
   755   apply(simp)
       
   756   done
       
   757 
       
   758 
       
   759 
       
   760 
       
   761 
       
   762 fun nonnested :: "arexp \<Rightarrow> bool"
       
   763   where
       
   764   "nonnested (AALTs bs2 []) = True"
       
   765 | "nonnested (AALTs bs2 ((AALTs bs1 rs1) # rs2)) = False"
       
   766 | "nonnested (AALTs bs2 (r # rs2)) = nonnested (AALTs bs2 rs2)"
       
   767 | "nonnested r = True"
       
   768 
   713 
   769 lemma flts_append:
   714 lemma flts_append:
   770   shows "flts (xs1 @ xs2) = flts xs1 @ flts xs2"
   715   shows "flts (xs1 @ xs2) = flts xs1 @ flts xs2"
   771 by (induct xs1  arbitrary: xs2  rule: flts.induct)(auto)
   716 by (induct xs1  arbitrary: xs2  rule: flts.induct)(auto)
   772 
   717 
  1004   apply(drule contextrewrites2)
   949   apply(drule contextrewrites2)
  1005   apply auto
   950   apply auto
  1006   done
   951   done
  1007 
   952 
  1008 
   953 
  1009 corollary srewrites_alt1: "rs1 s\<leadsto>* rs2 \<Longrightarrow> AALTs bs rs1 \<leadsto>* AALTs bs rs2"
   954 corollary srewrites_alt1: 
       
   955   assumes "rs1 s\<leadsto>* rs2"
       
   956   shows "AALTs bs rs1 \<leadsto>* AALTs bs rs2"
       
   957 using assms
  1010   by (metis append.left_neutral srewrites_alt)
   958   by (metis append.left_neutral srewrites_alt)
  1011 
   959 
  1012 
   960 
  1013 lemma star_seq:  "r1 \<leadsto>* r2 \<Longrightarrow> ASEQ bs r1 r3 \<leadsto>* ASEQ bs r2 r3"
   961 lemma star_seq:  
  1014   apply(induct r1 r2 arbitrary: r3 rule: rrewrites.induct)
   962   assumes "r1 \<leadsto>* r2"
  1015    apply(rule rs1)
   963   shows "ASEQ bs r1 r3 \<leadsto>* ASEQ bs r2 r3"
  1016   apply(erule rrewrites.cases)
   964 using assms
  1017    apply(simp)
   965 apply(induct r1 r2 arbitrary: r3 rule: rrewrites.induct)
  1018    apply(rule r_in_rstar)
   966 apply(auto intro: rrewrite.intros)
  1019    apply(rule rrewrite.intros(4))
   967 done
  1020    apply simp
   968 
  1021   apply(rule rs2)
   969 lemma star_seq2:  
  1022    apply(assumption)
   970   assumes "r3 \<leadsto>* r4"
  1023   apply(rule rrewrite.intros(4))
   971   shows "ASEQ bs r1 r3 \<leadsto>* ASEQ bs r1 r4"
  1024   by assumption
   972 using assms
  1025 
   973 apply(induct r3 r4 arbitrary: r1 rule: rrewrites.induct)
  1026 lemma star_seq2:  "r3 \<leadsto>* r4 \<Longrightarrow> ASEQ bs r1 r3 \<leadsto>* ASEQ bs r1 r4"
   974 apply(auto intro: rrewrite.intros)
  1027   apply(induct r3 r4 arbitrary: r1 rule: rrewrites.induct)
   975 done
  1028    apply auto
   976 
  1029   using rrewrite.intros(5) by blast
   977 lemma continuous_rewrite: 
  1030 
   978   assumes "r1 \<leadsto>* AZERO"
  1031 
   979   shows "ASEQ bs1 r1 r2 \<leadsto>* AZERO"
  1032 lemma continuous_rewrite: "\<lbrakk>r1 \<leadsto>* AZERO\<rbrakk> \<Longrightarrow> ASEQ bs1 r1 r2 \<leadsto>* AZERO"
   980 using assms
  1033   apply(induction ra\<equiv>"r1" rb\<equiv>"AZERO" arbitrary: bs1 r1 r2 rule: rrewrites.induct)
   981   apply(induction ra\<equiv>"r1" rb\<equiv>"AZERO" arbitrary: bs1 r1 r2 rule: rrewrites.induct)
  1034    apply (simp add: r_in_rstar rrewrite.intros(1))
   982   apply(auto intro: rrewrite.intros r_in_rstar star_seq)
  1035 
   983   by (meson rrewrite.intros(1) rs2 star_seq)
  1036   by (meson rrewrite.intros(1) rrewrites.intros(2) star_seq)
   984   
  1037   
   985 
  1038 
   986 
  1039 
   987 lemma bsimp_aalts_simpcases: 
  1040 lemma bsimp_aalts_simpcases: "AONE bs \<leadsto>* (bsimp (AONE bs))"  "AZERO \<leadsto>* bsimp AZERO" "ACHAR bs c \<leadsto>* (bsimp (ACHAR bs c))"
   988   shows "AONE bs \<leadsto>* bsimp (AONE bs)"  
  1041   apply (simp add: rrewrites.intros(1))
   989   and   "AZERO \<leadsto>* bsimp AZERO" 
  1042   apply (simp add: rrewrites.intros(1))
   990   and   "ACHAR bs c \<leadsto>* bsimp (ACHAR bs c)"
  1043   by (simp add: rrewrites.intros(1))
   991   by (simp_all)
  1044 
   992 
  1045 lemma trivialbsimpsrewrites: "\<lbrakk>\<And>x. x \<in> set rs \<Longrightarrow> x \<leadsto>* f x \<rbrakk> \<Longrightarrow> rs s\<leadsto>* (map f rs)"
   993 lemma trivialbsimpsrewrites: "\<lbrakk>\<And>x. x \<in> set rs \<Longrightarrow> x \<leadsto>* f x \<rbrakk> \<Longrightarrow> rs s\<leadsto>* (map f rs)"
  1046 
   994 
  1047   apply(induction rs)
   995   apply(induction rs)
  1048    apply simp
   996    apply simp
  1067   where
  1015   where
  1068 fs1: "[] f\<leadsto>* []"
  1016 fs1: "[] f\<leadsto>* []"
  1069 |fs2: "\<lbrakk>rs f\<leadsto>* rs'\<rbrakk> \<Longrightarrow> (AZERO#rs) f\<leadsto>* rs'"
  1017 |fs2: "\<lbrakk>rs f\<leadsto>* rs'\<rbrakk> \<Longrightarrow> (AZERO#rs) f\<leadsto>* rs'"
  1070 |fs3: "\<lbrakk>rs f\<leadsto>* rs'\<rbrakk> \<Longrightarrow> ((AALTs bs rs1) # rs) f\<leadsto>* ((map (fuse bs) rs1) @ rs')"
  1018 |fs3: "\<lbrakk>rs f\<leadsto>* rs'\<rbrakk> \<Longrightarrow> ((AALTs bs rs1) # rs) f\<leadsto>* ((map (fuse bs) rs1) @ rs')"
  1071 |fs4: "\<lbrakk>rs f\<leadsto>* rs';nonalt r; nonazero r\<rbrakk> \<Longrightarrow> (r#rs) f\<leadsto>* (r#rs')"
  1019 |fs4: "\<lbrakk>rs f\<leadsto>* rs';nonalt r; nonazero r\<rbrakk> \<Longrightarrow> (r#rs) f\<leadsto>* (r#rs')"
  1072 
       
  1073 
       
  1074 
  1020 
  1075 
  1021 
  1076 
  1022 
  1077 lemma flts_prepend: "\<lbrakk>nonalt a; nonazero a\<rbrakk> \<Longrightarrow> flts (a#rs) = a # (flts rs)"
  1023 lemma flts_prepend: "\<lbrakk>nonalt a; nonazero a\<rbrakk> \<Longrightarrow> flts (a#rs) = a # (flts rs)"
  1078   by (metis append_Cons append_Nil flts_single1 flts_append)
  1024   by (metis append_Cons append_Nil flts_single1 flts_append)
  1096   thm nonalt.elims
  1042   thm nonalt.elims
  1097    
  1043    
  1098          apply blast
  1044          apply blast
  1099    
  1045    
  1100   using bbbbs1 apply blast
  1046   using bbbbs1 apply blast
  1101        apply(simp add: nonalt.simps)+
  1047        apply(simp)+
  1102    
  1048    
  1103    apply (meson nonazero.elims(3))
  1049    apply (meson nonazero.elims(3))
  1104    
  1050    
  1105   by (meson fs4 nonalt.elims(3) nonazero.elims(3))
  1051   by (meson fs4 nonalt.elims(3) nonazero.elims(3))
  1106 
  1052 
  1107 
  1053 
  1108 lemma rrewrite0away: "AALTs bs ( AZERO # rsb) \<leadsto> AALTs bs rsb"
  1054 lemma rrewrite0away: "AALTs bs (AZERO # rsb) \<leadsto> AALTs bs rsb"
  1109   by (metis append_Nil rrewrite.intros(7))
  1055   by (metis append_Nil rrewrite.intros(7))
  1110 
  1056 
  1111 
  1057 
  1112 lemma frewritesaalts:"rs f\<leadsto>* rs' \<Longrightarrow> (AALTs bs (rs1@rs)) \<leadsto>* (AALTs bs (rs1@rs'))"
  1058 lemma frewritesaalts:"rs f\<leadsto>* rs' \<Longrightarrow> (AALTs bs (rs1@rs)) \<leadsto>* (AALTs bs (rs1@rs'))"
  1113   apply(induct rs rs' arbitrary: bs rs1 rule:frewrites.induct)
  1059   apply(induct rs rs' arbitrary: bs rs1 rule:frewrites.induct)
  1137 
  1083 
  1138 
  1084 
  1139 
  1085 
  1140   apply(case_tac "\<exists>bs2 rs2. a = AALTs bs2 rs2")
  1086   apply(case_tac "\<exists>bs2 rs2. a = AALTs bs2 rs2")
  1141    apply(erule exE)+
  1087    apply(erule exE)+
  1142    apply(simp add: flts.simps)
  1088    apply(simp)
  1143    prefer 2
  1089    prefer 2
  1144 
  1090 
  1145   apply(subst flts_prepend)
  1091   apply(subst flts_prepend)
  1146    
  1092    
  1147      apply (meson nonalt.elims(3))
  1093      apply (meson nonalt.elims(3))
  1162 
  1108 
  1163 lemma threelistsappend: "rsa@a#rsb = (rsa@[a])@rsb"
  1109 lemma threelistsappend: "rsa@a#rsb = (rsa@[a])@rsb"
  1164   apply auto
  1110   apply auto
  1165   done
  1111   done
  1166 
  1112 
  1167 fun distinctByAcc :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b set \<Rightarrow> 'b set"
       
  1168   where
       
  1169   "distinctByAcc [] f acc = acc"
       
  1170 | "distinctByAcc (x#xs) f acc = 
       
  1171      (if (f x) \<in> acc then distinctByAcc xs f acc 
       
  1172       else  (distinctByAcc xs f ({f x} \<union> acc)))"
       
  1173 
       
  1174 lemma dB_single_step: "distinctBy (a#rs) f {} = a # distinctBy rs f {f a}"
       
  1175   apply simp
       
  1176   done
       
  1177 
  1113 
  1178 lemma somewhereInside: "r \<in> set rs \<Longrightarrow> \<exists>rs1 rs2. rs = rs1@[r]@rs2"
  1114 lemma somewhereInside: "r \<in> set rs \<Longrightarrow> \<exists>rs1 rs2. rs = rs1@[r]@rs2"
  1179   using split_list by fastforce
  1115   using split_list by fastforce
  1180 
  1116 
  1181 lemma somewhereMapInside: "f r \<in> f ` set rs \<Longrightarrow> \<exists>rs1 rs2 a. rs = rs1@[a]@rs2 \<and> f a = f r"
  1117 lemma somewhereMapInside: "f r \<in> f ` set rs \<Longrightarrow> \<exists>rs1 rs2 a. rs = rs1@[a]@rs2 \<and> f a = f r"
  1234 
  1170 
  1235 
  1171 
  1236   
  1172   
  1237 
  1173 
  1238 
  1174 
  1239 lemma bsimp_rewrite: " (rrewrites r ( bsimp r))"
  1175 lemma bsimp_rewrite: 
       
  1176   shows "r \<leadsto>* bsimp r"
  1240   apply(induction r rule: bsimp.induct)
  1177   apply(induction r rule: bsimp.induct)
  1241        apply simp
  1178        apply simp
  1242        apply(case_tac "bsimp r1 = AZERO")
  1179        apply(case_tac "bsimp r1 = AZERO")
  1243         apply simp
  1180         apply simp
  1244   using continuous_rewrite apply blast
  1181   using continuous_rewrite apply blast
  1313 
  1250 
  1314 
  1251 
  1315 
  1252 
  1316 lemma nomember_bnullable: "\<lbrakk> \<not> (\<exists>r0\<in>set rs1. bnullable r0); \<not> bnullable r; \<not> (\<exists>r0\<in>set rs2. bnullable r0)\<rbrakk>
  1253 lemma nomember_bnullable: "\<lbrakk> \<not> (\<exists>r0\<in>set rs1. bnullable r0); \<not> bnullable r; \<not> (\<exists>r0\<in>set rs2. bnullable r0)\<rbrakk>
  1317  \<Longrightarrow> \<not>bnullable (AALTs bs (rs1 @ [r] @ rs2))"
  1254  \<Longrightarrow> \<not>bnullable (AALTs bs (rs1 @ [r] @ rs2))"
  1318   using nonbnullable_lists_concat qq3 by presburger
  1255   using bnullable.simps(4) nonbnullable_lists_concat by presburger
  1319 
  1256 
  1320 lemma bnullable_segment: " bnullable (AALTs bs (rs1@[r]@rs2)) \<Longrightarrow> bnullable (AALTs bs rs1) \<or> bnullable (AALTs bs rs2) \<or> bnullable r"
  1257 lemma bnullable_segment: " bnullable (AALTs bs (rs1@[r]@rs2)) \<Longrightarrow> bnullable (AALTs bs rs1) \<or> bnullable (AALTs bs rs2) \<or> bnullable r"
  1321   apply(case_tac "\<exists>r0\<in>set rs1.  bnullable r0")
  1258   apply(case_tac "\<exists>r0\<in>set rs1.  bnullable r0")
  1322 
  1259   using r2 apply blast
  1323   using qq3 apply blast
       
  1324   apply(case_tac "bnullable r")
  1260   apply(case_tac "bnullable r")
  1325 
  1261 
  1326   apply blast
  1262   apply blast
  1327   apply(case_tac "\<exists>r0\<in>set rs2.  bnullable r0")
  1263   apply(case_tac "\<exists>r0\<in>set rs2.  bnullable r0")
  1328 
  1264 
  1357 lemma third_segment_bmkeps:  "\<lbrakk>bnullable (AALTs bs (rs1@rs2@rs3)); \<not>bnullable (AALTs bs rs1); \<not>bnullable (AALTs bs rs2)\<rbrakk> \<Longrightarrow> 
  1293 lemma third_segment_bmkeps:  "\<lbrakk>bnullable (AALTs bs (rs1@rs2@rs3)); \<not>bnullable (AALTs bs rs1); \<not>bnullable (AALTs bs rs2)\<rbrakk> \<Longrightarrow> 
  1358 bmkeps (AALTs bs (rs1@rs2@rs3) ) = bmkeps (AALTs bs rs3)"
  1294 bmkeps (AALTs bs (rs1@rs2@rs3) ) = bmkeps (AALTs bs rs3)"
  1359   apply(subgoal_tac "bnullable (AALTs bs rs3)")
  1295   apply(subgoal_tac "bnullable (AALTs bs rs3)")
  1360    apply(subgoal_tac "\<forall>r \<in> set (rs1@rs2). \<not>bnullable r")
  1296    apply(subgoal_tac "\<forall>r \<in> set (rs1@rs2). \<not>bnullable r")
  1361   apply(subgoal_tac "bmkeps (AALTs bs (rs1@rs2@rs3)) = bmkeps (AALTs bs ((rs1@rs2)@rs3) )")
  1297   apply(subgoal_tac "bmkeps (AALTs bs (rs1@rs2@rs3)) = bmkeps (AALTs bs ((rs1@rs2)@rs3) )")
  1362   apply (metis qq2 qq3)
  1298   apply (metis bnullable.simps(4) qq2)
  1363 
  1299 
  1364   apply (metis append.assoc)
  1300   apply (metis append.assoc)
  1365 
  1301 
  1366   apply (metis append.assoc in_set_conv_decomp r2 third_segment_bnullable)
  1302   apply (metis append.assoc in_set_conv_decomp r2 third_segment_bnullable)
  1367 
  1303 
  1378 
  1314 
  1379   
  1315   
  1380   using r2 apply blast
  1316   using r2 apply blast
  1381   
  1317   
  1382     apply (metis list.set_intros(1))
  1318     apply (metis list.set_intros(1))
  1383   apply (smt (verit, ccfv_threshold) append_eq_append_conv2 list.set_intros(1) qq2 qq3 rewrite_nullable rrewrite.intros(8) self_append_conv2 spillbmkepslistr)
  1319   apply (smt (verit, ccfv_threshold) append_eq_append_conv2 list.set_intros(1) qq2 bnullable.simps(4) rewrite_nullable rrewrite.intros(8) self_append_conv2 spillbmkepslistr)
  1384 
  1320 
  1385 
  1321 
  1386   thm qq1
  1322   thm qq1
  1387   apply(subgoal_tac "bmkeps  (AALTs bs (rsa @ AALTs bs1 rs1 # rsb)) = bmkeps (AALTs bs rsb) ")
  1323   apply(subgoal_tac "bmkeps  (AALTs bs (rsa @ AALTs bs1 rs1 # rsb)) = bmkeps (AALTs bs rsb) ")
  1388    prefer 2
  1324    prefer 2
  1425 
  1361 
  1426   using bnullable.simps(1) apply blast
  1362   using bnullable.simps(1) apply blast
  1427 
  1363 
  1428   apply (simp add: b2)
  1364   apply (simp add: b2)
  1429  
  1365  
  1430   by (smt (z3) Un_iff bnullable_correctness erase.simps(5) qq1 qq2 qq3 set_append)
  1366   by (smt (z3) Un_iff bnullable_correctness erase.simps(5) qq1 qq2 bnullable.simps(4) set_append)
  1431 
  1367 
  1432 lemma rewrites_bmkeps: 
  1368 lemma rewrites_bmkeps: 
  1433   assumes "r1 \<leadsto>* r2" "bnullable r1" 
  1369   assumes "r1 \<leadsto>* r2" "bnullable r1" 
  1434   shows "bmkeps r1 = bmkeps r2"
  1370   shows "bmkeps r1 = bmkeps r2"
  1435   using assms
  1371   using assms
  1494 
  1430 
  1495   using rrewrite.intros(13) by auto
  1431   using rrewrite.intros(13) by auto
  1496 
  1432 
  1497   
  1433   
  1498 
  1434 
  1499 lemma rewrites_fuse:  "r2 \<leadsto>* r2' \<Longrightarrow>  (fuse bs1 r2) \<leadsto>*  (fuse bs1 r2')"
  1435 lemma rewrites_fuse:  
  1500   apply(induction r2 r2' arbitrary: bs1 rule: rrewrites.induct)
  1436   assumes "r2 \<leadsto>* r2'"
  1501    apply simp
  1437   shows "fuse bs1 r2 \<leadsto>* fuse bs1 r2'"
  1502   by (meson real_trans rewrite_fuse)
  1438 using assms
  1503 
  1439 apply(induction r2 r2' arbitrary: bs1 rule: rrewrites.induct)
  1504 lemma  bder_fuse_list: " map (bder c \<circ> fuse bs1) rs1 = map (fuse bs1 \<circ> bder c) rs1"
  1440 apply(auto intro: rewrite_fuse real_trans)
  1505   apply(induction rs1)
  1441 done
  1506   apply simp
  1442 
  1507   by (simp add: bder_fuse)
  1443 lemma  bder_fuse_list: 
  1508 
  1444   shows "map (bder c \<circ> fuse bs1) rs1 = map (fuse bs1 \<circ> bder c) rs1"
       
  1445 apply(induction rs1)
       
  1446 apply(simp_all add: bder_fuse)
       
  1447 done
  1509 
  1448 
  1510 
  1449 
  1511 lemma rewrite_der_altmiddle: "bder c (AALTs bs (rsa @ AALTs bs1 rs1 # rsb)) \<leadsto>* bder c (AALTs bs (rsa @ map (fuse bs1) rs1 @ rsb))"
  1450 lemma rewrite_der_altmiddle: "bder c (AALTs bs (rsa @ AALTs bs1 rs1 # rsb)) \<leadsto>* bder c (AALTs bs (rsa @ map (fuse bs1) rs1 @ rsb))"
  1512    apply simp
  1451    apply simp
  1513    apply(simp add: bder_fuse_list)
  1452    apply(simp add: bder_fuse_list)