-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Haskell98 partial maps and filters over MonadPlus. -- -- Filtering and folding over arbitrary MonadPlus instances. This -- package generalizes many common stream operations such as -- filter, catMaybes etc. @package monadplus @version 1.4.3 -- | Partial maps and filters over Alternative instances. -- -- This is considerably weaker than MonadPlus, as we have no -- possibility of removing intermediate structure, as in -- mcatMaybes. module Control.Applicative.Alternative -- | The sum of a collection of actions, generalizing concat. -- -- asum is just like msum, but generalised to -- Alternative. -- --
-- >>> asum [Just "Hello", Nothing, Just "World"] -- Just "Hello" --asum :: (Foldable t, Alternative f) => t (f a) -> f a -- | Fold a value into an arbitrary MonadPlus type. -- -- This function generalizes the toList function. afold :: (Alternative f, Foldable t) => t a -> f a -- | This function generalizes the listToMaybe function. afromList :: Alternative f => [a] -> f a -- | Translate maybe to an arbitrary Alternative type. -- -- This function generalizes the maybeToList function. afromMaybe :: Alternative f => Maybe a -> f a -- | Partial maps and filters over MonadPlus instances. The basic -- idea here is that the monad interface together with the monoidal -- structure of MonadPlus is enough to implement partial maps and -- filters (i.e. mmapMaybe and mfilter). -- -- This is especially useful for sequential structures such as event -- lists, tracks etc. -- -- Inspired by the following blog post: -- -- module Control.Monad.Plus -- | Conditional failure of Alternative computations. Defined by -- --
-- guard True = pure () -- guard False = empty ---- --
-- >>> 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) --guard :: Alternative f => Bool -> f () -- | The join function is the conventional monad join operator. It -- is used to remove one level of monadic structure, projecting its bound -- argument into the outer level. -- -- 'join bss' can be understood as the do -- expression -- --
-- do bs <- bss -- bs ---- --
-- atomically :: STM a -> IO a ---- -- is used to run STM transactions atomically. So, by specializing -- the types of atomically and join to -- --
-- atomically :: STM (IO b) -> IO (IO b) -- join :: IO (IO b) -> IO b ---- -- we can compose them as -- --
-- join . atomically :: STM (IO b) -> IO b ---- -- to run an STM transaction and the IO action it returns. join :: Monad m => m (m a) -> m a -- | The Monad class defines the basic operations over a -- monad, a concept from a branch of mathematics known as -- category theory. From the perspective of a Haskell programmer, -- however, it is best to think of a monad as an abstract datatype -- of actions. Haskell's do expressions provide a convenient -- syntax for writing monadic expressions. -- -- Instances of Monad should satisfy the following: -- --
-- do a <- as -- bs a --(>>=) :: Monad m => m a -> (a -> m b) -> m b -- | Sequentially compose two actions, discarding any value produced by the -- first, like sequencing operators (such as the semicolon) in imperative -- languages. -- -- 'as >> bs' can be understood as the do -- expression -- --
-- do as -- bs --(>>) :: Monad m => m a -> m b -> m b -- | Inject a value into the monadic type. return :: Monad m => a -> m a infixl 1 >> infixl 1 >>= -- | A type f is a Functor if it provides a function fmap -- which, given any types a and b lets you apply any -- function from (a -> b) to turn an f a into an -- f b, preserving the structure of f. Furthermore -- f needs to adhere to the following: -- -- -- -- Note, that the second law follows from the free theorem of the type -- fmap and the first law, so you need only check that the former -- condition holds. class Functor (f :: Type -> Type) -- | fmap is used to apply a function of type (a -> b) -- to a value of type f a, where f is a functor, to produce a -- value of type f b. Note that for any type constructor with -- more than one parameter (e.g., Either), only the last type -- parameter can be modified with fmap (e.g., b in -- `Either a b`). -- -- Some type constructors with two parameters or more have a -- Bifunctor instance that allows both the last and the -- penultimate parameters to be mapped over. -- --
-- >>> fmap show Nothing -- Nothing -- -- >>> fmap show (Just 3) -- Just "3" ---- -- Convert from an Either Int Int to an Either Int -- String using show: -- --
-- >>> fmap show (Left 17) -- Left 17 -- -- >>> fmap show (Right 17) -- Right "17" ---- -- Double each element of a list: -- --
-- >>> fmap (*2) [1,2,3] -- [2,4,6] ---- -- Apply even to the second element of a pair: -- --
-- >>> fmap even (2,2) -- (2,True) ---- -- It may seem surprising that the function is only applied to the last -- element of the tuple compared to the list example above which applies -- it to every element in the list. To understand, remember that tuples -- are type constructors with multiple type parameters: a tuple of 3 -- elements (a,b,c) can also be written (,,) a b c and -- its Functor instance is defined for Functor ((,,) a -- b) (i.e., only the third parameter is free to be mapped over with -- fmap). -- -- It explains why fmap can be used with tuples containing -- values of different types as in the following example: -- --
-- >>> fmap even ("hello", 1.0, 4)
-- ("hello",1.0,True)
--
fmap :: Functor f => (a -> b) -> f a -> f b
-- | 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.
(<$) :: Functor f => a -> f b -> f a
infixl 4 <$
-- | When a value is bound in do-notation, the pattern on the left
-- hand side of <- might not match. In this case, this class
-- provides a function to recover.
--
-- A Monad without a MonadFail instance may only be used in
-- conjunction with pattern that always match, such as newtypes, tuples,
-- data types with only a single data constructor, and irrefutable
-- patterns (~pat).
--
-- Instances of MonadFail should satisfy the following law:
-- fail s should be a left zero for >>=,
--
-- -- fail s >>= f = fail s ---- -- If your Monad is also MonadPlus, a popular definition is -- --
-- fail _ = mzero --class Monad m => MonadFail (m :: Type -> Type) fail :: MonadFail m => String -> m a -- | Map each element of a structure to a monadic action, evaluate these -- actions from left to right, and collect the results. For a version -- that ignores the results see mapM_. -- --
-- >>> sequence $ Right [1,2,3,4] -- [Right 1,Right 2,Right 3,Right 4] ---- --
-- >>> sequence $ [Right 1,Right 2,Right 3,Right 4] -- Right [1,2,3,4] ---- -- The following examples demonstrate short circuit behavior for -- sequence. -- --
-- >>> sequence $ Left [1,2,3,4] -- Left [1,2,3,4] ---- --
-- >>> sequence $ [Left 0, Right 1,Right 2,Right 3,Right 4] -- Left 0 --sequence :: (Traversable t, Monad m) => t (m a) -> m (t a) -- | zipWithM_ is the extension of zipWithM which ignores the -- final result. zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m () -- | The zipWithM function generalizes zipWith to arbitrary -- applicative functors. zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] -- | The reverse of when. unless :: Applicative f => Bool -> f () -> f () -- | Like replicateM, but discards the result. -- --
-- >>> replicateM_ 3 (putStrLn "a") -- a -- a -- a --replicateM_ :: Applicative m => Int -> m a -> m () -- | replicateM n act performs the action act -- n times, and then returns the list of results: -- --
-- >>> import Control.Monad.State -- -- >>> runState (replicateM 3 $ state $ \s -> (s, s + 1)) 1 -- ([1,2,3],4) --replicateM :: Applicative m => Int -> m a -> m [a] -- | Direct MonadPlus equivalent of filter. -- --
-- filter = ( mfilter :: (a -> Bool) -> [a] -> [a] ) ---- -- An example using mfilter with the Maybe monad: -- --
-- >>> mfilter odd (Just 1) -- Just 1 -- -- >>> mfilter odd (Just 2) -- Nothing --mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a -- | The mapAndUnzipM function maps its first argument over a list, -- returning the result as a pair of lists. This function is mainly used -- with complicated data structures or a state monad. mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c]) -- | Repeat an action indefinitely. -- --
-- echoServer :: Socket -> IO () -- echoServer socket = forever $ do -- client <- accept socket -- forkFinally (echo client) (\_ -> hClose client) -- where -- echo :: Handle -> IO () -- echo client = forever $ -- hGetLine client >>= hPutStrLn client ---- -- Note that "forever" isn't necessarily non-terminating. If the action -- is in a MonadPlus and short-circuits after some number -- of iterations. then forever actually returns -- mzero, effectively short-circuiting its caller. forever :: Applicative f => f a -> f b -- | Like foldM, but discards the result. foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m () -- | The foldM function is analogous to foldl, except that -- its result is encapsulated in a monad. Note that foldM works -- from left-to-right over the list arguments. This could be an issue -- where (>>) and the `folded function' are not -- commutative. -- --
-- foldM f a1 [x1, x2, ..., xm] -- -- == -- -- do -- a2 <- f a1 x1 -- a3 <- f a2 x2 -- ... -- f am xm ---- -- If right-to-left evaluation is required, the input list should be -- reversed. -- -- Note: foldM is the same as foldlM foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b -- | This generalizes the list-based filter function. filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] -- | Left-to-right composition of Kleisli arrows. -- -- '(bs >=> cs) a' can be understood as the -- do expression -- --
-- do b <- bs a -- cs b --(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1 >=> -- | Right-to-left composition of Kleisli arrows. -- (>=>), with the arguments flipped. -- -- Note how this operator resembles function composition -- (.): -- --
-- (.) :: (b -> c) -> (a -> b) -> a -> c -- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c --(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c infixr 1 <=< -- | Strict version of <$>. (<$!>) :: Monad m => (a -> b) -> m a -> m b infixl 4 <$!> -- | forM is mapM with its arguments flipped. For a version -- that ignores the results see forM_. forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b) -- | Evaluate each monadic action in the structure from left to right, and -- ignore the results. For a version that doesn't ignore the results see -- sequence. -- -- sequence_ is just like sequenceA_, but specialised to -- monadic actions. sequence_ :: (Foldable t, Monad m) => t (m a) -> m () -- | Map each element of a structure to a monadic action, evaluate these -- actions from left to right, and ignore the results. For a version that -- doesn't ignore the results see mapM. -- -- mapM_ is just like traverse_, but specialised to monadic -- actions. mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m () -- | forM_ is mapM_ with its arguments flipped. For a version -- that doesn't ignore the results see forM. -- -- forM_ is just like for_, but specialised to monadic -- actions. forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m () -- | void value discards or ignores the result of -- evaluation, such as the return value of an IO action. -- --
-- >>> void Nothing -- Nothing -- -- >>> void (Just 3) -- Just () ---- -- Replace the contents of an Either Int -- Int with unit, resulting in an Either -- Int (): -- --
-- >>> void (Left 8675309) -- Left 8675309 -- -- >>> void (Right 8675309) -- Right () ---- -- Replace every element of a list with unit: -- --
-- >>> void [1,2,3] -- [(),(),()] ---- -- Replace the second element of a pair with unit: -- --
-- >>> void (1,2) -- (1,()) ---- -- Discard the result of an IO action: -- --
-- >>> mapM print [1,2] -- 1 -- 2 -- [(),()] -- -- >>> void $ mapM print [1,2] -- 1 -- 2 --void :: Functor f => f a -> f () -- | Monads that also support choice and failure. class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) -- | The identity of mplus. It should also satisfy the equations -- --
-- mzero >>= f = mzero -- v >> mzero = mzero ---- -- The default definition is -- --
-- mzero = empty --mzero :: MonadPlus m => m a -- | An associative operation. The default definition is -- --
-- mplus = (<|>) --mplus :: MonadPlus m => m a -> m a -> m a -- | 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. when :: Applicative f => Bool -> f () -> f () -- | Promote a function to a monad, scanning the monadic arguments from -- left to right (cf. liftM2). liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r -- | Promote a function to a monad, scanning the monadic arguments from -- left to right (cf. liftM2). liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r -- | Promote a function to a monad, scanning the monadic arguments from -- left to right (cf. liftM2). liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r -- | Promote a function to a monad, scanning the monadic arguments from -- left to right. For example, -- --
-- liftM2 (+) [0,1] [0,2] = [0,2,1,3] -- liftM2 (+) (Just 1) Nothing = Nothing --liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r -- | Promote a function to a monad. liftM :: Monad m => (a1 -> r) -> m a1 -> m r -- | In many situations, the liftM operations can be replaced by -- uses of ap, which promotes function application. -- --
-- return f `ap` x1 `ap` ... `ap` xn ---- -- is equivalent to -- --
-- liftMn f x1 x2 ... xn --ap :: Monad m => m (a -> b) -> m a -> m b -- | Same as >>=, but with the arguments interchanged. (=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 =<< -- | The sum of a collection of actions, generalizing concat. -- -- msum is just like asum, but specialised to -- MonadPlus. msum :: (Foldable t, MonadPlus m) => t (m a) -> m a -- | This generalizes the list-based concat function. msum' :: (MonadPlus m, Foldable t) => t (m a) -> m a -- | Fold a value into an arbitrary MonadPlus type. -- -- This function generalizes the toList function. mfold :: (MonadPlus m, Foldable t) => t a -> m a -- | Translate a list to an arbitrary MonadPlus type. -- -- This function generalizes the listToMaybe function. mfromList :: MonadPlus m => [a] -> m a -- | Translate maybe to an arbitrary MonadPlus type. -- -- This function generalizes the maybeToList function. mfromMaybe :: MonadPlus m => Maybe a -> m a -- | Convert a partial function to a function returning an arbitrary -- MonadPlus type. mreturn :: MonadPlus m => (a -> Maybe b) -> a -> m b -- | The partition function takes a predicate a list and returns the -- pair of lists of elements which do and do not satisfy the predicate, -- respectively; i.e., -- --
-- partition p xs == (filter p xs, filter (not . p) xs) ---- -- This function generalizes the partition function. mpartition :: MonadPlus m => (a -> Bool) -> m a -> (m a, m a) -- | Join list elements together. -- -- This function generalizes the catMaybes function. mscatter :: MonadPlus m => m [b] -> m b -- | Join foldable elements together. -- -- This function generalizes the catMaybes function. mscatter' :: (MonadPlus m, Foldable t) => m (t b) -> m b -- | Pass through Just elements. -- -- This function generalizes the catMaybes function. mcatMaybes :: MonadPlus m => m (Maybe a) -> m a -- | Pass through Left elements. -- -- This function generalizes the lefts function. mlefts :: MonadPlus m => m (Either a b) -> m a -- | Pass through Right elements. -- -- This function generalizes the rights function. mrights :: MonadPlus m => m (Either a b) -> m b -- | Separate Left and Right elements. -- -- This function generalizes the partitionEithers function. mpartitionEithers :: MonadPlus m => m (Either a b) -> (m a, m b) -- | Modify or discard a value. -- -- This function generalizes the mapMaybe function. mmapMaybe :: MonadPlus m => (a -> Maybe b) -> m a -> m b -- | Modify, discard or spawn values. -- -- This function generalizes the concatMap function. mconcatMap :: MonadPlus m => (a -> [b]) -> m a -> m b -- | Modify, discard or spawn values. -- -- This function generalizes the concatMap function. mconcatMap' :: (MonadPlus m, Foldable t) => (a -> t b) -> m a -> m b -- | Wrapper for partial functions with MonadPlus instance. newtype Partial a b Partial :: (a -> Maybe b) -> Partial a b [getPartial] :: Partial a b -> a -> Maybe b -- | Convert a predicate to a partial function. partial :: (a -> Bool) -> a -> Maybe a -- | Convert a partial function to a predicate. predicate :: (a -> Maybe a) -> a -> Bool -- | Convert a total function to a partial function. always :: (a -> b) -> a -> Maybe b -- | Make a partial function that always rejects its input. never :: a -> Maybe c instance GHC.Base.Functor (Control.Monad.Plus.Partial r) instance GHC.Base.Monad (Control.Monad.Plus.Partial r) instance GHC.Base.MonadPlus (Control.Monad.Plus.Partial r) instance GHC.Base.Applicative (Control.Monad.Plus.Partial r) instance GHC.Base.Alternative (Control.Monad.Plus.Partial r) instance Control.Category.Category Control.Monad.Plus.Partial instance GHC.Base.Semigroup (Control.Monad.Plus.Partial a b) instance GHC.Base.Monoid (Control.Monad.Plus.Partial a b)