No subject


Fri Sep 2 19:57:00 CEST 2011


> class Functor f where
>       fmap :: (a -> b) -> f a -> f b
>       default fmap :: Applicative f => (a -> b) -> f a -> f b
>       f `fmap` m = pure f <*> m
>       (<$) :: a -> f b -> f a
>       (<$) = fmap . const
> 
> class Functor f => Pointed f where
>       point :: a -> f a
>       default point :: Applicative f => a -> f a
>       point = pure
>
> class Pointed f => Applicative f where
>       pure :: a -> f a
>       default pure :: Monad f => a -> f a
>       pure = return
>       (<*>) :: f (a -> b) -> f a -> f b
>       default (<*>) :: Monad f => f (a -> b) -> f a -> f b
>       f <*> v = liftM2 ($) f v
>       (*>) :: f a -> f b -> f b
>       (*>) = liftA2 (const id)
>       (<*) :: f a -> f b -> f a
>       (<*) = liftA2 const
> 
> class Applicative f => Monad f where
>       return :: a -> f a
>       (>>=) :: f a -> (a -> f b) -> f b
>       (>>) :: f a -> f b -> f b
>       m >> k = m >>= const k

Then having:

> data List a = List a (List a) | Empty
>
> instance Monad List where
>       return x = List x Empty
>       Empty >>= _ = Empty
>       List v vs >>= f = List (f v) (vs >>= f)

is sufficient to use (in current GHC) to use:

test = Empty <*> List 1 Empty

Hence adding default instances + fixing the hierarchy seems to be
sufficient to have backward compatibility for instance declaration.

> > default fmap :: Applicative f => (a -> b) -> f a -> f b
> > f `fmap` m = pure f <*> m
> > default pure :: Monad f => a -> f a
> > pure = return
> > default (<*>) :: Monad f => f (a -> b) -> f a -> f b
> > (<*>) = liftM2 ($)
> 
> Duncan

Regards





More information about the Libraries mailing list