9722
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
9924
|
3 |
\def\isabellecontext{Trie}%
|
11866
|
4 |
\isamarkupfalse%
|
8749
|
5 |
%
|
|
6 |
\begin{isamarkuptext}%
|
|
7 |
To minimize running time, each node of a trie should contain an array that maps
|
10795
|
8 |
letters to subtries. We have chosen a
|
8749
|
9 |
representation where the subtries are held in an association list, i.e.\ a
|
9792
|
10 |
list of (letter,trie) pairs. Abstracting over the alphabet \isa{{\isacharprime}a} and the
|
|
11 |
values \isa{{\isacharprime}v} we define a trie as follows:%
|
8749
|
12 |
\end{isamarkuptext}%
|
11866
|
13 |
\isamarkuptrue%
|
|
14 |
\isacommand{datatype}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}trie\ {\isacharequal}\ Trie\ \ {\isachardoublequote}{\isacharprime}v\ option{\isachardoublequote}\ \ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isacharasterisk}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}trie{\isacharparenright}list{\isachardoublequote}\isamarkupfalse%
|
|
15 |
%
|
8749
|
16 |
\begin{isamarkuptext}%
|
|
17 |
\noindent
|
11480
|
18 |
\index{datatypes!and nested recursion}%
|
8749
|
19 |
The first component is the optional value, the second component the
|
|
20 |
association list of subtries. This is an example of nested recursion involving products,
|
|
21 |
which is fine because products are datatypes as well.
|
|
22 |
We define two selector functions:%
|
|
23 |
\end{isamarkuptext}%
|
11866
|
24 |
\isamarkuptrue%
|
9674
|
25 |
\isacommand{consts}\ value\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}trie\ {\isasymRightarrow}\ {\isacharprime}v\ option{\isachardoublequote}\isanewline
|
|
26 |
\ \ \ \ \ \ \ alist\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}trie\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isacharasterisk}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}trie{\isacharparenright}list{\isachardoublequote}\isanewline
|
11866
|
27 |
\isamarkupfalse%
|
9674
|
28 |
\isacommand{primrec}\ {\isachardoublequote}value{\isacharparenleft}Trie\ ov\ al{\isacharparenright}\ {\isacharequal}\ ov{\isachardoublequote}\isanewline
|
11866
|
29 |
\isamarkupfalse%
|
|
30 |
\isacommand{primrec}\ {\isachardoublequote}alist{\isacharparenleft}Trie\ ov\ al{\isacharparenright}\ {\isacharequal}\ al{\isachardoublequote}\isamarkupfalse%
|
|
31 |
%
|
8749
|
32 |
\begin{isamarkuptext}%
|
|
33 |
\noindent
|
11309
|
34 |
Association lists come with a generic lookup function. Its result
|
|
35 |
involves type \isa{option} because a lookup can fail:%
|
8749
|
36 |
\end{isamarkuptext}%
|
11866
|
37 |
\isamarkuptrue%
|
9674
|
38 |
\isacommand{consts}\ \ \ assoc\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}key\ {\isacharasterisk}\ {\isacharprime}val{\isacharparenright}list\ {\isasymRightarrow}\ {\isacharprime}key\ {\isasymRightarrow}\ {\isacharprime}val\ option{\isachardoublequote}\isanewline
|
11866
|
39 |
\isamarkupfalse%
|
9674
|
40 |
\isacommand{primrec}\ {\isachardoublequote}assoc\ {\isacharbrackleft}{\isacharbrackright}\ x\ {\isacharequal}\ None{\isachardoublequote}\isanewline
|
|
41 |
\ \ \ \ \ \ \ \ {\isachardoublequote}assoc\ {\isacharparenleft}p{\isacharhash}ps{\isacharparenright}\ x\ {\isacharequal}\isanewline
|
11866
|
42 |
\ \ \ \ \ \ \ \ \ \ \ {\isacharparenleft}let\ {\isacharparenleft}a{\isacharcomma}b{\isacharparenright}\ {\isacharequal}\ p\ in\ if\ a{\isacharequal}x\ then\ Some\ b\ else\ assoc\ ps\ x{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
43 |
%
|
8749
|
44 |
\begin{isamarkuptext}%
|
|
45 |
Now we can define the lookup function for tries. It descends into the trie
|
|
46 |
examining the letters of the search string one by one. As
|
|
47 |
recursion on lists is simpler than on tries, let us express this as primitive
|
|
48 |
recursion on the search string argument:%
|
|
49 |
\end{isamarkuptext}%
|
11866
|
50 |
\isamarkuptrue%
|
9674
|
51 |
\isacommand{consts}\ \ \ lookup\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}trie\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}v\ option{\isachardoublequote}\isanewline
|
11866
|
52 |
\isamarkupfalse%
|
9674
|
53 |
\isacommand{primrec}\ {\isachardoublequote}lookup\ t\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ value\ t{\isachardoublequote}\isanewline
|
|
54 |
\ \ \ \ \ \ \ \ {\isachardoublequote}lookup\ t\ {\isacharparenleft}a{\isacharhash}as{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}case\ assoc\ {\isacharparenleft}alist\ t{\isacharparenright}\ a\ of\isanewline
|
9521
|
55 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ None\ {\isasymRightarrow}\ None\isanewline
|
11866
|
56 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Some\ at\ {\isasymRightarrow}\ lookup\ at\ as{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
57 |
%
|
8749
|
58 |
\begin{isamarkuptext}%
|
|
59 |
As a first simple property we prove that looking up a string in the empty
|
9792
|
60 |
trie \isa{Trie\ None\ {\isacharbrackleft}{\isacharbrackright}} always returns \isa{None}. The proof merely
|
8749
|
61 |
distinguishes the two cases whether the search string is empty or not:%
|
|
62 |
\end{isamarkuptext}%
|
11866
|
63 |
\isamarkuptrue%
|
9674
|
64 |
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}lookup\ {\isacharparenleft}Trie\ None\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ as\ {\isacharequal}\ None{\isachardoublequote}\isanewline
|
11866
|
65 |
\isamarkupfalse%
|
10171
|
66 |
\isacommand{apply}{\isacharparenleft}case{\isacharunderscore}tac\ as{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\isanewline
|
11866
|
67 |
\isamarkupfalse%
|
|
68 |
\isacommand{done}\isamarkupfalse%
|
|
69 |
%
|
8749
|
70 |
\begin{isamarkuptext}%
|
|
71 |
Things begin to get interesting with the definition of an update function
|
12328
|
72 |
that adds a new (string, value) pair to a trie, overwriting the old value
|
8749
|
73 |
associated with that string:%
|
|
74 |
\end{isamarkuptext}%
|
11866
|
75 |
\isamarkuptrue%
|
9674
|
76 |
\isacommand{consts}\ update\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}trie\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}v\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}trie{\isachardoublequote}\isanewline
|
11866
|
77 |
\isamarkupfalse%
|
8749
|
78 |
\isacommand{primrec}\isanewline
|
9674
|
79 |
\ \ {\isachardoublequote}update\ t\ {\isacharbrackleft}{\isacharbrackright}\ \ \ \ \ v\ {\isacharequal}\ Trie\ {\isacharparenleft}Some\ v{\isacharparenright}\ {\isacharparenleft}alist\ t{\isacharparenright}{\isachardoublequote}\isanewline
|
|
80 |
\ \ {\isachardoublequote}update\ t\ {\isacharparenleft}a{\isacharhash}as{\isacharparenright}\ v\ {\isacharequal}\isanewline
|
|
81 |
\ \ \ \ \ {\isacharparenleft}let\ tt\ {\isacharequal}\ {\isacharparenleft}case\ assoc\ {\isacharparenleft}alist\ t{\isacharparenright}\ a\ of\isanewline
|
|
82 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ None\ {\isasymRightarrow}\ Trie\ None\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbar}\ Some\ at\ {\isasymRightarrow}\ at{\isacharparenright}\isanewline
|
11866
|
83 |
\ \ \ \ \ \ in\ Trie\ {\isacharparenleft}value\ t{\isacharparenright}\ {\isacharparenleft}{\isacharparenleft}a{\isacharcomma}update\ tt\ as\ v{\isacharparenright}\ {\isacharhash}\ alist\ t{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
84 |
%
|
8749
|
85 |
\begin{isamarkuptext}%
|
|
86 |
\noindent
|
|
87 |
The base case is obvious. In the recursive case the subtrie
|
|
88 |
\isa{tt} associated with the first letter \isa{a} is extracted,
|
|
89 |
recursively updated, and then placed in front of the association list.
|
|
90 |
The old subtrie associated with \isa{a} is still in the association list
|
|
91 |
but no longer accessible via \isa{assoc}. Clearly, there is room here for
|
|
92 |
optimizations!
|
|
93 |
|
|
94 |
Before we start on any proofs about \isa{update} we tell the simplifier to
|
|
95 |
expand all \isa{let}s and to split all \isa{case}-constructs over
|
|
96 |
options:%
|
|
97 |
\end{isamarkuptext}%
|
11866
|
98 |
\isamarkuptrue%
|
|
99 |
\isacommand{declare}\ Let{\isacharunderscore}def{\isacharbrackleft}simp{\isacharbrackright}\ option{\isachardot}split{\isacharbrackleft}split{\isacharbrackright}\isamarkupfalse%
|
|
100 |
%
|
8749
|
101 |
\begin{isamarkuptext}%
|
|
102 |
\noindent
|
|
103 |
The reason becomes clear when looking (probably after a failed proof
|
|
104 |
attempt) at the body of \isa{update}: it contains both
|
|
105 |
\isa{let} and a case distinction over type \isa{option}.
|
|
106 |
|
|
107 |
Our main goal is to prove the correct interaction of \isa{update} and
|
|
108 |
\isa{lookup}:%
|
|
109 |
\end{isamarkuptext}%
|
11866
|
110 |
\isamarkuptrue%
|
9674
|
111 |
\isacommand{theorem}\ {\isachardoublequote}{\isasymforall}t\ v\ bs{\isachardot}\ lookup\ {\isacharparenleft}update\ t\ as\ v{\isacharparenright}\ bs\ {\isacharequal}\isanewline
|
11866
|
112 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharparenleft}if\ as{\isacharequal}bs\ then\ Some\ v\ else\ lookup\ t\ bs{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
113 |
%
|
8749
|
114 |
\begin{isamarkuptxt}%
|
|
115 |
\noindent
|
|
116 |
Our plan is to induct on \isa{as}; hence the remaining variables are
|
|
117 |
quantified. From the definitions it is clear that induction on either
|
11480
|
118 |
\isa{as} or \isa{bs} is required. The choice of \isa{as} is
|
8749
|
119 |
guided by the intuition that simplification of \isa{lookup} might be easier
|
|
120 |
if \isa{update} has already been simplified, which can only happen if
|
|
121 |
\isa{as} is instantiated.
|
11480
|
122 |
The start of the proof is conventional:%
|
8749
|
123 |
\end{isamarkuptxt}%
|
11866
|
124 |
\isamarkuptrue%
|
|
125 |
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ as{\isacharcomma}\ auto{\isacharparenright}\isamarkupfalse%
|
|
126 |
%
|
8749
|
127 |
\begin{isamarkuptxt}%
|
|
128 |
\noindent
|
|
129 |
Unfortunately, this time we are left with three intimidating looking subgoals:
|
9723
|
130 |
\begin{isabelle}
|
8749
|
131 |
~1.~\dots~{\isasymLongrightarrow}~lookup~\dots~bs~=~lookup~t~bs\isanewline
|
|
132 |
~2.~\dots~{\isasymLongrightarrow}~lookup~\dots~bs~=~lookup~t~bs\isanewline
|
9792
|
133 |
~3.~\dots~{\isasymLongrightarrow}~lookup~\dots~bs~=~lookup~t~bs
|
9723
|
134 |
\end{isabelle}
|
8749
|
135 |
Clearly, if we want to make headway we have to instantiate \isa{bs} as
|
|
136 |
well now. It turns out that instead of induction, case distinction
|
|
137 |
suffices:%
|
|
138 |
\end{isamarkuptxt}%
|
11866
|
139 |
\isamarkuptrue%
|
10171
|
140 |
\isacommand{apply}{\isacharparenleft}case{\isacharunderscore}tac{\isacharbrackleft}{\isacharbang}{\isacharbrackright}\ bs{\isacharcomma}\ auto{\isacharparenright}\isanewline
|
11866
|
141 |
\isamarkupfalse%
|
|
142 |
\isacommand{done}\isamarkupfalse%
|
|
143 |
%
|
8749
|
144 |
\begin{isamarkuptext}%
|
|
145 |
\noindent
|
11480
|
146 |
\index{subgoal numbering}%
|
9541
|
147 |
All methods ending in \isa{tac} take an optional first argument that
|
9792
|
148 |
specifies the range of subgoals they are applied to, where \isa{{\isacharbrackleft}{\isacharbang}{\isacharbrackright}} means
|
10187
|
149 |
all subgoals, i.e.\ \isa{{\isacharbrackleft}{\isadigit{1}}{\isacharminus}{\isadigit{3}}{\isacharbrackright}} in our case. Individual subgoal numbers,
|
|
150 |
e.g. \isa{{\isacharbrackleft}{\isadigit{2}}{\isacharbrackright}} are also allowed.
|
8749
|
151 |
|
|
152 |
This proof may look surprisingly straightforward. However, note that this
|
9792
|
153 |
comes at a cost: the proof script is unreadable because the intermediate
|
|
154 |
proof states are invisible, and we rely on the (possibly brittle) magic of
|
10971
|
155 |
\isa{auto} (\isa{simp{\isacharunderscore}all} will not do --- try it) to split the subgoals
|
9792
|
156 |
of the induction up in such a way that case distinction on \isa{bs} makes
|
11480
|
157 |
sense and solves the proof.
|
11294
|
158 |
|
|
159 |
\begin{exercise}
|
|
160 |
Modify \isa{update} (and its type) such that it allows both insertion and
|
11304
|
161 |
deletion of entries with a single function. Prove the corresponding version
|
|
162 |
of the main theorem above.
|
|
163 |
Optimize your function such that it shrinks tries after
|
|
164 |
deletion if possible.
|
11294
|
165 |
\end{exercise}
|
|
166 |
|
|
167 |
\begin{exercise}
|
|
168 |
Write an improved version of \isa{update} that does not suffer from the
|
|
169 |
space leak (pointed out above) caused by not deleting overwritten entries
|
|
170 |
from the association list. Prove the main theorem for your improved
|
|
171 |
\isa{update}.
|
|
172 |
\end{exercise}
|
|
173 |
|
|
174 |
\begin{exercise}
|
|
175 |
Conceptually, each node contains a mapping from letters to optional
|
|
176 |
subtries. Above we have implemented this by means of an association
|
|
177 |
list. Replay the development replacing \isa{{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}v{\isacharparenright}\ trie{\isacharparenright}\ list}
|
|
178 |
with \isa{{\isacharprime}a\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}v{\isacharparenright}\ trie\ option}.
|
|
179 |
\end{exercise}%
|
8749
|
180 |
\end{isamarkuptext}%
|
11866
|
181 |
\isamarkuptrue%
|
|
182 |
\isamarkupfalse%
|
|
183 |
\isamarkupfalse%
|
|
184 |
\isamarkupfalse%
|
|
185 |
\isamarkupfalse%
|
|
186 |
\isamarkupfalse%
|
|
187 |
\isamarkupfalse%
|
|
188 |
\isamarkupfalse%
|
|
189 |
\isamarkupfalse%
|
|
190 |
\isamarkupfalse%
|
|
191 |
\isamarkupfalse%
|
|
192 |
\isamarkupfalse%
|
|
193 |
\isamarkupfalse%
|
|
194 |
\isamarkupfalse%
|
|
195 |
\isamarkupfalse%
|
|
196 |
\isamarkupfalse%
|
|
197 |
\isamarkupfalse%
|
|
198 |
\isamarkupfalse%
|
|
199 |
\isamarkupfalse%
|
|
200 |
\isamarkupfalse%
|
|
201 |
\isamarkupfalse%
|
|
202 |
\isamarkupfalse%
|
|
203 |
\isamarkupfalse%
|
|
204 |
\isamarkupfalse%
|
|
205 |
\isamarkupfalse%
|
|
206 |
\isamarkupfalse%
|
|
207 |
\isamarkupfalse%
|
|
208 |
\isamarkupfalse%
|
|
209 |
\isamarkupfalse%
|
|
210 |
\isamarkupfalse%
|
|
211 |
\isamarkupfalse%
|
|
212 |
\isamarkupfalse%
|
|
213 |
\isamarkupfalse%
|
|
214 |
\isamarkupfalse%
|
|
215 |
\isamarkupfalse%
|
|
216 |
\isamarkupfalse%
|
9722
|
217 |
\end{isabellebody}%
|
9145
|
218 |
%%% Local Variables:
|
|
219 |
%%% mode: latex
|
|
220 |
%%% TeX-master: "root"
|
|
221 |
%%% End:
|