doc-src/Exercises/2001/a3/Trie1.thy
author obua
Sun, 09 May 2004 23:04:36 +0200
changeset 14722 8e739a6eaf11
parent 13739 f5d0a66c8124
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:
13739
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     1
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     2
theory Trie1 = Main:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     3
(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     4
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     5
subsection {* Tries *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     6
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     7
text {* Section~3.4.4 of \cite{isabelle-tutorial} is a case study
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     8
about so-called \emph{tries}, a data structure for fast indexing with
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     9
strings. Read that section.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    10
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    11
The data type of tries over the alphabet type @{typ 'a} und the value
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    12
type @{typ 'v} is defined as follows: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    13
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    14
datatype ('a, 'v) trie = Trie  "'v option"  "('a * ('a,'v) trie) list";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    15
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    16
text {* A trie consists of an optional value and an association list
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    17
that maps letters of the alphabet to subtrees. Type @{typ "'a option"} is
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    18
defined in section~2.5.3 of \cite{isabelle-tutorial}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    19
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    20
There are also two selector functions @{term value} and @{term alist}: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    21
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    22
consts value :: "('a, 'v) trie \<Rightarrow> 'v option"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    23
primrec "value (Trie ov al) = ov"; 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    24
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    25
consts alist :: "('a, 'v) trie \<Rightarrow> ('a * ('a,'v) trie) list";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    26
primrec "alist (Trie ov al) = al";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    27
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    28
text {* Furthermore there is a function @{term lookup} on tries
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    29
defined with the help of the generic search function @{term assoc} on
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    30
association lists: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    31
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    32
consts assoc ::  "('key * 'val)list \<Rightarrow> 'key \<Rightarrow> 'val option";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    33
primrec "assoc [] x = None"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    34
        "assoc (p#ps) x =
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    35
           (let (a, b) = p in if a = x then Some b else assoc ps x)";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    36
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    37
consts lookup :: "('a, 'v) trie \<Rightarrow> 'a list \<Rightarrow> 'v option";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    38
primrec "lookup t [] = value t"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    39
        "lookup t (a#as) = (case assoc (alist t) a of
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    40
                              None \<Rightarrow> None
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    41
                            | Some at \<Rightarrow> lookup at as)";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    42
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    43
text {* Finally, @{term update} updates the value associated with some
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    44
string with a new value, overwriting the old one: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    45
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    46
consts update :: "('a, 'v) trie \<Rightarrow> 'a list \<Rightarrow> 'v \<Rightarrow> ('a, 'v) trie";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    47
primrec
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    48
  "update t []     v = Trie (Some v) (alist t)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    49
  "update t (a#as) v =
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    50
     (let tt = (case assoc (alist t) a of
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    51
                  None \<Rightarrow> Trie None [] 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    52
                | Some at \<Rightarrow> at)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    53
      in Trie (value t) ((a, update tt as v) # alist t))";
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    54
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    55
text {* The following theorem tells us that @{term update} behaves as
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    56
expected: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    57
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    58
theorem "\<forall>t v bs. lookup (update t as v) bs =
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    59
                    (if as = bs then Some v else lookup t bs)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    60
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    61
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    62
text {* As a warming up exercise, define a function *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    63
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    64
consts modify :: "('a, 'v) trie \<Rightarrow> 'a list \<Rightarrow> 'v option \<Rightarrow> ('a, 'v) trie"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    65
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    66
text{* for inserting as well as deleting elements from a trie. Show
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    67
that @{term modify} satisfies a suitably modified version of the
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    68
correctness theorem for @{term update}.  *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    69
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    70
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    71
end;
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    72
(*>*)