Here is a design problem I have faced repeatedly. Suppose you’re building a compiler, how do you store the types in the trees?
Consider a simple Expr and Type hierarchy, and assume that Plus and Equals are polymorphic (plus on booleans in just ||, for instance).
trait Type
case object BoolType extends Type
case object IntType extends Type
case object Untyped extends Type
trait Expr { var tpe : Type = Untyped }
case class Var(id : String) extends Expr
case class Plus(l : Expr, r : Expr) extends Expr
case class Equals(l : Expr, r : Expr) extends Expr
// ...
Assume further that I do not know the type of identifiers when I construct the expression trees, and therefore cannot know the type by construction.
Now a typical typechecking function could look like this:
def typeCheck(env : Map[String,Type])(expr : Expr) : Expr = expr match {
case Var(id) =>
expr.tpe = env(id)
expr
case Plus(l,r) =>
val tl = typeCheck(env)(l)
val tr = typeCheck(env)(r)
assert(tl == tr)
expr.tpe = tl
expr
// etc.
}
This is rather straightforward to write, but comes with two major problems:
Exprs are mutable. No one likes mutation.- Typed and untyped expressions cannot be distinguished. I cannot write a function whose signature specifies that its argument must be a typed expression.
So my question is the following. What is a good way (I dare not say design pattern) to define possibly untyped trees such that:
- I need to define the
Exprhierarchy only once. - Typed and untyped trees have distinct types and I can choose to make them incompatible.
Edit: One more requirement is that it should work for type systems with an unbounded and unpredictable number of types (think: case class ClassType(classID : String) extends Type, for instance).
This is a perfect use-case for type-level programming!
First, we need a type-level
Optionso that we can represent untyped trees in terms of type-levelNoneand typed trees of typeXin terms of type-levelSome[X]:Next, we lay out our type system hierarchy:
Now, all that’s left is to declare our expression tree:
EDIT:
A simple but complete type-checking function: