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