In a strict language, where creating the entire input list of tokens
in one shot may be infeasible, we can use a lazy "callback" kind of
architecture instead. The lexer returns a single token at a time,
together with a continuation. The next parser is responsible for
pulling on the token stream, applying the continuation where necessary.
Modulepolyparse-1.13Haskell98
Text.ParserCombinators.Poly.Lex
In a strict language, where creating the entire input list of tokens in one shot may be infeasible, we can use a lazy "callback" kind of architecture instead. The lexer returns a single token at a time, together with a continuation.
This module defines a Parser type (capable of use with the Poly combinators), specialised to the callback-lexer style of input stream.
- 7 types
- 2 classes
- 12 values
- Packagepolyparse-1.13
- Exports22
- LanguageHaskell98
- LicenceLicenseRef-LGPL
- SourceLex.hs
The Parser datatype
4 declarationsThis Parser datatype is a specialised parsing monad with error
reporting. This version is specialised to pre-lexed String input,
where the lexer has been written to yield a LexReturn.
Instances7Monad, Functor, MonadFail, Applicative, Alternative, Commitment, …
Monad (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LexFunctor (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LexMonadFail (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LexApplicative (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LexAlternative (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LexCommitment (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LexPolyParse (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.Lex
A return type like Either, that distinguishes not only between right and wrong answers, but also has commitment, so that a failure cannot be undone. This should only be used for writing very primitive parsers - really it is an internal detail of the library. The z type is the remaining unconsumed input.
Apply a parser to an input token sequence.
Basic parsers
Simply return the next token in the input tokenstream.
Succeed if the end of file/input has been reached, fail otherwise.
Return the next token if it satisfies the given predicate.
p onFail q means parse p, unless p fails, in which case
parse q instead.
Can be chained together to give multiple attempts to parse something.
(Note that q could itself be a failing parser, e.g. to change the error
message from that defined in p to something different.)
However, a severe failure in p cannot be ignored.
Re-parsing
Push some tokens back onto the front of the input stream and reparse. This is useful e.g. for recursively expanding macros. When the user-parser recognises a macro use, it can lookup the macro expansion from the parse state, lex it, and then stuff the lexed expansion back down into the parser.
Re-export all more general combinators
13 declarationsA 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.
Instances36Alternative, …
Alternative GetDefined in binary-0.8.9.3 · Data.Binary.Get.InternalAlternative 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 ParserDefined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerAlternative ParserDefined in polyparse-1.13 · Text.ParserCombinators.Poly.ByteStringAlternative ParserDefined in polyparse-1.13 · Text.ParserCombinators.Poly.ByteStringCharAlternative ParserDefined in polyparse-1.13 · Text.ParserCombinators.Poly.TextAlternative []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 (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LazyAlternative (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LexAlternative (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.ParserAlternative (Parser s)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.StateTextMonadPlus 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.ArrowAlternative (Parser s t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.StateLazyAlternative (Parser s t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.StateParserAlternative 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 m => Alternative (Kleisli m a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Arrow(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.GenericsAlternative (Parser s t e)Defined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerWallace(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
A functor with application, providing operations to
A minimal complete definition must include implementations of pure and of either <*> or liftA2. If it defines both, then they must behave the same as their default definitions:
(<*>) = liftA2 idliftA2 f x y = f Prelude.<$> x <*> yFurther, any definition must satisfy the following:
- Identity
pure id <*> v = v- Composition
pure (.) <*> u <*> v <*> w = u <*> (v <*> w)- Homomorphism
pure f <*> pure x = pure (f x)- Interchange
u <*> pure y = pure ($ y) <*> u
The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:
As a consequence of these laws, the Functor instance for f will satisfy
It may be useful to note that supposing
forall x y. p (q x y) = f x . g yit follows from the above that
liftA2 p (liftA2 q u v) = liftA2 f u . liftA2 g vIf f is also a Monad, it should satisfy
(which implies that pure and <*> satisfy the applicative functor laws).
Methods
pure :: a -> f aLift a value into the Structure.
Examples
Example1 expression pure 1 :: Maybe IntJust 1
Example1 expression pure 'z' :: [Char]"z"
Example1 expression pure (pure ":D") :: Maybe [String]Just [":D"]
(<*>) :: f (a -> b) -> f a -> f binfixl 4Sequential application.
A few functors support an implementation of <*> that is more efficient than the default one.
Example
Used in combination with
,(Data.Functor.<$>)can be used to build a record.(<*>)Example1 expression data MyState = MyState {arg1 :: Foo, arg2 :: Bar, arg3 :: Baz}Example3 expressions produceFoo :: Applicative f => f FooproduceBar :: Applicative f => f BarproduceBaz :: Applicative f => f Baz
Example2 expressions mkState :: Applicative f => f MyStatemkState = MyState <$> produceFoo <*> produceBar <*> produceBaz
liftA2 :: (a -> b -> c) -> f a -> f b -> f cLift a binary function to actions.
Some functors support an implementation of liftA2 that is more efficient than the default one. In particular, if fmap is an expensive operation, it is likely better to use liftA2 than to fmap over the structure and then use <*>.
This became a typeclass method in 4.10.0.0. Prior to that, it was a function defined in terms of <*> and fmap.
Example
Example1 expression liftA2 (,) (Just 3) (Just 5)Just (3,5)
Example1 expression liftA2 (+) [1, 2, 3] [4, 5, 6][5,6,7,6,7,8,7,8,9]
(*>) :: f a -> f b -> f binfixl 4Sequence actions, discarding the value of the first argument.
Examples
If used in conjunction with the Applicative instance for Maybe, you can chain Maybe computations, with a possible "early return" in case of Nothing.
Example1 expression Just 2 *> Just 3Just 3
Example1 expression Nothing *> Just 3Nothing
Of course a more interesting use case would be to have effectful computations instead of just returning pure values.
Example4 expressions import Data.Charimport GHC.Internal.Text.ParserCombinators.ReadPlet p = string "my name is " *> munch1 isAlpha <* eofreadP_to_S p "my name is Simon"[("Simon","")]
(<*) :: f a -> f b -> f ainfixl 4Sequence actions, discarding the value of the second argument.
Instances76Applicative, …
Applicative ComplexDefined in base-4.20.2.0 · Data.ComplexApplicative FirstDefined in base-4.20.2.0 · Data.SemigroupApplicative LastDefined in base-4.20.2.0 · Data.SemigroupApplicative MaxDefined in base-4.20.2.0 · Data.SemigroupApplicative MinDefined in base-4.20.2.0 · Data.SemigroupApplicative GetDefined in binary-0.8.9.3 · Data.Binary.Get.InternalApplicative PutMDefined in binary-0.8.9.3 · Data.Binary.PutApplicative PutDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.InternalApplicative SeqDefined in containers-0.7 · Data.Sequence.InternalApplicative TreeDefined in containers-0.7 · Data.TreeApplicative 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 ParserDefined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerApplicative ParserDefined in polyparse-1.13 · Text.ParserCombinators.Poly.ByteStringApplicative ParserDefined in polyparse-1.13 · Text.ParserCombinators.Poly.ByteStringCharApplicative ParserDefined in polyparse-1.13 · Text.ParserCombinators.Poly.TextApplicative PprMDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.PprLibApplicative QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.SyntaxApplicative []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseApplicative ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.ProxyApplicative U1Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsApplicative (SetM s)Defined in containers-0.7 · Data.GraphApplicative (State s)Defined in containers-0.7 · Utils.Containers.Internal.StateApplicative (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 (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LazyApplicative (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.LexApplicative (Parser t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.ParserApplicative (Parser s)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.StateTextApplicative (IParser t)Defined in text-2.1.3 · Data.Text.Internal.ReadMonad 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.ArrowApplicative (Parser s t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.StateLazyApplicative (Parser s t)Defined in polyparse-1.13 · Text.ParserCombinators.Poly.StateParserApplicative 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 m => Applicative (Kleisli m a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.ArrowMonad m => Applicative (StateT s m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.UtilsMonoid 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)).(Monoid a, Monoid b) => Applicative (Tuple3 a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base(Generic1 f, Applicative (Rep1 f)) => Applicative (Generically1 f)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsApplicative (Parser s t e)Defined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerWallaceApplicative ((->) 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(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)))
An infix synonym for fmap.
The name of this operator is an allusion to Prelude.$.
Note the similarities between their types:
($) :: (a -> b) -> a -> b
(<$>) :: Functor f => (a -> b) -> f a -> f bWhereas Prelude.$ 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
Constructors
WrapArrowunwrapArrow :: a b c
Instances8Generic1, 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.Applicativetype 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
Instances9Generic1, 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.Applicativetype 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]
Instances45Generic1, 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.DeepSeqFunctor (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.DeepSeqBounded 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.DeepSeqtype 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]
Instances18Functor, 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.DeepSeqGeneric1 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.DeepSeqtype 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