~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to docs/examples/monads/directInterpreter.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
object directInterpreter {
2
 
 
3
 
  type Name = String;
4
 
 
5
 
  trait Term;
6
 
  case class Var(x: Name) extends Term;
7
 
  case class Con(n: Int) extends Term;
8
 
  case class Add(l: Term, r: Term) extends Term;
9
 
  case class Lam(x: Name, body: Term) extends Term;
10
 
  case class App(fun: Term, arg: Term) extends Term;
11
 
 
12
 
  trait Value;
13
 
  case object Wrong extends Value;
14
 
  case class Num(n: Int) extends Value;
15
 
  case class Fun(f: Value => Value)extends Value;
16
 
 
17
 
  def showval(v: Value): String = v match {
18
 
    case Wrong => "<wrong>"
19
 
    case Num(n) => n.toString()
20
 
    case Fun(f) => "<function>"
21
 
  }
22
 
 
23
 
  type Environment = List[Pair[Name, Value]];
24
 
 
25
 
  def lookup(x: Name, e: Environment): Value = e match {
26
 
    case List() => Wrong
27
 
    case Pair(y, b) :: e1 => if (x == y) b else lookup(x, e1)
28
 
  }
29
 
 
30
 
  def add(a: Value, b: Value): Value = Pair(a, b) match {
31
 
    case Pair(Num(m), Num(n)) => Num(m + n)
32
 
    case _ => Wrong
33
 
  }
34
 
 
35
 
  def apply(a: Value, b: Value) = a match {
36
 
    case Fun(k) => k(b)
37
 
    case _ => Wrong
38
 
  }
39
 
 
40
 
  def interp(t: Term, e: Environment): Value = t match {
41
 
    case Var(x) => lookup(x, e)
42
 
    case Con(n) => Num(n)
43
 
    case Add(l, r) => add(interp(l, e), interp(r, e))
44
 
    case Lam(x, t) => Fun(a => interp(t, Pair(x, a) :: e))
45
 
    case App(f, t) => apply(interp(f, e), interp(t, e))
46
 
  }
47
 
 
48
 
  def test(t: Term): String = 
49
 
    showval(interp(t, List()));
50
 
 
51
 
  val term0 = App(Lam("x", Add(Var("x"), Var("x"))), Add(Con(10), Con(11)));
52
 
 
53
 
  def main(args: Array[String]) = 
54
 
    System.out.println(test(term0));
55
 
}