35617
|
1 |
(* Author: Florian Haftmann, TU Muenchen *)
|
|
2 |
|
|
3 |
header {* Tables: finite mappings implemented by red-black trees *}
|
|
4 |
|
|
5 |
theory Table
|
|
6 |
imports Main RBT
|
|
7 |
begin
|
|
8 |
|
|
9 |
subsection {* Type definition *}
|
|
10 |
|
|
11 |
typedef (open) ('a, 'b) table = "{t :: ('a\<Colon>linorder, 'b) rbt. is_rbt t}"
|
|
12 |
morphisms tree_of Table
|
|
13 |
proof -
|
|
14 |
have "RBT.Empty \<in> ?table" by simp
|
|
15 |
then show ?thesis ..
|
|
16 |
qed
|
|
17 |
|
|
18 |
lemma is_rbt_tree_of [simp, intro]:
|
|
19 |
"is_rbt (tree_of t)"
|
|
20 |
using tree_of [of t] by simp
|
|
21 |
|
|
22 |
lemma table_eq:
|
|
23 |
"t1 = t2 \<longleftrightarrow> tree_of t1 = tree_of t2"
|
|
24 |
by (simp add: tree_of_inject)
|
|
25 |
|
|
26 |
code_abstype Table tree_of
|
|
27 |
by (simp add: tree_of_inverse)
|
|
28 |
|
|
29 |
|
|
30 |
subsection {* Primitive operations *}
|
|
31 |
|
|
32 |
definition lookup :: "('a\<Colon>linorder, 'b) table \<Rightarrow> 'a \<rightharpoonup> 'b" where
|
|
33 |
[code]: "lookup t = RBT.lookup (tree_of t)"
|
|
34 |
|
|
35 |
definition empty :: "('a\<Colon>linorder, 'b) table" where
|
|
36 |
"empty = Table RBT.Empty"
|
|
37 |
|
|
38 |
lemma tree_of_empty [code abstract]:
|
|
39 |
"tree_of empty = RBT.Empty"
|
|
40 |
by (simp add: empty_def Table_inverse)
|
|
41 |
|
|
42 |
definition update :: "'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) table \<Rightarrow> ('a, 'b) table" where
|
|
43 |
"update k v t = Table (RBT.insert k v (tree_of t))"
|
|
44 |
|
|
45 |
lemma tree_of_update [code abstract]:
|
|
46 |
"tree_of (update k v t) = RBT.insert k v (tree_of t)"
|
|
47 |
by (simp add: update_def Table_inverse)
|
|
48 |
|
|
49 |
definition delete :: "'a\<Colon>linorder \<Rightarrow> ('a, 'b) table \<Rightarrow> ('a, 'b) table" where
|
|
50 |
"delete k t = Table (RBT.delete k (tree_of t))"
|
|
51 |
|
|
52 |
lemma tree_of_delete [code abstract]:
|
|
53 |
"tree_of (delete k t) = RBT.delete k (tree_of t)"
|
|
54 |
by (simp add: delete_def Table_inverse)
|
|
55 |
|
|
56 |
definition entries :: "('a\<Colon>linorder, 'b) table \<Rightarrow> ('a \<times> 'b) list" where
|
|
57 |
[code]: "entries t = RBT.entries (tree_of t)"
|
|
58 |
|
|
59 |
definition bulkload :: "('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) table" where
|
|
60 |
"bulkload xs = Table (RBT.bulkload xs)"
|
|
61 |
|
|
62 |
lemma tree_of_bulkload [code abstract]:
|
|
63 |
"tree_of (bulkload xs) = RBT.bulkload xs"
|
|
64 |
by (simp add: bulkload_def Table_inverse)
|
|
65 |
|
|
66 |
definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) table \<Rightarrow> ('a, 'b) table" where
|
|
67 |
"map_entry k f t = Table (RBT.map_entry k f (tree_of t))"
|
|
68 |
|
|
69 |
lemma tree_of_map_entry [code abstract]:
|
|
70 |
"tree_of (map_entry k f t) = RBT.map_entry k f (tree_of t)"
|
|
71 |
by (simp add: map_entry_def Table_inverse)
|
|
72 |
|
|
73 |
definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) table \<Rightarrow> ('a, 'b) table" where
|
|
74 |
"map f t = Table (RBT.map f (tree_of t))"
|
|
75 |
|
|
76 |
lemma tree_of_map [code abstract]:
|
|
77 |
"tree_of (map f t) = RBT.map f (tree_of t)"
|
|
78 |
by (simp add: map_def Table_inverse)
|
|
79 |
|
|
80 |
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) table \<Rightarrow> 'c \<Rightarrow> 'c" where
|
|
81 |
[code]: "fold f t = RBT.fold f (tree_of t)"
|
|
82 |
|
|
83 |
|
|
84 |
subsection {* Derived operations *}
|
|
85 |
|
|
86 |
definition is_empty :: "('a\<Colon>linorder, 'b) table \<Rightarrow> bool" where
|
|
87 |
[code]: "is_empty t = (case tree_of t of RBT.Empty \<Rightarrow> True | _ \<Rightarrow> False)"
|
|
88 |
|
|
89 |
|
|
90 |
subsection {* Abstract lookup properties *}
|
|
91 |
|
|
92 |
lemma lookup_Table:
|
|
93 |
"is_rbt t \<Longrightarrow> lookup (Table t) = RBT.lookup t"
|
|
94 |
by (simp add: lookup_def Table_inverse)
|
|
95 |
|
|
96 |
lemma lookup_tree_of:
|
|
97 |
"RBT.lookup (tree_of t) = lookup t"
|
|
98 |
by (simp add: lookup_def)
|
|
99 |
|
|
100 |
lemma entries_tree_of:
|
|
101 |
"RBT.entries (tree_of t) = entries t"
|
|
102 |
by (simp add: entries_def)
|
|
103 |
|
|
104 |
lemma lookup_empty [simp]:
|
|
105 |
"lookup empty = Map.empty"
|
|
106 |
by (simp add: empty_def lookup_Table expand_fun_eq)
|
|
107 |
|
|
108 |
lemma lookup_update [simp]:
|
|
109 |
"lookup (update k v t) = (lookup t)(k \<mapsto> v)"
|
|
110 |
by (simp add: update_def lookup_Table lookup_insert lookup_tree_of)
|
|
111 |
|
|
112 |
lemma lookup_delete [simp]:
|
|
113 |
"lookup (delete k t) = (lookup t)(k := None)"
|
|
114 |
by (simp add: delete_def lookup_Table lookup_delete lookup_tree_of restrict_complement_singleton_eq)
|
|
115 |
|
|
116 |
lemma map_of_entries [simp]:
|
|
117 |
"map_of (entries t) = lookup t"
|
|
118 |
by (simp add: entries_def map_of_entries lookup_tree_of)
|
|
119 |
|
|
120 |
lemma lookup_bulkload [simp]:
|
|
121 |
"lookup (bulkload xs) = map_of xs"
|
|
122 |
by (simp add: bulkload_def lookup_Table lookup_bulkload)
|
|
123 |
|
|
124 |
lemma lookup_map_entry [simp]:
|
|
125 |
"lookup (map_entry k f t) = (lookup t)(k := Option.map f (lookup t k))"
|
|
126 |
by (simp add: map_entry_def lookup_Table lookup_map_entry lookup_tree_of)
|
|
127 |
|
|
128 |
lemma lookup_map [simp]:
|
|
129 |
"lookup (map f t) k = Option.map (f k) (lookup t k)"
|
|
130 |
by (simp add: map_def lookup_Table lookup_map lookup_tree_of)
|
|
131 |
|
|
132 |
lemma fold_fold:
|
|
133 |
"fold f t = (\<lambda>s. foldl (\<lambda>s (k, v). f k v s) s (entries t))"
|
|
134 |
by (simp add: fold_def expand_fun_eq RBT.fold_def entries_tree_of)
|
|
135 |
|
|
136 |
hide (open) const tree_of lookup empty update delete
|
|
137 |
entries bulkload map_entry map fold
|
|
138 |
|
|
139 |
end
|