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 |
|
|
13 |
fun isin :: "'a trie \<Rightarrow> 'a list \<Rightarrow> bool" where
|
|
14 |
"isin (Nd b m) [] = b" |
|
|
15 |
"isin (Nd b m) (k # xs) = (case m k of None \<Rightarrow> False | Some t \<Rightarrow> isin t xs)"
|
|
16 |
|
|
17 |
fun insert :: "('a::linorder) list \<Rightarrow> 'a trie \<Rightarrow> 'a trie" where
|
|
18 |
"insert [] (Nd b m) = Nd True m" |
|
|
19 |
"insert (x#xs) (Nd b m) =
|
70262
|
20 |
Nd b (m(x := Some(insert xs (case m x of None \<Rightarrow> Nd False (\<lambda>_. None) | Some t \<Rightarrow> t))))"
|
70250
|
21 |
|
|
22 |
fun delete :: "('a::linorder) list \<Rightarrow> 'a trie \<Rightarrow> 'a trie" where
|
|
23 |
"delete [] (Nd b m) = Nd False m" |
|
|
24 |
"delete (x#xs) (Nd b m) = Nd b
|
|
25 |
(case m x of
|
|
26 |
None \<Rightarrow> m |
|
|
27 |
Some t \<Rightarrow> m(x := Some(delete xs t)))"
|
|
28 |
|
70262
|
29 |
text \<open>The actual definition of \<open>set\<close> is a bit cryptic but canonical, to enable
|
|
30 |
primrec to prove termination:\<close>
|
|
31 |
|
70250
|
32 |
primrec set :: "'a trie \<Rightarrow> 'a list set" where
|
|
33 |
"set (Nd b m) = (if b then {[]} else {}) \<union>
|
|
34 |
(\<Union>a. case (map_option set o m) a of None \<Rightarrow> {} | Some t \<Rightarrow> (#) a ` t)"
|
|
35 |
|
70262
|
36 |
text \<open>This is the more human-readable version:\<close>
|
|
37 |
|
70250
|
38 |
lemma set_Nd:
|
|
39 |
"set (Nd b m) =
|
|
40 |
(if b then {[]} else {}) \<union>
|
|
41 |
(\<Union>a. case m a of None \<Rightarrow> {} | Some t \<Rightarrow> (#) a ` set t)"
|
|
42 |
by (auto simp: split: option.splits)
|
|
43 |
|
|
44 |
lemma isin_set: "isin t xs = (xs \<in> set t)"
|
|
45 |
apply(induction t xs rule: isin.induct)
|
|
46 |
apply (auto split: option.split)
|
|
47 |
done
|
|
48 |
|
|
49 |
lemma set_insert: "set (insert xs t) = set t \<union> {xs}"
|
|
50 |
proof(induction xs t rule: insert.induct)
|
|
51 |
case 1 thus ?case by simp
|
|
52 |
next
|
70262
|
53 |
case 2
|
70250
|
54 |
thus ?case
|
|
55 |
apply(simp)
|
|
56 |
apply(subst set_eq_iff)
|
|
57 |
apply(auto split!: if_splits option.splits)
|
|
58 |
apply fastforce
|
|
59 |
by (metis imageI option.sel)
|
|
60 |
qed
|
|
61 |
|
|
62 |
lemma set_delete: "set (delete xs t) = set t - {xs}"
|
|
63 |
proof(induction xs t rule: delete.induct)
|
70262
|
64 |
case 1 thus ?case by (force split: option.splits)
|
70250
|
65 |
next
|
70262
|
66 |
case 2
|
70250
|
67 |
thus ?case
|
|
68 |
apply (auto simp add: image_iff split!: if_splits option.splits)
|
|
69 |
apply blast
|
|
70 |
apply (metis insertE insertI2 insert_Diff_single option.inject)
|
|
71 |
apply blast
|
|
72 |
by (metis insertE insertI2 insert_Diff_single option.inject)
|
|
73 |
qed
|
|
74 |
|
|
75 |
interpretation S: Set
|
70262
|
76 |
where empty = "Nd False (\<lambda>_. None)" and isin = isin and insert = insert and delete = delete
|
70250
|
77 |
and set = set and invar = "\<lambda>_. True"
|
|
78 |
proof (standard, goal_cases)
|
|
79 |
case 1 show ?case by (simp)
|
|
80 |
next
|
|
81 |
case 2 thus ?case by(simp add: isin_set)
|
|
82 |
next
|
|
83 |
case 3 thus ?case by(simp add: set_insert)
|
|
84 |
next
|
|
85 |
case 4 thus ?case by(simp add: set_delete)
|
|
86 |
qed (rule TrueI)+
|
|
87 |
|
|
88 |
end
|