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 Prelude.<$> 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.
Instances122Applicative, …
Applicative ComplexDefined in base-4.22.0.0 · Data.ComplexApplicative FirstDefined in base-4.22.0.0 · Data.SemigroupApplicative LastDefined in base-4.22.0.0 · Data.SemigroupApplicative MaxDefined in base-4.22.0.0 · Data.SemigroupApplicative MinDefined in base-4.22.0.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 SeqDefined in containers-0.8 · Data.Sequence.InternalApplicative TreeDefined in containers-0.8 · Data.TreeApplicative PprMDefined in ghc-boot-th-9.14.1 · GHC.Boot.TH.PprLibApplicative NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanApplicative STMDefined in ghc-internal-9.1401.0 · GHC.Internal.Conc.SyncApplicative IdentityDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.IdentityApplicative FirstDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidApplicative LastDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidApplicative DownDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.OrdApplicative DualDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalApplicative ProductDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalApplicative SumDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalApplicative ZipListDefined in ghc-internal-9.1401.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.1401.0 · GHC.Internal.GHCiApplicative Par1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsApplicative MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseApplicative QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxApplicative PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPApplicative ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPApplicative ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecApplicative SoloDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseApplicative IODefined in ghc-internal-9.1401.0 · GHC.Internal.BaseApplicative PprMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Boot.TH.PprLibApplicative NormMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Core.FamInstEnvApplicative LintMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Core.LintApplicative RuleMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Core.Opt.ConstantFoldApplicative CoreMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Core.Opt.MonadApplicative SimplMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Core.Opt.Simplify.MonadApplicative UMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Core.UnifyApplicative UnifyResultMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Core.UnifyApplicative InfiniteDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.List.InfiniteApplicative PairDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.PairApplicative MaybeDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.StrictApplicative HscDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Driver.Env.TypesApplicative GhcDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Driver.MonadApplicative TMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.HsToCore.TicksApplicative QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.SyntaxApplicative PDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Parser.LexerApplicative PVDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Parser.PostProcessApplicative HdkADefined in ghc-lib-parser-9.14.1.20251220 · GHC.Parser.PostProcess.HaddockApplicative HdkMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Parser.PostProcess.HaddockApplicative TcPluginMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Tc.TypesApplicative ZonkMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Tc.Zonk.MonadApplicative UniqDSMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Types.Unique.DSMApplicative UniqSMDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Types.Unique.SupplyApplicative STMDefined in stm-2.5.3.1 · Control.Sequential.STMApplicative []Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseApplicative ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.ProxyApplicative U1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsApplicative (State s)Defined in containers-0.8 · Utils.Containers.Internal.StateApplicative (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.ST.Lazy.ImpApplicative (Either e)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.EitherApplicative (StateL s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsApplicative (StateR s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsApplicative (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.STApplicative (IOEnv m)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.IOEnvApplicative (MaybeErr err)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.MaybeApplicative (CmdLineP s)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Driver.SessionApplicative (State s)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Utils.Monad.State.StrictApplicative (IParser t)Defined in text-2.1.3 · Data.Text.Internal.ReadApplicative f => Applicative (Lift f)Defined in transformers-0.6.1.2 · Control.Applicative.LiftA combination is Pure only if both parts are.
Applicative m => Applicative (GhcT m)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Driver.MonadMonad m => Applicative (WrappedMonad m)Defined in base-4.22.0.0 · Control.ApplicativeMonad m => Applicative (CatchT m)Defined in exceptions-0.10.11 · Control.Monad.Catch.PureMonad m => Applicative (EwM m)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Driver.CmdLineMonad m => Applicative (UniqDSMT m)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Types.Unique.DSMMonoid a => Applicative (Tuple2 a)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseArrow a => Applicative (ArrowMonad a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Arrow(Functor m, Monad m) => Applicative (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeApplicative (Stream m a)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.StreamApplicative (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.1401.0 · GHC.Internal.Data.MonoidApplicative f => Applicative (Alt f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalApplicative f => Applicative (Rec1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsApplicative f => Applicative (Backwards f)Defined in transformers-0.6.1.2 · Control.Applicative.BackwardsApply
f-actions in the reverse order.Applicative f => Applicative (Reverse f)Defined in transformers-0.6.1.2 · Data.Functor.ReverseDerived instance.
Applicative m => Applicative (Kleisli m a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowApplicative m => Applicative (IdentityT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.IdentityApplicative m => Applicative (ReaderT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ReaderMonad m => Applicative (StateT s m)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsMonad m => Applicative (StreamS m a)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.StreamMonoid a => Applicative (Constant a)Defined in transformers-0.6.1.2 · Data.Functor.ConstantMonoid m => Applicative (Const m)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.ConstArrow a => Applicative (WrappedArrow a b)Defined in base-4.22.0.0 · Control.Applicative(Applicative f, Monad f) => Applicative (WhenMissing f x)Defined in containers-0.8 · Data.IntMap.InternalEquivalent to
ReaderT k (ReaderT x (MaybeT f)).(Applicative f, Monad f) => Applicative (WhenMissing f x)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.Word64Map.InternalEquivalent to
ReaderT k (ReaderT x (MaybeT f)).(Functor m, Monad m) => Applicative (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Functor m, Monad m) => Applicative (SelectT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Select(Functor m, Monad m) => Applicative (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.Lazy(Functor m, Monad m) => Applicative (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.Strict(Functor m, Monad m) => Applicative (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.CPS(Monoid a, Monoid b) => Applicative (Tuple3 a b)Defined in ghc-internal-9.1401.0 · GHC.Internal.Base(Monoid w, Applicative m) => Applicative (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.Lazy(Monoid w, Applicative m) => Applicative (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.Strict(Monoid w, Functor m, Monad m) => Applicative (AccumT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Accum(Generic1 f, Applicative (Rep1 f)) => Applicative (Generically1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsApplicative (ParsecT s u m)Defined in parsec-3.1.18.0 · Text.Parsec.PrimApplicative (ContT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ContApplicative ((->) r)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonoid c => Applicative (K1 i c)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Applicative f, Applicative g) => Applicative (Product f g)Defined in base-4.22.0.0 · Data.Functor.Product(Applicative f, Applicative g) => Applicative (f :*: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Applicative f, Monad f) => Applicative (WhenMissing f k x)Defined in containers-0.8 · Data.Map.InternalEquivalent to
ReaderT k (ReaderT x (MaybeT f)).(Monad f, Applicative f) => Applicative (WhenMatched f x y)Defined in containers-0.8 · Data.IntMap.InternalEquivalent to
ReaderT Key (ReaderT x (ReaderT y (MaybeT f)))(Monad f, Applicative f) => Applicative (WhenMatched f x y)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Data.Word64Map.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.1401.0 · GHC.Internal.BaseApplicative f => Applicative (M1 i c f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Applicative f, Applicative g) => Applicative (Compose f g)Defined in base-4.22.0.0 · Data.Functor.Compose(Applicative f, Applicative g) => Applicative (f :.: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Functor m, Monad m) => Applicative (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.CPS(Monad f, Applicative f) => Applicative (WhenMatched f k x y)Defined in containers-0.8 · 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.2 · Control.Monad.Trans.RWS.Lazy(Monoid w, Functor m, Monad m) => Applicative (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Strict