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