-
Notifications
You must be signed in to change notification settings - Fork 5
/
hw-cbn.scala
66 lines (58 loc) · 2.1 KB
/
hw-cbn.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Here is some code from 06-eval.scala:
sealed abstract class Exp
case class Num(n: Int) extends Exp
case class Id(name: Symbol) extends Exp
case class Add(lhs: Exp, rhs: Exp) extends Exp
implicit def num2exp(n: Int) = Num(n)
implicit def id2exp(s: Symbol) = Id(s)
case class Fun(param: Symbol, body: Exp) extends Exp
case class App (funExpr: Exp, argExpr: Exp) extends Exp
def wth(x: Symbol, xdef: Exp, body: Exp) : Exp = App(Fun(x,body),xdef)
val test = App( Fun('x,Add('x,5)), 7)
val test2 = wth('x, 5, App(Fun('f, App('f,3)), Fun('y,Add('x,'y))))
sealed abstract class Value
type Env = Map[Symbol, Value]
case class NumV(n: Int) extends Value
case class ClosureV(f: Fun, env: Env) extends Value
def evalWithEnv(e: Exp, env: Env) : Value = e match {
case Num(n: Int) => NumV(n)
case Id(x) => env(x)
case Add(l,r) => {
(evalWithEnv(l,env), evalWithEnv(r,env)) match {
case (NumV(v1),NumV(v2)) => NumV(v1+v2)
case _ => sys.error("can only add numbers")
}
}
case f@Fun(param,body) => ClosureV(f, env)
case App(f,a) => evalWithEnv(f,env) match {
// Use environment stored in closure to realize proper lexical scoping!
case ClosureV(f,closureEnv) => evalWithEnv(f.body, closureEnv + (f.param -> evalWithEnv(a,env)))
case _ => sys.error("can only apply functions")
}
}
assert( evalWithEnv(test, Map.empty) == NumV(12))
assert( evalWithEnv(test2,Map.empty) == NumV(8))
// HOMEWORK ASSIGNMENT
// ===================
//
// Email homework as Scala source file to:
//
//
// Work in groups of 1 or 2 students. Send the email CC to the
// other student in your team. Hand in before the morning of
// Monday, December 8.
//
//
// Put "pl1-hw04" in subject, please
//
// 0. write in the email:
// - your names
// - your student ids ("Matrikelnummer")
// 1. Add a case class LazyFun and extend the interpreter so that
// LazyFun creates function with call-by-need evaluation order.
//
// About task 1: This is similar to how Scala has functions with
// and without the => annotation for call-by-name.
//
// Send question by email to [email protected]