2012-04-01
во дворце у снежной королевы
scala > implicit def ski[env,a,b](ef:env=>a=>b) = new { def S(es:env=>a) = (e:env) => ef(e)(es(e))} scala> S _ res24: ((Nothing) => (Nothing) => Nothing) => ((Nothing) => Nothing) => (Nothing) => Nothing = <function1>
Не выходит каменный цветок.
Entry tags:
- applicative,
- fp,
- fprog,
- scala
code snippet
Still 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.