doc-src/Exercises/2002/a2/a2.thy
author berghofe
Tue, 01 Jun 2004 15:02:05 +0200
changeset 14861 ca5cae7fb65a
parent 13739 f5d0a66c8124
permissions -rw-r--r--
Removed ~10000 hack in function idx that can lead to inconsistencies when unifying terms with a large number of abstractions.
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 a2 = Main:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     3
(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     4
subsection {* Sorting \label{psv2002a2} *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     5
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     6
text{* For simplicity we sort natural numbers. *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     7
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     8
subsubsection{* Sorting with lists*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     9
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    10
text {*
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    11
The task is to define insertion sort and prove its correctness.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    12
The following functions are required:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    13
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    14
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    15
consts 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    16
  insort :: "nat \<Rightarrow> nat list \<Rightarrow> nat list"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    17
  sort   :: "nat list \<Rightarrow> nat list"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    18
  le     :: "nat \<Rightarrow> nat list \<Rightarrow> bool"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    19
  sorted :: "nat list \<Rightarrow> bool"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    20
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    21
text {* In your definition, @{term "insort x xs"} should insert a
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    22
number @{term x} into an already sorted list @{text xs}, and @{term
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    23
"sort ys"} should build on @{text insort} to produce the sorted
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    24
version of @{text ys}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    25
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    26
To show that the resulting list is indeed sorted we need a predicate
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    27
@{term sorted} that checks if each element in the list is less or equal
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    28
to the following ones; @{term "le n xs"} should be true iff
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    29
@{term n} is less or equal to all elements of @{text xs}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    30
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    31
Start out by showing a monotonicity property of @{term le}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    32
For technical reasons the lemma should be phrased as follows:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    33
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    34
lemma [simp]: "x \<le> y \<Longrightarrow> le y xs \<longrightarrow> le x xs"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    35
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    36
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    37
text {* Now show the following correctness theorem: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    38
theorem "sorted (sort xs)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    39
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    40
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    41
text {* This theorem alone is too weak. It does not guarantee that the
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    42
sorted list contains the same elements as the input. In the worst
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    43
case, @{term sort} might always return @{term"[]"} --- surely an
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    44
undesirable implementation of sorting.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    45
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    46
Define a function @{term "count xs x"} that counts how often @{term x}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    47
occurs in @{term xs}. Show that
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    48
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    49
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    50
theorem "count (sort xs) x = count xs x"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    51
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    52
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    53
subsubsection {* Sorting with trees *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    54
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    55
text {* Our second sorting algorithm uses trees. Thus you should first
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    56
define a data type @{text bintree} of binary trees that are either
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    57
empty or consist of a node carrying a natural number and two subtrees.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    58
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    59
Define a function @{text tsorted} that checks if a binary tree is
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    60
sorted. It is convenien to employ two auxiliary functions @{text
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    61
tge}/@{text tle} that test whether a number is
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    62
greater-or-equal/less-or-equal to all elements of a tree.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    63
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    64
Finally define a function @{text tree_of} that turns a list into a
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    65
sorted tree. It is helpful to base @{text tree_of} on a function
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    66
@{term "ins n b"} that inserts a number @{term n} into a sorted tree
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    67
@{term b}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    68
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    69
Show
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    70
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    71
theorem [simp]: "tsorted (tree_of xs)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    72
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    73
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    74
text {* Again we have to show that no elements are lost (or added).
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    75
As for lists, define a function @{term "tcount x b"} that counts the
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    76
number of occurrences of the number @{term x} in the tree @{term b}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    77
Show
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    78
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    79
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    80
theorem "tcount (tree_of xs) x = count xs x"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    81
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    82
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    83
text {* Now we are ready to sort lists. We know how to produce an
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    84
ordered tree from a list. Thus we merely need a function @{text
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    85
list_of} that turns an (ordered) tree into an (ordered) list.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    86
Define this function and prove
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    87
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    88
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    89
theorem "sorted (list_of (tree_of xs))"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    90
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    91
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    92
theorem "count (list_of (tree_of xs)) n = count xs n"    
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    93
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    94
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    95
text {*
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    96
Hints:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    97
\begin{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    98
\item
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    99
Try to formulate all your lemmas as equations rather than implications
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   100
because that often simplifies their proof.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   101
Make sure that the right-hand side is (in some sense)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   102
simpler than the left-hand side.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   103
\item
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   104
Eventually you need to relate @{text sorted} and @{text tsorted}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   105
This is facilitated by a function @{text ge} on lists (analogously to
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   106
@{text tge} on trees) and the following lemma (that you will need to prove):
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   107
@{term[display] "sorted (a@x#b) = (sorted a \<and> sorted b \<and> ge x a \<and> le x b)"}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   108
\end{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   109
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   110
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   111
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   112
end
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
   113
(*>*)