4

Im a Haskell beginner and I'm still learning about Category Theory and its practical use in computer science.

I've spent last day watching couple lectures from Berkley's university about category theory, most of its content was showing a mathematical view of Rings, Semigroups, Groups, Magmas, Monoids, etc.

Hence, questions raised in my mind about monadic composition and kleisli category. Therefore, I would like to questions Haskell/Category Theory experts.

Is do notation a sort of monad composition?

Regards,

Pablo Parada

  • 4
    You don't really need Category theory for using most of the Haskell ecosystem. – Sibi Mar 01 '15 at 23:43
  • I would argue that `do` notation is sort of a happy accident. There is underlying mathematical theory to Monads - but a Monad is just the mathematical construct `(m, return :: forall a . a -> m a, bind :: forall a b . m a -> (a -> m b) -> m b)`. Do notation happens to be syntactic sugar which is compatible with the Monad structure; happens to look like a "stateful" control flow (which makes sense - monads are often used to model stateful computations) that is recognizable to programmers coming from imperative languages; and makes it easier (sometimes) to reason about monadic computations. – user2407038 Mar 02 '15 at 02:50

2 Answers2

10

Is do notation a sort of monad composition?

There is nothing special about do notation. It is just a syntax sugar over the monad functions. A nice example from Haskell wikibook:

do x1 <- action1
   x2 <- action2
   action3 x1 x2

De-sugars to:

action1
  >>=
    \ x1 -> action2
      >>=
        \ x2 -> action3 x1 x2

Real world haskell book has a nice section explaining how this de-sugaring happens in various scenarios.

Sibi
  • 47,472
  • 16
  • 95
  • 163
  • Maybe I couldn't express my question correctly. In a mathematical point of view, is do notation a monad composition? As you answered, the desugar looks like is function sequencing, not composition. – Pablo Parada Mar 02 '15 at 00:29
  • @PabloParada do notation gets converted by the compiler into the form in the answer, so those two ways of writing that code are exactly equivalent to each other. do notation is just another way of writing the calls to `>>=`. Also, usually when I think of monad composition in Haskell, I think of stuff at the type level like the list type composed with the `Maybe` type (`[Maybe a]`). – David Young Mar 02 '15 at 00:47
  • @DavidYoung Got what you said but what I'm trying to get is if do notation abstract composition for monadic structures. When I mean composition, is like function composition. e.g: (g o f )(1) = g( f(1)). – Pablo Parada Mar 02 '15 at 00:53
  • 3
    @PabloParada do notation is just a syntactic shorthand. What it actually does is call `>>=`. Because of the translation of do notation into `>>=` calls, that's exactly the same as asking if the `>>=` is monadic composition. It almost is, actually. There is a very similar function called `>=>` which is Kleisli composition. It is defined in terms of `>>=` here: http://haddocks.fpcomplete.com/fp/7.8/20140916-162/base/src/Control-Monad.html#line-178 . – David Young Mar 02 '15 at 02:03
4

Do notation is just syntactic sugar for >>=. Code such as

do x <- a
   b  -- b is an expression possibly involving x

is desugared to

a >>= \x -> b

If you are studying monads in CT, you will probably find that they are being defined as functors with two natural transformations

unit :: a -> m a        -- also known as η
join :: m (m a) -> m a  -- also known as μ

while Haskell defines

return :: a -> m a
(>>=)  :: m a -> (a -> m b) -> m b

Both presentations are equivalent. Indeed, unit and return are exactly the same thing. Instead, join can be expressed in terms of (>>=) as follows

join x = x >>= id

and, vice versa, (>>=) can be expressed in terms of join.

x >>= f = join (fmap f x)

above note that fmap takes a -> m b and m a to return m (m b), which is then flattened to m b by join.

chi
  • 111,837
  • 3
  • 133
  • 218