src/HOL/Library/RBT_Set.thy
changeset 51489 f738e6dbd844
parent 51115 7dbd6832a689
child 51540 eea5c4ca4a0e
equal deleted inserted replaced
51488:3c886fe611b8 51489:f738e6dbd844
   314 lemma rbt_min_eq_rbt_min_opt:
   314 lemma rbt_min_eq_rbt_min_opt:
   315   assumes "t \<noteq> RBT_Impl.Empty"
   315   assumes "t \<noteq> RBT_Impl.Empty"
   316   assumes "is_rbt t"
   316   assumes "is_rbt t"
   317   shows "rbt_min t = rbt_min_opt t"
   317   shows "rbt_min t = rbt_min_opt t"
   318 proof -
   318 proof -
   319   interpret ab_semigroup_idem_mult "(min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_min
   319   from assms have "hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t) = RBT_Impl.keys t" by (cases t) simp_all
   320     unfolding class.ab_semigroup_idem_mult_def by blast
   320   with assms show ?thesis
   321   show ?thesis
   321     by (simp add: rbt_min_def rbt_fold1_keys_def rbt_min_opt_is_min
   322     by (simp add: Min_eqI rbt_min_opt_is_min rbt_min_opt_in_set assms Min_def[symmetric]
   322       min_max.Inf_fin.set_eq_fold [symmetric] Min_eqI rbt_min_opt_in_set)
   323       non_empty_rbt_keys fold1_set_fold[symmetric] rbt_min_def rbt_fold1_keys_def)
       
   324 qed
   323 qed
   325 
   324 
   326 (* maximum *)
   325 (* maximum *)
   327 
   326 
   328 definition rbt_max :: "('a::linorder, unit) RBT_Impl.rbt \<Rightarrow> 'a" 
   327 definition rbt_max :: "('a::linorder, unit) RBT_Impl.rbt \<Rightarrow> 'a" 
   335 
   334 
   336 lemma fold_max_rev_eq:
   335 lemma fold_max_rev_eq:
   337   fixes xs :: "('a :: linorder) list"
   336   fixes xs :: "('a :: linorder) list"
   338   assumes "xs \<noteq> []"
   337   assumes "xs \<noteq> []"
   339   shows "List.fold max (tl xs) (hd xs) = List.fold max (tl (rev xs)) (hd (rev xs))" 
   338   shows "List.fold max (tl xs) (hd xs) = List.fold max (tl (rev xs)) (hd (rev xs))" 
   340 proof -
   339   using assms by (simp add: min_max.Sup_fin.set_eq_fold [symmetric])
   341   interpret ab_semigroup_idem_mult "(max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_max
       
   342     unfolding class.ab_semigroup_idem_mult_def by blast
       
   343   show ?thesis
       
   344   using assms by (auto simp add: fold1_set_fold[symmetric])
       
   345 qed
       
   346 
   340 
   347 lemma rbt_max_simps:
   341 lemma rbt_max_simps:
   348   assumes "is_rbt (Branch c lt k v RBT_Impl.Empty)" 
   342   assumes "is_rbt (Branch c lt k v RBT_Impl.Empty)" 
   349   shows "rbt_max (Branch c lt k v RBT_Impl.Empty) = k"
   343   shows "rbt_max (Branch c lt k v RBT_Impl.Empty) = k"
   350 proof -
   344 proof -
   414 lemma rbt_max_eq_rbt_max_opt:
   408 lemma rbt_max_eq_rbt_max_opt:
   415   assumes "t \<noteq> RBT_Impl.Empty"
   409   assumes "t \<noteq> RBT_Impl.Empty"
   416   assumes "is_rbt t"
   410   assumes "is_rbt t"
   417   shows "rbt_max t = rbt_max_opt t"
   411   shows "rbt_max t = rbt_max_opt t"
   418 proof -
   412 proof -
   419   interpret ab_semigroup_idem_mult "(max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_max
   413   from assms have "hd (RBT_Impl.keys t) # tl (RBT_Impl.keys t) = RBT_Impl.keys t" by (cases t) simp_all
   420     unfolding class.ab_semigroup_idem_mult_def by blast
   414   with assms show ?thesis
   421   show ?thesis
   415     by (simp add: rbt_max_def rbt_fold1_keys_def rbt_max_opt_is_max
   422     by (simp add: Max_eqI rbt_max_opt_is_max rbt_max_opt_in_set assms Max_def[symmetric]
   416       min_max.Sup_fin.set_eq_fold [symmetric] Max_eqI rbt_max_opt_in_set)
   423       non_empty_rbt_keys fold1_set_fold[symmetric] rbt_max_def rbt_fold1_keys_def)
       
   424 qed
   417 qed
   425 
   418 
   426 
   419 
   427 (** abstract **)
   420 (** abstract **)
   428 
   421 
   432 lemma fold1_keys_def_alt:
   425 lemma fold1_keys_def_alt:
   433   "fold1_keys f t = List.fold f (tl(keys t)) (hd(keys t))"
   426   "fold1_keys f t = List.fold f (tl(keys t)) (hd(keys t))"
   434   by transfer (simp add: rbt_fold1_keys_def)
   427   by transfer (simp add: rbt_fold1_keys_def)
   435 
   428 
   436 lemma finite_fold1_fold1_keys:
   429 lemma finite_fold1_fold1_keys:
   437   assumes "class.ab_semigroup_mult f"
   430   assumes "semilattice f"
   438   assumes "\<not> (is_empty t)"
   431   assumes "\<not> is_empty t"
   439   shows "Finite_Set.fold1 f (Set t) = fold1_keys f t"
   432   shows "semilattice_set.F f (Set t) = fold1_keys f t"
   440 proof -
   433 proof -
   441   interpret ab_semigroup_mult f by fact
   434   from `semilattice f` interpret semilattice_set f by (rule semilattice_set.intro)
   442   show ?thesis using assms 
   435   show ?thesis using assms 
   443     by (auto simp: fold1_keys_def_alt set_keys fold_def_alt fold1_distinct_set_fold non_empty_keys)
   436     by (auto simp: fold1_keys_def_alt set_keys fold_def_alt non_empty_keys set_eq_fold [symmetric])
   444 qed
   437 qed
   445 
   438 
   446 (* minimum *)
   439 (* minimum *)
   447 
   440 
   448 lift_definition r_min :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_min by simp
   441 lift_definition r_min :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_min by simp
   656   "Set t1 \<subseteq> Set t2 \<longleftrightarrow> foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> k \<in> Set t2) t1 True"
   649   "Set t1 \<subseteq> Set t2 \<longleftrightarrow> foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> k \<in> Set t2) t1 True"
   657 by (simp add: subset_code Ball_Set)
   650 by (simp add: subset_code Ball_Set)
   658 
   651 
   659 lemma card_Set [code]:
   652 lemma card_Set [code]:
   660   "card (Set t) = fold_keys (\<lambda>_ n. n + 1) t 0"
   653   "card (Set t) = fold_keys (\<lambda>_ n. n + 1) t 0"
   661 by (auto simp add: card_def fold_image_def intro!: finite_fold_fold_keys) (default, simp) 
   654   by (auto simp add: card.eq_fold intro: finite_fold_fold_keys comp_fun_commute_const)
   662 
   655 
   663 lemma setsum_Set [code]:
   656 lemma setsum_Set [code]:
   664   "setsum f (Set xs) = fold_keys (plus o f) xs 0"
   657   "setsum f (Set xs) = fold_keys (plus o f) xs 0"
   665 proof -
   658 proof -
   666   have "comp_fun_commute (\<lambda>x. op + (f x))" by default (auto simp: add_ac)
   659   have "comp_fun_commute (\<lambda>x. op + (f x))" by default (auto simp: add_ac)
   667   then show ?thesis 
   660   then show ?thesis 
   668     by (auto simp add: setsum_def fold_image_def finite_fold_fold_keys o_def)
   661     by (auto simp add: setsum.eq_fold finite_fold_fold_keys o_def)
   669 qed
   662 qed
   670 
   663 
   671 definition not_a_singleton_tree  where [code del]: "not_a_singleton_tree x y = x y"
   664 definition not_a_singleton_tree  where [code del]: "not_a_singleton_tree x y = x y"
   672 
   665 
   673 code_abort not_a_singleton_tree
   666 code_abort not_a_singleton_tree
   741 code_abort not_non_empty_tree
   734 code_abort not_non_empty_tree
   742 
   735 
   743 lemma Min_fin_set_fold [code]:
   736 lemma Min_fin_set_fold [code]:
   744   "Min (Set t) = (if is_empty t then not_non_empty_tree Min (Set t) else r_min_opt t)"
   737   "Min (Set t) = (if is_empty t then not_non_empty_tree Min (Set t) else r_min_opt t)"
   745 proof -
   738 proof -
   746   have *:"(class.ab_semigroup_mult (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a))" using ab_semigroup_idem_mult_min
   739   have *: "semilattice (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" ..
   747     unfolding class.ab_semigroup_idem_mult_def by blast
   740   with finite_fold1_fold1_keys [OF *, folded Min_def]
   748   show ?thesis
   741   show ?thesis
   749     by (auto simp add: Min_def not_non_empty_tree_def finite_fold1_fold1_keys[OF *] r_min_alt_def 
   742     by (simp add: not_non_empty_tree_def r_min_alt_def r_min_eq_r_min_opt [symmetric])  
   750       r_min_eq_r_min_opt[symmetric])  
       
   751 qed
   743 qed
   752 
   744 
   753 lemma Inf_fin_set_fold [code]:
   745 lemma Inf_fin_set_fold [code]:
   754   "Inf_fin (Set t) = Min (Set t)"
   746   "Inf_fin (Set t) = Min (Set t)"
   755 by (simp add: inf_min Inf_fin_def Min_def)
   747 by (simp add: inf_min Inf_fin_def Min_def)
   779 qed
   771 qed
   780 
   772 
   781 lemma Max_fin_set_fold [code]:
   773 lemma Max_fin_set_fold [code]:
   782   "Max (Set t) = (if is_empty t then not_non_empty_tree Max (Set t) else r_max_opt t)"
   774   "Max (Set t) = (if is_empty t then not_non_empty_tree Max (Set t) else r_max_opt t)"
   783 proof -
   775 proof -
   784   have *:"(class.ab_semigroup_mult (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a))" using ab_semigroup_idem_mult_max
   776   have *: "semilattice (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" ..
   785     unfolding class.ab_semigroup_idem_mult_def by blast
   777   with finite_fold1_fold1_keys [OF *, folded Max_def]
   786   show ?thesis
   778   show ?thesis
   787     by (auto simp add: Max_def not_non_empty_tree_def finite_fold1_fold1_keys[OF *] r_max_alt_def 
   779     by (simp add: not_non_empty_tree_def r_max_alt_def r_max_eq_r_max_opt [symmetric])  
   788       r_max_eq_r_max_opt[symmetric])  
       
   789 qed
   780 qed
   790 
   781 
   791 lemma Sup_fin_set_fold [code]:
   782 lemma Sup_fin_set_fold [code]:
   792   "Sup_fin (Set t) = Max (Set t)"
   783   "Sup_fin (Set t) = Max (Set t)"
   793 by (simp add: sup_max Sup_fin_def Max_def)
   784 by (simp add: sup_max Sup_fin_def Max_def)