composewell / streamly

High performance, concurrent functional programming abstractions
https://streamly.composewell.com
Other
859 stars 64 forks source link

MonadPlus instances #555

Open kirelagin opened 4 years ago

kirelagin commented 4 years ago

Currently, streamly does not provide MonadPlus instances for its transformers. (#18, #20, #60)

However, there is a well-known pattern for implementing library-agnostic stream sources (http://www.haskellforall.com/2014/11/how-to-build-library-agnostic-streaming.html) and it relies on having a MonadPlus instance. So, it would be highly desirable to pick some reasonable semantics (compatible with what other libraries implement) and provide MonadPlus instances to make streamly usable with this pattern.

harendra-kumar commented 4 years ago

Thanks for raising the issue and for the reference.

Implementing Alternative and MonadPlus is in the plan, we have not been able to get to it yet.

dagit commented 3 years ago

Implementing Alternative and MonadPlus is in the plan, we have not been able to get to it yet.

I would love to see an Alternative instance for SerialT. As far as I can tell you already have the machinery in place. I added the following trivial (orphan) instance to my code:

instance (Monad m) => Alternative (SerialT m) where
  (<|>) = mappend
  empty = mempty

This allowed me to convert some code that was written for LogicT to SerialT by just changing the type signatures (I wasn't relying on LogicT's fairness anywhere). I could have gotten by without that instance by writing my own replacement for Control.Monad.guard in terms of Monoid/Monad as well but having the instance may come in handy later.

harendra-kumar commented 3 years ago

@dagit Noted. We had an alternative instance once but it was removed later because we wanted to think about all the stream types and keep it consistent across all. We have been busy with getting other fundamental things right, but we will get back to it.

Have you seen https://github.com/composewell/ds-kanren? It uses streamly in place of logict with fairness (using wSerial).