src/HOL/List.thy
 changeset 53954 ccfd22f937be parent 53940 36cf426cb1c6 child 54147 97a8ff4e4ac9
```     1.1 --- a/src/HOL/List.thy	Fri Sep 27 10:40:02 2013 +0200
1.2 +++ b/src/HOL/List.thy	Fri Sep 27 15:38:23 2013 +0200
1.3 @@ -5961,6 +5961,37 @@
1.4    "setsum f (set [m..<n]) = listsum (map f [m..<n])"
1.6
1.7 +text{* Bounded @{text LEAST} operator: *}
1.8 +
1.9 +definition "Bleast S P = (LEAST x. x \<in> S \<and> P x)"
1.10 +
1.11 +definition "abort_Bleast S P = (LEAST x. x \<in> S \<and> P x)"
1.12 +
1.13 +code_abort abort_Bleast
1.14 +
1.15 +lemma Bleast_code [code]:
1.16 + "Bleast (set xs) P = (case filter P (sort xs) of
1.17 +    x#xs \<Rightarrow> x |
1.18 +    [] \<Rightarrow> abort_Bleast (set xs) P)"
1.19 +proof (cases "filter P (sort xs)")
1.20 +  case Nil thus ?thesis by (simp add: Bleast_def abort_Bleast_def)
1.21 +next
1.22 +  case (Cons x ys)
1.23 +  have "(LEAST x. x \<in> set xs \<and> P x) = x"
1.24 +  proof (rule Least_equality)
1.25 +    show "x \<in> set xs \<and> P x"
1.26 +      by (metis Cons Cons_eq_filter_iff in_set_conv_decomp set_sort)
1.27 +    next
1.28 +      fix y assume "y : set xs \<and> P y"
1.29 +      hence "y : set (filter P xs)" by auto
1.30 +      thus "x \<le> y"
1.31 +        by (metis Cons eq_iff filter_sort set_ConsD set_sort sorted_Cons sorted_sort)
1.32 +  qed
1.33 +  thus ?thesis using Cons by (simp add: Bleast_def)
1.34 +qed
1.35 +
1.36 +declare Bleast_def[symmetric, code_unfold]
1.37 +
1.38  text {* Summation over ints. *}
1.39
1.40  lemma greaterThanLessThan_upto [code_unfold]:
```