doc-src/Exercises/2000/a1/generated/Hanoi.tex
author paulson
Tue, 08 Jul 2003 11:44:30 +0200
changeset 14092 68da54626309
parent 13841 ed4e97874454
permissions -rw-r--r--
Conversion of ZF/UNITY/{FP,Union} to Isar script. Introduction of X-symbols to the ML files.
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{Hanoi}%
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{The towers of Hanoi \label{psv2000hanoi}%
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
We are given 3 pegs $A$, $B$ and $C$, and $n$ disks with a hole, such
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    12
that no two disks have the same diameter.  Initially all $n$ disks
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    13
rest on peg $A$, ordered according to their size, with the largest one
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    14
at the bottom. The aim is to transfer all $n$ disks from $A$ to $C$ by
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    15
a sequence of single-disk moves such that we never place a larger disk
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    16
on top of a smaller one. Peg $B$ may be used for intermediate storage.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    17
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    18
\begin{center}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    19
\includegraphics[width=0.8\textwidth]{Hanoi}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    20
\end{center}
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    21
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    22
\medskip The pegs and moves can be modelled as follows:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    23
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    24
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    25
\isacommand{datatype}\ peg\ {\isacharequal}\ A\ {\isacharbar}\ B\ {\isacharbar}\ C\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    26
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    27
\isacommand{types}\ move\ {\isacharequal}\ {\isachardoublequote}peg\ {\isacharasterisk}\ peg{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    28
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    29
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    30
Define a primitive recursive function%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    31
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    32
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    33
\isacommand{consts}\isanewline
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    34
\ \ moves\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isacharequal}{\isachargreater}\ peg\ {\isacharequal}{\isachargreater}\ peg\ {\isacharequal}{\isachargreater}\ peg\ {\isacharequal}{\isachargreater}\ move\ list{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    35
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    36
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    37
such that \isa{moves}$~n~a~b~c$ returns a list of (legal)
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    38
moves that transfer $n$ disks from peg $a$ via $b$ to $c$.
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    39
Show that this requires $2^n - 1$ moves:%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    40
\end{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    41
\isamarkuptrue%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    42
\isacommand{theorem}\ {\isachardoublequote}length\ {\isacharparenleft}moves\ n\ a\ b\ c{\isacharparenright}\ {\isacharequal}\ {\isadigit{2}}{\isacharcircum}n\ {\isacharminus}\ {\isadigit{1}}{\isachardoublequote}\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    43
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    44
%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    45
\begin{isamarkuptext}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    46
Hint: You need to strengthen the theorem for the
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    47
induction to go through. Beware: subtraction on natural numbers
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    48
behaves oddly: $n - m = 0$ if $n \le m$.%
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
\isamarkupfalse%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    52
\end{isabellebody}%
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    53
%%% Local Variables:
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    54
%%% mode: latex
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    55
%%% TeX-master: "root"
ed4e97874454 keep a copy of generated files in repository
kleing
parents:
diff changeset
    56
%%% End: