| Copyright | (c) 2016 Stephen Diehl (c) 2016-2018 Serokell (c) 2018-2023 Kowainik |
|---|---|
| License | MIT |
| Maintainer | Kowainik <xrom.xkov@gmail.com> |
| Stability | Stable |
| Portability | Portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Relude.Applicative
Contents
Description
This module contains reexports of Applicative and related functional.
Additionally, it provides convenient combinators to work with Applicative.
Synopsis
- class Applicative f => Alternative (f :: Type -> Type) where
- class Functor f => Applicative (f :: Type -> Type) where
- newtype Const a (b :: k) = Const {
- getConst :: a
- newtype ZipList a = ZipList {
- getZipList :: [a]
- liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
- optional :: Alternative f => f a -> f (Maybe a)
- (<**>) :: Applicative f => f a -> f (a -> b) -> f b
- pass :: Applicative f => f ()
- appliedTo :: Applicative f => f a -> f (a -> b) -> f b
Reexports
Main data types and functions reexported from Control.Applicative.
class Applicative f => Alternative (f :: Type -> Type) where #
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
The identity of <|>
empty <|> a == a a <|> empty == a
(<|>) :: f a -> f a -> f a infixl 3 #
An associative binary operation
One or more.
Examples
>>>some (putStr "la")lalalalalalalalala... * goes on forever *
>>>some Nothingnothing
>>>take 5 <$> some (Just 1)* hangs forever *
Note that this function can be used with Parsers based on
Applicatives. In that case some parser will attempt to
parse parser one or more times until it fails.
Zero or more.
Examples
>>>many (putStr "la")lalalalalalalalala... * goes on forever *
>>>many NothingJust []
>>>take 5 <$> many (Just 1)* hangs forever *
Note that this function can be used with Parsers based on
Applicatives. In that case many parser will attempt to
parse parser zero or more times until it fails.
Instances
class Functor f => Applicative (f :: Type -> Type) where #
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:
(<*>) =liftA2id
liftA2f x y = f<$>x<*>y
Further, any definition must satisfy the following:
- Identity
pureid<*>v = v- Composition
pure(.)<*>u<*>v<*>w = u<*>(v<*>w)- Homomorphism
puref<*>purex =pure(f x)- Interchange
u
<*>purey =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 y
it follows from the above that
liftA2p (liftA2q u v) =liftA2f u .liftA2g v
If f is also a Monad, it should satisfy
(which implies that pure and <*> satisfy the applicative functor laws).
Methods
Lift a value into the Structure.
Examples
>>>pure 1 :: Maybe IntJust 1
>>>pure 'z' :: [Char]"z"
>>>pure (pure ":D") :: Maybe [String]Just [":D"]
(<*>) :: f (a -> b) -> f a -> f b infixl 4 #
Sequential application.
A few functors support an implementation of <*> that is more
efficient than the default one.
Example
Used in combination with , (<$>) can be used to build a record.(<*>)
>>>data MyState = MyState {arg1 :: Foo, arg2 :: Bar, arg3 :: Baz}
>>>produceFoo :: Applicative f => f Foo>>>produceBar :: Applicative f => f Bar>>>produceBaz :: Applicative f => f Baz
>>>mkState :: Applicative f => f MyState>>>mkState = MyState <$> produceFoo <*> produceBar <*> produceBaz
liftA2 :: (a -> b -> c) -> f a -> f b -> f c #
Lift 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
>>>liftA2 (,) (Just 3) (Just 5)Just (3,5)
>>>liftA2 (+) [1, 2, 3] [4, 5, 6][5,6,7,6,7,8,7,8,9]
(*>) :: f a -> f b -> f b infixl 4 #
Sequence 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.
>>>Just 2 *> Just 3Just 3
>>>Nothing *> Just 3Nothing
Of course a more interesting use case would be to have effectful computations instead of just returning pure values.
>>>import Data.Char>>>import GHC.Internal.Text.ParserCombinators.ReadP>>>let p = string "my name is " *> munch1 isAlpha <* eof>>>readP_to_S p "my name is Simon"[("Simon","")]
(<*) :: f a -> f b -> f a infixl 4 #
Sequence actions, discarding the value of the second argument.
Instances
| Applicative Complex | Since: base-4.9.0.0 |
| Applicative First | Since: base-4.9.0.0 |
| Applicative Last | Since: base-4.9.0.0 |
| Applicative Max | Since: base-4.9.0.0 |
| Applicative Min | Since: base-4.9.0.0 |
| Applicative Put | |
| Applicative Seq | Since: containers-0.5.4 |
| Applicative Tree | |
| Applicative NonEmpty | @since base-4.9.0.0 |
| Applicative STM | @since base-4.8.0.0 |
| Applicative Identity | @since base-4.8.0.0 |
| Applicative First | @since base-4.8.0.0 |
| Applicative Last | @since base-4.8.0.0 |
| Applicative Down | @since base-4.11.0.0 |
| Applicative Dual | @since base-4.8.0.0 |
| Applicative Product | @since base-4.8.0.0 |
| Applicative Sum | @since base-4.8.0.0 |
| Applicative ZipList | f <$> ZipList xs1 <*> ... <*> ZipList xsN
= ZipList (zipWithN f xs1 ... xsN)where (\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"]}@since base-2.01 |
| Applicative Par1 | @since base-4.9.0.0 |
| Applicative P | @since base-4.5.0.0 |
| Applicative ReadP | @since base-4.6.0.0 |
| Applicative ReadPrec | @since base-4.6.0.0 |
Defined in GHC.Internal.Text.ParserCombinators.ReadPrec | |
| Applicative IO | @since base-2.01 |
| Applicative Q | |
| Applicative Maybe | @since base-2.01 |
| Applicative Solo | @since base-4.15 |
| Applicative [] | @since base-2.01 |
| Monad m => Applicative (WrappedMonad m) | Since: base-2.1 |
Defined in Control.Applicative Methods pure :: a -> WrappedMonad m a # (<*>) :: WrappedMonad m (a -> b) -> WrappedMonad m a -> WrappedMonad m b # liftA2 :: (a -> b -> c) -> WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m c # (*>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b # (<*) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m a # | |
| Applicative (SetM s) | |
| Arrow a => Applicative (ArrowMonad a) | @since base-4.6.0.0 |
Defined in GHC.Internal.Control.Arrow Methods pure :: a0 -> ArrowMonad a a0 # (<*>) :: ArrowMonad a (a0 -> b) -> ArrowMonad a a0 -> ArrowMonad a b # liftA2 :: (a0 -> b -> c) -> ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a c # (*>) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a b # (<*) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a a0 # | |
| Applicative (Either e) | @since base-3.0 |
| Applicative (Proxy :: Type -> Type) | @since base-4.7.0.0 |
| Applicative (U1 :: Type -> Type) | @since base-4.9.0.0 |
| Applicative (IParser t) | |
| Applicative f => Applicative (Lift f) | A combination is |
| (Functor m, Monad m) => Applicative (MaybeT m) | |
| Monoid a => Applicative ((,) a) | For tuples, the ("hello ", (+15)) <*> ("world!", 2002)
("hello world!",2017)@since base-2.01 |
| Arrow a => Applicative (WrappedArrow a b) | Since: base-2.1 |
Defined in Control.Applicative Methods pure :: a0 -> WrappedArrow a b a0 # (<*>) :: WrappedArrow a b (a0 -> b0) -> WrappedArrow a b a0 -> WrappedArrow a b b0 # liftA2 :: (a0 -> b0 -> c) -> WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b c # (*>) :: WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b b0 # (<*) :: WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b a0 # | |
| (Applicative f, Monad f) => Applicative (WhenMissing f x) | Equivalent to Since: containers-0.5.9 |
Defined in Data.IntMap.Internal Methods pure :: a -> WhenMissing f x a # (<*>) :: WhenMissing f x (a -> b) -> WhenMissing f x a -> WhenMissing f x b # liftA2 :: (a -> b -> c) -> WhenMissing f x a -> WhenMissing f x b -> WhenMissing f x c # (*>) :: WhenMissing f x a -> WhenMissing f x b -> WhenMissing f x b # (<*) :: WhenMissing f x a -> WhenMissing f x b -> WhenMissing f x a # | |
| Applicative m => Applicative (Kleisli m a) | @since base-4.14.0.0 |
Defined in GHC.Internal.Control.Arrow | |
| Monoid m => Applicative (Const m :: Type -> Type) | @since base-2.0.1 |
| Applicative f => Applicative (Ap f) | @since base-4.12.0.0 |
| Applicative f => Applicative (Alt f) | @since base-4.8.0.0 |
| (Generic1 f, Applicative (Rep1 f)) => Applicative (Generically1 f) | @since base-4.17.0.0 |
Defined in GHC.Internal.Generics Methods pure :: a -> Generically1 f a # (<*>) :: Generically1 f (a -> b) -> Generically1 f a -> Generically1 f b # liftA2 :: (a -> b -> c) -> Generically1 f a -> Generically1 f b -> Generically1 f c # (*>) :: Generically1 f a -> Generically1 f b -> Generically1 f b # (<*) :: Generically1 f a -> Generically1 f b -> Generically1 f a # | |
| Applicative f => Applicative (Rec1 f) | @since base-4.9.0.0 |
| Applicative (t m) => Applicative (LiftingAccum t m) | Since: mtl-2.3 |
Defined in Control.Monad.Accum Methods pure :: a -> LiftingAccum t m a # (<*>) :: LiftingAccum t m (a -> b) -> LiftingAccum t m a -> LiftingAccum t m b # liftA2 :: (a -> b -> c) -> LiftingAccum t m a -> LiftingAccum t m b -> LiftingAccum t m c # (*>) :: LiftingAccum t m a -> LiftingAccum t m b -> LiftingAccum t m b # (<*) :: LiftingAccum t m a -> LiftingAccum t m b -> LiftingAccum t m a # | |
| Applicative (t m) => Applicative (LiftingSelect t m) | Since: mtl-2.3 |
Defined in Control.Monad.Select Methods pure :: a -> LiftingSelect t m a # (<*>) :: LiftingSelect t m (a -> b) -> LiftingSelect t m a -> LiftingSelect t m b # liftA2 :: (a -> b -> c) -> LiftingSelect t m a -> LiftingSelect t m b -> LiftingSelect t m c # (*>) :: LiftingSelect t m a -> LiftingSelect t m b -> LiftingSelect t m b # (<*) :: LiftingSelect t m a -> LiftingSelect t m b -> LiftingSelect t m a # | |
| Applicative f => Applicative (Backwards f) | Apply |
Defined in Control.Applicative.Backwards | |
| (Monoid w, Functor m, Monad m) => Applicative (AccumT w m) | |
Defined in Control.Monad.Trans.Accum | |
| (Functor m, Monad m) => Applicative (ExceptT e m) | |
Defined in Control.Monad.Trans.Except | |
| Applicative m => Applicative (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
| Applicative m => Applicative (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
| (Functor m, Monad m) => Applicative (SelectT r m) | |
Defined in Control.Monad.Trans.Select | |
| (Functor m, Monad m) => Applicative (StateT s m) | |
Defined in Control.Monad.Trans.State.Lazy | |
| (Functor m, Monad m) => Applicative (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
| (Functor m, Monad m) => Applicative (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.CPS | |
| (Monoid w, Applicative m) => Applicative (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.Lazy | |
| (Monoid w, Applicative m) => Applicative (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.Strict | |
| Monoid a => Applicative (Constant a :: Type -> Type) | |
Defined in Data.Functor.Constant | |
| Applicative f => Applicative (Reverse f) | Derived instance. |
| (Monoid a, Monoid b) => Applicative ((,,) a b) | @since base-4.14.0.0 |
| (Applicative f, Applicative g) => Applicative (Product f g) | Since: base-4.9.0.0 |
Defined in Data.Functor.Product | |
| (Monad f, Applicative f) => Applicative (WhenMatched f x y) | Equivalent to Since: containers-0.5.9 |
Defined in Data.IntMap.Internal Methods pure :: a -> WhenMatched f x y a # (<*>) :: WhenMatched f x y (a -> b) -> WhenMatched f x y a -> WhenMatched f x y b # liftA2 :: (a -> b -> c) -> WhenMatched f x y a -> WhenMatched f x y b -> WhenMatched f x y c # (*>) :: WhenMatched f x y a -> WhenMatched f x y b -> WhenMatched f x y b # (<*) :: WhenMatched f x y a -> WhenMatched f x y b -> WhenMatched f x y a # | |
| (Applicative f, Monad f) => Applicative (WhenMissing f k x) | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal Methods pure :: a -> WhenMissing f k x a # (<*>) :: WhenMissing f k x (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b # liftA2 :: (a -> b -> c) -> WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x c # (*>) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x b # (<*) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x a # | |
| (Applicative f, Applicative g) => Applicative (f :*: g) | @since base-4.9.0.0 |
| Monoid c => Applicative (K1 i c :: Type -> Type) | @since base-4.12.0.0 |
| Applicative (ContT r m) | |
Defined in Control.Monad.Trans.Cont | |
| (Monoid a, Monoid b, Monoid c) => Applicative ((,,,) a b c) | @since base-4.14.0.0 |
Defined in GHC.Internal.Base | |
| Applicative ((->) r) | @since base-2.01 |
| (Applicative f, Applicative g) => Applicative (Compose f g) | Since: base-4.9.0.0 |
Defined in Data.Functor.Compose | |
| (Monad f, Applicative f) => Applicative (WhenMatched f k x y) | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal Methods pure :: a -> WhenMatched f k x y a # (<*>) :: WhenMatched f k x y (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b # liftA2 :: (a -> b -> c) -> WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y c # (*>) :: WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y b # (<*) :: WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y a # | |
| (Applicative f, Applicative g) => Applicative (f :.: g) | @since base-4.9.0.0 |
| Applicative f => Applicative (M1 i c f) | @since base-4.9.0.0 |
| (Functor m, Monad m) => Applicative (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.CPS | |
| (Monoid w, Functor m, Monad m) => Applicative (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.Lazy | |
| (Monoid w, Functor m, Monad m) => Applicative (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.Strict | |
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]
Instances
| Generic1 (Const a :: k -> Type) | |||||
Defined in GHC.Internal.Data.Functor.Const Associated Types
| |||||
| Bifoldable (Const :: Type -> Type -> Type) | Since: base-4.10.0.0 | ||||
| Bifoldable1 (Const :: Type -> Type -> Type) | |||||
Defined in Data.Bifoldable1 | |||||
| Bifunctor (Const :: Type -> Type -> Type) | Since: base-4.8.0.0 | ||||
| Bitraversable (Const :: Type -> Type -> Type) | Since: base-4.10.0.0 | ||||
Defined in Data.Bitraversable Methods bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Const a b -> f (Const c d) # | |||||
| Eq2 (Const :: Type -> Type -> Type) | Since: base-4.9.0.0 | ||||
| Ord2 (Const :: Type -> Type -> Type) | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes | |||||
| Read2 (Const :: Type -> Type -> Type) | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes Methods liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (Const a b) # liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [Const a b] # liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (Const a b) # liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [Const a b] # | |||||
| Show2 (Const :: Type -> Type -> Type) | Since: base-4.9.0.0 | ||||
| NFData2 (Const :: Type -> Type -> Type) | Since: deepseq-1.4.3.0 | ||||
Defined in Control.DeepSeq | |||||
| Hashable2 (Const :: Type -> Type -> Type) | |||||
Defined in Data.Hashable.Class | |||||
| Eq a => Eq1 (Const a :: Type -> Type) | Since: base-4.9.0.0 | ||||
| Ord a => Ord1 (Const a :: Type -> Type) | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes | |||||
| Read a => Read1 (Const a :: Type -> Type) | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes Methods liftReadsPrec :: (Int -> ReadS a0) -> ReadS [a0] -> Int -> ReadS (Const a a0) # liftReadList :: (Int -> ReadS a0) -> ReadS [a0] -> ReadS [Const a a0] # liftReadPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec (Const a a0) # liftReadListPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec [Const a a0] # | |||||
| Show a => Show1 (Const a :: Type -> Type) | Since: base-4.9.0.0 | ||||
| Contravariant (Const a :: Type -> Type) | |||||
| NFData a => NFData1 (Const a :: Type -> Type) | Since: deepseq-1.4.3.0 | ||||
Defined in Control.DeepSeq | |||||
| Monoid m => Applicative (Const m :: Type -> Type) | @since base-2.0.1 | ||||
| Functor (Const m :: Type -> Type) | @since base-2.01 | ||||
| Foldable (Const m :: Type -> Type) | @since base-4.7.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods fold :: Monoid m0 => Const m m0 -> m0 # foldMap :: Monoid m0 => (a -> m0) -> Const m a -> m0 # foldMap' :: Monoid m0 => (a -> m0) -> Const m a -> m0 # foldr :: (a -> b -> b) -> b -> Const m a -> b # foldr' :: (a -> b -> b) -> b -> Const m a -> b # foldl :: (b -> a -> b) -> b -> Const m a -> b # foldl' :: (b -> a -> b) -> b -> Const m a -> b # foldr1 :: (a -> a -> a) -> Const m a -> a # foldl1 :: (a -> a -> a) -> Const m a -> a # elem :: Eq a => a -> Const m a -> Bool # maximum :: Ord a => Const m a -> a # minimum :: Ord a => Const m a -> a # | |||||
| Traversable (Const m :: Type -> Type) | @since base-4.7.0.0 | ||||
Defined in GHC.Internal.Data.Traversable | |||||
| Hashable a => Hashable1 (Const a :: Type -> Type) | |||||
Defined in Data.Hashable.Class | |||||
| NFData a => NFData (Const a b) | Since: deepseq-1.4.0.0 | ||||
Defined in Control.DeepSeq | |||||
| Monoid a => Monoid (Const a b) | @since base-4.9.0.0 | ||||
| Semigroup a => Semigroup (Const a b) | @since base-4.9.0.0 | ||||
| Bits a => Bits (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods (.&.) :: Const a b -> Const a b -> Const a b # (.|.) :: Const a b -> Const a b -> Const a b # xor :: Const a b -> Const a b -> Const a b # complement :: Const a b -> Const a b # shift :: Const a b -> Int -> Const a b # rotate :: Const a b -> Int -> Const a b # setBit :: Const a b -> Int -> Const a b # clearBit :: Const a b -> Int -> Const a b # complementBit :: Const a b -> Int -> Const a b # testBit :: Const a b -> Int -> Bool # bitSizeMaybe :: Const a b -> Maybe Int # isSigned :: Const a b -> Bool # shiftL :: Const a b -> Int -> Const a b # unsafeShiftL :: Const a b -> Int -> Const a b # shiftR :: Const a b -> Int -> Const a b # unsafeShiftR :: Const a b -> Int -> Const a b # rotateL :: Const a b -> Int -> Const a b # | |||||
| FiniteBits a => FiniteBits (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods finiteBitSize :: Const a b -> Int # countLeadingZeros :: Const a b -> Int # countTrailingZeros :: Const a b -> Int # | |||||
| (Typeable k, Data a, Typeable b) => Data (Const a b) | @since base-4.10.0.0 | ||||
Defined in GHC.Internal.Data.Data Methods gfoldl :: (forall d b0. Data d => c (d -> b0) -> d -> c b0) -> (forall g. g -> c g) -> Const a b -> c (Const a b) # gunfold :: (forall b0 r. Data b0 => c (b0 -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Const a b) # toConstr :: Const a b -> Constr # dataTypeOf :: Const a b -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Const a b)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Const a b)) # gmapT :: (forall b0. Data b0 => b0 -> b0) -> Const a b -> Const a b # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Const a b -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Const a b -> r # gmapQ :: (forall d. Data d => d -> u) -> Const a b -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Const a b -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Const a b -> m (Const a b) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Const a b -> m (Const a b) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Const a b -> m (Const a b) # | |||||
| IsString a => IsString (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.String Methods fromString :: String -> Const a b # | |||||
| Bounded a => Bounded (Const a b) | @since base-4.9.0.0 | ||||
| Enum a => Enum (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods succ :: Const a b -> Const a b # pred :: Const a b -> Const a b # fromEnum :: Const a b -> Int # enumFrom :: Const a b -> [Const a b] # enumFromThen :: Const a b -> Const a b -> [Const a b] # enumFromTo :: Const a b -> Const a b -> [Const a b] # enumFromThenTo :: Const a b -> Const a b -> Const a b -> [Const a b] # | |||||
| Floating a => Floating (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods exp :: Const a b -> Const a b # log :: Const a b -> Const a b # sqrt :: Const a b -> Const a b # (**) :: Const a b -> Const a b -> Const a b # logBase :: Const a b -> Const a b -> Const a b # sin :: Const a b -> Const a b # cos :: Const a b -> Const a b # tan :: Const a b -> Const a b # asin :: Const a b -> Const a b # acos :: Const a b -> Const a b # atan :: Const a b -> Const a b # sinh :: Const a b -> Const a b # cosh :: Const a b -> Const a b # tanh :: Const a b -> Const a b # asinh :: Const a b -> Const a b # acosh :: Const a b -> Const a b # atanh :: Const a b -> Const a b # log1p :: Const a b -> Const a b # expm1 :: Const a b -> Const a b # | |||||
| RealFloat a => RealFloat (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods floatRadix :: Const a b -> Integer # floatDigits :: Const a b -> Int # floatRange :: Const a b -> (Int, Int) # decodeFloat :: Const a b -> (Integer, Int) # encodeFloat :: Integer -> Int -> Const a b # exponent :: Const a b -> Int # significand :: Const a b -> Const a b # scaleFloat :: Int -> Const a b -> Const a b # isInfinite :: Const a b -> Bool # isDenormalized :: Const a b -> Bool # isNegativeZero :: Const a b -> Bool # | |||||
| Storable a => Storable (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const | |||||
| Generic (Const a b) | |||||
Defined in GHC.Internal.Data.Functor.Const Associated Types
| |||||
| Ix a => Ix (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods range :: (Const a b, Const a b) -> [Const a b] # index :: (Const a b, Const a b) -> Const a b -> Int # unsafeIndex :: (Const a b, Const a b) -> Const a b -> Int # inRange :: (Const a b, Const a b) -> Const a b -> Bool # rangeSize :: (Const a b, Const a b) -> Int # unsafeRangeSize :: (Const a b, Const a b) -> Int # | |||||
| Num a => Num (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const | |||||
| Read a => Read (Const a b) | This instance would be equivalent to the derived instances of the
@since base-4.8.0.0 | ||||
| Fractional a => Fractional (Const a b) | @since base-4.9.0.0 | ||||
| Integral a => Integral (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods quot :: Const a b -> Const a b -> Const a b # rem :: Const a b -> Const a b -> Const a b # div :: Const a b -> Const a b -> Const a b # mod :: Const a b -> Const a b -> Const a b # quotRem :: Const a b -> Const a b -> (Const a b, Const a b) # divMod :: Const a b -> Const a b -> (Const a b, Const a b) # | |||||
| Real a => Real (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const Methods toRational :: Const a b -> Rational # | |||||
| RealFrac a => RealFrac (Const a b) | @since base-4.9.0.0 | ||||
| Show a => Show (Const a b) | This instance would be equivalent to the derived instances of the
@since base-4.8.0.0 | ||||
| Eq a => Eq (Const a b) | @since base-4.9.0.0 | ||||
| Ord a => Ord (Const a b) | @since base-4.9.0.0 | ||||
| Hashable a => Hashable (Const a b) | |||||
Defined in Data.Hashable.Class | |||||
| type Rep1 (Const a :: k -> Type) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const | |||||
| type Rep (Const a b) | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Data.Functor.Const | |||||
Lists, but with an Applicative functor based on zipping.
Examples
In contrast to the Applicative for 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
| ZipList | |
Fields
| |
Instances
| NFData1 ZipList | Since: deepseq-1.4.3.0 | ||||
Defined in Control.DeepSeq | |||||
| Alternative ZipList | @since base-4.11.0.0 | ||||
| Applicative ZipList | f <$> ZipList xs1 <*> ... <*> ZipList xsN
= ZipList (zipWithN f xs1 ... xsN)where (\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"]}@since base-2.01 | ||||
| Functor ZipList | @since base-2.01 | ||||
| Foldable ZipList | @since base-4.9.0.0 | ||||
Defined in GHC.Internal.Functor.ZipList Methods fold :: Monoid m => ZipList m -> m # foldMap :: Monoid m => (a -> m) -> ZipList a -> m # foldMap' :: Monoid m => (a -> m) -> ZipList a -> m # foldr :: (a -> b -> b) -> b -> ZipList a -> b # foldr' :: (a -> b -> b) -> b -> ZipList a -> b # foldl :: (b -> a -> b) -> b -> ZipList a -> b # foldl' :: (b -> a -> b) -> b -> ZipList a -> b # foldr1 :: (a -> a -> a) -> ZipList a -> a # foldl1 :: (a -> a -> a) -> ZipList a -> a # elem :: Eq a => a -> ZipList a -> Bool # maximum :: Ord a => ZipList a -> a # minimum :: Ord a => ZipList a -> a # | |||||
| Traversable ZipList | @since base-4.9.0.0 | ||||
| Generic1 ZipList | |||||
Defined in GHC.Internal.Functor.ZipList Associated Types
| |||||
| NFData a => NFData (ZipList a) | Since: deepseq-1.4.0.0 | ||||
Defined in Control.DeepSeq | |||||
| Data a => Data (ZipList a) | @since base-4.14.0.0 | ||||
Defined in GHC.Internal.Functor.ZipList Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ZipList a -> c (ZipList a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (ZipList a) # toConstr :: ZipList a -> Constr # dataTypeOf :: ZipList a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (ZipList a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (ZipList a)) # gmapT :: (forall b. Data b => b -> b) -> ZipList a -> ZipList a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ZipList a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ZipList a -> r # gmapQ :: (forall d. Data d => d -> u) -> ZipList a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ZipList a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ZipList a -> m (ZipList a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ZipList a -> m (ZipList a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ZipList a -> m (ZipList a) # | |||||
| Generic (ZipList a) | |||||
Defined in GHC.Internal.Functor.ZipList Associated Types
| |||||
| IsList (ZipList a) | @since base-4.15.0.0 | ||||
| Read a => Read (ZipList a) | @since base-4.7.0.0 | ||||
| Show a => Show (ZipList a) | @since base-4.7.0.0 | ||||
| Eq a => Eq (ZipList a) | @since base-4.7.0.0 | ||||
| Ord a => Ord (ZipList a) | @since base-4.7.0.0 | ||||
| type Rep1 ZipList | @since base-4.7.0.0 | ||||
Defined in GHC.Internal.Functor.ZipList | |||||
| type Rep (ZipList a) | @since base-4.7.0.0 | ||||
Defined in GHC.Internal.Functor.ZipList | |||||
| type Item (ZipList a) | |||||
Defined in GHC.Internal.IsList | |||||
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d #
Lift a ternary function to actions.
optional :: Alternative f => f a -> f (Maybe a) #
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.Except
>>>canFail = throwError "it failed" :: Except String Int>>>final = 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
(<**>) :: Applicative f => f a -> f (a -> b) -> f b infixl 4 #
A variant of <*> with the types of the arguments reversed. It differs from
in that the effects are resolved in the order the arguments are
presented.flip (<*>)
Examples
>>>(<**>) (print 1) (id <$ print 2)1 2
>>>flip (<*>) (print 1) (id <$ print 2)2 1
>>>ZipList [4, 5, 6] <**> ZipList [(+1), (*2), (/3)]ZipList {getZipList = [5.0,10.0,2.0]}
Combinators
pass :: Applicative f => f () #
Shorter alias for pure ().
>>>pass :: Maybe ()Just ()
Useful shortcut when need an empty action:
printJust :: Maybe Int -> IO ()
printJust mInt = case mInt of
Just i -> putStrLn $ "Number: " ++ show i
Nothing -> pass
appliedTo :: Applicative f => f a -> f (a -> b) -> f b #
Named version of the <**> operator, which is <*> but flipped. It is
helpful for chaining applicative operations in forward applications using
&.
>>>Just (+ 1) & appliedTo (Just 2)Just 3>>>Just (+) & appliedTo (Just 1) & appliedTo (Just 2)Just 3>>>Nothing & appliedTo (Just 2)Nothing
Since: 0.5.0