diff -r f493a20feeb3 -r 04b5e904a220 thys3/ClosedFormsBounds.thy --- a/thys3/ClosedFormsBounds.thy Sat Apr 30 00:50:08 2022 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,448 +0,0 @@ - -theory ClosedFormsBounds - imports "GeneralRegexBound" "ClosedForms" -begin -lemma alts_ders_lambda_shape_ders: - shows "\r \ set (map (\r. rders_simp r ( s)) rs ). \r1 \ set rs. r = rders_simp r1 s" - by (simp add: image_iff) - -lemma rlist_bound: - assumes "\r \ set rs. rsize r \ N" - shows "rsizes rs \ N * (length rs)" - using assms - apply(induct rs) - apply simp - by simp - -lemma alts_closed_form_bounded: - assumes "\r \ set rs. \s. rsize (rders_simp r s) \ N" - shows "rsize (rders_simp (RALTS rs) s) \ max (Suc (N * (length rs))) (rsize (RALTS rs))" -proof (cases s) - case Nil - then show "rsize (rders_simp (RALTS rs) s) \ max (Suc (N * length rs)) (rsize (RALTS rs))" - by simp -next - case (Cons a s) - - from assms have "\r \ set (map (\r. rders_simp r (a # s)) rs ). rsize r \ N" - by (metis alts_ders_lambda_shape_ders) - then have a: "rsizes (map (\r. rders_simp r (a # s)) rs ) \ N * (length rs)" - by (metis length_map rlist_bound) - - have "rsize (rders_simp (RALTS rs) (a # s)) - = rsize (rsimp (RALTS (map (\r. rders_simp r (a # s)) rs)))" - by (metis alts_closed_form_variant list.distinct(1)) - also have "... \ rsize (RALTS (map (\r. rders_simp r (a # s)) rs))" - using rsimp_mono by blast - also have "... = Suc (rsizes (map (\r. rders_simp r (a # s)) rs))" - by simp - also have "... \ Suc (N * (length rs))" - using a by blast - finally have "rsize (rders_simp (RALTS rs) (a # s)) \ max (Suc (N * length rs)) (rsize (RALTS rs))" - by auto - then show ?thesis using local.Cons by simp -qed - -lemma alts_simp_ineq_unfold: - shows "rsize (rsimp (RALTS rs)) \ Suc (rsizes (rdistinct (rflts (map rsimp rs)) {}))" - using rsimp_aalts_smaller by auto - - -lemma rdistinct_mono_list: - shows "rsizes (rdistinct (x5 @ rs) rset) \ rsizes x5 + rsizes (rdistinct rs ((set x5 ) \ rset))" - apply(induct x5 arbitrary: rs rset) - apply simp - apply(case_tac "a \ rset") - apply simp - apply (simp add: add.assoc insert_absorb trans_le_add2) - apply simp - by (metis Un_insert_right) - - -lemma flts_size_reduction_alts: - assumes a: "\noalts_set alts_set corr_set. - (\r\noalts_set. \xs. r \ RALTS xs) \ - (\a\alts_set. \xs. a = RALTS xs \ set xs \ corr_set) \ - Suc (rsizes (rdistinct (rflts rs) (noalts_set \ corr_set))) - \ Suc (rsizes (rdistinct rs (insert RZERO (noalts_set \ alts_set))))" - and b: "\r\noalts_set. \xs. r \ RALTS xs" - and c: "\a\alts_set. \xs. a = RALTS xs \ set xs \ corr_set" - and d: "a = RALTS x5" - shows "rsizes (rdistinct (rflts (a # rs)) (noalts_set \ corr_set)) - \ rsizes (rdistinct (a # rs) (insert RZERO (noalts_set \ alts_set)))" - - apply(case_tac "a \ alts_set") - using a b c d - apply simp - apply(subgoal_tac "set x5 \ corr_set") - apply(subst rdistinct_concat) - apply auto[1] - apply presburger - apply fastforce - using a b c d - apply (subgoal_tac "a \ noalts_set") - prefer 2 - apply blast - apply simp - apply(subgoal_tac "rsizes (rdistinct (x5 @ rflts rs) (noalts_set \ corr_set)) - \ rsizes x5 + rsizes (rdistinct (rflts rs) ((set x5) \ (noalts_set \ corr_set)))") - prefer 2 - using rdistinct_mono_list apply presburger - apply(subgoal_tac "insert (RALTS x5) (noalts_set \ alts_set) = noalts_set \ (insert (RALTS x5) alts_set)") - apply(simp only:) - apply(subgoal_tac "rsizes x5 + rsizes (rdistinct (rflts rs) (noalts_set \ (corr_set \ (set x5)))) \ - rsizes x5 + rsizes (rdistinct rs (insert RZERO (noalts_set \ insert (RALTS x5) alts_set)))") - - apply (simp add: Un_left_commute inf_sup_aci(5)) - apply(subgoal_tac "rsizes (rdistinct (rflts rs) (noalts_set \ (corr_set \ set x5))) \ - rsizes (rdistinct rs (insert RZERO (noalts_set \ insert (RALTS x5) alts_set)))") - apply linarith - apply(subgoal_tac "\r \ insert (RALTS x5) alts_set. \xs1.( r = RALTS xs1 \ set xs1 \ corr_set \ set x5)") - apply presburger - apply (meson insert_iff sup.cobounded2 sup.coboundedI1) - by blast - - -lemma flts_vs_nflts1: - assumes "\r \ noalts_set. \xs. r \ RALTS xs" - and "\a \ alts_set. (\xs. a = RALTS xs \ set xs \ corr_set)" - shows "rsizes (rdistinct (rflts rs) (noalts_set \ corr_set)) - \ rsizes (rdistinct rs (insert RZERO (noalts_set \ alts_set)))" - using assms - apply(induct rs arbitrary: noalts_set alts_set corr_set) - apply simp - apply(case_tac a) - apply(case_tac "RZERO \ noalts_set") - apply simp - apply(subgoal_tac "RZERO \ alts_set") - apply simp - apply fastforce - apply(case_tac "RONE \ noalts_set") - apply simp - apply(subgoal_tac "RONE \ alts_set") - prefer 2 - apply fastforce - apply(case_tac "RONE \ corr_set") - apply(subgoal_tac "rflts (a # rs) = RONE # rflts rs") - apply(simp only:) - apply(subgoal_tac "rdistinct (RONE # rflts rs) (noalts_set \ corr_set) = - rdistinct (rflts rs) (noalts_set \ corr_set)") - apply(simp only:) - apply(subgoal_tac "rdistinct (RONE # rs) (insert RZERO (noalts_set \ alts_set)) = - RONE # (rdistinct rs (insert RONE (insert RZERO (noalts_set \ alts_set)))) ") - apply(simp only:) - apply(subgoal_tac "rdistinct (rflts rs) (noalts_set \ corr_set) = - rdistinct (rflts rs) (insert RONE (noalts_set \ corr_set))") - apply (simp only:) - apply(subgoal_tac "insert RONE (noalts_set \ corr_set) = (insert RONE noalts_set) \ corr_set") - apply(simp only:) - apply(subgoal_tac "insert RONE (insert RZERO (noalts_set \ alts_set)) = - insert RZERO ((insert RONE noalts_set) \ alts_set)") - apply(simp only:) - apply(subgoal_tac "rsizes (rdistinct rs (insert RZERO (insert RONE noalts_set \ alts_set))) - \ rsizes (RONE # rdistinct rs (insert RZERO (insert RONE noalts_set \ alts_set)))") - apply (smt (verit, best) dual_order.trans insert_iff rrexp.distinct(15)) - apply (metis (no_types, opaque_lifting) le_add_same_cancel2 list.simps(9) sum_list.Cons zero_le) - apply fastforce - apply fastforce - apply (metis Un_iff insert_absorb) - apply (metis UnE insertE insert_is_Un rdistinct.simps(2) rrexp.distinct(1)) - apply (meson UnCI rdistinct.simps(2)) - using rflts.simps(4) apply presburger - apply simp - apply(subgoal_tac "insert RONE (noalts_set \ corr_set) = (insert RONE noalts_set) \ corr_set") - apply(simp only:) - apply (metis Un_insert_left insertE rrexp.distinct(15)) - apply fastforce - apply(case_tac "a \ noalts_set") - apply simp - apply(subgoal_tac "a \ alts_set") - prefer 2 - apply blast - apply(case_tac "a \ corr_set") - apply(subgoal_tac "noalts_set \ corr_set = insert a ( noalts_set \ corr_set)") - prefer 2 - apply fastforce - apply(simp only:) - apply(subgoal_tac "rsizes (rdistinct (a # rs) (insert RZERO ((insert a noalts_set) \ alts_set))) \ - rsizes (rdistinct (a # rs) (insert RZERO (noalts_set \ alts_set)))") - - apply(subgoal_tac "rsizes (rdistinct (rflts (a # rs)) ((insert a noalts_set) \ corr_set)) \ - rsizes (rdistinct (a # rs) (insert RZERO ((insert a noalts_set) \ alts_set)))") - apply fastforce - apply simp - apply(subgoal_tac "(insert a (noalts_set \ alts_set)) = (insert a noalts_set) \ alts_set") - apply(simp only:) - apply(subgoal_tac "noalts_set \ corr_set = (insert a noalts_set) \ corr_set") - apply(simp only:) - apply (metis insertE rrexp.distinct(21)) - apply blast - - apply fastforce - apply force - apply simp - apply (metis Un_insert_left insert_iff rrexp.distinct(21)) - apply(case_tac "a \ noalts_set") - apply simp - apply(subgoal_tac "a \ alts_set") - prefer 2 - apply blast - apply(case_tac "a \ corr_set") - apply(subgoal_tac "noalts_set \ corr_set = insert a ( noalts_set \ corr_set)") - prefer 2 - apply fastforce - apply(simp only:) - apply(subgoal_tac "rsizes (rdistinct (a # rs) (insert RZERO ((insert a noalts_set) \ alts_set))) \ - rsizes (rdistinct (a # rs) (insert RZERO (noalts_set \ alts_set)))") - - apply(subgoal_tac "rsizes (rdistinct (rflts (a # rs)) ((insert a noalts_set) \ corr_set)) \ - rsizes (rdistinct (a # rs) (insert RZERO ((insert a noalts_set) \ alts_set)))") - apply fastforce - apply simp - apply(subgoal_tac "(insert a (noalts_set \ alts_set)) = (insert a noalts_set) \ alts_set") - apply(simp only:) - apply(subgoal_tac "noalts_set \ corr_set = (insert a noalts_set) \ corr_set") - apply(simp only:) - - - apply (metis insertE rrexp.distinct(25)) - apply blast - apply fastforce - apply force - apply simp - - apply (metis Un_insert_left insertE rrexp.distinct(25)) - - using Suc_le_mono flts_size_reduction_alts apply presburger - apply(case_tac "a \ noalts_set") - apply simp - apply(subgoal_tac "a \ alts_set") - prefer 2 - apply blast - apply(case_tac "a \ corr_set") - apply(subgoal_tac "noalts_set \ corr_set = insert a ( noalts_set \ corr_set)") - prefer 2 - apply fastforce - apply(simp only:) - apply(subgoal_tac "rsizes (rdistinct (a # rs) (insert RZERO ((insert a noalts_set) \ alts_set))) \ - rsizes (rdistinct (a # rs) (insert RZERO (noalts_set \ alts_set)))") - - apply(subgoal_tac "rsizes (rdistinct (rflts (a # rs)) ((insert a noalts_set) \ corr_set)) \ - rsizes (rdistinct (a # rs) (insert RZERO ((insert a noalts_set) \ alts_set)))") - apply fastforce - apply simp - apply(subgoal_tac "(insert a (noalts_set \ alts_set)) = (insert a noalts_set) \ alts_set") - apply(simp only:) - apply(subgoal_tac "noalts_set \ corr_set = (insert a noalts_set) \ corr_set") - apply(simp only:) - apply (metis insertE rrexp.distinct(29)) - - apply blast - - apply fastforce - apply force - apply simp - apply (metis Un_insert_left insert_iff rrexp.distinct(29)) - done - - -lemma flts_vs_nflts: - assumes "\r \ noalts_set. \xs. r \ RALTS xs" - and "\a \ alts_set. (\xs. a = RALTS xs \ set xs \ corr_set)" - shows "rsizes (rdistinct (rflts rs) (noalts_set \ corr_set)) - \ rsizes (rdistinct rs (insert RZERO (noalts_set \ alts_set)))" - by (simp add: assms flts_vs_nflts1) - -lemma distinct_simp_ineq_general: - assumes "rsimp ` no_simp = has_simp" "finite no_simp" - shows "rsizes (rdistinct (map rsimp rs) has_simp) \ rsizes (rdistinct rs no_simp)" - using assms - apply(induct rs no_simp arbitrary: has_simp rule: rdistinct.induct) - apply simp - apply(auto) - using add_le_mono rsimp_mono by presburger - -lemma larger_acc_smaller_distinct_res0: - assumes "ss \ SS" - shows "rsizes (rdistinct rs SS) \ rsizes (rdistinct rs ss)" - using assms - apply(induct rs arbitrary: ss SS) - apply simp - by (metis distinct_early_app1 rdistinct_smaller) - -lemma without_flts_ineq: - shows "rsizes (rdistinct (rflts rs) {}) \ rsizes (rdistinct rs {})" -proof - - have "rsizes (rdistinct (rflts rs) {}) \ rsizes (rdistinct rs (insert RZERO {}))" - by (metis empty_iff flts_vs_nflts sup_bot_left) - also have "... \ rsizes (rdistinct rs {})" - by (simp add: larger_acc_smaller_distinct_res0) - finally show ?thesis - by blast -qed - - -lemma distinct_simp_ineq: - shows "rsizes (rdistinct (map rsimp rs) {}) \ rsizes (rdistinct rs {})" - using distinct_simp_ineq_general by blast - - -lemma alts_simp_control: - shows "rsize (rsimp (RALTS rs)) \ Suc (rsizes (rdistinct rs {}))" -proof - - have "rsize (rsimp (RALTS rs)) \ Suc (rsizes (rdistinct (rflts (map rsimp rs)) {}))" - using alts_simp_ineq_unfold by auto - moreover have "\ \ Suc (rsizes (rdistinct (map rsimp rs) {}))" - using without_flts_ineq by blast - ultimately show "rsize (rsimp (RALTS rs)) \ Suc (rsizes (rdistinct rs {}))" - by (meson Suc_le_mono distinct_simp_ineq le_trans) -qed - - -lemma larger_acc_smaller_distinct_res: - shows "rsizes (rdistinct rs (insert a ss)) \ rsizes (rdistinct rs ss)" - by (simp add: larger_acc_smaller_distinct_res0 subset_insertI) - -lemma triangle_inequality_distinct: - shows "rsizes (rdistinct (a # rs) ss) \ rsize a + rsizes (rdistinct rs ss)" - apply(case_tac "a \ ss") - apply simp - by (simp add: larger_acc_smaller_distinct_res) - - -lemma distinct_list_size_len_bounded: - assumes "\r \ set rs. rsize r \ N" "length rs \ lrs" - shows "rsizes rs \ lrs * N " - using assms - by (metis rlist_bound dual_order.trans mult.commute mult_le_mono1) - - - -lemma rdistinct_same_set: - shows "r \ set rs \ r \ set (rdistinct rs {})" - apply(induct rs) - apply simp - by (metis rdistinct_set_equality) - -(* distinct_list_rexp_up_to_certain_size_bouded_by_set_enumerating_up_to_that_size *) -lemma distinct_list_rexp_upto: - assumes "\r\ set rs. (rsize r) \ N" - shows "rsizes (rdistinct rs {}) \ (card (sizeNregex N)) * N" - - apply(subgoal_tac "distinct (rdistinct rs {})") - prefer 2 - using rdistinct_does_the_job apply blast - apply(subgoal_tac "length (rdistinct rs {}) \ card (sizeNregex N)") - apply(rule distinct_list_size_len_bounded) - using assms - apply (meson rdistinct_same_set) - apply blast - apply(subgoal_tac "\r \ set (rdistinct rs {}). rsize r \ N") - prefer 2 - using assms - apply (meson rdistinct_same_set) - apply(subgoal_tac "length (rdistinct rs {}) = card (set (rdistinct rs {}))") - prefer 2 - apply (simp add: distinct_card) - apply(simp) - by (metis card_mono finite_size_n mem_Collect_eq sizeNregex_def subsetI) - - -lemma star_control_bounded: - assumes "\s. rsize (rders_simp r s) \ N" - shows "rsizes (rdistinct (map (\s1. RSEQ (rders_simp r s1) (RSTAR r)) (star_updates s r [[c]])) {}) - \ (card (sizeNregex (Suc (N + rsize (RSTAR r))))) * (Suc (N + rsize (RSTAR r)))" - by (smt (verit) add_Suc_shift add_mono_thms_linordered_semiring(3) assms distinct_list_rexp_upto image_iff list.set_map plus_nat.simps(2) rsize.simps(5)) - - -lemma star_closed_form_bounded: - assumes "\s. rsize (rders_simp r s) \ N" - shows "rsize (rders_simp (RSTAR r) s) \ - max ((Suc (card (sizeNregex (Suc (N + rsize (RSTAR r))))) * (Suc (N + rsize (RSTAR r))))) (rsize (RSTAR r))" -proof(cases s) - case Nil - then show "rsize (rders_simp (RSTAR r) s) - \ max (Suc (card (sizeNregex (Suc (N + rsize (RSTAR r))))) * Suc (N + rsize (RSTAR r))) (rsize (RSTAR r))" - by simp -next - case (Cons a list) - then have "rsize (rders_simp (RSTAR r) s) = - rsize (rsimp (RALTS ((map (\s1. RSEQ (rders_simp r s1) (RSTAR r)) (star_updates list r [[a]])))))" - using star_closed_form by fastforce - also have "... \ Suc (rsizes (rdistinct (map (\s1. RSEQ (rders_simp r s1) (RSTAR r)) (star_updates list r [[a]])) {}))" - using alts_simp_control by blast - also have "... \ Suc (card (sizeNregex (Suc (N + rsize (RSTAR r))))) * (Suc (N + rsize (RSTAR r)))" - using star_control_bounded[OF assms] by (metis add_mono le_add1 mult_Suc plus_1_eq_Suc) - also have "... \ max (Suc (card (sizeNregex (Suc (N + rsize (RSTAR r))))) * Suc (N + rsize (RSTAR r))) (rsize (RSTAR r))" - by simp - finally show ?thesis by simp -qed - - -lemma seq_estimate_bounded: - assumes "\s. rsize (rders_simp r1 s) \ N1" - and "\s. rsize (rders_simp r2 s) \ N2" - shows - "rsizes (rdistinct (RSEQ (rders_simp r1 s) r2 # map (rders_simp r2) (vsuf s r1)) {}) - \ (Suc (N1 + (rsize r2)) + (N2 * card (sizeNregex N2)))" -proof - - have a: "rsizes (rdistinct (map (rders_simp r2) (vsuf s r1)) {}) \ N2 * card (sizeNregex N2)" - by (metis assms(2) distinct_list_rexp_upto ex_map_conv mult.commute) - - have "rsizes (rdistinct (RSEQ (rders_simp r1 s) r2 # map (rders_simp r2) (vsuf s r1)) {}) \ - rsize (RSEQ (rders_simp r1 s) r2) + rsizes (rdistinct (map (rders_simp r2) (vsuf s r1)) {})" - using triangle_inequality_distinct by blast - also have "... \ rsize (RSEQ (rders_simp r1 s) r2) + N2 * card (sizeNregex N2)" - by (simp add: a) - also have "... \ Suc (N1 + (rsize r2) + N2 * card (sizeNregex N2))" - by (simp add: assms(1)) - finally show ?thesis - by force -qed - - -lemma seq_closed_form_bounded2: - assumes "\s. rsize (rders_simp r1 s) \ N1" - and "\s. rsize (rders_simp r2 s) \ N2" -shows "rsize (rders_simp (RSEQ r1 r2) s) - \ max (2 + N1 + (rsize r2) + (N2 * card (sizeNregex N2))) (rsize (RSEQ r1 r2))" -proof(cases s) - case Nil - then show "rsize (rders_simp (RSEQ r1 r2) s) - \ max (2 + N1 + (rsize r2) + (N2 * card (sizeNregex N2))) (rsize (RSEQ r1 r2))" - by simp -next - case (Cons a list) - then have "rsize (rders_simp (RSEQ r1 r2) s) = - rsize (rsimp (RALTS ((RSEQ (rders_simp r1 s) r2) # (map (rders_simp r2) (vsuf s r1)))))" - using seq_closed_form_variant by (metis list.distinct(1)) - also have "... \ Suc (rsizes (rdistinct (RSEQ (rders_simp r1 s) r2 # map (rders_simp r2) (vsuf s r1)) {}))" - using alts_simp_control by blast - also have "... \ 2 + N1 + (rsize r2) + (N2 * card (sizeNregex N2))" - using seq_estimate_bounded[OF assms] by auto - ultimately show "rsize (rders_simp (RSEQ r1 r2) s) - \ max (2 + N1 + (rsize r2) + N2 * card (sizeNregex N2)) (rsize (RSEQ r1 r2))" - by auto -qed - - -lemma rders_simp_bounded: - shows "\N. \s. rsize (rders_simp r s) \ N" - apply(induct r) - apply(rule_tac x = "Suc 0 " in exI) - using three_easy_cases0 apply force - using three_easy_cases1 apply blast - using three_easy_casesC apply blast - apply(erule exE)+ - apply(rule exI) - apply(rule allI) - apply(rule seq_closed_form_bounded2) - apply(assumption) - apply(assumption) - apply (metis alts_closed_form_bounded size_list_estimation') - using star_closed_form_bounded by blast - - -unused_thms - -end