{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

-- | A carrier for 'Cut' and 'NonDet' effects used in tandem (@Cut :+: NonDet@).
--
-- @since 1.0.0.0
module Control.Carrier.Cut.Church
( -- * Cut carrier
  runCut
, runCutA
, runCutM
, CutC(..)
  -- * Cut effect
, module Control.Effect.Cut
  -- * NonDet effects
, module Control.Effect.NonDet
) where

import Control.Algebra
import Control.Applicative
import Control.Effect.Cut
import Control.Effect.NonDet
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Data.Coerce (coerce)
import Data.Functor.Identity

-- | Run a 'Cut' effect with continuations respectively interpreting 'pure' / '<|>', 'empty', and 'cutfail'.
--
-- @
-- runCut cons nil fail ('pure' a '<|>' 'empty') = cons a nil
-- @
-- @
-- runCut cons nil fail 'cutfail' = fail
-- @
-- @
-- runCut cons nil fail ('call' 'cutfail') = nil
-- @
--
-- @since 1.0.0.0
runCut :: (a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut :: forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut a -> m b -> m b
cons m b
nil m b
fail (CutC forall b. (a -> m b -> m b) -> m b -> m b -> m b
runCutC) = (a -> m b -> m b) -> m b -> m b -> m b
forall b. (a -> m b -> m b) -> m b -> m b -> m b
runCutC a -> m b -> m b
cons m b
nil m b
fail
{-# INLINE runCut #-}

-- | Run a 'Cut' effect, returning all its results in an 'Alternative' collection.
--
-- @since 1.0.0.0
runCutA :: (Alternative f, Applicative m) => CutC m a -> m (f a)
runCutA :: forall (f :: * -> *) (m :: * -> *) a.
(Alternative f, Applicative m) =>
CutC m a -> m (f a)
runCutA = (a -> m (f a) -> m (f a))
-> m (f a) -> m (f a) -> CutC m a -> m (f a)
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut ((f a -> f a) -> m (f a) -> m (f a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((f a -> f a) -> m (f a) -> m (f a))
-> (a -> f a -> f a) -> a -> m (f a) -> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> f a -> f a
forall a. f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (f a -> f a -> f a) -> (a -> f a) -> a -> f a -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f a
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) (f a -> m (f a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure f a
forall a. f a
forall (f :: * -> *) a. Alternative f => f a
empty) (f a -> m (f a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure f a
forall a. f a
forall (f :: * -> *) a. Alternative f => f a
empty)
{-# INLINE runCutA #-}

-- | Run a 'Cut' effect, mapping results into a 'Monoid'.
--
-- @since 1.0.0.0
runCutM :: (Applicative m, Monoid b) => (a -> b) -> CutC m a -> m b
runCutM :: forall (m :: * -> *) b a.
(Applicative m, Monoid b) =>
(a -> b) -> CutC m a -> m b
runCutM a -> b
leaf = (a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut ((b -> b) -> m b -> m b
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> b) -> m b -> m b) -> (a -> b -> b) -> a -> m b -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> b -> b
forall a. Monoid a => a -> a -> a
mappend (b -> b -> b) -> (a -> b) -> a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
leaf) (b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
forall a. Monoid a => a
mempty) (b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
forall a. Monoid a => a
mempty)
{-# INLINE runCutM #-}

-- | @since 1.0.0.0
newtype CutC m a = CutC (forall b . (a -> m b -> m b) -> m b -> m b -> m b)
  deriving ((forall a b. (a -> b) -> CutC m a -> CutC m b)
-> (forall a b. a -> CutC m b -> CutC m a) -> Functor (CutC m)
forall a b. a -> CutC m b -> CutC m a
forall a b. (a -> b) -> CutC m a -> CutC 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 -> CutC m b -> CutC m a
forall (m :: * -> *) a b. (a -> b) -> CutC m a -> CutC m b
$cfmap :: forall (m :: * -> *) a b. (a -> b) -> CutC m a -> CutC m b
fmap :: forall a b. (a -> b) -> CutC m a -> CutC m b
$c<$ :: forall (m :: * -> *) a b. a -> CutC m b -> CutC m a
<$ :: forall a b. a -> CutC m b -> CutC m a
Functor)

instance Applicative (CutC m) where
  pure :: forall a. a -> CutC m a
pure a
a = (forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
CutC (\ a -> m b -> m b
cons m b
nil m b
_ -> a -> m b -> m b
cons a
a m b
nil)
  {-# INLINE pure #-}

  CutC forall b. ((a -> b) -> m b -> m b) -> m b -> m b -> m b
f <*> :: forall a b. CutC m (a -> b) -> CutC m a -> CutC m b
<*> CutC forall b. (a -> m b -> m b) -> m b -> m b -> m b
a = (forall b. (b -> m b -> m b) -> m b -> m b -> m b) -> CutC m b
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
CutC ((forall b. (b -> m b -> m b) -> m b -> m b -> m b) -> CutC m b)
-> (forall b. (b -> m b -> m b) -> m b -> m b -> m b) -> CutC m b
forall a b. (a -> b) -> a -> b
$ \ b -> m b -> m b
cons m b
nil m b
fail ->
    ((a -> b) -> m b -> m b) -> m b -> m b -> m b
forall b. ((a -> b) -> m b -> m b) -> m b -> m b -> m b
f (\ a -> b
f' m b
fs -> (a -> m b -> m b) -> m b -> m b -> m b
forall b. (a -> m b -> m b) -> m b -> m b -> m b
a (b -> m b -> m b
cons (b -> m b -> m b) -> (a -> b) -> a -> m b -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f') m b
fs m b
fail) m b
nil m b
fail
  {-# INLINE (<*>) #-}

instance Alternative (CutC m) where
  empty :: forall a. CutC m a
empty = (forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
CutC (\ a -> m b -> m b
_ m b
nil m b
_ -> m b
nil)
  {-# INLINE empty #-}

  CutC forall b. (a -> m b -> m b) -> m b -> m b -> m b
l <|> :: forall a. CutC m a -> CutC m a -> CutC m a
<|> CutC forall b. (a -> m b -> m b) -> m b -> m b -> m b
r = (forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
CutC (\ a -> m b -> m b
cons m b
nil m b
fail -> (a -> m b -> m b) -> m b -> m b -> m b
forall b. (a -> m b -> m b) -> m b -> m b -> m b
l a -> m b -> m b
cons ((a -> m b -> m b) -> m b -> m b -> m b
forall b. (a -> m b -> m b) -> m b -> m b -> m b
r a -> m b -> m b
cons m b
nil m b
fail) m b
fail)
  {-# INLINE (<|>) #-}

instance Monad (CutC m) where
  CutC forall b. (a -> m b -> m b) -> m b -> m b -> m b
a >>= :: forall a b. CutC m a -> (a -> CutC m b) -> CutC m b
>>= a -> CutC m b
f = (forall b. (b -> m b -> m b) -> m b -> m b -> m b) -> CutC m b
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
CutC ((forall b. (b -> m b -> m b) -> m b -> m b -> m b) -> CutC m b)
-> (forall b. (b -> m b -> m b) -> m b -> m b -> m b) -> CutC m b
forall a b. (a -> b) -> a -> b
$ \ b -> m b -> m b
cons m b
nil m b
fail ->
    (a -> m b -> m b) -> m b -> m b -> m b
forall b. (a -> m b -> m b) -> m b -> m b -> m b
a (\ a
a' m b
as -> (b -> m b -> m b) -> m b -> m b -> CutC m b -> m b
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut b -> m b -> m b
cons m b
as m b
fail (a -> CutC m b
f a
a')) m b
nil m b
fail
  {-# INLINE (>>=) #-}

instance Fail.MonadFail m => Fail.MonadFail (CutC m) where
  fail :: forall a. String -> CutC m a
fail String
s = m a -> CutC m a
forall (m :: * -> *) a. Monad m => m a -> CutC m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (String -> m a
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail String
s)
  {-# INLINE fail #-}

-- | A single fixpoint is shared between all branches.
instance MonadFix m => MonadFix (CutC m) where
  mfix :: forall a. (a -> CutC m a) -> CutC m a
mfix a -> CutC m a
f = (forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
CutC ((forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a)
-> (forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
forall a b. (a -> b) -> a -> b
$ \ a -> m b -> m b
cons m b
nil m b
fail -> (CutC Identity a -> m (CutC Identity a)) -> m (CutC Identity a)
forall a. (a -> m a) -> m a
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix
    (CutC m a -> m (CutC Identity a)
forall {a}. CutC m a -> m (CutC Identity a)
toCut (CutC m a -> m (CutC Identity a))
-> (CutC Identity a -> CutC m a)
-> CutC Identity a
-> m (CutC Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> CutC m a
f (a -> CutC m a)
-> (CutC Identity a -> a) -> CutC Identity a -> CutC m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
run (Identity a -> a)
-> (CutC Identity a -> Identity a) -> CutC Identity a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CutC Identity a -> Identity a
forall {b}. CutC Identity b -> Identity b
fromCut)
    m (CutC Identity a) -> (CutC Identity a -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Identity (m b) -> m b
forall a. Identity a -> a
run (Identity (m b) -> m b)
-> (CutC Identity a -> Identity (m b)) -> CutC Identity a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Identity (m b) -> Identity (m b))
-> Identity (m b)
-> Identity (m b)
-> CutC Identity a
-> Identity (m b)
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut ((m b -> m b) -> Identity (m b) -> Identity (m b)
forall a b. (a -> b) -> Identity a -> Identity b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((m b -> m b) -> Identity (m b) -> Identity (m b))
-> (a -> m b -> m b) -> a -> Identity (m b) -> Identity (m b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b -> m b
cons) (m b -> Identity (m b)
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure m b
nil) (m b -> Identity (m b)
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure m b
fail) where
    toCut :: CutC m a -> m (CutC Identity a)
toCut = (a -> m (CutC Identity a) -> m (CutC Identity a))
-> m (CutC Identity a)
-> m (CutC Identity a)
-> CutC m a
-> m (CutC Identity a)
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut ((CutC Identity a -> CutC Identity a)
-> m (CutC Identity a) -> m (CutC Identity a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CutC Identity a -> CutC Identity a)
 -> m (CutC Identity a) -> m (CutC Identity a))
-> (a -> CutC Identity a -> CutC Identity a)
-> a
-> m (CutC Identity a)
-> m (CutC Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CutC Identity a -> CutC Identity a -> CutC Identity a
forall a. CutC Identity a -> CutC Identity a -> CutC Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (CutC Identity a -> CutC Identity a -> CutC Identity a)
-> (a -> CutC Identity a)
-> a
-> CutC Identity a
-> CutC Identity a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> CutC Identity a
forall a. a -> CutC Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) (CutC Identity a -> m (CutC Identity a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CutC Identity a
forall a. CutC Identity a
forall (f :: * -> *) a. Alternative f => f a
empty) (CutC Identity a -> m (CutC Identity a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CutC Identity a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has Cut sig m =>
m a
cutfail)
    fromCut :: CutC Identity b -> Identity b
fromCut = (b -> Identity b -> Identity b)
-> Identity b -> Identity b -> CutC Identity b -> Identity b
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut b -> Identity b -> Identity b
forall a b. a -> Identity b -> Identity a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
(<$) (String -> Identity b
forall a. HasCallStack => String -> a
error String
"mfix CutC: empty") (String -> Identity b
forall a. HasCallStack => String -> a
error String
"mfix CutC: cutfail")
  {-# INLINE mfix #-}

instance MonadIO m => MonadIO (CutC m) where
  liftIO :: forall a. IO a -> CutC m a
liftIO IO a
io = m a -> CutC m a
forall (m :: * -> *) a. Monad m => m a -> CutC m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
io)
  {-# INLINE liftIO #-}

instance MonadPlus (CutC m)

instance MonadTrans CutC where
  lift :: forall (m :: * -> *) a. Monad m => m a -> CutC m a
lift m a
m = (forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
CutC (\ a -> m b -> m b
cons m b
nil m b
_ -> m a
m m a -> (a -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a -> m b -> m b) -> m b -> a -> m b
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> m b -> m b
cons m b
nil)
  {-# INLINE lift #-}

instance Algebra sig m => Algebra (Cut :+: NonDet :+: sig) (CutC m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (CutC m)
-> (:+:) Cut (NonDet :+: sig) n a -> ctx () -> CutC m (ctx a)
alg Handler ctx n (CutC m)
hdl (:+:) Cut (NonDet :+: sig) n a
sig ctx ()
ctx = (forall b. (ctx a -> m b -> m b) -> m b -> m b -> m b)
-> CutC m (ctx a)
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b -> m b) -> CutC m a
CutC ((forall b. (ctx a -> m b -> m b) -> m b -> m b -> m b)
 -> CutC m (ctx a))
-> (forall b. (ctx a -> m b -> m b) -> m b -> m b -> m b)
-> CutC m (ctx a)
forall a b. (a -> b) -> a -> b
$ \ ctx a -> m b -> m b
cons m b
nil m b
fail -> case (:+:) Cut (NonDet :+: sig) n a
sig of
    L Cut n a
Cutfail        -> m b
fail
    L (Call n a
m)       -> (ctx a -> m b -> m b) -> m b -> m b -> CutC m (ctx a) -> m b
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut ctx a -> m b -> m b
cons m b
nil m b
nil (ctx (n a) -> CutC m (ctx a)
Handler ctx n (CutC m)
hdl (n a
m n a -> ctx () -> ctx (n a)
forall a b. a -> ctx b -> ctx a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx))
    R (L (L Empty n a
Empty))  -> m b
nil
    R (L (R Choose n a
Choose)) -> ctx a -> m b -> m b
cons (a
Bool
True a -> ctx () -> ctx a
forall a b. a -> ctx b -> ctx a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx) (ctx a -> m b -> m b
cons (a
Bool
False a -> ctx () -> ctx a
forall a b. a -> ctx b -> ctx a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx) m b
nil)
    R (R sig n a
other)      -> Handler (Compose (CutC Identity) ctx) n m
-> sig n a -> CutC Identity (ctx ()) -> m (CutC Identity (ctx a))
forall (ctx1 :: * -> *) (ctx2 :: * -> *)
       (sig :: (* -> *) -> * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor ctx1, Functor ctx2, Algebra sig m) =>
Handler (Compose ctx1 ctx2) n m
-> sig n a -> ctx1 (ctx2 ()) -> m (ctx1 (ctx2 a))
thread (CutC Identity (CutC m x) -> m (CutC Identity x)
forall {x}. CutC Identity (CutC m x) -> m (CutC Identity x)
forall (m :: * -> *) a.
Applicative m =>
CutC Identity (CutC m a) -> m (CutC Identity a)
dst (forall {x}. CutC Identity (CutC m x) -> m (CutC Identity x))
-> Handler ctx n (CutC m)
-> Handler (Compose (CutC Identity) ctx) n m
forall (n :: * -> *) (ctx1 :: * -> *) (m :: * -> *)
       (ctx2 :: * -> *) (l :: * -> *).
(Functor n, Functor ctx1) =>
Handler ctx1 m n
-> Handler ctx2 l m -> Handler (Compose ctx1 ctx2) l n
~<~ ctx (n x) -> CutC m (ctx x)
Handler ctx n (CutC m)
hdl) sig n a
other (ctx () -> CutC Identity (ctx ())
forall a. a -> CutC Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ctx ()
ctx) m (CutC Identity (ctx a)) -> (CutC Identity (ctx a) -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Identity (m b) -> m b
forall a. Identity a -> a
run (Identity (m b) -> m b)
-> (CutC Identity (ctx a) -> Identity (m b))
-> CutC Identity (ctx a)
-> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ctx a -> Identity (m b) -> Identity (m b))
-> Identity (m b)
-> Identity (m b)
-> CutC Identity (ctx a)
-> Identity (m b)
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut ((ctx a -> m b -> m b) -> ctx a -> Identity (m b) -> Identity (m b)
forall a b. Coercible a b => a -> b
coerce ctx a -> m b -> m b
cons) (m b -> Identity (m b)
forall a b. Coercible a b => a -> b
coerce m b
nil) (m b -> Identity (m b)
forall a b. Coercible a b => a -> b
coerce m b
fail)
    where
    dst :: Applicative m => CutC Identity (CutC m a) -> m (CutC Identity a)
    dst :: forall (m :: * -> *) a.
Applicative m =>
CutC Identity (CutC m a) -> m (CutC Identity a)
dst = Identity (m (CutC Identity a)) -> m (CutC Identity a)
forall a. Identity a -> a
run (Identity (m (CutC Identity a)) -> m (CutC Identity a))
-> (CutC Identity (CutC m a) -> Identity (m (CutC Identity a)))
-> CutC Identity (CutC m a)
-> m (CutC Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CutC m a
 -> Identity (m (CutC Identity a))
 -> Identity (m (CutC Identity a)))
-> Identity (m (CutC Identity a))
-> Identity (m (CutC Identity a))
-> CutC Identity (CutC m a)
-> Identity (m (CutC Identity a))
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut ((m (CutC Identity a) -> m (CutC Identity a))
-> Identity (m (CutC Identity a)) -> Identity (m (CutC Identity a))
forall a b. (a -> b) -> Identity a -> Identity b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((m (CutC Identity a) -> m (CutC Identity a))
 -> Identity (m (CutC Identity a))
 -> Identity (m (CutC Identity a)))
-> (CutC m a -> m (CutC Identity a) -> m (CutC Identity a))
-> CutC m a
-> Identity (m (CutC Identity a))
-> Identity (m (CutC Identity a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CutC Identity a -> CutC Identity a -> CutC Identity a)
-> m (CutC Identity a)
-> m (CutC Identity a)
-> m (CutC Identity a)
forall a b c. (a -> b -> c) -> m a -> m b -> m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 CutC Identity a -> CutC Identity a -> CutC Identity a
forall a. CutC Identity a -> CutC Identity a -> CutC Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (m (CutC Identity a) -> m (CutC Identity a) -> m (CutC Identity a))
-> (CutC m a -> m (CutC Identity a))
-> CutC m a
-> m (CutC Identity a)
-> m (CutC Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m (CutC Identity a) -> m (CutC Identity a))
-> m (CutC Identity a)
-> m (CutC Identity a)
-> CutC m a
-> m (CutC Identity a)
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> m b -> CutC m a -> m b
runCut ((CutC Identity a -> CutC Identity a)
-> m (CutC Identity a) -> m (CutC Identity a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CutC Identity a -> CutC Identity a)
 -> m (CutC Identity a) -> m (CutC Identity a))
-> (a -> CutC Identity a -> CutC Identity a)
-> a
-> m (CutC Identity a)
-> m (CutC Identity a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CutC Identity a -> CutC Identity a -> CutC Identity a
forall a. CutC Identity a -> CutC Identity a -> CutC Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (CutC Identity a -> CutC Identity a -> CutC Identity a)
-> (a -> CutC Identity a)
-> a
-> CutC Identity a
-> CutC Identity a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> CutC Identity a
forall a. a -> CutC Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) (CutC Identity a -> m (CutC Identity a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CutC Identity a
forall a. CutC Identity a
forall (f :: * -> *) a. Alternative f => f a
empty) (CutC Identity a -> m (CutC Identity a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CutC Identity a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has Cut sig m =>
m a
cutfail)) (m (CutC Identity a) -> Identity (m (CutC Identity a))
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CutC Identity a -> m (CutC Identity a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CutC Identity a
forall a. CutC Identity a
forall (f :: * -> *) a. Alternative f => f a
empty)) (m (CutC Identity a) -> Identity (m (CutC Identity a))
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CutC Identity a -> m (CutC Identity a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CutC Identity a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has Cut sig m =>
m a
cutfail))
  {-# INLINE alg #-}