What is Haskell's Data.Typeable?

HaskellTypeclassGeneric ProgrammingScrap Your-Boilerplate

Haskell Problem Overview


I've come across references to Haskell's Data.Typeable, but it's not clear to me why I would want to use it in my code.

What problem does it solve, and how?

Haskell Solutions


Solution 1 - Haskell

Data.Typeable is an encoding of an well known approach (see e.g. Harper) to implementing delayed (dynamic) type checking in a statically typed language -- using a universal type.

Such a type wraps code for which type checking would not succeed until a later phase. Rather than reject the program as ill-typed, the compiler passes it on for runtime checking.

The style originated in Abadi et al., and developed for Haskell by Cheney and Hinze as a wrapper to represent all dynamic types, with the Typeable class appearing as part of the SYB work of SPJ and Lammel.


Reference


Even in the text books: dynamic types (with typeable representations) are statically typed languages with only one type, Harper ch 20:

> ### 20.4 Untyped Means Uni-Typed

> The untyped λ-calculus may be faithfully embedded in a typed language with recursive types. This means that every untyped λ-term has a representation as a typed expression in such a way that execution of the representation of a λ-term corresponds to execution of the term itself. This embedding is not a matter of writing an interpreter for the λ-calculus in ℒ{+×⇀µ} (which we could surely do), but rather a direct representation of untyped λ-terms as typed expressions in a language with recursive types.

> The key observation is that the untyped λ-calculus is really the uni-typed λ-calculus! It is not the absence of types that gives it its power, but rather that it has only one type, namely the recursive type

> D = µt.t → t.

Solution 2 - Haskell

It's a library that allows, among other things, naming types. If a type a is declared Typeable, then you can get its name using show $ typeOf x where x is any value of type a. It also features limited type-casting.

(This is somewhat similar to C++'s RTTI or dynamic languages' reflection.)

Solution 3 - Haskell

One of the earliest descriptions I could find of a Data.Typeable-like library for Haskell is by John Peterson from 1992: http://www.cs.yale.edu/publications/techreports/tr1022.pdf

The earliest "official" paper I know of introducing the actual Data.Typeable library is the first Scrap Your Boilerplate paper from 2003: http://research.microsoft.com/en-us/um/people/simonpj/Papers/hmap/index.htm

I'm sure there's lots of intervening history that someone here can chime in with!

Solution 4 - Haskell

The Data.Typeable class is used primarily for generic programming in the Scrap Your Boilerplate (SYB) style. See also Data.Data

The idea is that SYB defines a collection combinators for performing operations such as printing, counting, searching, substiting, etc in a uniform manner over a variety of user-created types. The Typeable typeclass provides the necessary plumbing.

In modern GHC, you can just say deriving Data.Typeable when defining your own type in order to provide it with the necessary instances.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuinView Question on Stackoverflow
Solution 1 - HaskellDon StewartView Answer on Stackoverflow
Solution 2 - HaskellFred FooView Answer on Stackoverflow
Solution 3 - HaskellsclvView Answer on Stackoverflow
Solution 4 - HaskellLambdageekView Answer on Stackoverflow