Last active
August 29, 2015 14:28
-
-
Save ognis1205/5afa9345e44196c163b8 to your computer and use it in GitHub Desktop.
Scala Metaprogrammatic Hanoi's Tower Solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object TowerOfHanoi { | |
import scala.reflect.Manifest | |
def name(manifest:Manifest[_]):String = { | |
manifest.erasure.getName | |
} | |
trait NaturalNumber | |
final class Zero extends NaturalNumber | |
final class Successor[Previous<:NaturalNumber] extends NaturalNumber | |
type One = Successor[Zero] | |
type Two = Successor[One] | |
type Three = Successor[Two] | |
type Four = Successor[Three] | |
type Five = Successor[Four] | |
type Six = Successor[Five] | |
type Seven = Successor[Six] | |
type Eight = Successor[Seven] | |
type Nine = Successor[Eight] | |
type Ten = Successor[Nine] | |
case class Move[N<:NaturalNumber, Peg1, Peg2, Peg3]() | |
implicit def move[Peg1, Peg2, Peg3](implicit peg1:Manifest[Peg1], peg2:Manifest[Peg2]):Move[Zero, Peg1, Peg2, Peg3] = { | |
System.out.println("Move from " + name(peg1) + " to " + name(peg2)) | |
null | |
} | |
implicit def prepare[COUNT<:NaturalNumber, Peg1, Peg2, Peg3]( | |
implicit move1:Move[COUNT, Peg1, Peg3, Peg2], move2:Move[ZERO, Peg1, Peg2, Peg3], move3:Move[COUNT, Peg3, Peg2, Peg1]) | |
:Move[Successor[COUNT], Peg1, Peg2, Peg3] = null | |
def run[HEIGHT<:NaturalNumber, Peg1, Peg2, Peg3](implicit move:Move[HEIGHT, Peg1, Peg2, Peg3]) = null | |
case class Left() | |
case class Center() | |
case class Right() | |
def main(args:Array[String]){ | |
run[Ten, Left, Right, Center] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment