{-# LANGUAGE Trustworthy #-}
module Cleff.Fresh
(
Fresh (..)
, fresh
, freshIntToState
, freshEnumToState
, runFreshAtomicCounter
, runFreshUnique
) where
import Cleff
import Cleff.Internal.Base (thisIsPureTrustMe)
import Cleff.State
import Data.Atomics.Counter (incrCounter, newCounter)
import Data.Unique (Unique, newUnique)
data Fresh u :: Effect where
Fresh :: Fresh u m u
makeEffect_ ''Fresh
fresh :: Fresh u :> es => Eff es u
freshEnumToState :: Enum a => Eff (Fresh a ': es) ~> Eff (State a ': es)
freshEnumToState :: Eff (Fresh a : es) ~> Eff (State a : es)
freshEnumToState = Handler (Fresh a) (State a : es)
-> Eff (Fresh a : es) ~> Eff (State a : es)
forall (e' :: (Type -> Type) -> Type -> Type)
(e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
Handler e (e' : es) -> Eff (e : es) ~> Eff (e' : es)
reinterpret \case
Fresh a (Eff esSend) a
Fresh -> (a -> (a, a)) -> Eff (State a : es) a
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> (a, s)) -> Eff es a
state \a
s -> (a
s, a -> a
forall a. Enum a => a -> a
succ a
s)
{-# INLINE freshEnumToState #-}
freshIntToState :: Eff (Fresh Int ': es) ~> Eff (State Int ': es)
freshIntToState :: Eff (Fresh Int : es) a -> Eff (State Int : es) a
freshIntToState = Eff (Fresh Int : es) a -> Eff (State Int : es) a
forall a (es :: [(Type -> Type) -> Type -> Type]).
Enum a =>
Eff (Fresh a : es) ~> Eff (State a : es)
freshEnumToState
{-# INLINE freshIntToState #-}
runFreshAtomicCounter :: Eff (Fresh Int ': es) ~> Eff es
runFreshAtomicCounter :: Eff (Fresh Int : es) a -> Eff es a
runFreshAtomicCounter Eff (Fresh Int : es) a
m = Eff (IOE : es) a -> Eff es a
forall (es :: [(Type -> Type) -> Type -> Type]).
Eff (IOE : es) ~> Eff es
thisIsPureTrustMe do
AtomicCounter
counter <- IO AtomicCounter -> Eff (IOE : es) AtomicCounter
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (IO AtomicCounter -> Eff (IOE : es) AtomicCounter)
-> IO AtomicCounter -> Eff (IOE : es) AtomicCounter
forall a b. (a -> b) -> a -> b
$ Int -> IO AtomicCounter
newCounter Int
forall a. Bounded a => a
minBound
Handler (Fresh Int) (IOE : es)
-> Eff (Fresh Int : es) a -> Eff (IOE : es) a
forall (e' :: (Type -> Type) -> Type -> Type)
(e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
Handler e (e' : es) -> Eff (e : es) ~> Eff (e' : es)
reinterpret (\case
Fresh Int (Eff esSend) a
Fresh -> IO Int -> Eff (IOE : es) Int
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (IO Int -> Eff (IOE : es) Int) -> IO Int -> Eff (IOE : es) Int
forall a b. (a -> b) -> a -> b
$ Int -> AtomicCounter -> IO Int
incrCounter Int
1 AtomicCounter
counter) Eff (Fresh Int : es) a
m
{-# INLINE runFreshAtomicCounter #-}
runFreshUnique :: IOE :> es => Eff (Fresh Unique ': es) ~> Eff es
runFreshUnique :: Eff (Fresh Unique : es) ~> Eff es
runFreshUnique = Handler (Fresh Unique) es -> Eff (Fresh Unique : es) ~> Eff es
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
Handler e es -> Eff (e : es) ~> Eff es
interpret \case
Fresh Unique (Eff esSend) a
Fresh -> IO Unique -> Eff es Unique
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO IO Unique
newUnique
{-# INLINE runFreshUnique #-}