HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

ModuleFailT-0.1.2.0Haskell2010

Control.Monad.Trans.Fail

  • 3 types
  • 20 values
  • PackageFailT-0.1.2.0
  • Exports23
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceFail.hs

Fail

6 declarations
valuerunFail :: (IsString e, Semigroup e) => Fail e a -> Either e a
#

Unwrap the pure Fail monad and reveal the underlying result of monadic computation.

Example4 expressions
runFail (fail "Something went wrong") :: Either String ()Left "Something went wrong"runFail (failT "Something went wrong" >> pure ())Left "Something went wrong"import Control.ApplicativerunFail (failT "Something could have gone wrong" <|> pure ())Right ()

All errors accrued during the monadic computation will be combined using the Semigroup instance and delimited by a comma:

Example1 expression
runFail (fail "One thing went wrong" <|> fail "Another thing went wrong") :: Either String ()Left "One thing went wrong, Another thing went wrong"

Failing with one of instances functions mempty or empty will yield a no-reason error report:

Example1 expression
runFail mempty :: Either String ()Left "No failure reason given"
valuerunFailLast :: IsString e => Fail e a -> Either e a
#

This is a variant of runFailAgg where only the error reported for the very last failed computation will be produced and others discarded. This is useful when it is not relevant to retain information about all the attempts and only the last one matters, eg. parsing with backtracking.

valuerunFailAgg :: Fail e a -> Either [e] a
#

Convert a Fail monad computation in an Either, where the Left will contain all failures in the same order they where received, or Right upon a successful computation.

Example2 expressions
runFailAgg (fail "One bad thing" <|> fail "Another bad thing") :: Either [String] ()Left ["One bad thing","Another bad thing"]runFailAgg (fail "A bad thing" <|> pure "A good thing") :: Either [String] StringRight "A good thing"
valueerrorFail :: (Show e, HasCallStack) => Fail e a -> a
#

Throw an error if there was a failure, otherwise return the result of computation. Use throwFailT in case you'd like to handle an actual exception in some other underlying monad.

Example2 expressions
errorFail (fail "This didn't work" :: Fail String ())*** Exception: "This didn't work"CallStack (from HasCallStack):...errorFail (fail "This didn't work" <|> pure "That Worked" :: Fail String String)"That Worked"
valueerrorFailWithoutStackTrace :: Show e => Fail e a -> a
#

Same as errorFail, but without the stack trace:

Example2 expressions
errorFailWithoutStackTrace (fail "This didn't work" :: Fail String ())*** Exception: "This didn't work"errorFailWithoutStackTrace (fail "This didn't work" <|> pure "That Worked" :: Fail String String)"That Worked"

FailT

14 declarations
newtypenewtype FailT e (m :: Type -> Type) a
#

Fail monad transformer that plays well with MonadFail type class.

Constructors

Instances31MonadRWS, MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, …
valuefailManyT :: Applicative m => [e] -> FailT e m a
#

Similar to failT, but accepts a list of failures.

Property
runFailAgg (foldMap failT (xs :: [String])) == runFailAgg (failManyT xs)
valuerunFailT
  1. :: (IsString e, Semigroup e, Functor m)
  2. => FailT e m a
  3. -> m (Either e a)
#

Similar to runFail, except underlying monad is not restricted to Identity.

Unwrap the FailT monad transformer and produce an action that can be executed in the underlying monad and, which will produce either a comma delimited error message upon a failure or the result otherwise.

Example1 expression
runFailT (failT "Could have failed" <|> liftIO (putStrLn "Nothing went wrong"))Nothing went wrongRight ()
valuehoistFailT :: (forall a. m a -> n a) -> FailT e m b -> FailT e n b
#

Change the underlying monad with the hoisting function.

valuemapErrorsFailT :: Functor m => ([e] -> [e']) -> FailT e m a -> FailT e' m a
#

Map a function over the aggregation of errors in the FailT monad. Could be used for example for clearing our all of the aggregated error messages:

Example1 expression
runFail (mapErrorsFailT (const []) $ failT "Something went wrong") :: Either String ()Left "No failure reason given"

Helpers

3 declarations