src/HOL/Library/RBT_Set.thy
changeset 48623 bea613f2543d
child 49757 73ab6d4a9236
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/RBT_Set.thy	Tue Jul 31 13:55:39 2012 +0200
     1.3 @@ -0,0 +1,824 @@
     1.4 +(*  Title:      HOL/Library/RBT_Set.thy
     1.5 +    Author:     Ondrej Kuncar
     1.6 +*)
     1.7 +
     1.8 +header {* Implementation of sets using RBT trees *}
     1.9 +
    1.10 +theory RBT_Set
    1.11 +imports RBT Product_ord
    1.12 +begin
    1.13 +
    1.14 +(*
    1.15 +  Users should be aware that by including this file all code equations
    1.16 +  outside of List.thy using 'a list as an implenentation of sets cannot be
    1.17 +  used for code generation. If such equations are not needed, they can be
    1.18 +  deleted from the code generator. Otherwise, a user has to provide their 
    1.19 +  own equations using RBT trees. 
    1.20 +*)
    1.21 +
    1.22 +section {* Definition of code datatype constructors *}
    1.23 +
    1.24 +definition Set :: "('a\<Colon>linorder, unit) rbt \<Rightarrow> 'a set" 
    1.25 +  where "Set t = {x . lookup t x = Some ()}"
    1.26 +
    1.27 +definition Coset :: "('a\<Colon>linorder, unit) rbt \<Rightarrow> 'a set" 
    1.28 +  where [simp]: "Coset t = - Set t"
    1.29 +
    1.30 +
    1.31 +section {* Deletion of already existing code equations *}
    1.32 +
    1.33 +lemma [code, code del]:
    1.34 +  "Set.empty = Set.empty" ..
    1.35 +
    1.36 +lemma [code, code del]:
    1.37 +  "Set.is_empty = Set.is_empty" ..
    1.38 +
    1.39 +lemma [code, code del]:
    1.40 +  "uminus_set_inst.uminus_set = uminus_set_inst.uminus_set" ..
    1.41 +
    1.42 +lemma [code, code del]:
    1.43 +  "Set.member = Set.member" ..
    1.44 +
    1.45 +lemma [code, code del]:
    1.46 +  "Set.insert = Set.insert" ..
    1.47 +
    1.48 +lemma [code, code del]:
    1.49 +  "Set.remove = Set.remove" ..
    1.50 +
    1.51 +lemma [code, code del]:
    1.52 +  "UNIV = UNIV" ..
    1.53 +
    1.54 +lemma [code, code del]:
    1.55 +  "Set.project = Set.project" ..
    1.56 +
    1.57 +lemma [code, code del]:
    1.58 +  "image = image" ..
    1.59 +
    1.60 +lemma [code, code del]:
    1.61 +  "Set.subset_eq = Set.subset_eq" ..
    1.62 +
    1.63 +lemma [code, code del]:
    1.64 +  "Ball = Ball" ..
    1.65 +
    1.66 +lemma [code, code del]:
    1.67 +  "Bex = Bex" ..
    1.68 +
    1.69 +lemma [code, code del]:
    1.70 +  "Set.union = Set.union" ..
    1.71 +
    1.72 +lemma [code, code del]:
    1.73 +  "minus_set_inst.minus_set = minus_set_inst.minus_set" ..
    1.74 +
    1.75 +lemma [code, code del]:
    1.76 +  "Set.inter = Set.inter" ..
    1.77 +
    1.78 +lemma [code, code del]:
    1.79 +  "card = card" ..
    1.80 +
    1.81 +lemma [code, code del]:
    1.82 +  "the_elem = the_elem" ..
    1.83 +
    1.84 +lemma [code, code del]:
    1.85 +  "Pow = Pow" ..
    1.86 +
    1.87 +lemma [code, code del]:
    1.88 +  "setsum = setsum" ..
    1.89 +
    1.90 +lemma [code, code del]:
    1.91 +  "Product_Type.product = Product_Type.product"  ..
    1.92 +
    1.93 +lemma [code, code del]:
    1.94 +  "Id_on = Id_on" ..
    1.95 +
    1.96 +lemma [code, code del]:
    1.97 +  "Image = Image" ..
    1.98 +
    1.99 +lemma [code, code del]:
   1.100 +  "trancl = trancl" ..
   1.101 +
   1.102 +lemma [code, code del]:
   1.103 +  "relcomp = relcomp" ..
   1.104 +
   1.105 +lemma [code, code del]:
   1.106 +  "wf = wf" ..
   1.107 +
   1.108 +lemma [code, code del]:
   1.109 +  "Min = Min" ..
   1.110 +
   1.111 +lemma [code, code del]:
   1.112 +  "Inf_fin = Inf_fin" ..
   1.113 +
   1.114 +lemma [code, code del]:
   1.115 +  "INFI = INFI" ..
   1.116 +
   1.117 +lemma [code, code del]:
   1.118 +  "Max = Max" ..
   1.119 +
   1.120 +lemma [code, code del]:
   1.121 +  "Sup_fin = Sup_fin" ..
   1.122 +
   1.123 +lemma [code, code del]:
   1.124 +  "SUPR = SUPR" ..
   1.125 +
   1.126 +lemma [code, code del]:
   1.127 +  "(Inf :: 'a set set \<Rightarrow> 'a set) = Inf" ..
   1.128 +
   1.129 +lemma [code, code del]:
   1.130 +  "(Sup :: 'a set set \<Rightarrow> 'a set) = Sup" ..
   1.131 +
   1.132 +lemma [code, code del]:
   1.133 +  "sorted_list_of_set = sorted_list_of_set" ..
   1.134 +
   1.135 +lemma [code, code del]: 
   1.136 +  "List.map_project = List.map_project" ..
   1.137 +
   1.138 +section {* Lemmas *}
   1.139 +
   1.140 +
   1.141 +subsection {* Auxiliary lemmas *}
   1.142 +
   1.143 +lemma [simp]: "x \<noteq> Some () \<longleftrightarrow> x = None"
   1.144 +by (auto simp: not_Some_eq[THEN iffD1])
   1.145 +
   1.146 +lemma finite_Set [simp, intro!]: "finite (Set x)"
   1.147 +proof -
   1.148 +  have "Set x = dom (lookup x)" by (auto simp: Set_def)
   1.149 +  then show ?thesis by simp
   1.150 +qed
   1.151 +
   1.152 +lemma set_keys: "Set t = set(keys t)"
   1.153 +proof -
   1.154 + have "\<And>k. ((k, ()) \<in> set (entries t)) = (k \<in> set (keys t))" 
   1.155 +    by (simp add: keys_entries)
   1.156 +  then show ?thesis by (auto simp: lookup_in_tree Set_def)
   1.157 +qed
   1.158 +
   1.159 +subsection {* fold and filter *}
   1.160 +
   1.161 +lemma finite_fold_rbt_fold_eq:
   1.162 +  assumes "comp_fun_commute f" 
   1.163 +  shows "Finite_Set.fold f A (set(entries t)) = fold (curry f) t A"
   1.164 +proof -
   1.165 +  have *: "remdups (entries t) = entries t"
   1.166 +    using distinct_entries distinct_map by (auto intro: distinct_remdups_id)
   1.167 +  show ?thesis using assms by (auto simp: fold_def_alt comp_fun_commute.fold_set_fold_remdups *)
   1.168 +qed
   1.169 +
   1.170 +definition fold_keys :: "('a :: linorder \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, _) rbt \<Rightarrow> 'b \<Rightarrow> 'b" 
   1.171 +  where [code_unfold]:"fold_keys f t A = fold (\<lambda>k _ t. f k t) t A"
   1.172 +
   1.173 +lemma fold_keys_def_alt:
   1.174 +  "fold_keys f t s = List.fold f (keys t) s"
   1.175 +by (auto simp: fold_map o_def split_def fold_def_alt keys_def_alt fold_keys_def)
   1.176 +
   1.177 +lemma finite_fold_fold_keys:
   1.178 +  assumes "comp_fun_commute f"
   1.179 +  shows "Finite_Set.fold f A (Set t) = fold_keys f t A"
   1.180 +using assms
   1.181 +proof -
   1.182 +  interpret comp_fun_commute f by fact
   1.183 +  have "set (keys t) = fst ` (set (entries t))" by (auto simp: fst_eq_Domain keys_entries)
   1.184 +  moreover have "inj_on fst (set (entries t))" using distinct_entries distinct_map by auto
   1.185 +  ultimately show ?thesis 
   1.186 +    by (auto simp add: set_keys fold_keys_def curry_def fold_image finite_fold_rbt_fold_eq 
   1.187 +      comp_comp_fun_commute)
   1.188 +qed
   1.189 +
   1.190 +definition rbt_filter :: "('a :: linorder \<Rightarrow> bool) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'a set" where
   1.191 +  "rbt_filter P t = fold (\<lambda>k _ A'. if P k then Set.insert k A' else A') t {}"
   1.192 +
   1.193 +lemma finite_filter_rbt_filter:
   1.194 +  "Finite_Set.filter P (Set t) = rbt_filter P t"
   1.195 +by (simp add: fold_keys_def Finite_Set.filter_def rbt_filter_def 
   1.196 +  finite_fold_fold_keys[OF comp_fun_commute_filter_fold])
   1.197 +
   1.198 +
   1.199 +subsection {* foldi and Ball *}
   1.200 +
   1.201 +lemma Ball_False: "RBT_Impl.fold (\<lambda>k v s. s \<and> P k) t False = False"
   1.202 +by (induction t) auto
   1.203 +
   1.204 +lemma rbt_foldi_fold_conj: 
   1.205 +  "RBT_Impl.foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> P k) t val = RBT_Impl.fold (\<lambda>k v s. s \<and> P k) t val"
   1.206 +proof (induction t arbitrary: val) 
   1.207 +  case (Branch c t1) then show ?case
   1.208 +    by (cases "RBT_Impl.fold (\<lambda>k v s. s \<and> P k) t1 True") (simp_all add: Ball_False) 
   1.209 +qed simp
   1.210 +
   1.211 +lemma foldi_fold_conj: "foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> P k) t val = fold_keys (\<lambda>k s. s \<and> P k) t val"
   1.212 +unfolding fold_keys_def by transfer (rule rbt_foldi_fold_conj)
   1.213 +
   1.214 +
   1.215 +subsection {* foldi and Bex *}
   1.216 +
   1.217 +lemma Bex_True: "RBT_Impl.fold (\<lambda>k v s. s \<or> P k) t True = True"
   1.218 +by (induction t) auto
   1.219 +
   1.220 +lemma rbt_foldi_fold_disj: 
   1.221 +  "RBT_Impl.foldi (\<lambda>s. s = False) (\<lambda>k v s. s \<or> P k) t val = RBT_Impl.fold (\<lambda>k v s. s \<or> P k) t val"
   1.222 +proof (induction t arbitrary: val) 
   1.223 +  case (Branch c t1) then show ?case
   1.224 +    by (cases "RBT_Impl.fold (\<lambda>k v s. s \<or> P k) t1 False") (simp_all add: Bex_True) 
   1.225 +qed simp
   1.226 +
   1.227 +lemma foldi_fold_disj: "foldi (\<lambda>s. s = False) (\<lambda>k v s. s \<or> P k) t val = fold_keys (\<lambda>k s. s \<or> P k) t val"
   1.228 +unfolding fold_keys_def by transfer (rule rbt_foldi_fold_disj)
   1.229 +
   1.230 +
   1.231 +subsection {* folding over non empty trees and selecting the minimal and maximal element *}
   1.232 +
   1.233 +(** concrete **)
   1.234 +
   1.235 +(* The concrete part is here because it's probably not general enough to be moved to RBT_Impl *)
   1.236 +
   1.237 +definition rbt_fold1_keys :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a::linorder, 'b) RBT_Impl.rbt \<Rightarrow> 'a" 
   1.238 +  where "rbt_fold1_keys f t = List.fold f (tl(RBT_Impl.keys t)) (hd(RBT_Impl.keys t))"
   1.239 +
   1.240 +(* minimum *)
   1.241 +
   1.242 +definition rbt_min :: "('a::linorder, unit) RBT_Impl.rbt \<Rightarrow> 'a" 
   1.243 +  where "rbt_min t = rbt_fold1_keys min t"
   1.244 +
   1.245 +lemma key_le_right: "rbt_sorted (Branch c lt k v rt) \<Longrightarrow> (\<And>x. x \<in>set (RBT_Impl.keys rt) \<Longrightarrow> k \<le> x)"
   1.246 +by  (auto simp: rbt_greater_prop less_imp_le)
   1.247 +
   1.248 +lemma left_le_key: "rbt_sorted (Branch c lt k v rt) \<Longrightarrow> (\<And>x. x \<in>set (RBT_Impl.keys lt) \<Longrightarrow> x \<le> k)"
   1.249 +by (auto simp: rbt_less_prop less_imp_le)
   1.250 +
   1.251 +lemma fold_min_triv:
   1.252 +  fixes k :: "_ :: linorder"
   1.253 +  shows "(\<forall>x\<in>set xs. k \<le> x) \<Longrightarrow> List.fold min xs k = k" 
   1.254 +by (induct xs) (auto simp add: min_def)
   1.255 +
   1.256 +lemma rbt_min_simps:
   1.257 +  "is_rbt (Branch c RBT_Impl.Empty k v rt) \<Longrightarrow> rbt_min (Branch c RBT_Impl.Empty k v rt) = k"
   1.258 +by (auto intro: fold_min_triv dest: key_le_right is_rbt_rbt_sorted simp: rbt_fold1_keys_def rbt_min_def)
   1.259 +
   1.260 +fun rbt_min_opt where
   1.261 +  "rbt_min_opt (Branch c RBT_Impl.Empty k v rt) = k" |
   1.262 +  "rbt_min_opt (Branch c (Branch lc llc lk lv lrt) k v rt) = rbt_min_opt (Branch lc llc lk lv lrt)"
   1.263 +
   1.264 +lemma rbt_min_opt_Branch:
   1.265 +  "t1 \<noteq> rbt.Empty \<Longrightarrow> rbt_min_opt (Branch c t1 k () t2) = rbt_min_opt t1" 
   1.266 +by (cases t1) auto
   1.267 +
   1.268 +lemma rbt_min_opt_induct [case_names empty left_empty left_non_empty]:
   1.269 +  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
   1.270 +  assumes "P rbt.Empty"
   1.271 +  assumes "\<And>color t1 a b t2. P t1 \<Longrightarrow> P t2 \<Longrightarrow> t1 = rbt.Empty \<Longrightarrow> P (Branch color t1 a b t2)"
   1.272 +  assumes "\<And>color t1 a b t2. P t1 \<Longrightarrow> P t2 \<Longrightarrow> t1 \<noteq> rbt.Empty \<Longrightarrow> P (Branch color t1 a b t2)"
   1.273 +  shows "P t"
   1.274 +using assms
   1.275 +  apply (induction t)
   1.276 +  apply simp
   1.277 +  apply (case_tac "t1 = rbt.Empty")
   1.278 +  apply simp_all
   1.279 +done
   1.280 +
   1.281 +lemma rbt_min_opt_in_set: 
   1.282 +  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
   1.283 +  assumes "t \<noteq> rbt.Empty"
   1.284 +  shows "rbt_min_opt t \<in> set (RBT_Impl.keys t)"
   1.285 +using assms by (induction t rule: rbt_min_opt.induct) (auto)
   1.286 +
   1.287 +lemma rbt_min_opt_is_min:
   1.288 +  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
   1.289 +  assumes "rbt_sorted t"
   1.290 +  assumes "t \<noteq> rbt.Empty"
   1.291 +  shows "\<And>y. y \<in> set (RBT_Impl.keys t) \<Longrightarrow> y \<ge> rbt_min_opt t"
   1.292 +using assms 
   1.293 +proof (induction t rule: rbt_min_opt_induct)
   1.294 +  case empty
   1.295 +    then show ?case by simp
   1.296 +next
   1.297 +  case left_empty
   1.298 +    then show ?case by (auto intro: key_le_right simp del: rbt_sorted.simps)
   1.299 +next
   1.300 +  case (left_non_empty c t1 k v t2 y)
   1.301 +    then have "y = k \<or> y \<in> set (RBT_Impl.keys t1) \<or> y \<in> set (RBT_Impl.keys t2)" by auto
   1.302 +    with left_non_empty show ?case 
   1.303 +    proof(elim disjE)
   1.304 +      case goal1 then show ?case 
   1.305 +        by (auto simp add: rbt_min_opt_Branch intro: left_le_key rbt_min_opt_in_set)
   1.306 +    next
   1.307 +      case goal2 with left_non_empty show ?case by (auto simp add: rbt_min_opt_Branch)
   1.308 +    next 
   1.309 +      case goal3 show ?case
   1.310 +      proof -
   1.311 +        from goal3 have "rbt_min_opt t1 \<le> k" by (simp add: left_le_key rbt_min_opt_in_set)
   1.312 +        moreover from goal3 have "k \<le> y" by (simp add: key_le_right)
   1.313 +        ultimately show ?thesis using goal3 by (simp add: rbt_min_opt_Branch)
   1.314 +      qed
   1.315 +    qed
   1.316 +qed
   1.317 +
   1.318 +lemma rbt_min_eq_rbt_min_opt:
   1.319 +  assumes "t \<noteq> RBT_Impl.Empty"
   1.320 +  assumes "is_rbt t"
   1.321 +  shows "rbt_min t = rbt_min_opt t"
   1.322 +proof -
   1.323 +  interpret ab_semigroup_idem_mult "(min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_min
   1.324 +    unfolding class.ab_semigroup_idem_mult_def by blast
   1.325 +  show ?thesis
   1.326 +    by (simp add: Min_eqI rbt_min_opt_is_min rbt_min_opt_in_set assms Min_def[symmetric]
   1.327 +      non_empty_rbt_keys fold1_set_fold[symmetric] rbt_min_def rbt_fold1_keys_def)
   1.328 +qed
   1.329 +
   1.330 +(* maximum *)
   1.331 +
   1.332 +definition rbt_max :: "('a::linorder, unit) RBT_Impl.rbt \<Rightarrow> 'a" 
   1.333 +  where "rbt_max t = rbt_fold1_keys max t"
   1.334 +
   1.335 +lemma fold_max_triv:
   1.336 +  fixes k :: "_ :: linorder"
   1.337 +  shows "(\<forall>x\<in>set xs. x \<le> k) \<Longrightarrow> List.fold max xs k = k" 
   1.338 +by (induct xs) (auto simp add: max_def)
   1.339 +
   1.340 +lemma fold_max_rev_eq:
   1.341 +  fixes xs :: "('a :: linorder) list"
   1.342 +  assumes "xs \<noteq> []"
   1.343 +  shows "List.fold max (tl xs) (hd xs) = List.fold max (tl (rev xs)) (hd (rev xs))" 
   1.344 +proof -
   1.345 +  interpret ab_semigroup_idem_mult "(max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_max
   1.346 +    unfolding class.ab_semigroup_idem_mult_def by blast
   1.347 +  show ?thesis
   1.348 +  using assms by (auto simp add: fold1_set_fold[symmetric])
   1.349 +qed
   1.350 +
   1.351 +lemma rbt_max_simps:
   1.352 +  assumes "is_rbt (Branch c lt k v RBT_Impl.Empty)" 
   1.353 +  shows "rbt_max (Branch c lt k v RBT_Impl.Empty) = k"
   1.354 +proof -
   1.355 +  have "List.fold max (tl (rev(RBT_Impl.keys lt @ [k]))) (hd (rev(RBT_Impl.keys lt @ [k]))) = k"
   1.356 +    using assms by (auto intro!: fold_max_triv dest!: left_le_key is_rbt_rbt_sorted)
   1.357 +  then show ?thesis by (auto simp add: rbt_max_def rbt_fold1_keys_def fold_max_rev_eq)
   1.358 +qed
   1.359 +
   1.360 +fun rbt_max_opt where
   1.361 +  "rbt_max_opt (Branch c lt k v RBT_Impl.Empty) = k" |
   1.362 +  "rbt_max_opt (Branch c lt k v (Branch rc rlc rk rv rrt)) = rbt_max_opt (Branch rc rlc rk rv rrt)"
   1.363 +
   1.364 +lemma rbt_max_opt_Branch:
   1.365 +  "t2 \<noteq> rbt.Empty \<Longrightarrow> rbt_max_opt (Branch c t1 k () t2) = rbt_max_opt t2" 
   1.366 +by (cases t2) auto
   1.367 +
   1.368 +lemma rbt_max_opt_induct [case_names empty right_empty right_non_empty]:
   1.369 +  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
   1.370 +  assumes "P rbt.Empty"
   1.371 +  assumes "\<And>color t1 a b t2. P t1 \<Longrightarrow> P t2 \<Longrightarrow> t2 = rbt.Empty \<Longrightarrow> P (Branch color t1 a b t2)"
   1.372 +  assumes "\<And>color t1 a b t2. P t1 \<Longrightarrow> P t2 \<Longrightarrow> t2 \<noteq> rbt.Empty \<Longrightarrow> P (Branch color t1 a b t2)"
   1.373 +  shows "P t"
   1.374 +using assms
   1.375 +  apply (induction t)
   1.376 +  apply simp
   1.377 +  apply (case_tac "t2 = rbt.Empty")
   1.378 +  apply simp_all
   1.379 +done
   1.380 +
   1.381 +lemma rbt_max_opt_in_set: 
   1.382 +  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
   1.383 +  assumes "t \<noteq> rbt.Empty"
   1.384 +  shows "rbt_max_opt t \<in> set (RBT_Impl.keys t)"
   1.385 +using assms by (induction t rule: rbt_max_opt.induct) (auto)
   1.386 +
   1.387 +lemma rbt_max_opt_is_max:
   1.388 +  fixes t :: "('a :: linorder, unit) RBT_Impl.rbt"
   1.389 +  assumes "rbt_sorted t"
   1.390 +  assumes "t \<noteq> rbt.Empty"
   1.391 +  shows "\<And>y. y \<in> set (RBT_Impl.keys t) \<Longrightarrow> y \<le> rbt_max_opt t"
   1.392 +using assms 
   1.393 +proof (induction t rule: rbt_max_opt_induct)
   1.394 +  case empty
   1.395 +    then show ?case by simp
   1.396 +next
   1.397 +  case right_empty
   1.398 +    then show ?case by (auto intro: left_le_key simp del: rbt_sorted.simps)
   1.399 +next
   1.400 +  case (right_non_empty c t1 k v t2 y)
   1.401 +    then have "y = k \<or> y \<in> set (RBT_Impl.keys t2) \<or> y \<in> set (RBT_Impl.keys t1)" by auto
   1.402 +    with right_non_empty show ?case 
   1.403 +    proof(elim disjE)
   1.404 +      case goal1 then show ?case 
   1.405 +        by (auto simp add: rbt_max_opt_Branch intro: key_le_right rbt_max_opt_in_set)
   1.406 +    next
   1.407 +      case goal2 with right_non_empty show ?case by (auto simp add: rbt_max_opt_Branch)
   1.408 +    next 
   1.409 +      case goal3 show ?case
   1.410 +      proof -
   1.411 +        from goal3 have "rbt_max_opt t2 \<ge> k" by (simp add: key_le_right rbt_max_opt_in_set)
   1.412 +        moreover from goal3 have "y \<le> k" by (simp add: left_le_key)
   1.413 +        ultimately show ?thesis using goal3 by (simp add: rbt_max_opt_Branch)
   1.414 +      qed
   1.415 +    qed
   1.416 +qed
   1.417 +
   1.418 +lemma rbt_max_eq_rbt_max_opt:
   1.419 +  assumes "t \<noteq> RBT_Impl.Empty"
   1.420 +  assumes "is_rbt t"
   1.421 +  shows "rbt_max t = rbt_max_opt t"
   1.422 +proof -
   1.423 +  interpret ab_semigroup_idem_mult "(max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" using ab_semigroup_idem_mult_max
   1.424 +    unfolding class.ab_semigroup_idem_mult_def by blast
   1.425 +  show ?thesis
   1.426 +    by (simp add: Max_eqI rbt_max_opt_is_max rbt_max_opt_in_set assms Max_def[symmetric]
   1.427 +      non_empty_rbt_keys fold1_set_fold[symmetric] rbt_max_def rbt_fold1_keys_def)
   1.428 +qed
   1.429 +
   1.430 +
   1.431 +(** abstract **)
   1.432 +
   1.433 +lift_definition fold1_keys :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> 'a"
   1.434 +  is rbt_fold1_keys by simp
   1.435 +
   1.436 +lemma fold1_keys_def_alt:
   1.437 +  "fold1_keys f t = List.fold f (tl(keys t)) (hd(keys t))"
   1.438 +  by transfer (simp add: rbt_fold1_keys_def)
   1.439 +
   1.440 +lemma finite_fold1_fold1_keys:
   1.441 +  assumes "class.ab_semigroup_mult f"
   1.442 +  assumes "\<not> (is_empty t)"
   1.443 +  shows "Finite_Set.fold1 f (Set t) = fold1_keys f t"
   1.444 +proof -
   1.445 +  interpret ab_semigroup_mult f by fact
   1.446 +  show ?thesis using assms 
   1.447 +    by (auto simp: fold1_keys_def_alt set_keys fold_def_alt fold1_distinct_set_fold non_empty_keys)
   1.448 +qed
   1.449 +
   1.450 +(* minimum *)
   1.451 +
   1.452 +lift_definition r_min :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_min by simp
   1.453 +
   1.454 +lift_definition r_min_opt :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_min_opt by simp
   1.455 +
   1.456 +lemma r_min_alt_def: "r_min t = fold1_keys min t"
   1.457 +by transfer (simp add: rbt_min_def)
   1.458 +
   1.459 +lemma r_min_eq_r_min_opt:
   1.460 +  assumes "\<not> (is_empty t)"
   1.461 +  shows "r_min t = r_min_opt t"
   1.462 +using assms unfolding is_empty_empty by transfer (auto intro: rbt_min_eq_rbt_min_opt)
   1.463 +
   1.464 +lemma fold_keys_min_top_eq:
   1.465 +  fixes t :: "('a :: {linorder, bounded_lattice_top}, unit) rbt"
   1.466 +  assumes "\<not> (is_empty t)"
   1.467 +  shows "fold_keys min t top = fold1_keys min t"
   1.468 +proof -
   1.469 +  have *: "\<And>t. RBT_Impl.keys t \<noteq> [] \<Longrightarrow> List.fold min (RBT_Impl.keys t) top = 
   1.470 +    List.fold min (hd(RBT_Impl.keys t) # tl(RBT_Impl.keys t)) top"
   1.471 +    by (simp add: hd_Cons_tl[symmetric])
   1.472 +  { fix x :: "_ :: {linorder, bounded_lattice_top}" and xs
   1.473 +    have "List.fold min (x#xs) top = List.fold min xs x"
   1.474 +    by (simp add: inf_min[symmetric])
   1.475 +  } note ** = this
   1.476 +  show ?thesis using assms
   1.477 +    unfolding fold_keys_def_alt fold1_keys_def_alt is_empty_empty
   1.478 +    apply transfer 
   1.479 +    apply (case_tac t) 
   1.480 +    apply simp 
   1.481 +    apply (subst *)
   1.482 +    apply simp
   1.483 +    apply (subst **)
   1.484 +    apply simp
   1.485 +  done
   1.486 +qed
   1.487 +
   1.488 +(* maximum *)
   1.489 +
   1.490 +lift_definition r_max :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_max by simp
   1.491 +
   1.492 +lift_definition r_max_opt :: "('a :: linorder, unit) rbt \<Rightarrow> 'a" is rbt_max_opt by simp
   1.493 +
   1.494 +lemma r_max_alt_def: "r_max t = fold1_keys max t"
   1.495 +by transfer (simp add: rbt_max_def)
   1.496 +
   1.497 +lemma r_max_eq_r_max_opt:
   1.498 +  assumes "\<not> (is_empty t)"
   1.499 +  shows "r_max t = r_max_opt t"
   1.500 +using assms unfolding is_empty_empty by transfer (auto intro: rbt_max_eq_rbt_max_opt)
   1.501 +
   1.502 +lemma fold_keys_max_bot_eq:
   1.503 +  fixes t :: "('a :: {linorder, bounded_lattice_bot}, unit) rbt"
   1.504 +  assumes "\<not> (is_empty t)"
   1.505 +  shows "fold_keys max t bot = fold1_keys max t"
   1.506 +proof -
   1.507 +  have *: "\<And>t. RBT_Impl.keys t \<noteq> [] \<Longrightarrow> List.fold max (RBT_Impl.keys t) bot = 
   1.508 +    List.fold max (hd(RBT_Impl.keys t) # tl(RBT_Impl.keys t)) bot"
   1.509 +    by (simp add: hd_Cons_tl[symmetric])
   1.510 +  { fix x :: "_ :: {linorder, bounded_lattice_bot}" and xs
   1.511 +    have "List.fold max (x#xs) bot = List.fold max xs x"
   1.512 +    by (simp add: sup_max[symmetric])
   1.513 +  } note ** = this
   1.514 +  show ?thesis using assms
   1.515 +    unfolding fold_keys_def_alt fold1_keys_def_alt is_empty_empty
   1.516 +    apply transfer 
   1.517 +    apply (case_tac t) 
   1.518 +    apply simp 
   1.519 +    apply (subst *)
   1.520 +    apply simp
   1.521 +    apply (subst **)
   1.522 +    apply simp
   1.523 +  done
   1.524 +qed
   1.525 +
   1.526 +
   1.527 +section {* Code equations *}
   1.528 +
   1.529 +code_datatype Set Coset
   1.530 +
   1.531 +lemma empty_Set [code]:
   1.532 +  "Set.empty = Set RBT.empty"
   1.533 +by (auto simp: Set_def)
   1.534 +
   1.535 +lemma UNIV_Coset [code]:
   1.536 +  "UNIV = Coset RBT.empty"
   1.537 +by (auto simp: Set_def)
   1.538 +
   1.539 +lemma is_empty_Set [code]:
   1.540 +  "Set.is_empty (Set t) = RBT.is_empty t"
   1.541 +  unfolding Set.is_empty_def by (auto simp: fun_eq_iff Set_def intro: lookup_empty_empty[THEN iffD1])
   1.542 +
   1.543 +lemma compl_code [code]:
   1.544 +  "- Set xs = Coset xs"
   1.545 +  "- Coset xs = Set xs"
   1.546 +by (simp_all add: Set_def)
   1.547 +
   1.548 +lemma member_code [code]:
   1.549 +  "x \<in> (Set t) = (RBT.lookup t x = Some ())"
   1.550 +  "x \<in> (Coset t) = (RBT.lookup t x = None)"
   1.551 +by (simp_all add: Set_def)
   1.552 +
   1.553 +lemma insert_code [code]:
   1.554 +  "Set.insert x (Set t) = Set (RBT.insert x () t)"
   1.555 +  "Set.insert x (Coset t) = Coset (RBT.delete x t)"
   1.556 +by (auto simp: Set_def)
   1.557 +
   1.558 +lemma remove_code [code]:
   1.559 +  "Set.remove x (Set t) = Set (RBT.delete x t)"
   1.560 +  "Set.remove x (Coset t) = Coset (RBT.insert x () t)"
   1.561 +by (auto simp: Set_def)
   1.562 +
   1.563 +lemma union_Set [code]:
   1.564 +  "Set t \<union> A = fold_keys Set.insert t A"
   1.565 +proof -
   1.566 +  interpret comp_fun_idem Set.insert
   1.567 +    by (fact comp_fun_idem_insert)
   1.568 +  from finite_fold_fold_keys[OF `comp_fun_commute Set.insert`]
   1.569 +  show ?thesis by (auto simp add: union_fold_insert)
   1.570 +qed
   1.571 +
   1.572 +lemma inter_Set [code]:
   1.573 +  "A \<inter> Set t = rbt_filter (\<lambda>k. k \<in> A) t"
   1.574 +by (simp add: inter_filter finite_filter_rbt_filter)
   1.575 +
   1.576 +lemma minus_Set [code]:
   1.577 +  "A - Set t = fold_keys Set.remove t A"
   1.578 +proof -
   1.579 +  interpret comp_fun_idem Set.remove
   1.580 +    by (fact comp_fun_idem_remove)
   1.581 +  from finite_fold_fold_keys[OF `comp_fun_commute Set.remove`]
   1.582 +  show ?thesis by (auto simp add: minus_fold_remove)
   1.583 +qed
   1.584 +
   1.585 +lemma union_Coset [code]:
   1.586 +  "Coset t \<union> A = - rbt_filter (\<lambda>k. k \<notin> A) t"
   1.587 +proof -
   1.588 +  have *: "\<And>A B. (-A \<union> B) = -(-B \<inter> A)" by blast
   1.589 +  show ?thesis by (simp del: boolean_algebra_class.compl_inf add: * inter_Set)
   1.590 +qed
   1.591 + 
   1.592 +lemma union_Set_Set [code]:
   1.593 +  "Set t1 \<union> Set t2 = Set (union t1 t2)"
   1.594 +by (auto simp add: lookup_union map_add_Some_iff Set_def)
   1.595 +
   1.596 +lemma inter_Coset [code]:
   1.597 +  "A \<inter> Coset t = fold_keys Set.remove t A"
   1.598 +by (simp add: Diff_eq [symmetric] minus_Set)
   1.599 +
   1.600 +lemma inter_Coset_Coset [code]:
   1.601 +  "Coset t1 \<inter> Coset t2 = Coset (union t1 t2)"
   1.602 +by (auto simp add: lookup_union map_add_Some_iff Set_def)
   1.603 +
   1.604 +lemma minus_Coset [code]:
   1.605 +  "A - Coset t = rbt_filter (\<lambda>k. k \<in> A) t"
   1.606 +by (simp add: inter_Set[simplified Int_commute])
   1.607 +
   1.608 +lemma project_Set [code]:
   1.609 +  "Set.project P (Set t) = (rbt_filter P t)"
   1.610 +by (auto simp add: project_filter finite_filter_rbt_filter)
   1.611 +
   1.612 +lemma image_Set [code]:
   1.613 +  "image f (Set t) = fold_keys (\<lambda>k A. Set.insert (f k) A) t {}"
   1.614 +proof -
   1.615 +  have "comp_fun_commute (\<lambda>k. Set.insert (f k))" by default auto
   1.616 +  then show ?thesis by (auto simp add: image_fold_insert intro!: finite_fold_fold_keys)
   1.617 +qed
   1.618 +
   1.619 +lemma Ball_Set [code]:
   1.620 +  "Ball (Set t) P \<longleftrightarrow> foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> P k) t True"
   1.621 +proof -
   1.622 +  have "comp_fun_commute (\<lambda>k s. s \<and> P k)" by default auto
   1.623 +  then show ?thesis 
   1.624 +    by (simp add: foldi_fold_conj[symmetric] Ball_fold finite_fold_fold_keys)
   1.625 +qed
   1.626 +
   1.627 +lemma Bex_Set [code]:
   1.628 +  "Bex (Set t) P \<longleftrightarrow> foldi (\<lambda>s. s = False) (\<lambda>k v s. s \<or> P k) t False"
   1.629 +proof -
   1.630 +  have "comp_fun_commute (\<lambda>k s. s \<or> P k)" by default auto
   1.631 +  then show ?thesis 
   1.632 +    by (simp add: foldi_fold_disj[symmetric] Bex_fold finite_fold_fold_keys)
   1.633 +qed
   1.634 +
   1.635 +lemma subset_code [code]:
   1.636 +  "Set t \<le> B \<longleftrightarrow> (\<forall>x\<in>Set t. x \<in> B)"
   1.637 +  "A \<le> Coset t \<longleftrightarrow> (\<forall>y\<in>Set t. y \<notin> A)"
   1.638 +by auto
   1.639 +
   1.640 +definition non_empty_trees where [code del]: "non_empty_trees t1 t2 \<longleftrightarrow> Coset t1 \<le> Set t2"
   1.641 +
   1.642 +code_abort non_empty_trees
   1.643 +
   1.644 +lemma subset_Coset_empty_Set_empty [code]:
   1.645 +  "Coset t1 \<le> Set t2 \<longleftrightarrow> (case (impl_of t1, impl_of t2) of 
   1.646 +    (rbt.Empty, rbt.Empty) => False |
   1.647 +    (_, _) => non_empty_trees t1 t2)"
   1.648 +proof -
   1.649 +  have *: "\<And>t. impl_of t = rbt.Empty \<Longrightarrow> t = RBT rbt.Empty"
   1.650 +    by (subst(asm) RBT_inverse[symmetric]) (auto simp: impl_of_inject)
   1.651 +  have **: "Lifting.invariant is_rbt rbt.Empty rbt.Empty" unfolding Lifting.invariant_def by simp
   1.652 +  show ?thesis  
   1.653 +    by (auto simp: Set_def lookup.abs_eq[OF **] dest!: * split: rbt.split simp: non_empty_trees_def)
   1.654 +qed
   1.655 +
   1.656 +text {* A frequent case – avoid intermediate sets *}
   1.657 +lemma [code_unfold]:
   1.658 +  "Set t1 \<subseteq> Set t2 \<longleftrightarrow> foldi (\<lambda>s. s = True) (\<lambda>k v s. s \<and> k \<in> Set t2) t1 True"
   1.659 +by (simp add: subset_code Ball_Set)
   1.660 +
   1.661 +lemma card_Set [code]:
   1.662 +  "card (Set t) = fold_keys (\<lambda>_ n. n + 1) t 0"
   1.663 +by (auto simp add: card_def fold_image_def intro!: finite_fold_fold_keys) (default, simp) 
   1.664 +
   1.665 +lemma setsum_Set [code]:
   1.666 +  "setsum f (Set xs) = fold_keys (plus o f) xs 0"
   1.667 +proof -
   1.668 +  have "comp_fun_commute (\<lambda>x. op + (f x))" by default (auto simp: add_ac)
   1.669 +  then show ?thesis 
   1.670 +    by (auto simp add: setsum_def fold_image_def finite_fold_fold_keys o_def)
   1.671 +qed
   1.672 +
   1.673 +definition not_a_singleton_tree  where [code del]: "not_a_singleton_tree x y = x y"
   1.674 +
   1.675 +code_abort not_a_singleton_tree
   1.676 +
   1.677 +lemma the_elem_set [code]:
   1.678 +  fixes t :: "('a :: linorder, unit) rbt"
   1.679 +  shows "the_elem (Set t) = (case impl_of t of 
   1.680 +    (Branch RBT_Impl.B RBT_Impl.Empty x () RBT_Impl.Empty) \<Rightarrow> x
   1.681 +    | _ \<Rightarrow> not_a_singleton_tree the_elem (Set t))"
   1.682 +proof -
   1.683 +  {
   1.684 +    fix x :: "'a :: linorder"
   1.685 +    let ?t = "Branch RBT_Impl.B RBT_Impl.Empty x () RBT_Impl.Empty" 
   1.686 +    have *:"?t \<in> {t. is_rbt t}" unfolding is_rbt_def by auto
   1.687 +    then have **:"Lifting.invariant is_rbt ?t ?t" unfolding Lifting.invariant_def by auto
   1.688 +
   1.689 +    have "impl_of t = ?t \<Longrightarrow> the_elem (Set t) = x" 
   1.690 +      by (subst(asm) RBT_inverse[symmetric, OF *])
   1.691 +        (auto simp: Set_def the_elem_def lookup.abs_eq[OF **] impl_of_inject)
   1.692 +  }
   1.693 +  then show ?thesis unfolding not_a_singleton_tree_def
   1.694 +    by(auto split: rbt.split unit.split color.split)
   1.695 +qed
   1.696 +
   1.697 +lemma Pow_Set [code]:
   1.698 +  "Pow (Set t) = fold_keys (\<lambda>x A. A \<union> Set.insert x ` A) t {{}}"
   1.699 +by (simp add: Pow_fold finite_fold_fold_keys[OF comp_fun_commute_Pow_fold])
   1.700 +
   1.701 +lemma product_Set [code]:
   1.702 +  "Product_Type.product (Set t1) (Set t2) = 
   1.703 +    fold_keys (\<lambda>x A. fold_keys (\<lambda>y. Set.insert (x, y)) t2 A) t1 {}"
   1.704 +proof -
   1.705 +  have *:"\<And>x. comp_fun_commute (\<lambda>y. Set.insert (x, y))" by default auto
   1.706 +  show ?thesis using finite_fold_fold_keys[OF comp_fun_commute_product_fold, of "Set t2" "{}" "t1"]  
   1.707 +    by (simp add: product_fold Product_Type.product_def finite_fold_fold_keys[OF *])
   1.708 +qed
   1.709 +
   1.710 +lemma Id_on_Set [code]:
   1.711 +  "Id_on (Set t) =  fold_keys (\<lambda>x. Set.insert (x, x)) t {}"
   1.712 +proof -
   1.713 +  have "comp_fun_commute (\<lambda>x. Set.insert (x, x))" by default auto
   1.714 +  then show ?thesis
   1.715 +    by (auto simp add: Id_on_fold intro!: finite_fold_fold_keys)
   1.716 +qed
   1.717 +
   1.718 +lemma Image_Set [code]:
   1.719 +  "(Set t) `` S = fold_keys (\<lambda>(x,y) A. if x \<in> S then Set.insert y A else A) t {}"
   1.720 +by (auto simp add: Image_fold finite_fold_fold_keys[OF comp_fun_commute_Image_fold])
   1.721 +
   1.722 +lemma trancl_set_ntrancl [code]:
   1.723 +  "trancl (Set t) = ntrancl (card (Set t) - 1) (Set t)"
   1.724 +by (simp add: finite_trancl_ntranl)
   1.725 +
   1.726 +lemma relcomp_Set[code]:
   1.727 +  "(Set t1) O (Set t2) = fold_keys 
   1.728 +    (\<lambda>(x,y) A. fold_keys (\<lambda>(w,z) A'. if y = w then Set.insert (x,z) A' else A') t2 A) t1 {}"
   1.729 +proof -
   1.730 +  interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
   1.731 +  have *: "\<And>x y. comp_fun_commute (\<lambda>(w, z) A'. if y = w then Set.insert (x, z) A' else A')"
   1.732 +    by default (auto simp add: fun_eq_iff)
   1.733 +  show ?thesis using finite_fold_fold_keys[OF comp_fun_commute_relcomp_fold, of "Set t2" "{}" t1]
   1.734 +    by (simp add: relcomp_fold finite_fold_fold_keys[OF *])
   1.735 +qed
   1.736 +
   1.737 +lemma wf_set [code]:
   1.738 +  "wf (Set t) = acyclic (Set t)"
   1.739 +by (simp add: wf_iff_acyclic_if_finite)
   1.740 +
   1.741 +definition not_non_empty_tree  where [code del]: "not_non_empty_tree x y = x y"
   1.742 +
   1.743 +code_abort not_non_empty_tree
   1.744 +
   1.745 +lemma Min_fin_set_fold [code]:
   1.746 +  "Min (Set t) = (if is_empty t then not_non_empty_tree Min (Set t) else r_min_opt t)"
   1.747 +proof -
   1.748 +  have *:"(class.ab_semigroup_mult (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a))" using ab_semigroup_idem_mult_min
   1.749 +    unfolding class.ab_semigroup_idem_mult_def by blast
   1.750 +  show ?thesis
   1.751 +    by (auto simp add: Min_def not_non_empty_tree_def finite_fold1_fold1_keys[OF *] r_min_alt_def 
   1.752 +      r_min_eq_r_min_opt[symmetric])  
   1.753 +qed
   1.754 +
   1.755 +lemma Inf_fin_set_fold [code]:
   1.756 +  "Inf_fin (Set t) = Min (Set t)"
   1.757 +by (simp add: inf_min Inf_fin_def Min_def)
   1.758 +
   1.759 +lemma Inf_Set_fold:
   1.760 +  fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
   1.761 +  shows "Inf (Set t) = (if is_empty t then top else r_min_opt t)"
   1.762 +proof -
   1.763 +  have "comp_fun_commute (min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" by default (simp add: fun_eq_iff ac_simps)
   1.764 +  then have "t \<noteq> empty \<Longrightarrow> Finite_Set.fold min top (Set t) = fold1_keys min t"
   1.765 +    by (simp add: finite_fold_fold_keys fold_keys_min_top_eq)
   1.766 +  then show ?thesis 
   1.767 +    by (auto simp add: Inf_fold_inf inf_min empty_Set[symmetric] r_min_eq_r_min_opt[symmetric] r_min_alt_def)
   1.768 +qed
   1.769 +
   1.770 +definition Inf' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a" where [code del]: "Inf' x = Inf x"
   1.771 +declare Inf'_def[symmetric, code_unfold]
   1.772 +declare Inf_Set_fold[folded Inf'_def, code]
   1.773 +
   1.774 +lemma INFI_Set_fold [code]:
   1.775 +  "INFI (Set t) f = fold_keys (inf \<circ> f) t top"
   1.776 +proof -
   1.777 +  have "comp_fun_commute ((inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a) \<circ> f)" 
   1.778 +    by default (auto simp add: fun_eq_iff ac_simps)
   1.779 +  then show ?thesis
   1.780 +    by (auto simp: INF_fold_inf finite_fold_fold_keys)
   1.781 +qed
   1.782 +
   1.783 +lemma Max_fin_set_fold [code]:
   1.784 +  "Max (Set t) = (if is_empty t then not_non_empty_tree Max (Set t) else r_max_opt t)"
   1.785 +proof -
   1.786 +  have *:"(class.ab_semigroup_mult (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a))" using ab_semigroup_idem_mult_max
   1.787 +    unfolding class.ab_semigroup_idem_mult_def by blast
   1.788 +  show ?thesis
   1.789 +    by (auto simp add: Max_def not_non_empty_tree_def finite_fold1_fold1_keys[OF *] r_max_alt_def 
   1.790 +      r_max_eq_r_max_opt[symmetric])  
   1.791 +qed
   1.792 +
   1.793 +lemma Sup_fin_set_fold [code]:
   1.794 +  "Sup_fin (Set t) = Max (Set t)"
   1.795 +by (simp add: sup_max Sup_fin_def Max_def)
   1.796 +
   1.797 +lemma Sup_Set_fold:
   1.798 +  fixes t :: "('a :: {linorder, complete_lattice}, unit) rbt"
   1.799 +  shows "Sup (Set t) = (if is_empty t then bot else r_max_opt t)"
   1.800 +proof -
   1.801 +  have "comp_fun_commute (max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a)" by default (simp add: fun_eq_iff ac_simps)
   1.802 +  then have "t \<noteq> empty \<Longrightarrow> Finite_Set.fold max bot (Set t) = fold1_keys max t"
   1.803 +    by (simp add: finite_fold_fold_keys fold_keys_max_bot_eq)
   1.804 +  then show ?thesis 
   1.805 +    by (auto simp add: Sup_fold_sup sup_max empty_Set[symmetric] r_max_eq_r_max_opt[symmetric] r_max_alt_def)
   1.806 +qed
   1.807 +
   1.808 +definition Sup' :: "'a :: {linorder, complete_lattice} set \<Rightarrow> 'a" where [code del]: "Sup' x = Sup x"
   1.809 +declare Sup'_def[symmetric, code_unfold]
   1.810 +declare Sup_Set_fold[folded Sup'_def, code]
   1.811 +
   1.812 +lemma SUPR_Set_fold [code]:
   1.813 +  "SUPR (Set t) f = fold_keys (sup \<circ> f) t bot"
   1.814 +proof -
   1.815 +  have "comp_fun_commute ((sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a) \<circ> f)" 
   1.816 +    by default (auto simp add: fun_eq_iff ac_simps)
   1.817 +  then show ?thesis
   1.818 +    by (auto simp: SUP_fold_sup finite_fold_fold_keys)
   1.819 +qed
   1.820 +
   1.821 +lemma sorted_list_set[code]:
   1.822 +  "sorted_list_of_set (Set t) = keys t"
   1.823 +by (auto simp add: set_keys intro: sorted_distinct_set_unique) 
   1.824 +
   1.825 +hide_const (open) RBT_Set.Set RBT_Set.Coset
   1.826 +
   1.827 +end