MyNixOS website logo
Description

A 'Failable' error monad class to unify failure across monads that can fail.

This library contains a Failable error monad class to unify failure across monads and transformers most commonly used to implement pipelines that can fail and does so in a simple nonsense way by providing the means of signaling a computation "failure" while striving to keep the failure behaviour consistent with the actual definition of the monad/transformer. Please refer to the README file for a more elaborate description and some examples.

Control.Monad.Failable

Yet another "error" handling monad (class)

This library provides a 'Failable' error monad class to unify failure across monads and transformers most commonly used to implement pipelines that can fail.

But.. don't we have 'MonadFail', 'MonadThrow', 'MonadError',.. and the true haskeller should be using 'Alternative' anyway!

I am sure a lot of ink has been spilled in forums and around water coolers all around the world, debating the merits and fallacies of one approach or the other. The reason for this package is not to participate in this discussion but rather to provide a simple no nonsense means of signaling a computation "failure" in those monads that provide the inherent means to do so, and to do it in a consistent manner

Usage


data FooError = NotImplemented deriving (Typeable, Show)

instance Exception FooError

foo :: (Failable m) => m Int
foo = failure NotImplemented

Now, if one called foo in a Maybe monad:

> foo :: Maybe Int
> Nothing

the failure is then conveyed by returning Nothing as per definition of the Maybe monad. Now in the case of the Either SomeException monad:

> foo :: Either SomeException Int
> Left NotImplemented

but what if we are working in the IO monad?

> foo :: IO Int
> * * * Exception: NotImplemented

In this case, the failure can only be conveyed by throwing an IO exception.

Now, the point where Failable diverges from say MonadThrow for example is when it comes to monad transformers. For example:

> runMaybeT foo :: IO (Maybe Int)

Would throw an Exception: NotImplemented if it was implemented in a MonadThrow context. Since the reason d'etre for the runMaybeT is to provide the underlying monad (transformer) with Maybe like behaviour, i.e. have Nothing be returned in case of aborting the Maybe pipeline so to speak, then throwing an exception defeats IMHO the purpose of using MaybeT in the first place. So, in the case of Failable:

> runMaybeT foo :: IO (Maybe Int)
> Nothing

And the same thing applies to runExceptT etc.

The IO problem

One of the most common complaints about error monads is that they erroneously give the impression that if the user deals with the returned failed condition (i.e. Nothing or Left <SomeError> for Maybe(MaybeT) or Either(ExceptT) respectively) the job is done and the code is now "safe", when in reality all one has done is opened up an additional error "path" on top of IO exceptions. Regarldess of one's position on IO exceptions, truth is they are not going to go away.. probably ever. So one has to find a way to live with them in the best possible manner. To this effect, this library offers a utility function failableIO. This function can be used if the Failable monad is also an instance of MonadIO and it lifts an IO operation into the monad but in the event of an IO error, it returns this as a failure in the right context. So for example:

foo :: (Failable m, MonadIO m) => m ()
foo = do
  failableIO $ do
    txt <- readFile "foo.txt"
    putStrLn txt
> runExceptT foo
> Left foo.txt: openFile: does not exist (No such file or directory)

> runMaybeT foo
> Nothing

but if ran directly on IO:

> foo
> *** Exception: foo.txt: openFile: does not exist (No such file or directory)

IMHO this is an improvement from having foo fail with an IO exception or a failure value depending on the context.

Metadata

Version

1.2.4.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