8745
|
1 |
(*<*)
|
|
2 |
theory Trie = Main:;
|
|
3 |
(*>*)
|
|
4 |
text{*
|
|
5 |
To minimize running time, each node of a trie should contain an array that maps
|
|
6 |
letters to subtries. We have chosen a (sometimes) more space efficient
|
|
7 |
representation where the subtries are held in an association list, i.e.\ a
|
|
8 |
list of (letter,trie) pairs. Abstracting over the alphabet \isa{'a} and the
|
|
9 |
values \isa{'v} we define a trie as follows:
|
9458
|
10 |
*};
|
8745
|
11 |
|
|
12 |
datatype ('a,'v)trie = Trie "'v option" "('a * ('a,'v)trie)list";
|
|
13 |
|
|
14 |
text{*\noindent
|
|
15 |
The first component is the optional value, the second component the
|
|
16 |
association list of subtries. This is an example of nested recursion involving products,
|
|
17 |
which is fine because products are datatypes as well.
|
|
18 |
We define two selector functions:
|
9458
|
19 |
*};
|
8745
|
20 |
|
|
21 |
consts value :: "('a,'v)trie \\<Rightarrow> 'v option"
|
|
22 |
alist :: "('a,'v)trie \\<Rightarrow> ('a * ('a,'v)trie)list";
|
|
23 |
primrec "value(Trie ov al) = ov";
|
|
24 |
primrec "alist(Trie ov al) = al";
|
|
25 |
|
|
26 |
text{*\noindent
|
|
27 |
Association lists come with a generic lookup function:
|
9458
|
28 |
*};
|
8745
|
29 |
|
|
30 |
consts assoc :: "('key * 'val)list \\<Rightarrow> 'key \\<Rightarrow> 'val option";
|
|
31 |
primrec "assoc [] x = None"
|
|
32 |
"assoc (p#ps) x =
|
|
33 |
(let (a,b) = p in if a=x then Some b else assoc ps x)";
|
|
34 |
|
|
35 |
text{*
|
|
36 |
Now we can define the lookup function for tries. It descends into the trie
|
|
37 |
examining the letters of the search string one by one. As
|
|
38 |
recursion on lists is simpler than on tries, let us express this as primitive
|
|
39 |
recursion on the search string argument:
|
9458
|
40 |
*};
|
8745
|
41 |
|
|
42 |
consts lookup :: "('a,'v)trie \\<Rightarrow> 'a list \\<Rightarrow> 'v option";
|
|
43 |
primrec "lookup t [] = value t"
|
|
44 |
"lookup t (a#as) = (case assoc (alist t) a of
|
|
45 |
None \\<Rightarrow> None
|
|
46 |
| Some at \\<Rightarrow> lookup at as)";
|
|
47 |
|
|
48 |
text{*
|
|
49 |
As a first simple property we prove that looking up a string in the empty
|
|
50 |
trie \isa{Trie~None~[]} always returns \isa{None}. The proof merely
|
|
51 |
distinguishes the two cases whether the search string is empty or not:
|
9458
|
52 |
*};
|
8745
|
53 |
|
|
54 |
lemma [simp]: "lookup (Trie None []) as = None";
|
9689
|
55 |
by(case_tac as, simp_all);
|
8745
|
56 |
|
|
57 |
text{*
|
|
58 |
Things begin to get interesting with the definition of an update function
|
|
59 |
that adds a new (string,value) pair to a trie, overwriting the old value
|
|
60 |
associated with that string:
|
9458
|
61 |
*};
|
8745
|
62 |
|
|
63 |
consts update :: "('a,'v)trie \\<Rightarrow> 'a list \\<Rightarrow> 'v \\<Rightarrow> ('a,'v)trie";
|
|
64 |
primrec
|
|
65 |
"update t [] v = Trie (Some v) (alist t)"
|
|
66 |
"update t (a#as) v =
|
|
67 |
(let tt = (case assoc (alist t) a of
|
|
68 |
None \\<Rightarrow> Trie None [] | Some at \\<Rightarrow> at)
|
|
69 |
in Trie (value t) ((a,update tt as v)#alist t))";
|
|
70 |
|
|
71 |
text{*\noindent
|
|
72 |
The base case is obvious. In the recursive case the subtrie
|
|
73 |
\isa{tt} associated with the first letter \isa{a} is extracted,
|
|
74 |
recursively updated, and then placed in front of the association list.
|
|
75 |
The old subtrie associated with \isa{a} is still in the association list
|
|
76 |
but no longer accessible via \isa{assoc}. Clearly, there is room here for
|
|
77 |
optimizations!
|
|
78 |
|
|
79 |
Before we start on any proofs about \isa{update} we tell the simplifier to
|
|
80 |
expand all \isa{let}s and to split all \isa{case}-constructs over
|
|
81 |
options:
|
9458
|
82 |
*};
|
8745
|
83 |
|
9541
|
84 |
lemmas [simp] = Let_def;
|
|
85 |
lemmas [split] = option.split;
|
8745
|
86 |
|
|
87 |
text{*\noindent
|
|
88 |
The reason becomes clear when looking (probably after a failed proof
|
|
89 |
attempt) at the body of \isa{update}: it contains both
|
|
90 |
\isa{let} and a case distinction over type \isa{option}.
|
|
91 |
|
|
92 |
Our main goal is to prove the correct interaction of \isa{update} and
|
|
93 |
\isa{lookup}:
|
9458
|
94 |
*};
|
8745
|
95 |
|
|
96 |
theorem "\\<forall>t v bs. lookup (update t as v) bs =
|
|
97 |
(if as=bs then Some v else lookup t bs)";
|
|
98 |
|
|
99 |
txt{*\noindent
|
|
100 |
Our plan is to induct on \isa{as}; hence the remaining variables are
|
|
101 |
quantified. From the definitions it is clear that induction on either
|
|
102 |
\isa{as} or \isa{bs} is required. The choice of \isa{as} is merely
|
|
103 |
guided by the intuition that simplification of \isa{lookup} might be easier
|
|
104 |
if \isa{update} has already been simplified, which can only happen if
|
|
105 |
\isa{as} is instantiated.
|
|
106 |
The start of the proof is completely conventional:
|
9458
|
107 |
*};
|
8771
|
108 |
apply(induct_tac as, auto);
|
8745
|
109 |
|
|
110 |
txt{*\noindent
|
|
111 |
Unfortunately, this time we are left with three intimidating looking subgoals:
|
9723
|
112 |
\begin{isabelle}
|
8745
|
113 |
~1.~\dots~{\isasymLongrightarrow}~lookup~\dots~bs~=~lookup~t~bs\isanewline
|
|
114 |
~2.~\dots~{\isasymLongrightarrow}~lookup~\dots~bs~=~lookup~t~bs\isanewline
|
|
115 |
~3.~\dots~{\isasymLongrightarrow}~lookup~\dots~bs~=~lookup~t~bs%
|
9723
|
116 |
\end{isabelle}
|
8745
|
117 |
Clearly, if we want to make headway we have to instantiate \isa{bs} as
|
|
118 |
well now. It turns out that instead of induction, case distinction
|
|
119 |
suffices:
|
9458
|
120 |
*};
|
9541
|
121 |
by(case_tac[!] bs, auto);
|
8745
|
122 |
|
|
123 |
text{*\noindent
|
9541
|
124 |
All methods ending in \isa{tac} take an optional first argument that
|
|
125 |
specifies the range of subgoals they are applied to, where \isa{[!]} means all
|
|
126 |
subgoals, i.e.\ \isa{[1-3]} in our case. Individual subgoal numbers,
|
|
127 |
e.g. \isa{[2]} are also allowed.
|
8745
|
128 |
|
|
129 |
This proof may look surprisingly straightforward. However, note that this
|
|
130 |
comes at a cost: the proof script is unreadable because the
|
|
131 |
intermediate proof states are invisible, and we rely on the (possibly
|
9689
|
132 |
brittle) magic of \isa{auto} (\isa{simp\_all} will not do---try it) to split the subgoals
|
|
133 |
of the induction up in such a way that case distinction on \isa{bs} makes sense and
|
8745
|
134 |
solves the proof. Part~\ref{Isar} shows you how to write readable and stable
|
|
135 |
proofs.
|
9458
|
136 |
*};
|
8745
|
137 |
|
|
138 |
(*<*)
|
9458
|
139 |
end;
|
8745
|
140 |
(*>*)
|