このgist (このHaskell タグレス インタープリターの部分的な Scala ポート) は 2.11.1 でコンパイルされますが、新しい2.11.6scalac
では失敗します。
typechecker.scala:55: error: type mismatch;
found : Expr[B] where type B
required: Expr[Int]
case (lhs ::: RInt, rhs ::: RInt) => Add(lhs, rhs) ::: RInt
...
scalac
のパターンマッチを通じてどのように型を伝播します:::
か? 2.11.1
からへの変更2.11.6
点 scalac -print
andの出力を調べてscalac -Xprint-types
みましたが、役に立ちませんでした。
完全なコードについてはGistを参照してください。
// ADT for untyped expressions
sealed trait UExpr
case class UAdd(lhs: UExpr, rhs: UExpr) extends UExpr
// GADT for typed expressions
sealed trait Expr[T]
case class Add(lhs: Expr[Int], rhs: Expr[Int]) extends Expr[Int]
// Reification of types
sealed trait RType[T]
case object RInt extends RType[Int]
// Expression annotated with reified type
class :::[A,B](val expr: Expr[A], val typ: RType[B])(implicit witness: A === B)
object ::: {
def apply =
...
def unapply =
...
}
// typechecker from UExpr to Expr
def typed(uexpr: UExpr): Option[:::[_,_]] = uexpr match {
case UAdd(l, r) => typed(l, r) collect {
// vvvvvvvv HERE vvvvvvvv
case (lhs ::: RInt, rhs ::: RInt) => Add(lhs, rhs) ::: RInt
// ^^^^^^^^ HERE ^^^^^^^^
}
}
// helper for typing two expressions
def typed(lhs: UExpr, rhs: UExpr): Option[(:::[_,_], :::[_,_])] =
...