Skip to content

Commit

Permalink
Add test to close scala#3666
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaisorblade committed Mar 4, 2018
1 parent 4cfffbe commit fc58463
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/pos/i3666.scala
@@ -0,0 +1,42 @@
object i3666 {
sealed trait Exp[T]
case class Num(n: Int) extends Exp[Int]
case class Plus(e1: Exp[Int], e2: Exp[Int]) extends Exp[Int]
case class Var[T](name: String) extends Exp[T]
case class Lambda[T, U](x: Var[T], e: Exp[U]) extends Exp[T => U]
case class App[T, U](f: Exp[T => U], e: Exp[T]) extends Exp[U]

abstract class Env { outer =>
def apply[T](x: Var[T]): T

def + [T](xe: (Var[T], T)) = new Env {
def apply[T](x: Var[T]): T =
if (x == xe._1) xe._2.asInstanceOf[T]
else outer(x)
}
}

object Env {
val empty = new Env {
def apply[T](x: Var[T]): T = ???
}
}

object Test {

val exp = App(Lambda(Var[Int]("x"), Plus(Var[Int]("x"), Num(1))), Var[Int]("2"))

def eval[T](e: Exp[T])(env: Env): T = e match {
case Num(n) => n
case Plus(e1, e2) => eval(e1)(env) + eval(e2)(env)
//case v: Var[T] => env(v)
case v: Var[_] =>
val w: Var[Nothing] = w
env(v)
case Lambda(x: Var[s], e) => ((y: s) => eval(e)(env + (x -> y)))
case App(f, e) => eval(f)(env)(eval(e)(env))
}

eval(exp)(Env.empty)
}
}

0 comments on commit fc58463

Please sign in to comment.