Apr. 1st, 2012
code snippet
Apr. 1st, 2012 10:12 pmStill struggling implementing McBride/Paterson in Scala.
Kind of funny how one has to bypass the lack of Hindley-Milner... oh, whatever.
Will explain all this later, when I'm done with McBride-Paterson.
def K[env,a](x:a) = (gamma:env) => x
implicit def ski[env,a,b](ef:env=>a=>b) = new {
val S = (ea:env=>a) => (gamma:env) => ef(gamma)(ea(gamma))
}
val add = (i: Int) => (j: Int) => i+j
type Env = String => Int
def fetch(x: String) = (env: Env) => env(x)
trait Expr
case class Var(x: String) extends Expr
case class Val(i: Int) extends Expr
case class Add(p: Expr, q: Expr) extends Expr
def Ke[T](x:T) = K[Env, T](x)
def eval(exp: Expr): (Env => Int) = exp match {
case Var(x) => fetch(x)
case Val(i) => Ke(i)
case Add(p,q) => (Ke(add) S (eval(p))) S (eval(q))
}
eval(Add(Var("one"),Add(Var("three"), Val(11))))(Map("one" -> 1, "two" -> 2, "three" -> 3))
Kind of funny how one has to bypass the lack of Hindley-Milner... oh, whatever.
Will explain all this later, when I'm done with McBride-Paterson.