# HG changeset patch # User haftmann # Date 1749136707 0 # Node ID b391142bd2d2934e36f8b3f74008dc9bdfbbb65a # Parent 97b9ac57751e8f24dd4b8d783c6f2c7781518823 prefer already existing operation to calculate minimum diff -r 97b9ac57751e -r b391142bd2d2 NEWS --- a/NEWS Wed Jun 04 19:43:13 2025 +0000 +++ b/NEWS Thu Jun 05 15:18:27 2025 +0000 @@ -95,6 +95,9 @@ const List.all_interval_nat List.all_interval_int discontinued in favour of a generic List.all_range + const List.Bleast + discontinued in favour of more concise List.Least as variant of Min + The _def suffix for characteristic theorems is avoided to emphasize that these auxiliary operations are candidates for unfolding since they are variants of existing logical concepts. The [simp] declarations try to move the attention diff -r 97b9ac57751e -r b391142bd2d2 src/HOL/Lattices_Big.thy --- a/src/HOL/Lattices_Big.thy Wed Jun 04 19:43:13 2025 +0000 +++ b/src/HOL/Lattices_Big.thy Thu Jun 05 15:18:27 2025 +0000 @@ -813,6 +813,24 @@ } from this [of "{a. P a}"] assms show ?thesis by simp qed +lemma Greatest_Max: + assumes "finite {a. P a}" and "\a. P a" + shows "(GREATEST a. P a) = Max {a. P a}" +proof - + { fix A :: "'a set" + assume A: "finite A" "A \ {}" + have "(GREATEST a. a \ A) = Max A" + using A proof (induct A rule: finite_ne_induct) + case singleton show ?case by (rule Greatest_equality) simp_all + next + case (insert a A) + have "(GREATEST b. b = a \ b \ A) = max a (GREATEST a. a \ A)" + by (auto intro!: Greatest_equality simp add: max_def not_le insert.hyps) + with insert show ?case by simp + qed + } from this [of "{a. P a}"] assms show ?thesis by simp +qed + lemma infinite_growing: assumes "X \ {}" assumes *: "\x. x \ X \ \y\X. y > x" diff -r 97b9ac57751e -r b391142bd2d2 src/HOL/Library/RBT_Set.thy --- a/src/HOL/Library/RBT_Set.thy Wed Jun 04 19:43:13 2025 +0000 +++ b/src/HOL/Library/RBT_Set.thy Thu Jun 05 15:18:27 2025 +0000 @@ -33,7 +33,7 @@ card the_elem Pow sum prod Product_Type.product Id_on Image trancl relcomp wf_on wf_code Min Inf_fin Max Sup_fin "(Inf :: 'a set set \ 'a set)" "(Sup :: 'a set set \ 'a set)" - sorted_list_of_set List.map_project List.Bleast]] + sorted_list_of_set List.map_project List.Least List.Greatest]] section \Lemmas\ @@ -769,34 +769,24 @@ end -lemma sorted_list_set[code]: "sorted_list_of_set (Set t) = RBT.keys t" +lemma sorted_list_set [code]: "sorted_list_of_set (Set t) = RBT.keys t" by (auto simp add: set_keys intro: sorted_distinct_set_unique) -lemma Bleast_code [code]: - "Bleast (Set t) P = - (case List.filter P (RBT.keys t) of - x # xs \ x - | [] \ abort_Bleast (Set t) P)" -proof (cases "List.filter P (RBT.keys t)") - case Nil - thus ?thesis by (simp add: Bleast_def abort_Bleast_def) -next - case (Cons x ys) - have "(LEAST x. x \ Set t \ P x) = x" - proof (rule Least_equality) - show "x \ Set t \ P x" - using Cons[symmetric] - by (auto simp add: set_keys Cons_eq_filter_iff) - next - fix y - assume "y \ Set t \ P y" - then show "x \ y" - using Cons[symmetric] - by(auto simp add: set_keys Cons_eq_filter_iff) - (metis sorted_wrt.simps(2) sorted_append sorted_keys) - qed - thus ?thesis using Cons by (simp add: Bleast_def) -qed +lemma Least_code [code]: + \List.Least (Set t) = (if RBT.is_empty t then List.Least_abort {} else Min (Set t))\ + apply (auto simp add: List.Least_abort_def simp flip: empty_Set) + apply (subst Least_Min) + using is_empty_Set + apply auto + done + +lemma Greatest_code [code]: + \List.Greatest (Set t) = (if RBT.is_empty t then List.Greatest_abort {} else Max (Set t))\ + apply (auto simp add: List.Greatest_abort_def simp flip: empty_Set) + apply (subst Greatest_Max) + using is_empty_Set + apply auto + done hide_const (open) RBT_Set.Set RBT_Set.Coset diff -r 97b9ac57751e -r b391142bd2d2 src/HOL/List.thy --- a/src/HOL/List.thy Wed Jun 04 19:43:13 2025 +0000 +++ b/src/HOL/List.thy Thu Jun 05 15:18:27 2025 +0000 @@ -8099,40 +8099,6 @@ end -text \Bounded \LEAST\ operator.\ - -definition Bleast :: \'a::ord set \ ('a \ bool) \ 'a\ - where \Bleast S P = (LEAST x. x \ S \ P x)\ - -declare Bleast_def [symmetric, code_unfold] - -definition abort_Bleast :: \'a::ord set \ ('a \ bool) \ 'a\ - where \abort_Bleast S P = (LEAST x. x \ S \ P x)\ - -declare [[code abort: abort_Bleast]] - -lemma Bleast_code [code]: - \Bleast (set xs) P = (case filter P (sort xs) of - x # xs \ x | - [] \ abort_Bleast (set xs) P)\ -proof (cases "filter P (sort xs)") - case Nil thus ?thesis by (simp add: Bleast_def abort_Bleast_def) -next - case (Cons x ys) - have "(LEAST x. x \ set xs \ P x) = x" - proof (rule Least_equality) - show "x \ set xs \ P x" - by (metis Cons Cons_eq_filter_iff in_set_conv_decomp set_sort) - next - fix y assume "y \ set xs \ P y" - hence "y \ set (filter P xs)" by auto - thus "x \ y" - by (metis Cons eq_iff filter_sort set_ConsD set_sort sorted_wrt.simps(2) sorted_sort) - qed - thus ?thesis using Cons by (simp add: Bleast_def) -qed - - subsubsection \Special implementations\ text \ @@ -8345,6 +8311,45 @@ lemma wf_code_set[code]: "wf_code (set xs) = acyclic (set xs)" unfolding wf_code_def using wf_set . +text \\LEAST\ and \GREATEST\ operator.\ + +context +begin + +qualified definition Least :: \'a::linorder set \ 'a\ \ \only for code generation\ + where Least_eq [code_abbrev, simp]: \Least S = (LEAST x. x \ S)\ + +qualified lemma Min_filter_eq [code_abbrev]: + \Least (Set.filter P S) = (LEAST x. x \ S \ P x)\ + by simp + +qualified definition Least_abort :: \'a set \ 'a::linorder\ + where \Least_abort = Least\ + +declare [[code abort: Least_abort]] + +qualified lemma Least_code [code]: + \Least A = (if finite A \ Set.is_empty A then Least_abort A else Min A)\ + using Least_Min [of \\x. x \ A\] by (auto simp add: Least_abort_def) + +qualified definition Greatest :: \'a::linorder set \ 'a\ \ \only for code generation\ + where Greatest_eq [code_abbrev, simp]: \Greatest S = (GREATEST x. x \ S)\ + +qualified lemma Greatest_filter_eq [code_abbrev]: + \Greatest (Set.filter P S) = (GREATEST x. x \ S \ P x)\ + by simp + +qualified definition Greatest_abort :: \'a set \ 'a::linorder\ + where \Greatest_abort = Greatest\ + +declare [[code abort: Greatest_abort]] + +qualified lemma Greatest_code [code]: + \Greatest A = (if finite A \ Set.is_empty A then Greatest_abort A else Max A)\ + using Greatest_Max [of \\x. x \ A\] by (auto simp add: Greatest_abort_def) + +end + subsubsection \Pretty lists\