--- a/src/HOL/Library/RBT_Set.thy Fri Sep 27 14:43:26 2013 +0200
+++ b/src/HOL/Library/RBT_Set.thy Fri Sep 27 17:20:02 2013 +0200
@@ -135,6 +135,9 @@
lemma [code, code del]:
"List.map_project = List.map_project" ..
+lemma [code, code del]:
+ "List.Bleast = List.Bleast" ..
+
section {* Lemmas *}
@@ -805,6 +808,28 @@
"sorted_list_of_set (Set t) = keys t"
by (auto simp add: set_keys intro: sorted_distinct_set_unique)
+lemma Bleast_code [code]:
+ "Bleast (Set t) P = (case filter P (keys t) of
+ x#xs \<Rightarrow> x |
+ [] \<Rightarrow> abort_Bleast (Set t) P)"
+proof (cases "filter P (keys t)")
+ case Nil thus ?thesis by (simp add: Bleast_def abort_Bleast_def)
+next
+ case (Cons x ys)
+ have "(LEAST x. x \<in> Set t \<and> P x) = x"
+ proof (rule Least_equality)
+ show "x \<in> Set t \<and> P x" using Cons[symmetric]
+ by(auto simp add: set_keys Cons_eq_filter_iff)
+ next
+ fix y assume "y : Set t \<and> P y"
+ then show "x \<le> y" using Cons[symmetric]
+ by(auto simp add: set_keys Cons_eq_filter_iff)
+ (metis sorted_Cons sorted_append sorted_keys)
+ qed
+ thus ?thesis using Cons by (simp add: Bleast_def)
+qed
+
+
hide_const (open) RBT_Set.Set RBT_Set.Coset
end
--- a/src/HOL/List.thy Fri Sep 27 14:43:26 2013 +0200
+++ b/src/HOL/List.thy Fri Sep 27 17:20:02 2013 +0200
@@ -5961,6 +5961,37 @@
"setsum f (set [m..<n]) = listsum (map f [m..<n])"
by (simp add: interv_listsum_conv_setsum_set_nat)
+text{* Bounded @{text LEAST} operator: *}
+
+definition "Bleast S P = (LEAST x. x \<in> S \<and> P x)"
+
+definition "abort_Bleast S P = (LEAST x. x \<in> S \<and> P x)"
+
+code_abort abort_Bleast
+
+lemma Bleast_code [code]:
+ "Bleast (set xs) P = (case filter P (sort xs) of
+ x#xs \<Rightarrow> x |
+ [] \<Rightarrow> 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 \<in> set xs \<and> P x) = x"
+ proof (rule Least_equality)
+ show "x \<in> set xs \<and> P x"
+ by (metis Cons Cons_eq_filter_iff in_set_conv_decomp set_sort)
+ next
+ fix y assume "y : set xs \<and> P y"
+ hence "y : set (filter P xs)" by auto
+ thus "x \<le> y"
+ by (metis Cons eq_iff filter_sort set_ConsD set_sort sorted_Cons sorted_sort)
+ qed
+ thus ?thesis using Cons by (simp add: Bleast_def)
+qed
+
+declare Bleast_def[symmetric, code_unfold]
+
text {* Summation over ints. *}
lemma greaterThanLessThan_upto [code_unfold]: