public
Last active

Fun with Applicative, Monad and Monoidal

  • Download Gist
Applicative.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
-- See http://blog.ezyang.com/2012/08/applicative-functors/
module Main
where
 
import Prelude hiding ((**))
import Control.Monad
 
bind :: Monad m => m a -> (a -> m b) -> m b
bind = (>>=)
 
-- join :: Monad m => m (m a) -> m a
 
join_via_bind :: Monad m => m (m a) -> m a
join_via_bind = (>>= id)
 
bind_via_join :: (Functor m, Monad m) => m a -> (a -> m b) -> m b
bind_via_join ma f = join $ fmap f ma
 
class Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
 
class Functor f => Monoidal f where
unit :: f ()
(**) :: f a -> f b -> f (a,b)
 
-- Functor f => (a -> b) -> f a -> f b
 
pure_via_monoidal :: Monoidal f => a -> f a
pure_via_monoidal a = fmap (const a) unit
 
star_via_monoidal :: Monoidal f => f (a -> b) -> f a -> f b
star_via_monoidal ff fa = fmap (\(f,a) -> f a) (ff ** fa)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.