[Haskell-cafe] Data.Generics: how do I get the type name of a value?

Neil Mitchell ndmitchell at gmail.com
Fri Dec 14 09:15:13 EST 2007


Hi Alistair,

http://haskell.org/hoogle/?q=typeOf

C:\>ghci
GHCi, version 6.8.1: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> :m Data.Typeable
Prelude Data.Typeable> typeOf "neil"
[Char]

Thanks

Neil


On 12/14/07, Alistair Bayley <alistair at abayley.org> wrote:
> I've been learning/playing with Data.Generics a bit, and have a how-to
> question...
>
> If I say
> > dataTypeOf ""
> then I get
> > DataType {tycon = "Prelude.[]", datarep = AlgRep [[],(:)]}
>
> No surprises there. But I'd really like to know that I have a String,
> or [Char]. How do I get the name of the concrete type that the list
> contains? Is that a reasonable thing to ask for?
>
> I can say:
> > gmapQ dataTypeOf "a"
> to get:
> > [ DataType {tycon = "Prelude.Char", datarep = StringRep}
> > , DataType {tycon = "Prelude.[]", datarep = AlgRep [[],(:)]}
> > ]
>
> But if I say:
> > gmapQ dataTypeOf ""
> I get:
> > []
>
> which makes sense when you consider the stucture of the List ADT, but
> doesn't help me determine the type of the value.
>
> Alistair
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list