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.
Instances154Applicative, …
Applicative 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 SeqDefined in containers-0.7 · Data.Sequence.InternalApplicative TreeDefined in containers-0.7 · Data.TreeApplicative AssemblerDefined in ghc-9.10.3 · GHC.ByteCode.AsmApplicative CmmOptMDefined in ghc-9.10.3 · GHC.Cmm.GenericOptApplicative CmmLintDefined in ghc-9.10.3 · GHC.Cmm.LintApplicative PDDefined in ghc-9.10.3 · GHC.Cmm.Parser.MonadApplicative NatMDefined in ghc-9.10.3 · GHC.CmmToAsm.MonadApplicative WasmAsmMDefined in ghc-9.10.3 · GHC.CmmToAsm.Wasm.AsmApplicative TEDefined in ghc-9.10.3 · GHC.CmmToCApplicative LlvmMDefined in ghc-9.10.3 · GHC.CmmToLlvm.BaseApplicative NormMDefined in ghc-9.10.3 · GHC.Core.FamInstEnvApplicative LintMDefined in ghc-9.10.3 · GHC.Core.LintApplicative RuleMDefined in ghc-9.10.3 · GHC.Core.Opt.ConstantFoldApplicative CoreMDefined in ghc-9.10.3 · GHC.Core.Opt.MonadApplicative SimplMDefined in ghc-9.10.3 · GHC.Core.Opt.Simplify.MonadApplicative UMDefined in ghc-9.10.3 · GHC.Core.UnifyApplicative UnifyResultMDefined in ghc-9.10.3 · GHC.Core.UnifyApplicative CtsMDefined in ghc-9.10.3 · GHC.CoreToStgApplicative NullCollapseVizDefined in ghc-9.10.3 · GHC.Data.Graph.CollapseApplicative InfiniteDefined in ghc-9.10.3 · GHC.Data.List.InfiniteApplicative PairDefined in ghc-9.10.3 · GHC.Data.PairApplicative MaybeDefined in ghc-9.10.3 · GHC.Data.StrictApplicative HscDefined in ghc-9.10.3 · GHC.Driver.Env.TypesApplicative GhcDefined in ghc-9.10.3 · GHC.Driver.MonadApplicative HookedUseDefined in ghc-9.10.3 · GHC.Driver.Pipeline.ExecuteApplicative MatchResultDefined in ghc-9.10.3 · GHC.HsToCore.MonadProduct is an "or" on fallibility---the combined match result is infallible only if the left and right argument match results both were.
This is useful for combining a bunch of alternatives together and then getting the overall fallibility of the entire group. See
mkDataConCasefor an example.Applicative TMDefined in ghc-9.10.3 · GHC.HsToCore.TicksApplicative DFFVDefined in ghc-9.10.3 · GHC.Iface.TidyApplicative PDefined in ghc-9.10.3 · GHC.Parser.LexerApplicative PVDefined in ghc-9.10.3 · GHC.Parser.PostProcessApplicative HdkADefined in ghc-9.10.3 · GHC.Parser.PostProcess.HaddockApplicative HdkMDefined in ghc-9.10.3 · GHC.Parser.PostProcess.HaddockApplicative CpsRnDefined in ghc-9.10.3 · GHC.Rename.PatApplicative TPRnMDefined in ghc-9.10.3 · GHC.Rename.PatApplicative RMDefined in ghc-9.10.3 · GHC.Stg.InferTags.RewriteApplicative LiftMDefined in ghc-9.10.3 · GHC.Stg.Lift.MonadApplicative LintMDefined in ghc-9.10.3 · GHC.Stg.LintApplicative StgMDefined in ghc-9.10.3 · GHC.Stg.PipelineApplicative BcMDefined in ghc-9.10.3 · GHC.StgToByteCodeApplicative CmmParseDefined in ghc-9.10.3 · GHC.StgToCmm.ExtCodeApplicative FCodeDefined in ghc-9.10.3 · GHC.StgToCmm.MonadApplicative KindRepMDefined in ghc-9.10.3 · GHC.Tc.Instance.TypeableApplicative SolverStageDefined in ghc-9.10.3 · GHC.Tc.Solver.MonadApplicative TcSDefined in ghc-9.10.3 · GHC.Tc.Solver.MonadApplicative RewriteMDefined in ghc-9.10.3 · GHC.Tc.Solver.RewriteApplicative RoleMDefined in ghc-9.10.3 · GHC.Tc.TyCl.UtilsApplicative SynCycleMDefined in ghc-9.10.3 · GHC.Tc.TyCl.UtilsApplicative TcPluginMDefined in ghc-9.10.3 · GHC.Tc.TypesApplicative ZonkMDefined in ghc-9.10.3 · GHC.Tc.Zonk.MonadApplicative UniqSMDefined in ghc-9.10.3 · GHC.Types.Unique.SupplyApplicative 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 GHCiQDefined in ghci-9.10.3 · GHCi.THApplicative 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 []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 (SetM s)Defined in containers-0.7 · Data.GraphApplicative (State s)Defined in containers-0.7 · Utils.Containers.Internal.StateApplicative (RegM freeRegs)Defined in ghc-9.10.3 · GHC.CmmToAsm.Reg.Linear.StateApplicative (WasmCodeGenM w)Defined in ghc-9.10.3 · GHC.CmmToAsm.Wasm.TypesApplicative (IOEnv m)Defined in ghc-9.10.3 · GHC.Data.IOEnvApplicative (MaybeErr err)Defined in ghc-9.10.3 · GHC.Data.MaybeApplicative (CmdLineP s)Defined in ghc-9.10.3 · GHC.Driver.SessionApplicative (PuResult a)Defined in ghc-9.10.3 · GHC.Tc.Utils.UnifyApplicative (ZonkBndrT m)Defined in ghc-9.10.3 · GHC.Tc.Zonk.EnvApplicative (CvtM' err)Defined in ghc-9.10.3 · GHC.ThToHsApplicative (Codensity f)Defined in ghc-9.10.3 · GHC.Utils.Monad.CodensityApplicative (State s)Defined in ghc-9.10.3 · GHC.Utils.Monad.State.StrictApplicative (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 f => Applicative (Lift f)Defined in transformers-0.6.1.1 · Control.Applicative.LiftA combination is Pure only if both parts are.
Applicative m => Applicative (GhcT m)Defined in ghc-9.10.3 · GHC.Driver.MonadApplicative m => Applicative (ZonkT m)Defined in ghc-9.10.3 · GHC.Tc.Zonk.EnvMonad 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 (EwM m)Defined in ghc-9.10.3 · GHC.Driver.CmdLineMonoid 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.Arrow(Functor m, Monad m) => Applicative (MaybeT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.MaybeApplicative (S z s)Defined in ghc-9.10.3 · GHC.CmmToAsm.CFG.DominatorsApplicative (Stream m a)Defined in ghc-9.10.3 · 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.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 (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 (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.ReaderMonad m => Applicative (StreamS m a)Defined in ghc-9.10.3 · GHC.Data.StreamMonad 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.ConstArrow a => Applicative (WrappedArrow a b)Defined in base-4.20.2.0 · Control.Applicative(Applicative f, Monad f) => Applicative (WhenMissing f x)Defined in containers-0.7 · Data.IntMap.InternalEquivalent to
ReaderT k (ReaderT x (MaybeT f)).(Applicative f, Monad f) => Applicative (WhenMissing f x)Defined in ghc-9.10.3 · 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.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 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.GenericsApplicative (ContT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.ContApplicative ((->) r)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid 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)))(Monad f, Applicative f) => Applicative (WhenMatched f x y)Defined in ghc-9.10.3 · 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.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