13739
|
1 |
(*<*)
|
|
2 |
theory Trie1 = Main:
|
|
3 |
(*>*)
|
|
4 |
|
|
5 |
subsection {* Tries *}
|
|
6 |
|
|
7 |
text {* Section~3.4.4 of \cite{isabelle-tutorial} is a case study
|
|
8 |
about so-called \emph{tries}, a data structure for fast indexing with
|
|
9 |
strings. Read that section.
|
|
10 |
|
|
11 |
The data type of tries over the alphabet type @{typ 'a} und the value
|
|
12 |
type @{typ 'v} is defined as follows: *}
|
|
13 |
|
|
14 |
datatype ('a, 'v) trie = Trie "'v option" "('a * ('a,'v) trie) list";
|
|
15 |
|
|
16 |
text {* A trie consists of an optional value and an association list
|
|
17 |
that maps letters of the alphabet to subtrees. Type @{typ "'a option"} is
|
|
18 |
defined in section~2.5.3 of \cite{isabelle-tutorial}.
|
|
19 |
|
|
20 |
There are also two selector functions @{term value} and @{term alist}: *}
|
|
21 |
|
|
22 |
consts value :: "('a, 'v) trie \<Rightarrow> 'v option"
|
|
23 |
primrec "value (Trie ov al) = ov";
|
|
24 |
|
|
25 |
consts alist :: "('a, 'v) trie \<Rightarrow> ('a * ('a,'v) trie) list";
|
|
26 |
primrec "alist (Trie ov al) = al";
|
|
27 |
|
|
28 |
text {* Furthermore there is a function @{term lookup} on tries
|
|
29 |
defined with the help of the generic search function @{term assoc} on
|
|
30 |
association lists: *}
|
|
31 |
|
|
32 |
consts assoc :: "('key * 'val)list \<Rightarrow> 'key \<Rightarrow> 'val option";
|
|
33 |
primrec "assoc [] x = None"
|
|
34 |
"assoc (p#ps) x =
|
|
35 |
(let (a, b) = p in if a = x then Some b else assoc ps x)";
|
|
36 |
|
|
37 |
consts lookup :: "('a, 'v) trie \<Rightarrow> 'a list \<Rightarrow> 'v option";
|
|
38 |
primrec "lookup t [] = value t"
|
|
39 |
"lookup t (a#as) = (case assoc (alist t) a of
|
|
40 |
None \<Rightarrow> None
|
|
41 |
| Some at \<Rightarrow> lookup at as)";
|
|
42 |
|
|
43 |
text {* Finally, @{term update} updates the value associated with some
|
|
44 |
string with a new value, overwriting the old one: *}
|
|
45 |
|
|
46 |
consts update :: "('a, 'v) trie \<Rightarrow> 'a list \<Rightarrow> 'v \<Rightarrow> ('a, 'v) trie";
|
|
47 |
primrec
|
|
48 |
"update t [] v = Trie (Some v) (alist t)"
|
|
49 |
"update t (a#as) v =
|
|
50 |
(let tt = (case assoc (alist t) a of
|
|
51 |
None \<Rightarrow> Trie None []
|
|
52 |
| Some at \<Rightarrow> at)
|
|
53 |
in Trie (value t) ((a, update tt as v) # alist t))";
|
|
54 |
|
|
55 |
text {* The following theorem tells us that @{term update} behaves as
|
|
56 |
expected: *}
|
|
57 |
|
|
58 |
theorem "\<forall>t v bs. lookup (update t as v) bs =
|
|
59 |
(if as = bs then Some v else lookup t bs)"
|
|
60 |
(*<*)oops(*>*)
|
|
61 |
|
|
62 |
text {* As a warming up exercise, define a function *}
|
|
63 |
|
|
64 |
consts modify :: "('a, 'v) trie \<Rightarrow> 'a list \<Rightarrow> 'v option \<Rightarrow> ('a, 'v) trie"
|
|
65 |
|
|
66 |
text{* for inserting as well as deleting elements from a trie. Show
|
|
67 |
that @{term modify} satisfies a suitably modified version of the
|
|
68 |
correctness theorem for @{term update}. *}
|
|
69 |
|
|
70 |
(*<*)
|
|
71 |
end;
|
|
72 |
(*>*)
|