diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-12-10 11:14:46 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-12-10 11:14:46 -0800 |
commit | f40541788957c257ee8b4035e0063a8a48945c11 (patch) | |
tree | df574bd0aefe1e809fb890e42cdf2bf189659cfa /docs/examples/monads/simpleInterpreter.scala | |
parent | 0c927046dc5df974e6c39187107cf3548825282b (diff) | |
parent | 27a38602decb891399fc9868b4dd7135f6e1ec2c (diff) | |
download | scala-f40541788957c257ee8b4035e0063a8a48945c11.tar.gz scala-f40541788957c257ee8b4035e0063a8a48945c11.tar.bz2 scala-f40541788957c257ee8b4035e0063a8a48945c11.zip |
Merge pull request #3248 from adriaanm/dist-cleanup-licenses
Clean up Scala distribution a bit.
Diffstat (limited to 'docs/examples/monads/simpleInterpreter.scala')
-rw-r--r-- | docs/examples/monads/simpleInterpreter.scala | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/docs/examples/monads/simpleInterpreter.scala b/docs/examples/monads/simpleInterpreter.scala deleted file mode 100644 index cde3a92dbb..0000000000 --- a/docs/examples/monads/simpleInterpreter.scala +++ /dev/null @@ -1,75 +0,0 @@ -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 (a <- interp(l, e); - b <- interp(r, e); - c <- add(a, b)) - yield c - case Lam(x, t) => unitM(Fun(a => interp(t, Pair(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 = - 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)); - } -} - |