diff options
author | michelou <michelou@epfl.ch> | 2006-03-08 10:08:03 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2006-03-08 10:08:03 +0000 |
commit | c44a597469fcd3549618348ad9808d923a1c9c2c (patch) | |
tree | 4f889d88e69cb0c8cd0a528d3f2f97529d60ff80 /docs/examples/monads | |
parent | 8a9572b96be66414598ca9d27b29f5f3867f2023 (diff) | |
download | scala-c44a597469fcd3549618348ad9808d923a1c9c2c.tar.gz scala-c44a597469fcd3549618348ad9808d923a1c9c2c.tar.bz2 scala-c44a597469fcd3549618348ad9808d923a1c9c2c.zip |
adapted some more examples to Scala 2
Diffstat (limited to 'docs/examples/monads')
-rw-r--r-- | docs/examples/monads/errorInterpreter.scala | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/docs/examples/monads/errorInterpreter.scala b/docs/examples/monads/errorInterpreter.scala index 05dc979621..4edb5caf88 100644 --- a/docs/examples/monads/errorInterpreter.scala +++ b/docs/examples/monads/errorInterpreter.scala @@ -1,47 +1,47 @@ object errorInterpreter { trait M[A] { - def show: String; - def bind[B](k: A => M[B]); - 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 show: String + def bind[B](k: A => M[B]): M[B] + 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] = new Suc(a); - def errorM[A](msg: String): M[A] = new Err(msg); + def unitM[A](a: A): M[A] = new Suc(a) + def errorM[A](msg: String): M[A] = new Err(msg) - def showM(m: M[Value]): String = m.show; + def showM(m: M[Value]): String = m.show - class Suc[A](x: T) extends M[A] { - def bind[A](k: A => M[B]): M[B] = k(x); - def show: String = "Success: " + x; + class Suc[A](x: A) extends M[A] { + def bind[B](k: A => M[B]): M[B] = k(x) + def show: String = "Success: " + x } - class Err[T](msg: String) extends M[T] { - def bind[A](k: A => M[B]): M[B] = new Err(msg); - def show: String = "Error: " + msg; + class Err[A](msg: String) extends M[A] { + def bind[B](k: A => M[B]): M[B] = new Err(msg) + def show: String = "Error: " + msg } - type Name = String; + 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; + 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 - trait Value; + trait Value case object Wrong extends Value { - override def toString() = "wrong" + override def toString() = "wrong" } case class Num(n: int) extends Value { - override def toString() = n.toString(); + override def toString() = n.toString() } case class Fun(f: Value => M[Value]) extends Value { override def toString() = "<function>" } - type Environment = List[Pair[Name, Value]]; + type Environment = List[Pair[Name, Value]] def lookup(x: Name, e: Environment): M[Value] = e match { case List() => errorM("unbound variable: " + x); @@ -55,32 +55,32 @@ object errorInterpreter { def apply(a: Value, b: Value): M[Value] = a match { case Fun(k) => k(b) - case _ => errorM("should be function: " + a); + case _ => errorM("should be function: " + a) } 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)) + 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 + val b <- interp(t, e); + val c <- apply(a, b)) + yield c } def test(t: Term): String = - showM(interp(t, List())); + 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 term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11))) + val term1 = App(Con(1), Con(2)) def main(args: Array[String]) = { - System.out.println(test(term0)); - System.out.println(test(term1)); + System.out.println(test(term0)) + System.out.println(test(term1)) } } |