MyNixOS website logo
Description

A 'FailT' monad transformer that plays well with 'MonadFail'

Fail gracefully when stuck in a MonadFail

>>> runFailT (fail "Failure!?" >> pure "Success!!")
Left "Failure!?"
>>> runFailT (fail "Failure!?" <|> pure "Success!!")
Right "Success!!"
>>> runFailT (pure ["Success!!"] <> fail "Failure!?" <> pure ["At", "Last!"])
Right ["Success!!","At","Last!"]

FailT

This package solves a fairly simple, but very common problem of gracefully converting a monadic computation that uses MonadFail into either a result or a string failure message(s).

Github ActionsCoverallsHackageNightlyLTS
GA-CICoverallsHackageNightlyLTS

Motivation

When we have a function that can fail in a MonadFail, there is no instance in base that would allow us to get the failure message without a runtime exception. It is best to demonstrate the problem with an example.

Here is formatParseM function from the time package, which is designed to parse time:

formatParseM :: MonadFail m => Format t -> String -> m t

We can use it out of the box with various packages like aeson, attoparsec, binary, etc. Here is how it could be used to successfully parse a string with time in the IO monad:

λ> import Data.Time (UTCTime)
λ> import Data.Time.Format.ISO8601 (formatParseM, iso8601Format)
λ> formatParseM iso8601Format "2023-01-08T00:29:00Z" :: IO UTCTime
2023-01-08 00:29:00 UTC

However, when it comes to bad input, there is not a single monad in base or other package that is wired with GHC that has a MonadFail instance, which would allow us to gracefully fail and retrieve the error message. Below are all the instances from base:

λ> formatParseM iso8601Format "Bad time" :: IO UTCTime
*** Exception: user error (no parse of "Bad time")
λ> formatParseM iso8601Format "Bad time" :: Maybe UTCTime
Nothing
λ> formatParseM iso8601Format "Bad time" :: [UTCTime]
[]

Solution

This is where FailT package comes to help:

λ> import Control.Monad.Trans.Fail.String
λ> runFail $ formatParseM iso8601Format "Bad time" :: Either String UTCTime
Left "no parse of \"Bad time\""
λ> runFail $ formatParseM iso8601Format "2023-01-08T00:29:00Z" :: Either String UTCTime
Right 2023-01-08 00:29:00 UTC

Features

Monad transformer

Naturally, as the package name suggests, it provides a FailT monad transformer.

The example above used the Fail type synonym, whcih is restricts the underlying monad to Identity. Below is the example of running FailT with IO:

λ> import Control.Monad.IO.Class
λ> runFailT (liftIO . print . utctDayTime =<< formatParseM iso8601Format "2023-01-08T00:29:00Z")
1740s
Right ()
λ> runFailT (liftIO . print . utctDayTime =<< formatParseM iso8601Format "Bad input")
Left "no parse of \"Bad input\""

Polymorphic failure

Thus far examples only showed using String type for failure messages, but that is not a requirement. This library was designed to be agnostic with respect to failure message type with restriction to IsString type class. Reason for the constraint is because the failure message normally originates with the fail function and is string like by its nature:

fail :: MonadFail m => String -> m ()

The more general implementation is located in import Control.Monad.Trans.Fail, which contains polymorphic implementation that allows the user to choose a more specific type for the failure type e:

runFailT :: (IsString e, Semigroup e, Functor m) => FailT e m a -> m (Either e a)

This package implements convenience modules:

  • Control.Monad.Trans.Fail.String
  • Control.Monad.Trans.Fail.Text

which provide type synonyms and functions with more restricted failure types: String and Text respectfully. Modules were designed to be drop-in replacements of each other.

Convenient instances

There are many type class instances for FailT monad. Such as instances for type classes from mtl package, MonadIO instance, etc. Most of them rely on the instances of the underlying monad by lifting the functionality. However, here are some of the more notable and useful instances that do not require corresponding instances from the underlying monad:

  • MonadFail with Monadic sequencing, which allow to stop the computation upon the first invocation of fail.

    λ> runFailT (fail "Failure!?" >> pure "Success!!")
    Left "Failure!?"
    
  • Alternative, which will continue until the first successful computation is encountered.

    λ> runFailT (fail "Failure!?" <|> pure "Success!!")
    Right "Success!!"
    
  • Semigroup and Monoid, which will not stop until all of the actions are executed. Produced at the end are either all of the failures or the results of all of the successful cases combined with <>

    λ> runFailT (pure ["Success!!"] <> fail "Failure!?" <> pure ["At", "Last!"])
    Right ["Success!!","At","Last!"]
    λ> runFailT mempty :: IO (Either String ())
    Left "No failure reason given"
    
Metadata

Version

0.1.2.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows