HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerelude-1.2.0.0Haskell2010

Relude.Bool.Guard

SPDX-License-Identifier : MIT Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

Monadic boolean combinators.

  • 7 values
  • Packagerelude-1.2.0.0
  • Exports7
  • LanguageHaskell2010
  • LicenceMIT
  • SourceGuard.hs
valueguarded :: Alternative f => (a -> Bool) -> a -> f a
#

Either lifts a value into an alternative context or gives a minimal value depending on a predicate. Works with Alternatives.

Example4 expressions
guarded even 3 :: [Int][]guarded even 2 :: [Int][2]guarded (const True) "hello" :: Maybe StringJust "hello"guarded (const False) "world" :: Maybe StringNothing

You can use this function to implement smart constructors simpler:

newtype HttpHost = HttpHost
    { unHttpHost :: Text
    }

mkHttpHost :: Text -> Maybe HttpHost
mkHttpHost host = HttpHost <$> guarded (not . Text.null) host
valueguardM :: MonadPlus m => m Bool -> m ()
#

Monadic version of guard that help to check that a condition (Bool) holds inside. Works with Monads that are also Alternative.

Example3 expressions
guardM (Just True)Just ()guardM (Just False)NothingguardM NothingNothing

Here some complex but real-life example:

findSomePath :: IO (Maybe FilePath)

somePath :: MaybeT IO FilePath
somePath = do
    path <- MaybeT findSomePath
    guardM $ liftIO $ doesDirectoryExist path
    return path
valueifM :: Monad m => m Bool -> m a -> m a -> m a
#

Monadic version of if-then-else.

Example2 expressions
ifM (pure True) (putTextLn "True text") (putTextLn "False text")True textifM (pure False) (putTextLn "True text") (putTextLn "False text")False text
valueunlessM :: Monad m => m Bool -> m () -> m ()
#

Monadic version of unless. Reverse of whenM. Conditionally don't execute the provided action.

Example2 expressions
unlessM (pure False) $ putTextLn "No text :("No text :(unlessM (pure True) $ putTextLn "Yes text :)"
valuewhenM :: Monad m => m Bool -> m () -> m ()
#

Monadic version of when. Conditionally executes the provided action.

Example5 expressions
whenM (pure False) $ putTextLn "No text :("whenM (pure True)  $ putTextLn "Yes text :)"Yes text :)whenM (Just True) (pure ())Just ()whenM (Just False) (pure ())Just ()whenM Nothing (pure ())Nothing
value(&&^) :: Monad m => m Bool -> m Bool -> m Bool
#

Monadic version of (Data.Bool.&&) operator.

It is lazy by the second argument (similar to (Data.Bool.||)), meaning that if the first argument is False, the function will return False without evaluating the second argument.

Example5 expressions
Just False &&^ Just TrueJust FalseJust True &&^ Just TrueJust TrueJust True &&^ NothingNothingJust False &&^ NothingJust FalseJust False &&^ error "Shouldn't be evaluated"Just False
value(||^) :: Monad m => m Bool -> m Bool -> m Bool
#

Monadic version of (Data.Bool.||) operator.

It is lazy by the second argument (similar to (Data.Bool.||)), meaning that if the first argument is True, the function will return True without evaluating the second argument.

Example5 expressions
Just False ||^ Just TrueJust TrueJust False ||^ Just FalseJust FalseJust False ||^ NothingNothingJust True ||^ NothingJust TrueJust True ||^ error "Shouldn't be evaluated"Just True