HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

ModuleAgda-2.7.0.1Haskell2010

Agda.Utils.Monad

  • 1 class
  • 44 values
  • PackageAgda-2.7.0.1
  • Exports47
  • LanguageHaskell2010
  • LicenceMIT
  • SourceMonad.hs
valueifM :: Monad m => m Bool -> m a -> m a -> m a
#

Monadic if-then-else.

valuebracket_
  1. :: Monad m
  2. => m a

    Acquires resource. Run first.

  3. -> (a -> m ())

    Releases resource. Run last.

  4. -> m b

    Computes result. Run in-between.

  5. -> m b
#

Bracket without failure. Typically used to preserve state.

valuefinally :: MonadError e m => m a -> m () -> m a
#

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

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

Binary bind.

value(<*!>) :: Monad m => m (a -> b) -> m a -> m b
#

Strict ap

valueifNotM :: Monad m => m Bool -> m a -> m a -> m a
#
ifNotM mc = ifM (not $ mc)
valuealtM1 :: Monad m => (a -> m (Either err b)) -> [a] -> m (Either err b)
#

Lazy monadic disjunction with Either truth values. Returns the last error message if all fail.

valuemapM' :: (Foldable t, Applicative m, Monoid b) => (a -> m b) -> t a -> m b
#

Generalized version of traverse_ :: Applicative 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.

valueforM' :: (Foldable t, Applicative m, Monoid b) => t a -> (a -> m b) -> m b
#

Generalized version of for_ :: Applicative m => [a] -> (a -> m ()) -> m ()

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

A monadic version of dropWhileEnd :: (a -> Bool) -> [a] -> m [a]. Effects happen starting at the end of the list until p becomes false.

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

Conditional execution of Applicative expressions. For example,

Examples
when debug (putStrLn "Debugging")

will output the string Debugging if the Boolean value debug is True, and otherwise do nothing.

Example1 expression
putStr "pi:" >> when False (print 3.14159)pi:
valueunless :: Applicative f => Bool -> f () -> f ()
#

The reverse of when.

Examples
Example1 expression
do x <- getLine       unless (x == "hi") (putStrLn "hi!")comingupwithexamplesisdifficulthi!
Example1 expression
unless (pi > exp 1) NothingJust ()
classclass (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) 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

    The default definition is

    mzero = empty
    
  • mplus :: m a -> m a -> m a

    An associative operation. The default definition is

    mplus = (<|>)
    
Instances48MonadPlus, …
value(<$>) :: Functor f => (a -> b) -> f a -> f b
#

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 b

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

Examples

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

Example1 expression
show <$> NothingNothing
Example1 expression
show <$> Just 3Just "3"

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

Example1 expression
show <$> Left 17Left 17
Example1 expression
show <$> Right 17Right "17"

Double each element of a list:

Example1 expression
(*2) <$> [1,2,3][2,4,6]

Apply even to the second element of a pair:

Example1 expression
even <$> (2,2)(2,True)
method(<*>) :: f (a -> b) -> f a -> f b
#

Sequential 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
value(<$!>) :: Monad m => (a -> b) -> m a -> m b
#

Strict version of Data.Functor.<$>.

method(<$) :: 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.

Examples

Perform a computation with Maybe and replace the result with a constant value if it is Just:

Example2 expressions
'a' <$ Just 2Just 'a''a' <$ NothingNothing