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