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