src/HOL/Library/RBT_Set.thy
changeset 82773 4ec8e654112f
parent 82688 b391142bd2d2
child 82774 2865a6618cba
equal deleted inserted replaced
82772:59b937edcff8 82773:4ec8e654112f
    21 definition Set :: "('a::linorder, unit) rbt \<Rightarrow> 'a set" 
    21 definition Set :: "('a::linorder, unit) rbt \<Rightarrow> 'a set" 
    22   where "Set t = {x . RBT.lookup t x = Some ()}"
    22   where "Set t = {x . RBT.lookup t x = Some ()}"
    23 
    23 
    24 definition Coset :: "('a::linorder, unit) rbt \<Rightarrow> 'a set" 
    24 definition Coset :: "('a::linorder, unit) rbt \<Rightarrow> 'a set" 
    25   where [simp]: "Coset t = - Set t"
    25   where [simp]: "Coset t = - Set t"
    26 
       
    27 
       
    28 section \<open>Deletion of already existing code equations\<close>
       
    29 
       
    30 declare [[code drop: Set.empty Set.is_empty uminus_set_inst.uminus_set
       
    31   Set.member Set.insert Set.remove UNIV Set.filter image
       
    32   Set.subset_eq Ball Bex Set.can_select Set.union minus_set_inst.minus_set Set.inter
       
    33   card the_elem Pow sum prod Product_Type.product Id_on
       
    34   Image trancl relcomp wf_on wf_code Min Inf_fin Max Sup_fin
       
    35   "(Inf :: 'a set set \<Rightarrow> 'a set)" "(Sup :: 'a set set \<Rightarrow> 'a set)"
       
    36   sorted_list_of_set List.map_project List.Least List.Greatest]]
       
    37 
    26 
    38 
    27 
    39 section \<open>Lemmas\<close>
    28 section \<open>Lemmas\<close>
    40 
    29 
    41 subsection \<open>Auxiliary lemmas\<close>
    30 subsection \<open>Auxiliary lemmas\<close>
   580     by standard (auto simp: ac_simps)
   569     by standard (auto simp: ac_simps)
   581   then show ?thesis 
   570   then show ?thesis 
   582     by (auto simp add: sum.eq_fold finite_fold_fold_keys o_def)
   571     by (auto simp add: sum.eq_fold finite_fold_fold_keys o_def)
   583 qed
   572 qed
   584 
   573 
       
   574 lemma prod_Set [code]:
       
   575   "prod f (Set xs) = fold_keys (times \<circ> f) xs 1"
       
   576 proof -
       
   577   have "comp_fun_commute (\<lambda>x. (*) (f x))"
       
   578     by standard (auto simp: ac_simps)
       
   579   then show ?thesis 
       
   580     by (auto simp add: prod.eq_fold finite_fold_fold_keys o_def)
       
   581 qed
       
   582 
   585 lemma the_elem_set [code]:
   583 lemma the_elem_set [code]:
   586   fixes t :: "('a :: linorder, unit) rbt"
   584   fixes t :: "('a :: linorder, unit) rbt"
   587   shows "the_elem (Set t) = (case RBT.impl_of t of 
   585   shows "the_elem (Set t) = (case RBT.impl_of t of 
   588     (Branch RBT_Impl.B RBT_Impl.Empty x () RBT_Impl.Empty) \<Rightarrow> x
   586     (Branch RBT_Impl.B RBT_Impl.Empty x () RBT_Impl.Empty) \<Rightarrow> x
   589     | _ \<Rightarrow> Code.abort (STR ''not_a_singleton_tree'') (\<lambda>_. the_elem (Set t)))"
   587     | _ \<Rightarrow> Code.abort (STR ''not_a_singleton_tree'') (\<lambda>_. the_elem (Set t)))"
   664 
   662 
   665 lemma Inf_fin_set_fold [code]:
   663 lemma Inf_fin_set_fold [code]:
   666   "Inf_fin (Set t) = Min (Set t)"
   664   "Inf_fin (Set t) = Min (Set t)"
   667 by (simp add: inf_min Inf_fin_def Min_def)
   665 by (simp add: inf_min Inf_fin_def Min_def)
   668 
   666 
       
   667 declare [[code drop: "Inf :: _ \<Rightarrow> 'a set"]]
       
   668 
   669 lemma Inf_Set_fold:
   669 lemma Inf_Set_fold:
   670   fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
   670   fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
   671   shows "Inf (Set t) = (if RBT.is_empty t then top else r_min_opt t)"
   671   shows "Inf (Set t) = (if RBT.is_empty t then top else r_min_opt t)"
   672 proof -
   672 proof -
   673   have "comp_fun_commute (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)"
   673   have "comp_fun_commute (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)"
   693 
   693 
   694 lemma Sup_fin_set_fold [code]:
   694 lemma Sup_fin_set_fold [code]:
   695   "Sup_fin (Set t) = Max (Set t)"
   695   "Sup_fin (Set t) = Max (Set t)"
   696 by (simp add: sup_max Sup_fin_def Max_def)
   696 by (simp add: sup_max Sup_fin_def Max_def)
   697 
   697 
       
   698 declare [[code drop: "Sup :: _ \<Rightarrow> 'a set"]]
       
   699 
   698 lemma Sup_Set_fold:
   700 lemma Sup_Set_fold:
   699   fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
   701   fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
   700   shows "Sup (Set t) = (if RBT.is_empty t then bot else r_max_opt t)"
   702   shows "Sup (Set t) = (if RBT.is_empty t then bot else r_max_opt t)"
   701 proof -
   703 proof -
   702   have "comp_fun_commute (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)"
   704   have "comp_fun_commute (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)"
   709 qed
   711 qed
   710 
   712 
   711 context
   713 context
   712 begin
   714 begin
   713 
   715 
   714 declare [[code drop: Gcd_fin Lcm_fin \<open>Gcd :: _ \<Rightarrow> nat\<close> \<open>Gcd :: _ \<Rightarrow> int\<close> \<open>Lcm :: _ \<Rightarrow> nat\<close> \<open>Lcm :: _ \<Rightarrow> int\<close>]]
   716 qualified definition Inf' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a"
       
   717   where [code_abbrev]: "Inf' = Inf"
       
   718 
       
   719 lemma Inf'_Set_fold [code]:
       
   720   "Inf' (Set t) = (if RBT.is_empty t then top else r_min_opt t)"
       
   721   by (simp add: Inf'_def Inf_Set_fold)
       
   722 
       
   723 qualified definition Sup' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a"
       
   724   where [code_abbrev]: "Sup' = Sup"
       
   725 
       
   726 lemma Sup'_Set_fold [code]:
       
   727   "Sup' (Set t) = (if RBT.is_empty t then bot else r_max_opt t)"
       
   728   by (simp add: Sup'_def Sup_Set_fold)
       
   729 
       
   730 end
   715 
   731 
   716 lemma [code]:
   732 lemma [code]:
   717   "Gcd\<^sub>f\<^sub>i\<^sub>n (Set t) = fold_keys gcd t (0::'a::{semiring_gcd, linorder})"
   733   "Gcd\<^sub>f\<^sub>i\<^sub>n (Set t) = fold_keys gcd t (0::'a::{semiring_gcd, linorder})"
   718 proof -
   734 proof -
   719   have "comp_fun_commute (gcd :: 'a \<Rightarrow> _)"
   735   have "comp_fun_commute (gcd :: 'a \<Rightarrow> _)"
   722   have "Finite_Set.fold gcd 0 (Set t) = fold_keys gcd t 0"
   738   have "Finite_Set.fold gcd 0 (Set t) = fold_keys gcd t 0"
   723     by blast
   739     by blast
   724   then show ?thesis
   740   then show ?thesis
   725     by (simp add: Gcd_fin.eq_fold)
   741     by (simp add: Gcd_fin.eq_fold)
   726 qed
   742 qed
   727     
   743 
   728 lemma [code]:
   744 lemma [code]:
   729   "Gcd (Set t) = (Gcd\<^sub>f\<^sub>i\<^sub>n (Set t) :: nat)"
   745   "Gcd (Set t) = (Gcd\<^sub>f\<^sub>i\<^sub>n (Set t) :: nat)"
   730   by simp
   746   by simp
   731 
   747 
   732 lemma [code]:
   748 lemma [code]:
   743     by blast
   759     by blast
   744   then show ?thesis
   760   then show ?thesis
   745     by (simp add: Lcm_fin.eq_fold)
   761     by (simp add: Lcm_fin.eq_fold)
   746 qed
   762 qed
   747 
   763 
   748 lemma [code drop: "Lcm :: _ \<Rightarrow> nat", code]:
   764 lemma [code]:
   749   "Lcm (Set t) = (Lcm\<^sub>f\<^sub>i\<^sub>n (Set t) :: nat)"
   765   "Lcm (Set t) = (Lcm\<^sub>f\<^sub>i\<^sub>n (Set t) :: nat)"
   750   by simp
   766   by simp
   751 
   767 
   752 lemma [code drop: "Lcm :: _ \<Rightarrow> int", code]:
   768 lemma [code]:
   753   "Lcm (Set t) = (Lcm\<^sub>f\<^sub>i\<^sub>n (Set t) :: int)"
   769   "Lcm (Set t) = (Lcm\<^sub>f\<^sub>i\<^sub>n (Set t) :: int)"
   754   by simp
   770   by simp
   755 
       
   756 qualified definition Inf' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a"
       
   757   where [code_abbrev]: "Inf' = Inf"
       
   758 
       
   759 lemma Inf'_Set_fold [code]:
       
   760   "Inf' (Set t) = (if RBT.is_empty t then top else r_min_opt t)"
       
   761   by (simp add: Inf'_def Inf_Set_fold)
       
   762 
       
   763 qualified definition Sup' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a"
       
   764   where [code_abbrev]: "Sup' = Sup"
       
   765 
       
   766 lemma Sup'_Set_fold [code]:
       
   767   "Sup' (Set t) = (if RBT.is_empty t then bot else r_max_opt t)"
       
   768   by (simp add: Sup'_def Sup_Set_fold)
       
   769 
       
   770 end
       
   771 
   771 
   772 lemma sorted_list_set [code]: "sorted_list_of_set (Set t) = RBT.keys t"
   772 lemma sorted_list_set [code]: "sorted_list_of_set (Set t) = RBT.keys t"
   773   by (auto simp add: set_keys intro: sorted_distinct_set_unique) 
   773   by (auto simp add: set_keys intro: sorted_distinct_set_unique) 
   774 
   774 
   775 lemma Least_code [code]:
   775 lemma Least_code [code]:
   786   apply (subst Greatest_Max)
   786   apply (subst Greatest_Max)
   787   using is_empty_Set
   787   using is_empty_Set
   788     apply auto
   788     apply auto
   789   done
   789   done
   790 
   790 
       
   791 declare [[code drop: Set.can_select List.map_project Wellfounded.acc pred_of_set
       
   792   \<open>Inf :: _ \<Rightarrow> 'a Predicate.pred\<close> \<open>Sup :: _ \<Rightarrow> 'a Predicate.pred\<close>]]
       
   793 
   791 hide_const (open) RBT_Set.Set RBT_Set.Coset
   794 hide_const (open) RBT_Set.Set RBT_Set.Coset
   792 
   795 
   793 end
   796 end