Agda-2.5.2: A dependently typed functional programming language and proof assistant

Safe Haskell | None
Language | Haskell2010

Agda.Utils.Monad

Synopsis

- (==<<) :: Monad m => (a -> b -> m c) -> (m a, m b) -> m c
- when_ :: Monad m => Bool -> m a -> m ()
- unless_ :: Monad m => Bool -> m a -> m ()
- whenM :: Monad m => m Bool -> m a -> m ()
- unlessM :: Monad m => m Bool -> m a -> m ()
- guardM :: (Monad m, MonadPlus m) => m Bool -> m ()
- ifM :: Monad m => m Bool -> m a -> m a -> m a
- ifNotM :: Monad m => m Bool -> m a -> m a -> m a
- and2M :: Monad m => m Bool -> m Bool -> m Bool
- andM :: (Foldable f, Monad m) => f (m Bool) -> m Bool
- allM :: (Functor f, Foldable f, Monad m) => f a -> (a -> m Bool) -> m Bool
- or2M :: Monad m => m Bool -> m Bool -> m Bool
- orM :: (Foldable f, Monad m) => f (m Bool) -> m Bool
- anyM :: (Functor f, Foldable f, Monad m) => f a -> (a -> m Bool) -> m Bool
- altM1 :: Monad m => (a -> m (Either err b)) -> [a] -> m (Either err b)
- mapM' :: (Foldable t, Monad m, Monoid b) => (a -> m b) -> t a -> m b
- forM' :: (Foldable t, Monad m, Monoid b) => t a -> (a -> m b) -> m b
- type Cont r a = (a -> r) -> r
- thread :: (a -> Cont r b) -> [a] -> Cont r [b]
- zipWithM' :: Monad m => (a -> b -> m c) -> [a] -> [b] -> m [c]
- mapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b]
- forMaybeM :: Monad m => [a] -> (a -> m (Maybe b)) -> m [b]
- dropWhileM :: Monad m => (a -> m Bool) -> [a] -> m [a]
- partitionM :: (Functor m, Applicative m) => (a -> m Bool) -> [a] -> m ([a], [a])
- finally :: MonadError e m => m a -> m b -> m a
- tryMaybe :: (MonadError e m, Functor m) => m a -> m (Maybe a)
- bracket_ :: Monad m => m a -> (a -> m c) -> m b -> m b
- localState :: MonadState s m => m a -> m a
- readM :: (Error e, MonadError e m, Read a) => String -> m a
- when :: Applicative f => Bool -> f () -> f ()
- unless :: Applicative f => Bool -> f () -> f ()
- class (Alternative m, Monad m) => MonadPlus m where
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- (<*>) :: Applicative f => forall a b. f (a -> b) -> f a -> f b
- (<$) :: Functor f => forall a b. a -> f b -> f a
- modify' :: MonadState s m => (s -> s) -> m ()

# Documentation

(==<<) :: Monad m => (a -> b -> m c) -> (m a, m b) -> m c #

Binary bind.

when_ :: Monad m => Bool -> m a -> m () #

`when_` is just `Control.Monad.when` with a more general type.

unless_ :: Monad m => Bool -> m a -> m () #

`unless_` is just `Control.Monad.unless` with a more general type.

whenM :: Monad m => m Bool -> m a -> m () #

unlessM :: Monad m => m Bool -> m a -> m () #

guardM :: (Monad m, MonadPlus m) => m Bool -> m () #

Monadic guard.

ifM :: Monad m => m Bool -> m a -> m a -> m a #

Monadic if-then-else.

ifNotM :: Monad m => m Bool -> m a -> m a -> m a #

    ifNotM mc = ifM (not $ mc)

and2M :: Monad m => m Bool -> m Bool -> m Bool #

Lazy monadic conjunction.

andM :: (Foldable f, Monad m) => f (m Bool) -> m Bool #

allM :: (Functor f, Foldable f, Monad m) => f a -> (a -> m Bool) -> m Bool #

or2M :: Monad m => m Bool -> m Bool -> m Bool #

Lazy monadic disjunction.

orM :: (Foldable f, Monad m) => f (m Bool) -> m Bool #

anyM :: (Functor f, Foldable f, Monad m) => f a -> (a -> m Bool) -> m Bool #

altM1 :: Monad m => (a -> m (Either err b)) -> [a] -> m (Either err b) #

Lazy monadic disjunction with `Either` truth values.

mapM' :: (Foldable t, Monad m, Monoid b) => (a -> m b) -> t a -> m b #

