[Haskell-cafe] Data structure containing elements which are instances of the same type class

Daniel Trstenjak daniel.trstenjak at gmail.com
Tue Aug 7 21:12:44 CEST 2012


Hi Joey,

On Tue, Aug 07, 2012 at 02:13:09PM -0400, Joey Adams wrote:
> Are you looking for existential quantification [1]?
> 
>     data SomeFoo = forall a. Foo a => a
> 
>  [1]: http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#existential-quantification

Thanks! Yes, that looks really nice. :)

data A = A deriving Show
data B = B deriving Show
data C = C deriving Show

data Foo = forall a. Show a => MkFoo a (Int -> Bool)

instance Show Foo where
   show (MkFoo a f) = show a

hasId foos id = filter (\(MkFoo a f) -> f id) foos

*Main> let foos = [MkFoo A (==1), MkFoo B (==2), MkFoo C (==3)]
*Main> hasId foos 1
[A]


Greetings,
Daniel



More information about the Haskell-Cafe mailing list