# HG changeset patch # User blanchet # Date 1709717985 -3600 # Node ID abb5e57c92a798c791cda46c46493d1e08ecdd00 # Parent 2746dfc9ceaea9b32fd36d68b340cb747f947326 more multiset lemmas diff -r 2746dfc9ceae -r abb5e57c92a7 src/HOL/Finite_Set.thy --- a/src/HOL/Finite_Set.thy Tue Feb 27 13:46:42 2024 +0100 +++ b/src/HOL/Finite_Set.thy Wed Mar 06 10:39:45 2024 +0100 @@ -1032,6 +1032,22 @@ text \Other properties of \<^const>\fold\:\ +lemma finite_set_fold_single [simp]: "Finite_Set.fold f z {x} = f x z" +proof - + have "fold_graph f z {x} (f x z)" + by (auto intro: fold_graph.intros) + moreover + { + fix X y + have "fold_graph f z X y \ (X = {} \ y = z) \ (X = {x} \ y = f x z)" + by (induct rule: fold_graph.induct) auto + } + ultimately have "(THE y. fold_graph f z {x} y) = f x z" + by blast + thus ?thesis + by (simp add: Finite_Set.fold_def) +qed + lemma fold_graph_image: assumes "inj_on g A" shows "fold_graph f z (g ` A) = fold_graph (f \ g) z A" diff -r 2746dfc9ceae -r abb5e57c92a7 src/HOL/Library/Multiset.thy --- a/src/HOL/Library/Multiset.thy Tue Feb 27 13:46:42 2024 +0100 +++ b/src/HOL/Library/Multiset.thy Wed Mar 06 10:39:45 2024 +0100 @@ -243,6 +243,9 @@ with that show ?thesis by blast qed +lemma count_gt_imp_in_mset: "count M x > n \ x \# M" + using count_greater_zero_iff by fastforce + subsubsection \Union\ @@ -393,6 +396,11 @@ abbreviation Max_mset :: "'a::linorder multiset \ 'a" where "Max_mset m \ Max (set_mset m)" +lemma + Min_in_mset: "M \ {#} \ Min_mset M \# M" and + Max_in_mset: "M \ {#} \ Max_mset M \# M" + by simp+ + subsubsection \Equality of multisets\ @@ -684,6 +692,12 @@ using A by (simp add: mset_subset_eq_add_mset_cancel) qed simp +lemma nonempty_subseteq_mset_eq_single: "M \ {#} \ M \# {#x#} \ M = {#x#}" + by (cases M) (metis single_is_union subset_mset.less_eqE) + +lemma nonempty_subseteq_mset_iff_single: "(M \ {#} \ M \# {#x#} \ P) \ M = {#x#} \ P" + by (cases M) (metis empty_not_add_mset nonempty_subseteq_mset_eq_single subset_mset.order_refl) + subsubsection \Intersection and bounded union\ @@ -1374,6 +1388,9 @@ unfolding \M = M'\ using assms by (auto intro: filter_mset_cong0) +lemma filter_eq_replicate_mset: "{#y \# D. y = x#} = replicate_mset (count D x) x" + by (induct D) (simp add: multiset_eqI) + subsubsection \Size\ @@ -1480,6 +1497,9 @@ "M \# M' \ size (M' - M) = size M' - size(M::'a multiset)" by (metis add_diff_cancel_left' size_union mset_subset_eq_exists_conv) +lemma size_lt_imp_ex_count_lt: "size M < size N \ \x \# N. count M x < count N x" + by (metis count_eq_zero_iff leD not_le_imp_less not_less_zero size_mset_mono subseteq_mset_def) + subsection \Induction and case splits\ @@ -1644,6 +1664,9 @@ lemma fold_mset_empty [simp]: "fold_mset f s {#} = s" by (simp add: fold_mset_def) +lemma fold_mset_single [simp]: "fold_mset f s {#x#} = f x s" + by (simp add: fold_mset_def) + context comp_fun_commute begin @@ -1674,9 +1697,6 @@ qed qed -corollary fold_mset_single: "fold_mset f s {#x#} = f x s" - by simp - lemma fold_mset_fun_left_comm: "f x (fold_mset f s M) = fold_mset f (f x s) M" by (induct M) (simp_all add: fun_left_comm) @@ -1849,7 +1869,6 @@ "image_mset f (filter_mset (\x. P (f x)) M) = filter_mset P (image_mset f M)" by (induction M rule: multiset_induct) simp_all - lemma image_mset_eq_plusD: "image_mset f A = B + C \ \B' C'. A = B' + C' \ B = image_mset f B' \ C = image_mset f C'" proof (induction A arbitrary: B C) @@ -2278,7 +2297,7 @@ qed -subsection \More properties of the replicate and repeat operations\ +subsection \More properties of the replicate, repeat, and image operations\ lemma in_replicate_mset[simp]: "x \# replicate_mset n y \ n > 0 \ x = y" unfolding replicate_mset_def by (induct n) auto @@ -2292,9 +2311,6 @@ lemma count_le_replicate_mset_subset_eq: "n \ count M x \ replicate_mset n x \# M" by (auto simp add: mset_subset_eqI) (metis count_replicate_mset subseteq_mset_def) -lemma filter_eq_replicate_mset: "{#y \# D. y = x#} = replicate_mset (count D x) x" - by (induct D) simp_all - lemma replicate_count_mset_eq_filter_eq: "replicate (count (mset xs) k) k = filter (HOL.eq k) xs" by (induct xs) auto @@ -2354,6 +2370,195 @@ then show thesis using A .. qed +lemma count_image_mset_lt_imp_lt_raw: + assumes + "finite A" and + "A = set_mset M \ set_mset N" and + "count (image_mset f M) b < count (image_mset f N) b" + shows "\x. f x = b \ count M x < count N x" + using assms +proof (induct A arbitrary: M N b rule: finite_induct) + case (insert x F) + note fin = this(1) and x_ni_f = this(2) and ih = this(3) and x_f_eq_m_n = this(4) and + cnt_b = this(5) + + let ?Ma = "{#y \# M. y \ x#}" + let ?Mb = "{#y \# M. y = x#}" + let ?Na = "{#y \# N. y \ x#}" + let ?Nb = "{#y \# N. y = x#}" + + have m_part: "M = ?Mb + ?Ma" and n_part: "N = ?Nb + ?Na" + using multiset_partition by blast+ + + have f_eq_ma_na: "F = set_mset ?Ma \ set_mset ?Na" + using x_f_eq_m_n x_ni_f by auto + + show ?case + proof (cases "count (image_mset f ?Ma) b < count (image_mset f ?Na) b") + case cnt_ba: True + obtain xa where "f xa = b" and "count ?Ma xa < count ?Na xa" + using ih[OF f_eq_ma_na cnt_ba] by blast + thus ?thesis + by (metis count_filter_mset not_less0) + next + case cnt_ba: False + have fx_eq_b: "f x = b" + using cnt_b cnt_ba + by (subst (asm) m_part, subst (asm) n_part, + auto simp: filter_eq_replicate_mset split: if_splits) + moreover have "count M x < count N x" + using cnt_b cnt_ba + by (subst (asm) m_part, subst (asm) n_part, + auto simp: filter_eq_replicate_mset split: if_splits) + ultimately show ?thesis + by blast + qed +qed auto + +lemma count_image_mset_lt_imp_lt: + assumes cnt_b: "count (image_mset f M) b < count (image_mset f N) b" + shows "\x. f x = b \ count M x < count N x" + by (rule count_image_mset_lt_imp_lt_raw[of "set_mset M \ set_mset N", OF _ refl cnt_b]) auto + +lemma count_image_mset_le_imp_lt_raw: + assumes + "finite A" and + "A = set_mset M \ set_mset N" and + "count (image_mset f M) (f a) + count N a < count (image_mset f N) (f a) + count M a" + shows "\b. f b = f a \ count M b < count N b" + using assms +proof (induct A arbitrary: M N rule: finite_induct) + case (insert x F) + note fin = this(1) and x_ni_f = this(2) and ih = this(3) and x_f_eq_m_n = this(4) and + cnt_lt = this(5) + + let ?Ma = "{#y \# M. y \ x#}" + let ?Mb = "{#y \# M. y = x#}" + let ?Na = "{#y \# N. y \ x#}" + let ?Nb = "{#y \# N. y = x#}" + + have m_part: "M = ?Mb + ?Ma" and n_part: "N = ?Nb + ?Na" + using multiset_partition by blast+ + + have f_eq_ma_na: "F = set_mset ?Ma \ set_mset ?Na" + using x_f_eq_m_n x_ni_f by auto + + show ?case + proof (cases "f x = f a") + case fx_ne_fa: False + + have cnt_fma_fa: "count (image_mset f ?Ma) (f a) = count (image_mset f M) (f a)" + using fx_ne_fa by (subst (2) m_part) (auto simp: filter_eq_replicate_mset) + have cnt_fna_fa: "count (image_mset f ?Na) (f a) = count (image_mset f N) (f a)" + using fx_ne_fa by (subst (2) n_part) (auto simp: filter_eq_replicate_mset) + have cnt_ma_a: "count ?Ma a = count M a" + using fx_ne_fa by (subst (2) m_part) (auto simp: filter_eq_replicate_mset) + have cnt_na_a: "count ?Na a = count N a" + using fx_ne_fa by (subst (2) n_part) (auto simp: filter_eq_replicate_mset) + + obtain b where fb_eq_fa: "f b = f a" and cnt_b: "count ?Ma b < count ?Na b" + using ih[OF f_eq_ma_na] cnt_lt unfolding cnt_fma_fa cnt_fna_fa cnt_ma_a cnt_na_a by blast + have fx_ne_fb: "f x \ f b" + using fb_eq_fa fx_ne_fa by simp + + have cnt_ma_b: "count ?Ma b = count M b" + using fx_ne_fb by (subst (2) m_part) auto + have cnt_na_b: "count ?Na b = count N b" + using fx_ne_fb by (subst (2) n_part) auto + + show ?thesis + using fb_eq_fa cnt_b unfolding cnt_ma_b cnt_na_b by blast + next + case fx_eq_fa: True + show ?thesis + proof (cases "x = a") + case x_eq_a: True + have "count (image_mset f ?Ma) (f a) + count ?Na a + < count (image_mset f ?Na) (f a) + count ?Ma a" + using cnt_lt x_eq_a by (subst (asm) (1 2) m_part, subst (asm) (1 2) n_part, + auto simp: filter_eq_replicate_mset) + thus ?thesis + using ih[OF f_eq_ma_na] by (metis count_filter_mset nat_neq_iff) + next + case x_ne_a: False + show ?thesis + proof (cases "count M x < count N x") + case True + thus ?thesis + using fx_eq_fa by blast + next + case False + hence cnt_x: "count M x \ count N x" + by fastforce + have "count M x + count (image_mset f ?Ma) (f a) + count ?Na a + < count N x + count (image_mset f ?Na) (f a) + count ?Ma a" + using cnt_lt x_ne_a fx_eq_fa by (subst (asm) (1 2) m_part, subst (asm) (1 2) n_part, + auto simp: filter_eq_replicate_mset) + hence "count (image_mset f ?Ma) (f a) + count ?Na a + < count (image_mset f ?Na) (f a) + count ?Ma a" + using cnt_x by linarith + thus ?thesis + using ih[OF f_eq_ma_na] by (metis count_filter_mset nat_neq_iff) + qed + qed + qed +qed auto + +lemma count_image_mset_le_imp_lt: + assumes + "count (image_mset f M) (f a) \ count (image_mset f N) (f a)" and + "count M a > count N a" + shows "\b. f b = f a \ count M b < count N b" + using assms by (auto intro: count_image_mset_le_imp_lt_raw[of "set_mset M \ set_mset N"]) + +lemma size_filter_unsat_elem: + assumes "x \# M" and "\ P x" + shows "size {#x \# M. P x#} < size M" +proof - + have "size (filter_mset P M) \ size M" + using assms by (metis add.right_neutral add_diff_cancel_left' count_filter_mset mem_Collect_eq + multiset_partition nonempty_has_size set_mset_def size_union) + then show ?thesis + by (meson leD nat_neq_iff size_filter_mset_lesseq) +qed + +lemma size_filter_ne_elem: "x \# M \ size {#y \# M. y \ x#} < size M" + by (simp add: size_filter_unsat_elem[of x M "\y. y \ x"]) + +lemma size_eq_ex_count_lt: + assumes + sz_m_eq_n: "size M = size N" and + m_eq_n: "M \ N" + shows "\x. count M x < count N x" +proof - + obtain x where "count M x \ count N x" + using m_eq_n by (meson multiset_eqI) + moreover + { + assume "count M x < count N x" + hence ?thesis + by blast + } + moreover + { + assume cnt_x: "count M x > count N x" + + have "size {#y \# M. y = x#} + size {#y \# M. y \ x#} = + size {#y \# N. y = x#} + size {#y \# N. y \ x#}" + using sz_m_eq_n multiset_partition by (metis size_union) + hence sz_m_minus_x: "size {#y \# M. y \ x#} < size {#y \# N. y \ x#}" + using cnt_x by (simp add: filter_eq_replicate_mset) + then obtain y where "count {#y \# M. y \ x#} y < count {#y \# N. y \ x#} y" + using size_lt_imp_ex_count_lt[OF sz_m_minus_x] by blast + hence "count M y < count N y" + by (metis count_filter_mset less_asym) + hence ?thesis + by blast + } + ultimately show ?thesis + by fastforce +qed + subsection \Big operators\ @@ -2571,7 +2776,6 @@ lemma Union_image_single_mset[simp]: "\\<^sub># (image_mset (\x. {#x#}) m) = m" by(induction m) auto - context comm_monoid_mult begin diff -r 2746dfc9ceae -r abb5e57c92a7 src/HOL/Library/Multiset_Order.thy --- a/src/HOL/Library/Multiset_Order.thy Tue Feb 27 13:46:42 2024 +0100 +++ b/src/HOL/Library/Multiset_Order.thy Wed Mar 06 10:39:45 2024 +0100 @@ -853,4 +853,63 @@ end +lemma add_mset_lt_left_lt: "a < b \ add_mset a A < add_mset b A" + by fastforce + +lemma add_mset_le_left_le: "a \ b \ add_mset a A \ add_mset b A" for a :: "'a :: linorder" + by fastforce + +lemma add_mset_lt_right_lt: "A < B \ add_mset a A < add_mset a B" + by fastforce + +lemma add_mset_le_right_le: "A \ B \ add_mset a A \ add_mset a B" + by fastforce + +lemma add_mset_lt_lt_lt: + assumes a_lt_b: "a < b" and A_le_B: "A < B" + shows "add_mset a A < add_mset b B" + by (rule less_trans[OF add_mset_lt_left_lt[OF a_lt_b] add_mset_lt_right_lt[OF A_le_B]]) + +lemma add_mset_lt_lt_le: "a < b \ A \ B \ add_mset a A < add_mset b B" + using add_mset_lt_lt_lt le_neq_trans by fastforce + +lemma add_mset_lt_le_lt: "a \ b \ A < B \ add_mset a A < add_mset b B" for a :: "'a :: linorder" + using add_mset_lt_lt_lt by (metis add_mset_lt_right_lt le_less) + +lemma add_mset_le_le_le: + fixes a :: "'a :: linorder" + assumes a_le_b: "a \ b" and A_le_B: "A \ B" + shows "add_mset a A \ add_mset b B" + by (rule order.trans[OF add_mset_le_left_le[OF a_le_b] add_mset_le_right_le[OF A_le_B]]) + +lemma Max_lt_imp_lt_mset: + assumes n_nemp: "N \ {#}" and max: "Max_mset M < Max_mset N" (is "?max_M < ?max_N") + shows "M < N" +proof (cases "M = {#}") + case m_nemp: False + + have max_n_in_n: "?max_N \# N" + using n_nemp by simp + have max_n_nin_m: "?max_N \# M" + using max Max_ge leD by auto + + have "M \ N" + using max by auto + moreover + { + fix y + assume "count N y < count M y" + hence "y \# M" + by (simp add: count_inI) + hence "?max_M \ y" + by simp + hence "?max_N > y" + using max by auto + hence "\x > y. count M x < count N x" + using max_n_nin_m max_n_in_n count_inI by force + } + ultimately show ?thesis + unfolding less_multiset\<^sub>H\<^sub>O by blast +qed (auto simp: n_nemp) + end