Haskell for Elm developers: giving names to stuff (part 3 - Monads! šŸ‘»)

Hey!

The third post in the series is ready, this time finally tackling Monads! :monkey:

I really appreciate how friendly and welcoming the Elm community always is, thank you all in advance for all your positive and constructive feedback and please let me know if something is unclear or you would have expressed it in a better way!

15 Likes

Iā€™ve really enjoyed reading all 3 of these articles, theyā€™re very clear and build up the concepts gradually, thanks for writing them. Iā€™ve read about most of the concepts before, but they havenā€™t stuck because I didnā€™t understand why these properties were important. The Elm examples gave me a bit more context, so hopefully theyā€™ll stick better this time.

I have an esoteric question regarding Monad definition: you need >> to define the monad, but if >> can be defined in terms of >>=, why do you need to define >>? Why do we even care about Mr Pointy at all, isnā€™t it just a specific case of bind?

1 Like

Thanks very much for your nice words! :star_struck:

Actually that is a great question! As you mentioned, since defining >>= we get >> why should we care? Well we could say it is just an utility function in case we just want to perform some computation and discard the value out of it, for example in the following code:

sequencing :: IO ()
sequencing = do
  putStrLn "blah" >>
  putStrLn "another thing"

We have not covered yet the IO monad in the series, but what this is doing is performing side effects (this is like a console.log in Haskell) one after the other and discarding their values because we do not care about them, we just want to perform the action of logging to the console.

Hope it is more clear now but do not worry, the important part about Monads is just the >>=! :wink:

Canā€™t wait for more!:raised_hands: Iā€™d love to see something related about state handling. State, Writer and Reader monads in Haskell seem over-engineered sometimes :thinking:

1 Like

Sometimes, specialized >> can have better performance characteristics than bind with nullary function. If you donā€™t use results of an action, you can run those actions in parallel instead of sequencing them!

Also there was this idea that using class constraints we could reuse *> from Applicative. Some nasty bugs happen with legacy code and decision was reverted. We didnā€™t have Applicative class at the start!

1 Like

Elm Program is a mix of Reader/Writer/State monad. We have internal state, environment defined as flags and some ways to output data.

1 Like

Sure, and in Elm it seems so intuitive to me. I mean, I love Haskell as much as Elm, but I think it has too many ways of doing things, and for beginners (like me) implementing state can become tediousā€¦

My point is, Iā€™m not brilliant, I like to work with things that are easy to reason about, and I think something like Elm state handling would fit very well in Haskell. But if this way to do it is not the norm, then I guess itā€™s because itā€™s not needed, or itā€™s unviable in Haskell, so who knowsā€¦ :man_shrugging:

1 Like

You mean wrapping everything inside one monad? You can do this if you want.

But thatā€™s not good default solution for Haskell, because itā€™s general purpose language in contrast to Elm.

1 Like

Wow, itā€™s just there for *gulp*ā€¦ practical reasons? This is a revelation to me, I always assume with Haskell that everything is there for some complex reason I donā€™t understand, probably something to do with type theory. This combined with the ā€œpureā€ vs ā€œreturnā€ history and the simple explanation of forall in the article that was linked in the post, both of which confused me, has taken some of Haskellā€™s mystique away.

2 Likes

hahaha yes!!

In fact, as Haskell was born first as a research language, it did not even have a way to perform side effects at the beginning! The IO monad was ā€œdiscovered/inventedā€ later to be able to do useful stuff with Haskell, and the hierarchy described in the series of blogpost wasnā€™t like that from the beginning!

Thatā€™s why we have sometimes duplicity or redundant functions and because of backwards compatibility the String type sucks, for example! Definitely not mysterious but a language in constant evolution! :sunglasses:

1 Like

You wrote:

The only thing the Mr. Pointy operator does is sequencing two actions while discarding any result value of the first action.

I think there is a subtle error in that statement.

If m is a Monad then the statement follows but by the type of >> alone you cannot make that statement. The implementer of >>= has to ensure that the instance satisfies the Monad laws so that >> will work in the way we want it to work. All this to say that the Monad laws are a vital part of it all.

Why I stress this is because Iā€™ve seen people write andThen functions in Elm that donā€™t satisfy the monad laws. In practice it means you canā€™t make certain transformations on your code because the reasoning will be flawed.

Thanks for your input, actually, that line is almost identical to this one in the implementation in GHC.Base:

  -- | Sequentially compose two actions, discarding any value produced
  -- by the first, like sequencing operators (such as the semicolon)
  -- in imperative languages.

So I do not think it is wrong :sweat_smile:

Not wrong, just a subtlety that I thought needed further explanation.

any chance to have a post related to websocket and coroutine in haskell, would be much much much appreciated!!! tks for your work ~

1 Like

Very interesting point about >> and *> that @Iceland_Jack pointed out in Reddit just now! :100:

1 Like

just a bit of warning about the ā€œparallelā€ - you donā€™t want this with monads at all - indeed monads are one way of making sure that you ā€œsequenceā€ the operations (order matters) - take this example here - you donā€™t want to run both prints in parallel (you really want one thing to write to stdout at the same time) and you want to keep the order.

It might be a bit hidden, but this sequencing is build into the monad via the type of >>= - as with generic types and a >>= b >>= c you really can only evaluate b (with itā€™s side effects) once youā€™ve got an a-Result and similar with c.

(Semi-Groups)/Monoids are where youā€™ll want to look for parallel opportunities as these are pure but still come with an associativity law - so you can rearange

a <> b <> c <> d

into

(a <> b) <> (c <> d)

and do a <> b and c <> d in parallel before combining the results of those

2 Likes

That depends on the monad instance. There are no monad laws about >> operator because it is really not an operator for categorical monad. So it is really a choice dictated by semantics of specific monad instance.

Some instances treat >> as a shortcut to Applicative. Itā€™s kinda iffy because of all that backwards compatibility and lack of constraint in monad class on Applicative.

Your example is absolutely valid. But thatā€™s result of IO semantics. Itā€™s not a coincidence that IO has no Applicative instance.

1 Like

What? I think we are talking about different things here - assuming you talk about Haskells IO than this is wrong (see here for example) and it has to be wrong for quite some time (Monad should always have Applicative as itā€™s superclass - this is straight from math - and Haskell fixed this a while back)

PS: Iā€™ve never heard of >> as anything other then sequencing - here is the description for it from Haskells prelude

Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.

1 Like

Yup! I am wrong on this one, I jumped in my mind multiple times and lost context. Sorry for the confusion.

On the bigger picture I was thinking about old proposal where >> could be implemented by default as *> from Applicative. And possibilities of static analysis - you could ā€œparalleliseā€ or ā€œskipā€ some effects if you can ensure that theyā€™re not needed. I canā€™t share any production code here so letā€™s say I step back from the argument and return with better examples.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.