Skip to content

@joshcough /gist:1676216
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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)) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.