-
Notifications
You must be signed in to change notification settings - Fork 53
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
Proposal: allow automatic derivation of HFunctor instances. #170
Changes from all commits
a28304e
fd5f24d
faca34e
2d3f6f6
a8141b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{-# LANGUAGE DeriveFunctor, FlexibleInstances, GeneralizedNewtypeDeriving, KindSignatures, MultiParamTypeClasses, TypeOperators, UndecidableInstances #-} | ||
{-# LANGUAGE DeriveAnyClass, DeriveFunctor, DerivingStrategies, FlexibleInstances, GeneralizedNewtypeDeriving, KindSignatures, MultiParamTypeClasses, TypeOperators, UndecidableInstances #-} | ||
module Control.Effect.Fail | ||
( Fail(..) | ||
, MonadFail(..) | ||
|
@@ -14,20 +14,11 @@ import Control.Monad (MonadPlus(..)) | |
import Control.Monad.Fail | ||
import Control.Monad.IO.Class | ||
import Control.Monad.Trans.Class | ||
import Data.Coerce | ||
import Prelude hiding (fail) | ||
|
||
newtype Fail (m :: * -> *) k = Fail String | ||
deriving (Functor) | ||
|
||
instance HFunctor Fail where | ||
hmap _ = coerce | ||
{-# INLINE hmap #-} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we lose inlining with the default definition? Or does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The INLINE pragma applies to each derived instance 🎉 |
||
|
||
instance Effect Fail where | ||
handle _ _ = coerce | ||
{-# INLINE handle #-} | ||
|
||
deriving stock Functor | ||
deriving anyclass (HFunctor, Effect) | ||
|
||
-- | Run a 'Fail' effect, returning failure messages in 'Left' and successful computations’ results in 'Right'. | ||
-- | ||
|
@@ -36,7 +27,7 @@ runFail :: FailC m a -> m (Either String a) | |
runFail = runError . runFailC | ||
|
||
newtype FailC m a = FailC { runFailC :: ErrorC String m a } | ||
deriving (Alternative, Applicative, Functor, Monad, MonadIO, MonadPlus, MonadTrans) | ||
deriving newtype (Alternative, Applicative, Functor, Monad, MonadIO, MonadPlus, MonadTrans) | ||
|
||
instance (Carrier sig m, Effect sig) => MonadFail (FailC m) where | ||
fail s = FailC (throwError s) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do the error messages look like when you try to use the default definition with a higher-order effect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be worse, but it could be better: