Skip to content

Instantly share code, notes, and snippets.

@joshcough
Created January 25, 2012 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshcough/1676216 to your computer and use it in GitHub Desktop.
Save joshcough/1676216 to your computer and use it in GitHub Desktop.
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