[Haskell-beginners] Heterogenous list implemented with GADT

Daniel Fischer daniel.is.fischer at web.de
Thu Sep 30 16:00:13 EDT 2010

On Thursday 30 September 2010 21:08:01, Ken Overton wrote:
> Hello fellow beginners (and the rest of you, too),  I was reading
> http://en.wikibooks.org/wiki/Haskell/GADT and I really liked the simple
> eval example.  But I reached the end and was confused by the following
> definition of a heterogenous list:
>         data TG2 where
>                 MkTG2 :: Show b => [b] -> TG2
> I had thought a heterogeneous list was something like
>         [ 5 , 'Z', 3.14, False ]
> How can the TG2 definition support the list I've given above?

It can't.

> Or am I just wrong about what a heterogeneous list is?

I rather think the wikibook author expressed (her|him)self incorrectly.
What TG2 does is wrap a list of (henceforth) unknown type (as long as the 
type has a Show instance, and that Show instance is made available by 
pattern matching).

To achieve something like your example, you'd need

data TG3 where
  MkTG3 :: Show a => a -> TG3

xs :: [TG3]
xs = [MkTG3 5, MkTG3 'Z', MkTG3 3.14, MkTG3 False]

> Thanks,
> -- kov

More information about the Beginners mailing list