doc-src/Exercises/2002/a2/generated/a2.tex
author berghofe
Tue, 01 Jun 2004 15:02:05 +0200
changeset 14861 ca5cae7fb65a
parent 13841 ed4e97874454
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:
13841
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     1
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     2
\begin{isabellebody}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     3
\def\isabellecontext{a{\isadigit{2}}}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     4
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     5
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     6
\isamarkupsubsection{Sorting \label{psv2002a2}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     7
}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     8
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
     9
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    10
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    11
For simplicity we sort natural numbers.%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    12
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    13
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    14
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    15
\isamarkupsubsubsection{Sorting with lists%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    16
}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    17
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    18
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    19
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    20
The task is to define insertion sort and prove its correctness.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    21
The following functions are required:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    22
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    23
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    24
\isacommand{consts}\ \isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    25
\ \ insort\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymRightarrow}\ nat\ list\ {\isasymRightarrow}\ nat\ list{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    26
\ \ sort\ \ \ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ list\ {\isasymRightarrow}\ nat\ list{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    27
\ \ le\ \ \ \ \ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymRightarrow}\ nat\ list\ {\isasymRightarrow}\ bool{\isachardoublequote}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    28
\ \ sorted\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ list\ {\isasymRightarrow}\ bool{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    29
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    30
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    31
In your definition, \isa{insort\ x\ xs} should insert a
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    32
number \isa{x} into an already sorted list \isa{xs}, and \isa{sort\ ys} should build on \isa{insort} to produce the sorted
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    33
version of \isa{ys}.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    34
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    35
To show that the resulting list is indeed sorted we need a predicate
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    36
\isa{sorted} that checks if each element in the list is less or equal
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    37
to the following ones; \isa{le\ n\ xs} should be true iff
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    38
\isa{n} is less or equal to all elements of \isa{xs}.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    39
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    40
Start out by showing a monotonicity property of \isa{le}.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    41
For technical reasons the lemma should be phrased as follows:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    42
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    43
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    44
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}x\ {\isasymle}\ y\ {\isasymLongrightarrow}\ le\ y\ xs\ {\isasymlongrightarrow}\ le\ x\ xs{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    45
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    46
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    47
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    48
Now show the following correctness theorem:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    49
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    50
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    51
\isacommand{theorem}\ {\isachardoublequote}sorted\ {\isacharparenleft}sort\ xs{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    52
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    53
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    54
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    55
This theorem alone is too weak. It does not guarantee that the
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    56
sorted list contains the same elements as the input. In the worst
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    57
case, \isa{sort} might always return \isa{{\isacharbrackleft}{\isacharbrackright}} --- surely an
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    58
undesirable implementation of sorting.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    59
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    60
Define a function \isa{count\ xs\ x} that counts how often \isa{x}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    61
occurs in \isa{xs}. Show that%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    62
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    63
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    64
\isacommand{theorem}\ {\isachardoublequote}count\ {\isacharparenleft}sort\ xs{\isacharparenright}\ x\ {\isacharequal}\ count\ xs\ x{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    65
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    66
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    67
\isamarkupsubsubsection{Sorting with trees%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    68
}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    69
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    70
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    71
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    72
Our second sorting algorithm uses trees. Thus you should first
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    73
define a data type \isa{bintree} of binary trees that are either
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    74
empty or consist of a node carrying a natural number and two subtrees.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    75
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    76
Define a function \isa{tsorted} that checks if a binary tree is
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    77
sorted. It is convenien to employ two auxiliary functions \isa{tge}/\isa{tle} that test whether a number is
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    78
greater-or-equal/less-or-equal to all elements of a tree.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    79
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    80
Finally define a function \isa{tree{\isacharunderscore}of} that turns a list into a
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    81
sorted tree. It is helpful to base \isa{tree{\isacharunderscore}of} on a function
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    82
\isa{ins\ n\ b} that inserts a number \isa{n} into a sorted tree
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    83
\isa{b}.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    84
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    85
Show%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    86
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    87
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    88
\isacommand{theorem}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}tsorted\ {\isacharparenleft}tree{\isacharunderscore}of\ xs{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    89
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    90
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    91
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    92
Again we have to show that no elements are lost (or added).
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    93
As for lists, define a function \isa{tcount\ x\ b} that counts the
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    94
number of occurrences of the number \isa{x} in the tree \isa{b}.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    95
Show%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    96
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    97
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    98
\isacommand{theorem}\ {\isachardoublequote}tcount\ {\isacharparenleft}tree{\isacharunderscore}of\ xs{\isacharparenright}\ x\ {\isacharequal}\ count\ xs\ x{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    99
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   100
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   101
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   102
Now we are ready to sort lists. We know how to produce an
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   103
ordered tree from a list. Thus we merely need a function \isa{list{\isacharunderscore}of} that turns an (ordered) tree into an (ordered) list.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   104
Define this function and prove%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   105
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   106
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   107
\isacommand{theorem}\ {\isachardoublequote}sorted\ {\isacharparenleft}list{\isacharunderscore}of\ {\isacharparenleft}tree{\isacharunderscore}of\ xs{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   108
\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   109
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   110
\isacommand{theorem}\ {\isachardoublequote}count\ {\isacharparenleft}list{\isacharunderscore}of\ {\isacharparenleft}tree{\isacharunderscore}of\ xs{\isacharparenright}{\isacharparenright}\ n\ {\isacharequal}\ count\ xs\ n{\isachardoublequote}\ \ \ \ \isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   111
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   112
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   113
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   114
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   115
Hints:
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   116
\begin{itemize}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   117
\item
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   118
Try to formulate all your lemmas as equations rather than implications
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   119
because that often simplifies their proof.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   120
Make sure that the right-hand side is (in some sense)
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   121
simpler than the left-hand side.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   122
\item
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   123
Eventually you need to relate \isa{sorted} and \isa{tsorted}.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   124
This is facilitated by a function \isa{ge} on lists (analogously to
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   125
\isa{tge} on trees) and the following lemma (that you will need to prove):
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   126
\begin{isabelle}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   127
sorted\ {\isacharparenleft}a\ {\isacharat}\ x\ {\isacharhash}\ b{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}sorted\ a\ {\isasymand}\ sorted\ b\ {\isasymand}\ ge\ x\ a\ {\isasymand}\ le\ x\ b{\isacharparenright}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   128
\end{isabelle}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   129
\end{itemize}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   130
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   131
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   132
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   133
\end{isabellebody}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   134
%%% Local Variables:
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   135
%%% mode: latex
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   136
%%% TeX-master: "root"
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
   137
%%% End: