!0V      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~(c) Andrey Mokhov 2018-2019MIT (see the file LICENSE)andrey.mokhov@gmail.com experimentalNone4MPe2  selectiveSelective instance for the standard applicative functor Validation. This is a good example of a non-trivial selective functor which is not a monad. selective?Static analysis of selective functors with under-approximation. selective>Static analysis of selective functors with over-approximation.  selectiveAny monad can be given a  instance by defining  = S. This data type captures this pattern, so you can use it in combination with the  DerivingVia extension as follows: _newtype V1 a = V1 a deriving (Functor, Applicative, Selective, Monad) via SelectM Identity  selective'Any applicative functor can be given a  instance by defining  = S. This data type captures this pattern, so you can use it in combination with the  DerivingVia extension as follows: _newtype Over m a = Over m deriving (Functor, Applicative, Selective) via SelectA (Const m)  selective7A list of values, equipped with a fast membership test. selective1Selective applicative functors. You can think of B as a selective function application: when given a value of type  a, you  must apply' the given function, but when given a  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  is reminiscent of both  and d, and indeed a selective functor is in some sense a composition of an applicative functor and the  monad.Laws: Identity: #x <*? pure id = either id id <$> x Distributivity; note that y and z have the same type  f (a -> b): 7pure 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 / (for selective functors that are also monads): select = selectM %There are also a few useful theorems:$Apply a pure function to the result: 8f <$> select x y = select (fmap f <$> x) (fmap f <$> y) Apply a pure function to the Left case of the first argument: 2select (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  = . The following  interchange( law holds for rigid selective functors:  x *> (y <*? z) = (x *> y) <*? z If f is also a , we require that  = , from which one can prove  = . selectiveAn operator alias for S, which is sometimes convenient. It tries to follow the notational convention for  operators. The angle bracket pointing to the left means we always use the corresponding value. The value on the right, however, may be skipped, hence the question mark. selectiveThe ) function is a natural generalisation of : instead of skipping an unnecessary effect, it chooses which of the two given effectful functions to apply to a given argument; the other effect is unnecessary. It is possible to implement  in terms of +, which is a good puzzle (give it a try!).We can also implement  via : aselectB :: Selective f => f (Either a b) -> f (a -> b) -> f b selectB x y = branch x y (pure id)  selective3We can write a function with the type signature of  using the  type class, but it will always execute the effects associated with the second argument, hence being potentially less efficient. selective!Recover the application operator  from . Rigid$ selective functors satisfy the law  = J and furthermore, the resulting applicative functor satisfies all laws of : Identity: pure id <*> v = v Homomorphism: pure f <*> pure x = pure (f x) Interchange: u <*> pure y = pure ($y) <*> u Composition: '(.) <$> u <*> v <*> w = u <*> (v <*> w) selective#One can easily implement a monadic % that satisfies the laws, hence any  is . selective8Branch on a Boolean value, skipping unnecessary effects. selectiveEliminate a specified value a from f (Either a b) by replacing it with a given f b. selective;The list of all possible values of an enumerable data type. selectiveEmbed a list of values into 6 using the trivial but slow membership test based on . selectiveEliminate all specified values a from f (Either a b)) by replacing each of them with a given f a. selectiveEliminate all specified values a from f (Either a b)) by replacing each of them with a given f a. selectiveAA restricted version of monadic bind. Fails with an error if the  and  instances for a do not cover all values of a. selective Conditionally perform an effect. selectiveA lifted version of . selectiveReturn the first Right value. If both are Left's, accumulate errors.  selectiveAccumulate the Right values, or return the first Left. selectiveSwap Left and Right. selective*Append two semigroup values or return the Right one.! selective4Keep checking an effectful condition while it holds." selective9Keep running an effectful computation until it returns a Right value, collecting the Left's using a supplied Monoid instance.# selective$A lifted version of lazy Boolean OR.$ selective%A lifted version of lazy Boolean AND.% selectiveA lifted version of ). Retains the short-circuiting behaviour.& selectiveA lifted version of ). Retains the short-circuiting behaviour.' selective8Generalised folding with the short-circuiting behaviour.(  !"#$%&'( "!#$'%&  4(c) Andrey Mokhov 2018-2019MIT (see the file LICENSE)andrey.mokhov@gmail.com experimentalNoneSXr:] selectiveFree selective functors._ selective1Lift a functor into a free selective computation.` selective$Given a natural transformation from f to g6, this gives a canonical natural transformation from Select f to g+. Note that here we rely on the fact that g is a lawful selective functor.a selective8Concatenate all effects of a free selective computation.b selective>Extract the resulting value if there are no necessary effects.c selectiveCollect all possible effects; in the order they appear in a free selective computation.d selectiveExtract all necessary effects; in the order they appear in a free selective computation.]^_`abcd]^_bcd`a(c) Andrey Mokhov 2018-2019MIT (see the file LICENSE)andrey.mokhov@gmail.com experimentalNone&'PSX~h selectiveFree rigid selective functors.k selective1Lift a functor into a free selective computation.l selective$Given a natural transformation from f to g6, this gives a canonical natural transformation from Select f to g.m selective8Concatenate all effects of a free selective computation.n selective>Extract the resulting value if there are no necessary effects.o selectiveWCollect all possible effects in the order they appear in a free selective computation.p selectiveExtract the necessary effect from a free selective computation. Note: there can be at most one effect that is statically guaranteed to be necessary. hjiklmnop hjiknoplm(c) Andrey Mokhov 2018-2019MIT (see the file LICENSE)andrey.mokhov@gmail.com experimentalNone&'4SX t selectiveFree rigid selective functors.w selective1Lift a functor into a free selective computation.x selective$Given a natural transformation from f to g6, this gives a canonical natural transformation from Select f to g.y selective8Concatenate all effects of a free selective computation.z selective>Extract the resulting value if there are no necessary effects.{ selectiveWCollect all possible effects in the order they appear in a free selective computation.| selectiveExtract the necessary effect from a free selective computation. Note: there can be at most one effect that is statically guaranteed to be necessary. tvuwxyz{| tvuwz{|xy        !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_``abcdefghi`j`abcdekghi`j`abcdekghilmnlmolpqlprlmslptlpuvlwxlyzly{|}lw~lw$selective-0.3-1y485WjazJzDKnmH82jYQ5Control.SelectiveControl.Selective.FreeControl.Selective.Rigid.FreeControl.Selective.Rigid.Freer Data.Maybe fromMaybe ValidationFailureSuccessUndergetUnderOvergetOverSelectM getSelectMSelectA getSelectACases Selectiveselect<*?branchselectAapSselectMifS casesEnumcasesmatchSmatchMbindSwhenS fromMaybeSorElseandAlsowhileS untilRight<||><&&>anySallSfoldS$fSelectiveArrowMonad$fSelectiveWriterT$fSelectiveWriterT0$fSelectiveStateT$fSelectiveStateT0$fSelectiveRWST$fSelectiveRWST0$fSelectiveReaderT$fSelectiveMaybeT$fSelectiveIdentityT$fSelectiveExceptT$fSelectiveContT$fSelectiveSTM $fSelectiveST$fSelectiveProxy$fSelectiveNonEmpty$fSelectiveMaybe$fSelectiveIdentity$fSelectiveEither $fSelective->$fSelective(,) $fSelective[] $fSelectiveIO$fSelectiveCompose$fSelectiveProduct$fSelectiveZipList$fSelectiveLift$fSelectiveSelectA$fSelectiveSelectM$fSelectiveOver$fApplicativeOver$fSelectiveUnder$fApplicativeUnder$fSelectiveValidation$fApplicativeValidation$fAlternativeComposeEither$fApplicativeComposeEither$fFunctorSelectA$fApplicativeSelectA$fFunctorSelectM$fApplicativeSelectM$fMonadSelectM$fEqOver $fFunctorOver $fOrdOver $fShowOver $fEqUnder$fFunctorUnder $fOrdUnder $fShowUnder$fFunctorValidation$fShowValidation$fFunctorComposeEitherSelect liftSelect runSelect foldSelectgetPure getEffectsgetNecessaryEffects$fSelectiveSelect$fApplicativeSelect$fFunctorSelectPuregetNecessaryEffectbase Data.EitherLeftRightGHC.Base<*>>>=EitherMonad Applicative eliminate Data.FoldableelemGHC.EnumBoundedEnum swapEither appendLeftanyall