doc-src/Exercises/2002/a5/a5.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 a5 = 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 {* Merge sort *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     6
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     7
text {* Implement \emph{merge sort}: a list is sorted by splitting it
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     8
into two lists, sorting them separately, and merging the results.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     9
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    10
With the help of @{text recdef} define two functions
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    11
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    12
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    13
consts merge :: "nat list \<times> nat list \<Rightarrow> nat list"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    14
       msort :: "nat list \<Rightarrow> nat list"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    15
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    16
text {* and show *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    17
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    18
theorem "sorted (msort xs)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    19
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    20
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    21
theorem "count (msort xs) x = count xs x"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    22
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    23
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    24
text {* where @{term sorted} and @{term count} are defined as in
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    25
section~\ref{psv2002a2}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    26
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    27
Hints:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    28
\begin{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    29
\item For @{text recdef} see section~3.5 of \cite{isabelle-tutorial}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    30
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    31
\item To split a list into two halves of almost equal length you can
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    32
use the functions \mbox{@{text "n div 2"}}, @{term take} und @{term drop},
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    33
where @{term "take n xs"} returns the first @{text n} elements of
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    34
@{text xs} and @{text "drop n xs"} the remainder.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    35
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    36
\item Here are some potentially useful lemmas:\\
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    37
  @{text "linorder_not_le:"} @{thm linorder_not_le [no_vars]}\\
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    38
  @{text "order_less_le:"} @{thm order_less_le [no_vars]}\\
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    39
  @{text "min_def:"} @{thm min_def [no_vars]}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    40
\end{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    41
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    42
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    43
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    44
end 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    45
(*>*)