Selective applicative functors. You can think of select as a selective
function application: when given a value of type Left a, you must apply
the given function, but when given a Right b, you may skip the
function and associated effects, and simply return the b.
Note that it is not a requirement for selective functors to skip unnecessary effects. It may be counterintuitive, but this makes them more useful. Why? Typically, when executing a selective computation, you would want to skip the effects (saving work); but on the other hand, if your goal is to statically analyse a given selective computation and extract the set of all possible effects (without actually executing them), then you do not want to skip any effects, because that defeats the purpose of static analysis.
The type signature of select is reminiscent of both <*> and >>=, and indeed a selective functor is in some sense a composition of an applicative functor and the Either monad.
Laws:
Identity:
x <*? pure id = either id id <$> x
Distributivity; note that
yandzhave the same typef (a -> b):
pure x <*? (y *> z) = (pure x <*? y) *> (pure x <*? z)
Associativity:
x <*? (y <*? z) = (f <$> x) <*? (g <$> y) <*? (h <$> z)
where
f x = Right <$> x
g y = a -> bimap (,a) ($a) y
h z = uncurry z
Monadic select (for selective functors that are also monads):
select = selectM
There are also a few useful theorems:
Apply a pure function to the result:
f <$> select x y = select (fmap f <$> x) (fmap f <$> y)
Apply a pure function to the
Leftcase of the first argument:
select (first f <$> x) y = select x ((. f) <$> y)
Apply a pure function to the second argument:
select x (f <$> y) = select (first (flip f) <$> x) ((&) <$> y)
Generalised identity:
x <*? pure y = either y id <$> x
A selective functor is rigid if it satisfies <*>
=apS. The following interchange law holds for rigid selective functors:
x *> (y <*? z) = (x *> y) <*? z
If f is also a Monad, we require that select = selectM, from which one
can prove <*> = apS.
Instances37Selective, …
Selective NonEmptyDefined in selective-0.7.0.1 · Control.SelectiveSelective STMDefined in selective-0.7.0.1 · Control.SelectiveSelective IdentityDefined in selective-0.7.0.1 · Control.SelectiveSelective ZipListDefined in selective-0.7.0.1 · Control.SelectiveSelective MaybeDefined in selective-0.7.0.1 · Control.SelectiveSelective IODefined in selective-0.7.0.1 · Control.SelectiveSelective []Defined in selective-0.7.0.1 · Control.SelectiveApplicative f => Selective (SelectA f)Defined in selective-0.7.0.1 · Control.SelectiveFunctor f => Selective (Select f)Defined in selective-0.7.0.1 · Control.Selective.Rigid.FreeMonad f => Selective (SelectM f)Defined in selective-0.7.0.1 · Control.SelectiveMonad m => Selective (MaybeT m)Defined in selective-0.7.0.1 · Control.SelectiveMonoid a => Selective (Tuple2 a)Defined in selective-0.7.0.1 · Control.SelectiveMonoid m => Selective (Over m)Defined in selective-0.7.0.1 · Control.SelectiveMonoid m => Selective (Under m)Defined in selective-0.7.0.1 · Control.SelectiveSemigroup e => Selective (Validation e)Defined in selective-0.7.0.1 · Control.SelectiveArrowChoice a => Selective (ArrowMonad a)Defined in selective-0.7.0.1 · Control.SelectiveSelective ProxyDefined in selective-0.7.0.1 · Control.SelectiveSelective (Either e)Defined in selective-0.7.0.1 · Control.SelectiveSelective (ST s)Defined in selective-0.7.0.1 · Control.SelectiveSelective (Select f)Defined in selective-0.7.0.1 · Control.Selective.FreeSelective (Select f)Defined in selective-0.7.0.1 · Control.Selective.Rigid.FreerSelective f => Selective (Lift f)Defined in selective-0.7.0.1 · Control.SelectiveMonad m => Selective (StateT s m)Defined in selective-0.7.0.1 · Control.SelectiveMonad m => Selective (StateT s m)Defined in selective-0.7.0.1 · Control.SelectiveSelective f => Selective (ComposeEither f e)Defined in selective-0.7.0.1 · Control.SelectiveSelective f => Selective (ExceptT e f)Defined in selective-0.7.0.1 · Control.Selective.Trans.ExceptSelective f => Selective (IdentityT f)Defined in selective-0.7.0.1 · Control.SelectiveSelective f => Selective (ReaderT env f)Defined in selective-0.7.0.1 · Control.Selective(Monoid w, Selective f) => Selective (WriterT w f)Defined in selective-0.7.0.1 · Control.Selective(Monoid w, Selective f) => Selective (WriterT w f)Defined in selective-0.7.0.1 · Control.Selective(Selective f, Applicative g, Traversable g) => Selective (ComposeTraversable f g)Defined in selective-0.7.0.1 · Control.SelectiveSelective (ContT r m)Defined in selective-0.7.0.1 · Control.SelectiveSelective ((->) a)Defined in selective-0.7.0.1 · Control.Selective(Selective f, Selective g) => Selective (Product f g)Defined in selective-0.7.0.1 · Control.Selective(Applicative f, Selective g) => Selective (Compose f g)Defined in selective-0.7.0.1 · Control.Selective(Monoid w, Monad m) => Selective (RWST r w s m)Defined in selective-0.7.0.1 · Control.Selective(Monoid w, Monad m) => Selective (RWST r w s m)Defined in selective-0.7.0.1 · Control.Selective