A functor with application, providing operations to
A minimal complete definition must include implementations of pure and of either <*> or liftA2. If it defines both, then they must behave the same as their default definitions:
(<*>) = liftA2 idliftA2 f x y = f <$> x <*> yFurther, any definition must satisfy the following:
- Identity
pure id <*> v = v- Composition
pure (.) <*> u <*> v <*> w = u <*> (v <*> w)- Homomorphism
pure f <*> pure x = pure (f x)- Interchange
u <*> pure y = pure ($ y) <*> u
The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:
As a consequence of these laws, the Functor instance for f will satisfy
It may be useful to note that supposing
forall x y. p (q x y) = f x . g yit follows from the above that
liftA2 p (liftA2 q u v) = liftA2 f u . liftA2 g vIf f is also a Monad, it should satisfy
(which implies that pure and <*> satisfy the applicative functor laws).
Methods
pure :: a -> f aLift a value into the Structure.
Examples
Example1 expression pure 1 :: Maybe IntJust 1
Example1 expression pure 'z' :: [Char]"z"
Example1 expression pure (pure ":D") :: Maybe [String]Just [":D"]
(<*>) :: f (a -> b) -> f a -> f binfixl 4Sequential application.
A few functors support an implementation of <*> that is more efficient than the default one.
Example
Used in combination with
,(Data.Functor.<$>)can be used to build a record.(<*>)Example1 expression data MyState = MyState {arg1 :: Foo, arg2 :: Bar, arg3 :: Baz}Example3 expressions produceFoo :: Applicative f => f FooproduceBar :: Applicative f => f BarproduceBaz :: Applicative f => f Baz
Example2 expressions mkState :: Applicative f => f MyStatemkState = MyState <$> produceFoo <*> produceBar <*> produceBaz
liftA2 :: (a -> b -> c) -> f a -> f b -> f cLift a binary function to actions.
Some functors support an implementation of liftA2 that is more efficient than the default one. In particular, if fmap is an expensive operation, it is likely better to use liftA2 than to fmap over the structure and then use <*>.
This became a typeclass method in 4.10.0.0. Prior to that, it was a function defined in terms of <*> and fmap.
Example
Example1 expression liftA2 (,) (Just 3) (Just 5)Just (3,5)
Example1 expression liftA2 (+) [1, 2, 3] [4, 5, 6][5,6,7,6,7,8,7,8,9]
(*>) :: f a -> f b -> f binfixl 4Sequence actions, discarding the value of the first argument.
Examples
If used in conjunction with the Applicative instance for Maybe, you can chain Maybe computations, with a possible "early return" in case of Nothing.
Example1 expression Just 2 *> Just 3Just 3
Example1 expression Nothing *> Just 3Nothing
Of course a more interesting use case would be to have effectful computations instead of just returning pure values.
Example4 expressions import Data.Charimport GHC.Internal.Text.ParserCombinators.ReadPlet p = string "my name is " *> munch1 isAlpha <* eofreadP_to_S p "my name is Simon"[("Simon","")]
(<*) :: f a -> f b -> f ainfixl 4Sequence actions, discarding the value of the second argument.
Instances139Applicative, …
Applicative ConcurrentlyDefined in async-2.2.5 · Control.Concurrent.Async.InternalApplicative ComplexDefined in base-4.20.2.0 · Data.ComplexApplicative FirstDefined in base-4.20.2.0 · Data.SemigroupApplicative LastDefined in base-4.20.2.0 · Data.SemigroupApplicative MaxDefined in base-4.20.2.0 · Data.SemigroupApplicative MinDefined in base-4.20.2.0 · Data.SemigroupApplicative GetDefined in binary-0.8.9.3 · Data.Binary.Get.InternalApplicative PutMDefined in binary-0.8.9.3 · Data.Binary.PutApplicative PutDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.InternalApplicative RGBDefined in colour-2.3.6 · Data.Colour.RGBApplicative SeqDefined in containers-0.7 · Data.Sequence.InternalApplicative TreeDefined in containers-0.7 · Data.TreeApplicative NonEmptyDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseApplicative STMDefined in ghc-internal-9.1003.0 · GHC.Internal.Conc.SyncApplicative IdentityDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.IdentityApplicative FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidApplicative LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidApplicative DownDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.OrdApplicative DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalApplicative ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalApplicative SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalApplicative ZipListDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListf <$> ZipList xs1 <*> ... <*> ZipList xsN = ZipList (zipWithN f xs1 ... xsN)where
zipWithNrefers to thezipWithfunction of the appropriate arity (zipWith,zipWith3,zipWith4, ...). For example:(\a b c -> stimes c [a, b]) <$> ZipList "abcd" <*> ZipList "567" <*> ZipList [1..] = ZipList (zipWith3 (\a b c -> stimes c [a, b]) "abcd" "567" [1..]) = ZipList {getZipList = ["a5","b6b6","c7c7c7"]}Applicative NoIODefined in ghc-internal-9.1003.0 · GHC.Internal.GHCiApplicative Par1Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsApplicative MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseApplicative PDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPApplicative ReadPDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPApplicative ReadPrecDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPrecApplicative SoloDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseApplicative IODefined in ghc-internal-9.1003.0 · GHC.Internal.BaseApplicative ManagedDefined in managed-1.0.10 · Control.Monad.ManagedApplicative OptionalDefined in optional-args-1.0.2 · Data.OptionalApplicative ChunkDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Help.ChunkApplicative ComplResultDefined in optparse-applicative-0.18.1.0 · Options.Applicative.InternalApplicative CompletionDefined in optparse-applicative-0.18.1.0 · Options.Applicative.InternalApplicative PDefined in optparse-applicative-0.18.1.0 · Options.Applicative.InternalApplicative ParserDefined in optparse-applicative-0.18.1.0 · Options.Applicative.TypesApplicative ParserMDefined in optparse-applicative-0.18.1.0 · Options.Applicative.TypesApplicative ParserResultDefined in optparse-applicative-0.18.1.0 · Options.Applicative.TypesApplicative ReadMDefined in optparse-applicative-0.18.1.0 · Options.Applicative.TypesApplicative ArrayDefined in primitive-0.9.1.0 · Data.Primitive.ArrayApplicative SmallArrayDefined in primitive-0.9.1.0 · Data.Primitive.SmallArrayApplicative STMDefined in stm-2.5.3.1 · Control.Sequential.STMApplicative PprMDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.PprLibApplicative QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.SyntaxApplicative PatternDefined in turtle-1.6.2 · Turtle.PatternApplicative ShellDefined in turtle-1.6.2 · Turtle.ShellApplicative VectorDefined in vector-0.13.2.0 · Data.VectorApplicative IdDefined in vector-0.13.2.0 · Data.Vector.Fusion.UtilApplicative VectorDefined in vector-0.13.2.0 · Data.Vector.StrictApplicative BoxDefined in vector-stream-0.1.0.1 · Data.Stream.MonadicApplicative StreamDefined in zlib-0.7.1.0 · Codec.Compression.Zlib.StreamApplicative []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseApplicative ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.ProxyApplicative U1Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsApplicative (ConcurrentlyE e)Defined in async-2.2.5 · Control.Concurrent.Async.InternalApplicative (SetM s)Defined in containers-0.7 · Data.GraphApplicative (State s)Defined in containers-0.7 · Utils.Containers.Internal.StateApplicative (Fold a)Defined in foldl-1.4.18 · Control.FoldlApplicative (Fold1 a)Defined in foldl-1.4.18 · Control.Foldl.NonEmptyApplicative (ReverseState s)Defined in foldl-1.4.18 · Control.ScanlApplicative (Scan a)Defined in foldl-1.4.18 · Control.ScanlApplicative (ST s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.ST.Lazy.ImpApplicative (Either e)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.EitherApplicative (StateL s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.UtilsApplicative (StateR s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.UtilsApplicative (ST s)Defined in ghc-internal-9.1003.0 · GHC.Internal.STApplicative (UniqueParser s)Defined in prettyprinter-1.7.1 · Prettyprinter.Render.Util.SimpleDocTreeApplicative (IParser t)Defined in text-2.1.3 · Data.Text.Internal.ReadApplicative f => Applicative (WrappedApplicative f)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.ClassApplicative f => Applicative (Lift f)Defined in transformers-0.6.1.1 · Control.Applicative.LiftA combination is Pure only if both parts are.
Monad m => Applicative (WrappedMonad m)Defined in base-4.20.2.0 · Control.ApplicativeMonad m => Applicative (CatchT m)Defined in exceptions-0.10.9 · Control.Monad.Catch.PureMonad m => Applicative (ListT m)Defined in optparse-applicative-0.18.1.0 · Options.Applicative.InternalMonad m => Applicative (NondetT m)Defined in optparse-applicative-0.18.1.0 · Options.Applicative.InternalMonoid a => Applicative (Tuple2 a)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseArrow a => Applicative (ArrowMonad a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.ArrowApply f => Applicative (MaybeApply f)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class(Functor m, Monad m) => Applicative (MaybeT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.MaybeBiapplicative p => Applicative (Fix p)Defined in bifunctors-5.6.2 · Data.Bifunctor.FixBiapplicative p => Applicative (Join p)Defined in bifunctors-5.6.2 · Data.Bifunctor.JoinApplicative (Mag a b)Defined in bifunctors-5.6.2 · Data.BiapplicativeApplicative (Bazaar a b)Defined in profunctors-5.6.3 · Data.Profunctor.TraversingApplicative (Tagged s)Defined in tagged-0.8.9 · Data.TaggedApplicative (bi a) => Applicative (Biap bi a)Defined in bifunctors-5.6.2 · Data.Bifunctor.BiapApplicative (t m) => Applicative (LiftingAccum t m)Defined in mtl-2.3.1 · Control.Monad.AccumApplicative (t m) => Applicative (LiftingSelect t m)Defined in mtl-2.3.1 · Control.Monad.SelectApplicative f => Applicative (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidApplicative f => Applicative (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalApplicative f => Applicative (Rec1 f)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsApplicative f => Applicative (Indexing f)Defined in indexed-traversable-0.1.4 · WithIndexApplicative f => Applicative (Static f a)Defined in semigroupoids-6.0.1 · Data.Semigroupoid.StaticApplicative f => Applicative (Backwards f)Defined in transformers-0.6.1.1 · Control.Applicative.BackwardsApply
f-actions in the reverse order.Applicative f => Applicative (Reverse f)Defined in transformers-0.6.1.1 · Data.Functor.ReverseDerived instance.
Applicative m => Applicative (FoldM m a)Defined in foldl-1.4.18 · Control.FoldlApplicative m => Applicative (ScanM m a)Defined in foldl-1.4.18 · Control.ScanlApplicative m => Applicative (Kleisli m a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.ArrowApplicative m => Applicative (IdentityT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.IdentityApplicative m => Applicative (ReaderT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.ReaderApplicative w => Applicative (TracedT m w)Defined in comonad-5.0.9 · Control.Comonad.Trans.TracedMonad m => Applicative (StateT s m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.UtilsMonoid a => Applicative (Constant a)Defined in transformers-0.6.1.1 · Data.Functor.ConstantMonoid m => Applicative (Const m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstMonoid output => Applicative (StackMachine output style)Defined in prettyprinter-1.7.1 · Prettyprinter.Render.Util.StackMachineArrow a => Applicative (WrappedArrow a b)Defined in base-4.20.2.0 · Control.Applicative(Applicative (Rep p), Representable p) => Applicative (Prep p)Defined in profunctors-5.6.3 · Data.Profunctor.Rep(Applicative f, Monad f) => Applicative (WhenMissing f x)Defined in containers-0.7 · Data.IntMap.InternalEquivalent to
ReaderT k (ReaderT x (MaybeT f)).(Applicative w, Monoid s) => Applicative (StoreT s w)Defined in comonad-5.0.9 · Control.Comonad.Trans.Store(Functor m, Monad m) => Applicative (ExceptT e m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Except(Functor m, Monad m) => Applicative (SelectT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Select(Functor m, Monad m) => Applicative (StateT s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.State.Lazy(Functor m, Monad m) => Applicative (StateT s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.State.Strict(Functor m, Monad m) => Applicative (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.CPS(Monoid a, Monoid b) => Applicative (Tuple3 a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base(Monoid e, Applicative m) => Applicative (EnvT e m)Defined in comonad-5.0.9 · Control.Comonad.Trans.Env(Monoid w, Applicative m) => Applicative (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.Lazy(Monoid w, Applicative m) => Applicative (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.Strict(Monoid w, Functor m, Monad m) => Applicative (AccumT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Accum(Generic1 f, Applicative (Rep1 f)) => Applicative (Generically1 f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Profunctor p, Arrow p) => Applicative (Closure p a)Defined in profunctors-5.6.3 · Data.Profunctor.Closed(Profunctor p, Arrow p) => Applicative (Tambara p a)Defined in profunctors-5.6.3 · Data.Profunctor.StrongApplicative (Cokleisli w a)Defined in comonad-5.0.9 · Control.ComonadApplicative (Costar f a)Defined in profunctors-5.6.3 · Data.Profunctor.TypesApplicative (ContT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.ContApplicative ((->) r)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseApplicative f => Applicative (Star f a)Defined in profunctors-5.6.3 · Data.Profunctor.TypesMonoid c => Applicative (K1 i c)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Applicative f, Applicative g) => Applicative (Product f g)Defined in base-4.20.2.0 · Data.Functor.Product(Applicative f, Applicative g) => Applicative (f :*: g)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Applicative f, Monad f) => Applicative (WhenMissing f k x)Defined in containers-0.7 · Data.Map.InternalEquivalent to
ReaderT k (ReaderT x (MaybeT f)).(Monad f, Applicative f) => Applicative (WhenMatched f x y)Defined in containers-0.7 · Data.IntMap.InternalEquivalent to
ReaderT Key (ReaderT x (ReaderT y (MaybeT f)))(Monoid a, Monoid b, Monoid c) => Applicative (Tuple4 a b c)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseApplicative f => Applicative (M1 i c f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Applicative f, Applicative g) => Applicative (Compose f g)Defined in base-4.20.2.0 · Data.Functor.Compose(Applicative f, Applicative g) => Applicative (f :.: g)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Functor m, Monad m) => Applicative (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.CPS(Monad f, Applicative f) => Applicative (WhenMatched f k x y)Defined in containers-0.7 · Data.Map.InternalEquivalent to
ReaderT k (ReaderT x (ReaderT y (MaybeT f)))(Monoid w, Functor m, Monad m) => Applicative (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.Lazy(Monoid w, Functor m, Monad m) => Applicative (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.Strict