| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Applicative
Synopsis
- class Functor f => Applicative (f :: * -> *) where
- between :: Applicative m => m open -> m close -> m a -> m a
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- forever :: Applicative f => f a -> f b
- liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
- replicateM :: Applicative m => Int -> m a -> m [a]
- replicateM_ :: Applicative m => Int -> m a -> m ()
- unless :: Applicative f => Bool -> f () -> f ()
- when :: Applicative f => Bool -> f () -> f ()
- zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c]
- zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m ()
- class Applicative f => Alternative (f :: * -> *) where
- endBy :: Alternative m => m a -> m sep -> m [a]
- endBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a)
- guard :: Alternative f => Bool -> f ()
- manyTill :: Alternative m => m a -> m end -> m [a]
- optional :: (Alt f, Applicative f) => f a -> f (Maybe a)
- sepBy :: Alternative m => m a -> m sep -> m [a]
- sepEndBy :: Alternative m => m a -> m sep -> m [a]
- sepBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a)
- sepEndBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a)
- skipMany :: Alternative m => m a -> m ()
- skipManyTill :: Alternative m => m a -> m end -> m end
- skipSome :: Alternative m => m a -> m ()
- skipSomeTill :: Alternative m => m a -> m end -> m end
- some1 :: Alternative f => f a -> f (NonEmpty a)
- someTill :: Alternative m => m a -> m end -> m (NonEmpty a)
- newtype Alt (f :: k -> *) (a :: k) :: forall k. (k -> *) -> k -> * = Alt {
- getAlt :: f a
- data Ap (f :: * -> *) a where
- runAp :: Applicative g => (forall x. f x -> g x) -> Ap f a -> g a
- runAp_ :: Monoid m => (forall a. f a -> m) -> Ap f b -> m
- liftAp :: f a -> Ap f a
- iterAp :: Functor g => (g a -> a) -> Ap g a -> a
- hoistAp :: (forall a. f a -> g a) -> Ap f b -> Ap g b
- retractAp :: Applicative f => Ap f a -> f a
Applicative
class Functor f => Applicative (f :: * -> *) 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.
(<*>) :: 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.
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 <*>.
(*>) :: f a -> f b -> f b infixl 4 #
Sequence actions, discarding the value of the first argument.
(<*) :: f a -> f b -> f a infixl 4 #
Sequence actions, discarding the value of the second argument.
Instances
between :: Applicative m => m open -> m close -> m a -> m a #
parses between open close popen, followed by p and close.
Returns the value returned by p.
braces = between (symbol "{") (symbol "}")filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] #
This generalizes the list-based filter function.
forever :: Applicative f => f a -> f b #
repeats the action infinitely.forever act
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d #
Lift a ternary function to actions.
replicateM :: Applicative m => Int -> m a -> m [a] #
performs the action replicateM n actn times,
gathering the results.
replicateM_ :: Applicative m => Int -> m a -> m () #
Like replicateM, but discards the result.
unless :: Applicative f => Bool -> f () -> f () #
The reverse of when.
when :: Applicative f => Bool -> f () -> f () #
Conditional execution of Applicative expressions. For example,
when debug (putStrLn "Debugging")
will output the string Debugging if the Boolean value debug
is True, and otherwise do nothing.
zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] #
zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m () #
Alternative
class Applicative f => Alternative (f :: * -> *) where #
A monoid on applicative functors.
If defined, some and many should be the least solutions
of the equations:
Methods
The identity of <|>
(<|>) :: f a -> f a -> f a infixl 3 #
An associative binary operation
Zero or more.
Instances
endBy :: Alternative m => m a -> m sep -> m [a] #
parses zero or more occurrences of endBy p sepp, separated and
ended by sep. Returns a list of values returned by p.
cStatements = cStatement `endBy` semicolon
endBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a) #
parses one or more occurrences of endBy1 p sepp, separated and
ended by sep. Returns a non-empty list of values returned by p.
guard :: Alternative f => Bool -> f () #
Conditional failure of Alternative computations. Defined by
guard True =pure() guard False =empty
Examples
Common uses of guard include conditionally signaling an error in
an error monad and conditionally rejecting the current choice in an
Alternative-based parser.
As an example of signaling an error in the error monad Maybe,
consider a safe division function safeDiv x y that returns
Nothing when the denominator y is zero and otherwise. For example:Just (x `div`
y)
>>> safeDiv 4 0 Nothing >>> safeDiv 4 2 Just 2
A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y | y /= 0 = Just (x `div` y)
| otherwise = Nothing
A definition of safeDiv using guard and Monad do-notation:
safeDiv :: Int -> Int -> Maybe Int safeDiv x y = do guard (y /= 0) return (x `div` y)
manyTill :: Alternative m => m a -> m end -> m [a] #
applies parser manyTill p endp zero or more times until parser
end succeeds. Returns the list of values returned by p.
See also: skipMany, skipManyTill.
optional :: (Alt f, Applicative f) => f a -> f (Maybe a) #
One or none.
sepBy :: Alternative m => m a -> m sep -> m [a] #
parses zero or more occurrences of sepBy p sepp, separated by
sep. Returns a list of values returned by p.
commaSep p = p `sepBy` comma
sepEndBy :: Alternative m => m a -> m sep -> m [a] #
parses zero or more occurrences of sepEndBy p sepp, separated
and optionally ended by sep. Returns a list of values returned by p.
sepBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a) #
parses one or more occurrences of sepBy1 p sepp, separated by
sep. Returns a non-empty list of values returned by p.
sepEndBy1 :: Alternative m => m a -> m sep -> m (NonEmpty a) #
parses one or more occurrences of sepEndBy1 p sepp, separated
and optionally ended by sep. Returns a non-empty list of values returned by
p.
skipMany :: Alternative m => m a -> m () #
applies the parser skipMany pp zero or more times, skipping
its result.
See also: manyTill, skipManyTill.
skipManyTill :: Alternative m => m a -> m end -> m end #
applies the parser skipManyTill p endp zero or more times
skipping results until parser end succeeds. Result parsed by end is
then returned.
skipSome :: Alternative m => m a -> m () #
applies the parser skipSome pp one or more times, skipping its
result.
See also: someTill, skipSomeTill.
skipSomeTill :: Alternative m => m a -> m end -> m end #
applies the parser skipSomeTill p endp one or more times
skipping results until parser end succeeds. Result parsed by end is
then returned.
some1 :: Alternative f => f a -> f (NonEmpty a) #
sequences some1 xx one or more times.
someTill :: Alternative m => m a -> m end -> m (NonEmpty a) #
works similarly to someTill p end, but manyTill p endp
should succeed at least once.
See also: skipSome, skipSomeTill.
Newtypes
newtype Alt (f :: k -> *) (a :: k) :: forall k. (k -> *) -> k -> * #
Monoid under <|>.
Since: base-4.8.0.0
Instances
| Generic1 (Alt f :: k -> *) | |
| Monad f => Monad (Alt f) | |
| Functor f => Functor (Alt f) | |
| MonadFix f => MonadFix (Alt f) | Since: base-4.8.0.0 |
Defined in Control.Monad.Fix | |
| Applicative f => Applicative (Alt f) | |
| Alternative f => Alternative (Alt f) | |
| Contravariant f => Contravariant (Alt f) | |
| MonadPlus f => MonadPlus (Alt f) | |
| MonadZip f => MonadZip (Alt f) | Since: base-4.8.0.0 |
| Divisible f => Divisible (Alt f) | |
| Decidable f => Decidable (Alt f) | |
| Apply f => Apply (Alt f) | |
| Traversable1 f => Traversable1 (Alt f) | |
| Foldable1 f => Foldable1 (Alt f) | |
| Bind f => Bind (Alt f) | |
| Extend f => Extend (Alt f) | |
| Enum (f a) => Enum (Alt f a) | |
| Eq (f a) => Eq (Alt f a) | |
| (Data (f a), Data a, Typeable f) => Data (Alt f a) | Since: base-4.8.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Alt f a -> c (Alt f a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Alt f a) # toConstr :: Alt f a -> Constr # dataTypeOf :: Alt f a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Alt f a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Alt f a)) # gmapT :: (forall b. Data b => b -> b) -> Alt f a -> Alt f a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Alt f a -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Alt f a -> r # gmapQ :: (forall d. Data d => d -> u) -> Alt f a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Alt f a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Alt f a -> m (Alt f a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Alt f a -> m (Alt f a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Alt f a -> m (Alt f a) # | |
| Num (f a) => Num (Alt f a) | |
| Ord (f a) => Ord (Alt f a) | |
Defined in Data.Semigroup.Internal | |
| Read (f a) => Read (Alt f a) | |
| Show (f a) => Show (Alt f a) | |
| Generic (Alt f a) | |
| Alternative f => Semigroup (Alt f a) | Since: base-4.9.0.0 |
| Alternative f => Monoid (Alt f a) | Since: base-4.8.0.0 |
| Wrapped (Alt f a) | |
| Serialise (f a) => Serialise (Alt f a) | Since: serialise-0.2.0.0 |
| t ~ Alt g b => Rewrapped (Alt f a) t | |
Defined in Control.Lens.Wrapped | |
| type Rep1 (Alt f :: k -> *) | |
Defined in Data.Semigroup.Internal | |
| type Rep (Alt f a) | |
Defined in Data.Semigroup.Internal | |
| type Unwrapped (Alt f a) | |
Defined in Control.Lens.Wrapped | |
Free applicative
data Ap (f :: * -> *) a where #
The free Applicative for a Functor f.
runAp :: Applicative g => (forall x. f x -> g x) -> Ap f a -> g a #
Given a natural transformation from f to g, this gives a canonical monoidal natural transformation from to Ap fg.
runAp t == retractApp . hoistApp t
runAp_ :: Monoid m => (forall a. f a -> m) -> Ap f b -> m #
Perform a monoidal analysis over free applicative value.
Example:
count :: Ap f a -> Int count = getSum . runAp_ (\_ -> Sum 1)
iterAp :: Functor g => (g a -> a) -> Ap g a -> a #
Tear down a free Applicative using iteration.