Structured concurrency in Haskell

Hello folks,

I’d like to announce a new structured concurrency library in the Haskell ecosystem, which was heavily inspired by many of the blog posts and libraries shared here. I’d just like to say thanks for all of the interesting material!

Package: https://hackage.haskell.org/package/ki
Source code: https://github.com/mitchellwrosen/ki

One of the main differences in Haskell as compared to Python and Kotlin is the existence of the GHC runtime system. It makes writing concurrent code in Haskell a bit more “adversarial”, as a thread does not typically voluntarily yield control, and can be delivered a so-called “asynchronous exception” from the RTS at any time.

Otherwise, the straightforward concept of delimiting the lifetime of concurrent threads with a syntactic block of code translated over very nicely!

What about the other main piece of structured concurrency, which is cancellation? (I haven’t read the package material.)

Ah, yes - in ki this is implemented as follows.

A “context” is passed around implicitly using an implicit parameter.

A scope object can be explicitly cancelled, and any thread can query for whether its scope (or its scope’s parent, and on and on…) has been cancelled. If it has, it may either terminate gracefully or throw a special “honor the cancellation request” exception that will not be propagated like a normal exception.

A thread running in a cancelled scope can still perform blocking IO operations - noticing/honoring a cancellation request is fully cooperative.