Meta-Programming with Scala: Loop Unrolling

package michid.meta

import michid.meta.Naturals._

object PreProc {
  def LOOP[N] = null.asInstanceOf[N]

  trait Loop[N] {
    def apply(block: => Unit)
  }

  implicit def loop0(n: ZERO) = new Loop[ZERO] {
    def apply(block: => Unit) { }
  }

  implicit def loop[N <: NAT&#93;(n: SUCC&#91;N&#93;)(implicit f: N => Loop[N]) = new Loop[SUCC[N]] {
    def apply(block: => Unit) {
      block
      null.asInstanceOf[N].apply(block)
    }
  }

}

object LoopUnroll {
  import PreProc._

  def unrollTest() {
    // Need -Yrecursion 1
    //LOOP[(_3 x _2)#v] {
    LOOP[_5] {
      println("hello world")
    }
  }

  def main(args: Array[String]) {
    unrollTest()
  }
}

package michid.meta

object Naturals {
trait NAT {
type a[s[_ <: NAT] <: NAT, z <: NAT] <: NAT type v = a[SUCC, ZERO] } final class ZERO extends NAT { type a[s[_ <: NAT] <: NAT, z <: NAT] = z } final class SUCC[n <: NAT] extends NAT { type a[s[_ <: NAT] <: NAT, z <: NAT] = s[n#a[s, z]] } type _0 = ZERO type _1 = SUCC[_0] type _2 = SUCC[_1] type _3 = SUCC[_2] type _4 = SUCC[_3] type _5 = SUCC[_4] type _6 = SUCC[_5] trait ADD[n <: NAT, m <: NAT] extends NAT { type a[s[_ <: NAT] <: NAT, z <: NAT] = n#a[s, m#a[s, z]] } trait MUL[n <: NAT, m <: NAT] extends NAT { trait curry[n[_[_], _], s[_]] { type f[z] = n[s, z] } type a[s[_ <: NAT] <: NAT, z <: NAT] = n#a[curry[m#a, s]#f, z] } type +[n <: NAT, m <: NAT] = ADD[n, m] type x[n <: NAT, m <: NAT] = MUL[n, m] } [/sourcecode]

Advertisements

One response

26 02 2016
zhouchao

the code can not be compiled with scala-2.11.7 with sbt

name := “scala-scratch”

version := “1.0”

scalaVersion := “2.11.7”

scalacOptions += “-Yrecursion”

error message:

Error:(79, 46) type arguments [s,z] do not conform to type a’s type parameter bounds [s[_ <: Naturals.NAT] <: Naturals.NAT,z <: Naturals.NAT]
type a[s[_ <: NAT] <: NAT, z Unit)
}

implicit def loop0(n: ZERO) = new Loop[ZERO] {
def apply(block: => Unit) {}
}

implicit def loop[N Loop[N]) = new Loop[SUCC[N]] {
def apply(block: => Unit) {
block
null.asInstanceOf[N].apply(block)
}
}
}

object LoopUnroll {

import PreProc._
import Naturals._5

def unrollTest() {
// Need -Yrecursion 1
// LOOP[(_3 x _2)#v] {
LOOP[_5] {
println(“hello world”)
}
}

def main(args: Array[String]) {
unrollTest()
}
}

object Naturals {

trait NAT {
type a[s[_ <: NAT] <: NAT, z <: NAT] <: NAT
type v = a[SUCC, ZERO]
}

final class ZERO extends NAT {
type a[s[_ <: NAT] <: NAT, z <: NAT] = z
}

final class SUCC[n <: NAT] extends NAT {
type a[s[_ <: NAT] <: NAT, z <: NAT] = s[n#a[s, z]]
}

type _0 = ZERO
type _1 = SUCC[_0]
type _2 = SUCC[_1]
type _3 = SUCC[_2]
type _4 = SUCC[_3]
type _5 = SUCC[_4]
type _6 = SUCC[_5]

trait ADD[n <: NAT, m <: NAT] extends NAT {
type a[s[_ <: NAT] <: NAT, z <: NAT] = n#a[s, m#a[s, z]]
}

trait MUL[n <: NAT, m <: NAT] extends NAT {

trait curry[n[_[_], _], s[_]] {
type f[z] = n[s, z]
}

type a[s[_ <: NAT] <: NAT, z <: NAT] = n#a[curry[m#a, s]#f, z]
}

type +[n <: NAT, m <: NAT] = ADD[n, m]
type x[n <: NAT, m <: NAT] = MUL[n, m]
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: