summaryrefslogtreecommitdiff
path: root/docs/examples/monads
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/monads')
-rw-r--r--docs/examples/monads/callccInterpreter.scala18
-rw-r--r--docs/examples/monads/directInterpreter.scala14
-rw-r--r--docs/examples/monads/errorInterpreter.scala10
-rw-r--r--docs/examples/monads/simpleInterpreter.scala14
-rw-r--r--docs/examples/monads/stateInterpreter.scala24
5 files changed, 40 insertions, 40 deletions
diff --git a/docs/examples/monads/callccInterpreter.scala b/docs/examples/monads/callccInterpreter.scala
index 5b556bd8fa..b5008c4c1b 100644
--- a/docs/examples/monads/callccInterpreter.scala
+++ b/docs/examples/monads/callccInterpreter.scala
@@ -14,7 +14,7 @@ object callccInterpreter {
def showM(m: M[Value]): String = (m in id).toString();
- def callCC[A](h: (A => M[A]) => M[A]) =
+ def callCC[A](h: (A => M[A]) => M[A]) =
M[A](c => h(a => M[A](d => c(a))) in c);
type Name = String;
@@ -30,7 +30,7 @@ object callccInterpreter {
trait Value;
case object Wrong extends Value {
override def toString() = "wrong"
- }
+ }
case class Num(n: Int) extends Value {
override def toString() = n.toString();
}
@@ -38,15 +38,15 @@ object callccInterpreter {
override def toString() = "<function>"
}
- type Environment = List[Pair[Name, Value]];
+ type Environment = List[Tuple2[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)
+ case (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))
+ def add(a: Value, b: Value): M[Value] = (a, b) match {
+ case (Num(m), Num(n)) => unitM(Num(m + n))
case _ => unitM(Wrong)
}
@@ -62,15 +62,15 @@ object callccInterpreter {
b <- interp(r, e);
c <- add(a, b))
yield c
- case Lam(x, t) => unitM(Fun(a => interp(t, Pair(x, a) :: e)))
+ case Lam(x, t) => unitM(Fun(a => interp(t, (x, a) :: e)))
case App(f, t) => for (a <- interp(f, e);
b <- interp(t, e);
c <- apply(a, b))
yield c
- case Ccc(x, t) => callCC(k => interp(t, Pair(x, Fun(k)) :: e))
+ case Ccc(x, t) => callCC(k => interp(t, (x, Fun(k)) :: e))
}
- def test(t: Term): String =
+ def test(t: Term): String =
showM(interp(t, List()));
val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11)));
diff --git a/docs/examples/monads/directInterpreter.scala b/docs/examples/monads/directInterpreter.scala
index 06fffba8e2..d8ca8ccfa7 100644
--- a/docs/examples/monads/directInterpreter.scala
+++ b/docs/examples/monads/directInterpreter.scala
@@ -20,15 +20,15 @@ object directInterpreter {
case Fun(f) => "<function>"
}
- type Environment = List[Pair[Name, Value]];
+ type Environment = List[Tuple2[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)
+ case (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)
+ def add(a: Value, b: Value): Value = (a, b) match {
+ case (Num(m), Num(n)) => Num(m + n)
case _ => Wrong
}
@@ -41,15 +41,15 @@ object directInterpreter {
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 Lam(x, t) => Fun(a => interp(t, (x, a) :: e))
case App(f, t) => apply(interp(f, e), interp(t, e))
}
- def test(t: Term): String =
+ 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]) =
+ def main(args: Array[String]) =
System.out.println(test(term0));
}
diff --git a/docs/examples/monads/errorInterpreter.scala b/docs/examples/monads/errorInterpreter.scala
index d3cc45627d..c15e1041e2 100644
--- a/docs/examples/monads/errorInterpreter.scala
+++ b/docs/examples/monads/errorInterpreter.scala
@@ -41,15 +41,15 @@ object errorInterpreter {
override def toString() = "<function>"
}
- type Environment = List[Pair[Name, Value]]
+ type Environment = List[Tuple2[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)
+ case (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))
+ def add(a: Value, b: Value): M[Value] = (a, b) match {
+ case (Num(m), Num(n)) => unitM(Num(m + n))
case _ => errorM("should be numbers: " + a + "," + b)
}
@@ -65,7 +65,7 @@ object errorInterpreter {
b <- interp(r, e);
c <- add(a, b))
yield c
- case Lam(x, t) => unitM(Fun(a => interp(t, Pair(x, a) :: e)))
+ case Lam(x, t) => unitM(Fun(a => interp(t, (x, a) :: e)))
case App(f, t) => for (a <- interp(f, e);
b <- interp(t, e);
c <- apply(a, b))
diff --git a/docs/examples/monads/simpleInterpreter.scala b/docs/examples/monads/simpleInterpreter.scala
index cde3a92dbb..64636749ff 100644
--- a/docs/examples/monads/simpleInterpreter.scala
+++ b/docs/examples/monads/simpleInterpreter.scala
@@ -22,7 +22,7 @@ object simpleInterpreter {
trait Value;
case object Wrong extends Value {
override def toString() = "wrong"
- }
+ }
case class Num(n: Int) extends Value {
override def toString() = n.toString();
}
@@ -30,15 +30,15 @@ object simpleInterpreter {
override def toString() = "<function>"
}
- type Environment = List[Pair[Name, Value]];
+ type Environment = List[Tuple2[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)
+ case (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))
+ def add(a: Value, b: Value): M[Value] = (a, b) match {
+ case (Num(m), Num(n)) => unitM(Num(m + n))
case _ => unitM(Wrong)
}
@@ -54,14 +54,14 @@ object simpleInterpreter {
b <- interp(r, e);
c <- add(a, b))
yield c
- case Lam(x, t) => unitM(Fun(a => interp(t, Pair(x, a) :: e)))
+ case Lam(x, t) => unitM(Fun(a => interp(t, (x, a) :: e)))
case App(f, t) => for (a <- interp(f, e);
b <- interp(t, e);
c <- apply(a, b))
yield c
}
- def test(t: Term): String =
+ def test(t: Term): String =
showM(interp(t, List()));
val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11)));
diff --git a/docs/examples/monads/stateInterpreter.scala b/docs/examples/monads/stateInterpreter.scala
index 97f3335dab..e13e9049da 100644
--- a/docs/examples/monads/stateInterpreter.scala
+++ b/docs/examples/monads/stateInterpreter.scala
@@ -4,20 +4,20 @@ object stateInterpreter {
type State = Int;
- val tickS = new M(s => Pair((), s + 1));
+ val tickS = new M(s => ((), 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
+ case class M[A](in: State => Tuple2[A, State]) {
+ def bind[B](k: A => M[B]) = M[B]{ s0 =>
+ val (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 unitM[A](a: A) = M[A](s => (a, s));
def showM(m: M[Value]): String = {
- val Pair(a, s1) = m in 0;
+ val (a, s1) = m in 0;
"Value: " + a + "; Count: " + s1
}
@@ -41,15 +41,15 @@ object stateInterpreter {
override def toString() = "<function>"
}
- type Environment = List[Pair[Name, Value]];
+ type Environment = List[Tuple2[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)
+ case (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 (_ <- tickS) yield Num(m + n)
+ def add(a: Value, b: Value): M[Value] = (a, b) match {
+ case (Num(m), Num(n)) => for (_ <- tickS) yield Num(m + n)
case _ => unitM(Wrong)
}
@@ -65,14 +65,14 @@ object stateInterpreter {
b <- interp(r, e);
c <- add(a, b))
yield c
- case Lam(x, t) => unitM(Fun(a => interp(t, Pair(x, a) :: e)))
+ case Lam(x, t) => unitM(Fun(a => interp(t, (x, a) :: e)))
case App(f, t) => for (a <- interp(f, e);
b <- interp(t, e);
c <- apply(a, b))
yield c
}
- def test(t: Term): String =
+ def test(t: Term): String =
showM(interp(t, List()));
val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11)));