[Haskell-cafe] Reading open data types

Benja Fallenstein benja.fallenstein at gmail.com
Wed Jun 13 12:26:50 EDT 2007


Hi Isaac,

2007/6/13, Isaac Dupree <isaacdupree at charter.net>:
> Since Show instances can overlap (e.g. (show (1::Int)) == (show
> (1::Integer))), we need to tag with the type.

Indeed. But that's the easy part :-)

> Reminds me of Typeable.
> Since GHC lets us derive Typeable with a guarantee of different types
> being distinct (at least for a single compile-and-run session...), maybe
> that can be leveraged somehow?

We can perhaps use it to go from the type to the type tag, but as far
as I understand we can not, unfortunately, use it to go from the type
tag to the type. (We *may* be able to use the TypeRep as the key of
the map I proposed in my earlier mail, but we can't use it to replace
the map, afaiu.)

The problem is that, in the type system, a TypeRep is not actually
associated with the type that it represents, nor is there (afaik) an
internal table that associates TypeReps with the types they represent.

In Data.Generics, there are some functions that let you take a
DataTypeRep and use it to create a value of the corresponding type.
However, the code doesn't use the DataTypeRep to get the type. The
relevant function looks like this:

fromConstr :: Data a => Constr -> a

Like 'read' gets the 'Read' instance, this gets the 'Data' instance
from its return type (you have to call it in a context that constrains
the return type). So, you can *not* use it to write a function like
this:

data D = forall a. Data a => D a
fromConstrD :: Constr -> D

because, in the type system, you wouldn't be able to use the Constr to
get the Data instance corresponding to the Constr's TypeRep.

Hope that makes sense ... I'm not explaining it too well :-/

Thanks,
- Benja


More information about the Haskell-Cafe mailing list