13739
|
1 |
(*<*)
|
|
2 |
theory Hanoi = Main:
|
|
3 |
(*>*)
|
|
4 |
subsection {* The towers of Hanoi \label{psv2000hanoi} *}
|
|
5 |
|
|
6 |
text {*
|
|
7 |
|
|
8 |
We are given 3 pegs $A$, $B$ and $C$, and $n$ disks with a hole, such
|
|
9 |
that no two disks have the same diameter. Initially all $n$ disks
|
|
10 |
rest on peg $A$, ordered according to their size, with the largest one
|
|
11 |
at the bottom. The aim is to transfer all $n$ disks from $A$ to $C$ by
|
|
12 |
a sequence of single-disk moves such that we never place a larger disk
|
|
13 |
on top of a smaller one. Peg $B$ may be used for intermediate storage.
|
|
14 |
|
|
15 |
\begin{center}
|
|
16 |
\includegraphics[width=0.8\textwidth]{Hanoi}
|
|
17 |
\end{center}
|
|
18 |
|
|
19 |
\medskip The pegs and moves can be modelled as follows:
|
|
20 |
*};
|
|
21 |
|
|
22 |
datatype peg = A | B | C
|
|
23 |
types move = "peg * peg"
|
|
24 |
|
|
25 |
text {*
|
|
26 |
Define a primitive recursive function
|
|
27 |
*};
|
|
28 |
|
|
29 |
consts
|
|
30 |
moves :: "nat => peg => peg => peg => move list";
|
|
31 |
|
|
32 |
text {* such that @{term moves}$~n~a~b~c$ returns a list of (legal)
|
|
33 |
moves that transfer $n$ disks from peg $a$ via $b$ to $c$.
|
|
34 |
Show that this requires $2^n - 1$ moves:
|
|
35 |
*}
|
|
36 |
|
|
37 |
theorem "length (moves n a b c) = 2^n - 1"
|
|
38 |
(*<*)oops(*>*)
|
|
39 |
|
|
40 |
text {* Hint: You need to strengthen the theorem for the
|
|
41 |
induction to go through. Beware: subtraction on natural numbers
|
|
42 |
behaves oddly: $n - m = 0$ if $n \le m$. *}
|
|
43 |
|
|
44 |
(*<*)
|
|
45 |
end
|
|
46 |
(*>*)
|