Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HoistFail #7

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hoist-error.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hoist-error
version: 0.2.2.0
version: 0.2.3.0
synopsis: Some convenience facilities for hoisting errors into a monad
description: Provides a typeclass and useful combinators for hoisting errors into a monad.
license: MIT
Expand Down Expand Up @@ -33,6 +33,7 @@ source-repository head

library
exposed-modules: Control.Monad.Error.Hoist
, Control.Monad.Fail.Hoist
build-depends: base >=4.7 && <4.18
, mtl >=2.1 && <2.3
, either >=4 && <6
Expand Down
83 changes: 83 additions & 0 deletions src/Control/Monad/Fail/Hoist.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}

-- | 'HoistFail extends 'MonadFail with 'hoistFail, which enables lifting
-- of partiality types such as 'Maybe' and @'Either' e@ into the monad.
module Control.Monad.Fail.Hoist
( HoistFail(..)
, hoistFail_
, hoistFailE
, hoistFailE_
, (<%!>)
, (<!>)
) where

import Control.Monad.Error.Class (MonadError (..))

import Data.Either (Either, either)

class Monad m => HoistFail t m e | t -> e where

-- | Given a conversion from the error in @t a@ to @String@, we can hoist the
-- computation into @m@.
--
-- @
-- 'hoistFail' :: 'MonadFail' m => (() -> e) -> 'Maybe' a -> m a
-- 'hoistFail' :: 'MonadFail' m => (a -> e) -> 'Either' a b -> m b
-- @
hoistFail
:: (e -> String)
-> t a
-> m a

instance MonadFail m => HoistFail Maybe m () where
hoistFail f = maybe (fail $ f ()) pure

instance MonadFail m => HoistFail (Either e) m e where
hoistFail f = either (fail . f) pure

hoistFail_ :: HoistFail t m String => t a -> m a
hoistFail_ = hoistFail id
lrworth marked this conversation as resolved.
Show resolved Hide resolved

hoistFailE :: MonadFail m => (e -> String) -> Either e a -> m a
hoistFailE = hoistFail
lrworth marked this conversation as resolved.
Show resolved Hide resolved

hoistFailE_ :: MonadFail m => Either String a -> m a
hoistFailE_ = hoistFail_

-- | A flipped synonym for 'hoistFail'.
--
-- @
-- ('<%!>') :: 'MonadFail' m => 'Maybe' a -> (() -> e) -> m a
-- ('<%!>') :: 'MonadFail' m => 'Either' a b -> (a -> e) -> m b
-- @
(<%!>)
lrworth marked this conversation as resolved.
Show resolved Hide resolved
:: HoistFail t m e
=> t a
-> (e -> String)
-> m a
(<%!>) = flip hoistFail

infixl 8 <%!>
{-# INLINE (<%!>) #-}

-- | A version of '<%!>' that ignores the error in @t a@ and replaces it
-- with a new one.
--
-- @
-- ('<!>') :: 'MonadFail' m => 'Maybe' a -> e -> m a
-- ('<!>') :: 'MonadFail' m => 'Either' a b -> e -> m b
-- @
(<!>)
:: HoistFail t m e
=> t a
-> String
-> m a
m <!> e = m <%!> const e

infixl 8 <!>
{-# INLINE (<!>) #-}