{-# 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) = 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
{-# 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) = 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
{-# INLINE evalFresh #-}
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, 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, 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 :: * -> *}. 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 -> 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
<$ :: 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, 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 :: * -> *). 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, 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
fail :: forall a. String -> FreshC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => 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
mfix :: forall a. (a -> FreshC m a) -> FreshC m a
$cmfix :: forall (m :: * -> *) a.
MonadFix m =>
(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
liftIO :: forall a. IO a -> FreshC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => 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
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)
-> 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 :: 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 = 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 (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 (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 #-}