Skip to content

Remove Monad and Applicative constraints on the Parallel class #43

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

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Notable changes to this project are documented in this file. The format is based
## [Unreleased]

Breaking changes:
- Remove `Monad` and `Applicative` constraints on the `Parallel` class. Both
types now only require `Apply`. (#43 by @artemisSystem)

New features:

Expand Down
4 changes: 4 additions & 0 deletions src/Control/Parallel.purs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ parApply mf ma = sequential(apply (parallel mf) (parallel ma))
parTraverse
:: forall f m t a b
. Parallel f m
=> Applicative f
=> Traversable t
=> (a -> m b)
-> t a
Expand All @@ -40,6 +41,7 @@ parTraverse f = sequential <<< traverse (parallel <<< f)
parTraverse_
:: forall f m t a b
. Parallel f m
=> Applicative f
=> Foldable t
=> (a -> m b)
-> t a
Expand All @@ -49,6 +51,7 @@ parTraverse_ f = sequential <<< traverse_ (parallel <<< f)
parSequence
:: forall a t m f
. Parallel f m
=> Applicative f
=> Traversable t
=> t (m a)
-> m (t a)
Expand All @@ -57,6 +60,7 @@ parSequence = parTraverse identity
parSequence_
:: forall a t m f
. Parallel f m
=> Applicative f
=> Foldable t
=> t (m a)
-> m Unit
Expand Down
13 changes: 7 additions & 6 deletions src/Control/Parallel/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import Data.Profunctor.Star (Star(..))
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Ref as Ref

-- | The `Parallel` class abstracts over monads which support
-- | parallel composition via some related `Applicative`.
class (Monad m, Applicative f) <= Parallel f m | m -> f, f -> m where
-- | The `Parallel` class abstracts over pairs of `Apply`s where one of them
-- | (`m`) composes sequentially, and the other (`f`) composes in parallel.
-- | `m` is usually a `Monad`, which enforces the sequential nature of its
-- | composition, but it doesn't need to be.
class (Apply m, Apply f) <= Parallel f m | m -> f, f -> m where
parallel :: m ~> f
sequential :: f ~> m

instance monadParExceptT :: Parallel f m => Parallel (Compose f (Either e)) (ExceptT e m) where
instance monadParExceptT :: (Parallel f m, Monad m) => Parallel (Compose f (Either e)) (ExceptT e m) where
parallel (ExceptT ma) = Compose (parallel ma)
sequential (Compose fa) = ExceptT (sequential fa)

Expand All @@ -37,7 +39,7 @@ instance monadParWriterT :: (Monoid w, Parallel f m) => Parallel (WriterT w f) (
parallel = mapWriterT parallel
sequential = mapWriterT sequential

instance monadParMaybeT :: Parallel f m => Parallel (Compose f Maybe) (MaybeT m) where
instance monadParMaybeT :: (Parallel f m, Monad m) => Parallel (Compose f Maybe) (MaybeT m) where
parallel (MaybeT ma) = Compose (parallel ma)
sequential (Compose fa) = MaybeT (sequential fa)

Expand All @@ -49,7 +51,6 @@ instance monadParCostar :: Parallel f m => Parallel (Costar f a) (Costar m a) wh
parallel (Costar f) = (Costar $ sequential >>> f)
sequential (Costar f) = (Costar $ parallel >>> f)


-- | The `ParCont` type constructor provides an `Applicative` instance
-- | based on `ContT Unit m`, which waits for multiple continuations to be
-- | resumed simultaneously.
Expand Down