返回顶部

收藏

Scala 调用 cc 解释器

更多

callccInterpreter.scala

object callccInterpreter {

  type Answer = Value;

  /** 
   * A continuation monad. 
   */
  case class M[A](in: (A => Answer) => Answer) {
    def bind[B](k: A => M[B])          = M[B](c => in (a => k(a) in c))
    def map[B](f: A => B): M[B]        = bind(x => unitM(f(x)))
    def flatMap[B](f: A => M[B]): M[B] = bind(f)
  }

  def unitM[A](a: A) = M[A](c => c(a))

  def id[A] = (x: A) => x
  def showM(m: M[Value]): String = (m in id).toString()

  def callCC[A](h: (A => M[A]) => M[A]) =
    M[A](c => h(a => M[A](d => c(a))) in c)

  type Name = String

  trait Term
  case class Var(x: Name) extends Term
  case class Con(n: int) extends Term
  case class Add(l: Term, r: Term) extends Term
  case class Lam(x: Name, body: Term) extends Term
  case class App(fun: Term, arg: Term) extends Term
  case class Ccc(x: Name, t: Term) extends Term

  trait Value
  case object Wrong extends Value {
   override def toString() = "wrong"
  }
  case class Num(n: Int) extends Value {
    override def toString() = n.toString()
  }
  case class Fun(f: Value => M[Value]) extends Value {
    override def toString() = "<function>"
  }

  type Environment = List[Pair[Name, Value]];

  def lookup(x: Name, e: Environment): M[Value] = e match {
    case List() => unitM(Wrong)
    case Pair(y, b) :: e1 => if (x == y) unitM(b) else lookup(x, e1)
  }

  def add(a: Value, b: Value): M[Value] = Pair(a, b) match {
    case Pair(Num(m), Num(n)) => unitM(Num(m + n))
    case _ => unitM(Wrong)
  }

  def apply(a: Value, b: Value): M[Value] = a match {
    case Fun(k) => k(b)
    case _ => unitM(Wrong)
  }

  def interp(t: Term, e: Environment): M[Value] = t match {
    case Var(x) => lookup(x, e)
    case Con(n) => unitM(Num(n))
    case Add(l, r) => for (val a <- interp(l, e);
               val b <- interp(r, e);
               val c <- add(a, b))
                      yield c
    case Lam(x, t) => unitM(Fun(a => interp(t, Pair(x, a) :: e)))
    case App(f, t) => for (val a <- interp(f, e);
               val b <- interp(t, e);
               val c <- apply(a, b))
              yield c
    case Ccc(x, t) => callCC(k => interp(t, Pair(x, Fun(k)) :: e))
  }

  def test(t: Term): String = showM(interp(t, List()))

  val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11)))
  val term1 = App(Con(1), Con(2))
  val term2 = Add(Con(1), Ccc("k", Add(Con(2), App(Var("k"), Con(4)))))

  def main(args: Array[String]) {
    println(test(term0))
    println(test(term1))
    println(test(term2))
  }
}

标签:scala

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2017-03-06 13:27:34在maven项目中混合使用java和scala两种jvm语言 by 甄码农
  2. 2015-08-06 10:27:46将 Scala XML 转成 Java DOM by huwei
  3. 2015-08-04 13:40:48Scale 之长整型 bigint by 风云轩
  4. 2015-08-04 13:35:16Scala 时间处理一例 by 睡到自然醒
  5. 2015-08-03 19:46:27Scala Actors by 跳跳虎
  6. 2015-07-10 20:53:17Scala 之 Properties by fengsweat
  7. 2015-07-09 21:21:23Scala Lazy Evaluation by Hugh
  8. 2015-07-04 11:04:16Scala 之 Socket 通讯示例 by 杨洋
  9. 2015-07-03 09:32:17Scala 程序中使用 Map 数据结构 by G.Conanca
  10. 2015-07-02 10:46:02Scala 的 for 和 yield 构造 by 童学芬
  11. 2015-07-02 10:38:23Scala 模式匹配 (Case Classes) by loking
相关聚客文章
  1. Harries 发表 2018-10-16 12:22:30 从Scala(2.10)类型的标签或符号获取Java类的任何方法?
  2. 博主 发表 2016-12-31 04:38:37 hadoop流水账之HBase,Spark和在Spark上操作HBase
  3. 博主 发表 2016-12-31 04:38:37 Java/Scala杂记之一
  4. 博主 发表 2016-12-31 04:38:37 Java/Scala杂记之二
  5. 博主 发表 2016-12-31 04:38:37 Java/Scala杂记之三
  6. 博主 发表 2016-12-31 04:38:37 Java/Scala杂记之三
  7. 博主 发表 2017-01-07 07:34:01 一年一语言之2016
  8. xiaoli.wang 发表 2018-09-10 13:30:56 穷人创业指南,告诉你没钱该如何创业?
  9. 炒饭 发表 2015-06-13 08:32:43 Scala学习(一)——类、对象和变量
  10. 炒饭 发表 2015-06-16 15:48:52 Scala学习(二)——成员,方法和构造方法
  11. 炒饭 发表 2015-06-18 07:42:29 Scala学习(三)——代码块和流程控制
  12. 炒饭 发表 2015-06-18 14:44:34 Scala学习(四)——类型