70250
|
1 |
section \<open>Tries via Functions\<close>
|
|
2 |
|
|
3 |
theory Trie_Fun
|
|
4 |
imports
|
|
5 |
Set_Specs
|
|
6 |
begin
|
|
7 |
|
|
8 |
text \<open>A trie where each node maps a key to sub-tries via a function.
|
|
9 |
Nice abstract model. Not efficient because of the function space.\<close>
|
|
10 |
|
70262
|
11 |
datatype 'a trie = Nd bool "'a \<Rightarrow> 'a trie option"
|
70250
|
12 |
|
70266
|
13 |
definition empty :: "'a trie" where
|
|
14 |
[simp]: "empty = Nd False (\<lambda>_. None)"
|
|
15 |
|
70250
|
16 |
fun isin :: "'a trie \<Rightarrow> 'a list \<Rightarrow> bool" where
|
|
17 |
"isin (Nd b m) [] = b" |
|
|
18 |
"isin (Nd b m) (k # xs) = (case m k of None \<Rightarrow> False | Some t \<Rightarrow> isin t xs)"
|
|
19 |
|
72912
|
20 |
fun insert :: "'a list \<Rightarrow> 'a trie \<Rightarrow> 'a trie" where
|
70250
|
21 |
"insert [] (Nd b m) = Nd True m" |
|
|
22 |
"insert (x#xs) (Nd b m) =
|
72979
|
23 |
(let s = (case m x of None \<Rightarrow> empty | Some t \<Rightarrow> t) in Nd b (m(x := Some(insert xs s))))"
|
70250
|
24 |
|
72912
|
25 |
fun delete :: "'a list \<Rightarrow> 'a trie \<Rightarrow> 'a trie" where
|
70250
|
26 |
"delete [] (Nd b m) = Nd False m" |
|
|
27 |
"delete (x#xs) (Nd b m) = Nd b
|
|
28 |
(case m x of
|
|
29 |
None \<Rightarrow> m |
|
|
30 |
Some t \<Rightarrow> m(x := Some(delete xs t)))"
|
|
31 |
|
72979
|
32 |
text \<open>Use (a tuned version of) @{const isin} as an abstraction function:\<close>
|
70250
|
33 |
|
72979
|
34 |
lemma isin_case: "isin (Nd b m) xs =
|
|
35 |
(case xs of
|
|
36 |
[] \<Rightarrow> b |
|
|
37 |
x # ys \<Rightarrow> (case m x of None \<Rightarrow> False | Some t \<Rightarrow> isin t ys))"
|
|
38 |
by(cases xs)auto
|
70262
|
39 |
|
72979
|
40 |
definition set :: "'a trie \<Rightarrow> 'a list set" where
|
|
41 |
[simp]: "set t = {xs. isin t xs}"
|
70250
|
42 |
|
|
43 |
lemma isin_set: "isin t xs = (xs \<in> set t)"
|
72979
|
44 |
by simp
|
70250
|
45 |
|
|
46 |
lemma set_insert: "set (insert xs t) = set t \<union> {xs}"
|
72979
|
47 |
by (induction xs t rule: insert.induct)
|
|
48 |
(auto simp: isin_case split!: if_splits option.splits list.splits)
|
70250
|
49 |
|
|
50 |
lemma set_delete: "set (delete xs t) = set t - {xs}"
|
72979
|
51 |
by (induction xs t rule: delete.induct)
|
|
52 |
(auto simp: isin_case split!: if_splits option.splits list.splits)
|
70250
|
53 |
|
|
54 |
interpretation S: Set
|
70266
|
55 |
where empty = empty and isin = isin and insert = insert and delete = delete
|
70250
|
56 |
and set = set and invar = "\<lambda>_. True"
|
|
57 |
proof (standard, goal_cases)
|
72979
|
58 |
case 1 show ?case by (simp add: isin_case split: list.split)
|
70250
|
59 |
next
|
72979
|
60 |
case 2 show ?case by(rule isin_set)
|
70250
|
61 |
next
|
72979
|
62 |
case 3 show ?case by(rule set_insert)
|
70250
|
63 |
next
|
72979
|
64 |
case 4 show ?case by(rule set_delete)
|
70250
|
65 |
qed (rule TrueI)+
|
|
66 |
|
|
67 |
end
|