[Haskell-cafe] Help understanding type error

Derek Elkins derek.a.elkins at gmail.com
Fri Sep 7 22:46:01 EDT 2007


On Sat, 2007-09-08 at 12:24 +1000, Stuart Cook wrote:
> On 9/8/07, Ryan Ingram <ryani.spam at gmail.com> wrote:
> > This does what you want, I think:
> >
> > {-# LANGUAGE ExistentialQuantification #-}
> > module Exist where
> >
> > data Showable = forall a. (Show a) => Showable a
> > instance Show Showable where
> >    showsPrec p (Showable a) = showsPrec p a
> >    show (Showable a) = show a
> >    -- You have to use the default implementation of showList
> >    -- because a list could be heterogeneous
> >
> > data T a = forall b. (Show b) => T b a
> >
> > extShow :: T a -> Showable
> > extShow (T b _) = Showable b
> 
> Wow, I'm impressed! Making the existential wrapper an instance of its
> own typeclass solves quite a few problems.
> 
> While the idiom is obvious in hindsight, I don't think I've seen it
> documented before. (Looking around just now I found some oblique
> references to the technique, but nothing that really called attention
> to itself.)

It's documented on the old wiki... and in the papers that introduce
local existential types I believe.



More information about the Haskell-Cafe mailing list