diff options
Diffstat (limited to 'docs/examples/monads/stateInterpreter.scala')
-rw-r--r-- | docs/examples/monads/stateInterpreter.scala | 24 |
1 files changed, 12 insertions, 12 deletions
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))); |