Proposal: merge either into transformers
Henning Thielemann
lemming at henning-thielemann.de
Wed Dec 12 18:20:06 CET 2012
On Wed, 12 Dec 2012, Ross Paterson wrote:
> 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
If you tolerate a (Functor f) constraint you may use a helper type, that
lifts (ShowF f, Show a) to (Show (f a)):
class ShowF f where
showsPrecF :: (Show a) => Int -> f a -> ShowS
newtype Inner g a = Inner (g a)
instance (ShowF g, Show a) => Show (Inner g a) where
showsPrec p (Inner g) = showsPrecF p g
instance (Functor f, ShowF f, ShowF g) => ShowF (Compose f g) where
showsPrecF d (Compose x) = showParen (d > 10) $
showString "Compose " . showsPrecF 11 (fmap Inner x)
instance (Functor f, ShowF f, ShowF g, Show a) => Show (Compose f g a) where
showsPrec = showsPrecF
You may also make Functor a superclass of ShowF.
More information about the Libraries
mailing list