Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dunai: Clean up & organise Data.MonadicStreamFunction.Async #338

Open
turion opened this issue Nov 8, 2022 · 0 comments
Open

dunai: Clean up & organise Data.MonadicStreamFunction.Async #338

turion opened this issue Nov 8, 2022 · 0 comments

Comments

@turion
Copy link
Contributor

turion commented Nov 8, 2022

The module Data.MonadicStreamFunction.Async only contains a single function, concatS, which uses the MSF constructor (which is discouraged). I've often thought that this function must be a special case of some more general principle, similar to how Yampa's switch functions are special cases of exception handling. The module is called Async because it transforms MSFs such that they consume a different number of input and output samples, as opposed to synchronous transformations like morphS which preserve this.

I got the basic insight for a principled generalization from @AshleyYakeley in haskell/mtl#85 (comment). There, we were discussing how ListT interacts with other monads, and that it is a kind of a stream transformer. The argument is: since MSF (MaybeT m) a b == ListT (ReaderT a m) b, it should have a monad instance. This means that if we define

newtype MSFAsync m a b = MSFAsync (MSF (MaybeT m) a b)

we can define Monad m => Monad (MSFAsync m a).

This monad instance can be interpreted this way:

  • An MSFAsync m a b may output several (potentially infinitely many) bs, while producing side effects in m and consuming one a for every b. It can be understood as a side-effectful, a-consuming list of bs.
  • return produces a 1-element list without any further side effects, disregarding the input value.
  • >>= flatmaps the lists, and similarly join concatenates them.

In particular, if we have msfasync >>= f, and msfasync never terminates (producing infinitely many samples), the result will also never terminate. This special case is concatS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants