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