{-# LANGUAGE DeriveFunctor, ExistentialQuantification, FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, LambdaCase, MultiParamTypeClasses, StandaloneDeriving, TypeOperators, UndecidableInstances #-}
module Control.Effect.Cull
( -- * Cull effect
  Cull(..)
, cull
  -- * Cull carriers
, runCull
, CullC(..)
, runNonDetOnce
, OnceC(..)
-- * Re-exports
, Carrier
, Member
, run
) where

import Control.Effect.Carrier
import Control.Effect.NonDet
import Control.Effect.Reader
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 Prelude hiding (fail)

-- | 'Cull' effects are used with 'NonDet' to provide control over branching.
data Cull m k
  = forall a . Cull (m a) (a -> m k)

deriving instance Functor m => Functor (Cull m)

instance HFunctor Cull where
  hmap :: (forall x. m x -> n x) -> Cull m a -> Cull n a
hmap f :: forall x. m x -> n x
f (Cull m :: m a
m k :: a -> m a
k) = n a -> (a -> n a) -> Cull n a
forall (m :: * -> *) k a. m a -> (a -> m k) -> Cull m k
Cull (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 Cull where
  handle :: f () -> (forall x. f (m x) -> n (f x)) -> Cull m a -> Cull n (f a)
handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler (Cull m :: m a
m k :: a -> m a
k) = n (f a) -> (f a -> n (f a)) -> Cull n (f a)
forall (m :: * -> *) k a. m a -> (a -> m k) -> Cull m k
Cull (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (m a
m m a -> f () -> f (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state)) (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (f (m a) -> n (f a)) -> (f a -> f (m a)) -> f a -> n (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m a) -> f a -> f (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> m a
k)
  {-# INLINE handle #-}

-- | Cull nondeterminism in the argument, returning at most one result.
--
--   prop> run (runNonDet (runCull (cull (pure a <|> pure b)))) === [a]
--   prop> run (runNonDet (runCull (cull (pure a <|> pure b) <|> pure c))) === [a, c]
--   prop> run (runNonDet (runCull (cull (asum (map pure (repeat a)))))) === [a]
cull :: (Carrier sig m, Member Cull sig) => m a -> m a
cull :: m a -> m a
cull m :: m a
m = Cull m a -> m a
forall (effect :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(Member effect sig, Carrier sig m) =>
effect m a -> m a
send (m a -> (a -> m a) -> Cull m a
forall (m :: * -> *) k a. m a -> (a -> m k) -> Cull m k
Cull m a
m a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)


-- | Run a 'Cull' effect. Branches outside of any 'cull' block will not be pruned.
--
--   prop> run (runNonDet (runCull (pure a <|> pure b))) === [a, b]
runCull :: Alternative m => CullC m a -> m a
runCull :: CullC m a -> m a
runCull (CullC m :: ReaderC Bool (NonDetC m) a
m) = NonDetC m a -> (a -> m a -> m a) -> m a -> m a
forall (m :: * -> *) a.
NonDetC m a -> forall b. (a -> m b -> m b) -> m b -> m b
runNonDetC (Bool -> ReaderC Bool (NonDetC m) a -> NonDetC m a
forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader Bool
False ReaderC Bool (NonDetC m) a
m) (m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (m a -> m a -> m a) -> (a -> m a) -> a -> m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) m a
forall (f :: * -> *) a. Alternative f => f a
empty

newtype CullC m a = CullC { CullC m a -> ReaderC Bool (NonDetC m) a
runCullC :: ReaderC Bool (NonDetC m) a }
  deriving (Functor (CullC m)
a -> CullC m a
Functor (CullC m) =>
(forall a. a -> CullC m a)
-> (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 a b. CullC m a -> CullC m b -> CullC m b)
-> (forall a b. CullC m a -> CullC m b -> CullC m a)
-> Applicative (CullC m)
CullC m a -> CullC m b -> CullC m b
CullC m a -> CullC m b -> CullC m a
CullC m (a -> b) -> CullC m a -> CullC m b
(a -> b -> c) -> CullC m a -> CullC m b -> CullC m c
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
<* :: CullC m a -> CullC m b -> CullC m a
$c<* :: forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m a
*> :: CullC m a -> CullC m b -> CullC m b
$c*> :: forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m b
liftA2 :: (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
<*> :: 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 :: a -> CullC m a
$cpure :: forall (m :: * -> *) a. a -> CullC m a
$cp1Applicative :: forall (m :: * -> *). Functor (CullC m)
Applicative, a -> CullC m b -> CullC m a
(a -> b) -> CullC m a -> CullC m b
(forall a b. (a -> b) -> CullC m a -> CullC m b)
-> (forall a b. a -> CullC m b -> CullC m a) -> Functor (CullC m)
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
<$ :: a -> CullC m b -> CullC m a
$c<$ :: forall (m :: * -> *) a b. a -> CullC m b -> CullC m a
fmap :: (a -> b) -> CullC m a -> CullC m b
$cfmap :: forall (m :: * -> *) a b. (a -> b) -> CullC m a -> CullC m b
Functor, Applicative (CullC m)
a -> CullC m a
Applicative (CullC m) =>
(forall a b. CullC m a -> (a -> CullC m b) -> CullC m b)
-> (forall a b. CullC m a -> CullC m b -> CullC m b)
-> (forall a. a -> CullC m a)
-> Monad (CullC m)
CullC m a -> (a -> CullC m b) -> CullC m b
CullC m a -> CullC m b -> CullC m b
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 :: a -> CullC m a
$creturn :: forall (m :: * -> *) a. a -> CullC m a
>> :: CullC m a -> CullC m b -> CullC m b
$c>> :: forall (m :: * -> *) a b. CullC m a -> CullC m b -> CullC m 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
$cp1Monad :: forall (m :: * -> *). Applicative (CullC m)
Monad, Monad (CullC m)
Monad (CullC m) =>
(forall a. String -> CullC m a) -> MonadFail (CullC m)
String -> CullC m a
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 :: String -> CullC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> CullC m a
$cp1MonadFail :: forall (m :: * -> *). MonadFail m => Monad (CullC m)
Fail.MonadFail, Monad (CullC m)
Monad (CullC m) =>
(forall a. (a -> CullC m a) -> CullC m a) -> MonadFix (CullC m)
(a -> CullC m a) -> CullC m a
forall a. (a -> CullC m a) -> CullC m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
forall (m :: * -> *). MonadFix m => Monad (CullC m)
forall (m :: * -> *) a. MonadFix m => (a -> CullC m a) -> CullC m a
mfix :: (a -> CullC m a) -> CullC m a
$cmfix :: forall (m :: * -> *) a. MonadFix m => (a -> CullC m a) -> CullC m a
$cp1MonadFix :: forall (m :: * -> *). MonadFix m => Monad (CullC m)
MonadFix, Monad (CullC m)
Monad (CullC m) =>
(forall a. IO a -> CullC m a) -> MonadIO (CullC m)
IO a -> CullC m a
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 :: IO a -> CullC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> CullC m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (CullC m)
MonadIO)

instance Alternative (CullC m) where
  empty :: CullC m a
empty = ReaderC Bool (NonDetC m) a -> CullC m a
forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC ReaderC Bool (NonDetC m) a
forall (f :: * -> *) a. Alternative f => f a
empty
  {-# INLINE empty #-}
  l :: CullC m a
l <|> :: CullC m a -> CullC m a -> CullC m a
<|> r :: CullC m a
r = ReaderC Bool (NonDetC m) a -> CullC m a
forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC (ReaderC Bool (NonDetC m) a -> CullC m a)
-> ReaderC Bool (NonDetC m) a -> CullC m a
forall a b. (a -> b) -> a -> b
$ (Bool -> NonDetC m a) -> ReaderC Bool (NonDetC m) a
forall r (m :: * -> *) a. (r -> m a) -> ReaderC r m a
ReaderC ((Bool -> NonDetC m a) -> ReaderC Bool (NonDetC m) a)
-> (Bool -> NonDetC m a) -> ReaderC Bool (NonDetC m) a
forall a b. (a -> b) -> a -> b
$ \ cull :: Bool
cull -> (forall b. (a -> m b -> m b) -> m b -> m b) -> NonDetC m a
forall (m :: * -> *) a.
(forall b. (a -> m b -> m b) -> m b -> m b) -> NonDetC m a
NonDetC ((forall b. (a -> m b -> m b) -> m b -> m b) -> NonDetC m a)
-> (forall b. (a -> m b -> m b) -> m b -> m b) -> NonDetC m a
forall a b. (a -> b) -> a -> b
$ \ cons :: a -> m b -> m b
cons nil :: m b
nil -> do
    NonDetC m a -> (a -> m b -> m b) -> m b -> m b
forall (m :: * -> *) a.
NonDetC m a -> forall b. (a -> m b -> m b) -> m b -> m b
runNonDetC (Bool -> ReaderC Bool (NonDetC m) a -> NonDetC m a
forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader Bool
cull (CullC m a -> ReaderC Bool (NonDetC m) a
forall (m :: * -> *) a. CullC m a -> ReaderC Bool (NonDetC m) a
runCullC CullC m a
l))
      (\ a :: a
a as :: m b
as -> a -> m b -> m b
cons a
a (if Bool
cull then m b
nil else m b
as))
      (NonDetC m a -> (a -> m b -> m b) -> m b -> m b
forall (m :: * -> *) a.
NonDetC m a -> forall b. (a -> m b -> m b) -> m b -> m b
runNonDetC (Bool -> ReaderC Bool (NonDetC m) a -> NonDetC m a
forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader Bool
cull (CullC m a -> ReaderC Bool (NonDetC m) a
forall (m :: * -> *) a. CullC m a -> ReaderC Bool (NonDetC m) a
runCullC CullC m a
r)) a -> m b -> m b
cons m b
nil)
  {-# INLINE (<|>) #-}

instance MonadPlus (CullC m)

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

instance (Carrier sig m, Effect sig) => Carrier (Cull :+: NonDet :+: sig) (CullC m) where
  eff :: (:+:) Cull (NonDet :+: sig) (CullC m) a -> CullC m a
eff (L (Cull m :: CullC m a
m k :: a -> CullC m a
k))     = ReaderC Bool (NonDetC m) a -> CullC m a
forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC ((Bool -> Bool)
-> ReaderC Bool (NonDetC m) a -> ReaderC Bool (NonDetC m) a
forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Member (Reader r) sig, Carrier sig m) =>
(r -> r) -> m a -> m a
local (Bool -> Bool -> Bool
forall a b. a -> b -> a
const Bool
True) (CullC m a -> ReaderC Bool (NonDetC m) a
forall (m :: * -> *) a. CullC m a -> ReaderC Bool (NonDetC m) a
runCullC CullC m a
m)) CullC m a -> (a -> CullC m a) -> CullC m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> CullC m a
k
  eff (R (L Empty))      = CullC m a
forall (f :: * -> *) a. Alternative f => f a
empty
  eff (R (L (Choose k :: Bool -> CullC m a
k))) = Bool -> CullC m a
k Bool
True CullC m a -> CullC m a -> CullC m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Bool -> CullC m a
k Bool
False
  eff (R (R other :: sig (CullC m) a
other))      = ReaderC Bool (NonDetC m) a -> CullC m a
forall (m :: * -> *) a. ReaderC Bool (NonDetC m) a -> CullC m a
CullC ((:+:) (Reader Bool) (NonDet :+: sig) (ReaderC Bool (NonDetC m)) a
-> ReaderC Bool (NonDetC m) a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Carrier sig m =>
sig m a -> m a
eff ((:+:) NonDet sig (ReaderC Bool (NonDetC m)) a
-> (:+:)
     (Reader Bool) (NonDet :+: sig) (ReaderC Bool (NonDetC m)) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R (sig (ReaderC Bool (NonDetC m)) a
-> (:+:) NonDet sig (ReaderC Bool (NonDetC m)) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R (sig (CullC m) a -> sig (ReaderC Bool (NonDetC m)) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible sig (CullC m) a
other))))
  {-# INLINE eff #-}


-- | Run a 'NonDet' effect, returning the first successful result in an 'Alternative' functor.
--
--   Unlike 'runNonDet', this will terminate immediately upon finding a solution.
--
--   prop> run (runNonDetOnce (asum (map pure (repeat a)))) === [a]
--   prop> run (runNonDetOnce (asum (map pure (repeat a)))) === Just a
runNonDetOnce :: (Alternative f, Carrier sig m, Effect sig) => OnceC m a -> m (f a)
runNonDetOnce :: OnceC m a -> m (f a)
runNonDetOnce = NonDetC m a -> m (f a)
forall (f :: * -> *) (m :: * -> *) a.
(Alternative f, Applicative m) =>
NonDetC m a -> m (f a)
runNonDet (NonDetC m a -> m (f a))
-> (OnceC m a -> NonDetC m a) -> OnceC m a -> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CullC (NonDetC m) a -> NonDetC m a
forall (m :: * -> *) a. Alternative m => CullC m a -> m a
runCull (CullC (NonDetC m) a -> NonDetC m a)
-> (OnceC m a -> CullC (NonDetC m) a) -> OnceC m a -> NonDetC m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CullC (NonDetC m) a -> CullC (NonDetC m) a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Carrier sig m, Member Cull sig) =>
m a -> m a
cull (CullC (NonDetC m) a -> CullC (NonDetC m) a)
-> (OnceC m a -> CullC (NonDetC m) a)
-> OnceC m a
-> CullC (NonDetC m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnceC m a -> CullC (NonDetC m) a
forall (m :: * -> *) a. OnceC m a -> CullC (NonDetC m) a
runOnceC

newtype OnceC m a = OnceC { OnceC m a -> CullC (NonDetC m) a
runOnceC :: CullC (NonDetC m) a }
  deriving (Applicative (OnceC m)
OnceC m a
Applicative (OnceC m) =>
(forall a. OnceC m a)
-> (forall a. OnceC m a -> OnceC m a -> OnceC m a)
-> (forall a. OnceC m a -> OnceC m [a])
-> (forall a. OnceC m a -> OnceC m [a])
-> Alternative (OnceC m)
OnceC m a -> OnceC m a -> OnceC m a
OnceC m a -> OnceC m [a]
OnceC m a -> OnceC m [a]
forall a. OnceC m a
forall a. OnceC m a -> OnceC m [a]
forall a. OnceC m a -> OnceC m a -> OnceC m a
forall (m :: * -> *). Applicative (OnceC m)
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 :: * -> *) a. OnceC m a
forall (m :: * -> *) a. OnceC m a -> OnceC m [a]
forall (m :: * -> *) a. OnceC m a -> OnceC m a -> OnceC m a
many :: OnceC m a -> OnceC m [a]
$cmany :: forall (m :: * -> *) a. OnceC m a -> OnceC m [a]
some :: OnceC m a -> OnceC m [a]
$csome :: forall (m :: * -> *) a. OnceC m a -> OnceC m [a]
<|> :: OnceC m a -> OnceC m a -> OnceC m a
$c<|> :: forall (m :: * -> *) a. OnceC m a -> OnceC m a -> OnceC m a
empty :: OnceC m a
$cempty :: forall (m :: * -> *) a. OnceC m a
$cp1Alternative :: forall (m :: * -> *). Applicative (OnceC m)
Alternative, Functor (OnceC m)
a -> OnceC m a
Functor (OnceC m) =>
(forall a. a -> OnceC m a)
-> (forall a b. OnceC m (a -> b) -> OnceC m a -> OnceC m b)
-> (forall a b c.
    (a -> b -> c) -> OnceC m a -> OnceC m b -> OnceC m c)
-> (forall a b. OnceC m a -> OnceC m b -> OnceC m b)
-> (forall a b. OnceC m a -> OnceC m b -> OnceC m a)
-> Applicative (OnceC m)
OnceC m a -> OnceC m b -> OnceC m b
OnceC m a -> OnceC m b -> OnceC m a
OnceC m (a -> b) -> OnceC m a -> OnceC m b
(a -> b -> c) -> OnceC m a -> OnceC m b -> OnceC m c
forall a. a -> OnceC m a
forall a b. OnceC m a -> OnceC m b -> OnceC m a
forall a b. OnceC m a -> OnceC m b -> OnceC m b
forall a b. OnceC m (a -> b) -> OnceC m a -> OnceC m b
forall a b c. (a -> b -> c) -> OnceC m a -> OnceC m b -> OnceC m c
forall (m :: * -> *). Functor (OnceC 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 -> OnceC m a
forall (m :: * -> *) a b. OnceC m a -> OnceC m b -> OnceC m a
forall (m :: * -> *) a b. OnceC m a -> OnceC m b -> OnceC m b
forall (m :: * -> *) a b.
OnceC m (a -> b) -> OnceC m a -> OnceC m b
forall (m :: * -> *) a b c.
(a -> b -> c) -> OnceC m a -> OnceC m b -> OnceC m c
<* :: OnceC m a -> OnceC m b -> OnceC m a
$c<* :: forall (m :: * -> *) a b. OnceC m a -> OnceC m b -> OnceC m a
*> :: OnceC m a -> OnceC m b -> OnceC m b
$c*> :: forall (m :: * -> *) a b. OnceC m a -> OnceC m b -> OnceC m b
liftA2 :: (a -> b -> c) -> OnceC m a -> OnceC m b -> OnceC m c
$cliftA2 :: forall (m :: * -> *) a b c.
(a -> b -> c) -> OnceC m a -> OnceC m b -> OnceC m c
<*> :: OnceC m (a -> b) -> OnceC m a -> OnceC m b
$c<*> :: forall (m :: * -> *) a b.
OnceC m (a -> b) -> OnceC m a -> OnceC m b
pure :: a -> OnceC m a
$cpure :: forall (m :: * -> *) a. a -> OnceC m a
$cp1Applicative :: forall (m :: * -> *). Functor (OnceC m)
Applicative, a -> OnceC m b -> OnceC m a
(a -> b) -> OnceC m a -> OnceC m b
(forall a b. (a -> b) -> OnceC m a -> OnceC m b)
-> (forall a b. a -> OnceC m b -> OnceC m a) -> Functor (OnceC m)
forall a b. a -> OnceC m b -> OnceC m a
forall a b. (a -> b) -> OnceC m a -> OnceC 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 -> OnceC m b -> OnceC m a
forall (m :: * -> *) a b. (a -> b) -> OnceC m a -> OnceC m b
<$ :: a -> OnceC m b -> OnceC m a
$c<$ :: forall (m :: * -> *) a b. a -> OnceC m b -> OnceC m a
fmap :: (a -> b) -> OnceC m a -> OnceC m b
$cfmap :: forall (m :: * -> *) a b. (a -> b) -> OnceC m a -> OnceC m b
Functor, Applicative (OnceC m)
a -> OnceC m a
Applicative (OnceC m) =>
(forall a b. OnceC m a -> (a -> OnceC m b) -> OnceC m b)
-> (forall a b. OnceC m a -> OnceC m b -> OnceC m b)
-> (forall a. a -> OnceC m a)
-> Monad (OnceC m)
OnceC m a -> (a -> OnceC m b) -> OnceC m b
OnceC m a -> OnceC m b -> OnceC m b
forall a. a -> OnceC m a
forall a b. OnceC m a -> OnceC m b -> OnceC m b
forall a b. OnceC m a -> (a -> OnceC m b) -> OnceC m b
forall (m :: * -> *). Applicative (OnceC 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 -> OnceC m a
forall (m :: * -> *) a b. OnceC m a -> OnceC m b -> OnceC m b
forall (m :: * -> *) a b.
OnceC m a -> (a -> OnceC m b) -> OnceC m b
return :: a -> OnceC m a
$creturn :: forall (m :: * -> *) a. a -> OnceC m a
>> :: OnceC m a -> OnceC m b -> OnceC m b
$c>> :: forall (m :: * -> *) a b. OnceC m a -> OnceC m b -> OnceC m b
>>= :: OnceC m a -> (a -> OnceC m b) -> OnceC m b
$c>>= :: forall (m :: * -> *) a b.
OnceC m a -> (a -> OnceC m b) -> OnceC m b
$cp1Monad :: forall (m :: * -> *). Applicative (OnceC m)
Monad, Monad (OnceC m)
Monad (OnceC m) =>
(forall a. String -> OnceC m a) -> MonadFail (OnceC m)
String -> OnceC m a
forall a. String -> OnceC m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
forall (m :: * -> *). MonadFail m => Monad (OnceC m)
forall (m :: * -> *) a. MonadFail m => String -> OnceC m a
fail :: String -> OnceC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> OnceC m a
$cp1MonadFail :: forall (m :: * -> *). MonadFail m => Monad (OnceC m)
Fail.MonadFail, Monad (OnceC m)
Monad (OnceC m) =>
(forall a. (a -> OnceC m a) -> OnceC m a) -> MonadFix (OnceC m)
(a -> OnceC m a) -> OnceC m a
forall a. (a -> OnceC m a) -> OnceC m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
forall (m :: * -> *). MonadFix m => Monad (OnceC m)
forall (m :: * -> *) a. MonadFix m => (a -> OnceC m a) -> OnceC m a
mfix :: (a -> OnceC m a) -> OnceC m a
$cmfix :: forall (m :: * -> *) a. MonadFix m => (a -> OnceC m a) -> OnceC m a
$cp1MonadFix :: forall (m :: * -> *). MonadFix m => Monad (OnceC m)
MonadFix, Monad (OnceC m)
Monad (OnceC m) =>
(forall a. IO a -> OnceC m a) -> MonadIO (OnceC m)
IO a -> OnceC m a
forall a. IO a -> OnceC m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (OnceC m)
forall (m :: * -> *) a. MonadIO m => IO a -> OnceC m a
liftIO :: IO a -> OnceC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> OnceC m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (OnceC m)
MonadIO, Monad (OnceC m)
Alternative (OnceC m)
OnceC m a
(Alternative (OnceC m), Monad (OnceC m)) =>
(forall a. OnceC m a)
-> (forall a. OnceC m a -> OnceC m a -> OnceC m a)
-> MonadPlus (OnceC m)
OnceC m a -> OnceC m a -> OnceC m a
forall a. OnceC m a
forall a. OnceC m a -> OnceC m a -> OnceC m a
forall (m :: * -> *). Monad (OnceC m)
forall (m :: * -> *). Alternative (OnceC m)
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
forall (m :: * -> *) a. OnceC m a
forall (m :: * -> *) a. OnceC m a -> OnceC m a -> OnceC m a
mplus :: OnceC m a -> OnceC m a -> OnceC m a
$cmplus :: forall (m :: * -> *) a. OnceC m a -> OnceC m a -> OnceC m a
mzero :: OnceC m a
$cmzero :: forall (m :: * -> *) a. OnceC m a
$cp2MonadPlus :: forall (m :: * -> *). Monad (OnceC m)
$cp1MonadPlus :: forall (m :: * -> *). Alternative (OnceC m)
MonadPlus)

instance (Carrier sig m, Effect sig) => Carrier (NonDet :+: sig) (OnceC m) where
  eff :: (:+:) NonDet sig (OnceC m) a -> OnceC m a
eff = CullC (NonDetC m) a -> OnceC m a
forall (m :: * -> *) a. CullC (NonDetC m) a -> OnceC m a
OnceC (CullC (NonDetC m) a -> OnceC m a)
-> ((:+:) NonDet sig (OnceC m) a -> CullC (NonDetC m) a)
-> (:+:) NonDet sig (OnceC m) a
-> OnceC m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) Cull (NonDet :+: (NonDet :+: sig)) (CullC (NonDetC m)) a
-> CullC (NonDetC m) a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Carrier sig m =>
sig m a -> m a
eff ((:+:) Cull (NonDet :+: (NonDet :+: sig)) (CullC (NonDetC m)) a
 -> CullC (NonDetC m) a)
-> ((:+:) NonDet sig (OnceC m) a
    -> (:+:) Cull (NonDet :+: (NonDet :+: sig)) (CullC (NonDetC m)) a)
-> (:+:) NonDet sig (OnceC m) a
-> CullC (NonDetC m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) NonDet (NonDet :+: sig) (CullC (NonDetC m)) a
-> (:+:) Cull (NonDet :+: (NonDet :+: sig)) (CullC (NonDetC m)) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R ((:+:) NonDet (NonDet :+: sig) (CullC (NonDetC m)) a
 -> (:+:) Cull (NonDet :+: (NonDet :+: sig)) (CullC (NonDetC m)) a)
-> ((:+:) NonDet sig (OnceC m) a
    -> (:+:) NonDet (NonDet :+: sig) (CullC (NonDetC m)) a)
-> (:+:) NonDet sig (OnceC m) a
-> (:+:) Cull (NonDet :+: (NonDet :+: sig)) (CullC (NonDetC m)) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) NonDet sig (CullC (NonDetC m)) a
-> (:+:) NonDet (NonDet :+: sig) (CullC (NonDetC m)) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R ((:+:) NonDet sig (CullC (NonDetC m)) a
 -> (:+:) NonDet (NonDet :+: sig) (CullC (NonDetC m)) a)
-> ((:+:) NonDet sig (OnceC m) a
    -> (:+:) NonDet sig (CullC (NonDetC m)) a)
-> (:+:) NonDet sig (OnceC m) a
-> (:+:) NonDet (NonDet :+: sig) (CullC (NonDetC m)) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) NonDet sig (OnceC m) a
-> (:+:) NonDet sig (CullC (NonDetC m)) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible
  {-# INLINE eff #-}


-- $setup
-- >>> :seti -XFlexibleContexts
-- >>> import Test.QuickCheck
-- >>> import Control.Effect.NonDet
-- >>> import Control.Effect.Pure
-- >>> import Data.Foldable (asum)