[Haskell-cafe] Why Kleisli composition is not in the Monad signature?

Andreas Abel andreas.abel at ifi.lmu.de
Mon Oct 22 18:45:42 CEST 2012


When I teach my students Haskell's monads, I never put "Kleisli" into my 
mouth.  I tell them that monads are for sequencing effects; and the 
sequencing is visible clearly in

   (>>)  :: IO a -> IO b -> IO b
   (>>=) :: IO a -> (a -> IO b) -> IO b

but not in

   fmap :: (a -> b) -> IO a -> IO b
   join :: IO (IO a) -> IO a

To me,

   print 1 >> print 2

looks natural, and

   print 1 >>= const (print 2)

still understandable, but

   join $ fmap (const (print 2)) (print 1)

rather not (I needed ghci to get this example right).

I would not want to introduce category theory or Kleisli composition 
just to teach my students some effectful Haskell programming.

Cheers,
Andreas

On 16.10.2012 21:45, Simon Thompson wrote:
>
> Not sure I really have anything substantial to contribute, but it's certainly true that if you see
>
>    a -> m b
>
> as a generalisation of the usual function type, a -> b, then return generalises the identity and
> kleisli generalises function composition. This makes the types pretty memorable (and often the
> definitions too).
>
> Simon
>
>
> On 16 Oct 2012, at 20:14, David Thomas <davidleothomas at gmail.com> wrote:
>
>>> class Monad m where
>>>   return :: a -> m a
>>>   kleisli :: (a -> m b) -> (b -> m c) -> (a -> m c)
>
> Simon Thompson | Professor of Logic and Computation
> School of Computing | University of Kent | Canterbury, CT2 7NF, UK
> s.j.thompson at kent.ac.uk | M +44 7986 085754 | W www.cs.kent.ac.uk/~sjt
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


-- 
Andreas Abel  <><      Du bist der geliebte Mensch.

Theoretical Computer Science, University of Munich
Oettingenstr. 67, D-80538 Munich, GERMANY

andreas.abel at ifi.lmu.de
http://www2.tcs.ifi.lmu.de/~abel/



More information about the Haskell-Cafe mailing list