Pure Monad Stacks in Elm

I finally published elm-stax 1.0.0 which is more philosophically interesting than useful, but it shows that Monad Transformers are not as necessary as one might think. Monad Transformers in Haskell/PureScript allow one to construct monad stacks out of transformers ReaderT, WriterT, StateT, ExceptT, MaybeT with either Identity at the base for pure or IO (Haskell) or Effect/Aff(PureScript) for effectful. So if considering pure stacks only, the truth is that there are only about 19 useful / reasonable combinations! :laughing: So what occurred to me is that if you offer just TWO stacks Reader/Writer/State/Except and Reader/Writer/State you get all the combinations by just choosing one or the other and plugging in Unit () for the ones you don’t care about! And using () as your Error basically gives you MaybeT.

So it is a neat example of “This thing is really powerful in Haskell… oh but you can kinda do 80% of it in Elm with great error messages and blazing fast compile times.”. It is also useful if you’ve ever heard the term Monad Transformer or Monad Stack and thought it was some scary thing. It isn’t. You can go look at the boneheaded code in my library. Now, it isn’t a transformer library per se. It is a concrete hard coded implementation of two stacks, but you can still see how it is possible to layer together multiple monads.

This code has been sitting on my computer for three years and I finally decided to publish it because I was just laid off (modernization project I was supposed to lead had been terminated).

4 Likes

Very interesting, thanks for publishing.

A question - reader gives you config you can read, writer gives you somewhere to send output - state carries state. But to be useful you need to read and write the state, correct ?

So is the state strictly needed ? Can I get it by plugging in something that bridges read and write and carries the state.