Skip to content

Commit fc58463

Browse files
committed
Add test to close scala#3666
1 parent 4cfffbe commit fc58463

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/pos/i3666.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
object i3666 {
2+
sealed trait Exp[T]
3+
case class Num(n: Int) extends Exp[Int]
4+
case class Plus(e1: Exp[Int], e2: Exp[Int]) extends Exp[Int]
5+
case class Var[T](name: String) extends Exp[T]
6+
case class Lambda[T, U](x: Var[T], e: Exp[U]) extends Exp[T => U]
7+
case class App[T, U](f: Exp[T => U], e: Exp[T]) extends Exp[U]
8+
9+
abstract class Env { outer =>
10+
def apply[T](x: Var[T]): T
11+
12+
def + [T](xe: (Var[T], T)) = new Env {
13+
def apply[T](x: Var[T]): T =
14+
if (x == xe._1) xe._2.asInstanceOf[T]
15+
else outer(x)
16+
}
17+
}
18+
19+
object Env {
20+
val empty = new Env {
21+
def apply[T](x: Var[T]): T = ???
22+
}
23+
}
24+
25+
object Test {
26+
27+
val exp = App(Lambda(Var[Int]("x"), Plus(Var[Int]("x"), Num(1))), Var[Int]("2"))
28+
29+
def eval[T](e: Exp[T])(env: Env): T = e match {
30+
case Num(n) => n
31+
case Plus(e1, e2) => eval(e1)(env) + eval(e2)(env)
32+
//case v: Var[T] => env(v)
33+
case v: Var[_] =>
34+
val w: Var[Nothing] = w
35+
env(v)
36+
case Lambda(x: Var[s], e) => ((y: s) => eval(e)(env + (x -> y)))
37+
case App(f, e) => eval(f)(env)(eval(e)(env))
38+
}
39+
40+
eval(exp)(Env.empty)
41+
}
42+
}

0 commit comments

Comments
 (0)