Monadic if-then-else.
ModuleAgda-2.7.0.1Haskell2010
Agda.Utils.Monad
- 1 class
- 44 values
- PackageAgda-2.7.0.1
- Exports47
- LanguageHaskell2010
- LicenceMIT
- SourceMonad.hs
Like guard, but raise given error when condition fails.
A monadic version of mapMaybe :: (a -> Maybe b) -> [a] -> [b].
Try a computation, return Nothing if an Error occurs.
bracket_ :: Monad m=> m aAcquires resource. Run first.
-> (a -> m ())Releases resource. Run last.
-> m bComputes result. Run in-between.
-> m b
Bracket without failure. Typically used to preserve state.
Finally for the Error class. Errors in the finally part take
precedence over prior errors.
Output a single value.
A monadic version of dropWhile :: (a -> Bool) -> [a] -> [a].
Binary bind.
Strict ap
Monadic guard.
ifNotM mc = ifM (not $ mc)Lazy monadic conjunction.
Lazy monadic disjunction.
Lazy monadic disjunction with Either truth values.
Returns the last error message if all fail.
Lazy monadic disjunction with accumulation of errors in a monoid. Errors are discarded if we succeed.
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 tthat collects results in right-to-left order (effects still left-to-right). It might be preferable for right associative monoids.
Generalized version of for_ :: Applicative m => [a] -> (a -> m ()) -> m ()
A version of mapMaybeM with a computation for the input list.
The for version of mapMaybeM.
The for version of mapMaybeMM.
A monadic version of .
Effects happen starting at the end of the list until dropWhileEnd :: (a -> Bool) -> [a] -> m [a]p becomes false.
A `monadic' version of @partition :: (a -> Bool) -> [a] -> ([a],[a])
Branch over elements of a monadic Foldable data structure.
Run a command, catch the exception and return it.
Restore state after computation.
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.
putStr "pi:" >> when False (print 3.14159)pi:
The reverse of when.
Examples
do x <- getLine unless (x == "hi") (putStrLn "hi!")comingupwithexamplesisdifficulthi!
unless (pi > exp 1) NothingJust ()
Monads that also support choice and failure.
Instances48MonadPlus, …
MonadPlus NLMDefined in Agda-2.7.0.1 · Agda.TypeChecking.Rewriting.NonLinMatchMonadPlus IResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalMonadPlus ParserDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalMonadPlus ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalMonadPlus GetDefined in binary-0.8.9.3 · Data.Binary.Get.InternalMonadPlus SeqDefined in containers-0.7 · Data.Sequence.InternalMonadPlus DListDefined in dlist-1.0 · Data.DList.InternalMonadPlus STMDefined in ghc-internal-9.1003.0 · GHC.Internal.Conc.SyncMonadPlus MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonadPlus PDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadPlus ReadPDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadPlus ReadPrecDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPrecMonadPlus IODefined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonadPlus ArrayDefined in primitive-0.9.1.0 · Data.Primitive.ArrayMonadPlus SmallArrayDefined in primitive-0.9.1.0 · Data.Primitive.SmallArrayMonadPlus CapabilityDefined in terminfo-0.4.1.7 · System.Console.Terminfo.BaseMonadPlus VectorDefined in vector-0.13.2.0 · Data.VectorMonadPlus VectorDefined in vector-0.13.2.0 · Data.Vector.StrictMonadPlus []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseCombines lists by concatenation, starting from the empty list.
Monad m => MonadPlus (CatchT m)Defined in exceptions-0.10.9 · Control.Monad.Catch.PureMonad m => MonadPlus (MaybeT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.MaybeMonadPlus ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.ProxyMonadPlus U1Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Functor m, Applicative m, Monad m) => MonadPlus (ListT m)Defined in Agda-2.7.0.1 · Agda.Utils.ListT(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.ArrowMonadPlus (bi a) => MonadPlus (Biap bi a)Defined in bifunctors-5.6.2 · Data.Bifunctor.BiapMonadPlus f => MonadPlus (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidMonadPlus f => MonadPlus (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalMonadPlus f => MonadPlus (Rec1 f)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsMonadPlus m => MonadPlus (Kleisli m a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.ArrowMonadPlus m => MonadPlus (IdentityT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.IdentityMonadPlus m => MonadPlus (ReaderT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.ReaderMonadPlus m => MonadPlus (SelectT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.SelectMonadPlus m => MonadPlus (StateT s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.State.LazyMonadPlus m => MonadPlus (StateT s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.State.StrictMonadPlus m => MonadPlus (Reverse m)Defined in transformers-0.6.1.1 · Data.Functor.ReverseDerived instance.
(Functor m, MonadPlus m) => MonadPlus (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.CPS(Monad m, Monoid e) => MonadPlus (ExceptT e m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Except(Monoid w, Functor m, MonadPlus m) => MonadPlus (AccumT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Accum(Monoid w, MonadPlus m) => MonadPlus (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.Lazy(Monoid w, MonadPlus m) => MonadPlus (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.StrictMonadPlus (ParsecT s u m)Defined in parsec-3.1.18.0 · Text.Parsec.Prim(MonadPlus f, MonadPlus g) => MonadPlus (Product f g)Defined in base-4.20.2.0 · Data.Functor.Product(MonadPlus f, MonadPlus g) => MonadPlus (f :*: g)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsMonadPlus f => MonadPlus (M1 i c f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Functor m, MonadPlus m) => MonadPlus (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.CPS(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.Lazy(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.Strict
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)
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.(<*>)
data MyState = MyState {arg1 :: Foo, arg2 :: Bar, arg3 :: Baz}produceFoo :: Applicative f => f FooproduceBar :: Applicative f => f BarproduceBaz :: Applicative f => f Baz
mkState :: Applicative f => f MyStatemkState = MyState <$> produceFoo <*> produceBar <*> produceBaz
Strict version of Data.Functor.<$>.
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