equal
deleted
inserted
replaced
1 (* Title: HOL/Library/RBT.thy |
1 (* Title: HOL/Library/RBT.thy |
2 Author: Lukas Bulwahn and Ondrej Kuncar |
2 Author: Lukas Bulwahn and Ondrej Kuncar |
3 *) |
3 *) |
4 |
4 |
5 section {* Abstract type of RBT trees *} |
5 section \<open>Abstract type of RBT trees\<close> |
6 |
6 |
7 theory RBT |
7 theory RBT |
8 imports Main RBT_Impl |
8 imports Main RBT_Impl |
9 begin |
9 begin |
10 |
10 |
11 subsection {* Type definition *} |
11 subsection \<open>Type definition\<close> |
12 |
12 |
13 typedef ('a, 'b) rbt = "{t :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt. is_rbt t}" |
13 typedef ('a, 'b) rbt = "{t :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt. is_rbt t}" |
14 morphisms impl_of RBT |
14 morphisms impl_of RBT |
15 proof - |
15 proof - |
16 have "RBT_Impl.Empty \<in> ?rbt" by simp |
16 have "RBT_Impl.Empty \<in> ?rbt" by simp |
31 |
31 |
32 lemma RBT_impl_of [simp, code abstype]: |
32 lemma RBT_impl_of [simp, code abstype]: |
33 "RBT (impl_of t) = t" |
33 "RBT (impl_of t) = t" |
34 by (simp add: impl_of_inverse) |
34 by (simp add: impl_of_inverse) |
35 |
35 |
36 subsection {* Primitive operations *} |
36 subsection \<open>Primitive operations\<close> |
37 |
37 |
38 setup_lifting type_definition_rbt |
38 setup_lifting type_definition_rbt |
39 |
39 |
40 lift_definition lookup :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" is "rbt_lookup" . |
40 lift_definition lookup :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" is "rbt_lookup" . |
41 |
41 |
66 by (simp add: rbt_union_is_rbt) |
66 by (simp add: rbt_union_is_rbt) |
67 |
67 |
68 lift_definition foldi :: "('c \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a :: linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" |
68 lift_definition foldi :: "('c \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a :: linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" |
69 is RBT_Impl.foldi . |
69 is RBT_Impl.foldi . |
70 |
70 |
71 subsection {* Derived operations *} |
71 subsection \<open>Derived operations\<close> |
72 |
72 |
73 definition is_empty :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> bool" where |
73 definition is_empty :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> bool" where |
74 [code]: "is_empty t = (case impl_of t of RBT_Impl.Empty \<Rightarrow> True | _ \<Rightarrow> False)" |
74 [code]: "is_empty t = (case impl_of t of RBT_Impl.Empty \<Rightarrow> True | _ \<Rightarrow> False)" |
75 |
75 |
76 |
76 |
77 subsection {* Abstract lookup properties *} |
77 subsection \<open>Abstract lookup properties\<close> |
78 |
78 |
79 lemma lookup_RBT: |
79 lemma lookup_RBT: |
80 "is_rbt t \<Longrightarrow> lookup (RBT t) = rbt_lookup t" |
80 "is_rbt t \<Longrightarrow> lookup (RBT t) = rbt_lookup t" |
81 by (simp add: lookup_def RBT_inverse) |
81 by (simp add: lookup_def RBT_inverse) |
82 |
82 |
180 |
180 |
181 lemma keys_def_alt: |
181 lemma keys_def_alt: |
182 "keys t = List.map fst (entries t)" |
182 "keys t = List.map fst (entries t)" |
183 by transfer (simp add: RBT_Impl.keys_def) |
183 by transfer (simp add: RBT_Impl.keys_def) |
184 |
184 |
185 subsection {* Quickcheck generators *} |
185 subsection \<open>Quickcheck generators\<close> |
186 |
186 |
187 quickcheck_generator rbt predicate: is_rbt constructors: empty, insert |
187 quickcheck_generator rbt predicate: is_rbt constructors: empty, insert |
188 |
188 |
189 subsection {* Hide implementation details *} |
189 subsection \<open>Hide implementation details\<close> |
190 |
190 |
191 lifting_update rbt.lifting |
191 lifting_update rbt.lifting |
192 lifting_forget rbt.lifting |
192 lifting_forget rbt.lifting |
193 |
193 |
194 hide_const (open) impl_of empty lookup keys entries bulkload delete map fold union insert map_entry foldi |
194 hide_const (open) impl_of empty lookup keys entries bulkload delete map fold union insert map_entry foldi |