summaryrefslogtreecommitdiff
path: root/docs/examples/monads
diff options
context:
space:
mode:
authorGilles Dubochet <gilles.dubochet@epfl.ch>2005-12-16 18:44:33 +0000
committerGilles Dubochet <gilles.dubochet@epfl.ch>2005-12-16 18:44:33 +0000
commit53a3cc7b17f4cf97075b7e71720777fd84109696 (patch)
tree0cc784e0b47ea49cc151a136d19f20bfa8ee2197 /docs/examples/monads
parentdf50e05006b43b007c2587549030d24b5c154398 (diff)
downloadscala-53a3cc7b17f4cf97075b7e71720777fd84109696.tar.gz
scala-53a3cc7b17f4cf97075b7e71720777fd84109696.tar.bz2
scala-53a3cc7b17f4cf97075b7e71720777fd84109696.zip
Created proper 'docs' folder for new layout.
Diffstat (limited to 'docs/examples/monads')
-rw-r--r--docs/examples/monads/callccInterpreter.scala84
-rw-r--r--docs/examples/monads/directInterpreter.scala55
-rw-r--r--docs/examples/monads/errorInterpreter.scala86
-rw-r--r--docs/examples/monads/simpleInterpreter.scala75
-rw-r--r--docs/examples/monads/stateInterpreter.scala86
5 files changed, 386 insertions, 0 deletions
diff --git a/docs/examples/monads/callccInterpreter.scala b/docs/examples/monads/callccInterpreter.scala
new file mode 100644
index 0000000000..934fce1a9e
--- /dev/null
+++ b/docs/examples/monads/callccInterpreter.scala
@@ -0,0 +1,84 @@
+object callccInterpreter {
+
+ type Answer = Value;
+
+ 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 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]) = {
+ System.out.println(test(term0));
+ System.out.println(test(term1));
+ System.out.println(test(term2));
+ }
+}
+
diff --git a/docs/examples/monads/directInterpreter.scala b/docs/examples/monads/directInterpreter.scala
new file mode 100644
index 0000000000..0e2aae4a7b
--- /dev/null
+++ b/docs/examples/monads/directInterpreter.scala
@@ -0,0 +1,55 @@
+object directInterpreter {
+
+ 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 Value;
+ case object Wrong extends Value;
+ case class Num(n: int) extends Value;
+ case class Fun(f: Value => Value)extends Value;
+
+ def showval(v: Value): String = v match {
+ case Wrong => "<wrong>"
+ case Num(n) => n.toString()
+ case Fun(f) => "<function>"
+ }
+
+ type Environment = List[Pair[Name, Value]];
+
+ def lookup(x: Name, e: Environment): Value = e match {
+ case List() => Wrong
+ case Pair(y, b) :: e1 => if (x == y) b else lookup(x, e1)
+ }
+
+ def add(a: Value, b: Value): Value = Pair(a, b) match {
+ case Pair(Num(m), Num(n)) => Num(m + n)
+ case _ => Wrong
+ }
+
+ def apply(a: Value, b: Value) = a match {
+ case Fun(k) => k(b)
+ case _ => Wrong
+ }
+
+ def interp(t: Term, e: Environment): Value = t match {
+ case Var(x) => lookup(x, e)
+ case Con(n) => Num(n)
+ case Add(l, r) => add(interp(l, e), interp(r, e))
+ case Lam(x, t) => Fun(a => interp(t, Pair(x, a) :: e))
+ case App(f, t) => apply(interp(f, e), interp(t, e))
+ }
+
+ def test(t: Term): String =
+ showval(interp(t, List()));
+
+ val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11)));
+
+ def main(args: Array[String]) =
+ System.out.println(test(term0));
+}
diff --git a/docs/examples/monads/errorInterpreter.scala b/docs/examples/monads/errorInterpreter.scala
new file mode 100644
index 0000000000..05dc979621
--- /dev/null
+++ b/docs/examples/monads/errorInterpreter.scala
@@ -0,0 +1,86 @@
+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 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;
+
+ 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 Err[T](msg: String) extends M[T] {
+ def bind[A](k: A => M[B]): M[B] = new Err(msg);
+ def show: String = "Error: " + msg;
+ }
+
+ 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 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() => errorM("unbound variable: " + x);
+ 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 _ => errorM("should be numbers: " + a + "," + b)
+ }
+
+ def apply(a: Value, b: Value): M[Value] = a match {
+ case Fun(k) => k(b)
+ 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))
+ 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
+ }
+
+ 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));
+
+ def main(args: Array[String]) = {
+ System.out.println(test(term0));
+ System.out.println(test(term1));
+ }
+}
+
diff --git a/docs/examples/monads/simpleInterpreter.scala b/docs/examples/monads/simpleInterpreter.scala
new file mode 100644
index 0000000000..d728c620cf
--- /dev/null
+++ b/docs/examples/monads/simpleInterpreter.scala
@@ -0,0 +1,75 @@
+object simpleInterpreter {
+
+ case class M[A](value: A) {
+ def bind[B](k: A => M[B]): M[B] = k(value);
+ 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] = M(a);
+
+ def showM(m: M[Value]): String = m.value.toString();
+
+ 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 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
+ }
+
+ 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));
+
+ def main(args: Array[String]) = {
+ System.out.println(test(term0));
+ System.out.println(test(term1));
+ }
+}
+
diff --git a/docs/examples/monads/stateInterpreter.scala b/docs/examples/monads/stateInterpreter.scala
new file mode 100644
index 0000000000..593f2d9e3e
--- /dev/null
+++ b/docs/examples/monads/stateInterpreter.scala
@@ -0,0 +1,86 @@
+package examples.monads;
+
+object stateInterpreter {
+
+ type State = int;
+
+ val tickS = new M(s => Pair((), s + 1));
+
+ case class M[A](in: State => Pair[A, State]) {
+ def bind[B](k: A => M[B]) = M[B]{ s0 =>
+ val Pair(a, s1) = this in s0; k(a) in s1
+ }
+ 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](s => Pair(a, s));
+
+ def showM(m: M[Value]): String = {
+ val Pair(a, s1) = m in 0;
+ "Value: " + a + "; Count: " + s1
+ }
+
+ 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 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)) => for (val _ <- tickS) yield Num(m + n)
+ case _ => unitM(Wrong)
+ }
+
+ def apply(a: Value, b: Value): M[Value] = a match {
+ case Fun(k) => for (val _ <- tickS; val c <- k(b)) yield c
+ 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
+ }
+
+ 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));
+
+ def main(args: Array[String]) = {
+ System.out.println(test(term0));
+ System.out.println(test(term1));
+ }
+}
+