5

这个要点(这个Haskell 无标记解释器的部分 Scala 端口)用scalac2.11.1编译,但在更新的2.11.6 中失败:

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 -printscalac -Xprint-types没有发现它们有帮助。

查看完整代码的要点,但大致上,我们有

// 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[(:::[_,_], :::[_,_])] =
    ...
4

0 回答 0