Generalized version of `mapM_ :: Monad m => (a -> m ()) -> [a] -> m ()` Executes effects and collects results in left-to-right order. Works best with left-associative monoids.

Note that there is an alternative

    mapM' f t = foldr mappend mempty $ mapM f t

that collects results in right-to-left order (effects still left-to-right). It might be preferable for right associative monoids.

forM' :: (Foldable t, Monad m, Monoid b) => t a -> (a -> m b) -> m b #

Generalized version of `forM_ :: Monad m => [a] -> (a -> m ()) -> m ()`

type Cont r a = (a -> r) -> r #

thread :: (a -> Cont r b) -> [a] -> Cont r [b] #

`mapM` for the continuation monad. Terribly useful.

zipWithM' :: Monad m => (a -> b -> m c) -> [a] -> [b] -> m [c] #

Requires both lists to have the same lengths.

mapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b] #

A monadic version of ``mapMaybe` :: (a -> Maybe b) -> [a] -> [b]`.

forMaybeM :: Monad m => [a] -> (a -> m (Maybe b)) -> m [b] #

The `for` version of `mapMaybeM`.

dropWhileM :: Monad m => (a -> m Bool) -> [a] -> m [a] #

A monadic version of ``dropWhile` :: (a -> Bool) -> [a] -> [a]`.

partitionM :: (Functor m, Applicative m) => (a -> m Bool) -> [a] -> m ([a], [a]) #

A ``monadic'` version of @`partition` :: (a -> Bool) -> [a] -> ([a],[a])

finally :: MonadError e m => m a -> m b -> m a #

Finally for the `Error` class. Errors in the finally part take precedence over prior errors.

tryMaybe :: (MonadError e m, Functor m) => m a -> m (Maybe a) #

Try a computation, return `Nothing` if an `Error` occurs.

bracket_ #

Arguments

:: Monad m |
=> m a | Acquires resource. Run first.
-> (a -> m c) | Releases resource. Run last.
-> m b | Computes result. Run in-between.
-> m b |

Bracket without failure. Typically used to preserve state.

localState :: MonadState s m => m a -> m a #

Restore state after computation.

readM :: (Error e, MonadError e m, Read a) => String -> m a #

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.

unless :: Applicative f => Bool -> f () -> f () #

The reverse of `when`.

class (Alternative m, Monad m) => MonadPlus m where #

Monads that also support choice and failure.

Methods

mzero :: m a #

the identity of `mplus`. It should also satisfy the equations

    mzero >>= f  =  mzero
    v >> mzero   =  mzero

mplus :: m a -> m a -> m a #

an associative operation

Instances

