diff -r 65e786a58365 -r a73b2e553804 thys2/SizeBound6CT.thy --- a/thys2/SizeBound6CT.thy Tue Mar 01 11:14:17 2022 +0000 +++ b/thys2/SizeBound6CT.thy Wed Mar 02 23:13:59 2022 +0000 @@ -21,6 +21,12 @@ |"orderedPrefAux 0 ss = Nil" +fun ordsuf :: "char list \ char list list" + where + "ordsuf [] = []" +| "ordsuf (x # xs) = (ordsuf xs) @ [(x # xs)]" + + fun orderedPref :: "char list \ char list list" where "orderedPref s = orderedPrefAux (length s) s" @@ -46,6 +52,26 @@ lemma shape_of_suf_3list: shows "orderedSuf [c1, c2, c3] = [[c3], [c2, c3], [c1, c2, c3]]" by auto + + +lemma ordsuf_last: + shows "ordsuf (xs @ [x]) = [x] # (map (\s. s @ [x]) (ordsuf xs))" + apply(induct xs) + apply(auto) + done + +lemma ordsuf_append: + shows "ordsuf (s1 @ s) = (ordsuf s) @ (map (\s11. s11 @ s) (ordsuf s1))" +apply(induct s1 arbitrary: s rule: rev_induct) + apply(simp) + apply(drule_tac x="[x] @ s" in meta_spec) + apply(simp) + apply(subst ordsuf_last) + apply(simp) + done + + + (* lemma throwing_elem_around: shows "orderedSuf (s1 @ [a] @ s) = (orderedSuf s) @ (map (\s11. s11 @ s) (orderedSuf ( s1 @ [a]) ))" @@ -90,10 +116,16 @@ *) +datatype cchar = +Achar +|Bchar +|Cchar +|Dchar + datatype rrexp = RZERO | RONE -| RCHAR char +| RCHAR cchar | RSEQ rrexp rrexp | RALTS "rrexp list" | RSTAR rrexp @@ -113,13 +145,19 @@ | "rnullable (RSEQ r1 r2) = (rnullable r1 \ rnullable r2)" | "rnullable (RSTAR r) = True" +fun convert_cchar_char :: "cchar \ char" + where +"convert_cchar_char Achar = (CHR 0x41) " +| "convert_cchar_char Bchar = (CHR 0x42) " +| "convert_cchar_char Cchar = (CHR 0x43) " +| "convert_cchar_char Dchar = (CHR 0x44) " fun rder :: "char \ rrexp \ rrexp" where "rder c (RZERO) = RZERO" | "rder c (RONE) = RZERO" -| "rder c (RCHAR d) = (if c = d then RONE else RZERO)" +| "rder c (RCHAR d) = (if c = (convert_cchar_char d) then RONE else RZERO)" | "rder c (RALTS rs) = RALTS (map (rder c) rs)" | "rder c (RSEQ r1 r2) = (if rnullable r1 @@ -252,6 +290,12 @@ "cond_list r1 r2 s = rders_cond_list r2 (nullable_bools r1 (orderedPref s) ) (orderedSuf s)" +fun seq_update :: " char \ rrexp \ char list list \ char list list" + where +"seq_update c r Ss = (if (rnullable r) then ([c] # (map (\s1. s1 @ [c]) Ss)) else (map (\s1. s1 @ [c]) Ss))" + + + lemma rSEQ_mono: shows "rsize (rsimp_SEQ r1 r2) \rsize ( RSEQ r1 r2)" apply auto @@ -378,7 +422,7 @@ apply(case_tac r2) apply simp_all done - +(* lemma rsimp_aalts_another: shows "\r \ (set (map rsimp ((RSEQ (rders r1 s) r2) # (rders_cond_list r2 (nullable_bools r1 (orderedPref s)) (orderedSuf s)) )) ). (rsize r) < N " @@ -424,8 +468,55 @@ prefer 2 apply auto sorry +*) +(* +lemma shape_derssimpseq_onechar2: + shows "rders_simp (RSEQ r1 r2) [c] = + rsimp (RALTS ((RSEQ (rders_simp r1 [c]) r2) # + (rders_cond_list r2 (nullable_bools r1 (orderedPref [c])) (orderedSuf [c]))) )" + sorry +*) + +lemma shape_derssimpseq_onechar: + shows " (rders_simp r [c]) = (rsimp (rders r [c]))" +and "rders_simp (RSEQ r1 r2) [c] = + rsimp (RALTS ((RSEQ (rders r1 [c]) r2) # + (rders_cond_list r2 (nullable_bools r1 (orderedPref [c])) (orderedSuf [c]))) )" + apply simp + apply(simp add: rders.simps) + apply(case_tac "rsimp (rder c r1) = RZERO") + apply auto + apply(case_tac "rsimp (rder c r1) = RONE") + apply auto + apply(subgoal_tac "rsimp_SEQ RONE (rsimp r2) = rsimp r2") + prefer 2 + using idiot + apply simp + apply(subgoal_tac "rsimp_SEQ RONE (rsimp r2) = rsimp_ALTs (rdistinct (rflts [rsimp r2]) {})") + prefer 2 + apply auto + apply(case_tac "rsimp r2") + apply auto + apply(subgoal_tac "rdistinct x5 {} = x5") + prefer 2 + using no_further_dB_after_simp + apply metis + apply(subgoal_tac "rsimp_ALTs (rdistinct x5 {}) = rsimp_ALTs x5") + prefer 2 + apply fastforce + apply auto + apply (metis no_alt_short_list_after_simp) + apply (case_tac "rsimp r2 = RZERO") + apply(subgoal_tac "rsimp_SEQ (rsimp (rder c r1)) (rsimp r2) = RZERO") + prefer 2 + apply(case_tac "rsimp ( rder c r1)") + apply auto + apply(subgoal_tac "rsimp_SEQ (rsimp (rder c r1)) (rsimp r2) = RSEQ (rsimp (rder c r1)) (rsimp r2)") + prefer 2 + apply auto + sorry lemma shape_derssimpseq_onechar2: shows "rders_simp (RSEQ r1 r2) [c] = @@ -433,6 +524,31 @@ (rders_cond_list r2 (nullable_bools r1 (orderedPref [c])) (orderedSuf [c]))) )" sorry +(* +lemma simp_helps_der_pierce_dB: + shows " rsimp + (rsimp_ALTs + (map (rder x) + (rdistinct rs {}))) = + rsimp (rsimp_ALTs (rdistinct (map (rder x) rs) {}))" + + sorry +*) +(* +lemma simp_helps_der_pierce_flts: + shows " rsimp + (rsimp_ALTs + (rdistinct + (map (rder x) + (rflts rs ) + ) {} + ) + ) = + rsimp (rsimp_ALTs (rdistinct (rflts (map (rder x) rs)) {}) )" + + sorry + +*) lemma rders__onechar: shows " (rders_simp r [c]) = (rsimp (rders r [c]))" @@ -484,29 +600,6 @@ )" sorry -lemma simp_helps_der_pierce_dB: - shows " rsimp - (rsimp_ALTs - (map (rder x) - (rdistinct rs {}))) = - rsimp (rsimp_ALTs (rdistinct (map (rder x) rs) {}))" - - sorry - -lemma simp_helps_der_pierce_flts: - shows " rsimp - (rsimp_ALTs - (rdistinct - (map (rder x) - (rflts rs ) - ) {} - ) - ) = - rsimp (rsimp_ALTs (rdistinct (rflts (map (rder x) rs)) {}) )" - - sorry - - lemma unfold_ders_simp_inside_only: shows " (rders_simp (RSEQ r1 r2) xs = rsimp (RALTS (RSEQ (rders_simp r1 xs) r2 # rders_cond_list r2 (nullable_bools r1 (orderedPref xs)) (orderedSuf xs)))) @@ -594,6 +687,55 @@ sorry + + +lemma seq_update_seq_ders: + shows "rsimp (rder c ( rsimp (RALTS ((RSEQ (rders_simp r1 s) r2) # +(map (rders_simp r2) Ss))))) = + rsimp (RALTS ((RSEQ (rders_simp r1 (s @ [c])) r2) # +(map (rders_simp r2) (seq_update c (rders_simp r1 s) Ss)))) " + sorry + +lemma seq_ders_closed_form1: + shows "\Ss. rders_simp (RSEQ r1 r2) [c] = rsimp (RALTS ((RSEQ (rders_simp r1 [c]) r2) # +(map ( rders_simp r2 ) Ss)))" + apply(case_tac "rnullable r1") + apply(subgoal_tac " rders_simp (RSEQ r1 r2) [c] = +rsimp (RALTS ((RSEQ (rders_simp r1 [c]) r2) # (map (rders_simp r2) [[c]])))") + prefer 2 + apply (simp add: rsimp_idem) + apply(rule_tac x = "[[c]]" in exI) + apply simp + apply(subgoal_tac " rders_simp (RSEQ r1 r2) [c] = +rsimp (RALTS ((RSEQ (rders_simp r1 [c]) r2) # (map (rders_simp r2) [])))") + apply blast + apply(simp add: rsimp_idem) + sorry + + + +lemma seq_ders_closed_form: + shows "s \ [] \ \Ss. rders_simp (RSEQ r1 r2) s = rsimp (RALTS ((RSEQ (rders_simp r1 s) r2) # +(map ( rders_simp r2 ) Ss)))" + apply(induct s rule: rev_induct) + apply simp + apply(case_tac xs) + using seq_ders_closed_form1 apply auto[1] + apply(subgoal_tac "\Ss. rders_simp (RSEQ r1 r2) xs = rsimp (RALTS (RSEQ (rders_simp r1 xs) r2 # map (rders_simp r2) Ss))") + prefer 2 + + apply blast + apply(erule exE) + apply(rule_tac x = "seq_update x (rders_simp r1 xs) Ss" in exI) + apply(subst rders_simp_append) + apply(subgoal_tac "rders_simp (rders_simp (RSEQ r1 r2) xs) [x] = rsimp (rder x (rders_simp (RSEQ r1 r2) xs))") + apply(simp only:) + apply(subst seq_update_seq_ders) + apply blast + using rders_simp_one_char by presburger + + + (*nullable_seq_with_list1 related *) lemma LHS0_def_der_alt: shows "rsimp (rder x (RALTS ( (RSEQ (rders_simp r1 xs) r2) # (cond_list r1 r2 xs)))) = @@ -710,32 +852,6 @@ apply simp using r1r2ders_whole rders_simp_append rders_simp_one_char by presburger -(* - - apply(subgoal_tac " rsimp - (rder x - (rsimp_ALTs - (rdistinct - (rflts - (rsimp_SEQ (rsimp (rders_simp r1 xs)) (rsimp r2) # - map rsimp - (rders_cond_list r2 (nullable_bools r1 (orderedPrefAux (length xs) xs)) (orderedSufAux (length xs) xs)))) - {}))) = - rsimp - ( - (rsimp_ALTs - (map (rder x) - (rdistinct - (rflts - (rsimp_SEQ (rsimp (rders_simp r1 xs)) (rsimp r2) # - map rsimp - (rders_cond_list r2 (nullable_bools r1 (orderedPrefAux (length xs) xs)) (orderedSufAux (length xs) xs)))) - {}) - ) - ) - ) ") - prefer 2 -*) lemma shape_derssimp_alts: shows "s \ [] \ rders_simp (RALTS rs) s = rsimp (RALTS (map (\r. rders r s) rs))" @@ -751,13 +867,10 @@ |"rexp_encode (RCHAR c) = 2" |"rexp_encode (RSEQ r1 r2) = ( 2 ^ (rexp_encode r1)) " *) -lemma finite_chars: - shows " \N. ( (\r \ (set rs). \c. r = RCHAR c) \ (distinct rs) \ length rs < N)" - apply(rule_tac x = "Suc 256" in exI) - sorry + -definition all_chars :: "int \ char list" - where "all_chars n = map char_of [0..n]" + + (* fun rexp_enum :: "nat \ rrexp list" where @@ -766,26 +879,150 @@ |"rexp_enum (Suc n) = [(RSEQ r1 r2). r1 \ set (rexp_enum i) \ r2 \ set (rexp_enum j) \ i + j = n]" *) - -fun rexp_enum :: "nat \ rrexp set" +context notes rev_conj_cong[fundef_cong] begin +function (sequential) rexp_enum :: "nat \ rrexp set" where "rexp_enum 0 = {}" -|"rexp_enum (Suc 0) = {RALTS []} \ {RZERO} \ {RONE} \ { (RCHAR c) |c. c \ set (all_chars 255)}" -|"rexp_enum (Suc n) = {(RSEQ r1 r2)|r1 r2 i j. r1 \ (rexp_enum i) \ r2 \ (rexp_enum j) \ i + j = n}" +|"rexp_enum (Suc 0) = {RALTS []} \ {RZERO} \ {RONE} \ { (RCHAR c) |c. c \{Achar, Bchar, Cchar, Dchar} }" +|"rexp_enum (Suc n) = {(RSEQ r1 r2)|r1 r2 i j. r1 \ (rexp_enum i) \ r2 \ (rexp_enum j) \ i + j = n} \ +{(RALTS (a # rs)) | a rs i j. a \ (rexp_enum i) \ (RALTS rs) \ (rexp_enum j) \ i + j = Suc n \ i \ n \ j \ n} \ +{RSTAR r0 | r0. r0 \ (rexp_enum n)} \ +(rexp_enum n)" + by pat_completeness auto +termination + by (relation "measure size") auto +end + +lemma rexp_enum_inclusion: + shows "(rexp_enum n) \ (rexp_enum (Suc n))" + apply(induct n) + apply auto[1] + apply(simp) + done + +lemma rexp_enum_mono: + shows "n \ m \ (rexp_enum n) \ (rexp_enum m)" + by (simp add: lift_Suc_mono_le rexp_enum_inclusion) + +lemma enum_inductive_cases: + shows "rsize (RSEQ r1 r2) = Suc n \ \i j. rsize r1 = i \ rsize r2 = j\ i + j = n" + by (metis Suc_inject rsize.simps(5)) +thm rsize.simps(5) + +lemma enumeration_finite: + shows "\Nn. card (rexp_enum n) < Nn" + apply(simp add:no_top_class.gt_ex) + done + +lemma size1_rexps: + shows "RCHAR x \ rexp_enum 1" + apply(cases x) + apply auto + done + +lemma non_zero_size: + shows "rsize r \ Suc 0" + apply(induct r) + apply auto done + +corollary size_geq1: + shows "rsize r \ 1" + by (simp add: non_zero_size) + + +lemma rexp_size_induct: + shows "\N r x5 a list. + \ rsize r = Suc N; r = RALTS x5; + x5 = a # list\ \\i j. rsize a = i \ rsize (RALTS list) = j \ i + j = Suc N \ i \ N \ j \ N" + apply(rule_tac x = "rsize a" in exI) + apply(rule_tac x = "rsize (RALTS list)" in exI) + apply(subgoal_tac "rsize a \ 1") + prefer 2 + using One_nat_def non_zero_size apply presburger + apply(subgoal_tac "rsize (RALTS list) \ 1 ") + prefer 2 + using size_geq1 apply blast + apply simp + done - -lemma finite_sized_rexp_forms_finite_set: - shows " \SN. ( \r.( rsize r < N \ r \ SN)) \ (finite SN)" - apply(induct N) +lemma rexp_enum_case3: + shows "N \ Suc 0 \ rexp_enum (Suc N) = {(RSEQ r1 r2)|r1 r2 i j. r1 \ (rexp_enum i) \ r2 \ (rexp_enum j) \ i + j = N} \ +{(RALTS (a # rs)) | a rs i j. a \ (rexp_enum i) \ (RALTS rs) \ (rexp_enum j) \ i + j = Suc N \ i \ N \ j \ N} \ +{RSTAR r0 | r0. r0 \ (rexp_enum N)} \ +(rexp_enum N)" + apply(case_tac N) apply simp - apply auto - (*\\r. rsize r < N \ r \ SN; finite SN\ \ \SN. (\r. rsize r < Suc N \ r \ SN) \ finite SN*) - (* \N. \SN. (\r. rsize r < N \ r \ SN) \ finite SN \ \SN. (\r. rsize r < Suc N \ r \ SN) \ finite SN*) + apply auto + done + + + +lemma def_enum_alts: + shows "\ r = RALTS x5; x5 = a # list; + rsize a = i \ rsize (RALTS list) = j \ i + j = Suc N \ a \ (rexp_enum i) \ (RALTS list) \ (rexp_enum j) \ + \ r \ rexp_enum (Suc N)" + apply(subgoal_tac "N \ 1") + prefer 2 + apply (metis One_nat_def add_is_1 less_Suc0 linorder_le_less_linear non_zero_size) + apply(subgoal_tac " rexp_enum (Suc N) = {(RSEQ r1 r2)|r1 r2 i j. r1 \ (rexp_enum i) \ r2 \ (rexp_enum j) \ i + j = N} \ +{(RALTS (a # rs)) | a rs i j. a \ (rexp_enum i) \ (RALTS rs) \ (rexp_enum j) \ i + j = Suc N\ i \ N \ j \ N} \ +{RSTAR r0 | r0. r0 \ (rexp_enum N)} \ +(rexp_enum N)") + prefer 2 + using One_nat_def rexp_enum_case3 apply presburger + apply(subgoal_tac "i \ N \ j \ N") + prefer 2 + using non_zero_size apply auto[1] + apply(subgoal_tac "r \ {uu. + \a rs i j. uu = RALTS (a # rs) \ a \ rexp_enum i \ RALTS rs \ rexp_enum j \ i + j = Suc N \ i \ N \ j \ N}") + apply auto[1] + apply(subgoal_tac "RALTS (a # list) \ {uu. + \a rs i j. uu = RALTS (a # rs) \ a \ rexp_enum i \ RALTS rs \ rexp_enum j \ i + j = Suc N \ i \ N \ j \ N}") + + + apply fastforce + apply(subgoal_tac "a \ rexp_enum i") + prefer 2 + + apply linarith + by blast + +lemma rexp_enum_covers: + shows " rsize r \ N \ r \ rexp_enum N \ r \ rexp_enum (rsize r)" + apply(induct N arbitrary : r) + apply simp + + using rsize.elims apply auto[1] + apply(case_tac "rsize r \ N") + using enumeration_finite + + apply (meson in_mono rexp_enum_inclusion) + apply(subgoal_tac "rsize r = Suc N") + prefer 2 + using le_Suc_eq apply blast + + apply(case_tac r) + prefer 5 + apply(case_tac x5) + apply(subgoal_tac "rsize r =1") + prefer 2 + using hand_made_def_rlist_size rlist_size.simps(2) rsize.simps(4) apply presburger + apply simp + apply(subgoal_tac "a \ rexp_enum (rsize a)") + apply(subgoal_tac "RALTS list \ rexp_enum (rsize (RALTS list))") + + apply (meson def_enum_alts rexp_size_induct) + apply(subgoal_tac "rsize (RALTS list) \ N") + apply(subgoal_tac "RALTS list \ rexp_enum N") + prefer 2 + apply presburger + sorry lemma finite_size_finite_regx: shows " \l. \rs. ((\r \ (set rs). rsize r < N) \ (distinct rs) \ (length rs) < l) " + sorry (*below probably needs proved concurrently*)