{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Carrier.Fresh.Church
(
runFresh
, evalFresh
, FreshC(FreshC)
, module Control.Effect.Fresh
) where
import Control.Algebra
import Control.Applicative (Alternative)
import Control.Carrier.State.Church
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 -> a -> m b) -> Int -> FreshC m a -> m b
runFresh :: (Int -> a -> m b) -> Int -> FreshC m a -> m b
runFresh Int -> a -> m b
k Int
n = (Int -> a -> m b) -> Int -> StateC Int m a -> m b
forall s (m :: * -> *) a b.
(s -> a -> m b) -> s -> StateC s m a -> m b
runState Int -> a -> m b
k Int
n (StateC Int m a -> m b)
-> (FreshC m a -> StateC Int m a) -> FreshC m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FreshC m a -> StateC Int m a
forall (m :: * -> *) a. FreshC m a -> StateC Int m a
runFreshC
{-# INLINE runFresh #-}
evalFresh :: Applicative m => Int -> FreshC m a -> m a
evalFresh :: Int -> FreshC m a -> m a
evalFresh Int
n = Int -> StateC Int m a -> m a
forall s (m :: * -> *) a. Applicative m => s -> StateC s m a -> m a
evalState Int
n (StateC Int m a -> m a)
-> (FreshC m a -> StateC Int m a) -> FreshC m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FreshC m a -> StateC Int m a
forall (m :: * -> *) a. FreshC m a -> StateC Int m a
runFreshC
{-# INLINE evalFresh #-}
newtype FreshC m a = FreshC { FreshC m a -> StateC Int m a
runFreshC :: 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 => Applicative (FreshC m)
forall (m :: * -> *) a. Alternative m => FreshC m a
forall (m :: * -> *) a. Alternative m => FreshC m a -> FreshC m [a]
forall (m :: * -> *) a.
Alternative m =>
FreshC m a -> FreshC m a -> FreshC m a
many :: FreshC m a -> FreshC m [a]
$cmany :: forall (m :: * -> *) a. Alternative m => FreshC m a -> FreshC m [a]
some :: FreshC m a -> FreshC m [a]
$csome :: forall (m :: * -> *) a. Alternative m => FreshC m a -> FreshC m [a]
<|> :: FreshC m a -> FreshC m a -> FreshC m a
$c<|> :: forall (m :: * -> *) a.
Alternative m =>
FreshC m a -> FreshC m a -> FreshC m a
empty :: FreshC m a
$cempty :: forall (m :: * -> *) a. Alternative m => FreshC m a
$cp1Alternative :: forall (m :: * -> *). Alternative 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 :: * -> *). Functor (FreshC m)
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 (m :: * -> *) a. a -> FreshC m a
forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m a
forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m b
forall (m :: * -> *) a b.
FreshC m (a -> b) -> FreshC m a -> FreshC m b
forall (m :: * -> *) a b c.
(a -> b -> c) -> FreshC m a -> FreshC m b -> FreshC m c
<* :: FreshC m a -> FreshC m b -> FreshC m a
$c<* :: forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m a
*> :: FreshC m a -> FreshC m b -> FreshC m b
$c*> :: forall (m :: * -> *) a b. 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.
(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.
FreshC m (a -> b) -> FreshC m a -> FreshC m b
pure :: a -> FreshC m a
$cpure :: forall (m :: * -> *) a. a -> FreshC m a
$cp1Applicative :: forall (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 (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
forall (m :: * -> *) a b. a -> FreshC m b -> FreshC m a
forall (m :: * -> *) a b. (a -> b) -> FreshC m a -> FreshC m b
<$ :: a -> FreshC m b -> FreshC m a
$c<$ :: forall (m :: * -> *) a b. a -> FreshC m b -> FreshC m a
fmap :: (a -> b) -> FreshC m a -> FreshC m b
$cfmap :: forall (m :: * -> *) a b. (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 :: * -> *). Applicative (FreshC m)
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
forall (m :: * -> *) a. a -> FreshC m a
forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m b
forall (m :: * -> *) a b.
FreshC m a -> (a -> FreshC m b) -> FreshC m b
return :: a -> FreshC m a
$creturn :: forall (m :: * -> *) a. a -> FreshC m a
>> :: FreshC m a -> FreshC m b -> FreshC m b
$c>> :: forall (m :: * -> *) a b. FreshC m a -> FreshC m b -> FreshC m b
>>= :: FreshC m a -> (a -> FreshC m b) -> FreshC m b
$c>>= :: forall (m :: * -> *) a b.
FreshC m a -> (a -> FreshC m b) -> FreshC m b
$cp1Monad :: forall (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 => Algebra (Fresh :+: sig) (FreshC m) where
alg :: 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 -> (Int -> (Int, ctx Int)) -> StateC Int m (ctx Int)
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
(s -> (s, a)) -> m a
state ((Int -> (Int, ctx Int)) -> StateC Int m (ctx Int))
-> (Int -> (Int, ctx Int)) -> StateC Int m (ctx Int)
forall a b. (a -> b) -> a -> b
$ \ Int
i -> (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, Int
i Int -> ctx () -> ctx Int
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 #-}