[Haskell-cafe] DSL for data definition (e.g. compiling Haskell type defs into Google's protocol buffers type defs)

Erik Hesselink hesselink at gmail.com
Tue Oct 4 23:35:41 CEST 2011

On Tue, Oct 4, 2011 at 20:33, Karel Gardas <karel.gardas at centrum.cz> wrote:
> data PersonType = Person {
>        id :: Int
>        , name :: String
>        , email :: Maybe String
>        }
>        deriving (Show, Data, Typeable)
> so I have `PersonType' as type constructor and Person as value constructor
> (or data constructor) -- speaking using terms from Real World Haskell,
> Chapter 3[1]. And now I see that none of typeOf/dataTypeOf/toContr is
> applicable to *type constructor* but all are applicable to *value/data
> constructor*. Ditto happen when testing Color versus RED, GREEN, BLUE. At
> least GHCi complains this way:
> *Main> typeOf Color
> <interactive>:0:8: Not in scope: data constructor `Color'
> *Main> typeOf PersonType
> <interactive>:0:8: Not in scope: data constructor `PersonType'
> But, I'd like to start processing of data definition from the *type
> constructor*. So:
> emit_proto PersonType 1
> emit_proto Color 1
> Is that possible at all? I mean in the scope/context of GHC's
> Data/Data.Data/Data.Typeable etc. modules. (w/o considering TH now).

A definition of 'typeOf' is not supposed to use its argument, since
the normal way to call it is to pass undefined. The documentation

"The value of the argument should be ignored by any instance of
Typeable, so that it is safe to pass undefined as the argument. "

So you should call it like:

typeOf (undefined :: PersonType).


More information about the Haskell-Cafe mailing list