Proposal: merge either into transformers

Ross Paterson ross at soi.city.ac.uk
Wed Dec 12 17:05:05 CET 2012


On Sun, Dec 09, 2012 at 01:24:36AM +0000, Ross Paterson wrote:
> A more economical variation on this idea would be to lift these classes
> to functors, e.g.
> 
> class ShowF f where
>     showsPrecF :: Show a => Int -> f a -> ShowS
> 
> instance (ShowF m, Show e, Show a) => Show (EitherT e m a) where
>     showsPrec d (EitherT m) = showParen (d > 10) $
>         showString "EitherT " . showsPrecF 11 m
> 
> instance (ShowF m, Show e) => ShowF (EitherT e m) where
>     showsPrecF = showsPrec

To also lift Prelude classes like Show to Compose (which seems
desirable), we'd need to use an explicit dictionary instead:

class ShowF f where
    showsPrecF :: (Int -> a -> ShowS) -> Int -> f a -> ShowS

instance (ShowF f, ShowF g) => ShowF (Compose f g) where
    showsPrecF sp d (Compose x) = showParen (d > 10) $
        showString "Compose " . showsPrecF (showsPrecF sp) 11 x

instance (ShowF f, ShowF g, Show a) => Show (Compose f g a) where
    showsPrec = showsPrecF showsPrec



More information about the Libraries mailing list