This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Would you mind explain the example code ?

1 reply
sw2wolf
Joined: 2009-07-16,
User offline. Last seen 42 years 45 weeks ago.

I have used java for several years. And i also know a bit haskell, but I
still donot understand the following code. Would somebody shed a light on me
?

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() = ""
}

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))
}
}

the result is:
42
wrong
5

-----
nightmare = unsafePerformIO(getWrongWife >>= sex)

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Would you mind explain the example code ?

It looks to me like an interpreter for Scheme, but without the parser;
so you have to build up your expressions via method calls.

> val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11)))
Scheme: ((lambda (x) (+ x x)) (+ 10 11))
(+ 10 11) is 21, so x is 21 in that invocation of that lambda: (+ 21 21) is 42.

> val term1 = App(Con(1), Con(2))
Scheme: (1 2) ; an error, as 1 is not a procedure.

> val term2 = Add(Con(1), Ccc("k", Add(Con(2), App(Var("k"), Con(4)))))
I'll probably get this one wrong if I try, but if you look at Scheme's
call/cc you'll probably get it.

2009/7/17 sw2wolf :
>
> I have used java for several years. And i also know a bit haskell, but I
> still donot understand the following code. Would somebody shed a light on me
> ?
>
> 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() = ""
>  }
>
>  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))
>  }
> }
>
> the result is:
> 42
> wrong
> 5
>
>
>
> -----
> nightmare = unsafePerformIO(getWrongWife >>= sex)
> --
> View this message in context: http://www.nabble.com/Would-you--mind-explain-the-example-code---tp24534...
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland