{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Carrier.Random.Gen
(
runRandom
, evalRandom
, execRandom
, evalRandomSystem
, RandomC(RandomC)
, module Control.Effect.Random
) where
import Control.Algebra
import Control.Applicative (Alternative)
import Control.Carrier.State.Church
import Control.Effect.Random
import Control.Monad (MonadPlus)
import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Trans.Class
import qualified System.Random as R (Random(..), RandomGen(..), StdGen, newStdGen)
runRandom :: Applicative m => g -> RandomC g m a -> m (g, a)
runRandom :: forall (m :: * -> *) g a.
Applicative m =>
g -> RandomC g m a -> m (g, a)
runRandom g
g = (g -> a -> m (g, a)) -> g -> StateC g m a -> m (g, a)
forall s (m :: * -> *) a b.
(s -> a -> m b) -> s -> StateC s m a -> m b
runState (((g, a) -> m (g, a)) -> g -> a -> m (g, a)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (g, a) -> m (g, a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) g
g (StateC g m a -> m (g, a))
-> (RandomC g m a -> StateC g m a) -> RandomC g m a -> m (g, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RandomC g m a -> StateC g m a
forall g (m :: * -> *) a. RandomC g m a -> StateC g m a
runRandomC
{-# INLINE runRandom #-}
evalRandom :: Applicative m => g -> RandomC g m a -> m a
evalRandom :: forall (m :: * -> *) g a.
Applicative m =>
g -> RandomC g m a -> m a
evalRandom g
g = g -> StateC g m a -> m a
forall s (m :: * -> *) a. Applicative m => s -> StateC s m a -> m a
evalState g
g (StateC g m a -> m a)
-> (RandomC g m a -> StateC g m a) -> RandomC g m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RandomC g m a -> StateC g m a
forall g (m :: * -> *) a. RandomC g m a -> StateC g m a
runRandomC
{-# INLINE evalRandom #-}
execRandom :: Applicative m => g -> RandomC g m a -> m g
execRandom :: forall (m :: * -> *) g a.
Applicative m =>
g -> RandomC g m a -> m g
execRandom g
g = g -> StateC g m a -> m g
forall s (m :: * -> *) a. Applicative m => s -> StateC s m a -> m s
execState g
g (StateC g m a -> m g)
-> (RandomC g m a -> StateC g m a) -> RandomC g m a -> m g
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RandomC g m a -> StateC g m a
forall g (m :: * -> *) a. RandomC g m a -> StateC g m a
runRandomC
{-# INLINE execRandom #-}
evalRandomSystem :: MonadIO m => RandomC R.StdGen m a -> m a
evalRandomSystem :: forall (m :: * -> *) a. MonadIO m => RandomC StdGen m a -> m a
evalRandomSystem RandomC StdGen m a
m = IO StdGen -> m StdGen
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO StdGen
forall (m :: * -> *). MonadIO m => m StdGen
R.newStdGen m StdGen -> (StdGen -> m a) -> m a
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (StdGen -> RandomC StdGen m a -> m a)
-> RandomC StdGen m a -> StdGen -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip StdGen -> RandomC StdGen m a -> m a
forall (m :: * -> *) g a.
Applicative m =>
g -> RandomC g m a -> m a
evalRandom RandomC StdGen m a
m
{-# INLINE evalRandomSystem #-}
newtype RandomC g m a = RandomC { forall g (m :: * -> *) a. RandomC g m a -> StateC g m a
runRandomC :: StateC g m a }
deriving (Applicative (RandomC g m)
Applicative (RandomC g m) =>
(forall a. RandomC g m a)
-> (forall a. RandomC g m a -> RandomC g m a -> RandomC g m a)
-> (forall a. RandomC g m a -> RandomC g m [a])
-> (forall a. RandomC g m a -> RandomC g m [a])
-> Alternative (RandomC g m)
forall a. RandomC g m a
forall a. RandomC g m a -> RandomC g m [a]
forall a. RandomC g m a -> RandomC g m a -> RandomC g m a
forall g (m :: * -> *). Alternative m => Applicative (RandomC g m)
forall g (m :: * -> *) a. Alternative m => RandomC g m a
forall g (m :: * -> *) a.
Alternative m =>
RandomC g m a -> RandomC g m [a]
forall g (m :: * -> *) a.
Alternative m =>
RandomC g m a -> RandomC g m a -> RandomC g 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
$cempty :: forall g (m :: * -> *) a. Alternative m => RandomC g m a
empty :: forall a. RandomC g m a
$c<|> :: forall g (m :: * -> *) a.
Alternative m =>
RandomC g m a -> RandomC g m a -> RandomC g m a
<|> :: forall a. RandomC g m a -> RandomC g m a -> RandomC g m a
$csome :: forall g (m :: * -> *) a.
Alternative m =>
RandomC g m a -> RandomC g m [a]
some :: forall a. RandomC g m a -> RandomC g m [a]
$cmany :: forall g (m :: * -> *) a.
Alternative m =>
RandomC g m a -> RandomC g m [a]
many :: forall a. RandomC g m a -> RandomC g m [a]
Alternative, Functor (RandomC g m)
Functor (RandomC g m) =>
(forall a. a -> RandomC g m a)
-> (forall a b.
RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b)
-> (forall a b c.
(a -> b -> c) -> RandomC g m a -> RandomC g m b -> RandomC g m c)
-> (forall a b. RandomC g m a -> RandomC g m b -> RandomC g m b)
-> (forall a b. RandomC g m a -> RandomC g m b -> RandomC g m a)
-> Applicative (RandomC g m)
forall a. a -> RandomC g m a
forall a b. RandomC g m a -> RandomC g m b -> RandomC g m a
forall a b. RandomC g m a -> RandomC g m b -> RandomC g m b
forall a b. RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b
forall a b c.
(a -> b -> c) -> RandomC g m a -> RandomC g m b -> RandomC g m c
forall g (m :: * -> *). Functor (RandomC g m)
forall g (m :: * -> *) a. a -> RandomC g m a
forall g (m :: * -> *) a b.
RandomC g m a -> RandomC g m b -> RandomC g m a
forall g (m :: * -> *) a b.
RandomC g m a -> RandomC g m b -> RandomC g m b
forall g (m :: * -> *) a b.
RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b
forall g (m :: * -> *) a b c.
(a -> b -> c) -> RandomC g m a -> RandomC g m b -> RandomC g 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
$cpure :: forall g (m :: * -> *) a. a -> RandomC g m a
pure :: forall a. a -> RandomC g m a
$c<*> :: forall g (m :: * -> *) a b.
RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b
<*> :: forall a b. RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b
$cliftA2 :: forall g (m :: * -> *) a b c.
(a -> b -> c) -> RandomC g m a -> RandomC g m b -> RandomC g m c
liftA2 :: forall a b c.
(a -> b -> c) -> RandomC g m a -> RandomC g m b -> RandomC g m c
$c*> :: forall g (m :: * -> *) a b.
RandomC g m a -> RandomC g m b -> RandomC g m b
*> :: forall a b. RandomC g m a -> RandomC g m b -> RandomC g m b
$c<* :: forall g (m :: * -> *) a b.
RandomC g m a -> RandomC g m b -> RandomC g m a
<* :: forall a b. RandomC g m a -> RandomC g m b -> RandomC g m a
Applicative, (forall a b. (a -> b) -> RandomC g m a -> RandomC g m b)
-> (forall a b. a -> RandomC g m b -> RandomC g m a)
-> Functor (RandomC g m)
forall a b. a -> RandomC g m b -> RandomC g m a
forall a b. (a -> b) -> RandomC g m a -> RandomC g m b
forall g (m :: * -> *) a b. a -> RandomC g m b -> RandomC g m a
forall g (m :: * -> *) a b.
(a -> b) -> RandomC g m a -> RandomC g m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall g (m :: * -> *) a b.
(a -> b) -> RandomC g m a -> RandomC g m b
fmap :: forall a b. (a -> b) -> RandomC g m a -> RandomC g m b
$c<$ :: forall g (m :: * -> *) a b. a -> RandomC g m b -> RandomC g m a
<$ :: forall a b. a -> RandomC g m b -> RandomC g m a
Functor, Applicative (RandomC g m)
Applicative (RandomC g m) =>
(forall a b.
RandomC g m a -> (a -> RandomC g m b) -> RandomC g m b)
-> (forall a b. RandomC g m a -> RandomC g m b -> RandomC g m b)
-> (forall a. a -> RandomC g m a)
-> Monad (RandomC g m)
forall a. a -> RandomC g m a
forall a b. RandomC g m a -> RandomC g m b -> RandomC g m b
forall a b. RandomC g m a -> (a -> RandomC g m b) -> RandomC g m b
forall g (m :: * -> *). Applicative (RandomC g m)
forall g (m :: * -> *) a. a -> RandomC g m a
forall g (m :: * -> *) a b.
RandomC g m a -> RandomC g m b -> RandomC g m b
forall g (m :: * -> *) a b.
RandomC g m a -> (a -> RandomC g m b) -> RandomC g 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
$c>>= :: forall g (m :: * -> *) a b.
RandomC g m a -> (a -> RandomC g m b) -> RandomC g m b
>>= :: forall a b. RandomC g m a -> (a -> RandomC g m b) -> RandomC g m b
$c>> :: forall g (m :: * -> *) a b.
RandomC g m a -> RandomC g m b -> RandomC g m b
>> :: forall a b. RandomC g m a -> RandomC g m b -> RandomC g m b
$creturn :: forall g (m :: * -> *) a. a -> RandomC g m a
return :: forall a. a -> RandomC g m a
Monad, Monad (RandomC g m)
Monad (RandomC g m) =>
(forall a. String -> RandomC g m a) -> MonadFail (RandomC g m)
forall a. String -> RandomC g m a
forall g (m :: * -> *). MonadFail m => Monad (RandomC g m)
forall g (m :: * -> *) a. MonadFail m => String -> RandomC g m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
$cfail :: forall g (m :: * -> *) a. MonadFail m => String -> RandomC g m a
fail :: forall a. String -> RandomC g m a
Fail.MonadFail, Monad (RandomC g m)
Monad (RandomC g m) =>
(forall a. (a -> RandomC g m a) -> RandomC g m a)
-> MonadFix (RandomC g m)
forall a. (a -> RandomC g m a) -> RandomC g m a
forall g (m :: * -> *). MonadFix m => Monad (RandomC g m)
forall g (m :: * -> *) a.
MonadFix m =>
(a -> RandomC g m a) -> RandomC g m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
$cmfix :: forall g (m :: * -> *) a.
MonadFix m =>
(a -> RandomC g m a) -> RandomC g m a
mfix :: forall a. (a -> RandomC g m a) -> RandomC g m a
MonadFix, Monad (RandomC g m)
Monad (RandomC g m) =>
(forall a. IO a -> RandomC g m a) -> MonadIO (RandomC g m)
forall a. IO a -> RandomC g m a
forall g (m :: * -> *). MonadIO m => Monad (RandomC g m)
forall g (m :: * -> *) a. MonadIO m => IO a -> RandomC g m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
$cliftIO :: forall g (m :: * -> *) a. MonadIO m => IO a -> RandomC g m a
liftIO :: forall a. IO a -> RandomC g m a
MonadIO, Monad (RandomC g m)
Alternative (RandomC g m)
(Alternative (RandomC g m), Monad (RandomC g m)) =>
(forall a. RandomC g m a)
-> (forall a. RandomC g m a -> RandomC g m a -> RandomC g m a)
-> MonadPlus (RandomC g m)
forall a. RandomC g m a
forall a. RandomC g m a -> RandomC g m a -> RandomC g m a
forall g (m :: * -> *).
(Alternative m, Monad m) =>
Monad (RandomC g m)
forall g (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (RandomC g m)
forall g (m :: * -> *) a. (Alternative m, Monad m) => RandomC g m a
forall g (m :: * -> *) a.
(Alternative m, Monad m) =>
RandomC g m a -> RandomC g m a -> RandomC g m a
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
$cmzero :: forall g (m :: * -> *) a. (Alternative m, Monad m) => RandomC g m a
mzero :: forall a. RandomC g m a
$cmplus :: forall g (m :: * -> *) a.
(Alternative m, Monad m) =>
RandomC g m a -> RandomC g m a -> RandomC g m a
mplus :: forall a. RandomC g m a -> RandomC g m a -> RandomC g m a
MonadPlus, (forall (m :: * -> *). Monad m => Monad (RandomC g m)) =>
(forall (m :: * -> *) a. Monad m => m a -> RandomC g m a)
-> MonadTrans (RandomC g)
forall g (m :: * -> *). Monad m => Monad (RandomC g m)
forall g (m :: * -> *) a. Monad m => m a -> RandomC g m a
forall (m :: * -> *). Monad m => Monad (RandomC g m)
forall (m :: * -> *) a. Monad m => m a -> RandomC g m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *). Monad m => Monad (t m)) =>
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
$clift :: forall g (m :: * -> *) a. Monad m => m a -> RandomC g m a
lift :: forall (m :: * -> *) a. Monad m => m a -> RandomC g m a
MonadTrans)
instance (Algebra sig m, R.RandomGen g) => Algebra (Random :+: sig) (RandomC g m) where
alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (RandomC g m)
-> (:+:) Random sig n a -> ctx () -> RandomC g m (ctx a)
alg Handler ctx n (RandomC g m)
hdl (:+:) Random sig n a
sig ctx ()
ctx = StateC g m (ctx a) -> RandomC g m (ctx a)
forall g (m :: * -> *) a. StateC g m a -> RandomC g m a
RandomC (StateC g m (ctx a) -> RandomC g m (ctx a))
-> StateC g m (ctx a) -> RandomC g m (ctx a)
forall a b. (a -> b) -> a -> b
$ case (:+:) Random sig n a
sig of
L Random n a
random -> (forall r. (g -> ctx a -> m r) -> g -> m r) -> StateC g m (ctx a)
forall s (m :: * -> *) a.
(forall r. (s -> a -> m r) -> s -> m r) -> StateC s m a
StateC ((forall r. (g -> ctx a -> m r) -> g -> m r) -> StateC g m (ctx a))
-> (forall r. (g -> ctx a -> m r) -> g -> m r)
-> StateC g m (ctx a)
forall a b. (a -> b) -> a -> b
$ \ g -> ctx a -> m r
k g
g -> case Random n a
random of
Random n a
Uniform -> let (a
a, g
g') = g -> (a, g)
forall g. RandomGen g => g -> (a, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
R.random g
g in g -> ctx a -> m r
k g
g' (a
a a -> ctx () -> ctx a
forall a b. a -> ctx b -> ctx a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx)
UniformR (a, a)
r -> let (a
a, g
g') = (a, a) -> g -> (a, g)
forall g. RandomGen g => (a, a) -> g -> (a, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
R.randomR (a, a)
r g
g in g -> ctx a -> m r
k g
g' (a
a a -> ctx () -> ctx a
forall a b. a -> ctx b -> ctx a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx)
Interleave n a
m -> let (g
g'', g
g') = g -> (g, g)
forall g. RandomGen g => g -> (g, g)
R.split g
g in (g -> ctx a -> m r) -> g -> StateC g m (ctx a) -> m r
forall s (m :: * -> *) a b.
(s -> a -> m b) -> s -> StateC s m a -> m b
runState ((ctx a -> m r) -> g -> ctx a -> m r
forall a b. a -> b -> a
const (g -> ctx a -> m r
k g
g'')) g
g' (RandomC g m (ctx a) -> StateC g m (ctx a)
forall g (m :: * -> *) a. RandomC g m a -> StateC g m a
runRandomC (ctx (n a) -> RandomC g m (ctx a)
Handler ctx n (RandomC g m)
hdl (n a
m n a -> ctx () -> ctx (n a)
forall a b. a -> ctx b -> ctx a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx)))
R sig n a
other -> Handler ctx n (StateC g m)
-> (:+:) (State g) sig n a -> ctx () -> StateC g m (ctx a)
forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (StateC g m)
-> (:+:) (State g) sig n a -> ctx () -> StateC g 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 (RandomC g m (ctx x) -> StateC g m (ctx x)
forall g (m :: * -> *) a. RandomC g m a -> StateC g m a
runRandomC (RandomC g m (ctx x) -> StateC g m (ctx x))
-> (ctx (n x) -> RandomC g m (ctx x))
-> ctx (n x)
-> StateC g m (ctx x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx (n x) -> RandomC g m (ctx x)
Handler ctx n (RandomC g m)
hdl) (sig n a -> (:+:) (State g) sig n a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other) ctx ()
ctx
{-# INLINE alg #-}