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

-- | A church-encoded carrier for a 'Fresh' effect, providing access to a monotonically increasing stream of 'Int' values.
--
-- @since 1.1.0.0
module Control.Carrier.Fresh.Church
( -- * Fresh carrier
  runFresh
, evalFresh
, FreshC(FreshC)
  -- * Fresh effect
, module Control.Effect.Fresh
) where

import Control.Algebra
import Control.Applicative (Alternative)
import Control.Carrier.State.Church
import Control.Effect.Fresh
import Control.Monad (MonadPlus)
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class

-- | Run a 'Fresh' effect counting up from 0.
--
-- @
-- 'runFresh' k n ('pure' a) = k n a
-- @
-- @
-- 'runFresh' k n 'fresh' = k (n '+' 1) n
-- @
--
-- @since 1.1.0.0
runFresh :: (Int -> a -> m b) -> Int -> FreshC m a -> m b
runFresh :: forall a (m :: * -> *) b.
(Int -> a -> m b) -> Int -> FreshC m a -> m b
runFresh Int -> a -> m b
k Int
n = (Int -> a -> m b) -> Int -> StateC Int m a -> m b
forall s (m :: * -> *) a b.
(s -> a -> m b) -> s -> StateC s m a -> m b
runState Int -> a -> m b
k Int
n (StateC Int m a -> m b)
-> (FreshC m a -> StateC Int m a) -> FreshC m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FreshC m a -> StateC Int m a
forall (m :: * -> *) a. FreshC m a -> StateC Int m a
runFreshC
{-# INLINE runFresh #-}

-- | Run a 'Fresh' effect counting up from an initial value, and forgetting the final value.
--
-- @
-- 'evalFresh' n ('pure' a) = 'pure' a
-- @
-- @
-- 'evalFresh' n 'fresh' = 'pure' n
-- @
--
-- @since 1.1.0.0
evalFresh :: Applicative m => Int -> FreshC m a -> m a
evalFresh :: forall (m :: * -> *) a. Applicative m => Int -> FreshC m a -> m a
evalFresh Int
n = Int -> StateC Int m a -> m a
forall s (m :: * -> *) a. Applicative m => s -> StateC s m a -> m a
evalState Int
n (StateC Int m a -> m a)
-> (FreshC m a -> StateC Int m a) -> FreshC m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FreshC m a -> StateC Int m a
forall (m :: * -> *) a. FreshC m a -> StateC Int m a
runFreshC
{-# INLINE evalFresh #-}

-- | @since 1.1.0.0
newtype FreshC m a = FreshC { forall (m :: * -> *) a. FreshC m a -> StateC Int m a
runFreshC :: StateC Int m a }
  deriving (Applicative (FreshC m)
Applicative (FreshC m) =>
(forall a. FreshC m a)
-> (forall a. FreshC m a -> FreshC m a -> FreshC m a)
-> (forall a. FreshC m a -> FreshC m [a])
-> (forall a. FreshC m a -> FreshC m [a])
-> Alternative (FreshC m)
forall a. FreshC m a
forall a. FreshC m a -> FreshC m [a]
forall a. FreshC m a -> FreshC m a -> FreshC m a
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 :: * -> *). Alternative m => Applicative (FreshC m)
forall (m :: * -> *) a. Alternative m => FreshC m a
forall (m :: * -> *) a. Alternative m => FreshC m a -> FreshC m [a]
forall (m :: * -> *) a.
Alternative m =>
FreshC m a -> FreshC m a -> FreshC m a
$cempty :: forall (m :: * -> *) a. Alternative m => FreshC m a
empty :: forall a. FreshC m a
$c<|> :: forall (m :: * -> *) a.
Alternative m =>
FreshC m a -> FreshC m a -> FreshC m a
<|> :: forall a. FreshC m a -> FreshC m a -> FreshC m a
$csome :: forall (m :: * -> *) a. Alternative m => FreshC m a -> FreshC m [a]
some :: forall a. FreshC m a -> FreshC m [a]
$cmany :: forall (m :: * -> *) a. Alternative m => FreshC m a -> FreshC m [a]
many :: forall a. FreshC m a -> FreshC m [a]
Alternative, Functor (FreshC m)
Functor (FreshC m) =>
(forall a. a -> FreshC m a)
-> (forall a b. FreshC m (a -> b) -> FreshC m a -> FreshC m b)
-> (forall a b c.
    (a -> b -> c) -> FreshC m a -> FreshC m b -> FreshC m c)
-> (forall a b. FreshC m a -> FreshC m b -> FreshC m b)
-> (forall a b. FreshC m a -> FreshC m b -> FreshC m a)
-> Applicative (FreshC m)
forall a. a -> FreshC m a
forall a b. FreshC m a -> FreshC m b -> FreshC m a
forall a b. FreshC m a -> FreshC m b -> FreshC m b
forall a b. FreshC m (a -> b) -> FreshC m a -> FreshC m b
forall a b c.
(a -> b -> c) -> FreshC m a -> FreshC m b -> FreshC m c
forall (m :: * -> *). Functor (FreshC 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 -> FreshC m a
forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m a
forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m b
forall (m :: * -> *) a b.
FreshC m (a -> b) -> FreshC m a -> FreshC m b
forall (m :: * -> *) a b c.
(a -> b -> c) -> FreshC m a -> FreshC m b -> FreshC m c
$cpure :: forall (m :: * -> *) a. a -> FreshC m a
pure :: forall a. a -> FreshC m a
$c<*> :: forall (m :: * -> *) a b.
FreshC m (a -> b) -> FreshC m a -> FreshC m b
<*> :: forall a b. FreshC m (a -> b) -> FreshC m a -> FreshC m b
$cliftA2 :: forall (m :: * -> *) a b c.
(a -> b -> c) -> FreshC m a -> FreshC m b -> FreshC m c
liftA2 :: forall a b c.
(a -> b -> c) -> FreshC m a -> FreshC m b -> FreshC m c
$c*> :: forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m b
*> :: forall a b. FreshC m a -> FreshC m b -> FreshC m b
$c<* :: forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m a
<* :: forall a b. FreshC m a -> FreshC m b -> FreshC m a
Applicative, (forall a b. (a -> b) -> FreshC m a -> FreshC m b)
-> (forall a b. a -> FreshC m b -> FreshC m a)
-> Functor (FreshC m)
forall a b. a -> FreshC m b -> FreshC m a
forall a b. (a -> b) -> FreshC m a -> FreshC 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 -> FreshC m b -> FreshC m a
forall (m :: * -> *) a b. (a -> b) -> FreshC m a -> FreshC m b
$cfmap :: forall (m :: * -> *) a b. (a -> b) -> FreshC m a -> FreshC m b
fmap :: forall a b. (a -> b) -> FreshC m a -> FreshC m b
$c<$ :: forall (m :: * -> *) a b. a -> FreshC m b -> FreshC m a
<$ :: forall a b. a -> FreshC m b -> FreshC m a
Functor, Applicative (FreshC m)
Applicative (FreshC m) =>
(forall a b. FreshC m a -> (a -> FreshC m b) -> FreshC m b)
-> (forall a b. FreshC m a -> FreshC m b -> FreshC m b)
-> (forall a. a -> FreshC m a)
-> Monad (FreshC m)
forall a. a -> FreshC m a
forall a b. FreshC m a -> FreshC m b -> FreshC m b
forall a b. FreshC m a -> (a -> FreshC m b) -> FreshC m b
forall (m :: * -> *). Applicative (FreshC 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 -> FreshC m a
forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m b
forall (m :: * -> *) a b.
FreshC m a -> (a -> FreshC m b) -> FreshC m b
$c>>= :: forall (m :: * -> *) a b.
FreshC m a -> (a -> FreshC m b) -> FreshC m b
>>= :: forall a b. FreshC m a -> (a -> FreshC m b) -> FreshC m b
$c>> :: forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m b
>> :: forall a b. FreshC m a -> FreshC m b -> FreshC m b
$creturn :: forall (m :: * -> *) a. a -> FreshC m a
return :: forall a. a -> FreshC m a
Monad, Monad (FreshC m)
Monad (FreshC m) =>
(forall a. String -> FreshC m a) -> MonadFail (FreshC m)
forall a. String -> FreshC m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
forall (m :: * -> *). MonadFail m => Monad (FreshC m)
forall (m :: * -> *) a. MonadFail m => String -> FreshC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> FreshC m a
fail :: forall a. String -> FreshC m a
Fail.MonadFail, Monad (FreshC m)
Monad (FreshC m) =>
(forall a. (a -> FreshC m a) -> FreshC m a) -> MonadFix (FreshC m)
forall a. (a -> FreshC m a) -> FreshC m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
forall (m :: * -> *). MonadFix m => Monad (FreshC m)
forall (m :: * -> *) a.
MonadFix m =>
(a -> FreshC m a) -> FreshC m a
$cmfix :: forall (m :: * -> *) a.
MonadFix m =>
(a -> FreshC m a) -> FreshC m a
mfix :: forall a. (a -> FreshC m a) -> FreshC m a
MonadFix, Monad (FreshC m)
Monad (FreshC m) =>
(forall a. IO a -> FreshC m a) -> MonadIO (FreshC m)
forall a. IO a -> FreshC m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (FreshC m)
forall (m :: * -> *) a. MonadIO m => IO a -> FreshC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> FreshC m a
liftIO :: forall a. IO a -> FreshC m a
MonadIO, Monad (FreshC m)
Alternative (FreshC m)
(Alternative (FreshC m), Monad (FreshC m)) =>
(forall a. FreshC m a)
-> (forall a. FreshC m a -> FreshC m a -> FreshC m a)
-> MonadPlus (FreshC m)
forall a. FreshC m a
forall a. FreshC m a -> FreshC m a -> FreshC m a
forall (m :: * -> *). (Alternative m, Monad m) => Monad (FreshC m)
forall (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (FreshC m)
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
forall (m :: * -> *) a. (Alternative m, Monad m) => FreshC m a
forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FreshC m a -> FreshC m a -> FreshC m a
$cmzero :: forall (m :: * -> *) a. (Alternative m, Monad m) => FreshC m a
mzero :: forall a. FreshC m a
$cmplus :: forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FreshC m a -> FreshC m a -> FreshC m a
mplus :: forall a. FreshC m a -> FreshC m a -> FreshC m a
MonadPlus, (forall (m :: * -> *). Monad m => Monad (FreshC m)) =>
(forall (m :: * -> *) a. Monad m => m a -> FreshC m a)
-> MonadTrans FreshC
forall (m :: * -> *). Monad m => Monad (FreshC m)
forall (m :: * -> *) a. Monad m => m a -> FreshC m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *). Monad m => Monad (t m)) =>
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
$clift :: forall (m :: * -> *) a. Monad m => m a -> FreshC m a
lift :: forall (m :: * -> *) a. Monad m => m a -> FreshC m a
MonadTrans)

instance Algebra sig m => Algebra (Fresh :+: sig) (FreshC m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (FreshC m)
-> (:+:) Fresh sig n a -> ctx () -> FreshC m (ctx a)
alg Handler ctx n (FreshC m)
hdl (:+:) Fresh sig n a
sig ctx ()
ctx = StateC Int m (ctx a) -> FreshC m (ctx a)
forall (m :: * -> *) a. StateC Int m a -> FreshC m a
FreshC (StateC Int m (ctx a) -> FreshC m (ctx a))
-> StateC Int m (ctx a) -> FreshC m (ctx a)
forall a b. (a -> b) -> a -> b
$ case (:+:) Fresh sig n a
sig of
    L Fresh n a
Fresh -> (a -> (a, ctx a)) -> StateC Int m (ctx a)
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
(s -> (s, a)) -> m a
state ((a -> (a, ctx a)) -> StateC Int m (ctx a))
-> (a -> (a, ctx a)) -> StateC Int m (ctx a)
forall a b. (a -> b) -> a -> b
$ \ a
i -> (a
i a -> a -> a
forall a. Num a => a -> a -> a
+ a
1, a
i 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)
    R sig n a
other -> Handler ctx n (StateC Int m)
-> (:+:) (State Int) sig n a -> ctx () -> StateC Int m (ctx a)
forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (StateC Int m)
-> (:+:) (State Int) sig n a -> ctx () -> StateC Int m (ctx a)
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
       (n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (FreshC m (ctx x) -> StateC Int m (ctx x)
forall (m :: * -> *) a. FreshC m a -> StateC Int m a
runFreshC (FreshC m (ctx x) -> StateC Int m (ctx x))
-> (ctx (n x) -> FreshC m (ctx x))
-> ctx (n x)
-> StateC Int m (ctx x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx (n x) -> FreshC m (ctx x)
Handler ctx n (FreshC m)
hdl) (sig n a -> (:+:) (State Int) sig n a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other) ctx ()
ctx
  {-# INLINE alg #-}