typelevel / cats

Lightweight, modular, and extensible library for functional programming.
https://typelevel.org/cats/
Other
5.27k stars 1.21k forks source link

Continuation monads #700

Open vikraman opened 9 years ago

vikraman commented 9 years ago

Should Cont, ContT, IndexedContT be added to cats? What would be the proper place to add them?

erikerlandson commented 8 years ago

I'd like to see this

erikerlandson commented 8 years ago

This blog post makes a better argument for value of continuation monads than I would have made: http://www.haskellforall.com/2012/12/the-continuation-monad.html

continuation_kleisli

johnynek commented 8 years ago

I took a stab at this: https://gist.github.com/johnynek/cdbb988351b63be58af053b716b33106

I don't see how to make it totally stack safe. I had an idea to use threads but that seems terrible. Anyway, something like my above implementation may actually be useful because stack depth is only proportional to the number of nested continuations, not flatMaps (which are trampolined).

I played with using this for the poor man's concurrency monad, and it seems okay.

I (or anyone) could make a cats PR if we think this would be generally useful.

erikerlandson commented 8 years ago

@johnynek that looks very cool! To the extent that I understand the use cases I've seen (which isn't as much as I'd like), I don't think they would manifest deep stacks.

adelbertc commented 8 years ago

Would get a 👍 from me. It would also give me an opportunity to harp on about extensible effects and Cont ;)

johnynek commented 8 years ago

take a look at the PR: #1400

danslapman commented 6 years ago

For all, who needs continuation monad, I just ported IndexedContT from scalaz: https://github.com/danslapman/cats-conts

bblfish commented 5 years ago

Very cool. Reading Bartosz Milewski's Category Theory for Programmers, Scala edition I understood that continuations are essentially what the Yoneda Lemma is about. So I also looked around to get a better grip on how these tie in to practical applications. In a way it's not difficult - there are many examples of CPS in JS, etc... The tricky bit is getting to the point that it is crystal clear how the theory and practice tie together, so that one can explain it and think with it in everyday coding.

Many of the blog posts on the topic are in Haskell. The best of them was the continuation monad post referenced by @erikerlandson above. There is also The mother of all monads which I thought very helpful.
There is a whole list of examples on the Reddit discussion thread: Real world examples of the Cont Monad. Looking through that I found a video lecture in Haskell Continuation Passing Style in Haskell. There was a blog post that showed in Haskell how to build web pages, but it had functions that were so long that it really was incomprehensible.

There is the nice tutorials in Scala on continuations by @devinsideyou (to get the full picture one should watch the previous and the next one). But from that I got the impression that the problem with continuations is that they don't really work that well in Scala because of the danger of them using up the whole stack. But I see that the second PR on this topic makes them stack safe! DevInsideYou then moves over to the Cont class in Scala to help with recursive datatypes and then moves on to FreeMonads. So I ended up getting the impression that there is a relation between Cont and Free Monads. So I found this article that Continuation passing style Free Monads and direct style Free Monads

There is video on using continuations to build web services in Scala.

japgolly commented 5 years ago

FYI this might be helpful to learning too: https://github.com/japgolly/scalajs-react/blob/master/core/src/main/scala/japgolly/scalajs/react/AsyncCallback.scala

It's ContT IO specialised for JS (where you read CallbackTo it's effectively IO). It allows people to write async code in scalajs-react in a manner similar to how people use Future, and purely.

On Thu, 22 Aug. 2019, 7:23 pm Henry Story, notifications@github.com wrote:

Very cool. Reading Bartosz Milewski's Category Theory for Programmers, Scala edition https://twitter.com/hmemcpy/status/1160870623943561216 I understood that continuations are essentially what the Yoneda Lemma is about. So I also looked around to get a better grip on how these tie in to practical applications. In a way it's not difficult - there are many examples of CPS in JS, etc... The tricky bit is getting to the point that it is crystal clear how the theory and practice tie together, so that one can explain it and think with it in everyday coding.

Many of the blog posts on the topic are in Haskell. The best of them was the continuation monad http://www.haskellforall.com/2012/12/the-continuation-monad.html post referenced by @erikerlandson https://github.com/erikerlandson above. There is also The mother of all monads http://blog.sigfpe.com/2008/12/mother-of-all-monads.html which I thought very helpful. There is a whole list of examples on the Reddit discussion thread: Real world examples of the Cont Monad https://www.reddit.com/r/haskell/comments/38zpuo/real_world_examples_of_the_cont_monad/. Looking through that I found a video lecture in Haskell Continuation Passing Style in Haskell https://begriffs.com/posts/2015-06-03-haskell-continuations.html. There was a blog post that showed in Haskell how to build web pages, but it had functions that were so long that it really was incomprehensible.

There is the nice turtorials in Scala on continuations https://www.youtube.com/watch?v=93NZXOGG8ak by @DevInsideYou https://github.com/DevInsideYou (to get the full picture one should watch the previous and the next one). But from that I got the impression that the problem with continuations is that they don't really work that well in Scala because of the danger of them using up the whole stack. But I see that the second PR on this topic https://github.com/typelevel/cats/pull/2506#issuecomment-439439652 makes them stack safe! DevInsideYou then moves over to the Cont class in Scala to help with recursive datatypes and then moves on to FreeMonads. So I ended up getting the impression that there is a relation between Cont and Free Monads. So I found this article that Continuation passing style Free Monads and direct style Free Monads https://deque.blog/2017/12/08/continuation-passing-style-free-monads-and-direct-style-free-monads/

There is video on using continuations to build web services http://cufp.org/2016/building-a-web-application-with-continuation-monads.html in Scala.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/typelevel/cats/issues/700?email_source=notifications&email_token=AABRRN32GRNOZMICXFMOBT3QFZLJVA5CNFSM4BVC6DXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD44O64Q#issuecomment-523825010, or mute the thread https://github.com/notifications/unsubscribe-auth/AABRRN6JZV5JHUM36EGZLQTQFZLJVANCNFSM4BVC6DXA .

diesalbla commented 3 years ago

Good afternoon!

So, looking at the current main branch, we already have a transformer ContT and a type alias Cont, so it would seem that the changes requested or proposed by this issue have already be done. Is there anything else needed to complete the work of this issue?

If there is a desire to add the IndexedCont or indexed continuations, perhaps that can be a separate issue with a separate discussion.

johnynek commented 3 years ago

Yeah I forgot to link to this from #2506

Indexed has not been done that I know of.