{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

-- | A carrier for 'Cull' and 'NonDet' effects used in tandem (@Cull :+: NonDet@).
--
-- @since 1.0.0.0
module Control.Carrier.Cull.Church
( -- * Cull carrier
  runCull
, runCullA
, runCullM
, CullC(CullC)
  -- * Cull effect
, module Control.Effect.Cull
  -- * NonDet effects
, module Control.Effect.NonDet
) where

import Control.Algebra
import Control.Applicative (liftA2)
import Control.Carrier.NonDet.Church
import Control.Carrier.Reader
import Control.Effect.Cull
import Control.Effect.NonDet
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class

-- | Run a 'Cull' effect with continuations respectively interpreting '<|>', 'pure', and 'empty'. Branches outside of any 'cull' block will not be pruned.
--
-- @
-- runCull fork leaf nil ('pure' a '<|>' 'empty') = leaf a \`fork\` nil
-- @
-- @
-- runCull fork leaf nil ('cull' ('pure' a '<|>' b)) = leaf a
-- @
--
-- @since 1.0.0.0
runCull :: (m b -> m b -> m b) -> (a -> m b) -> m b -> CullC m a -> m b
runCull :: forall (m :: * -> *) b a.
(m b -> m b -> m b) -> (a -> m b) -> m b -> CullC m a -> m b
runCull m b -> m b -> m b
fork a -> m b
leaf m b
nil (CullC ReaderC Bool (NonDetC m) a
m) = forall (m :: * -> *) b a.
(m b -> m b -> m b) -> (a -> m b) -> m b -> NonDetC m a -> m b
runNonDet m b -> m b -> m b
fork a -> m b
leaf m b
nil (forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader Bool
False ReaderC Bool (NonDetC m) a
m)
{-# INLINE runCull #-}

-- | Run a 'Cull' effect, interpreting the result into an 'Alternative' functor. Choice is handled with '<|>', embedding with 'pure', and failure with 'empty'.
--
-- @since 1.0.0.0
runCullA :: (Alternative f, Applicative m) => CullC m a -> m (f a)
runCullA :: forall (f :: * -> *) (m :: * -> *) a.
(Alternative f, Applicative m) =>
CullC m a -> m (f a)
runCullA = forall (m :: * -> *) b a.
(m b -> m b -> m b) -> (a -> m b) -> m b -> CullC m a -> m b
runCull (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a. Alternative f => f a
empty)
{-# INLINE runCullA #-}

-- | Run a 'Cull' effect, mapping results into a 'Monoid'.
--
-- @since 1.0.0.0
runCullM :: (Applicative m, Monoid b) => (a -> b) -> CullC m a -> m b
runCullM :: forall (m :: * -> *) b a.
(Applicative m, Monoid b) =>
(a -> b) -> CullC m a -> m b
runCullM a -> b
leaf = forall (m :: * -> *) b a.
(m b -> m b -> m b) -> (a -> m b) -> m b -> CullC m a -> m b
runCull (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Monoid a => a -> a -> a
mappend) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
leaf) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty)
{-# INLINE runCullM #-}

-- | @since 1.0.0.0
newtype CullC m a = CullC { forall (m :: * -> *) a. CullC m a -> ReaderC Bool (NonDetC m) a
runCullC :: ReaderC Bool (NonDetC m) a }
  deriving (forall a. a -> CullC m a
forall a b. CullC m a -> CullC m b -> CullC m a
forall a b. CullC m a -> CullC m b -> CullC m b
forall a b. CullC m (a -> b) -> CullC m a -> CullC m b
forall a b c. (a -> b -> c) -> CullC m a -> CullC m b -> CullC m c
forall {m :: * -> *}. Functor (CullC m)
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 :: * -> *) a. a -> CullC m a
forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m a
forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m b
forall (m :: * -> *) a b.
CullC m (a -> b) -> CullC m a -> CullC m b
forall (m :: * -> *) a b c.
(a -> b -> c) -> CullC m a -> CullC m b -> CullC m c
<* :: forall a b. CullC m a -> CullC m b -> CullC m a
$c<* :: forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m a
*> :: forall a b. CullC m a -> CullC m b -> CullC m b
$c*> :: forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m b
liftA2 :: forall a b c. (a -> b -> c) -> CullC m a -> CullC m b -> CullC m c
$cliftA2 :: forall (m :: * -> *) a b c.
(a -> b -> c) -> CullC m a -> CullC m b -> CullC m c
<*> :: forall a b. CullC m (a -> b) -> CullC m a -> CullC m b
$c<*> :: forall (m :: * -> *) a b.
CullC m (a -> b) -> CullC m a -> CullC m b
pure :: forall a. a -> CullC m a
$cpure :: forall (m :: * -> *) a. a -> CullC m a
Applicative, forall a b. a -> CullC m b -> CullC m a
forall a b. (a -> b) -> CullC m a -> CullC 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 -> CullC m b -> CullC m a
forall (m :: * -> *) a b. (a -> b) -> CullC m a -> CullC m b
<$ :: forall a b. a -> CullC m b -> CullC m a
$c<$ :: forall (m :: * -> *) a b. a -> CullC m b -> CullC m a
fmap :: forall a b. (a -> b) -> CullC m a -> CullC m b
$cfmap :: forall (m :: * -> *) a b. (a -> b) -> CullC m a -> CullC m b
Functor, forall a. a -> CullC m a
forall a b. CullC m a -> CullC m b -> CullC m b
forall a b. CullC m a -> (a -> CullC m b) -> CullC m b
forall (m :: * -> *). Applicative (CullC m)
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
forall (m :: * -> *) a. a -> CullC m a
forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m b
forall (m :: * -> *) a b.
CullC m a -> (a -> CullC m b) -> CullC m b
return :: forall a. a -> CullC m a
$creturn :: forall (m :: * -> *) a. a -> CullC m a
>> :: forall a b. CullC m a -> CullC m b -> CullC m b
$c>> :: forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m b
>>= :: forall a b. CullC m a -> (a -> CullC m b) -> CullC m b
$c>>= :: forall (m :: * -> *) a b.
CullC m a -> (a -> CullC m b) -> CullC m b
Monad, forall a. String -> CullC m a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
forall {m :: * -> *}. MonadFail m => Monad (CullC m)
forall (m :: * -> *) a. MonadFail m => String -> CullC m a
fail :: forall a. String -> CullC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> CullC m a
Fail.MonadFail, forall a. IO a -> CullC m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
forall {m :: * -> *}. MonadIO m => Monad (CullC m)
forall (m :: * -> *) a. MonadIO m => IO a -> CullC m a
liftIO :: forall a. IO a -> CullC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> CullC m a
MonadIO)

instance Alternative (CullC m) where
  empty :: forall a. CullC m a
empty = forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC forall (f :: * -> *) a. Alternative f => f a
empty
  {-# INLINE empty #-}

  CullC ReaderC Bool (NonDetC m) a
l <|> :: forall a. CullC m a -> CullC m a -> CullC m a
<|> CullC ReaderC Bool (NonDetC m) a
r = forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. (r -> m a) -> ReaderC r m a
ReaderC forall a b. (a -> b) -> a -> b
$ \ Bool
cull ->
    if Bool
cull then
      forall (m :: * -> *) a.
(forall b. (m b -> m b -> m b) -> (a -> m b) -> m b -> m b)
-> NonDetC m a
NonDetC forall a b. (a -> b) -> a -> b
$ \ m b -> m b -> m b
fork a -> m b
leaf m b
nil ->
        forall (m :: * -> *) b a.
(m b -> m b -> m b) -> (a -> m b) -> m b -> NonDetC m a -> m b
runNonDet m b -> m b -> m b
fork a -> m b
leaf (forall (m :: * -> *) b a.
(m b -> m b -> m b) -> (a -> m b) -> m b -> NonDetC m a -> m b
runNonDet m b -> m b -> m b
fork a -> m b
leaf m b
nil (forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader Bool
cull ReaderC Bool (NonDetC m) a
r)) (forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader Bool
cull ReaderC Bool (NonDetC m) a
l)
    else
      forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader Bool
cull ReaderC Bool (NonDetC m) a
l forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader Bool
cull ReaderC Bool (NonDetC m) a
r
  {-# INLINE (<|>) #-}

-- | Separate fixpoints are computed for each branch.
deriving instance MonadFix m => MonadFix (CullC m)

instance MonadPlus (CullC m)

instance MonadTrans CullC where
  lift :: forall (m :: * -> *) a. Monad m => m a -> CullC m a
lift = forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
  {-# INLINE lift #-}

instance Algebra sig m => Algebra (Cull :+: NonDet :+: sig) (CullC m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (CullC m)
-> (:+:) Cull (NonDet :+: sig) n a -> ctx () -> CullC m (ctx a)
alg Handler ctx n (CullC m)
hdl (:+:) Cull (NonDet :+: sig) n a
sig ctx ()
ctx = case (:+:) Cull (NonDet :+: sig) n a
sig of
    L (Cull n a
m)       -> forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC (forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Reader r) sig m =>
(r -> r) -> m a -> m a
local (forall a b. a -> b -> a
const Bool
True) (forall (m :: * -> *) a. CullC m a -> ReaderC Bool (NonDetC m) a
runCullC (Handler ctx n (CullC m)
hdl (n a
m forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx))))
    R (L (L Empty n a
Empty))  -> forall (f :: * -> *) a. Alternative f => f a
empty
    R (L (R Choose n a
Choose)) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool
True forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool
False forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx)
    R (R sig n a
other)      -> forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC (forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
       (n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (forall (m :: * -> *) a. CullC m a -> ReaderC Bool (NonDetC m) a
runCullC forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handler ctx n (CullC m)
hdl) (forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R (forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other)) ctx ()
ctx)
  {-# INLINE alg #-}