doc-src/Exercises/2001/a3/Trie2.thy
author kleing
Sun, 18 May 2003 15:28:41 +0200
changeset 14035 c46ce87960fb
parent 13739 f5d0a66c8124
permissions -rw-r--r--
attach log files
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 Trie2 = 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
text {* Die above definition of @{term update} has the disadvantage
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     6
that it often creates junk: each association list it passes through is
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     7
extended at the left end with a new (letter,value) pair without
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     8
removing any old association of that letter which may already be
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     9
present.  Logically, such cleaning up is unnecessary because @{term
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    10
assoc} always searches the list from the left. However, it constitutes
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    11
a space leak: the old associations cannot be garbage collected because
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    12
physically they are still reachable. This problem can be solved by
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    13
means of a function *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    14
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    15
consts overwrite :: "'a \<Rightarrow> 'b \<Rightarrow> ('a * 'b) list \<Rightarrow> ('a * 'b) list"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    16
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    17
text {* that does not just add new pairs at the front but replaces old
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    18
associations by new pairs if possible.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    19
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    20
Define @{term overwrite}, modify @{term modify} to employ @{term
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    21
overwrite}, and show the same relationship between @{term modify} and
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    22
@{term lookup} as before. *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    23
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    24
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    25
end;
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    26
(*>*)