|
|
|
|
|
by chowells
3251 days ago
|
|
In general, type reflection does mean making type information available at runtime. However, GHC does erase types at compile time. The resolution of this apparently contradiction is in the type class mechanism. Type classes ("classes" for short) can be seen as functions from type to value that are automatically resolved by the compiler as much as possible. (There are many other semantically-valid interpretations, like predicates on types, proof obligations, etc.) Most classes are library-level constructs, defined and implemented entirely with libraries, but there are a few special ones that are completely managed by the compiler. One of the few compiler-managed classes is named Typeable, which allows you to request a runtime representation of the type of an expression. It then provides tools for testing if the runtime representations correspond to the same type, and proving two types are the same at runtime. This is useful in the case where you can prove something is type safe, but the type system doesn't provide you with the tools to express it. Common cases where you end up doing this in Haskell are creating heterogeneous stores with type-safe keys, or mechanisms like GHC's exception system. Anyway, the new part here is the addition of a variant of Typeable. It gives more information at compile time so that it can enable a few more tricks within the type system and the implementation doesn't depend on as many unsafe internal primitives. |
|