{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
module Control.Effect.Cut
(
Cut(..)
, cutfail
, call
, cut
, Algebra
, Effect
, Has
, run
) where
import Control.Algebra
import Control.Applicative (Alternative(..))
data Cut m k
= Cutfail
| forall a . Call (m a) (a -> m k)
deriving instance Functor m => Functor (Cut m)
instance HFunctor Cut where
hmap :: (forall x. m x -> n x) -> Cut m a -> Cut n a
hmap _ Cutfail = Cut n a
forall (m :: * -> *) k. Cut m k
Cutfail
hmap f :: forall x. m x -> n x
f (Call m :: m a
m k :: a -> m a
k) = n a -> (a -> n a) -> Cut n a
forall (m :: * -> *) k a. m a -> (a -> m k) -> Cut m k
Call (m a -> n a
forall x. m x -> n x
f m a
m) (m a -> n a
forall x. m x -> n x
f (m a -> n a) -> (a -> m a) -> a -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
k)
{-# INLINE hmap #-}
instance Effect Cut where
thread :: ctx ()
-> (forall x. ctx (m x) -> n (ctx x)) -> Cut m a -> Cut n (ctx a)
thread _ _ Cutfail = Cut n (ctx a)
forall (m :: * -> *) k. Cut m k
Cutfail
thread ctx :: ctx ()
ctx handler :: forall x. ctx (m x) -> n (ctx x)
handler (Call m :: m a
m k :: a -> m a
k) = n (ctx a) -> (ctx a -> n (ctx a)) -> Cut n (ctx a)
forall (m :: * -> *) k a. m a -> (a -> m k) -> Cut m k
Call (ctx (m a) -> n (ctx a)
forall x. ctx (m x) -> n (ctx x)
handler (m a
m m a -> ctx () -> ctx (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx)) (ctx (m a) -> n (ctx a)
forall x. ctx (m x) -> n (ctx x)
handler (ctx (m a) -> n (ctx a))
-> (ctx a -> ctx (m a)) -> ctx a -> n (ctx a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m a) -> ctx a -> ctx (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> m a
k)
{-# INLINE thread #-}
cutfail :: Has Cut sig m => m a
cutfail :: m a
cutfail = Cut m a -> m a
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send Cut m a
forall (m :: * -> *) k. Cut m k
Cutfail
{-# INLINE cutfail #-}
call :: Has Cut sig m => m a -> m a
call :: m a -> m a
call m :: m a
m = Cut m a -> m a
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (m a -> (a -> m a) -> Cut m a
forall (m :: * -> *) k a. m a -> (a -> m k) -> Cut m k
Call m a
m a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
{-# INLINE call #-}
cut :: (Alternative m, Has Cut sig m) => m ()
cut :: m ()
cut = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure () m () -> m () -> m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> m ()
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has Cut sig m =>
m a
cutfail
{-# INLINE cut #-}