[Haskell-cafe] reifying based-on type of a newtype or data

AntC anthony_clayden at clear.net.nz
Tue Oct 29 00:15:21 UTC 2013

[reposted from Beginners, where it met stoney silence.]

So I have (or rather the user of my package has):

> {-# LANGUAGE  DeriveDataTypeable    #-}
>    newtype Foo = Foo Int    deriving (Read, Show, Typeable, Data, ...)
>    someFoo     = Foo 7

* the `newtype` could be `data` -- if that would help.
* this is _not_ a parameterised type, but a 'baked in' `Int`.
* the data constr is named same as the type -- if that would help.

I can ask for `typeOf someFoo` and get `Foo` OK.
I can ask for `typeOf Foo`  and get `Int -> Foo` OK.

If I ask for `typeOf (typeOf someFoo)` I get `TypeRep`.
`typeOf (show $ typeOf someFoo`) gets me `[Char]` (aka `String`)

So far very logical, but not very helpful.

What I want is to get the based-on type baked inside `someFoo`
-- that is: `Int`
(It would also be handy to get the name of the data constr, just in case 
it's different to the type.)

Do I need to get into `deriving (..., Generic)` ?

That looks like serious machinery!


More information about the Haskell-Cafe mailing list