{-# LANGUAGE CPP, LambdaCase, TupleSections, DeriveFunctor #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
module Control.Selective (
Selective (..), (<*?), branch, selectA, apS, selectM,
ifS, whenS, fromMaybeS, orElse, andAlso, untilRight, whileS, (<||>), (<&&>),
foldS, anyS, allS, bindS, Cases, casesEnum, cases, matchS, matchM,
SelectA (..), SelectM (..), Over (..), Under (..), Validation (..),
swapEither, ComposeEither (..)
) where
import Control.Applicative
import Control.Applicative.Lift
import Control.Arrow
import Control.Monad.ST
import Control.Monad.Trans.Cont
import Control.Monad.Trans.Except
import Control.Monad.Trans.Identity
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader
import Control.Monad.Trans.RWS
import Control.Monad.Trans.State
import Control.Monad.Trans.Writer
import Data.Bool
import Data.Function
import Data.Functor.Compose
import Data.Functor.Identity
import Data.Functor.Product
import Data.List.NonEmpty
import Data.Proxy
import Data.Semigroup (Semigroup (..))
import GHC.Conc (STM)
import qualified Control.Monad.Trans.RWS.Strict as S
import qualified Control.Monad.Trans.State.Strict as S
import qualified Control.Monad.Trans.Writer.Strict as S
class Applicative f => Selective f where
select :: f (Either a b) -> f (a -> b) -> f b
(<*?) :: Selective f => f (Either a b) -> f (a -> b) -> f b
<*? :: f (Either a b) -> f (a -> b) -> f b
(<*?) = f (Either a b) -> f (a -> b) -> f b
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select
infixl 4 <*?
branch :: Selective f => f (Either a b) -> f (a -> c) -> f (b -> c) -> f c
branch :: f (Either a b) -> f (a -> c) -> f (b -> c) -> f c
branch f (Either a b)
x f (a -> c)
l f (b -> c)
r = (Either a b -> Either a (Either b c))
-> f (Either a b) -> f (Either a (Either b c))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> Either b c) -> Either a b -> Either a (Either b c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> Either b c
forall a b. a -> Either a b
Left) f (Either a b)
x f (Either a (Either b c)) -> f (a -> Either b c) -> f (Either b c)
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
<*? ((a -> c) -> a -> Either b c) -> f (a -> c) -> f (a -> Either b c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((c -> Either b c) -> (a -> c) -> a -> Either b c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> Either b c
forall a b. b -> Either a b
Right) f (a -> c)
l f (Either b c) -> f (b -> c) -> f c
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
<*? f (b -> c)
r
selectA :: Applicative f => f (Either a b) -> f (a -> b) -> f b
selectA :: f (Either a b) -> f (a -> b) -> f b
selectA f (Either a b)
x f (a -> b)
y = (\Either a b
e a -> b
f -> (a -> b) -> (b -> b) -> Either a b -> b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> b
f b -> b
forall a. a -> a
id Either a b
e) (Either a b -> (a -> b) -> b)
-> f (Either a b) -> f ((a -> b) -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either a b)
x f ((a -> b) -> b) -> f (a -> b) -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f (a -> b)
y
apS :: Selective f => f (a -> b) -> f a -> f b
apS :: f (a -> b) -> f a -> f b
apS f (a -> b)
f f a
x = f (Either (a -> b) b) -> f ((a -> b) -> b) -> f b
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select ((a -> b) -> Either (a -> b) b
forall a b. a -> Either a b
Left ((a -> b) -> Either (a -> b) b)
-> f (a -> b) -> f (Either (a -> b) b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (a -> b)
f) (a -> (a -> b) -> b
forall a b. a -> (a -> b) -> b
(&) (a -> (a -> b) -> b) -> f a -> f ((a -> b) -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
x)
selectM :: Monad f => f (Either a b) -> f (a -> b) -> f b
selectM :: f (Either a b) -> f (a -> b) -> f b
selectM f (Either a b)
x f (a -> b)
y = f (Either a b)
x f (Either a b) -> (Either a b -> f b) -> f b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case Left a
a -> ((a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$a
a) ((a -> b) -> b) -> f (a -> b) -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (a -> b)
y
Right b
b -> b -> f b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
b
ifS :: Selective f => f Bool -> f a -> f a -> f a
ifS :: f Bool -> f a -> f a -> f a
ifS f Bool
x f a
t f a
e = f (Either () ()) -> f (() -> a) -> f (() -> a) -> f a
forall (f :: * -> *) a b c.
Selective f =>
f (Either a b) -> f (a -> c) -> f (b -> c) -> f c
branch (Either () () -> Either () () -> Bool -> Either () ()
forall a. a -> a -> Bool -> a
bool (() -> Either () ()
forall a b. b -> Either a b
Right ()) (() -> Either () ()
forall a b. a -> Either a b
Left ()) (Bool -> Either () ()) -> f Bool -> f (Either () ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f Bool
x) (a -> () -> a
forall a b. a -> b -> a
const (a -> () -> a) -> f a -> f (() -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
t) (a -> () -> a
forall a b. a -> b -> a
const (a -> () -> a) -> f a -> f (() -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
e)
eliminate :: (Eq a, Selective f) => a -> f b -> f (Either a b) -> f (Either a b)
eliminate :: a -> f b -> f (Either a b) -> f (Either a b)
eliminate a
x f b
fb f (Either a b)
fa = f (Either () (Either a b))
-> f (() -> Either a b) -> f (Either a b)
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select (a -> Either a b -> Either () (Either a b)
forall a b. Eq a => a -> Either a b -> Either () (Either a b)
match a
x (Either a b -> Either () (Either a b))
-> f (Either a b) -> f (Either () (Either a b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either a b)
fa) (Either a b -> () -> Either a b
forall a b. a -> b -> a
const (Either a b -> () -> Either a b)
-> (b -> Either a b) -> b -> () -> Either a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Either a b
forall a b. b -> Either a b
Right (b -> () -> Either a b) -> f b -> f (() -> Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f b
fb)
where
match :: a -> Either a b -> Either () (Either a b)
match a
_ (Right b
y) = Either a b -> Either () (Either a b)
forall a b. b -> Either a b
Right (b -> Either a b
forall a b. b -> Either a b
Right b
y)
match a
x (Left a
y) = if a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y then () -> Either () (Either a b)
forall a b. a -> Either a b
Left () else Either a b -> Either () (Either a b)
forall a b. b -> Either a b
Right (a -> Either a b
forall a b. a -> Either a b
Left a
y)
data Cases a = Cases [a] (a -> Bool)
casesEnum :: (Bounded a, Enum a) => Cases a
casesEnum :: Cases a
casesEnum = [a] -> (a -> Bool) -> Cases a
forall a. [a] -> (a -> Bool) -> Cases a
Cases [a
forall a. Bounded a => a
minBound..a
forall a. Bounded a => a
maxBound] (Bool -> a -> Bool
forall a b. a -> b -> a
const Bool
True)
cases :: Eq a => [a] -> Cases a
cases :: [a] -> Cases a
cases [a]
as = [a] -> (a -> Bool) -> Cases a
forall a. [a] -> (a -> Bool) -> Cases a
Cases [a]
as (a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
as)
matchS :: (Eq a, Selective f) => Cases a -> f a -> (a -> f b) -> f (Either a b)
matchS :: Cases a -> f a -> (a -> f b) -> f (Either a b)
matchS (Cases [a]
cs a -> Bool
_) f a
x a -> f b
f = (a -> f (Either a b) -> f (Either a b))
-> f (Either a b) -> [a] -> f (Either a b)
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
c -> a -> f b -> f (Either a b) -> f (Either a b)
forall a (f :: * -> *) b.
(Eq a, Selective f) =>
a -> f b -> f (Either a b) -> f (Either a b)
eliminate a
c (a -> f b
f a
c)) (a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> f a -> f (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
x) [a]
cs
matchM :: Monad m => Cases a -> m a -> (a -> m b) -> m (Either a b)
matchM :: Cases a -> m a -> (a -> m b) -> m (Either a b)
matchM (Cases [a]
_ a -> Bool
p) m a
mx a -> m b
f = do
a
x <- m a
mx
if a -> Bool
p a
x then b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> m b -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
f a
x else Either a b -> m (Either a b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Either a b
forall a b. a -> Either a b
Left a
x)
bindS :: (Bounded a, Enum a, Eq a, Selective f) => f a -> (a -> f b) -> f b
bindS :: f a -> (a -> f b) -> f b
bindS f a
x a -> f b
f = Either a b -> b
forall a p. Either a p -> p
fromRight (Either a b -> b) -> f (Either a b) -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Cases a -> f a -> (a -> f b) -> f (Either a b)
forall a (f :: * -> *) b.
(Eq a, Selective f) =>
Cases a -> f a -> (a -> f b) -> f (Either a b)
matchS Cases a
forall a. (Bounded a, Enum a) => Cases a
casesEnum f a
x a -> f b
f
where
fromRight :: Either a p -> p
fromRight (Right p
b) = p
b
fromRight Either a p
_ = [Char] -> p
forall a. HasCallStack => [Char] -> a
error [Char]
"Selective.bindS: incorrect Bounded and/or Enum instance"
whenS :: Selective f => f Bool -> f () -> f ()
whenS :: f Bool -> f () -> f ()
whenS f Bool
x f ()
y = f (Either () ()) -> f (() -> ()) -> f ()
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select (Either () () -> Either () () -> Bool -> Either () ()
forall a. a -> a -> Bool -> a
bool (() -> Either () ()
forall a b. b -> Either a b
Right ()) (() -> Either () ()
forall a b. a -> Either a b
Left ()) (Bool -> Either () ()) -> f Bool -> f (Either () ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f Bool
x) (() -> () -> ()
forall a b. a -> b -> a
const (() -> () -> ()) -> f () -> f (() -> ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f ()
y)
fromMaybeS :: Selective f => f a -> f (Maybe a) -> f a
fromMaybeS :: f a -> f (Maybe a) -> f a
fromMaybeS f a
x f (Maybe a)
mx = f (Either () a) -> f (() -> a) -> f a
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select (Either () a -> (a -> Either () a) -> Maybe a -> Either () a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> Either () a
forall a b. a -> Either a b
Left ()) a -> Either () a
forall a b. b -> Either a b
Right (Maybe a -> Either () a) -> f (Maybe a) -> f (Either () a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Maybe a)
mx) (a -> () -> a
forall a b. a -> b -> a
const (a -> () -> a) -> f a -> f (() -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
x)
orElse :: (Selective f, Semigroup e) => f (Either e a) -> f (Either e a) -> f (Either e a)
orElse :: f (Either e a) -> f (Either e a) -> f (Either e a)
orElse f (Either e a)
x f (Either e a)
y = f (Either e a)
-> f (e -> Either e a) -> f (a -> Either e a) -> f (Either e a)
forall (f :: * -> *) a b c.
Selective f =>
f (Either a b) -> f (a -> c) -> f (b -> c) -> f c
branch f (Either e a)
x ((e -> Either e a -> Either e a) -> Either e a -> e -> Either e a
forall a b c. (a -> b -> c) -> b -> a -> c
flip e -> Either e a -> Either e a
forall a b. Semigroup a => a -> Either a b -> Either a b
appendLeft (Either e a -> e -> Either e a)
-> f (Either e a) -> f (e -> Either e a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either e a)
y) ((a -> Either e a) -> f (a -> Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure a -> Either e a
forall a b. b -> Either a b
Right)
andAlso :: (Selective f, Semigroup a) => f (Either e a) -> f (Either e a) -> f (Either e a)
andAlso :: f (Either e a) -> f (Either e a) -> f (Either e a)
andAlso f (Either e a)
x f (Either e a)
y = Either a e -> Either e a
forall a b. Either a b -> Either b a
swapEither (Either a e -> Either e a) -> f (Either a e) -> f (Either e a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either a e) -> f (Either a e) -> f (Either a e)
forall (f :: * -> *) e a.
(Selective f, Semigroup e) =>
f (Either e a) -> f (Either e a) -> f (Either e a)
orElse (Either e a -> Either a e
forall a b. Either a b -> Either b a
swapEither (Either e a -> Either a e) -> f (Either e a) -> f (Either a e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either e a)
x) (Either e a -> Either a e
forall a b. Either a b -> Either b a
swapEither (Either e a -> Either a e) -> f (Either e a) -> f (Either a e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either e a)
y)
swapEither :: Either a b -> Either b a
swapEither :: Either a b -> Either b a
swapEither = (a -> Either b a) -> (b -> Either b a) -> Either a b -> Either b a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Either b a
forall a b. b -> Either a b
Right b -> Either b a
forall a b. a -> Either a b
Left
appendLeft :: Semigroup a => a -> Either a b -> Either a b
appendLeft :: a -> Either a b -> Either a b
appendLeft a
a1 (Left a
a2) = a -> Either a b
forall a b. a -> Either a b
Left (a
a1 a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a2)
appendLeft a
_ (Right b
b) = b -> Either a b
forall a b. b -> Either a b
Right b
b
whileS :: Selective f => f Bool -> f ()
whileS :: f Bool -> f ()
whileS f Bool
act = f Bool -> f () -> f ()
forall (f :: * -> *). Selective f => f Bool -> f () -> f ()
whenS f Bool
act (f Bool -> f ()
forall (f :: * -> *). Selective f => f Bool -> f ()
whileS f Bool
act)
untilRight :: (Monoid a, Selective f) => f (Either a b) -> f (a, b)
untilRight :: f (Either a b) -> f (a, b)
untilRight f (Either a b)
x = f (Either a (a, b)) -> f (a -> (a, b)) -> f (a, b)
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select f (Either a (a, b))
y f (a -> (a, b))
h
where
y :: f (Either a (a, b))
y = (b -> (a, b)) -> Either a b -> Either a (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
forall a. Monoid a => a
mempty,) (Either a b -> Either a (a, b))
-> f (Either a b) -> f (Either a (a, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either a b)
x
h :: f (a -> (a, b))
h = (\(a
as, b
b) a
a -> (a -> a -> a
forall a. Monoid a => a -> a -> a
mappend a
a a
as, b
b)) ((a, b) -> a -> (a, b)) -> f (a, b) -> f (a -> (a, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either a b) -> f (a, b)
forall a (f :: * -> *) b.
(Monoid a, Selective f) =>
f (Either a b) -> f (a, b)
untilRight f (Either a b)
x
(<||>) :: Selective f => f Bool -> f Bool -> f Bool
f Bool
a <||> :: f Bool -> f Bool -> f Bool
<||> f Bool
b = f Bool -> f Bool -> f Bool -> f Bool
forall (f :: * -> *) a. Selective f => f Bool -> f a -> f a -> f a
ifS f Bool
a (Bool -> f Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True) f Bool
b
(<&&>) :: Selective f => f Bool -> f Bool -> f Bool
f Bool
a <&&> :: f Bool -> f Bool -> f Bool
<&&> f Bool
b = f Bool -> f Bool -> f Bool -> f Bool
forall (f :: * -> *) a. Selective f => f Bool -> f a -> f a -> f a
ifS f Bool
a f Bool
b (Bool -> f Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False)
anyS :: Selective f => (a -> f Bool) -> [a] -> f Bool
anyS :: (a -> f Bool) -> [a] -> f Bool
anyS a -> f Bool
p = (a -> f Bool -> f Bool) -> f Bool -> [a] -> f Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (f Bool -> f Bool -> f Bool
forall (f :: * -> *). Selective f => f Bool -> f Bool -> f Bool
(<||>) (f Bool -> f Bool -> f Bool)
-> (a -> f Bool) -> a -> f Bool -> f Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f Bool
p) (Bool -> f Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False)
allS :: Selective f => (a -> f Bool) -> [a] -> f Bool
allS :: (a -> f Bool) -> [a] -> f Bool
allS a -> f Bool
p = (a -> f Bool -> f Bool) -> f Bool -> [a] -> f Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (f Bool -> f Bool -> f Bool
forall (f :: * -> *). Selective f => f Bool -> f Bool -> f Bool
(<&&>) (f Bool -> f Bool -> f Bool)
-> (a -> f Bool) -> a -> f Bool -> f Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f Bool
p) (Bool -> f Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True)
foldS :: (Selective f, Foldable t, Monoid a
#if !MIN_VERSION_base(4,11,0)
, Semigroup a
#endif
) => t (f (Either e a)) -> f (Either e a)
foldS :: t (f (Either e a)) -> f (Either e a)
foldS = (f (Either e a) -> f (Either e a) -> f (Either e a))
-> f (Either e a) -> t (f (Either e a)) -> f (Either e a)
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr f (Either e a) -> f (Either e a) -> f (Either e a)
forall (f :: * -> *) a e.
(Selective f, Semigroup a) =>
f (Either e a) -> f (Either e a) -> f (Either e a)
andAlso (Either e a -> f (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either e a
forall a b. b -> Either a b
Right a
forall a. Monoid a => a
mempty))
newtype SelectA f a = SelectA { SelectA f a -> f a
getSelectA :: f a }
deriving (a -> SelectA f b -> SelectA f a
(a -> b) -> SelectA f a -> SelectA f b
(forall a b. (a -> b) -> SelectA f a -> SelectA f b)
-> (forall a b. a -> SelectA f b -> SelectA f a)
-> Functor (SelectA f)
forall a b. a -> SelectA f b -> SelectA f a
forall a b. (a -> b) -> SelectA f a -> SelectA f b
forall (f :: * -> *) a b.
Functor f =>
a -> SelectA f b -> SelectA f a
forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> SelectA f a -> SelectA f b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> SelectA f b -> SelectA f a
$c<$ :: forall (f :: * -> *) a b.
Functor f =>
a -> SelectA f b -> SelectA f a
fmap :: (a -> b) -> SelectA f a -> SelectA f b
$cfmap :: forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> SelectA f a -> SelectA f b
Functor, Functor (SelectA f)
a -> SelectA f a
Functor (SelectA f)
-> (forall a. a -> SelectA f a)
-> (forall a b. SelectA f (a -> b) -> SelectA f a -> SelectA f b)
-> (forall a b c.
(a -> b -> c) -> SelectA f a -> SelectA f b -> SelectA f c)
-> (forall a b. SelectA f a -> SelectA f b -> SelectA f b)
-> (forall a b. SelectA f a -> SelectA f b -> SelectA f a)
-> Applicative (SelectA f)
SelectA f a -> SelectA f b -> SelectA f b
SelectA f a -> SelectA f b -> SelectA f a
SelectA f (a -> b) -> SelectA f a -> SelectA f b
(a -> b -> c) -> SelectA f a -> SelectA f b -> SelectA f c
forall a. a -> SelectA f a
forall a b. SelectA f a -> SelectA f b -> SelectA f a
forall a b. SelectA f a -> SelectA f b -> SelectA f b
forall a b. SelectA f (a -> b) -> SelectA f a -> SelectA f b
forall a b c.
(a -> b -> c) -> SelectA f a -> SelectA f b -> SelectA f c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall (f :: * -> *). Applicative f => Functor (SelectA f)
forall (f :: * -> *) a. Applicative f => a -> SelectA f a
forall (f :: * -> *) a b.
Applicative f =>
SelectA f a -> SelectA f b -> SelectA f a
forall (f :: * -> *) a b.
Applicative f =>
SelectA f a -> SelectA f b -> SelectA f b
forall (f :: * -> *) a b.
Applicative f =>
SelectA f (a -> b) -> SelectA f a -> SelectA f b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> SelectA f a -> SelectA f b -> SelectA f c
<* :: SelectA f a -> SelectA f b -> SelectA f a
$c<* :: forall (f :: * -> *) a b.
Applicative f =>
SelectA f a -> SelectA f b -> SelectA f a
*> :: SelectA f a -> SelectA f b -> SelectA f b
$c*> :: forall (f :: * -> *) a b.
Applicative f =>
SelectA f a -> SelectA f b -> SelectA f b
liftA2 :: (a -> b -> c) -> SelectA f a -> SelectA f b -> SelectA f c
$cliftA2 :: forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> SelectA f a -> SelectA f b -> SelectA f c
<*> :: SelectA f (a -> b) -> SelectA f a -> SelectA f b
$c<*> :: forall (f :: * -> *) a b.
Applicative f =>
SelectA f (a -> b) -> SelectA f a -> SelectA f b
pure :: a -> SelectA f a
$cpure :: forall (f :: * -> *) a. Applicative f => a -> SelectA f a
$cp1Applicative :: forall (f :: * -> *). Applicative f => Functor (SelectA f)
Applicative)
instance Applicative f => Selective (SelectA f) where
select :: SelectA f (Either a b) -> SelectA f (a -> b) -> SelectA f b
select = SelectA f (Either a b) -> SelectA f (a -> b) -> SelectA f b
forall (f :: * -> *) a b.
Applicative f =>
f (Either a b) -> f (a -> b) -> f b
selectA
instance Selective f => Selective (Lift f) where
select :: Lift f (Either a b) -> Lift f (a -> b) -> Lift f b
select Lift f (Either a b)
x (Pure a -> b
y) = (a -> b) -> (b -> b) -> Either a b -> b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> b
y b -> b
forall a. a -> a
id (Either a b -> b) -> Lift f (Either a b) -> Lift f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lift f (Either a b)
x
select (Pure (Right b
x)) Lift f (a -> b)
_ = b -> Lift f b
forall (f :: * -> *) a. a -> Lift f a
Pure b
x
select (Pure (Left a
x)) (Other f (a -> b)
y) = f b -> Lift f b
forall (f :: * -> *) a. f a -> Lift f a
Other (f b -> Lift f b) -> f b -> Lift f b
forall a b. (a -> b) -> a -> b
$ ((a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$a
x) ((a -> b) -> b) -> f (a -> b) -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (a -> b)
y
select (Other f (Either a b)
x ) (Other f (a -> b)
y) = f b -> Lift f b
forall (f :: * -> *) a. f a -> Lift f a
Other (f b -> Lift f b) -> f b -> Lift f b
forall a b. (a -> b) -> a -> b
$ f (Either a b)
x f (Either a b) -> f (a -> b) -> f b
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
<*? f (a -> b)
y
newtype SelectM f a = SelectM { SelectM f a -> f a
getSelectM :: f a }
deriving (a -> SelectM f b -> SelectM f a
(a -> b) -> SelectM f a -> SelectM f b
(forall a b. (a -> b) -> SelectM f a -> SelectM f b)
-> (forall a b. a -> SelectM f b -> SelectM f a)
-> Functor (SelectM f)
forall a b. a -> SelectM f b -> SelectM f a
forall a b. (a -> b) -> SelectM f a -> SelectM f b
forall (f :: * -> *) a b.
Functor f =>
a -> SelectM f b -> SelectM f a
forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> SelectM f a -> SelectM f b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> SelectM f b -> SelectM f a
$c<$ :: forall (f :: * -> *) a b.
Functor f =>
a -> SelectM f b -> SelectM f a
fmap :: (a -> b) -> SelectM f a -> SelectM f b
$cfmap :: forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> SelectM f a -> SelectM f b
Functor, Functor (SelectM f)
a -> SelectM f a
Functor (SelectM f)
-> (forall a. a -> SelectM f a)
-> (forall a b. SelectM f (a -> b) -> SelectM f a -> SelectM f b)
-> (forall a b c.
(a -> b -> c) -> SelectM f a -> SelectM f b -> SelectM f c)
-> (forall a b. SelectM f a -> SelectM f b -> SelectM f b)
-> (forall a b. SelectM f a -> SelectM f b -> SelectM f a)
-> Applicative (SelectM f)
SelectM f a -> SelectM f b -> SelectM f b
SelectM f a -> SelectM f b -> SelectM f a
SelectM f (a -> b) -> SelectM f a -> SelectM f b
(a -> b -> c) -> SelectM f a -> SelectM f b -> SelectM f c
forall a. a -> SelectM f a
forall a b. SelectM f a -> SelectM f b -> SelectM f a
forall a b. SelectM f a -> SelectM f b -> SelectM f b
forall a b. SelectM f (a -> b) -> SelectM f a -> SelectM f b
forall a b c.
(a -> b -> c) -> SelectM f a -> SelectM f b -> SelectM f c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall (f :: * -> *). Applicative f => Functor (SelectM f)
forall (f :: * -> *) a. Applicative f => a -> SelectM f a
forall (f :: * -> *) a b.
Applicative f =>
SelectM f a -> SelectM f b -> SelectM f a
forall (f :: * -> *) a b.
Applicative f =>
SelectM f a -> SelectM f b -> SelectM f b
forall (f :: * -> *) a b.
Applicative f =>
SelectM f (a -> b) -> SelectM f a -> SelectM f b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> SelectM f a -> SelectM f b -> SelectM f c
<* :: SelectM f a -> SelectM f b -> SelectM f a
$c<* :: forall (f :: * -> *) a b.
Applicative f =>
SelectM f a -> SelectM f b -> SelectM f a
*> :: SelectM f a -> SelectM f b -> SelectM f b
$c*> :: forall (f :: * -> *) a b.
Applicative f =>
SelectM f a -> SelectM f b -> SelectM f b
liftA2 :: (a -> b -> c) -> SelectM f a -> SelectM f b -> SelectM f c
$cliftA2 :: forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> SelectM f a -> SelectM f b -> SelectM f c
<*> :: SelectM f (a -> b) -> SelectM f a -> SelectM f b
$c<*> :: forall (f :: * -> *) a b.
Applicative f =>
SelectM f (a -> b) -> SelectM f a -> SelectM f b
pure :: a -> SelectM f a
$cpure :: forall (f :: * -> *) a. Applicative f => a -> SelectM f a
$cp1Applicative :: forall (f :: * -> *). Applicative f => Functor (SelectM f)
Applicative, Applicative (SelectM f)
a -> SelectM f a
Applicative (SelectM f)
-> (forall a b. SelectM f a -> (a -> SelectM f b) -> SelectM f b)
-> (forall a b. SelectM f a -> SelectM f b -> SelectM f b)
-> (forall a. a -> SelectM f a)
-> Monad (SelectM f)
SelectM f a -> (a -> SelectM f b) -> SelectM f b
SelectM f a -> SelectM f b -> SelectM f b
forall a. a -> SelectM f a
forall a b. SelectM f a -> SelectM f b -> SelectM f b
forall a b. SelectM f a -> (a -> SelectM f b) -> SelectM f b
forall (f :: * -> *). Monad f => Applicative (SelectM f)
forall (f :: * -> *) a. Monad f => a -> SelectM f a
forall (f :: * -> *) a b.
Monad f =>
SelectM f a -> SelectM f b -> SelectM f b
forall (f :: * -> *) a b.
Monad f =>
SelectM f a -> (a -> SelectM f b) -> SelectM f b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> SelectM f a
$creturn :: forall (f :: * -> *) a. Monad f => a -> SelectM f a
>> :: SelectM f a -> SelectM f b -> SelectM f b
$c>> :: forall (f :: * -> *) a b.
Monad f =>
SelectM f a -> SelectM f b -> SelectM f b
>>= :: SelectM f a -> (a -> SelectM f b) -> SelectM f b
$c>>= :: forall (f :: * -> *) a b.
Monad f =>
SelectM f a -> (a -> SelectM f b) -> SelectM f b
$cp1Monad :: forall (f :: * -> *). Monad f => Applicative (SelectM f)
Monad)
instance Monad f => Selective (SelectM f) where
select :: SelectM f (Either a b) -> SelectM f (a -> b) -> SelectM f b
select = SelectM f (Either a b) -> SelectM f (a -> b) -> SelectM f b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
newtype Over m a = Over { Over m a -> m
getOver :: m }
deriving (Over m a -> Over m a -> Bool
(Over m a -> Over m a -> Bool)
-> (Over m a -> Over m a -> Bool) -> Eq (Over m a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall m a. Eq m => Over m a -> Over m a -> Bool
/= :: Over m a -> Over m a -> Bool
$c/= :: forall m a. Eq m => Over m a -> Over m a -> Bool
== :: Over m a -> Over m a -> Bool
$c== :: forall m a. Eq m => Over m a -> Over m a -> Bool
Eq, (a -> b) -> Over m a -> Over m b
(forall a b. (a -> b) -> Over m a -> Over m b)
-> (forall a b. a -> Over m b -> Over m a) -> Functor (Over m)
forall a b. a -> Over m b -> Over m a
forall a b. (a -> b) -> Over m a -> Over m b
forall m a b. a -> Over m b -> Over m a
forall m a b. (a -> b) -> Over m a -> Over m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Over m b -> Over m a
$c<$ :: forall m a b. a -> Over m b -> Over m a
fmap :: (a -> b) -> Over m a -> Over m b
$cfmap :: forall m a b. (a -> b) -> Over m a -> Over m b
Functor, Eq (Over m a)
Eq (Over m a)
-> (Over m a -> Over m a -> Ordering)
-> (Over m a -> Over m a -> Bool)
-> (Over m a -> Over m a -> Bool)
-> (Over m a -> Over m a -> Bool)
-> (Over m a -> Over m a -> Bool)
-> (Over m a -> Over m a -> Over m a)
-> (Over m a -> Over m a -> Over m a)
-> Ord (Over m a)
Over m a -> Over m a -> Bool
Over m a -> Over m a -> Ordering
Over m a -> Over m a -> Over m a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall m a. Ord m => Eq (Over m a)
forall m a. Ord m => Over m a -> Over m a -> Bool
forall m a. Ord m => Over m a -> Over m a -> Ordering
forall m a. Ord m => Over m a -> Over m a -> Over m a
min :: Over m a -> Over m a -> Over m a
$cmin :: forall m a. Ord m => Over m a -> Over m a -> Over m a
max :: Over m a -> Over m a -> Over m a
$cmax :: forall m a. Ord m => Over m a -> Over m a -> Over m a
>= :: Over m a -> Over m a -> Bool
$c>= :: forall m a. Ord m => Over m a -> Over m a -> Bool
> :: Over m a -> Over m a -> Bool
$c> :: forall m a. Ord m => Over m a -> Over m a -> Bool
<= :: Over m a -> Over m a -> Bool
$c<= :: forall m a. Ord m => Over m a -> Over m a -> Bool
< :: Over m a -> Over m a -> Bool
$c< :: forall m a. Ord m => Over m a -> Over m a -> Bool
compare :: Over m a -> Over m a -> Ordering
$ccompare :: forall m a. Ord m => Over m a -> Over m a -> Ordering
$cp1Ord :: forall m a. Ord m => Eq (Over m a)
Ord, Int -> Over m a -> ShowS
[Over m a] -> ShowS
Over m a -> [Char]
(Int -> Over m a -> ShowS)
-> (Over m a -> [Char]) -> ([Over m a] -> ShowS) -> Show (Over m a)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall m a. Show m => Int -> Over m a -> ShowS
forall m a. Show m => [Over m a] -> ShowS
forall m a. Show m => Over m a -> [Char]
showList :: [Over m a] -> ShowS
$cshowList :: forall m a. Show m => [Over m a] -> ShowS
show :: Over m a -> [Char]
$cshow :: forall m a. Show m => Over m a -> [Char]
showsPrec :: Int -> Over m a -> ShowS
$cshowsPrec :: forall m a. Show m => Int -> Over m a -> ShowS
Show)
instance Monoid m => Applicative (Over m) where
pure :: a -> Over m a
pure a
_ = m -> Over m a
forall m a. m -> Over m a
Over m
forall a. Monoid a => a
mempty
Over m
x <*> :: Over m (a -> b) -> Over m a -> Over m b
<*> Over m
y = m -> Over m b
forall m a. m -> Over m a
Over (m -> m -> m
forall a. Monoid a => a -> a -> a
mappend m
x m
y)
instance Monoid m => Selective (Over m) where
select :: Over m (Either a b) -> Over m (a -> b) -> Over m b
select (Over m
x) (Over m
y) = m -> Over m b
forall m a. m -> Over m a
Over (m -> m -> m
forall a. Monoid a => a -> a -> a
mappend m
x m
y)
newtype Under m a = Under { Under m a -> m
getUnder :: m }
deriving (Under m a -> Under m a -> Bool
(Under m a -> Under m a -> Bool)
-> (Under m a -> Under m a -> Bool) -> Eq (Under m a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall m a. Eq m => Under m a -> Under m a -> Bool
/= :: Under m a -> Under m a -> Bool
$c/= :: forall m a. Eq m => Under m a -> Under m a -> Bool
== :: Under m a -> Under m a -> Bool
$c== :: forall m a. Eq m => Under m a -> Under m a -> Bool
Eq, (a -> b) -> Under m a -> Under m b
(forall a b. (a -> b) -> Under m a -> Under m b)
-> (forall a b. a -> Under m b -> Under m a) -> Functor (Under m)
forall a b. a -> Under m b -> Under m a
forall a b. (a -> b) -> Under m a -> Under m b
forall m a b. a -> Under m b -> Under m a
forall m a b. (a -> b) -> Under m a -> Under m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Under m b -> Under m a
$c<$ :: forall m a b. a -> Under m b -> Under m a
fmap :: (a -> b) -> Under m a -> Under m b
$cfmap :: forall m a b. (a -> b) -> Under m a -> Under m b
Functor, Eq (Under m a)
Eq (Under m a)
-> (Under m a -> Under m a -> Ordering)
-> (Under m a -> Under m a -> Bool)
-> (Under m a -> Under m a -> Bool)
-> (Under m a -> Under m a -> Bool)
-> (Under m a -> Under m a -> Bool)
-> (Under m a -> Under m a -> Under m a)
-> (Under m a -> Under m a -> Under m a)
-> Ord (Under m a)
Under m a -> Under m a -> Bool
Under m a -> Under m a -> Ordering
Under m a -> Under m a -> Under m a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall m a. Ord m => Eq (Under m a)
forall m a. Ord m => Under m a -> Under m a -> Bool
forall m a. Ord m => Under m a -> Under m a -> Ordering
forall m a. Ord m => Under m a -> Under m a -> Under m a
min :: Under m a -> Under m a -> Under m a
$cmin :: forall m a. Ord m => Under m a -> Under m a -> Under m a
max :: Under m a -> Under m a -> Under m a
$cmax :: forall m a. Ord m => Under m a -> Under m a -> Under m a
>= :: Under m a -> Under m a -> Bool
$c>= :: forall m a. Ord m => Under m a -> Under m a -> Bool
> :: Under m a -> Under m a -> Bool
$c> :: forall m a. Ord m => Under m a -> Under m a -> Bool
<= :: Under m a -> Under m a -> Bool
$c<= :: forall m a. Ord m => Under m a -> Under m a -> Bool
< :: Under m a -> Under m a -> Bool
$c< :: forall m a. Ord m => Under m a -> Under m a -> Bool
compare :: Under m a -> Under m a -> Ordering
$ccompare :: forall m a. Ord m => Under m a -> Under m a -> Ordering
$cp1Ord :: forall m a. Ord m => Eq (Under m a)
Ord, Int -> Under m a -> ShowS
[Under m a] -> ShowS
Under m a -> [Char]
(Int -> Under m a -> ShowS)
-> (Under m a -> [Char])
-> ([Under m a] -> ShowS)
-> Show (Under m a)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall m a. Show m => Int -> Under m a -> ShowS
forall m a. Show m => [Under m a] -> ShowS
forall m a. Show m => Under m a -> [Char]
showList :: [Under m a] -> ShowS
$cshowList :: forall m a. Show m => [Under m a] -> ShowS
show :: Under m a -> [Char]
$cshow :: forall m a. Show m => Under m a -> [Char]
showsPrec :: Int -> Under m a -> ShowS
$cshowsPrec :: forall m a. Show m => Int -> Under m a -> ShowS
Show)
instance Monoid m => Applicative (Under m) where
pure :: a -> Under m a
pure a
_ = m -> Under m a
forall m a. m -> Under m a
Under m
forall a. Monoid a => a
mempty
Under m
x <*> :: Under m (a -> b) -> Under m a -> Under m b
<*> Under m
y = m -> Under m b
forall m a. m -> Under m a
Under (m -> m -> m
forall a. Monoid a => a -> a -> a
mappend m
x m
y)
instance Monoid m => Selective (Under m) where
select :: Under m (Either a b) -> Under m (a -> b) -> Under m b
select (Under m
m) Under m (a -> b)
_ = m -> Under m b
forall m a. m -> Under m a
Under m
m
instance Selective ZipList where select :: ZipList (Either a b) -> ZipList (a -> b) -> ZipList b
select = ZipList (Either a b) -> ZipList (a -> b) -> ZipList b
forall (f :: * -> *) a b.
Applicative f =>
f (Either a b) -> f (a -> b) -> f b
selectA
data Validation e a = Failure e | Success a deriving (a -> Validation e b -> Validation e a
(a -> b) -> Validation e a -> Validation e b
(forall a b. (a -> b) -> Validation e a -> Validation e b)
-> (forall a b. a -> Validation e b -> Validation e a)
-> Functor (Validation e)
forall a b. a -> Validation e b -> Validation e a
forall a b. (a -> b) -> Validation e a -> Validation e b
forall e a b. a -> Validation e b -> Validation e a
forall e a b. (a -> b) -> Validation e a -> Validation e b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Validation e b -> Validation e a
$c<$ :: forall e a b. a -> Validation e b -> Validation e a
fmap :: (a -> b) -> Validation e a -> Validation e b
$cfmap :: forall e a b. (a -> b) -> Validation e a -> Validation e b
Functor, Int -> Validation e a -> ShowS
[Validation e a] -> ShowS
Validation e a -> [Char]
(Int -> Validation e a -> ShowS)
-> (Validation e a -> [Char])
-> ([Validation e a] -> ShowS)
-> Show (Validation e a)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall e a. (Show e, Show a) => Int -> Validation e a -> ShowS
forall e a. (Show e, Show a) => [Validation e a] -> ShowS
forall e a. (Show e, Show a) => Validation e a -> [Char]
showList :: [Validation e a] -> ShowS
$cshowList :: forall e a. (Show e, Show a) => [Validation e a] -> ShowS
show :: Validation e a -> [Char]
$cshow :: forall e a. (Show e, Show a) => Validation e a -> [Char]
showsPrec :: Int -> Validation e a -> ShowS
$cshowsPrec :: forall e a. (Show e, Show a) => Int -> Validation e a -> ShowS
Show)
instance Semigroup e => Applicative (Validation e) where
pure :: a -> Validation e a
pure = a -> Validation e a
forall e a. a -> Validation e a
Success
Failure e
e1 <*> :: Validation e (a -> b) -> Validation e a -> Validation e b
<*> Failure e
e2 = e -> Validation e b
forall e a. e -> Validation e a
Failure (e
e1 e -> e -> e
forall a. Semigroup a => a -> a -> a
<> e
e2)
Failure e
e1 <*> Success a
_ = e -> Validation e b
forall e a. e -> Validation e a
Failure e
e1
Success a -> b
_ <*> Failure e
e2 = e -> Validation e b
forall e a. e -> Validation e a
Failure e
e2
Success a -> b
f <*> Success a
a = b -> Validation e b
forall e a. a -> Validation e a
Success (a -> b
f a
a)
instance Semigroup e => Selective (Validation e) where
select :: Validation e (Either a b)
-> Validation e (a -> b) -> Validation e b
select (Success (Left a
a)) Validation e (a -> b)
f = ((a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$a
a) ((a -> b) -> b) -> Validation e (a -> b) -> Validation e b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Validation e (a -> b)
f
select (Success (Right b
b)) Validation e (a -> b)
_ = b -> Validation e b
forall e a. a -> Validation e a
Success b
b
select (Failure e
e ) Validation e (a -> b)
_ = e -> Validation e b
forall e a. e -> Validation e a
Failure e
e
instance (Selective f, Selective g) => Selective (Product f g) where
select :: Product f g (Either a b) -> Product f g (a -> b) -> Product f g b
select (Pair f (Either a b)
fx g (Either a b)
gx) (Pair f (a -> b)
fy g (a -> b)
gy) = f b -> g b -> Product f g b
forall k (f :: k -> *) (g :: k -> *) (a :: k).
f a -> g a -> Product f g a
Pair (f (Either a b) -> f (a -> b) -> f b
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select f (Either a b)
fx f (a -> b)
fy) (g (Either a b) -> g (a -> b) -> g b
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select g (Either a b)
gx g (a -> b)
gy)
instance (Applicative f, Selective g) => Selective (Compose f g) where
select :: Compose f g (Either a b) -> Compose f g (a -> b) -> Compose f g b
select (Compose f (g (Either a b))
x) (Compose f (g (a -> b))
y) = f (g b) -> Compose f g b
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (g (Either a b) -> g (a -> b) -> g b
forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
select (g (Either a b) -> g (a -> b) -> g b)
-> f (g (Either a b)) -> f (g (a -> b) -> g b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (g (Either a b))
x f (g (a -> b) -> g b) -> f (g (a -> b)) -> f (g b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f (g (a -> b))
y)
instance Selective IO where select :: IO (Either a b) -> IO (a -> b) -> IO b
select = IO (Either a b) -> IO (a -> b) -> IO b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective [] where select :: [Either a b] -> [a -> b] -> [b]
select = [Either a b] -> [a -> b] -> [b]
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Monoid a => Selective ((,) a) where select :: (a, Either a b) -> (a, a -> b) -> (a, b)
select = (a, Either a b) -> (a, a -> b) -> (a, b)
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective ((->) a) where select :: (a -> Either a b) -> (a -> a -> b) -> a -> b
select = (a -> Either a b) -> (a -> a -> b) -> a -> b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective (Either e) where select :: Either e (Either a b) -> Either e (a -> b) -> Either e b
select = Either e (Either a b) -> Either e (a -> b) -> Either e b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective Identity where select :: Identity (Either a b) -> Identity (a -> b) -> Identity b
select = Identity (Either a b) -> Identity (a -> b) -> Identity b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective Maybe where select :: Maybe (Either a b) -> Maybe (a -> b) -> Maybe b
select = Maybe (Either a b) -> Maybe (a -> b) -> Maybe b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective NonEmpty where select :: NonEmpty (Either a b) -> NonEmpty (a -> b) -> NonEmpty b
select = NonEmpty (Either a b) -> NonEmpty (a -> b) -> NonEmpty b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective Proxy where select :: Proxy (Either a b) -> Proxy (a -> b) -> Proxy b
select = Proxy (Either a b) -> Proxy (a -> b) -> Proxy b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective (ST s) where select :: ST s (Either a b) -> ST s (a -> b) -> ST s b
select = ST s (Either a b) -> ST s (a -> b) -> ST s b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective STM where select :: STM (Either a b) -> STM (a -> b) -> STM b
select = STM (Either a b) -> STM (a -> b) -> STM b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Selective (ContT r m) where select :: ContT r m (Either a b) -> ContT r m (a -> b) -> ContT r m b
select = ContT r m (Either a b) -> ContT r m (a -> b) -> ContT r m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Monad m => Selective (ExceptT e m) where select :: ExceptT e m (Either a b) -> ExceptT e m (a -> b) -> ExceptT e m b
select = ExceptT e m (Either a b) -> ExceptT e m (a -> b) -> ExceptT e m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Monad m => Selective (IdentityT m) where select :: IdentityT m (Either a b) -> IdentityT m (a -> b) -> IdentityT m b
select = IdentityT m (Either a b) -> IdentityT m (a -> b) -> IdentityT m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Monad m => Selective (MaybeT m) where select :: MaybeT m (Either a b) -> MaybeT m (a -> b) -> MaybeT m b
select = MaybeT m (Either a b) -> MaybeT m (a -> b) -> MaybeT m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Monad m => Selective (ReaderT r m) where select :: ReaderT r m (Either a b) -> ReaderT r m (a -> b) -> ReaderT r m b
select = ReaderT r m (Either a b) -> ReaderT r m (a -> b) -> ReaderT r m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance (Monoid w, Monad m) => Selective (RWST r w s m) where select :: RWST r w s m (Either a b)
-> RWST r w s m (a -> b) -> RWST r w s m b
select = RWST r w s m (Either a b)
-> RWST r w s m (a -> b) -> RWST r w s m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance (Monoid w, Monad m) => Selective (S.RWST r w s m) where select :: RWST r w s m (Either a b)
-> RWST r w s m (a -> b) -> RWST r w s m b
select = RWST r w s m (Either a b)
-> RWST r w s m (a -> b) -> RWST r w s m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Monad m => Selective (StateT s m) where select :: StateT s m (Either a b) -> StateT s m (a -> b) -> StateT s m b
select = StateT s m (Either a b) -> StateT s m (a -> b) -> StateT s m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance Monad m => Selective (S.StateT s m) where select :: StateT s m (Either a b) -> StateT s m (a -> b) -> StateT s m b
select = StateT s m (Either a b) -> StateT s m (a -> b) -> StateT s m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance (Monoid w, Monad m) => Selective (WriterT w m) where select :: WriterT w m (Either a b) -> WriterT w m (a -> b) -> WriterT w m b
select = WriterT w m (Either a b) -> WriterT w m (a -> b) -> WriterT w m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance (Monoid w, Monad m) => Selective (S.WriterT w m) where select :: WriterT w m (Either a b) -> WriterT w m (a -> b) -> WriterT w m b
select = WriterT w m (Either a b) -> WriterT w m (a -> b) -> WriterT w m b
forall (f :: * -> *) a b.
Monad f =>
f (Either a b) -> f (a -> b) -> f b
selectM
instance ArrowChoice a => Selective (ArrowMonad a) where
select :: ArrowMonad a (Either a b)
-> ArrowMonad a (a -> b) -> ArrowMonad a b
select (ArrowMonad a () (Either a b)
x) ArrowMonad a (a -> b)
y = a () b -> ArrowMonad a b
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (a () b -> ArrowMonad a b) -> a () b -> ArrowMonad a b
forall a b. (a -> b) -> a -> b
$ a () (Either a b)
x a () (Either a b) -> a (Either a b) b -> a () b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (ArrowMonad a (a -> b) -> a a b
forall (a :: * -> * -> *) i o.
Arrow a =>
ArrowMonad a (i -> o) -> a i o
toArrow ArrowMonad a (a -> b)
y a a b -> a b b -> a (Either a b) b
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| a b b
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA)
toArrow :: Arrow a => ArrowMonad a (i -> o) -> a i o
toArrow :: ArrowMonad a (i -> o) -> a i o
toArrow (ArrowMonad a () (i -> o)
f) = (i -> ((), i)) -> a i ((), i)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((),) a i ((), i) -> a ((), i) o -> a i o
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a () (i -> o) -> a ((), i) (i -> o, i)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first a () (i -> o)
f a ((), i) (i -> o, i) -> a (i -> o, i) o -> a ((), i) o
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((i -> o, i) -> o) -> a (i -> o, i) o
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (((i -> o) -> i -> o) -> (i -> o, i) -> o
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (i -> o) -> i -> o
forall a b. (a -> b) -> a -> b
($))
newtype ComposeEither f e a = ComposeEither (f (Either e a))
deriving a -> ComposeEither f e b -> ComposeEither f e a
(a -> b) -> ComposeEither f e a -> ComposeEither f e b
(forall a b.
(a -> b) -> ComposeEither f e a -> ComposeEither f e b)
-> (forall a b. a -> ComposeEither f e b -> ComposeEither f e a)
-> Functor (ComposeEither f e)
forall a b. a -> ComposeEither f e b -> ComposeEither f e a
forall a b. (a -> b) -> ComposeEither f e a -> ComposeEither f e b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
forall (f :: * -> *) e a b.
Functor f =>
a -> ComposeEither f e b -> ComposeEither f e a
forall (f :: * -> *) e a b.
Functor f =>
(a -> b) -> ComposeEither f e a -> ComposeEither f e b
<$ :: a -> ComposeEither f e b -> ComposeEither f e a
$c<$ :: forall (f :: * -> *) e a b.
Functor f =>
a -> ComposeEither f e b -> ComposeEither f e a
fmap :: (a -> b) -> ComposeEither f e a -> ComposeEither f e b
$cfmap :: forall (f :: * -> *) e a b.
Functor f =>
(a -> b) -> ComposeEither f e a -> ComposeEither f e b
Functor
instance Applicative f => Applicative (ComposeEither f e) where
pure :: a -> ComposeEither f e a
pure a
a = f (Either e a) -> ComposeEither f e a
forall (f :: * -> *) e a. f (Either e a) -> ComposeEither f e a
ComposeEither (Either e a -> f (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> f (Either e a)) -> Either e a -> f (Either e a)
forall a b. (a -> b) -> a -> b
$ a -> Either e a
forall a b. b -> Either a b
Right a
a)
ComposeEither f (Either e (a -> b))
x <*> :: ComposeEither f e (a -> b)
-> ComposeEither f e a -> ComposeEither f e b
<*> ComposeEither f (Either e a)
y = f (Either e b) -> ComposeEither f e b
forall (f :: * -> *) e a. f (Either e a) -> ComposeEither f e a
ComposeEither (Either e (a -> b) -> Either e a -> Either e b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) (Either e (a -> b) -> Either e a -> Either e b)
-> f (Either e (a -> b)) -> f (Either e a -> Either e b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Either e (a -> b))
x f (Either e a -> Either e b) -> f (Either e a) -> f (Either e b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f (Either e a)
y)
instance (Selective f, Monoid e
#if !MIN_VERSION_base(4,11,0)
, Semigroup e
#endif
) => Alternative (ComposeEither f e) where
empty :: ComposeEither f e a
empty = f (Either e a) -> ComposeEither f e a
forall (f :: * -> *) e a. f (Either e a) -> ComposeEither f e a
ComposeEither (Either e a -> f (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> f (Either e a)) -> Either e a -> f (Either e a)
forall a b. (a -> b) -> a -> b
$ e -> Either e a
forall a b. a -> Either a b
Left e
forall a. Monoid a => a
mempty)
ComposeEither f (Either e a)
x <|> :: ComposeEither f e a -> ComposeEither f e a -> ComposeEither f e a
<|> ComposeEither f (Either e a)
y = f (Either e a) -> ComposeEither f e a
forall (f :: * -> *) e a. f (Either e a) -> ComposeEither f e a
ComposeEither (f (Either e a)
x f (Either e a) -> f (Either e a) -> f (Either e a)
forall (f :: * -> *) e a.
(Selective f, Semigroup e) =>
f (Either e a) -> f (Either e a) -> f (Either e a)
`orElse` f (Either e a)
y)