{-# LANGUAGE DeriveFunctor, ExistentialQuantification, FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, ScopedTypeVariables, StandaloneDeriving, TypeOperators, UndecidableInstances #-}
module Control.Effect.Random
( -- * Random effect
  Random(..)
  -- * Random carrier
, runRandom
, evalRandom
, execRandom
, evalRandomIO
, RandomC(..)
  -- * Re-exports
, Carrier
, Member
, MonadRandom(..)
, MonadInterleave(..)
, run
) where

import Control.Applicative (Alternative(..))
import Control.Effect.Carrier
import Control.Effect.State
import Control.Monad (MonadPlus(..))
import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.Random.Class (MonadInterleave(..), MonadRandom(..))
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Trans.Class
import qualified System.Random as R (Random(..), RandomGen(..), StdGen, newStdGen)

data Random m k
  = forall a . R.Random a => Random (a -> m k)
  | forall a . R.Random a => RandomR (a, a) (a -> m k)
  | forall a . Interleave (m a) (a -> m k)

deriving instance Functor m => Functor (Random m)

instance HFunctor Random where
  hmap :: (forall x. m x -> n x) -> Random m a -> Random n a
hmap f :: forall x. m x -> n x
f (Random       k :: a -> m a
k) = (a -> n a) -> Random n a
forall (m :: * -> *) k a. Random a => (a -> m k) -> Random m k
Random           (m a -> n a
forall x. m x -> n x
f (m a -> n a) -> (a -> m a) -> a -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
k)
  hmap f :: forall x. m x -> n x
f (RandomR r :: (a, a)
r    k :: a -> m a
k) = (a, a) -> (a -> n a) -> Random n a
forall (m :: * -> *) k a.
Random a =>
(a, a) -> (a -> m k) -> Random m k
RandomR (a, a)
r        (m a -> n a
forall x. m x -> n x
f (m a -> n a) -> (a -> m a) -> a -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
k)
  hmap f :: forall x. m x -> n x
f (Interleave m :: m a
m k :: a -> m a
k) = n a -> (a -> n a) -> Random n a
forall (m :: * -> *) k a. m a -> (a -> m k) -> Random m k
Interleave (m a -> n a
forall x. m x -> n x
f m a
m) (m a -> n a
forall x. m x -> n x
f (m a -> n a) -> (a -> m a) -> a -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
k)
  {-# INLINE hmap #-}

instance Effect Random where
  handle :: f ()
-> (forall x. f (m x) -> n (f x)) -> Random m a -> Random n (f a)
handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler (Random       k :: a -> m a
k) = (a -> n (f a)) -> Random n (f a)
forall (m :: * -> *) k a. Random a => (a -> m k) -> Random m k
Random                            (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (f (m a) -> n (f a)) -> (a -> f (m a)) -> a -> n (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (m a -> f () -> f (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state) (m a -> f (m a)) -> (a -> m a) -> a -> f (m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
k)
  handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler (RandomR r :: (a, a)
r    k :: a -> m a
k) = (a, a) -> (a -> n (f a)) -> Random n (f a)
forall (m :: * -> *) k a.
Random a =>
(a, a) -> (a -> m k) -> Random m k
RandomR (a, a)
r                         (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (f (m a) -> n (f a)) -> (a -> f (m a)) -> a -> n (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (m a -> f () -> f (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state) (m a -> f (m a)) -> (a -> m a) -> a -> f (m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
k)
  handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler (Interleave m :: m a
m k :: a -> m a
k) = n (f a) -> (f a -> n (f a)) -> Random n (f a)
forall (m :: * -> *) k a. m a -> (a -> m k) -> Random m k
Interleave (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (m a
m m a -> f () -> f (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state)) (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (f (m a) -> n (f a)) -> (f a -> f (m a)) -> f a -> n (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m a) -> f a -> f (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> m a
k)


-- | Run a random computation starting from a given generator.
--
--   prop> run (runRandom (PureGen a) (pure b)) === (PureGen a, b)
runRandom :: g -> RandomC g m a -> m (g, a)
runRandom :: g -> RandomC g m a -> m (g, a)
runRandom g :: g
g = g -> StateC g m a -> m (g, a)
forall s (m :: * -> *) a. s -> StateC s m a -> m (s, a)
runState 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

-- | Run a random computation starting from a given generator and discarding the final generator.
--
--   prop> run (evalRandom (PureGen a) (pure b)) === b
evalRandom :: Functor m => g -> RandomC g m a -> m a
evalRandom :: g -> RandomC g m a -> m a
evalRandom g :: g
g = ((g, a) -> a) -> m (g, a) -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (g, a) -> a
forall a b. (a, b) -> b
snd (m (g, a) -> m a)
-> (RandomC g m a -> m (g, a)) -> RandomC g m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. g -> RandomC g m a -> m (g, a)
forall g (m :: * -> *) a. g -> RandomC g m a -> m (g, a)
runRandom g
g

-- | Run a random computation starting from a given generator and discarding the final result.
--
--   prop> run (execRandom (PureGen a) (pure b)) === PureGen a
execRandom :: Functor m => g -> RandomC g m a -> m g
execRandom :: g -> RandomC g m a -> m g
execRandom g :: g
g = ((g, a) -> g) -> m (g, a) -> m g
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (g, a) -> g
forall a b. (a, b) -> a
fst (m (g, a) -> m g)
-> (RandomC g m a -> m (g, a)) -> RandomC g m a -> m g
forall b c a. (b -> c) -> (a -> b) -> a -> c
. g -> RandomC g m a -> m (g, a)
forall g (m :: * -> *) a. g -> RandomC g m a -> m (g, a)
runRandom g
g

-- | Run a random computation in 'IO', splitting the global standard generator to get a new one for the computation.
evalRandomIO :: MonadIO m => RandomC R.StdGen m a -> m a
evalRandomIO :: RandomC StdGen m a -> m a
evalRandomIO m :: RandomC StdGen m a
m = IO StdGen -> m StdGen
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO StdGen
R.newStdGen m StdGen -> (StdGen -> m a) -> m a
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. Functor m => g -> RandomC g m a -> m a
evalRandom RandomC StdGen m a
m

newtype RandomC g m a = RandomC { RandomC g m a -> StateC g m a
runRandomC :: StateC g m a }
  deriving (Applicative (RandomC g m)
RandomC g m a
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)
RandomC g m a -> RandomC g m a -> RandomC g m a
RandomC g m a -> RandomC g m [a]
RandomC g m a -> RandomC g m [a]
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, Monad m) =>
Applicative (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]
forall g (m :: * -> *) a.
(Alternative m, Monad 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
many :: RandomC g m a -> RandomC g m [a]
$cmany :: forall g (m :: * -> *) a.
(Alternative m, Monad m) =>
RandomC g m a -> RandomC g m [a]
some :: RandomC g m a -> RandomC g m [a]
$csome :: forall g (m :: * -> *) a.
(Alternative m, Monad m) =>
RandomC g m a -> RandomC g m [a]
<|> :: RandomC g m a -> RandomC g m a -> RandomC g m a
$c<|> :: forall g (m :: * -> *) a.
(Alternative m, Monad m) =>
RandomC g m a -> RandomC g m a -> RandomC g m a
empty :: RandomC g m a
$cempty :: forall g (m :: * -> *) a. (Alternative m, Monad m) => RandomC g m a
$cp1Alternative :: forall g (m :: * -> *).
(Alternative m, Monad m) =>
Applicative (RandomC g m)
Alternative, Functor (RandomC g m)
a -> RandomC g m a
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)
RandomC g m a -> RandomC g m b -> RandomC g m b
RandomC g m a -> RandomC g m b -> RandomC g m a
RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b
(a -> b -> c) -> RandomC g m a -> RandomC g m b -> RandomC g m c
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 :: * -> *). Monad m => Functor (RandomC g m)
forall g (m :: * -> *) a. Monad m => a -> RandomC g m a
forall g (m :: * -> *) a b.
Monad m =>
RandomC g m a -> RandomC g m b -> RandomC g m a
forall g (m :: * -> *) a b.
Monad m =>
RandomC g m a -> RandomC g m b -> RandomC g m b
forall g (m :: * -> *) a b.
Monad m =>
RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b
forall g (m :: * -> *) a b c.
Monad m =>
(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
<* :: RandomC g m a -> RandomC g m b -> RandomC g m a
$c<* :: forall g (m :: * -> *) a b.
Monad m =>
RandomC g m a -> RandomC g m b -> RandomC g m a
*> :: RandomC g m a -> RandomC g m b -> RandomC g m b
$c*> :: forall g (m :: * -> *) a b.
Monad m =>
RandomC g m a -> RandomC g m b -> RandomC g m b
liftA2 :: (a -> b -> c) -> RandomC g m a -> RandomC g m b -> RandomC g m c
$cliftA2 :: forall g (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> RandomC g m a -> RandomC g m b -> RandomC g m c
<*> :: RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b
$c<*> :: forall g (m :: * -> *) a b.
Monad m =>
RandomC g m (a -> b) -> RandomC g m a -> RandomC g m b
pure :: a -> RandomC g m a
$cpure :: forall g (m :: * -> *) a. Monad m => a -> RandomC g m a
$cp1Applicative :: forall g (m :: * -> *). Monad m => Functor (RandomC g m)
Applicative, a -> RandomC g m b -> RandomC g m a
(a -> b) -> RandomC g m a -> RandomC g m b
(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.
Functor m =>
a -> RandomC g m b -> RandomC g m a
forall g (m :: * -> *) a b.
Functor m =>
(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
<$ :: a -> RandomC g m b -> RandomC g m a
$c<$ :: forall g (m :: * -> *) a b.
Functor m =>
a -> RandomC g m b -> RandomC g m a
fmap :: (a -> b) -> RandomC g m a -> RandomC g m b
$cfmap :: forall g (m :: * -> *) a b.
Functor m =>
(a -> b) -> RandomC g m a -> RandomC g m b
Functor, Applicative (RandomC g m)
a -> RandomC g m a
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)
RandomC g m a -> (a -> RandomC g m b) -> RandomC g m b
RandomC g m a -> RandomC g m b -> RandomC g m b
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 :: * -> *). Monad m => Applicative (RandomC g m)
forall g (m :: * -> *) a. Monad m => a -> RandomC g m a
forall g (m :: * -> *) a b.
Monad m =>
RandomC g m a -> RandomC g m b -> RandomC g m b
forall g (m :: * -> *) a b.
Monad m =>
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
return :: a -> RandomC g m a
$creturn :: forall g (m :: * -> *) a. Monad m => a -> RandomC g m a
>> :: RandomC g m a -> RandomC g m b -> RandomC g m b
$c>> :: forall g (m :: * -> *) a b.
Monad m =>
RandomC g m a -> RandomC g m b -> RandomC g m b
>>= :: RandomC g m a -> (a -> RandomC g m b) -> RandomC g m b
$c>>= :: forall g (m :: * -> *) a b.
Monad m =>
RandomC g m a -> (a -> RandomC g m b) -> RandomC g m b
$cp1Monad :: forall g (m :: * -> *). Monad m => Applicative (RandomC g m)
Monad, Monad (RandomC g m)
Monad (RandomC g m) =>
(forall a. String -> RandomC g m a) -> MonadFail (RandomC g m)
String -> RandomC g m a
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
fail :: String -> RandomC g m a
$cfail :: forall g (m :: * -> *) a. MonadFail m => String -> RandomC g m a
$cp1MonadFail :: forall g (m :: * -> *). MonadFail m => Monad (RandomC g m)
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)
(a -> RandomC g m a) -> RandomC g m a
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
mfix :: (a -> RandomC g m a) -> RandomC g m a
$cmfix :: forall g (m :: * -> *) a.
MonadFix m =>
(a -> RandomC g m a) -> RandomC g m a
$cp1MonadFix :: forall g (m :: * -> *). MonadFix m => Monad (RandomC g m)
MonadFix, Monad (RandomC g m)
Monad (RandomC g m) =>
(forall a. IO a -> RandomC g m a) -> MonadIO (RandomC g m)
IO a -> RandomC g m a
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
liftIO :: IO a -> RandomC g m a
$cliftIO :: forall g (m :: * -> *) a. MonadIO m => IO a -> RandomC g m a
$cp1MonadIO :: forall g (m :: * -> *). MonadIO m => Monad (RandomC g m)
MonadIO, Monad (RandomC g m)
Alternative (RandomC g m)
RandomC g m a
(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)
RandomC g m a -> RandomC g m a -> RandomC g m a
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
mplus :: RandomC g m a -> RandomC g m 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
mzero :: RandomC g m a
$cmzero :: forall g (m :: * -> *) a. (Alternative m, Monad m) => RandomC g m a
$cp2MonadPlus :: forall g (m :: * -> *).
(Alternative m, Monad m) =>
Monad (RandomC g m)
$cp1MonadPlus :: forall g (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (RandomC g m)
MonadPlus, m a -> RandomC g m a
(forall (m :: * -> *) a. Monad m => m a -> RandomC g m a)
-> MonadTrans (RandomC g)
forall g (m :: * -> *) a. Monad m => m a -> RandomC g m a
forall (m :: * -> *) a. Monad m => m a -> RandomC g m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> RandomC g m a
$clift :: forall g (m :: * -> *) a. Monad m => m a -> RandomC g m a
MonadTrans)

instance (Carrier sig m, Effect sig, R.RandomGen g) => MonadRandom (RandomC g m) where
  getRandom :: RandomC g m a
getRandom = StateC g m a -> RandomC g m a
forall g (m :: * -> *) a. StateC g m a -> RandomC g m a
RandomC (StateC g m a -> RandomC g m a) -> StateC g m a -> RandomC g m a
forall a b. (a -> b) -> a -> b
$ do
    (a :: a
a, g' :: g
g') <- (g -> (a, g)) -> StateC g m (a, g)
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Member (State s) sig, Carrier sig m) =>
(s -> a) -> m a
gets g -> (a, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
R.random
    a
a a -> StateC g m () -> StateC g m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ g -> StateC g m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
s -> m ()
put (g
g' :: g)
  {-# INLINE getRandom #-}
  getRandomR :: (a, a) -> RandomC g m a
getRandomR r :: (a, a)
r = StateC g m a -> RandomC g m a
forall g (m :: * -> *) a. StateC g m a -> RandomC g m a
RandomC (StateC g m a -> RandomC g m a) -> StateC g m a -> RandomC g m a
forall a b. (a -> b) -> a -> b
$ do
    (a :: a
a, g' :: g
g') <- (g -> (a, g)) -> StateC g m (a, g)
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Member (State s) sig, Carrier sig m) =>
(s -> a) -> m a
gets ((a, a) -> g -> (a, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
R.randomR (a, a)
r)
    a
a a -> StateC g m () -> StateC g m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ g -> StateC g m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
s -> m ()
put (g
g' :: g)
  {-# INLINE getRandomR #-}
  getRandomRs :: (a, a) -> RandomC g m [a]
getRandomRs interval :: (a, a)
interval = (:) (a -> [a] -> [a]) -> RandomC g m a -> RandomC g m ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a, a) -> RandomC g m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR (a, a)
interval RandomC g m ([a] -> [a]) -> RandomC g m [a] -> RandomC g m [a]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a, a) -> RandomC g m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs (a, a)
interval
  {-# INLINE getRandomRs #-}
  getRandoms :: RandomC g m [a]
getRandoms = (:) (a -> [a] -> [a]) -> RandomC g m a -> RandomC g m ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RandomC g m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom RandomC g m ([a] -> [a]) -> RandomC g m [a] -> RandomC g m [a]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RandomC g m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
  {-# INLINE getRandoms #-}

instance (Carrier sig m, Effect sig, R.RandomGen g) => MonadInterleave (RandomC g m) where
  interleave :: RandomC g m a -> RandomC g m a
interleave m :: RandomC g m a
m = StateC g m a -> RandomC g m a
forall g (m :: * -> *) a. StateC g m a -> RandomC g m a
RandomC (StateC g m a -> RandomC g m a) -> StateC g m a -> RandomC g m a
forall a b. (a -> b) -> a -> b
$ do
    (g1 :: g
g1, g2 :: g
g2) <- (g -> (g, g)) -> StateC g m (g, g)
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
(Member (State s) sig, Carrier sig m) =>
(s -> a) -> m a
gets g -> (g, g)
forall g. RandomGen g => g -> (g, g)
R.split
    g -> StateC g m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
s -> m ()
put (g
g1 :: g)
    a
a <- RandomC g m a -> StateC g m a
forall g (m :: * -> *) a. RandomC g m a -> StateC g m a
runRandomC RandomC g m a
m
    a
a a -> StateC g m () -> StateC g m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ g -> StateC g m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
s -> m ()
put g
g2
  {-# INLINE interleave #-}

instance (Carrier sig m, Effect sig, R.RandomGen g) => Carrier (Random :+: sig) (RandomC g m) where
  eff :: (:+:) Random sig (RandomC g m) a -> RandomC g m a
eff (L (Random       k :: a -> RandomC g m a
k)) = RandomC g m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom RandomC g m a -> (a -> RandomC g m a) -> RandomC g m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> RandomC g m a
k
  eff (L (RandomR r :: (a, a)
r    k :: a -> RandomC g m a
k)) = (a, a) -> RandomC g m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR (a, a)
r RandomC g m a -> (a -> RandomC g m a) -> RandomC g m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> RandomC g m a
k
  eff (L (Interleave m :: RandomC g m a
m k :: a -> RandomC g m a
k)) = RandomC g m a -> RandomC g m a
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave RandomC g m a
m RandomC g m a -> (a -> RandomC g m a) -> RandomC g m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> RandomC g m a
k
  eff (R other :: sig (RandomC g m) a
other)            = StateC g m a -> RandomC g m a
forall g (m :: * -> *) a. StateC g m a -> RandomC g m a
RandomC ((:+:) (State g) sig (StateC g m) a -> StateC g m a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Carrier sig m =>
sig m a -> m a
eff (sig (StateC g m) a -> (:+:) (State g) sig (StateC g m) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R (sig (RandomC g m) a -> sig (StateC g m) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible sig (RandomC g m) a
other)))
  {-# INLINE eff #-}


-- $setup
-- >>> :seti -XFlexibleContexts
-- >>> import System.Random
-- >>> import Test.QuickCheck
-- >>> import Control.Effect.Pure
-- >>> import Control.Effect.NonDet
-- >>> newtype PureGen = PureGen Int deriving (Eq, Show)
-- >>> instance RandomGen PureGen where next (PureGen i) = (i, PureGen i) ; split g = (g, g)