{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
-- | Create 'ClSF's with randomness without 'IO'.
--   Uses the @MonadRandom@ package.
--   This module copies the API from @dunai@'s
--   'Control.Monad.Trans.MSF.Random'.

module FRP.Rhine.ClSF.Random
  ( module FRP.Rhine.ClSF.Random
  , module X
  )
  where


-- transformers
import Control.Monad.IO.Class

-- MonadRandom
import Control.Monad.Random

-- dunai
import Control.Monad.Trans.MSF.Except (performOnFirstSample)
import qualified Control.Monad.Trans.MSF.Random as MSF
import Control.Monad.Trans.MSF.Random as X hiding (runRandS, evalRandS, getRandomS, getRandomRS, getRandomRS_)

-- rhine
import FRP.Rhine.ClSF.Core
import FRP.Rhine.ClSF.Random.Util

-- * Generating random values from the 'RandT' transformer

-- | Generates random values, updating the generator on every step.
runRandS
  :: (RandomGen g, Monad m)
  => ClSF (RandT g m) cl a     b
  -> g -- ^ The initial random seed
  -> ClSF          m  cl a (g, b)
runRandS :: ClSF (RandT g m) cl a b -> g -> ClSF m cl a (g, b)
runRandS ClSF (RandT g m) cl a b
clsf g
g = MSF (RandT g (ReaderT (TimeInfo cl) m)) a b
-> g -> ClSF m cl a (g, b)
forall g (m :: Type -> Type) a b.
(RandomGen g, Functor m, Monad m) =>
MSF (RandT g m) a b -> g -> MSF m a (g, b)
MSF.runRandS ((forall c.
 ReaderT (TimeInfo cl) (RandT g m) c
 -> RandT g (ReaderT (TimeInfo cl) m) c)
-> ClSF (RandT g m) cl a b
-> MSF (RandT g (ReaderT (TimeInfo cl) m)) a b
forall (m2 :: Type -> Type) (m1 :: Type -> Type) a b.
(Monad m2, Monad m1) =>
(forall c. m1 c -> m2 c) -> MSF m1 a b -> MSF m2 a b
morphS forall c.
ReaderT (TimeInfo cl) (RandT g m) c
-> RandT g (ReaderT (TimeInfo cl) m) c
forall r g (m :: Type -> Type) a.
ReaderT r (RandT g m) a -> RandT g (ReaderT r m) a
commuteReaderRand ClSF (RandT g m) cl a b
clsf) g
g

-- | Updates the generator every step but discards the generator.
evalRandS
  :: (RandomGen g, Monad m)
  => ClSF (RandT g m) cl a b
  -> g
  -> ClSF          m  cl a b
evalRandS :: ClSF (RandT g m) cl a b -> g -> ClSF m cl a b
evalRandS ClSF (RandT g m) cl a b
clsf g
g = ClSF (RandT g m) cl a b -> g -> ClSF m cl a (g, b)
forall g (m :: Type -> Type) cl a b.
(RandomGen g, Monad m) =>
ClSF (RandT g m) cl a b -> g -> ClSF m cl a (g, b)
runRandS ClSF (RandT g m) cl a b
clsf g
g ClSF m cl a (g, b)
-> MSF (ReaderT (TimeInfo cl) m) (g, b) b -> ClSF m cl a b
forall k (cat :: k -> k -> Type) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((g, b) -> b) -> MSF (ReaderT (TimeInfo cl) m) (g, b) b
forall (a :: Type -> Type -> Type) b c.
Arrow a =>
(b -> c) -> a b c
arr (g, b) -> b
forall a b. (a, b) -> b
snd

-- | Updates the generator every step but discards the value,
--   only outputting the generator.
execRandS
  :: (RandomGen g, Monad m)
  => ClSF (RandT g m) cl a b
  -> g
  -> ClSF          m  cl a g
execRandS :: ClSF (RandT g m) cl a b -> g -> ClSF m cl a g
execRandS ClSF (RandT g m) cl a b
clsf g
g = ClSF (RandT g m) cl a b -> g -> ClSF m cl a (g, b)
forall g (m :: Type -> Type) cl a b.
(RandomGen g, Monad m) =>
ClSF (RandT g m) cl a b -> g -> ClSF m cl a (g, b)
runRandS ClSF (RandT g m) cl a b
clsf g
g ClSF m cl a (g, b)
-> MSF (ReaderT (TimeInfo cl) m) (g, b) g -> ClSF m cl a g
forall k (cat :: k -> k -> Type) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((g, b) -> g) -> MSF (ReaderT (TimeInfo cl) m) (g, b) g
forall (a :: Type -> Type -> Type) b c.
Arrow a =>
(b -> c) -> a b c
arr (g, b) -> g
forall a b. (a, b) -> a
fst

-- | Evaluates the random computation by using the global random generator.
evalRandIOS
  :: Monad m
  =>     ClSF (RandT StdGen m) cl a b
  -> IO (ClSF               m  cl a b)
evalRandIOS :: ClSF (RandT StdGen m) cl a b -> IO (ClSF m cl a b)
evalRandIOS ClSF (RandT StdGen m) cl a b
clsf = do
  StdGen
g <- IO StdGen
forall (m :: Type -> Type). MonadIO m => m StdGen
newStdGen
  ClSF m cl a b -> IO (ClSF m cl a b)
forall (m :: Type -> Type) a. Monad m => a -> m a
return (ClSF m cl a b -> IO (ClSF m cl a b))
-> ClSF m cl a b -> IO (ClSF m cl a b)
forall a b. (a -> b) -> a -> b
$ ClSF (RandT StdGen m) cl a b -> StdGen -> ClSF m cl a b
forall g (m :: Type -> Type) cl a b.
(RandomGen g, Monad m) =>
ClSF (RandT g m) cl a b -> g -> ClSF m cl a b
evalRandS ClSF (RandT StdGen m) cl a b
clsf StdGen
g

-- | Evaluates the random computation by using the global random generator on the first tick.
evalRandIOS'
  :: MonadIO m
  => ClSF (RandT StdGen m) cl a b
  -> ClSF               m  cl a b
evalRandIOS' :: ClSF (RandT StdGen m) cl a b -> ClSF m cl a b
evalRandIOS' = ReaderT (TimeInfo cl) m (ClSF m cl a b) -> ClSF m cl a b
forall (m :: Type -> Type) a b.
Monad m =>
m (MSF m a b) -> MSF m a b
performOnFirstSample (ReaderT (TimeInfo cl) m (ClSF m cl a b) -> ClSF m cl a b)
-> (ClSF (RandT StdGen m) cl a b
    -> ReaderT (TimeInfo cl) m (ClSF m cl a b))
-> ClSF (RandT StdGen m) cl a b
-> ClSF m cl a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (ClSF m cl a b) -> ReaderT (TimeInfo cl) m (ClSF m cl a b)
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (IO (ClSF m cl a b) -> ReaderT (TimeInfo cl) m (ClSF m cl a b))
-> (ClSF (RandT StdGen m) cl a b -> IO (ClSF m cl a b))
-> ClSF (RandT StdGen m) cl a b
-> ReaderT (TimeInfo cl) m (ClSF m cl a b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClSF (RandT StdGen m) cl a b -> IO (ClSF m cl a b)
forall (m :: Type -> Type) cl a b.
Monad m =>
ClSF (RandT StdGen m) cl a b -> IO (ClSF m cl a b)
evalRandIOS

-- * Creating random behaviours

-- | Produce a random value at every tick.
getRandomS
  :: (MonadRandom m, Random a)
  => Behaviour m time a
getRandomS :: Behaviour m time a
getRandomS = m a -> ClSF m cl arbitrary a
forall (m :: Type -> Type) b cl a. Monad m => m b -> ClSF m cl a b
constMCl m a
forall (m :: Type -> Type) a. (MonadRandom m, Random a) => m a
getRandom

-- | Produce a random value at every tick,
--   within a range given per tick.
getRandomRS
  :: (MonadRandom m, Random a)
  => BehaviourF m time (a, a) a
getRandomRS :: BehaviourF m time (a, a) a
getRandomRS = ((a, a) -> m a) -> ClSF m cl (a, a) a
forall (m :: Type -> Type) a b cl.
Monad m =>
(a -> m b) -> ClSF m cl a b
arrMCl (a, a) -> m a
forall (m :: Type -> Type) a.
(MonadRandom m, Random a) =>
(a, a) -> m a
getRandomR

-- | Produce a random value at every tick,
--   within a range given once.
getRandomRS_
  :: (MonadRandom m, Random a)
  => (a, a)
  -> Behaviour m time a
getRandomRS_ :: (a, a) -> Behaviour m time a
getRandomRS_ (a, a)
range = m a -> ClSF m cl arbitrary a
forall (m :: Type -> Type) b cl a. Monad m => m b -> ClSF m cl a b
constMCl (m a -> ClSF m cl arbitrary a) -> m a -> ClSF m cl arbitrary a
forall a b. (a -> b) -> a -> b
$ (a, a) -> m a
forall (m :: Type -> Type) a.
(MonadRandom m, Random a) =>
(a, a) -> m a
getRandomR (a, a)
range