80404
|
1 |
section "Ternary Tries"
|
|
2 |
|
|
3 |
theory Trie_Ternary
|
|
4 |
imports
|
|
5 |
Tree_Map
|
|
6 |
Trie_Fun
|
|
7 |
begin
|
|
8 |
|
|
9 |
text \<open>An implementation of tries for an arbitrary alphabet \<open>'a\<close> where the mapping
|
|
10 |
from an element of type \<open>'a\<close> to the sub-trie is implemented by an (unbalanced) binary search tree.
|
|
11 |
In principle, other search trees (e.g. red-black trees) work just as well,
|
|
12 |
with some small adjustments (Exercise!).
|
|
13 |
|
|
14 |
This is an implementation of the ``ternary search trees'' by Bentley and Sedgewick
|
|
15 |
[SODA 1997, Dr. Dobbs 1998]. The name derives from the fact that a node in the BST can now
|
|
16 |
be drawn to have 3 children, where the middle child is the sub-trie that the node maps
|
|
17 |
its key to. Hence the name \<open>trie3\<close>.
|
|
18 |
|
|
19 |
Example from @{url "https://en.wikipedia.org/wiki/Ternary_search_tree#Description"}:
|
|
20 |
|
|
21 |
c
|
|
22 |
/ | \
|
|
23 |
a u h
|
|
24 |
| | | \
|
|
25 |
t. t e. u
|
|
26 |
/ / | / |
|
|
27 |
s. p. e. i. s.
|
|
28 |
|
|
29 |
Characters with a dot are final.
|
|
30 |
Thus the tree represents the set of strings "cute","cup","at","as","he","us" and "i".
|
|
31 |
\<close>
|
|
32 |
|
|
33 |
datatype 'a trie3 = Nd3 bool "('a * 'a trie3) tree"
|
|
34 |
|
|
35 |
text \<open>The development below works almost verbatim for any search tree implementation, eg \<open>RBT_Map\<close>,
|
|
36 |
and not just \<open>Tree_Map\<close>, except for the termination lemma \<open>lookup_size\<close>.\<close>
|
|
37 |
|
|
38 |
term size_tree
|
|
39 |
lemma lookup_size[termination_simp]:
|
|
40 |
fixes t :: "('a::linorder * 'a trie3) tree"
|
|
41 |
shows "lookup t a = Some b \<Longrightarrow> size b < Suc (size_tree (\<lambda>ab. Suc (size (snd( ab)))) t)"
|
|
42 |
apply(induction t a rule: lookup.induct)
|
|
43 |
apply(auto split: if_splits)
|
|
44 |
done
|
|
45 |
|
|
46 |
|
|
47 |
definition empty3 :: "'a trie3" where
|
|
48 |
[simp]: "empty3 = Nd3 False Leaf"
|
|
49 |
|
|
50 |
fun isin3 :: "('a::linorder) trie3 \<Rightarrow> 'a list \<Rightarrow> bool" where
|
|
51 |
"isin3 (Nd3 b m) [] = b" |
|
|
52 |
"isin3 (Nd3 b m) (x # xs) = (case lookup m x of None \<Rightarrow> False | Some t \<Rightarrow> isin3 t xs)"
|
|
53 |
|
|
54 |
fun insert3 :: "('a::linorder) list \<Rightarrow> 'a trie3 \<Rightarrow> 'a trie3" where
|
|
55 |
"insert3 [] (Nd3 b m) = Nd3 True m" |
|
|
56 |
"insert3 (x#xs) (Nd3 b m) =
|
|
57 |
Nd3 b (update x (insert3 xs (case lookup m x of None \<Rightarrow> empty3 | Some t \<Rightarrow> t)) m)"
|
|
58 |
|
|
59 |
fun delete3 :: "('a::linorder) list \<Rightarrow> 'a trie3 \<Rightarrow> 'a trie3" where
|
|
60 |
"delete3 [] (Nd3 b m) = Nd3 False m" |
|
|
61 |
"delete3 (x#xs) (Nd3 b m) = Nd3 b
|
|
62 |
(case lookup m x of
|
|
63 |
None \<Rightarrow> m |
|
|
64 |
Some t \<Rightarrow> update x (delete3 xs t) m)"
|
|
65 |
|
|
66 |
|
|
67 |
subsection "Correctness"
|
|
68 |
|
|
69 |
text \<open>Proof by stepwise refinement. First abs3tract to type @{typ "'a trie"}.\<close>
|
|
70 |
|
|
71 |
fun abs3 :: "'a::linorder trie3 \<Rightarrow> 'a trie" where
|
|
72 |
"abs3 (Nd3 b t) = Nd b (\<lambda>a. map_option abs3 (lookup t a))"
|
|
73 |
|
|
74 |
fun invar3 :: "('a::linorder)trie3 \<Rightarrow> bool" where
|
|
75 |
"invar3 (Nd3 b m) = (M.invar m \<and> (\<forall>a t. lookup m a = Some t \<longrightarrow> invar3 t))"
|
|
76 |
|
|
77 |
lemma isin_abs3: "isin3 t xs = isin (abs3 t) xs"
|
|
78 |
apply(induction t xs rule: isin3.induct)
|
|
79 |
apply(auto split: option.split)
|
|
80 |
done
|
|
81 |
|
|
82 |
lemma abs3_insert3: "invar3 t \<Longrightarrow> abs3(insert3 xs t) = insert xs (abs3 t)"
|
|
83 |
apply(induction xs t rule: insert3.induct)
|
|
84 |
apply(auto simp: M.map_specs Tree_Set.empty_def[symmetric] split: option.split)
|
|
85 |
done
|
|
86 |
|
|
87 |
lemma abs3_delete3: "invar3 t \<Longrightarrow> abs3(delete3 xs t) = delete xs (abs3 t)"
|
|
88 |
apply(induction xs t rule: delete3.induct)
|
|
89 |
apply(auto simp: M.map_specs split: option.split)
|
|
90 |
done
|
|
91 |
|
|
92 |
lemma invar3_insert3: "invar3 t \<Longrightarrow> invar3 (insert3 xs t)"
|
|
93 |
apply(induction xs t rule: insert3.induct)
|
|
94 |
apply(auto simp: M.map_specs Tree_Set.empty_def[symmetric] split: option.split)
|
|
95 |
done
|
|
96 |
|
|
97 |
lemma invar3_delete3: "invar3 t \<Longrightarrow> invar3 (delete3 xs t)"
|
|
98 |
apply(induction xs t rule: delete3.induct)
|
|
99 |
apply(auto simp: M.map_specs split: option.split)
|
|
100 |
done
|
|
101 |
|
|
102 |
text \<open>Overall correctness w.r.t. the \<open>Set\<close> ADT:\<close>
|
|
103 |
|
|
104 |
interpretation S2: Set
|
|
105 |
where empty = empty3 and isin = isin3 and insert = insert3 and delete = delete3
|
|
106 |
and set = "set o abs3" and invar = invar3
|
|
107 |
proof (standard, goal_cases)
|
|
108 |
case 1 show ?case by (simp add: isin_case split: list.split)
|
|
109 |
next
|
|
110 |
case 2 thus ?case by (simp add: isin_abs3)
|
|
111 |
next
|
|
112 |
case 3 thus ?case by (simp add: set_insert abs3_insert3 del: set_def)
|
|
113 |
next
|
|
114 |
case 4 thus ?case by (simp add: set_delete abs3_delete3 del: set_def)
|
|
115 |
next
|
|
116 |
case 5 thus ?case by (simp add: M.map_specs Tree_Set.empty_def[symmetric])
|
|
117 |
next
|
|
118 |
case 6 thus ?case by (simp add: invar3_insert3)
|
|
119 |
next
|
|
120 |
case 7 thus ?case by (simp add: invar3_delete3)
|
|
121 |
qed
|
|
122 |
|
|
123 |
end
|