src/HOL/Library/RBT_Impl.thy
changeset 46133 d9fe85d3d2cd
parent 45990 b7b905b23b2a
child 47397 d654c73e4b12
equal deleted inserted replaced
46132:5a29dbf4c155 46133:d9fe85d3d2cd
  1047 
  1047 
  1048 
  1048 
  1049 subsection {* Folding over entries *}
  1049 subsection {* Folding over entries *}
  1050 
  1050 
  1051 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
  1051 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
  1052   "fold f t = More_List.fold (prod_case f) (entries t)"
  1052   "fold f t = List.fold (prod_case f) (entries t)"
  1053 
  1053 
  1054 lemma fold_simps [simp, code]:
  1054 lemma fold_simps [simp, code]:
  1055   "fold f Empty = id"
  1055   "fold f Empty = id"
  1056   "fold f (Branch c lt k v rt) = fold f rt \<circ> f k v \<circ> fold f lt"
  1056   "fold f (Branch c lt k v rt) = fold f rt \<circ> f k v \<circ> fold f lt"
  1057   by (simp_all add: fold_def fun_eq_iff)
  1057   by (simp_all add: fold_def fun_eq_iff)
  1069 lemma lookup_bulkload:
  1069 lemma lookup_bulkload:
  1070   "lookup (bulkload xs) = map_of xs"
  1070   "lookup (bulkload xs) = map_of xs"
  1071 proof -
  1071 proof -
  1072   obtain ys where "ys = rev xs" by simp
  1072   obtain ys where "ys = rev xs" by simp
  1073   have "\<And>t. is_rbt t \<Longrightarrow>
  1073   have "\<And>t. is_rbt t \<Longrightarrow>
  1074     lookup (More_List.fold (prod_case insert) ys t) = lookup t ++ map_of (rev ys)"
  1074     lookup (List.fold (prod_case insert) ys t) = lookup t ++ map_of (rev ys)"
  1075       by (induct ys) (simp_all add: bulkload_def lookup_insert prod_case_beta)
  1075       by (induct ys) (simp_all add: bulkload_def lookup_insert prod_case_beta)
  1076   from this Empty_is_rbt have
  1076   from this Empty_is_rbt have
  1077     "lookup (More_List.fold (prod_case insert) (rev xs) Empty) = lookup Empty ++ map_of xs"
  1077     "lookup (List.fold (prod_case insert) (rev xs) Empty) = lookup Empty ++ map_of xs"
  1078      by (simp add: `ys = rev xs`)
  1078      by (simp add: `ys = rev xs`)
  1079   then show ?thesis by (simp add: bulkload_def lookup_Empty foldr_fold_rev)
  1079   then show ?thesis by (simp add: bulkload_def lookup_Empty foldr_def)
  1080 qed
  1080 qed
  1081 
  1081 
  1082 hide_const (open) R B Empty insert delete entries keys bulkload lookup map_entry map fold union sorted
  1082 hide_const (open) R B Empty insert delete entries keys bulkload lookup map_entry map fold union sorted
  1083 
  1083 
  1084 end
  1084 end