{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Carrier.Fresh.Strict
(
runFresh
, evalFresh
, FreshC(FreshC)
, 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 Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
runFresh :: Int -> FreshC m a -> m (Int, a)
runFresh :: forall (m :: * -> *) a. Int -> FreshC m a -> m (Int, a)
runFresh Int
n (FreshC StateC Int m a
m) = forall s (m :: * -> *) a. s -> StateC s m a -> m (s, a)
runState Int
n StateC Int m a
m
{-# INLINE runFresh #-}
evalFresh :: Functor m => Int -> FreshC m a -> m a
evalFresh :: forall (m :: * -> *) a. Functor m => Int -> FreshC m a -> m a
evalFresh Int
n (FreshC StateC Int m a
m) = forall s (m :: * -> *) a. Functor m => s -> StateC s m a -> m a
evalState Int
n StateC Int m a
m
{-# INLINE evalFresh #-}
newtype FreshC m a = FreshC { forall (m :: * -> *) a. FreshC m a -> StateC Int m a
runFreshC :: StateC Int m a }
deriving (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 :: forall a. FreshC m a -> FreshC m [a]
$cmany :: forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FreshC m a -> FreshC m [a]
some :: forall a. FreshC m a -> FreshC m [a]
$csome :: forall (m :: * -> *) a.
(Alternative m, Monad m) =>
FreshC m a -> FreshC m [a]
<|> :: forall 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 :: forall a. FreshC m a
$cempty :: forall (m :: * -> *) a. (Alternative m, Monad m) => FreshC m a
Alternative, 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
<* :: forall a b. 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
*> :: forall a b. 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 :: forall a b c.
(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
<*> :: forall a b. 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 :: forall a. a -> FreshC m a
$cpure :: forall (m :: * -> *) a. Monad m => a -> FreshC m a
Applicative, 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
<$ :: forall a b. a -> FreshC m b -> FreshC m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> FreshC m b -> FreshC m a
fmap :: forall a b. (a -> b) -> FreshC m a -> FreshC m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> FreshC m a -> FreshC m b
Functor, 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 :: forall a. a -> FreshC m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> FreshC m a
>> :: forall a b. 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
>>= :: forall a 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
Monad, 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 :: forall a. String -> FreshC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> FreshC m a
Fail.MonadFail, 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 :: forall a. (a -> FreshC m a) -> FreshC m a
$cmfix :: forall (m :: * -> *) a.
MonadFix m =>
(a -> FreshC m a) -> FreshC m a
MonadFix, 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 :: forall a. IO a -> FreshC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> FreshC m a
MonadIO, 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 :: forall a. 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 :: forall a. FreshC m a
$cmzero :: forall (m :: * -> *) a. (Alternative m, Monad m) => FreshC m a
MonadPlus, 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 :: forall (m :: * -> *) a. Monad m => m a -> FreshC m a
$clift :: 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 = forall (m :: * -> *) a. StateC Int m a -> FreshC m a
FreshC forall a b. (a -> b) -> a -> b
$ case (:+:) Fresh sig n a
sig of
L Fresh n a
Fresh -> forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
(s -> (s, a)) -> m a
state forall a b. (a -> b) -> a -> b
$ \ a
i -> (a
i forall a. Num a => a -> a -> a
+ a
1, a
i forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx)
R sig n a
other -> forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
(n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (forall (m :: * -> *) a. FreshC m a -> StateC Int m a
runFreshC forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handler ctx n (FreshC m)
hdl) (forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other) ctx ()
ctx
{-# INLINE alg #-}