2022-08-10 14:02:14 +03:00
|
|
|
--- Towers of Hanoi is a puzzle with three rods and n disks of decresing size.
|
|
|
|
---
|
|
|
|
--- The disks are stacked ontop of each other through the first rod.
|
|
|
|
--- The aim of the game is to move the stack of disks to another rod while
|
|
|
|
--- following these rules:
|
|
|
|
---
|
|
|
|
--- 1. Only one disk can be moved at a time
|
|
|
|
--- 2. You may only move a disk from the top of one of the stacks to the top of another stack
|
|
|
|
--- 3. No disk may be moved on top of a smaller disk
|
|
|
|
---
|
|
|
|
--- The function ;hanoi; computes the sequence of moves to solve puzzle.
|
|
|
|
|
|
|
|
module Hanoi;
|
|
|
|
|
|
|
|
open import Stdlib.Prelude;
|
|
|
|
|
|
|
|
--- Concatenation of ;String;s
|
|
|
|
infixr 5 ++str;
|
|
|
|
axiom ++str : String → String → String;
|
|
|
|
compile ++str {
|
|
|
|
c ↦ "concat";
|
|
|
|
};
|
|
|
|
|
|
|
|
--- Concatenates a list of strings
|
|
|
|
---
|
|
|
|
--- ;concat (("a" ∷ nil) ∷ ("b" ∷ nil)); evaluates to ;"a" ∷ "b" ∷ nil;
|
|
|
|
concat : List String → String;
|
2022-09-30 03:55:32 +03:00
|
|
|
concat := foldl (++str) "";
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
intercalate : String → List String → String;
|
2022-09-30 03:55:32 +03:00
|
|
|
intercalate sep xs := concat (intersperse sep xs);
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
--- Joins a list of strings with the newline character
|
|
|
|
unlines : List String → String;
|
2022-09-30 03:55:32 +03:00
|
|
|
unlines := intercalate "\n";
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
--- Produce a singleton List
|
|
|
|
singleton : {A : Type} → A → List A;
|
2022-09-30 03:55:32 +03:00
|
|
|
singleton a := a ∷ nil;
|
2022-08-10 14:02:14 +03:00
|
|
|
|
2022-09-30 03:55:32 +03:00
|
|
|
--- Produce a ;String; representation of a ;List Nat;
|
|
|
|
showList : List Nat → String;
|
|
|
|
showList xs := "[" ++str intercalate "," (map natToStr xs) ++str "]";
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
--- A Peg represents a peg in the towers of Hanoi game
|
2023-01-03 15:49:04 +03:00
|
|
|
type Peg :=
|
|
|
|
left : Peg
|
|
|
|
| middle : Peg
|
|
|
|
| right : Peg;
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
showPeg : Peg → String;
|
2022-09-30 03:55:32 +03:00
|
|
|
showPeg left := "left";
|
|
|
|
showPeg middle := "middle";
|
|
|
|
showPeg right := "right";
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
--- A Move represents a move between pegs
|
2023-01-03 15:49:04 +03:00
|
|
|
type Move := move : Peg → Peg → Move;
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
showMove : Move → String;
|
2022-09-30 03:55:32 +03:00
|
|
|
showMove (move from to) := showPeg from ++str " -> " ++str showPeg to;
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
--- Produce a list of ;Move;s that solves the towers of Hanoi game
|
2022-09-30 03:55:32 +03:00
|
|
|
hanoi : Nat → Peg → Peg → Peg → List Move;
|
|
|
|
hanoi zero _ _ _ := nil;
|
|
|
|
hanoi (suc n) p1 p2 p3 := hanoi n p1 p3 p2 ++ singleton (move p1 p2) ++ hanoi n p3 p2 p1;
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
main : IO;
|
2022-09-30 03:55:32 +03:00
|
|
|
main := putStrLn (unlines (map showMove (hanoi 5 left middle right)));
|
2022-08-10 14:02:14 +03:00
|
|
|
|
|
|
|
end;
|