Type-safe cast - Functional pearl
In a language with non-parametric or ad-hoc polymorphism, it is possible to determine the identity of a type variable at run time. With this facility, we can write a function to convert a term from one abstract type to another, if the two hidden types are identical. However, the naive implementation of this function requires that the term be destructed and rebuilt. In this paper, we show how to eliminate this over-head using higher-order type abstraction. We demonstrate this solution in two frameworks for ad-hoc polymorphism intensional type analysis and type classes.
ad-hoc polymorphism intensional type analysis type classes dynamic typing