{-# LANGUAGE DeriveAnyClass, DeriveFunctor, DeriveGeneric, DerivingStrategies, FlexibleInstances, GeneralizedNewtypeDeriving, KindSignatures, MultiParamTypeClasses, TypeOperators, UndecidableInstances #-}
module Control.Effect.Fail
(
Fail(..)
, runFail
, FailC(..)
, Carrier
, Member
, Fail.MonadFail(..)
, run
) where
import Control.Applicative (Alternative(..))
import Control.Effect.Carrier
import Control.Effect.Error
import Control.Monad (MonadPlus(..))
import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import GHC.Generics (Generic1)
newtype Fail (m :: * -> *) k = Fail String
deriving stock ((a -> b) -> Fail m a -> Fail m b
(forall a b. (a -> b) -> Fail m a -> Fail m b)
-> (forall a b. a -> Fail m b -> Fail m a) -> Functor (Fail m)
forall a b. a -> Fail m b -> Fail m a
forall a b. (a -> b) -> Fail m a -> Fail m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
forall (m :: * -> *) a b. a -> Fail m b -> Fail m a
forall (m :: * -> *) a b. (a -> b) -> Fail m a -> Fail m b
<$ :: a -> Fail m b -> Fail m a
$c<$ :: forall (m :: * -> *) a b. a -> Fail m b -> Fail m a
fmap :: (a -> b) -> Fail m a -> Fail m b
$cfmap :: forall (m :: * -> *) a b. (a -> b) -> Fail m a -> Fail m b
Functor, (forall a. Fail m a -> Rep1 (Fail m) a)
-> (forall a. Rep1 (Fail m) a -> Fail m a) -> Generic1 (Fail m)
forall a. Rep1 (Fail m) a -> Fail m a
forall a. Fail m a -> Rep1 (Fail m) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall (m :: * -> *) a. Rep1 (Fail m) a -> Fail m a
forall (m :: * -> *) a. Fail m a -> Rep1 (Fail m) a
$cto1 :: forall (m :: * -> *) a. Rep1 (Fail m) a -> Fail m a
$cfrom1 :: forall (m :: * -> *) a. Fail m a -> Rep1 (Fail m) a
Generic1)
deriving anyclass ((forall (f :: * -> *) a b.
Functor (Fail f) =>
(a -> b) -> Fail f a -> Fail f b)
-> (forall (m :: * -> *) (n :: * -> *) a.
Functor m =>
(forall x. m x -> n x) -> Fail m a -> Fail n a)
-> HFunctor Fail
forall (f :: * -> *) a b.
Functor (Fail f) =>
(a -> b) -> Fail f a -> Fail f b
forall (m :: * -> *) (n :: * -> *) a.
Functor m =>
(forall x. m x -> n x) -> Fail m a -> Fail n a
forall (h :: (* -> *) -> * -> *).
(forall (f :: * -> *) a b.
Functor (h f) =>
(a -> b) -> h f a -> h f b)
-> (forall (m :: * -> *) (n :: * -> *) a.
Functor m =>
(forall x. m x -> n x) -> h m a -> h n a)
-> HFunctor h
hmap :: (forall x. m x -> n x) -> Fail m a -> Fail n a
$chmap :: forall (m :: * -> *) (n :: * -> *) a.
Functor m =>
(forall x. m x -> n x) -> Fail m a -> Fail n a
fmap' :: (a -> b) -> Fail f a -> Fail f b
$cfmap' :: forall (f :: * -> *) a b.
Functor (Fail f) =>
(a -> b) -> Fail f a -> Fail f b
HFunctor, HFunctor Fail
HFunctor Fail =>
(forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor f, Monad m) =>
f () -> (forall x. f (m x) -> n (f x)) -> Fail m a -> Fail n (f a))
-> Effect Fail
forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor f, Monad m) =>
f () -> (forall x. f (m x) -> n (f x)) -> Fail m a -> Fail n (f a)
forall (sig :: (* -> *) -> * -> *).
HFunctor sig =>
(forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor f, Monad m) =>
f () -> (forall x. f (m x) -> n (f x)) -> sig m a -> sig n (f a))
-> Effect sig
handle :: f () -> (forall x. f (m x) -> n (f x)) -> Fail m a -> Fail n (f a)
$chandle :: forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor f, Monad m) =>
f () -> (forall x. f (m x) -> n (f x)) -> Fail m a -> Fail n (f a)
$cp1Effect :: HFunctor Fail
Effect)
runFail :: FailC m a -> m (Either String a)
runFail :: FailC m a -> m (Either String a)
runFail = ErrorC String m a -> m (Either String a)
forall exc (m :: * -> *) a. ErrorC exc m a -> m (Either exc a)
runError (ErrorC String m a -> m (Either String a))
-> (FailC m a -> ErrorC String m a)
-> FailC m a
-> m (Either String a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FailC m a -> ErrorC String m a
forall (m :: * -> *) a. FailC m a -> ErrorC String m a
runFailC
newtype FailC m a = FailC { FailC m a -> ErrorC String m a
runFailC :: ErrorC String m a }
deriving newtype (Applicative (FailC m)
FailC m a
Applicative (FailC m) =>
(forall a. FailC m a)
-> (forall a. FailC m a -> FailC m a -> FailC m a)
-> (forall a. FailC m a -> FailC m [a])
-> (forall a. FailC m a -> FailC m [a])
-> Alternative (FailC m)
FailC m a -> FailC m a -> FailC m a
FailC m a -> FailC m [a]
FailC m a -> FailC m [a]
forall a. FailC m a
forall a. FailC m a -> FailC m [a]
forall a. FailC m a -> FailC m a -> FailC m a
forall (f :: * -> *).
Applicative f =>
(forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
forall (m :: * -> *). Alternative m => Applicative (FailC m)
forall (m :: * -> *) a. Alternative m => FailC m a
forall (m :: * -> *) a. Alternative m => FailC m a -> FailC m [a]
forall (m :: * -> *) a.
Alternative m =>
FailC m a -> FailC m a -> FailC m a
many :: FailC m a -> FailC m [a]
$cmany :: forall (m :: * -> *) a. Alternative m => FailC m a -> FailC m [a]
some :: FailC m a -> FailC m [a]
$csome :: forall (m :: * -> *) a. Alternative m => FailC m a -> FailC m [a]
<|> :: FailC m a -> FailC m a -> FailC m a
$c<|> :: forall (m :: * -> *) a.
Alternative m =>
FailC m a -> FailC m a -> FailC m a
empty :: FailC m a
$cempty :: forall (m :: * -> *) a. Alternative m => FailC m a
$cp1Alternative :: forall (m :: * -> *). Alternative m => Applicative (FailC m)
Alternative, Functor (FailC m)
a -> FailC m a
Functor (FailC m) =>
(forall a. a -> FailC m a)
-> (forall a b. FailC m (a -> b) -> FailC m a -> FailC m b)
-> (forall a b c.
(a -> b -> c) -> FailC m a -> FailC m b -> FailC m c)
-> (forall a b. FailC m a -> FailC m b -> FailC m b)
-> (forall a b. FailC m a -> FailC m b -> FailC m a)
-> Applicative (FailC m)
FailC m a -> FailC m b -> FailC m b
FailC m a -> FailC m b -> FailC m a
FailC m (a -> b) -> FailC m a -> FailC m b
(a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
forall a. a -> FailC m a
forall a b. FailC m a -> FailC m b -> FailC m a
forall a b. FailC m a -> FailC m b -> FailC m b
forall a b. FailC m (a -> b) -> FailC m a -> FailC m b
forall a b c. (a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall (m :: * -> *). Applicative m => Functor (FailC m)
forall (m :: * -> *) a. Applicative m => a -> FailC m a
forall (m :: * -> *) a b.
Applicative m =>
FailC m a -> FailC m b -> FailC m a
forall (m :: * -> *) a b.
Applicative m =>
FailC m a -> FailC m b -> FailC m b
forall (m :: * -> *) a b.
Applicative m =>
FailC m (a -> b) -> FailC m a -> FailC m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
<* :: FailC m a -> FailC m b -> FailC m a
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
FailC m a -> FailC m b -> FailC m a
*> :: FailC m a -> FailC m b -> FailC m b
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
FailC m a -> FailC m b -> FailC m b
liftA2 :: (a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> FailC m a -> FailC m b -> FailC m c
<*> :: FailC m (a -> b) -> FailC m a -> FailC m b
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
FailC m (a -> b) -> FailC m a -> FailC m b
pure :: a -> FailC m a
$cpure :: forall (m :: * -> *) a. Applicative m => a -> FailC m a
$cp1Applicative :: forall (m :: * -> *). Applicative m => Functor (FailC m)
Applicative, a -> FailC m b -> FailC m a
(a -> b) -> FailC m a -> FailC m b
(forall a b. (a -> b) -> FailC m a -> FailC m b)
-> (forall a b. a -> FailC m b -> FailC m a) -> Functor (FailC m)
forall a b. a -> FailC m b -> FailC m a
forall a b. (a -> b) -> FailC m a -> FailC m b
forall (m :: * -> *) a b. Functor m => a -> FailC m b -> FailC m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> FailC m a -> FailC m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> FailC m b -> FailC m a
$c<$ :: forall (m :: * -> *) a b. Functor m => a -> FailC m b -> FailC m a
fmap :: (a -> b) -> FailC m a -> FailC m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> FailC m a -> FailC m b
Functor, Applicative (FailC m)
a -> FailC m a
Applicative (FailC m) =>
(forall a b. FailC m a -> (a -> FailC m b) -> FailC m b)
-> (forall a b. FailC m a -> FailC m b -> FailC m b)
-> (forall a. a -> FailC m a)
-> Monad (FailC m)
FailC m a -> (a -> FailC m b) -> FailC m b
FailC m a -> FailC m b -> FailC m b
forall a. a -> FailC m a
forall a b. FailC m a -> FailC m b -> FailC m b
forall a b. FailC m a -> (a -> FailC m b) -> FailC m b
forall (m :: * -> *). Monad m => Applicative (FailC m)
forall (m :: * -> *) a. Monad m => a -> FailC m a
forall (m :: * -> *) a b.
Monad m =>
FailC m a -> FailC m b -> FailC m b
forall (m :: * -> *) a b.
Monad m =>
FailC m a -> (a -> FailC m b) -> FailC m b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> FailC m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> FailC m a
>> :: FailC m a -> FailC m b -> FailC m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
FailC m a -> FailC m b -> FailC m b
>>= :: FailC m a -> (a -> FailC m b) -> FailC m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
FailC m a -> (a -> FailC m b) -> FailC m b
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (FailC m)
Monad, Monad (FailC m)
Monad (FailC m) =>
(forall a. (a -> FailC m a) -> FailC m a) -> MonadFix (FailC m)
(a -> FailC m a) -> FailC m a
forall a. (a -> FailC m a) -> FailC m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
forall (m :: * -> *). MonadFix m => Monad (FailC m)
forall (m :: * -> *) a. MonadFix m => (a -> FailC m a) -> FailC m a
mfix :: (a -> FailC m a) -> FailC m a
$cmfix :: forall (m :: * -> *) a. MonadFix m => (a -> FailC m a) -> FailC m a
$cp1MonadFix :: forall (m :: * -> *). MonadFix m => Monad (FailC m)
MonadFix, Monad (FailC m)
Monad (FailC m) =>
(forall a. IO a -> FailC m a) -> MonadIO (FailC m)
IO a -> FailC m a
forall a. IO a -> FailC m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (FailC m)
forall (m :: * -> *) a. MonadIO m => IO a -> FailC m a
liftIO :: IO a -> FailC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> FailC m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (FailC m)
MonadIO, Monad (FailC m)
Alternative (FailC m)
FailC m a
(Alternative (FailC m), Monad (FailC m)) =>
(forall a. FailC m a)
-> (forall a. FailC m a -> FailC m a -> FailC m a)
-> MonadPlus (FailC m)
FailC m a -> FailC m a -> FailC m a
forall a. FailC m a
forall a. FailC m a -> FailC m a -> FailC m a
forall (m :: * -> *). (Alternative m, Monad m) => Monad (FailC m)
forall (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (FailC m)
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
forall (m :: * -> *) a. (Alternative m, Monad m) => FailC m a
forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m a -> FailC m a
mplus :: FailC m a -> FailC m a -> FailC m a
$cmplus :: forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FailC m a -> FailC m a -> FailC m a
mzero :: FailC m a
$cmzero :: forall (m :: * -> *) a. (Alternative m, Monad m) => FailC m a
$cp2MonadPlus :: forall (m :: * -> *). (Alternative m, Monad m) => Monad (FailC m)
$cp1MonadPlus :: forall (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (FailC m)
MonadPlus, m a -> FailC m a
(forall (m :: * -> *) a. Monad m => m a -> FailC m a)
-> MonadTrans FailC
forall (m :: * -> *) a. Monad m => m a -> FailC m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> FailC m a
$clift :: forall (m :: * -> *) a. Monad m => m a -> FailC m a
MonadTrans)
instance (Carrier sig m, Effect sig) => Fail.MonadFail (FailC m) where
fail :: String -> FailC m a
fail s :: String
s = ErrorC String m a -> FailC m a
forall (m :: * -> *) a. ErrorC String m a -> FailC m a
FailC (String -> ErrorC String m a
forall exc (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Member (Error exc) sig, Carrier sig m) =>
exc -> m a
throwError String
s)
{-# INLINE fail #-}
instance (Carrier sig m, Effect sig) => Carrier (Fail :+: sig) (FailC m) where
eff :: (:+:) Fail sig (FailC m) a -> FailC m a
eff (L (Fail s :: String
s)) = String -> FailC m a
forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail String
s
eff (R other :: sig (FailC m) a
other) = ErrorC String m a -> FailC m a
forall (m :: * -> *) a. ErrorC String m a -> FailC m a
FailC ((:+:) (Error String) sig (ErrorC String m) a -> ErrorC String m a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Carrier sig m =>
sig m a -> m a
eff (sig (ErrorC String m) a
-> (:+:) (Error String) sig (ErrorC String m) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R (sig (FailC m) a -> sig (ErrorC String m) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible sig (FailC m) a
other)))
{-# INLINE eff #-}