{-# LANGUAGE DefaultSignatures, DeriveFunctor, EmptyCase, FlexibleContexts, FlexibleInstances, FunctionalDependencies, RankNTypes, TypeOperators, UndecidableInstances #-}
module Control.Effect.Carrier
( HFunctor(..)
, Effect(..)
, Carrier(..)
, send
, handlePure
, handleCoercible
-- * Generic deriving of 'HFunctor' & 'Effect' instances.
, GHFunctor(..)
, GEffect(..)
-- * Re-exports
, Pure.run
, (Sum.:+:)(..)
, Sum.Member(..)
) where

import qualified Control.Effect.Pure as Pure
import qualified Control.Effect.Sum as Sum
import           Data.Coerce
import           GHC.Generics

-- | Higher-order functors of kind @(* -> *) -> (* -> *)@ map functors to functors.
--
--   All effects must be 'HFunctor's.
class HFunctor h where
  -- | Apply a handler specified as a natural transformation to both higher-order and continuation positions within an 'HFunctor'.
  fmap' :: Functor (h f) => (a -> b) -> h f a -> h f b
  fmap' = (a -> b) -> h f a -> h f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
  {-# INLINE fmap' #-}

  -- | Higher-order functor map of a natural transformation over higher-order positions within the effect.
  --
  -- A definition for 'hmap' over first-order effects can be derived automatically provided a 'Generic1' instance is available.
  hmap :: Functor m => (forall x . m x -> n x) -> (h m a -> h n a)
  default hmap :: (Functor m, Generic1 (h m), Generic1 (h n), GHFunctor m n (Rep1 (h m)) (Rep1 (h n))) => (forall x . m x -> n x) -> (h m a -> h n a)
  hmap f :: forall x. m x -> n x
f = Rep1 (h n) a -> h n a
forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (Rep1 (h n) a -> h n a)
-> (h m a -> Rep1 (h n) a) -> h m a -> h n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. m x -> n x) -> Rep1 (h m) a -> Rep1 (h n) a
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) a.
(GHFunctor m m' rep rep', Functor m) =>
(forall x. m x -> m' x) -> rep a -> rep' a
ghmap forall x. m x -> n x
f (Rep1 (h m) a -> Rep1 (h n) a)
-> (h m a -> Rep1 (h m) a) -> h m a -> Rep1 (h n) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. h m a -> Rep1 (h m) a
forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1
  {-# INLINE hmap #-}

{-# DEPRECATED fmap' "fmap' has been subsumed by fmap." #-}

instance HFunctor Pure.Pure
instance (HFunctor f, HFunctor g) => HFunctor (f Sum.:+: g)


-- | The class of effect types, which must:
--
--   1. Be functorial in their last two arguments, and
--   2. Support threading effects in higher-order positions through using the carrier’s suspended state.
--
-- All first-order effects (those without existential occurrences of @m@) admit a default definition of 'handle' provided a 'Generic1' instance is available for the effect.
class HFunctor sig => Effect sig where
  -- | Handle any effects in a signature by threading the carrier’s state all the way through to the continuation.
  handle :: (Functor f, Monad m)
         => f ()
         -> (forall x . f (m x) -> n (f x))
         -> sig m a
         -> sig n (f a)
  default handle :: (Functor f, Monad m, Generic1 (sig m), Generic1 (sig n), GEffect m n (Rep1 (sig m)) (Rep1 (sig n)))
                 => f ()
                 -> (forall x . f (m x) -> n (f x))
                 -> sig m a
                 -> sig n (f a)
  handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler = Rep1 (sig n) (f a) -> sig n (f a)
forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (Rep1 (sig n) (f a) -> sig n (f a))
-> (sig m a -> Rep1 (sig n) (f a)) -> sig m a -> sig n (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ()
-> (forall x. f (m x) -> n (f x))
-> Rep1 (sig m) a
-> Rep1 (sig n) (f a)
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) (f :: * -> *) a.
(GEffect m m' rep rep', Functor f, Monad m) =>
f () -> (forall x. f (m x) -> m' (f x)) -> rep a -> rep' (f a)
ghandle f ()
state forall x. f (m x) -> n (f x)
handler (Rep1 (sig m) a -> Rep1 (sig n) (f a))
-> (sig m a -> Rep1 (sig m) a) -> sig m a -> Rep1 (sig n) (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. sig m a -> Rep1 (sig m) a
forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1
  {-# INLINE handle #-}

instance Effect Pure.Pure
instance (Effect f, Effect g) => Effect (f Sum.:+: g)


-- | The class of carriers (results) for algebras (effect handlers) over signatures (effects), whose actions are given by the 'eff' method.
class (HFunctor sig, Monad m) => Carrier sig m | m -> sig where
  -- | Construct a value in the carrier for an effect signature (typically a sum of a handled effect and any remaining effects).
  eff :: sig m a -> m a


instance Carrier Pure.Pure Pure.PureC where
  eff :: Pure PureC a -> PureC a
eff v :: Pure PureC a
v = case Pure PureC a
v of {}
  {-# INLINE eff #-}


-- | Construct a request for an effect to be interpreted by some handler later on.
send :: (Sum.Member effect sig, Carrier sig m) => effect m a -> m a
send :: effect m a -> m a
send = sig m a -> m a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Carrier sig m =>
sig m a -> m a
eff (sig m a -> m a) -> (effect m a -> sig m a) -> effect m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. effect m a -> sig m a
forall (sub :: (* -> *) -> * -> *) (sup :: (* -> *) -> * -> *)
       (m :: * -> *) a.
Member sub sup =>
sub m a -> sup m a
Sum.inj
{-# INLINE send #-}


-- | Apply a handler specified as a natural transformation to both higher-order and continuation positions within an 'HFunctor'.
handlePure :: (HFunctor sig, Functor f) => (forall x . f x -> g x) -> sig f a -> sig g a
handlePure :: (forall x. f x -> g x) -> sig f a -> sig g a
handlePure = (forall x. f x -> g x) -> sig f a -> sig g a
forall (h :: (* -> *) -> * -> *) (m :: * -> *) (n :: * -> *) a.
(HFunctor h, Functor m) =>
(forall x. m x -> n x) -> h m a -> h n a
hmap
{-# INLINE handlePure #-}
{-# DEPRECATED handlePure "handlePure has been subsumed by hmap." #-}

-- | Thread a 'Coercible' carrier through an 'HFunctor'.
--
--   This is applicable whenever @f@ is 'Coercible' to @g@, e.g. simple @newtype@s.
handleCoercible :: (HFunctor sig, Functor f, Coercible f g) => sig f a -> sig g a
handleCoercible :: sig f a -> sig g a
handleCoercible = (forall x. f x -> g x) -> sig f a -> sig g a
forall (h :: (* -> *) -> * -> *) (m :: * -> *) (n :: * -> *) a.
(HFunctor h, Functor m) =>
(forall x. m x -> n x) -> h m a -> h n a
hmap forall x. f x -> g x
forall a b. Coercible a b => a -> b
coerce
{-# INLINE handleCoercible #-}


-- | Generic implementation of 'HFunctor'.
class GHFunctor m m' rep rep' where
  -- | Generic implementation of 'hmap'.
  ghmap :: Functor m => (forall x . m x -> m' x) -> (rep a -> rep' a)

instance GHFunctor m m' rep rep' => GHFunctor m m' (M1 i c rep) (M1 i c rep') where
  ghmap :: (forall x. m x -> m' x) -> M1 i c rep a -> M1 i c rep' a
ghmap f :: forall x. m x -> m' x
f = rep' a -> M1 i c rep' a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (rep' a -> M1 i c rep' a)
-> (M1 i c rep a -> rep' a) -> M1 i c rep a -> M1 i c rep' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. m x -> m' x) -> rep a -> rep' a
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) a.
(GHFunctor m m' rep rep', Functor m) =>
(forall x. m x -> m' x) -> rep a -> rep' a
ghmap forall x. m x -> m' x
f (rep a -> rep' a)
-> (M1 i c rep a -> rep a) -> M1 i c rep a -> rep' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c rep a -> rep a
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1
  {-# INLINE ghmap #-}

instance (GHFunctor m m' l l', GHFunctor m m' r r') => GHFunctor m m' (l :+: r) (l' :+: r') where
  ghmap :: (forall x. m x -> m' x) -> (:+:) l r a -> (:+:) l' r' a
ghmap f :: forall x. m x -> m' x
f (L1 l :: l a
l) = l' a -> (:+:) l' r' a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 ((forall x. m x -> m' x) -> l a -> l' a
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) a.
(GHFunctor m m' rep rep', Functor m) =>
(forall x. m x -> m' x) -> rep a -> rep' a
ghmap forall x. m x -> m' x
f l a
l)
  ghmap f :: forall x. m x -> m' x
f (R1 r :: r a
r) = r' a -> (:+:) l' r' a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 ((forall x. m x -> m' x) -> r a -> r' a
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) a.
(GHFunctor m m' rep rep', Functor m) =>
(forall x. m x -> m' x) -> rep a -> rep' a
ghmap forall x. m x -> m' x
f r a
r)
  {-# INLINE ghmap #-}

instance (GHFunctor m m' l l', GHFunctor m m' r r') => GHFunctor m m' (l :*: r) (l' :*: r') where
  ghmap :: (forall x. m x -> m' x) -> (:*:) l r a -> (:*:) l' r' a
ghmap f :: forall x. m x -> m' x
f (l :: l a
l :*: r :: r a
r) = (forall x. m x -> m' x) -> l a -> l' a
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) a.
(GHFunctor m m' rep rep', Functor m) =>
(forall x. m x -> m' x) -> rep a -> rep' a
ghmap forall x. m x -> m' x
f l a
l l' a -> r' a -> (:*:) l' r' a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: (forall x. m x -> m' x) -> r a -> r' a
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) a.
(GHFunctor m m' rep rep', Functor m) =>
(forall x. m x -> m' x) -> rep a -> rep' a
ghmap forall x. m x -> m' x
f r a
r
  {-# INLINE ghmap #-}

instance GHFunctor m m' V1 V1 where
  ghmap :: (forall x. m x -> m' x) -> V1 a -> V1 a
ghmap _ v :: V1 a
v = case V1 a
v of {}
  {-# INLINE ghmap #-}

instance GHFunctor m m' U1 U1 where
  ghmap :: (forall x. m x -> m' x) -> U1 a -> U1 a
ghmap _ = U1 a -> U1 a
forall a. a -> a
id
  {-# INLINE ghmap #-}

instance GHFunctor m m' (K1 R c) (K1 R c) where
  ghmap :: (forall x. m x -> m' x) -> K1 R c a -> K1 R c a
ghmap _ = K1 R c a -> K1 R c a
forall a b. Coercible a b => a -> b
coerce
  {-# INLINE ghmap #-}

instance GHFunctor m m' Par1 Par1 where
  ghmap :: (forall x. m x -> m' x) -> Par1 a -> Par1 a
ghmap _ = Par1 a -> Par1 a
forall a b. Coercible a b => a -> b
coerce
  {-# INLINE ghmap #-}

instance (Functor f, GHFunctor m m' g g') => GHFunctor m m' (f :.: g) (f :.: g') where
  ghmap :: (forall x. m x -> m' x) -> (:.:) f g a -> (:.:) f g' a
ghmap f :: forall x. m x -> m' x
f = f (g' a) -> (:.:) f g' a
forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (p :: k1).
f (g p) -> (:.:) f g p
Comp1 (f (g' a) -> (:.:) f g' a)
-> ((:.:) f g a -> f (g' a)) -> (:.:) f g a -> (:.:) f g' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (g a -> g' a) -> f (g a) -> f (g' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((forall x. m x -> m' x) -> g a -> g' a
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) a.
(GHFunctor m m' rep rep', Functor m) =>
(forall x. m x -> m' x) -> rep a -> rep' a
ghmap forall x. m x -> m' x
f) (f (g a) -> f (g' a))
-> ((:.:) f g a -> f (g a)) -> (:.:) f g a -> f (g' a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:.:) f g a -> f (g a)
forall k2 (f :: k2 -> *) k1 (g :: k1 -> k2) (p :: k1).
(:.:) f g p -> f (g p)
unComp1
  {-# INLINE ghmap #-}

instance GHFunctor m m' (Rec1 m) (Rec1 m') where
  ghmap :: (forall x. m x -> m' x) -> Rec1 m a -> Rec1 m' a
ghmap f :: forall x. m x -> m' x
f = m' a -> Rec1 m' a
forall k (f :: k -> *) (p :: k). f p -> Rec1 f p
Rec1 (m' a -> Rec1 m' a) -> (Rec1 m a -> m' a) -> Rec1 m a -> Rec1 m' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> m' a
forall x. m x -> m' x
f (m a -> m' a) -> (Rec1 m a -> m a) -> Rec1 m a -> m' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec1 m a -> m a
forall k (f :: k -> *) (p :: k). Rec1 f p -> f p
unRec1
  {-# INLINE ghmap #-}

instance HFunctor f => GHFunctor m m' (Rec1 (f m)) (Rec1 (f m')) where
  ghmap :: (forall x. m x -> m' x) -> Rec1 (f m) a -> Rec1 (f m') a
ghmap f :: forall x. m x -> m' x
f = f m' a -> Rec1 (f m') a
forall k (f :: k -> *) (p :: k). f p -> Rec1 f p
Rec1 (f m' a -> Rec1 (f m') a)
-> (Rec1 (f m) a -> f m' a) -> Rec1 (f m) a -> Rec1 (f m') a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. m x -> m' x) -> f m a -> f m' a
forall (h :: (* -> *) -> * -> *) (m :: * -> *) (n :: * -> *) a.
(HFunctor h, Functor m) =>
(forall x. m x -> n x) -> h m a -> h n a
hmap forall x. m x -> m' x
f (f m a -> f m' a)
-> (Rec1 (f m) a -> f m a) -> Rec1 (f m) a -> f m' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec1 (f m) a -> f m a
forall k (f :: k -> *) (p :: k). Rec1 f p -> f p
unRec1
  {-# INLINE ghmap #-}


-- | Generic implementation of 'Effect'.
class GEffect m m' rep rep' where
  -- | Generic implementation of 'handle'.
  ghandle :: (Functor f, Monad m)
          => f ()
          -> (forall x . f (m x) -> m' (f x))
          -> rep a
          -> rep' (f a)

instance GEffect m m' rep rep' => GEffect m m' (M1 i c rep) (M1 i c rep') where
  ghandle :: f ()
-> (forall x. f (m x) -> m' (f x))
-> M1 i c rep a
-> M1 i c rep' (f a)
ghandle state :: f ()
state handler :: forall x. f (m x) -> m' (f x)
handler = rep' (f a) -> M1 i c rep' (f a)
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (rep' (f a) -> M1 i c rep' (f a))
-> (M1 i c rep a -> rep' (f a))
-> M1 i c rep a
-> M1 i c rep' (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f () -> (forall x. f (m x) -> m' (f x)) -> rep a -> rep' (f a)
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) (f :: * -> *) a.
(GEffect m m' rep rep', Functor f, Monad m) =>
f () -> (forall x. f (m x) -> m' (f x)) -> rep a -> rep' (f a)
ghandle f ()
state forall x. f (m x) -> m' (f x)
handler (rep a -> rep' (f a))
-> (M1 i c rep a -> rep a) -> M1 i c rep a -> rep' (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c rep a -> rep a
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1
  {-# INLINE ghandle #-}

instance (GEffect m m' l l', GEffect m m' r r') => GEffect m m' (l :+: r) (l' :+: r') where
  ghandle :: f ()
-> (forall x. f (m x) -> m' (f x))
-> (:+:) l r a
-> (:+:) l' r' (f a)
ghandle state :: f ()
state handler :: forall x. f (m x) -> m' (f x)
handler (L1 l :: l a
l) = l' (f a) -> (:+:) l' r' (f a)
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (f () -> (forall x. f (m x) -> m' (f x)) -> l a -> l' (f a)
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) (f :: * -> *) a.
(GEffect m m' rep rep', Functor f, Monad m) =>
f () -> (forall x. f (m x) -> m' (f x)) -> rep a -> rep' (f a)
ghandle f ()
state forall x. f (m x) -> m' (f x)
handler l a
l)
  ghandle state :: f ()
state handler :: forall x. f (m x) -> m' (f x)
handler (R1 r :: r a
r) = r' (f a) -> (:+:) l' r' (f a)
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (f () -> (forall x. f (m x) -> m' (f x)) -> r a -> r' (f a)
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) (f :: * -> *) a.
(GEffect m m' rep rep', Functor f, Monad m) =>
f () -> (forall x. f (m x) -> m' (f x)) -> rep a -> rep' (f a)
ghandle f ()
state forall x. f (m x) -> m' (f x)
handler r a
r)
  {-# INLINE ghandle #-}

instance (GEffect m m' l l', GEffect m m' r r') => GEffect m m' (l :*: r) (l' :*: r') where
  ghandle :: f ()
-> (forall x. f (m x) -> m' (f x))
-> (:*:) l r a
-> (:*:) l' r' (f a)
ghandle state :: f ()
state handler :: forall x. f (m x) -> m' (f x)
handler (l :: l a
l :*: r :: r a
r) = f () -> (forall x. f (m x) -> m' (f x)) -> l a -> l' (f a)
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) (f :: * -> *) a.
(GEffect m m' rep rep', Functor f, Monad m) =>
f () -> (forall x. f (m x) -> m' (f x)) -> rep a -> rep' (f a)
ghandle f ()
state forall x. f (m x) -> m' (f x)
handler l a
l l' (f a) -> r' (f a) -> (:*:) l' r' (f a)
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: f () -> (forall x. f (m x) -> m' (f x)) -> r a -> r' (f a)
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) (f :: * -> *) a.
(GEffect m m' rep rep', Functor f, Monad m) =>
f () -> (forall x. f (m x) -> m' (f x)) -> rep a -> rep' (f a)
ghandle f ()
state forall x. f (m x) -> m' (f x)
handler r a
r
  {-# INLINE ghandle #-}

instance GEffect m m' V1 V1 where
  ghandle :: f () -> (forall x. f (m x) -> m' (f x)) -> V1 a -> V1 (f a)
ghandle _ _ v :: V1 a
v = case V1 a
v of {}
  {-# INLINE ghandle #-}

instance GEffect m m' U1 U1 where
  ghandle :: f () -> (forall x. f (m x) -> m' (f x)) -> U1 a -> U1 (f a)
ghandle _ _ = U1 a -> U1 (f a)
forall a b. Coercible a b => a -> b
coerce
  {-# INLINE ghandle #-}

instance GEffect m m' (K1 R c) (K1 R c) where
  ghandle :: f () -> (forall x. f (m x) -> m' (f x)) -> K1 R c a -> K1 R c (f a)
ghandle _ _ = K1 R c a -> K1 R c (f a)
forall a b. Coercible a b => a -> b
coerce
  {-# INLINE ghandle #-}

instance GEffect m m' Par1 Par1 where
  ghandle :: f () -> (forall x. f (m x) -> m' (f x)) -> Par1 a -> Par1 (f a)
ghandle state :: f ()
state _ = f a -> Par1 (f a)
forall p. p -> Par1 p
Par1 (f a -> Par1 (f a)) -> (Par1 a -> f a) -> Par1 a -> Par1 (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f () -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state) (a -> f a) -> (Par1 a -> a) -> Par1 a -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Par1 a -> a
forall p. Par1 p -> p
unPar1
  {-# INLINE ghandle #-}

instance (Functor f, GEffect m m' g g') => GEffect m m' (f :.: g) (f :.: g') where
  ghandle :: f ()
-> (forall x. f (m x) -> m' (f x))
-> (:.:) f g a
-> (:.:) f g' (f a)
ghandle state :: f ()
state handler :: forall x. f (m x) -> m' (f x)
handler = f (g' (f a)) -> (:.:) f g' (f a)
forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (p :: k1).
f (g p) -> (:.:) f g p
Comp1 (f (g' (f a)) -> (:.:) f g' (f a))
-> ((:.:) f g a -> f (g' (f a))) -> (:.:) f g a -> (:.:) f g' (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (g a -> g' (f a)) -> f (g a) -> f (g' (f a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (f () -> (forall x. f (m x) -> m' (f x)) -> g a -> g' (f a)
forall (m :: * -> *) (m' :: * -> *) (rep :: * -> *)
       (rep' :: * -> *) (f :: * -> *) a.
(GEffect m m' rep rep', Functor f, Monad m) =>
f () -> (forall x. f (m x) -> m' (f x)) -> rep a -> rep' (f a)
ghandle f ()
state forall x. f (m x) -> m' (f x)
handler) (f (g a) -> f (g' (f a)))
-> ((:.:) f g a -> f (g a)) -> (:.:) f g a -> f (g' (f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:.:) f g a -> f (g a)
forall k2 (f :: k2 -> *) k1 (g :: k1 -> k2) (p :: k1).
(:.:) f g p -> f (g p)
unComp1
  {-# INLINE ghandle #-}

instance GEffect m m' (Rec1 m) (Rec1 m') where
  ghandle :: f ()
-> (forall x. f (m x) -> m' (f x)) -> Rec1 m a -> Rec1 m' (f a)
ghandle state :: f ()
state handler :: forall x. f (m x) -> m' (f x)
handler = m' (f a) -> Rec1 m' (f a)
forall k (f :: k -> *) (p :: k). f p -> Rec1 f p
Rec1 (m' (f a) -> Rec1 m' (f a))
-> (Rec1 m a -> m' (f a)) -> Rec1 m a -> Rec1 m' (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (m a) -> m' (f a)
forall x. f (m x) -> m' (f x)
handler (f (m a) -> m' (f a))
-> (Rec1 m a -> f (m a)) -> Rec1 m a -> m' (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (m a -> f () -> f (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state) (m a -> f (m a)) -> (Rec1 m a -> m a) -> Rec1 m a -> f (m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec1 m a -> m a
forall k (f :: k -> *) (p :: k). Rec1 f p -> f p
unRec1
  {-# INLINE ghandle #-}

instance Effect f => GEffect m m' (Rec1 (f m)) (Rec1 (f m')) where
  ghandle :: f ()
-> (forall x. f (m x) -> m' (f x))
-> Rec1 (f m) a
-> Rec1 (f m') (f a)
ghandle state :: f ()
state handler :: forall x. f (m x) -> m' (f x)
handler = f m' (f a) -> Rec1 (f m') (f a)
forall k (f :: k -> *) (p :: k). f p -> Rec1 f p
Rec1 (f m' (f a) -> Rec1 (f m') (f a))
-> (Rec1 (f m) a -> f m' (f a))
-> Rec1 (f m) a
-> Rec1 (f m') (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f () -> (forall x. f (m x) -> m' (f x)) -> f m a -> f m' (f a)
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (m :: * -> *)
       (n :: * -> *) a.
(Effect sig, Functor f, Monad m) =>
f () -> (forall x. f (m x) -> n (f x)) -> sig m a -> sig n (f a)
handle f ()
state forall x. f (m x) -> m' (f x)
handler (f m a -> f m' (f a))
-> (Rec1 (f m) a -> f m a) -> Rec1 (f m) a -> f m' (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec1 (f m) a -> f m a
forall k (f :: k -> *) (p :: k). Rec1 f p -> f p
unRec1
  {-# INLINE ghandle #-}