Skip to content

Instantly share code, notes, and snippets.

@jweslley
Forked from jrudolph/TowersOfHanoi.scala
Created November 21, 2009 03:31
Show Gist options
  • Select an option

  • Save jweslley/239977 to your computer and use it in GitHub Desktop.

Select an option

Save jweslley/239977 to your computer and use it in GitHub Desktop.

Revisions

  1. @jrudolph jrudolph revised this gist Feb 19, 2009. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion TowersOfHanoi.scala
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,8 @@ object TowersOfHanoi {
    System.out.println("Move from "+simpleName(a)+" to "+simpleName(b));null
    }

    implicit def moveN[P<:Nat,A,B,C](implicit m1:Move[P,A,C,B],m2:Move[_0,A,B,C], m3:Move[P,C,B,A]):Move[Succ[P],A,B,C] = null
    implicit def moveN[P<:Nat,A,B,C](implicit m1:Move[P,A,C,B],m2:Move[_0,A,B,C],m3:Move[P,C,B,A])
    :Move[Succ[P],A,B,C] = null

    def run[N<:Nat,A,B,C](implicit m:Move[N,A,B,C]) = null

  2. @jrudolph jrudolph revised this gist Feb 19, 2009. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions TowersOfHanoi.scala
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,11 @@
    /*
    * This is the Towers of Hanoi example from the prolog tutorial [1]
    * converted into Scala, using implicits to unfold the algorithm at
    * compile-time.
    *
    * [1] http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_3.html
    */

    object TowersOfHanoi {
    import scala.reflect.Manifest

  3. @jrudolph jrudolph renamed this gist Feb 19, 2009. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @jrudolph jrudolph created this gist Feb 19, 2009.
    35 changes: 35 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    object TowersOfHanoi {
    import scala.reflect.Manifest

    def simpleName(m:Manifest[_]):String = {
    val name = m.toString
    name.substring(name.lastIndexOf('$')+1)
    }

    trait Nat
    final class _0 extends Nat
    final class Succ[Pre<:Nat] extends Nat

    type _1 = Succ[_0]
    type _2 = Succ[_1]
    type _3 = Succ[_2]
    type _4 = Succ[_3]

    case class Move[N<:Nat,A,B,C]()

    implicit def move0[A,B,C](implicit a:Manifest[A],b:Manifest[B]):Move[_0,A,B,C] = {
    System.out.println("Move from "+simpleName(a)+" to "+simpleName(b));null
    }

    implicit def moveN[P<:Nat,A,B,C](implicit m1:Move[P,A,C,B],m2:Move[_0,A,B,C], m3:Move[P,C,B,A]):Move[Succ[P],A,B,C] = null

    def run[N<:Nat,A,B,C](implicit m:Move[N,A,B,C]) = null

    case class Left()
    case class Center()
    case class Right()

    def main(args:Array[String]){
    run[_2,Left,Right,Center]
    }
    }