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

Heinrich Apfelmus apfelmus at quantentunnel.de
Sun Aug 12 09:24:17 CEST 2012

Antoine Latter wrote:
> It should be pretty easy to write an adapter function of type "String ->
> (Show a => a)".

The type needs to be

    String -> (exists a. Show a => a)

which is equivalent to

    String -> (forall a. Show a => a -> c) -> c

Here is the implementation of the adapter

    newtype ExistsShow = E { showE :: String }
    instance Show ExistsShow where
        show = showE

    withShow :: String -> (forall a. Show a => a -> c) -> c
    withShow s f = f (E s)

Essentially, the point is that the types are equivalent

    ExistsShow  ==  exists a. Show a => a

Best regards,
Heinrich Apfelmus


More information about the Haskell-Cafe mailing list