DEV Community

Discussion on: IO: to be a Monad or not to be, that's the question!

Collapse
 
raulraja profile image
Raúl Raja Martínez

Suspended functions can be a replacement for all monads because continuations are the mother of all monads.

We could define NonDet and Either exclusively in terms of suspension. We are gonna bring suspension and imperative FP to all data types not just IO as described in this excellent article by @eureka84 and eliminate as much nested style as possible to make FP truly ergonomic in Kotlin.

We have already built shift/reset and a polymorphic ContT over suspension exclusively and two primitives that allow for a la carte strategies per datatype wherein strict ones we can simply fold and suspended ones suspend and resume in any thread.

Continuation also let us flatten three layers of transformers so binding is interleaved.
you can bind in place io, either, io of either and either of io.
nobody is doing this and there is no need to lift to a particular context.

The ergonomics of continuations and the duality of imperative programming has been historically disregarded because most langs have been copying the indirect Haskell encoding. Nothing wrong with that but the JVM prioritizes an imperative stack and labelled loop over reified data structures that need to be folded and interpreted as in most IOs in the JVM except Arrow Fx Coroutines.

But what I would like to bring to your attention with this comment is that suspend is not a replacement for IO but all monads and foldable (see SequenceBuilder in the std lib) and can support interleaved transformer binding at all layers of the transformers, also effect handlers and really anything since it isn't restricted to monads. We have proven monoidal builders as well and there is more we are researching drastically how FP is seen from Kotlin and it's all thanks to the suspension and compiler CPS transformation

Thank you Angelo again for this excellent article, we don't talk enough about these things.
Jannis, Simon and I are currently working on this if anyone wants to get in touch and find out more and get involved. Cheers!

Collapse
 
iquardt profile image
Iven Marquardt

But what I would like to bring to your attention with this comment is that suspend is not a replacement for IO but all monads and foldable (see SequenceBuilder in the std lib) and can support interleaved transformer binding at all layers of the transformers

This is very interesting to me. Can you provide further resources?

Collapse
 
raulraja profile image
Raúl Raja Martínez

schoolofhaskell.com/school/to-infi...

blog.poisson.chat/posts/2019-10-26...

gallium.inria.fr/~scherer/research...

And check out the multiple impls of ContT. Here goes one in swift:
gist.github.com/sjoerdvisscher/a56...

This great discussion about them in Kotlin gist.github.com/elizarov/5bbbe5a3b...
( we got passed the multiprompt issue thanks to Jannis but have not had time to update the combo)

Hope this helps.

Thread Thread
 
iquardt profile image
Iven Marquardt • Edited

Thanks for taking the time. Kotlin seems to be very interesting in the sense of a different approach to FP than Haskell.

Collapse
 
iquardt profile image
Iven Marquardt • Edited

You write that Kotlin can avoid monadic nesting in some/many cases using delimited continuations based on suspend functions. I am currently researching on this topic in the JS context, where we have generator functions. The problem is that such functions can only resume once at a specific position, but to flatten monadic computations in general we'd need multi-shot continuations. How do you solve that in Kotlin? Are Kotlin suspened functions multi-shot? Maybe you can provide some external resources?

Collapse
 
iquardt profile image
Iven Marquardt • Edited

Think I answered my own q: The Kotlin compiler performs a CPS transformation under the hood, similar to Haskell's do block transformation to nested >>=.

Thread Thread
 
raulraja profile image
Raúl Raja Martínez

yes and while multishot is not supported since they are single-shot delimited, you can still multi prompt and build multi-shot like continuations with it.