Moduletesting-feat-1.1.1.1Haskell2010
Test.Feat.Enumerate
Basic combinators for building enumerations most users will want to use the type class based combinators in Test.Feat.Class instead.
- 18 types
- 3 classes
- 13 values
- Packagetesting-feat-1.1.1.1
- Exports36
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceEnumerate.hs
A functional enumeration of type t is a partition of
t into finite numbered sets. Each part contains values
of a certain cost (typically the size of the value).
Instances6Functor, Applicative, Alternative, Sized, Semigroup, Monoid
Functor EnumerateDefined in testing-feat-1.1.1.1 · Test.Feat.EnumerateOnly use fmap with bijective functions (e.g. data constructors)
Applicative EnumerateDefined in testing-feat-1.1.1.1 · Test.Feat.EnumerateAlternative EnumerateDefined in testing-feat-1.1.1.1 · Test.Feat.EnumerateSized EnumerateDefined in testing-feat-1.1.1.1 · Test.Feat.EnumerateSemigroup (Enumerate a)Defined in testing-feat-1.1.1.1 · Test.Feat.EnumerateMonoid (Enumerate a)Defined in testing-feat-1.1.1.1 · Test.Feat.Enumerate
Reversed lists
2 declarationsA data structure that contains a list and the reversals of all initial segments of the list. Intuitively
reversals xs !! n = reverse (take (n+1) (fromRev xs))Any operation on a RevList typically discards the reversals and constructs
new reversals on demand.
Instances4Functor, Show, Semigroup, Monoid
Functor RevListDefined in testing-feat-1.1.1.1 · Test.Feat.EnumerateShow a => Show (RevList a)Defined in testing-feat-1.1.1.1 · Test.Feat.EnumerateSemigroup a => Semigroup (RevList a)Defined in testing-feat-1.1.1.1 · Test.Feat.EnumerateSemigroup a => Monoid (RevList a)Defined in testing-feat-1.1.1.1 · Test.Feat.EnumeratePadded zip
Constructs a "Reverse list" variant of a given list. In a sensible
Haskell implementation evaluating any inital segment of
reversals (toRev xs) uses linear memory in the size of the segment.
Finite ordered sets
2 declarationsInstances6Functor, Applicative, Alternative, Show, Semigroup, Monoid
Functor FiniteDefined in testing-feat-1.1.1.1 · Test.Feat.FiniteApplicative FiniteDefined in testing-feat-1.1.1.1 · Test.Feat.FiniteAlternative FiniteDefined in testing-feat-1.1.1.1 · Test.Feat.FiniteShow a => Show (Finite a)Defined in testing-feat-1.1.1.1 · Test.Feat.FiniteSemigroup (Finite a)Defined in testing-feat-1.1.1.1 · Test.Feat.FiniteMonoid (Finite a)Defined in testing-feat-1.1.1.1 · Test.Feat.Finite
Combinators for building enumerations
28 declarationsThe class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following:
- Right identity
- Left identity
- Associativity
(
law)
- Concatenation
You can alternatively define mconcat instead of mempty, in which case the laws are:
- Unit
- Multiplication
- Subclass
The method names refer to the monoid of lists under concatenation, but there are many other instances.
Some types can be viewed as a monoid in more than one way,
e.g. both addition and multiplication on numbers.
In such cases we often define newtypes and make those instances
of Monoid, e.g. Data.Semigroup.Sum and Data.Semigroup.Product.
NOTE: Semigroup is a superclass of Monoid since base-4.11.0.0.
Methods
mempty :: aIdentity of mappend
Examples
Example1 expression "Hello world" <> mempty"Hello world"
Example1 expression mempty <> [1, 2, 3][1,2,3]
mappend :: a -> a -> aAn associative operation
NOTE: This method is redundant and has the default implementation
mappend = (<>)since base-4.11.0.0. Should it be implemented manually, since mappend is a synonym for (<>), it is expected that the two functions are defined the same way. In a future GHC release mappend will be removed from Monoid.mconcat :: [a] -> aFold a list using the monoid.
For most types, the default definition for mconcat will be used, but the function is included in the class definition so that an optimized version can be provided for specific types.
Example1 expression mconcat ["Hello", " ", "Haskell", "!"]"Hello Haskell!"
Instances73Monoid, …
Monoid ByteArrayDefined in base-4.20.2.0 · Data.Array.ByteMonoid BuilderDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.InternalMonoid ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.TypeMonoid ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Lazy.InternalMonoid ShortByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Short.InternalMonoid IntSetDefined in containers-0.7 · Data.IntSet.InternalMonoid AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Internal.TypesMonoid EventLifetimeDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Internal.TypesMonoid LifetimeDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Internal.Typesmappendtakes the longer of two lifetimes.Monoid ExceptionContextDefined in ghc-internal-9.1003.0 · GHC.Internal.Exception.ContextMonoid OrderingDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid DocDefined in pretty-1.1.3.6 · Text.PrettyPrint.HughesPJMonoid ()Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid (Comparison a)Defined in base-4.20.2.0 · Data.Functor.ContravariantMonoid (Equivalence a)Defined in base-4.20.2.0 · Data.Functor.ContravariantMonoid (Predicate a)Defined in base-4.20.2.0 · Data.Functor.ContravariantMonoid (IntMap a)Defined in containers-0.7 · Data.IntMap.InternalMonoid (Seq a)Defined in containers-0.7 · Data.Sequence.InternalMonoid (MergeSet a)Defined in containers-0.7 · Data.Set.InternalMonoid (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonoid (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonoid (Endo a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid (Doc a)Defined in pretty-1.1.3.6 · Text.PrettyPrint.Annotated.HughesPJMonoid (Count a)Defined in size-based-0.1.3.2 · Control.Enumerable.CountMonoid (Enumerate a)Defined in testing-feat-1.1.1.1 · Test.Feat.EnumerateMonoid (Finite a)Defined in testing-feat-1.1.1.1 · Test.Feat.FiniteMonoid [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid a => Monoid (STM a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Conc.SyncMonoid a => Monoid (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.IdentityMonoid a => Monoid (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.OrdMonoid a => Monoid (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid a => Monoid (IO a)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid a => Monoid (Q a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.SyntaxMonoid a => Monoid (a)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid m => Monoid (WrappedMonoid m)Defined in base-4.20.2.0 · Data.SemigroupMonoid p => Monoid (Par1 p)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsSemigroup a => Monoid (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseLift a semigroup into Maybe forming a Monoid according to http://en.wikipedia.org/wiki/Monoid: "Any semigroup
Smay be turned into a monoid simply by adjoining an elementenot inSand defininge*e = eande*s = s = s*efor alls ∈ S."Since 4.11.0: constraint on inner
avalue generalised from Monoid to Semigroup.Semigroup a => Monoid (RevList a)Defined in testing-feat-1.1.1.1 · Test.Feat.EnumeratePadded zip
Bits a => Monoid (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.BitsBits a => Monoid (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.BitsFiniteBits a => Monoid (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.BitsThis constraint is arguably too strong. However, as some types (such as
Natural) have undefined complement, this is the only safe choice.FiniteBits a => Monoid (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.BitsThis constraint is arguably too strong. However, as some types (such as
Natural) have undefined complement, this is the only safe choice.Num a => Monoid (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNum a => Monoid (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalOrd a => Monoid (Set a)Defined in containers-0.7 · Data.Set.InternalOrd a => Monoid (Max a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.UtilsOrd a => Monoid (Min a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Utils(Generic a, Monoid (Rep a ())) => Monoid (Generically a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Ord a, Bounded a) => Monoid (Max a)Defined in base-4.20.2.0 · Data.Semigroup(Ord a, Bounded a) => Monoid (Min a)Defined in base-4.20.2.0 · Data.SemigroupMonoid (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.ProxyMonoid (U1 p)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsMonoid a => Monoid (Op a b)Defined in base-4.20.2.0 · Data.Functor.Contravariantmempty @(Op a b)without newtypes ismempty @(b->a)=_ -> mempty.mempty :: Op a b mempty = Op _ -> memptyMonoid a => Monoid (ST s a)Defined in ghc-internal-9.1003.0 · GHC.Internal.STMonoid b => Monoid (a -> b)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseOrd k => Monoid (Map k v)Defined in containers-0.7 · Data.Map.Internal(Monoid a, Monoid b) => Monoid (a, b)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseAlternative f => Monoid (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid (f p) => Monoid (Rec1 f p)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsMonoid a => Monoid (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstMonoid a => Monoid (Constant a b)Defined in transformers-0.6.1.1 · Data.Functor.Constant(Applicative f, Monoid a) => Monoid (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid c => Monoid (K1 i c p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Monoid (f a), Monoid (g a)) => Monoid (Product f g a)Defined in base-4.20.2.0 · Data.Functor.Product(Monoid (f p), Monoid (g p)) => Monoid ((:*:) f g p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid (f (g a)) => Monoid (Compose f g a)Defined in base-4.20.2.0 · Data.Functor.ComposeMonoid (f (g p)) => Monoid ((:.:) f g p)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsMonoid (f p) => Monoid (M1 i c f p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
Boolean monoid under disjunction (||).
Any x <> Any y = Any (x || y)Examples
Any True <> mempty <> Any FalseAny {getAny = True}
mconcat (map (\x -> Any (even x)) [2,4,6,7,8])Any {getAny = True}
Any False <> memptyAny {getAny = False}
Instances14Bounded, Eq, Data, Ord, Read, Show, …
Bounded AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalEq AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalData AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataOrd AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalRead AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalShow AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalGeneric AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalSemigroup AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNFData AnyDefined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary AnyDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary AnyDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction AnyDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep Any = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Any"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Any"
'PrefixI 'True) (S1 ('MetaSel ('Just"getAny"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool)))
This data type witnesses the lifting of a Monoid into an Applicative pointwise.
Examples
Ap (Just [1, 2, 3]) <> Ap NothingAp {getAp = Nothing}
Ap [Sum 10, Sum 20] <> Ap [Sum 1, Sum 2]Ap {getAp = [Sum {getSum = 11},Sum {getSum = 12},Sum {getSum = 21},Sum {getSum = 22}]}
Instances24Generic1, Monad, Functor, MonadFix, MonadFail, Applicative, …
Generic1 (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonad f => Monad (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFunctor f => Functor (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonadFix f => MonadFix (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixMonadFail f => MonadFail (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidApplicative f => Applicative (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFoldable f => Foldable (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable f => Traversable (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableAlternative f => Alternative (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonadPlus f => MonadPlus (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFoldable1 f => Foldable1 (Ap f)Defined in base-4.20.2.0 · Data.Foldable1(Applicative f, Bounded a) => Bounded (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidEnum (f a) => Enum (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidEq (f a) => Eq (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid(Data (f a), Data a, Typeable f) => Data (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Applicative f, Num a) => Num (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidNote that even if the underlying Num and Applicative instances are lawful, for most Applicatives, this instance will not be lawful. If you use this instance with the list Applicative, the following customary laws will not hold:
Commutativity:
Example2 expressions Ap [10,20] + Ap [1,2]Ap {getAp = [11,12,21,22]}Ap [1,2] + Ap [10,20]Ap {getAp = [11,21,12,22]}
Additive inverse:
Example2 expressions Ap [] + negate (Ap [])Ap {getAp = []}fromInteger 0 :: Ap [] IntAp {getAp = [0]}
Distributivity:
Example2 expressions Ap [1,2] * (3 + 4)Ap {getAp = [7,14]}(Ap [1,2] * 3) + (Ap [1,2] * 4)Ap {getAp = [7,11,10,14]}
Ord (f a) => Ord (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidRead (f a) => Read (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidShow (f a) => Show (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidGeneric (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid(Applicative f, Semigroup a) => Semigroup (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid(Applicative f, Monoid a) => Monoid (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoidtype Rep (Ap f a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid"Ap"
"GHC.Internal.Data.Monoid"
"ghc-internal"
'True) (C1 ('MetaCons"Ap"
'PrefixI 'True) (S1 ('MetaSel ('Just"getAp"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f a))))type Rep1 (Ap f) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid"Ap"
"GHC.Internal.Data.Monoid"
"ghc-internal"
'True) (C1 ('MetaCons"Ap"
'PrefixI 'True) (S1 ('MetaSel ('Just"getAp"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 f)))
Maybe monoid returning the leftmost non-Nothing value.
First a is isomorphic to Alt Maybe a, but precedes it
historically.
Beware that Data.Monoid.First is different from
Data.Semigroup.First. The former returns the first non-Nothing,
so Data.Monoid.First Nothing <> x = x. The latter simply returns the first value,
thus Data.Semigroup.First Nothing <> x = Data.Semigroup.First Nothing.
Examples
First (Just "hello") <> First Nothing <> First (Just "world")First {getFirst = Just "hello"}
First Nothing <> memptyFirst {getFirst = Nothing}
Instances23Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
Monad FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFunctor FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonadFix FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixApplicative FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFoldable FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableMonadZip FirstDefined in base-4.20.2.0 · Control.Monad.ZipNFData1 FirstDefined in deepseq-1.5.0.0 · Control.DeepSeqGeneric1 FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidEq a => Eq (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidData a => Data (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataOrd a => Ord (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidRead a => Read (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidShow a => Show (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidGeneric (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidSemigroup (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonoid (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidNFData a => NFData (First a)Defined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary a => Arbitrary (First a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary a => CoArbitrary (First a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction a => Function (First a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep (First a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid"First"
"GHC.Internal.Data.Monoid"
"ghc-internal"
'True) (C1 ('MetaCons"First"
'PrefixI 'True) (S1 ('MetaSel ('Just"getFirst"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe a))))type Rep1 First = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid"First"
"GHC.Internal.Data.Monoid"
"ghc-internal"
'True) (C1 ('MetaCons"First"
'PrefixI 'True) (S1 ('MetaSel ('Just"getFirst"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 Maybe)))
Maybe monoid returning the rightmost non-Nothing value.
Last a is isomorphic to Dual (First a), and thus to
Dual (Alt Maybe a)
Data.Semigroup.Last. The former returns the last non-Nothing,
so x <> Data.Monoid.Last Nothing = x. The latter simply returns the last value,
thus x <> Data.Semigroup.Last Nothing = Data.Semigroup.Last Nothing.
Examples
Last (Just "hello") <> Last Nothing <> Last (Just "world")Last {getLast = Just "world"}
Last Nothing <> memptyLast {getLast = Nothing}
Instances23Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
Monad LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFunctor LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonadFix LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixApplicative LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFoldable LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableMonadZip LastDefined in base-4.20.2.0 · Control.Monad.ZipNFData1 LastDefined in deepseq-1.5.0.0 · Control.DeepSeqGeneric1 LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidEq a => Eq (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidData a => Data (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataOrd a => Ord (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidRead a => Read (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidShow a => Show (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidGeneric (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidSemigroup (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonoid (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidNFData a => NFData (Last a)Defined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary a => Arbitrary (Last a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary a => CoArbitrary (Last a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction a => Function (Last a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep (Last a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid"Last"
"GHC.Internal.Data.Monoid"
"ghc-internal"
'True) (C1 ('MetaCons"Last"
'PrefixI 'True) (S1 ('MetaSel ('Just"getLast"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe a))))type Rep1 Last = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid"Last"
"GHC.Internal.Data.Monoid"
"ghc-internal"
'True) (C1 ('MetaCons"Last"
'PrefixI 'True) (S1 ('MetaSel ('Just"getLast"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 Maybe)))
Boolean monoid under conjunction (&&).
All x <> All y = All (x && y)Examples
All True <> mempty <> All False)All {getAll = False}
mconcat (map (\x -> All (even x)) [2,4,6,7,8])All {getAll = False}
All True <> memptyAll {getAll = True}
Instances14Bounded, Eq, Data, Ord, Read, Show, …
Bounded AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalEq AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalData AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataOrd AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalRead AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalShow AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalGeneric AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalSemigroup AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNFData AllDefined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary AllDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary AllDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction AllDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep All = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"All"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"All"
'PrefixI 'True) (S1 ('MetaSel ('Just"getAll"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool)))
Monoid under <|>.
Alt l <> Alt r == Alt (l <|> r)Examples
Alt (Just 12) <> Alt (Just 24)Alt {getAlt = Just 12}
Alt Nothing <> Alt (Just 24)Alt {getAlt = Just 24}
Instances27Generic1, Monad, Functor, MonadFix, Applicative, Foldable, …
Generic1 (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonad f => Monad (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFunctor f => Functor (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonadFix f => MonadFix (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixApplicative f => Applicative (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFoldable f => Foldable (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable f => Traversable (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableAlternative f => Alternative (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonadPlus f => MonadPlus (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonadZip f => MonadZip (Alt f)Defined in base-4.20.2.0 · Control.Monad.ZipFoldable1 f => Foldable1 (Alt f)Defined in base-4.20.2.0 · Data.Foldable1Contravariant f => Contravariant (Alt f)Defined in base-4.20.2.0 · Data.Functor.ContravariantEnum (f a) => Enum (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalEq (f a) => Eq (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal(Data (f a), Data a, Typeable f) => Data (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataNum (f a) => Num (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalOrd (f a) => Ord (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalRead (f a) => Read (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalShow (f a) => Show (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalGeneric (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalAlternative f => Semigroup (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalAlternative f => Monoid (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalArbitrary (f a) => Arbitrary (Alt f a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary (f a) => CoArbitrary (Alt f a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction (f a) => Function (Alt f a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep (Alt f a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Alt"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Alt"
'PrefixI 'True) (S1 ('MetaSel ('Just"getAlt"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f a))))type Rep1 (Alt f) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Alt"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Alt"
'PrefixI 'True) (S1 ('MetaSel ('Just"getAlt"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 f)))
The dual of a Monoid, obtained by swapping the arguments of (<>).
Dual a <> Dual b == Dual (b <> a)Examples
Dual "Hello" <> Dual "World"Dual {getDual = "WorldHello"}
Dual (Dual "Hello") <> Dual (Dual "World")Dual {getDual = Dual {getDual = "HelloWorld"}}
Instances25Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
Monad DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFunctor DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonadFix DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixApplicative DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFoldable DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableMonadZip DualDefined in base-4.20.2.0 · Control.Monad.ZipFoldable1 DualDefined in base-4.20.2.0 · Data.Foldable1NFData1 DualDefined in deepseq-1.5.0.0 · Control.DeepSeqGeneric1 DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalBounded a => Bounded (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalEq a => Eq (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalData a => Data (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataOrd a => Ord (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalRead a => Read (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalShow a => Show (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalGeneric (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalSemigroup a => Semigroup (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid a => Monoid (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNFData a => NFData (Dual a)Defined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary a => Arbitrary (Dual a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary a => CoArbitrary (Dual a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction a => Function (Dual a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep (Dual a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Dual"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Dual"
'PrefixI 'True) (S1 ('MetaSel ('Just"getDual"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))type Rep1 Dual = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Dual"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Dual"
'PrefixI 'True) (S1 ('MetaSel ('Just"getDual"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))
The monoid of endomorphisms under composition.
Endo f <> Endo g == Endo (f . g)Examples
let computation = Endo ("Hello, " ++) <> Endo (++ "!")appEndo computation "Haskell""Hello, Haskell!"
let computation = Endo (*3) <> Endo (+1)appEndo computation 16
Instances6Generic, Semigroup, Monoid, Arbitrary, CoArbitrary, Rep
Generic (Endo a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalSemigroup (Endo a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonoid (Endo a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal(Arbitrary a, CoArbitrary a) => Arbitrary (Endo a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrary(Arbitrary a, CoArbitrary a) => CoArbitrary (Endo a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrarytype Rep (Endo a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Endo"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Endo"
'PrefixI 'True) (S1 ('MetaSel ('Just"appEndo"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (a -> a))))
Monoid under multiplication.
Product x <> Product y == Product (x * y)Examples
Product 3 <> Product 4 <> memptyProduct {getProduct = 12}
mconcat [ Product n | n <- [2 .. 10]]Product {getProduct = 3628800}
Constructors
ProductgetProduct :: a
Instances26Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
Monad ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFunctor ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonadFix ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixApplicative ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFoldable ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableMonadZip ProductDefined in base-4.20.2.0 · Control.Monad.ZipFoldable1 ProductDefined in base-4.20.2.0 · Data.Foldable1NFData1 ProductDefined in deepseq-1.5.0.0 · Control.DeepSeqGeneric1 ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalBounded a => Bounded (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalEq a => Eq (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalData a => Data (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataNum a => Num (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalOrd a => Ord (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalRead a => Read (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalShow a => Show (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalGeneric (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNum a => Semigroup (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNum a => Monoid (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNFData a => NFData (Product a)Defined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary a => Arbitrary (Product a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary a => CoArbitrary (Product a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction a => Function (Product a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep (Product a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Product"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Product"
'PrefixI 'True) (S1 ('MetaSel ('Just"getProduct"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))type Rep1 Product = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Product"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Product"
'PrefixI 'True) (S1 ('MetaSel ('Just"getProduct"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))
Monoid under addition.
Sum a <> Sum b = Sum (a + b)Examples
Sum 1 <> Sum 2 <> memptySum {getSum = 3}
mconcat [ Sum n | n <- [3 .. 9]]Sum {getSum = 42}
Instances26Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
Monad SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFunctor SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonadFix SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixApplicative SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFoldable SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableMonadZip SumDefined in base-4.20.2.0 · Control.Monad.ZipFoldable1 SumDefined in base-4.20.2.0 · Data.Foldable1NFData1 SumDefined in deepseq-1.5.0.0 · Control.DeepSeqGeneric1 SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalBounded a => Bounded (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalEq a => Eq (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalData a => Data (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataNum a => Num (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalOrd a => Ord (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalRead a => Read (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalShow a => Show (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalGeneric (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNum a => Semigroup (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNum a => Monoid (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalNFData a => NFData (Sum a)Defined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary a => Arbitrary (Sum a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary a => CoArbitrary (Sum a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction a => Function (Sum a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep (Sum a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Sum"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Sum"
'PrefixI 'True) (S1 ('MetaSel ('Just"getSum"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))type Rep1 Sum = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal"Sum"
"GHC.Internal.Data.Semigroup.Internal"
"ghc-internal"
'True) (C1 ('MetaCons"Sum"
'PrefixI 'True) (S1 ('MetaSel ('Just"getSum"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))
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.
Instances92Applicative, …
Applicative GenDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.GenApplicative RoseDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.PropertyApplicative 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 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 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 CountDefined in size-based-0.1.3.2 · Control.Enumerable.CountApplicative MaxSizeDefined in size-based-0.1.3.2 · Control.Enumerable.ValuesApplicative ValuesDefined in size-based-0.1.3.2 · Control.Enumerable.ValuesApplicative PprMDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.PprLibApplicative QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.SyntaxApplicative EnumerateDefined in testing-feat-1.1.1.1 · Test.Feat.EnumerateApplicative FiniteDefined in testing-feat-1.1.1.1 · Test.Feat.FiniteApplicative []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 (PropertyM m)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.MonadicApplicative (SetM s)Defined in containers-0.7 · Data.GraphApplicative (State s)Defined in containers-0.7 · Utils.Containers.Internal.StateApplicative (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 (Shareable f)Defined in dictionary-sharing-0.1.0.0 · Data.ClassSharingApplicative 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.ApplicativeMonoid 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 (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 (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)).(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)))(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
An infix synonym for fmap.
The name of this operator is an allusion to $. Note the similarities between their types:
($) :: (a -> b) -> a -> b
(<$>) :: Functor f => (a -> b) -> f a -> f bWhereas $ is function application, <$> is function application lifted over a Functor.
Examples
Convert from a Maybe Int to a Maybe
String using show:
show <$> NothingNothing
show <$> Just 3Just "3"
Convert from an Either Int Int to an
Either Int String using show:
show <$> Left 17Left 17
show <$> Right 17Right "17"
Double each element of a list:
(*2) <$> [1,2,3][2,4,6]
Apply even to the second element of a pair:
even <$> (2,2)(2,True)
Replace all locations in the input with the same value.
The default definition is fmap . const, but this may be
overridden with a more efficient version.
Examples
Perform a computation with Maybe and replace the result with a constant value if it is Just:
'a' <$ Just 2Just 'a''a' <$ NothingNothing
A monoid on applicative functors.
If defined, some and many should be the least solutions of the equations:
Examples
Nothing <|> Just 42Just 42
[1, 2] <|> [3, 4][1,2,3,4]
empty <|> print (2^15)32768
Methods
empty :: f aThe identity of <|>
empty <|> a == a a <|> empty == a(<|>) :: f a -> f a -> f ainfixl 3An associative binary operation
some :: f a -> f [a]One or more.
Examples
Example1 expression some (putStr "la")lalalalalalalalala... * goes on forever *
Example1 expression some Nothingnothing
Example1 expression take 5 <$> some (Just 1)* hangs forever *
Note that this function can be used with Parsers based on Applicatives. In that case
some parserwill attempt to parseparserone or more times until it fails.many :: f a -> f [a]Zero or more.
Examples
Example1 expression many (putStr "la")lalalalalalalalala... * goes on forever *
Example1 expression many NothingJust []
Example1 expression take 5 <$> many (Just 1)* hangs forever *
Note that this function can be used with Parsers based on Applicatives. In that case
many parserwill attempt to parseparserzero or more times until it fails.
Instances47Alternative, …
Alternative SeqDefined in containers-0.7 · Data.Sequence.InternalAlternative STMDefined in ghc-internal-9.1003.0 · GHC.Internal.Conc.SyncAlternative ZipListDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListAlternative MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseAlternative PDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPAlternative ReadPDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPAlternative ReadPrecDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPrecAlternative IODefined in ghc-internal-9.1003.0 · GHC.Internal.BaseAlternative CountDefined in size-based-0.1.3.2 · Control.Enumerable.CountAlternative MaxSizeDefined in size-based-0.1.3.2 · Control.Enumerable.ValuesAlternative ValuesDefined in size-based-0.1.3.2 · Control.Enumerable.ValuesAlternative EnumerateDefined in testing-feat-1.1.1.1 · Test.Feat.EnumerateAlternative FiniteDefined in testing-feat-1.1.1.1 · Test.Feat.FiniteAlternative []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseCombines lists by concatenation, starting from the empty list.
Alternative ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.ProxyAlternative U1Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsAlternative f => Alternative (Shareable f)Defined in dictionary-sharing-0.1.0.0 · Data.ClassSharingAlternative f => Alternative (Lift f)Defined in transformers-0.6.1.1 · Control.Applicative.LiftA combination is Pure only either part is.
MonadPlus m => Alternative (WrappedMonad m)Defined in base-4.20.2.0 · Control.ApplicativeArrowPlus a => Alternative (ArrowMonad a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Arrow(Functor m, Monad m) => Alternative (MaybeT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.MaybeAlternative f => Alternative (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidAlternative f => Alternative (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalAlternative f => Alternative (Rec1 f)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsAlternative f => Alternative (Backwards f)Defined in transformers-0.6.1.1 · Control.Applicative.BackwardsTry alternatives in the same order as
f.Alternative f => Alternative (Reverse f)Defined in transformers-0.6.1.1 · Data.Functor.ReverseDerived instance.
Alternative m => Alternative (Kleisli m a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.ArrowAlternative m => Alternative (IdentityT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.IdentityAlternative m => Alternative (ReaderT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Reader(Functor m, Monad m, Monoid e) => Alternative (ExceptT e m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Except(Functor m, MonadPlus m) => Alternative (SelectT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Select(Functor m, MonadPlus m) => Alternative (StateT s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.State.Lazy(Functor m, MonadPlus m) => Alternative (StateT s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.State.Strict(Functor m, MonadPlus m) => Alternative (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.CPS(Monoid w, Alternative m) => Alternative (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.Lazy(Monoid w, Alternative m) => Alternative (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.Strict(Monoid w, Functor m, MonadPlus m) => Alternative (AccumT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Accum(ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b)Defined in base-4.20.2.0 · Control.Applicative(Generic1 f, Alternative (Rep1 f)) => Alternative (Generically1 f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Alternative f, Alternative g) => Alternative (Product f g)Defined in base-4.20.2.0 · Data.Functor.Product(Alternative f, Alternative g) => Alternative (f :*: g)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsAlternative f => Alternative (M1 i c f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Alternative f, Applicative g) => Alternative (Compose f g)Defined in base-4.20.2.0 · Data.Functor.Compose(Alternative f, Applicative g) => Alternative (f :.: g)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Functor m, MonadPlus m) => Alternative (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.CPS(Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.Lazy(Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.Strict
Constructors
WrapArrowunwrapArrow :: a b c
Instances9Generic1, Functor, Applicative, Alternative, Data, Generic, …
Generic1 (WrappedArrow a b)Defined in base-4.20.2.0 · Control.ApplicativeArrow a => Functor (WrappedArrow a b)Defined in base-4.20.2.0 · Control.ApplicativeArrow a => Applicative (WrappedArrow a b)Defined in base-4.20.2.0 · Control.Applicative(ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b)Defined in base-4.20.2.0 · Control.Applicative(Typeable a, Typeable b, Typeable c, Data (a b c)) => Data (WrappedArrow a b c)Defined in base-4.20.2.0 · Control.ApplicativeGeneric (WrappedArrow a b c)Defined in base-4.20.2.0 · Control.ApplicativeArbitrary (a b c) => Arbitrary (WrappedArrow a b c)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrarytype Rep (WrappedArrow a b c) = D1 ('MetaDataDefined in base-4.20.2.0 · Control.Applicative"WrappedArrow"
"Control.Applicative"
"base"
'True) (C1 ('MetaCons"WrapArrow"
'PrefixI 'True) (S1 ('MetaSel ('Just"unwrapArrow"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (a b c))))type Rep1 (WrappedArrow a b) = D1 ('MetaDataDefined in base-4.20.2.0 · Control.Applicative"WrappedArrow"
"Control.Applicative"
"base"
'True) (C1 ('MetaCons"WrapArrow"
'PrefixI 'True) (S1 ('MetaSel ('Just"unwrapArrow"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 (a b))))
Constructors
WrapMonadunwrapMonad :: m a
Instances10Generic1, Monad, Functor, Applicative, Alternative, Data, …
Generic1 (WrappedMonad m)Defined in base-4.20.2.0 · Control.ApplicativeMonad m => Monad (WrappedMonad m)Defined in base-4.20.2.0 · Control.ApplicativeMonad m => Functor (WrappedMonad m)Defined in base-4.20.2.0 · Control.ApplicativeMonad m => Applicative (WrappedMonad m)Defined in base-4.20.2.0 · Control.ApplicativeMonadPlus m => Alternative (WrappedMonad m)Defined in base-4.20.2.0 · Control.Applicative(Typeable m, Typeable a, Data (m a)) => Data (WrappedMonad m a)Defined in base-4.20.2.0 · Control.ApplicativeGeneric (WrappedMonad m a)Defined in base-4.20.2.0 · Control.ApplicativeArbitrary (m a) => Arbitrary (WrappedMonad m a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrarytype Rep (WrappedMonad m a) = D1 ('MetaDataDefined in base-4.20.2.0 · Control.Applicative"WrappedMonad"
"Control.Applicative"
"base"
'True) (C1 ('MetaCons"WrapMonad"
'PrefixI 'True) (S1 ('MetaSel ('Just"unwrapMonad"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (m a))))type Rep1 (WrappedMonad m) = D1 ('MetaDataDefined in base-4.20.2.0 · Control.Applicative"WrappedMonad"
"Control.Applicative"
"base"
'True) (C1 ('MetaCons"WrapMonad"
'PrefixI 'True) (S1 ('MetaSel ('Just"unwrapMonad"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 m)))
One or none.
It is useful for modelling any computation that is allowed to fail.
Examples
Using the Alternative instance of Control.Monad.Except, the following functions:
import Control.Monad.ExceptcanFail = throwError "it failed" :: Except String Intfinal = return 42 :: Except String Int
Can be combined by allowing the first function to fail:
runExcept $ canFail *> finalLeft "it failed"
runExcept $ optional canFail *> finalRight 42
A variant of <*> with the types of the arguments reversed. It differs from
flip in that the effects are resolved in the order the arguments are
presented.(<*>)
Examples
(<**>) (print 1) (id <$ print 2)12
flip (<*>) (print 1) (id <$ print 2)21
ZipList [4, 5, 6] <**> ZipList [(+1), (*2), (/3)]ZipList {getZipList = [5.0,10.0,2.0]}
Lift a function to actions.
Equivalent to Functor's fmap but implemented using only Applicative's methods:
liftA f a = pure f <*> a
As such this function may be used to implement a Functor instance from an Applicative one.
Examples
Using the Applicative instance for Lists:
liftA (+1) [1, 2][2,3]
Or the Applicative instance for Maybe
liftA (+1) (Just 3)Just 4
Lift a ternary function to actions.
The sum of a collection of actions using (<|>), generalizing concat.
asum is just like msum, but generalised to Alternative.
Examples
Basic usage:
asum [Just "Hello", Nothing, Just "World"]Just "Hello"
The Const functor.
Examples
fmap (++ "World") (Const "Hello")Const "Hello"
Because we ignore the second type parameter to Const,
the Applicative instance, which has
essentially turns into (<*>) :: Monoid m => Const m (a -> b) -> Const m a -> Const m bMonoid m => m -> m -> m, which is (<>)
Const [1, 2, 3] <*> Const [4, 5, 6]Const [1,2,3,4,5,6]
Instances50Generic1, Bifoldable, Bifoldable1, Bifunctor, Bitraversable, Eq2, …
Generic1 (Const a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstBifoldable ConstDefined in base-4.20.2.0 · Data.BifoldableBifoldable1 ConstDefined in base-4.20.2.0 · Data.Bifoldable1Bifunctor ConstDefined in base-4.20.2.0 · Data.BifunctorBitraversable ConstDefined in base-4.20.2.0 · Data.BitraversableEq2 ConstDefined in base-4.20.2.0 · Data.Functor.ClassesOrd2 ConstDefined in base-4.20.2.0 · Data.Functor.ClassesRead2 ConstDefined in base-4.20.2.0 · Data.Functor.ClassesShow2 ConstDefined in base-4.20.2.0 · Data.Functor.ClassesNFData2 ConstDefined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary2 ConstDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunctor (Const m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstMonoid m => Applicative (Const m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstFoldable (Const m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstTraversable (Const m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableEq a => Eq1 (Const a)Defined in base-4.20.2.0 · Data.Functor.ClassesOrd a => Ord1 (Const a)Defined in base-4.20.2.0 · Data.Functor.ClassesRead a => Read1 (Const a)Defined in base-4.20.2.0 · Data.Functor.ClassesShow a => Show1 (Const a)Defined in base-4.20.2.0 · Data.Functor.ClassesContravariant (Const a)Defined in base-4.20.2.0 · Data.Functor.ContravariantNFData a => NFData1 (Const a)Defined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary a => Arbitrary1 (Const a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryBounded a => Bounded (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstEnum a => Enum (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstEq a => Eq (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstFloating a => Floating (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstFractional a => Fractional (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstIntegral a => Integral (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const(Typeable k, Data a, Typeable b) => Data (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataNum a => Num (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstOrd a => Ord (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstRead a => Read (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstReal a => Real (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstRealFloat a => RealFloat (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstRealFrac a => RealFrac (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstShow a => Show (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstIx a => Ix (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstIsString a => IsString (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.StringGeneric (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstSemigroup a => Semigroup (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstMonoid a => Monoid (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstBits a => Bits (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstFiniteBits a => FiniteBits (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstStorable a => Storable (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstNFData a => NFData (Const a b)Defined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary a => Arbitrary (Const a b)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary a => CoArbitrary (Const a b)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction a => Function (Const a b)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Functiontype Rep (Const a b) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const"Const"
"GHC.Internal.Data.Functor.Const"
"ghc-internal"
'True) (C1 ('MetaCons"Const"
'PrefixI 'True) (S1 ('MetaSel ('Just"getConst"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))type Rep1 (Const a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const"Const"
"GHC.Internal.Data.Functor.Const"
"ghc-internal"
'True) (C1 ('MetaCons"Const"
'PrefixI 'True) (S1 ('MetaSel ('Just"getConst"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))
Lists, but with an Applicative functor based on zipping.
Examples
In contrast to the Applicative for GHC.List.List:
(+) <$> [1, 2, 3] <*> [4, 5, 6][5,6,7,6,7,8,7,8,9]
The Applicative instance of ZipList applies the operation by pairing up the elements, analogous to zipWithN
(+) <$> ZipList [1, 2, 3] <*> ZipList [4, 5, 6]ZipList {getZipList = [5,7,9]}
(,,,) <$> ZipList [1, 2] <*> ZipList [3, 4] <*> ZipList [5, 6] <*> ZipList [7, 8]ZipList {getZipList = [(1,3,5,7),(2,4,6,8)]}
ZipList [(+1), (^2), (/ 2)] <*> ZipList [5, 5, 5]ZipList {getZipList = [6.0,25.0,2.5]}
Constructors
ZipListgetZipList :: [a]
Instances21Functor, Applicative, Foldable, Traversable, Alternative, NFData1, …
Functor ZipListDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListApplicative 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"]}Foldable ZipListDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListTraversable ZipListDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListAlternative ZipListDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListNFData1 ZipListDefined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary1 ZipListDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryGeneric1 ZipListDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListIsList (ZipList a)Defined in ghc-internal-9.1003.0 · GHC.Internal.IsListEq a => Eq (ZipList a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListData a => Data (ZipList a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListOrd a => Ord (ZipList a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListRead a => Read (ZipList a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListShow a => Show (ZipList a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListGeneric (ZipList a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListNFData a => NFData (ZipList a)Defined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary a => Arbitrary (ZipList a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary a => CoArbitrary (ZipList a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrarytype Rep (ZipList a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipList"ZipList"
"GHC.Internal.Functor.ZipList"
"ghc-internal"
'True) (C1 ('MetaCons"ZipList"
'PrefixI 'True) (S1 ('MetaSel ('Just"getZipList"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [a])))type Rep1 ZipList = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipList"ZipList"
"GHC.Internal.Functor.ZipList"
"ghc-internal"
'True) (C1 ('MetaCons"ZipList"
'PrefixI 'True) (S1 ('MetaSel ('Just"getZipList"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 [])))type Item (ZipList a) = aDefined in ghc-internal-9.1003.0 · GHC.Internal.IsList
The definition of pure for the applicative instance.
Increases the cost/size of all values in the given set.