public
Created

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14
def evalF(e:Exp): (Env[N,V] => V) = e match {
case ValE(v) => (_ => v)
case OpE(op, e1, e2) => e => op(evalF(e1)(e),evalF(e2)(e))
case IdE(s) => e => e.find(s).getOrElse(sys.error("undefined variable: " + s))
case LetE(IdE(s), e1, e2) => (e:Env[N,V]) => evalF(e2)(e.extend(s -> evalF(e1)(e)))
}
 
 
def eval(e:Exp)(implicit m: ReaderMonad[Env[N,V]]): Reader[Env[N,V],V] = e match {
case ValE(v) => m.unit(v)
case OpE(op, e1, e2) => m.lift2(op)(eval(e1), eval(e2))
case IdE(s) => Reader(_.find(s).getOrElse(sys.error("undefined variable: " + s)))
case LetE(IdE(s), e1, e2) => m.bind(eval(e1)){ v => Reader.local((e:Env[N,V]) => e.extend(s -> v))(eval(e2)) }
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.