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

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

import Control.Algebra
import Control.Applicative (Alternative(..))
import Control.Carrier.State.Strict
import Control.Effect.Fresh
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

-- | Run a 'Fresh' effect counting up from 0.
--
-- @
-- 'runFresh' n ('pure' a) = 'pure' (n, a)
-- @
-- @
-- 'runFresh' n 'fresh' = 'pure' (n '+' 1, n)
-- @
--
-- @since 0.1.0.0
runFresh :: Int -> FreshC m a -> m (Int, a)
runFresh :: Int -> FreshC m a -> m (Int, a)
runFresh n :: Int
n (FreshC m :: StateC Int m a
m) = Int -> StateC Int m a -> m (Int, a)
forall s (m :: * -> *) a. s -> StateC s m a -> m (s, a)
runState Int
n StateC Int m a
m

-- | 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.0.0.0
evalFresh :: Functor m => Int -> FreshC m a -> m a
evalFresh :: Int -> FreshC m a -> m a
evalFresh n :: Int
n (FreshC m :: StateC Int m a
m) = Int -> StateC Int m a -> m a
forall s (m :: * -> *) a. Functor m => s -> StateC s m a -> m a
evalState Int
n StateC Int m a
m

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

instance (Algebra sig m, Effect sig) => Algebra (Fresh :+: sig) (FreshC m) where
  alg :: (:+:) Fresh sig (FreshC m) a -> FreshC m a
alg (L (Fresh k :: Int -> FreshC m a
k)) = StateC Int m Int -> FreshC m Int
forall (m :: * -> *) a. StateC Int m a -> FreshC m a
FreshC (StateC Int m Int
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
m s
get StateC Int m Int -> StateC Int m () -> StateC Int m Int
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (Int -> Int) -> StateC Int m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
(s -> s) -> m ()
modify (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (1 :: Int))) FreshC m Int -> (Int -> FreshC m a) -> FreshC m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> FreshC m a
k
  alg (R other :: sig (FreshC m) a
other)     = StateC Int m a -> FreshC m a
forall (m :: * -> *) a. StateC Int m a -> FreshC m a
FreshC ((:+:) (State Int) sig (StateC Int m) a -> StateC Int m a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Algebra sig m =>
sig m a -> m a
alg (sig (StateC Int m) a -> (:+:) (State Int) sig (StateC Int m) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R (sig (FreshC m) a -> sig (StateC Int m) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible sig (FreshC m) a
other)))
  {-# INLINE alg #-}