doc-src/Exercises/2001/a3/generated/Trie1.tex
author obua
Sun, 09 May 2004 23:04:36 +0200
changeset 14722 8e739a6eaf11
parent 13841 ed4e97874454
permissions -rw-r--r--
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13841
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     1
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     2
\begin{isabellebody}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     3
\def\isabellecontext{Trie{\isadigit{1}}}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     4
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     5
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     6
\isamarkupsubsection{Tries%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     7
}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     8
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     9
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    10
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    11
Section~3.4.4 of \cite{isabelle-tutorial} is a case study
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    12
about so-called \emph{tries}, a data structure for fast indexing with
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    13
strings. Read that section.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    14
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    15
The data type of tries over the alphabet type \isa{{\isacharprime}a} und the value
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    16
type \isa{{\isacharprime}v} is defined as follows:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    17
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    18
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    19
\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%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    20
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    21
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    22
A trie consists of an optional value and an association list
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    23
that maps letters of the alphabet to subtrees. Type \isa{{\isacharprime}a\ option} is
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    24
defined in section~2.5.3 of \cite{isabelle-tutorial}.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    25
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    26
There are also two selector functions \isa{value} and \isa{alist}:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    27
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    28
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    29
\isacommand{consts}\ value\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}v{\isacharparenright}\ trie\ {\isasymRightarrow}\ {\isacharprime}v\ option{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    30
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    31
\isacommand{primrec}\ {\isachardoublequote}value\ {\isacharparenleft}Trie\ ov\ al{\isacharparenright}\ {\isacharequal}\ ov{\isachardoublequote}\ \isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    32
\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    33
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    34
\isacommand{consts}\ 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
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    35
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    36
\isacommand{primrec}\ {\isachardoublequote}alist\ {\isacharparenleft}Trie\ ov\ al{\isacharparenright}\ {\isacharequal}\ al{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    37
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    38
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    39
Furthermore there is a function \isa{lookup} on tries
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    40
defined with the help of the generic search function \isa{assoc} on
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    41
association lists:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    42
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    43
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    44
\isacommand{consts}\ assoc\ {\isacharcolon}{\isacharcolon}\ \ {\isachardoublequote}{\isacharparenleft}{\isacharprime}key\ {\isacharasterisk}\ {\isacharprime}val{\isacharparenright}list\ {\isasymRightarrow}\ {\isacharprime}key\ {\isasymRightarrow}\ {\isacharprime}val\ option{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    45
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    46
\isacommand{primrec}\ {\isachardoublequote}assoc\ {\isacharbrackleft}{\isacharbrackright}\ x\ {\isacharequal}\ None{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    47
\ \ \ \ \ \ \ \ {\isachardoublequote}assoc\ {\isacharparenleft}p{\isacharhash}ps{\isacharparenright}\ x\ {\isacharequal}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    48
\ \ \ \ \ \ \ \ \ \ \ {\isacharparenleft}let\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}\ {\isacharequal}\ p\ in\ if\ a\ {\isacharequal}\ x\ then\ Some\ b\ else\ assoc\ ps\ x{\isacharparenright}{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    49
\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    50
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    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
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    52
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    53
\isacommand{primrec}\ {\isachardoublequote}lookup\ t\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ value\ t{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    54
\ \ \ \ \ \ \ \ {\isachardoublequote}lookup\ t\ {\isacharparenleft}a{\isacharhash}as{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}case\ assoc\ {\isacharparenleft}alist\ t{\isacharparenright}\ a\ of\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    55
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ None\ {\isasymRightarrow}\ None\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    56
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Some\ at\ {\isasymRightarrow}\ lookup\ at\ as{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    57
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    58
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    59
Finally, \isa{update} updates the value associated with some
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    60
string with a new value, overwriting the old one:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    61
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    62
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    63
\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
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    64
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    65
\isacommand{primrec}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    66
\ \ {\isachardoublequote}update\ t\ {\isacharbrackleft}{\isacharbrackright}\ \ \ \ \ v\ {\isacharequal}\ Trie\ {\isacharparenleft}Some\ v{\isacharparenright}\ {\isacharparenleft}alist\ t{\isacharparenright}{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    67
\ \ {\isachardoublequote}update\ t\ {\isacharparenleft}a{\isacharhash}as{\isacharparenright}\ v\ {\isacharequal}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    68
\ \ \ \ \ {\isacharparenleft}let\ tt\ {\isacharequal}\ {\isacharparenleft}case\ assoc\ {\isacharparenleft}alist\ t{\isacharparenright}\ a\ of\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    69
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ None\ {\isasymRightarrow}\ Trie\ None\ {\isacharbrackleft}{\isacharbrackright}\ \isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    70
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Some\ at\ {\isasymRightarrow}\ at{\isacharparenright}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    71
\ \ \ \ \ \ in\ Trie\ {\isacharparenleft}value\ t{\isacharparenright}\ {\isacharparenleft}{\isacharparenleft}a{\isacharcomma}\ update\ tt\ as\ v{\isacharparenright}\ {\isacharhash}\ alist\ t{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    72
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    73
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    74
The following theorem tells us that \isa{update} behaves as
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    75
expected:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    76
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    77
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    78
\isacommand{theorem}\ {\isachardoublequote}{\isasymforall}t\ v\ bs{\isachardot}\ lookup\ {\isacharparenleft}update\ t\ as\ v{\isacharparenright}\ bs\ {\isacharequal}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    79
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharparenleft}if\ as\ {\isacharequal}\ bs\ then\ Some\ v\ else\ lookup\ t\ bs{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    80
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    81
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    82
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    83
As a warming up exercise, define a function%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    84
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    85
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    86
\isacommand{consts}\ modify\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}v{\isacharparenright}\ trie\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}v\ option\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}v{\isacharparenright}\ trie{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    87
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    88
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    89
for inserting as well as deleting elements from a trie. Show
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    90
that \isa{modify} satisfies a suitably modified version of the
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    91
correctness theorem for \isa{update}.%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    92
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    93
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    94
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    95
\end{isabellebody}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    96
%%% Local Variables:
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    97
%%% mode: latex
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    98
%%% TeX-master: "root"
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    99
%%% End: