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