{-# LANGUAGE DerivingVia #-}
module Control.Effect.Fresh
  ( -- * Effects
    Fresh(..)

    -- * Actions
  , fresh

    -- * Interpretations
  , freshToIO

  , runFreshEnumIO

    -- * Unsafe interpretations
  , runFreshEnum

    -- * Simple variants of interpretations
  , runFreshEnumIOSimple

    -- * Threading constraints
  , StateThreads

    -- * Carriers
  , FreshToIOC
  , FreshEnumC
  ) where

import Data.Unique
import Data.IORef

import Control.Effect
import Control.Effect.State

-- For coercion purposes
import Control.Effect.Carrier
import Control.Effect.Internal.Utils
import Control.Effect.Carrier.Internal.Interpret
import Control.Effect.Carrier.Internal.Compose
import Control.Effect.Carrier.Internal.Intro
import Control.Monad.Trans.State.Strict (StateT)


-- | An effect for creating unique objects which may be used as references,
-- a la 'Unique'. Polymorphic code making use of 'Fresh' is expected
-- to place constraints upon @uniq@ as necessary.
--
-- Any interpreter for 'Fresh' has the responsibilty of ensuring
-- that any call to 'fresh' produces an object that __never__
-- compares equal to an object produced by a previous call to 'fresh'.
data Fresh uniq :: Effect where
  Fresh :: Fresh uniq m uniq

fresh :: Eff (Fresh uniq) m => m uniq
fresh :: m uniq
fresh = Fresh uniq m uniq -> m uniq
forall (e :: Effect) (m :: * -> *) a.
(Member e (Derivs m), Carrier m) =>
e m a -> m a
send Fresh uniq m uniq
forall uniq (m :: * -> *). Fresh uniq m uniq
Fresh
{-# INLINE fresh #-}

data FreshToIOH

instance Eff (Embed IO) m
      => Handler FreshToIOH (Fresh Unique) m where
  effHandler :: Fresh Unique (Effly z) x -> Effly z x
effHandler Fresh Unique (Effly z) x
Fresh = IO Unique -> Effly z Unique
forall (b :: * -> *) (m :: * -> *) a. Eff (Embed b) m => b a -> m a
embed IO Unique
newUnique
  {-# INLINEABLE effHandler #-}

type FreshToIOC = InterpretC FreshToIOH (Fresh Unique)

-- | Runs a 'Fresh' effect through generating 'Unique's using 'IO'.
--
-- @'Derivs' ('FreshToIOC' m) = 'Fresh' 'Unique' ': 'Derivs' m@
--
-- @'Control.Effect.Primitive.Prims'  ('FreshToIOC' m) = 'Control.Effect.Primitive.Prims' m@
freshToIO :: Eff (Embed IO) m
          => FreshToIOC m a
          -> m a
freshToIO :: FreshToIOC m a -> m a
freshToIO = FreshToIOC m a -> m a
forall h (e :: Effect) (m :: * -> *) a.
Handler h e m =>
InterpretC h e m a -> m a
interpretViaHandler
{-# INLINE freshToIO #-}

-- | Run a 'Fresh' effect through atomic operations in 'IO'
-- by specifying an 'Enum' to be used as the type of unique objects.
--
-- This is a safe variant of 'runFreshEnum'.
--
-- This has a higher-rank type, as it makes use of 'InterpretReifiedC'.
-- __This makes 'runFreshEnumIO' very difficult to use partially applied.__
-- __In particular, it can't be composed using @'.'@.__
--
-- If performance is secondary, consider using the slower
-- 'runFreshEnumIOSimple', which doesn't have a higher-rank type.
runFreshEnumIO :: forall uniq m a
                . ( Enum uniq
                  , Eff (Embed IO) m
                  )
               => InterpretReifiedC (Fresh uniq) m a
               -> m a
runFreshEnumIO :: InterpretReifiedC (Fresh uniq) m a -> m a
runFreshEnumIO InterpretReifiedC (Fresh uniq) m a
m = do
  IORef uniq
ref <- IO (IORef uniq) -> m (IORef uniq)
forall (b :: * -> *) (m :: * -> *) a. Eff (Embed b) m => b a -> m a
embed (IO (IORef uniq) -> m (IORef uniq))
-> IO (IORef uniq) -> m (IORef uniq)
forall a b. (a -> b) -> a -> b
$ uniq -> IO (IORef uniq)
forall a. a -> IO (IORef a)
newIORef (Int -> uniq
forall a. Enum a => Int -> a
toEnum @uniq Int
0)
  (EffHandler (Fresh uniq) m
-> InterpretReifiedC (Fresh uniq) m a -> m a
forall (e :: Effect) (m :: * -> *) a.
(RepresentationalEff e, Carrier m) =>
EffHandler e m -> InterpretReifiedC e m a -> m a
`interpret` InterpretReifiedC (Fresh uniq) m a
m) (EffHandler (Fresh uniq) m -> m a)
-> EffHandler (Fresh uniq) m -> m a
forall a b. (a -> b) -> a -> b
$ \case
    Fresh uniq (Effly z) x
Fresh -> IO uniq -> Effly z uniq
forall (b :: * -> *) (m :: * -> *) a. Eff (Embed b) m => b a -> m a
embed (IO uniq -> Effly z uniq) -> IO uniq -> Effly z uniq
forall a b. (a -> b) -> a -> b
$ IORef uniq -> (uniq -> (uniq, uniq)) -> IO uniq
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef uniq
ref (\uniq
s -> (uniq -> uniq
forall a. Enum a => a -> a
succ uniq
s, uniq
s))
{-# INLINE runFreshEnumIO #-}

-- | Run a 'Fresh' effect though atomic operations in 'IO'
-- by specifying an 'Enum' to be used as the type of unique objects.
--
-- This is a less performant version of 'runFreshEnumIO' that doesn't have
-- a higher-rank type, making it much easier to use partially applied.
runFreshEnumIOSimple :: forall uniq m a p
                      . ( Enum uniq
                        , Eff (Embed IO) m
                        , Threaders '[ReaderThreads] m p
                        )
                     => InterpretSimpleC (Fresh uniq) m a
                     -> m a
runFreshEnumIOSimple :: InterpretSimpleC (Fresh uniq) m a -> m a
runFreshEnumIOSimple InterpretSimpleC (Fresh uniq) m a
m = do
  IORef uniq
ref <- IO (IORef uniq) -> m (IORef uniq)
forall (b :: * -> *) (m :: * -> *) a. Eff (Embed b) m => b a -> m a
embed (IO (IORef uniq) -> m (IORef uniq))
-> IO (IORef uniq) -> m (IORef uniq)
forall a b. (a -> b) -> a -> b
$ uniq -> IO (IORef uniq)
forall a. a -> IO (IORef a)
newIORef (Int -> uniq
forall a. Enum a => Int -> a
toEnum @uniq Int
0)
  (EffHandler (Fresh uniq) m
-> InterpretSimpleC (Fresh uniq) m a -> m a
forall (e :: Effect) (m :: * -> *) a (p :: [Effect]).
(RepresentationalEff e, Threaders '[ReaderThreads] m p,
 Carrier m) =>
EffHandler e m -> InterpretSimpleC e m a -> m a
`interpretSimple` InterpretSimpleC (Fresh uniq) m a
m) (EffHandler (Fresh uniq) m -> m a)
-> EffHandler (Fresh uniq) m -> m a
forall a b. (a -> b) -> a -> b
$ \case
    Fresh uniq (Effly z) x
Fresh -> IO uniq -> Effly z uniq
forall (b :: * -> *) (m :: * -> *) a. Eff (Embed b) m => b a -> m a
embed (IO uniq -> Effly z uniq) -> IO uniq -> Effly z uniq
forall a b. (a -> b) -> a -> b
$ IORef uniq -> (uniq -> (uniq, uniq)) -> IO uniq
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef uniq
ref (\uniq
s -> (uniq -> uniq
forall a. Enum a => a -> a
succ uniq
s, uniq
s))
{-# INLINE runFreshEnumIOSimple #-}

data FreshEnumH

instance (Enum uniq, Eff (State uniq) m)
      => Handler FreshEnumH (Fresh uniq) m where
  effHandler :: Fresh uniq (Effly z) x -> Effly z x
effHandler Fresh uniq (Effly z) x
Fresh = (x -> (x, x)) -> Effly z x
forall s (m :: * -> *) a. Eff (State s) m => (s -> (s, a)) -> m a
state' (\x
s -> (x -> x
forall a. Enum a => a -> a
succ x
s, x
s))
  {-# INLINEABLE effHandler #-}

newtype FreshEnumC uniq m a = FreshEnumC {
    FreshEnumC uniq m a
-> ReinterpretC
     FreshEnumH (Fresh uniq) '[State uniq] (StateC uniq m) a
unFreshEnumC ::
        ReinterpretC FreshEnumH (Fresh uniq) '[State uniq]
      ( StateC uniq
      ( m
      )) a
  } deriving ( a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
(a -> b) -> FreshEnumC uniq m a -> FreshEnumC uniq m b
(forall a b.
 (a -> b) -> FreshEnumC uniq m a -> FreshEnumC uniq m b)
-> (forall a b. a -> FreshEnumC uniq m b -> FreshEnumC uniq m a)
-> Functor (FreshEnumC uniq m)
forall a b. a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
forall a b. (a -> b) -> FreshEnumC uniq m a -> FreshEnumC uniq m b
forall uniq (m :: * -> *) a b.
Functor m =>
a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
forall uniq (m :: * -> *) a b.
Functor m =>
(a -> b) -> FreshEnumC uniq m a -> FreshEnumC uniq m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
$c<$ :: forall uniq (m :: * -> *) a b.
Functor m =>
a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
fmap :: (a -> b) -> FreshEnumC uniq m a -> FreshEnumC uniq m b
$cfmap :: forall uniq (m :: * -> *) a b.
Functor m =>
(a -> b) -> FreshEnumC uniq m a -> FreshEnumC uniq m b
Functor, Functor (FreshEnumC uniq m)
a -> FreshEnumC uniq m a
Functor (FreshEnumC uniq m)
-> (forall a. a -> FreshEnumC uniq m a)
-> (forall a b.
    FreshEnumC uniq m (a -> b)
    -> FreshEnumC uniq m a -> FreshEnumC uniq m b)
-> (forall a b c.
    (a -> b -> c)
    -> FreshEnumC uniq m a
    -> FreshEnumC uniq m b
    -> FreshEnumC uniq m c)
-> (forall a b.
    FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b)
-> (forall a b.
    FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m a)
-> Applicative (FreshEnumC uniq m)
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
FreshEnumC uniq m (a -> b)
-> FreshEnumC uniq m a -> FreshEnumC uniq m b
(a -> b -> c)
-> FreshEnumC uniq m a
-> FreshEnumC uniq m b
-> FreshEnumC uniq m c
forall a. a -> FreshEnumC uniq m a
forall a b.
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
forall a b.
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
forall a b.
FreshEnumC uniq m (a -> b)
-> FreshEnumC uniq m a -> FreshEnumC uniq m b
forall a b c.
(a -> b -> c)
-> FreshEnumC uniq m a
-> FreshEnumC uniq m b
-> FreshEnumC uniq m c
forall uniq (m :: * -> *). Monad m => Functor (FreshEnumC uniq m)
forall uniq (m :: * -> *) a. Monad m => a -> FreshEnumC uniq m a
forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m (a -> b)
-> FreshEnumC uniq m a -> FreshEnumC uniq m b
forall uniq (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> FreshEnumC uniq m a
-> FreshEnumC uniq m b
-> FreshEnumC uniq 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
<* :: FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
$c<* :: forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m a
*> :: FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
$c*> :: forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
liftA2 :: (a -> b -> c)
-> FreshEnumC uniq m a
-> FreshEnumC uniq m b
-> FreshEnumC uniq m c
$cliftA2 :: forall uniq (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> FreshEnumC uniq m a
-> FreshEnumC uniq m b
-> FreshEnumC uniq m c
<*> :: FreshEnumC uniq m (a -> b)
-> FreshEnumC uniq m a -> FreshEnumC uniq m b
$c<*> :: forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m (a -> b)
-> FreshEnumC uniq m a -> FreshEnumC uniq m b
pure :: a -> FreshEnumC uniq m a
$cpure :: forall uniq (m :: * -> *) a. Monad m => a -> FreshEnumC uniq m a
$cp1Applicative :: forall uniq (m :: * -> *). Monad m => Functor (FreshEnumC uniq m)
Applicative, Applicative (FreshEnumC uniq m)
a -> FreshEnumC uniq m a
Applicative (FreshEnumC uniq m)
-> (forall a b.
    FreshEnumC uniq m a
    -> (a -> FreshEnumC uniq m b) -> FreshEnumC uniq m b)
-> (forall a b.
    FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b)
-> (forall a. a -> FreshEnumC uniq m a)
-> Monad (FreshEnumC uniq m)
FreshEnumC uniq m a
-> (a -> FreshEnumC uniq m b) -> FreshEnumC uniq m b
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
forall a. a -> FreshEnumC uniq m a
forall a b.
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
forall a b.
FreshEnumC uniq m a
-> (a -> FreshEnumC uniq m b) -> FreshEnumC uniq m b
forall uniq (m :: * -> *).
Monad m =>
Applicative (FreshEnumC uniq m)
forall uniq (m :: * -> *) a. Monad m => a -> FreshEnumC uniq m a
forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m a
-> (a -> FreshEnumC uniq m b) -> FreshEnumC uniq 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 -> FreshEnumC uniq m a
$creturn :: forall uniq (m :: * -> *) a. Monad m => a -> FreshEnumC uniq m a
>> :: FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
$c>> :: forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m a -> FreshEnumC uniq m b -> FreshEnumC uniq m b
>>= :: FreshEnumC uniq m a
-> (a -> FreshEnumC uniq m b) -> FreshEnumC uniq m b
$c>>= :: forall uniq (m :: * -> *) a b.
Monad m =>
FreshEnumC uniq m a
-> (a -> FreshEnumC uniq m b) -> FreshEnumC uniq m b
$cp1Monad :: forall uniq (m :: * -> *).
Monad m =>
Applicative (FreshEnumC uniq m)
Monad
             , Applicative (FreshEnumC uniq m)
FreshEnumC uniq m a
Applicative (FreshEnumC uniq m)
-> (forall a. FreshEnumC uniq m a)
-> (forall a.
    FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a)
-> (forall a. FreshEnumC uniq m a -> FreshEnumC uniq m [a])
-> (forall a. FreshEnumC uniq m a -> FreshEnumC uniq m [a])
-> Alternative (FreshEnumC uniq m)
FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
FreshEnumC uniq m a -> FreshEnumC uniq m [a]
FreshEnumC uniq m a -> FreshEnumC uniq m [a]
forall a. FreshEnumC uniq m a
forall a. FreshEnumC uniq m a -> FreshEnumC uniq m [a]
forall a.
FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
forall uniq (m :: * -> *).
MonadPlus m =>
Applicative (FreshEnumC uniq m)
forall uniq (m :: * -> *) a. MonadPlus m => FreshEnumC uniq m a
forall uniq (m :: * -> *) a.
MonadPlus m =>
FreshEnumC uniq m a -> FreshEnumC uniq m [a]
forall uniq (m :: * -> *) a.
MonadPlus m =>
FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq 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
many :: FreshEnumC uniq m a -> FreshEnumC uniq m [a]
$cmany :: forall uniq (m :: * -> *) a.
MonadPlus m =>
FreshEnumC uniq m a -> FreshEnumC uniq m [a]
some :: FreshEnumC uniq m a -> FreshEnumC uniq m [a]
$csome :: forall uniq (m :: * -> *) a.
MonadPlus m =>
FreshEnumC uniq m a -> FreshEnumC uniq m [a]
<|> :: FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
$c<|> :: forall uniq (m :: * -> *) a.
MonadPlus m =>
FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
empty :: FreshEnumC uniq m a
$cempty :: forall uniq (m :: * -> *) a. MonadPlus m => FreshEnumC uniq m a
$cp1Alternative :: forall uniq (m :: * -> *).
MonadPlus m =>
Applicative (FreshEnumC uniq m)
Alternative, Monad (FreshEnumC uniq m)
Alternative (FreshEnumC uniq m)
FreshEnumC uniq m a
Alternative (FreshEnumC uniq m)
-> Monad (FreshEnumC uniq m)
-> (forall a. FreshEnumC uniq m a)
-> (forall a.
    FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a)
-> MonadPlus (FreshEnumC uniq m)
FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
forall a. FreshEnumC uniq m a
forall a.
FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
forall uniq (m :: * -> *). MonadPlus m => Monad (FreshEnumC uniq m)
forall uniq (m :: * -> *).
MonadPlus m =>
Alternative (FreshEnumC uniq m)
forall uniq (m :: * -> *) a. MonadPlus m => FreshEnumC uniq m a
forall uniq (m :: * -> *) a.
MonadPlus m =>
FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
forall (m :: * -> *).
Alternative m
-> Monad m
-> (forall a. m a)
-> (forall a. m a -> m a -> m a)
-> MonadPlus m
mplus :: FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
$cmplus :: forall uniq (m :: * -> *) a.
MonadPlus m =>
FreshEnumC uniq m a -> FreshEnumC uniq m a -> FreshEnumC uniq m a
mzero :: FreshEnumC uniq m a
$cmzero :: forall uniq (m :: * -> *) a. MonadPlus m => FreshEnumC uniq m a
$cp2MonadPlus :: forall uniq (m :: * -> *). MonadPlus m => Monad (FreshEnumC uniq m)
$cp1MonadPlus :: forall uniq (m :: * -> *).
MonadPlus m =>
Alternative (FreshEnumC uniq m)
MonadPlus
             , Monad (FreshEnumC uniq m)
Monad (FreshEnumC uniq m)
-> (forall a. (a -> FreshEnumC uniq m a) -> FreshEnumC uniq m a)
-> MonadFix (FreshEnumC uniq m)
(a -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
forall a. (a -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
forall uniq (m :: * -> *). MonadFix m => Monad (FreshEnumC uniq m)
forall uniq (m :: * -> *) a.
MonadFix m =>
(a -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
forall (m :: * -> *).
Monad m -> (forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: (a -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
$cmfix :: forall uniq (m :: * -> *) a.
MonadFix m =>
(a -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
$cp1MonadFix :: forall uniq (m :: * -> *). MonadFix m => Monad (FreshEnumC uniq m)
MonadFix, Monad (FreshEnumC uniq m)
Monad (FreshEnumC uniq m)
-> (forall a. String -> FreshEnumC uniq m a)
-> MonadFail (FreshEnumC uniq m)
String -> FreshEnumC uniq m a
forall a. String -> FreshEnumC uniq m a
forall uniq (m :: * -> *). MonadFail m => Monad (FreshEnumC uniq m)
forall uniq (m :: * -> *) a.
MonadFail m =>
String -> FreshEnumC uniq m a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: String -> FreshEnumC uniq m a
$cfail :: forall uniq (m :: * -> *) a.
MonadFail m =>
String -> FreshEnumC uniq m a
$cp1MonadFail :: forall uniq (m :: * -> *). MonadFail m => Monad (FreshEnumC uniq m)
MonadFail, Monad (FreshEnumC uniq m)
Monad (FreshEnumC uniq m)
-> (forall a. IO a -> FreshEnumC uniq m a)
-> MonadIO (FreshEnumC uniq m)
IO a -> FreshEnumC uniq m a
forall a. IO a -> FreshEnumC uniq m a
forall uniq (m :: * -> *). MonadIO m => Monad (FreshEnumC uniq m)
forall uniq (m :: * -> *) a.
MonadIO m =>
IO a -> FreshEnumC uniq m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: IO a -> FreshEnumC uniq m a
$cliftIO :: forall uniq (m :: * -> *) a.
MonadIO m =>
IO a -> FreshEnumC uniq m a
$cp1MonadIO :: forall uniq (m :: * -> *). MonadIO m => Monad (FreshEnumC uniq m)
MonadIO
             , Monad (FreshEnumC uniq m)
e -> FreshEnumC uniq m a
Monad (FreshEnumC uniq m)
-> (forall e a. Exception e => e -> FreshEnumC uniq m a)
-> MonadThrow (FreshEnumC uniq m)
forall e a. Exception e => e -> FreshEnumC uniq m a
forall uniq (m :: * -> *).
MonadThrow m =>
Monad (FreshEnumC uniq m)
forall uniq (m :: * -> *) e a.
(MonadThrow m, Exception e) =>
e -> FreshEnumC uniq m a
forall (m :: * -> *).
Monad m -> (forall e a. Exception e => e -> m a) -> MonadThrow m
throwM :: e -> FreshEnumC uniq m a
$cthrowM :: forall uniq (m :: * -> *) e a.
(MonadThrow m, Exception e) =>
e -> FreshEnumC uniq m a
$cp1MonadThrow :: forall uniq (m :: * -> *).
MonadThrow m =>
Monad (FreshEnumC uniq m)
MonadThrow, MonadThrow (FreshEnumC uniq m)
MonadThrow (FreshEnumC uniq m)
-> (forall e a.
    Exception e =>
    FreshEnumC uniq m a
    -> (e -> FreshEnumC uniq m a) -> FreshEnumC uniq m a)
-> MonadCatch (FreshEnumC uniq m)
FreshEnumC uniq m a
-> (e -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
forall e a.
Exception e =>
FreshEnumC uniq m a
-> (e -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
forall uniq (m :: * -> *).
MonadCatch m =>
MonadThrow (FreshEnumC uniq m)
forall uniq (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
FreshEnumC uniq m a
-> (e -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
forall (m :: * -> *).
MonadThrow m
-> (forall e a. Exception e => m a -> (e -> m a) -> m a)
-> MonadCatch m
catch :: FreshEnumC uniq m a
-> (e -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
$ccatch :: forall uniq (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
FreshEnumC uniq m a
-> (e -> FreshEnumC uniq m a) -> FreshEnumC uniq m a
$cp1MonadCatch :: forall uniq (m :: * -> *).
MonadCatch m =>
MonadThrow (FreshEnumC uniq m)
MonadCatch, MonadCatch (FreshEnumC uniq m)
MonadCatch (FreshEnumC uniq m)
-> (forall b.
    ((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
     -> FreshEnumC uniq m b)
    -> FreshEnumC uniq m b)
-> (forall b.
    ((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
     -> FreshEnumC uniq m b)
    -> FreshEnumC uniq m b)
-> (forall a b c.
    FreshEnumC uniq m a
    -> (a -> ExitCase b -> FreshEnumC uniq m c)
    -> (a -> FreshEnumC uniq m b)
    -> FreshEnumC uniq m (b, c))
-> MonadMask (FreshEnumC uniq m)
FreshEnumC uniq m a
-> (a -> ExitCase b -> FreshEnumC uniq m c)
-> (a -> FreshEnumC uniq m b)
-> FreshEnumC uniq m (b, c)
((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
 -> FreshEnumC uniq m b)
-> FreshEnumC uniq m b
((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
 -> FreshEnumC uniq m b)
-> FreshEnumC uniq m b
forall b.
((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
 -> FreshEnumC uniq m b)
-> FreshEnumC uniq m b
forall a b c.
FreshEnumC uniq m a
-> (a -> ExitCase b -> FreshEnumC uniq m c)
-> (a -> FreshEnumC uniq m b)
-> FreshEnumC uniq m (b, c)
forall uniq (m :: * -> *).
MonadMask m =>
MonadCatch (FreshEnumC uniq m)
forall uniq (m :: * -> *) b.
MonadMask m =>
((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
 -> FreshEnumC uniq m b)
-> FreshEnumC uniq m b
forall uniq (m :: * -> *) a b c.
MonadMask m =>
FreshEnumC uniq m a
-> (a -> ExitCase b -> FreshEnumC uniq m c)
-> (a -> FreshEnumC uniq m b)
-> FreshEnumC uniq m (b, c)
forall (m :: * -> *).
MonadCatch m
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> (forall a b c.
    m a -> (a -> ExitCase b -> m c) -> (a -> m b) -> m (b, c))
-> MonadMask m
generalBracket :: FreshEnumC uniq m a
-> (a -> ExitCase b -> FreshEnumC uniq m c)
-> (a -> FreshEnumC uniq m b)
-> FreshEnumC uniq m (b, c)
$cgeneralBracket :: forall uniq (m :: * -> *) a b c.
MonadMask m =>
FreshEnumC uniq m a
-> (a -> ExitCase b -> FreshEnumC uniq m c)
-> (a -> FreshEnumC uniq m b)
-> FreshEnumC uniq m (b, c)
uninterruptibleMask :: ((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
 -> FreshEnumC uniq m b)
-> FreshEnumC uniq m b
$cuninterruptibleMask :: forall uniq (m :: * -> *) b.
MonadMask m =>
((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
 -> FreshEnumC uniq m b)
-> FreshEnumC uniq m b
mask :: ((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
 -> FreshEnumC uniq m b)
-> FreshEnumC uniq m b
$cmask :: forall uniq (m :: * -> *) b.
MonadMask m =>
((forall a. FreshEnumC uniq m a -> FreshEnumC uniq m a)
 -> FreshEnumC uniq m b)
-> FreshEnumC uniq m b
$cp1MonadMask :: forall uniq (m :: * -> *).
MonadMask m =>
MonadCatch (FreshEnumC uniq m)
MonadMask
             , MonadBase b, MonadBaseControl b
             )
    deriving (m a -> FreshEnumC uniq m a
(forall (m :: * -> *) a. Monad m => m a -> FreshEnumC uniq m a)
-> MonadTrans (FreshEnumC uniq)
forall uniq (m :: * -> *) a. Monad m => m a -> FreshEnumC uniq m a
forall (m :: * -> *) a. Monad m => m a -> FreshEnumC uniq m a
forall (t :: Effect).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> FreshEnumC uniq m a
$clift :: forall uniq (m :: * -> *) a. Monad m => m a -> FreshEnumC uniq m a
MonadTrans, MonadTrans (FreshEnumC uniq)
m (StT (FreshEnumC uniq) a) -> FreshEnumC uniq m a
MonadTrans (FreshEnumC uniq)
-> (forall (m :: * -> *) a.
    Monad m =>
    (Run (FreshEnumC uniq) -> m a) -> FreshEnumC uniq m a)
-> (forall (m :: * -> *) a.
    Monad m =>
    m (StT (FreshEnumC uniq) a) -> FreshEnumC uniq m a)
-> MonadTransControl (FreshEnumC uniq)
(Run (FreshEnumC uniq) -> m a) -> FreshEnumC uniq m a
forall uniq. MonadTrans (FreshEnumC uniq)
forall uniq (m :: * -> *) a.
Monad m =>
m (StT (FreshEnumC uniq) a) -> FreshEnumC uniq m a
forall uniq (m :: * -> *) a.
Monad m =>
(Run (FreshEnumC uniq) -> m a) -> FreshEnumC uniq m a
forall (m :: * -> *) a.
Monad m =>
m (StT (FreshEnumC uniq) a) -> FreshEnumC uniq m a
forall (m :: * -> *) a.
Monad m =>
(Run (FreshEnumC uniq) -> m a) -> FreshEnumC uniq m a
forall (t :: Effect).
MonadTrans t
-> (forall (m :: * -> *) a. Monad m => (Run t -> m a) -> t m a)
-> (forall (m :: * -> *) a. Monad m => m (StT t a) -> t m a)
-> MonadTransControl t
restoreT :: m (StT (FreshEnumC uniq) a) -> FreshEnumC uniq m a
$crestoreT :: forall uniq (m :: * -> *) a.
Monad m =>
m (StT (FreshEnumC uniq) a) -> FreshEnumC uniq m a
liftWith :: (Run (FreshEnumC uniq) -> m a) -> FreshEnumC uniq m a
$cliftWith :: forall uniq (m :: * -> *) a.
Monad m =>
(Run (FreshEnumC uniq) -> m a) -> FreshEnumC uniq m a
$cp1MonadTransControl :: forall uniq. MonadTrans (FreshEnumC uniq)
MonadTransControl)
    via CompositionBaseT
     '[ ReinterpretC FreshEnumH (Fresh uniq) '[State uniq]
      , StateC uniq
      ]

deriving instance (Carrier m, Enum uniq, Threads (StateT uniq) (Prims m))
               => Carrier (FreshEnumC uniq m)

-- | Run a 'Fresh' effect purely by specifying an 'Enum' to be used as the
-- type of unique objects.
--
-- __Beware:__ This is safe only if:
--
--   1. This is run after all interpreters which may revert local state
--      or produce multiple, inconsistent instances of local state.
--      This includes interpreters that may backtrack or produce multiple results
--      (such as 'Control.Error.Error.runError' or 'Control.Effect.NonDet.runNonDet').
--
--   2. You don't use any interpreter which may cause the final monad
--      to revert local state or produce multiple, inconsistent instances
--      of local state. This includes 'Control.Effect.Error.errorToIO' and
--      'Control.Effect.Conc.asyncToIO'.
--
-- Prefer 'freshToIO' or 'runFreshEnumIO' whenever possible.
--
-- @'Derivs' ('FreshEnumC' uniq m) = 'Fresh' uniq ': 'Derivs' m@
--
-- @'Control.Effect.Primitive.Prims'  ('FreshEnumC' uniq m) = 'Control.Effect.Primitive.Prims' m@
runFreshEnum :: forall uniq m a p
              . ( Enum uniq
                , Threaders '[StateThreads] m p
                , Carrier m
                )
             => FreshEnumC uniq m a
             -> m a
runFreshEnum :: FreshEnumC uniq m a -> m a
runFreshEnum =
     uniq -> StateC uniq m a -> m a
forall s (m :: * -> *) a (p :: [Effect]).
(Carrier m, Threaders '[StateThreads] m p) =>
s -> StateC s m a -> m a
evalState (Int -> uniq
forall a. Enum a => Int -> a
toEnum Int
0)
  (StateC uniq m a -> m a)
-> (ReinterpretC
      FreshEnumH (Fresh uniq) '[State uniq] (StateC uniq m) a
    -> StateC uniq m a)
-> ReinterpretC
     FreshEnumH (Fresh uniq) '[State uniq] (StateC uniq m) a
-> m a
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# ReinterpretC
  FreshEnumH (Fresh uniq) '[State uniq] (StateC uniq m) a
-> StateC uniq m a
forall h (e :: Effect) (new :: [Effect]) (m :: * -> *) a.
(Handler h e m, KnownList new, HeadEffs new m) =>
ReinterpretC h e new m a -> m a
reinterpretViaHandler
  (ReinterpretC
   FreshEnumH (Fresh uniq) '[State uniq] (StateC uniq m) a
 -> m a)
-> (FreshEnumC uniq m a
    -> ReinterpretC
         FreshEnumH (Fresh uniq) '[State uniq] (StateC uniq m) a)
-> FreshEnumC uniq m a
-> m a
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# FreshEnumC uniq m a
-> ReinterpretC
     FreshEnumH (Fresh uniq) '[State uniq] (StateC uniq m) a
forall uniq (m :: * -> *) a.
FreshEnumC uniq m a
-> ReinterpretC
     FreshEnumH (Fresh uniq) '[State uniq] (StateC uniq m) a
unFreshEnumC
{-# INLINE runFreshEnum #-}