src/HOL/Library/RBT_Impl.thy
changeset 55414 eab03e9cee8a
parent 55412 eb2caacf3ba4
child 55417 01fbfb60c33e
equal deleted inserted replaced
55413:a8e96847523c 55414:eab03e9cee8a
  1065 hide_const (open) map
  1065 hide_const (open) map
  1066 
  1066 
  1067 subsection {* Folding over entries *}
  1067 subsection {* Folding over entries *}
  1068 
  1068 
  1069 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
  1069 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
  1070   "fold f t = List.fold (prod_case f) (entries t)"
  1070   "fold f t = List.fold (case_prod f) (entries t)"
  1071 
  1071 
  1072 lemma fold_simps [simp]:
  1072 lemma fold_simps [simp]:
  1073   "fold f Empty = id"
  1073   "fold f Empty = id"
  1074   "fold f (Branch c lt k v rt) = fold f rt \<circ> f k v \<circ> fold f lt"
  1074   "fold f (Branch c lt k v rt) = fold f rt \<circ> f k v \<circ> fold f lt"
  1075   by (simp_all add: fold_def fun_eq_iff)
  1075   by (simp_all add: fold_def fun_eq_iff)
  1108 lemma rbt_lookup_rbt_bulkload:
  1108 lemma rbt_lookup_rbt_bulkload:
  1109   "rbt_lookup (rbt_bulkload xs) = map_of xs"
  1109   "rbt_lookup (rbt_bulkload xs) = map_of xs"
  1110 proof -
  1110 proof -
  1111   obtain ys where "ys = rev xs" by simp
  1111   obtain ys where "ys = rev xs" by simp
  1112   have "\<And>t. is_rbt t \<Longrightarrow>
  1112   have "\<And>t. is_rbt t \<Longrightarrow>
  1113     rbt_lookup (List.fold (prod_case rbt_insert) ys t) = rbt_lookup t ++ map_of (rev ys)"
  1113     rbt_lookup (List.fold (case_prod rbt_insert) ys t) = rbt_lookup t ++ map_of (rev ys)"
  1114       by (induct ys) (simp_all add: rbt_bulkload_def rbt_lookup_rbt_insert prod_case_beta)
  1114       by (induct ys) (simp_all add: rbt_bulkload_def rbt_lookup_rbt_insert case_prod_beta)
  1115   from this Empty_is_rbt have
  1115   from this Empty_is_rbt have
  1116     "rbt_lookup (List.fold (prod_case rbt_insert) (rev xs) Empty) = rbt_lookup Empty ++ map_of xs"
  1116     "rbt_lookup (List.fold (case_prod rbt_insert) (rev xs) Empty) = rbt_lookup Empty ++ map_of xs"
  1117      by (simp add: `ys = rev xs`)
  1117      by (simp add: `ys = rev xs`)
  1118   then show ?thesis by (simp add: rbt_bulkload_def rbt_lookup_Empty foldr_conv_fold)
  1118   then show ?thesis by (simp add: rbt_bulkload_def rbt_lookup_Empty foldr_conv_fold)
  1119 qed
  1119 qed
  1120 
  1120 
  1121 end
  1121 end