这是类型级编程的完美用例!
首先,我们需要一个类型级别Option
,以便我们可以根据类型级别表示无类型树,并根据类型级别表示类型的类型None
树:X
Some[X]
// We are restricting our type-level option to
// only (potentially) hold subtypes of `Type`.
sealed trait IsTyped
sealed trait Untyped extends IsTyped
sealed trait Typed[T <: Type] extends IsTyped
接下来,我们布置我们的类型系统层次结构:
sealed trait Type
// We can create complicated subhierarchies if we want.
sealed trait SimpleType extends Type
sealed trait CompoundType extends Type
sealed trait PrimitiveType extends Type
sealed trait UserType extends Type
// Declaring our types.
case object IntType extends SimpleType with PrimitiveType
case object BoolType extends SimpleType with PrimitiveType
// A type with unbounded attributes.
case class ClassType(classId: String) extends CompoundType with UserType
// A type that depends statically on another type.
case class ArrayType(elemType: Type) extends CompoundType with PrimitiveType
现在,剩下的就是声明我们的表达式树:
sealed trait Expr[IT <: IsTyped] { val getType: Option[Type] }
// Our actual expression types.
case class Var[IT <: IsTyped](id: String, override val getType: Option[Type] = None) extends Expr[IT]
case class Plus[IT <: IsTyped](l: Expr[IT], r: Expr[IT], override val getType: Option[Type] = None) extends Expr[IT]
case class Equals[IT <: IsTyped](l: Expr[IT], r: Expr[IT], override val getType: Option[Type] = None) extends Expr[IT]
case class ArrayLiteral[IT](elems: List[Expr[_ :< IsTyped]], override val getType: Option[Type] = None) extends Expr[IT]
编辑:
一个简单但完整的类型检查功能:
def typeCheck(expr: Expr[Untyped], env: Map[String, Type]): Option[Expr[Typed[_ :< Type]]] = expr match {
case Var(id, None) if env isDefinedAt id => Var[Typed[_ <: Type]](id, Some(env(id)))
case Plus(r, l, None) => for {
lt <- typeCheck(l, env)
IntType <- lt.getType
rt <- typeCheck(r, env)
IntType <- rt.getType
} yield Plus[Typed[IntType]](lt, rt, Some(IntType))
case Equals(r, l, None) => for {
lt <- typeCheck(l, env)
lType <- lt.getType
rt <- typeCheck(r, env)
rType <- rt.getType
if rType == lType
} yield Equals[Typed[BoolType]](lt, rt, Some(BoolType))
case ArrayLiteral(elems, None) => {
val elemst: List[Option[Expr[Typed[_ <: Type]]]] =
elems map { typeCheck(_, env) }
val elemType: Option[Type] = if (elemst.isEmpty) None else elemst map { elem =>
elem map { _.getType }
} reduce { (elemType1, elemType2) =>
for {
et1 <- elemType1
et2 <- elemType2
if et1 == et2
} yield et1
}
if (elemst forall { _.isDefined }) elemType map { et =>
ArrayLiteral[Typed[ArrayType]](elemst map { _.get }, ArrayType(et))
} else None
}
case _ => None
}