MonadPlus [] |
Methods mzero :: [a] # mplus :: [a] -> [a] -> [a] #
MonadPlus Maybe |
Methods mzero :: Maybe a # mplus :: Maybe a -> Maybe a -> Maybe a #
MonadPlus IO |
Methods mzero :: IO a # mplus :: IO a -> IO a -> IO a #
MonadPlus U1 |
Methods mzero :: U1 a # mplus :: U1 a -> U1 a -> U1 a #
MonadPlus Seq |
Methods mzero :: Seq a # mplus :: Seq a -> Seq a -> Seq a #
MonadPlus Seq |
Methods mzero :: Seq a # mplus :: Seq a -> Seq a -> Seq a #
MonadPlus P |
Methods mzero :: P a # mplus :: P a -> P a -> P a #
MonadPlus Option |
Methods mzero :: Option a # mplus :: Option a -> Option a -> Option a #
MonadPlus STM |
Methods mzero :: STM a # mplus :: STM a -> STM a -> STM a #
MonadPlus ReadPrec |
Methods mzero :: ReadPrec a # mplus :: ReadPrec a -> ReadPrec a -> ReadPrec a #
MonadPlus ReadP |
Methods mzero :: ReadP a # mplus :: ReadP a -> ReadP a -> ReadP a #
MonadPlus Get | Since: 0.7.1.0
Methods mzero :: Get a # mplus :: Get a -> Get a -> Get a #
MonadPlus Seq |
Methods mzero :: Seq a # mplus :: Seq a -> Seq a -> Seq a #
MonadPlus Array |
Methods mzero :: Array a # mplus :: Array a -> Array a -> Array a #
MonadPlus Vector |
Methods mzero :: Vector a # mplus :: Vector a -> Vector a -> Vector a #
MonadPlus f => MonadPlus (Rec1 f) |
Methods mzero :: Rec1 f a # mplus :: Rec1 f a -> Rec1 f a -> Rec1 f a #
(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) |
Methods mzero :: ArrowMonad a a # mplus :: ArrowMonad a a -> ArrowMonad a a -> ArrowMonad a a #
MonadPlus (Proxy *) |
Methods mzero :: Proxy * a # mplus :: Proxy * a -> Proxy * a -> Proxy * a #
Monad m => MonadPlus (MaybeT m) |
Methods mzero :: MaybeT m a # mplus :: MaybeT m a -> MaybeT m a -> MaybeT m a #
MonadPlus (Partial r) |
Methods mzero :: Partial r a # mplus :: Partial r a -> Partial r a -> Partial r a #
Monad m => MonadPlus (ListT m) |
Methods mzero :: ListT m a # mplus :: ListT m a -> ListT m a -> ListT m a #
MonadPlus (ReadP t) # |
Methods mzero :: ReadP t a # mplus :: ReadP t a -> ReadP t a -> ReadP t a #
(Functor m, Applicative m, Monad m) => MonadPlus (ListT m) # |
Methods mzero :: ListT m a # mplus :: ListT m a -> ListT m a -> ListT m a #
(MonadPlus f, MonadPlus g) => MonadPlus ((:*:) f g) |
Methods mzero :: (f :*: g) a # mplus :: (f :*: g) a -> (f :*: g) a -> (f :*: g) a #
MonadPlus f => MonadPlus (Alt * f) |
Methods mzero :: Alt * f a # mplus :: Alt * f a -> Alt * f a -> Alt * f a #
(Monad m, Error e) => MonadPlus (ErrorT e m) |
Methods mzero :: ErrorT e m a # mplus :: ErrorT e m a -> ErrorT e m a -> ErrorT e m a #
(Monad m, Monoid e) => MonadPlus (ExceptT e m) |
Methods mzero :: ExceptT e m a # mplus :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a #
MonadPlus m => MonadPlus (StateT s m) |
Methods mzero :: StateT s m a # mplus :: StateT s m a -> StateT s m a -> StateT s m a #
MonadPlus m => MonadPlus (StateT s m) |
Methods mzero :: StateT s m a # mplus :: StateT s m a -> StateT s m a -> StateT s m a #
(Monoid w, MonadPlus m) => MonadPlus (WriterT w m) |
Methods mzero :: WriterT w m a # mplus :: WriterT w m a -> WriterT w m a -> WriterT w m a #
(Monoid w, MonadPlus m) => MonadPlus (WriterT w m) |
Methods mzero :: WriterT w m a # mplus :: WriterT w m a -> WriterT w m a -> WriterT w m a #
MonadPlus m => MonadPlus (IdentityT * m) |
Methods mzero :: IdentityT * m a # mplus :: IdentityT * m a -> IdentityT * m a -> IdentityT * m a #
MonadPlus f => MonadPlus (M1 i c f) |
Methods mzero :: M1 i c f a # mplus :: M1 i c f a -> M1 i c f a -> M1 i c f a #
(MonadPlus f, MonadPlus g) => MonadPlus (Product * f g) |
Methods mzero :: Product * f g a # mplus :: Product * f g a -> Product * f g a -> Product * f g a #
MonadPlus m => MonadPlus (ReaderT * r m) |
Methods mzero :: ReaderT * r m a # mplus :: ReaderT * r m a -> ReaderT * r m a -> ReaderT * r m a #
MonadPlus (ParsecT s u m) |
Methods mzero :: ParsecT s u m a # mplus :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a #
(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) |
Methods mzero :: RWST r w s m a # mplus :: RWST r w s m a -> RWST r w s m a -> RWST r w s m a #
(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) |
Methods mzero :: RWST r w s m a # mplus :: RWST r w s m a -> RWST r w s m a -> RWST r w s m a #

(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #

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 b

Whereas `$` is function application, `<$>` is function application lifted over a `Functor`.

#### Examples

Convert from a ``Maybe` `Int`` to a ``Maybe` `String`` using `show`:

    >>> show <$> Nothing
    Nothing
    >>> show <$> Just 3
    Just "3"

Convert from an ``Either` `Int` `Int`` to an ``Either` `Int`` `String` using `show`:

    >>> show <$> Left 17
    Left 17
    >>> show <$> Right 17
    Right "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)

(<*>) :: Applicative f => forall a b. f (a -> b) -> f a -> f b #

Sequential application.

(<$) :: Functor f => forall a b. a -> f b -> f a #

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.

modify' :: MonadState s m => (s -> s) -> m () #

A variant of `modify` in which the computation is strict in the new state.