Skip to content

Commit

Permalink
Merge pull request #419 from fused-effects/you-can-choose-your-friend…
Browse files Browse the repository at this point in the history
…s-but-you-(probably)-can't-choose-your-nose

Instances for Choosing
  • Loading branch information
patrickt committed Mar 2, 2022
2 parents b447986 + 067dc23 commit 2b3d193
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
- Defines `Algebra` instances for the two mentioned carriers,
and for `Control.Monad.Trans.Accum` from `transformers`

- Defines, `Algebra`, `Alternative`, `Applicative`, `Foldable`, `Functor`, `Monad`, `MonadFail`, `MonadFix`, `MonadIO`, `MonadPlus`, `MonadTrans`, `MonadUnliftIO`, `MonadZip`, and `Traversable` instances for `Control.Effect.Choose.Choosing`. ([#419](https://github.com/fused-effects/fused-effects/pull/419))


# v1.1.1.2

- Adds support for `ghc` 9.2.1 and `base` 4.16.


# v1.1.1.1

- Adds support for `ghc` 9.0 & `base` 4.15.
Expand Down
36 changes: 36 additions & 0 deletions src/Control/Effect/Choose.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE UndecidableInstances #-}

{- | An effect modelling nondeterminism without failure (one or more successful results).
Expand Down Expand Up @@ -29,8 +30,16 @@ module Control.Effect.Choose
) where

import Control.Algebra
import qualified Control.Applicative as A
import Control.Effect.Choose.Internal (Choose(..))
import Control.Effect.Empty
import Control.Monad (MonadPlus)
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.IO.Unlift (MonadUnliftIO)
import Control.Monad.Trans.Class (MonadTrans(..))
import Control.Monad.Zip
import Data.Bool (bool)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Semigroup as S
Expand Down Expand Up @@ -107,6 +116,7 @@ some1 a = (:|) <$> a <*> many a

-- | @since 1.0.0.0
newtype Choosing m a = Choosing { getChoosing :: m a }
deriving (Algebra sig, Applicative, Foldable, Functor, Monad, Fail.MonadFail, MonadFix, MonadIO, MonadUnliftIO, MonadZip)

instance Has Choose sig m => S.Semigroup (Choosing m a) where
Choosing m1 <> Choosing m2 = Choosing (m1 <|> m2)
Expand All @@ -118,3 +128,29 @@ instance (Has Choose sig m, Has Empty sig m) => Monoid (Choosing m a) where

mappend = (S.<>)
{-# INLINE mappend #-}

instance (Has Choose sig m, Has Empty sig m) => A.Alternative (Choosing m) where
empty = mempty
{-# INLINE empty #-}

(<|>) = mappend
{-# INLINE (<|>) #-}

instance (Has Choose sig m, Has Empty sig m) => MonadPlus (Choosing m)

instance MonadTrans Choosing where
lift = Choosing
{-# INLINE lift #-}

instance Traversable m => Traversable (Choosing m) where
sequenceA (Choosing m) = fmap Choosing (sequenceA m)
{-# INLINE sequenceA #-}

traverse f (Choosing m) = fmap Choosing (traverse f m)
{-# INLINE traverse #-}

sequence (Choosing m) = fmap Choosing (sequence m)
{-# INLINE sequence #-}

mapM f (Choosing m) = fmap Choosing (mapM f m)
{-# INLINE mapM #-}

0 comments on commit 2b3d193

Please sign in to comment.