{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Random.Class (
MonadRandom(..),
MonadSplit(..),
MonadInterleave(..),
fromList,
fromListMay,
uniform,
uniformMay,
weighted,
weightedMay
) where
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Cont
import Control.Monad.Trans.Error
import Control.Monad.Trans.Except
import Control.Monad.Trans.Identity
import Control.Monad.Trans.List
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader
import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS
import qualified Control.Monad.Trans.State.Lazy as LazyState
import qualified Control.Monad.Trans.State.Strict as StrictState
import qualified Control.Monad.Trans.Writer.Lazy as LazyWriter
import qualified Control.Monad.Trans.Writer.Strict as StrictWriter
import qualified System.Random as Random
import qualified Data.Foldable as F
#if MIN_VERSION_base(4,8,0)
#else
import Data.Monoid (Monoid)
#endif
class (Monad m) => MonadRandom m where
getRandomR :: (Random.Random a) => (a, a) -> m a
getRandom :: (Random.Random a) => m a
getRandomRs :: (Random.Random a) => (a, a) -> m [a]
getRandoms :: (Random.Random a) => m [a]
instance MonadRandom IO where
getRandomR :: (a, a) -> IO a
getRandomR = (a, a) -> IO a
forall a (m :: * -> *). (Random a, MonadIO m) => (a, a) -> m a
Random.randomRIO
getRandom :: IO a
getRandom = IO a
forall a (m :: * -> *). (Random a, MonadIO m) => m a
Random.randomIO
getRandomRs :: (a, a) -> IO [a]
getRandomRs (a, a)
lohi = (StdGen -> [a]) -> IO StdGen -> IO [a]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ((a, a) -> StdGen -> [a]
forall a g. (Random a, RandomGen g) => (a, a) -> g -> [a]
Random.randomRs (a, a)
lohi) IO StdGen
forall (m :: * -> *). MonadIO m => m StdGen
Random.newStdGen
getRandoms :: IO [a]
getRandoms = (StdGen -> [a]) -> IO StdGen -> IO [a]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM StdGen -> [a]
forall a g. (Random a, RandomGen g) => g -> [a]
Random.randoms IO StdGen
forall (m :: * -> *). MonadIO m => m StdGen
Random.newStdGen
instance (MonadRandom m) => MonadRandom (ContT r m) where
getRandomR :: (a, a) -> ContT r m a
getRandomR = m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ContT r m a) -> ((a, a) -> m a) -> (a, a) -> ContT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: ContT r m a
getRandom = m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> ContT r m [a]
getRandomRs = m [a] -> ContT r m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> ContT r m [a])
-> ((a, a) -> m [a]) -> (a, a) -> ContT r m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: ContT r m [a]
getRandoms = m [a] -> ContT r m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (Error e, MonadRandom m) => MonadRandom (ErrorT e m) where
getRandomR :: (a, a) -> ErrorT e m a
getRandomR = m a -> ErrorT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ErrorT e m a) -> ((a, a) -> m a) -> (a, a) -> ErrorT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: ErrorT e m a
getRandom = m a -> ErrorT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> ErrorT e m [a]
getRandomRs = m [a] -> ErrorT e m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> ErrorT e m [a])
-> ((a, a) -> m [a]) -> (a, a) -> ErrorT e m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: ErrorT e m [a]
getRandoms = m [a] -> ErrorT e m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m) => MonadRandom (ExceptT e m) where
getRandomR :: (a, a) -> ExceptT e m a
getRandomR = m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ExceptT e m a)
-> ((a, a) -> m a) -> (a, a) -> ExceptT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: ExceptT e m a
getRandom = m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> ExceptT e m [a]
getRandomRs = m [a] -> ExceptT e m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> ExceptT e m [a])
-> ((a, a) -> m [a]) -> (a, a) -> ExceptT e m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: ExceptT e m [a]
getRandoms = m [a] -> ExceptT e m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m) => MonadRandom (IdentityT m) where
getRandomR :: (a, a) -> IdentityT m a
getRandomR = m a -> IdentityT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> IdentityT m a)
-> ((a, a) -> m a) -> (a, a) -> IdentityT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: IdentityT m a
getRandom = m a -> IdentityT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> IdentityT m [a]
getRandomRs = m [a] -> IdentityT m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> IdentityT m [a])
-> ((a, a) -> m [a]) -> (a, a) -> IdentityT m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: IdentityT m [a]
getRandoms = m [a] -> IdentityT m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m) => MonadRandom (ListT m) where
getRandomR :: (a, a) -> ListT m a
getRandomR = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a) -> ((a, a) -> m a) -> (a, a) -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: ListT m a
getRandom = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> ListT m [a]
getRandomRs = m [a] -> ListT m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> ListT m [a])
-> ((a, a) -> m [a]) -> (a, a) -> ListT m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: ListT m [a]
getRandoms = m [a] -> ListT m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m) => MonadRandom (MaybeT m) where
getRandomR :: (a, a) -> MaybeT m a
getRandomR = m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> MaybeT m a) -> ((a, a) -> m a) -> (a, a) -> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: MaybeT m a
getRandom = m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> MaybeT m [a]
getRandomRs = m [a] -> MaybeT m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> MaybeT m [a])
-> ((a, a) -> m [a]) -> (a, a) -> MaybeT m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: MaybeT m [a]
getRandoms = m [a] -> MaybeT m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (Monoid w, MonadRandom m) => MonadRandom (LazyRWS.RWST r w s m) where
getRandomR :: (a, a) -> RWST r w s m a
getRandomR = m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w s m a)
-> ((a, a) -> m a) -> (a, a) -> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: RWST r w s m a
getRandom = m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> RWST r w s m [a]
getRandomRs = m [a] -> RWST r w s m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> RWST r w s m [a])
-> ((a, a) -> m [a]) -> (a, a) -> RWST r w s m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: RWST r w s m [a]
getRandoms = m [a] -> RWST r w s m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (Monoid w, MonadRandom m) => MonadRandom (StrictRWS.RWST r w s m) where
getRandomR :: (a, a) -> RWST r w s m a
getRandomR = m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w s m a)
-> ((a, a) -> m a) -> (a, a) -> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: RWST r w s m a
getRandom = m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> RWST r w s m [a]
getRandomRs = m [a] -> RWST r w s m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> RWST r w s m [a])
-> ((a, a) -> m [a]) -> (a, a) -> RWST r w s m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: RWST r w s m [a]
getRandoms = m [a] -> RWST r w s m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m) => MonadRandom (ReaderT r m) where
getRandomR :: (a, a) -> ReaderT r m a
getRandomR = m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT r m a)
-> ((a, a) -> m a) -> (a, a) -> ReaderT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: ReaderT r m a
getRandom = m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> ReaderT r m [a]
getRandomRs = m [a] -> ReaderT r m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> ReaderT r m [a])
-> ((a, a) -> m [a]) -> (a, a) -> ReaderT r m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: ReaderT r m [a]
getRandoms = m [a] -> ReaderT r m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m) => MonadRandom (LazyState.StateT s m) where
getRandomR :: (a, a) -> StateT s m a
getRandomR = m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT s m a) -> ((a, a) -> m a) -> (a, a) -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: StateT s m a
getRandom = m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> StateT s m [a]
getRandomRs = m [a] -> StateT s m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> StateT s m [a])
-> ((a, a) -> m [a]) -> (a, a) -> StateT s m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: StateT s m [a]
getRandoms = m [a] -> StateT s m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m) => MonadRandom (StrictState.StateT s m) where
getRandomR :: (a, a) -> StateT s m a
getRandomR = m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT s m a) -> ((a, a) -> m a) -> (a, a) -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: StateT s m a
getRandom = m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> StateT s m [a]
getRandomRs = m [a] -> StateT s m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> StateT s m [a])
-> ((a, a) -> m [a]) -> (a, a) -> StateT s m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: StateT s m [a]
getRandoms = m [a] -> StateT s m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m, Monoid w) => MonadRandom (LazyWriter.WriterT w m) where
getRandomR :: (a, a) -> WriterT w m a
getRandomR = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a)
-> ((a, a) -> m a) -> (a, a) -> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: WriterT w m a
getRandom = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> WriterT w m [a]
getRandomRs = m [a] -> WriterT w m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> WriterT w m [a])
-> ((a, a) -> m [a]) -> (a, a) -> WriterT w m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: WriterT w m [a]
getRandoms = m [a] -> WriterT w m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
instance (MonadRandom m, Monoid w) => MonadRandom (StrictWriter.WriterT w m) where
getRandomR :: (a, a) -> WriterT w m a
getRandomR = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a)
-> ((a, a) -> m a) -> (a, a) -> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR
getRandom :: WriterT w m a
getRandom = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
forall (m :: * -> *) a. (MonadRandom m, Random a) => m a
getRandom
getRandomRs :: (a, a) -> WriterT w m [a]
getRandomRs = m [a] -> WriterT w m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> WriterT w m [a])
-> ((a, a) -> m [a]) -> (a, a) -> WriterT w m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> m [a]
forall (m :: * -> *) a.
(MonadRandom m, Random a) =>
(a, a) -> m [a]
getRandomRs
getRandoms :: WriterT w m [a]
getRandoms = m [a] -> WriterT w m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [a]
forall (m :: * -> *) a. (MonadRandom m, Random a) => m [a]
getRandoms
class (Monad m) => MonadSplit g m | m -> g where
getSplit :: m g
instance MonadSplit Random.StdGen IO where
getSplit :: IO StdGen
getSplit = IO StdGen
forall (m :: * -> *). MonadIO m => m StdGen
Random.newStdGen
instance (MonadSplit g m) => MonadSplit g (ContT r m) where
getSplit :: ContT r m g
getSplit = m g -> ContT r m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (Error e, MonadSplit g m) => MonadSplit g (ErrorT e m) where
getSplit :: ErrorT e m g
getSplit = m g -> ErrorT e m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (MonadSplit g m) => MonadSplit g (ExceptT e m) where
getSplit :: ExceptT e m g
getSplit = m g -> ExceptT e m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (MonadSplit g m) => MonadSplit g (IdentityT m) where
getSplit :: IdentityT m g
getSplit = m g -> IdentityT m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (MonadSplit g m) => MonadSplit g (ListT m) where
getSplit :: ListT m g
getSplit = m g -> ListT m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (MonadSplit g m) => MonadSplit g (MaybeT m) where
getSplit :: MaybeT m g
getSplit = m g -> MaybeT m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (Monoid w, MonadSplit g m) => MonadSplit g (LazyRWS.RWST r w s m) where
getSplit :: RWST r w s m g
getSplit = m g -> RWST r w s m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (Monoid w, MonadSplit g m) => MonadSplit g (StrictRWS.RWST r w s m) where
getSplit :: RWST r w s m g
getSplit = m g -> RWST r w s m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (MonadSplit g m) => MonadSplit g (ReaderT r m) where
getSplit :: ReaderT r m g
getSplit = m g -> ReaderT r m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (MonadSplit g m) => MonadSplit g (LazyState.StateT s m) where
getSplit :: StateT s m g
getSplit = m g -> StateT s m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (MonadSplit g m) => MonadSplit g (StrictState.StateT s m) where
getSplit :: StateT s m g
getSplit = m g -> StateT s m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (Monoid w, MonadSplit g m) => MonadSplit g (LazyWriter.WriterT w m) where
getSplit :: WriterT w m g
getSplit = m g -> WriterT w m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
instance (Monoid w, MonadSplit g m) => MonadSplit g (StrictWriter.WriterT w m) where
getSplit :: WriterT w m g
getSplit = m g -> WriterT w m g
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m g
forall g (m :: * -> *). MonadSplit g m => m g
getSplit
class MonadRandom m => MonadInterleave m where
interleave :: m a -> m a
instance (MonadInterleave m) => MonadInterleave (ContT r m) where
interleave :: ContT r m a -> ContT r m a
interleave = (m r -> m r) -> ContT r m a -> ContT r m a
forall k (m :: k -> *) (r :: k) a.
(m r -> m r) -> ContT r m a -> ContT r m a
mapContT m r -> m r
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (Error e, MonadInterleave m) => MonadInterleave (ErrorT e m) where
interleave :: ErrorT e m a -> ErrorT e m a
interleave = (m (Either e a) -> m (Either e a)) -> ErrorT e m a -> ErrorT e m a
forall (m :: * -> *) e a (n :: * -> *) e' b.
(m (Either e a) -> n (Either e' b))
-> ErrorT e m a -> ErrorT e' n b
mapErrorT m (Either e a) -> m (Either e a)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (MonadInterleave m) => MonadInterleave (ExceptT e m) where
interleave :: ExceptT e m a -> ExceptT e m a
interleave = (m (Either e a) -> m (Either e a))
-> ExceptT e m a -> ExceptT e m a
forall (m :: * -> *) e a (n :: * -> *) e' b.
(m (Either e a) -> n (Either e' b))
-> ExceptT e m a -> ExceptT e' n b
mapExceptT m (Either e a) -> m (Either e a)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (MonadInterleave m) => MonadInterleave (IdentityT m) where
interleave :: IdentityT m a -> IdentityT m a
interleave = (m a -> m a) -> IdentityT m a -> IdentityT m a
forall k1 k2 (m :: k1 -> *) (a :: k1) (n :: k2 -> *) (b :: k2).
(m a -> n b) -> IdentityT m a -> IdentityT n b
mapIdentityT m a -> m a
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (MonadInterleave m) => MonadInterleave (ListT m) where
interleave :: ListT m a -> ListT m a
interleave = (m [a] -> m [a]) -> ListT m a -> ListT m a
forall (m :: * -> *) a (n :: * -> *) b.
(m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT m [a] -> m [a]
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (MonadInterleave m) => MonadInterleave (MaybeT m) where
interleave :: MaybeT m a -> MaybeT m a
interleave = (m (Maybe a) -> m (Maybe a)) -> MaybeT m a -> MaybeT m a
forall (m :: * -> *) a (n :: * -> *) b.
(m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
mapMaybeT m (Maybe a) -> m (Maybe a)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (Monoid w, MonadInterleave m) => MonadInterleave (LazyRWS.RWST r w s m) where
interleave :: RWST r w s m a -> RWST r w s m a
interleave = (m (a, s, w) -> m (a, s, w)) -> RWST r w s m a -> RWST r w s m a
forall (m :: * -> *) a s w (n :: * -> *) b w' r.
(m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
LazyRWS.mapRWST m (a, s, w) -> m (a, s, w)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (Monoid w, MonadInterleave m) => MonadInterleave (StrictRWS.RWST r w s m) where
interleave :: RWST r w s m a -> RWST r w s m a
interleave = (m (a, s, w) -> m (a, s, w)) -> RWST r w s m a -> RWST r w s m a
forall (m :: * -> *) a s w (n :: * -> *) b w' r.
(m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
StrictRWS.mapRWST m (a, s, w) -> m (a, s, w)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (MonadInterleave m) => MonadInterleave (ReaderT r m) where
interleave :: ReaderT r m a -> ReaderT r m a
interleave = (m a -> m a) -> ReaderT r m a -> ReaderT r m a
forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT m a -> m a
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (MonadInterleave m) => MonadInterleave (LazyState.StateT s m) where
interleave :: StateT s m a -> StateT s m a
interleave = (m (a, s) -> m (a, s)) -> StateT s m a -> StateT s m a
forall (m :: * -> *) a s (n :: * -> *) b.
(m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
LazyState.mapStateT m (a, s) -> m (a, s)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (MonadInterleave m) => MonadInterleave (StrictState.StateT s m) where
interleave :: StateT s m a -> StateT s m a
interleave = (m (a, s) -> m (a, s)) -> StateT s m a -> StateT s m a
forall (m :: * -> *) a s (n :: * -> *) b.
(m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
StrictState.mapStateT m (a, s) -> m (a, s)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (Monoid w, MonadInterleave m) => MonadInterleave (LazyWriter.WriterT w m) where
interleave :: WriterT w m a -> WriterT w m a
interleave = (m (a, w) -> m (a, w)) -> WriterT w m a -> WriterT w m a
forall (m :: * -> *) a w (n :: * -> *) b w'.
(m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
LazyWriter.mapWriterT m (a, w) -> m (a, w)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
instance (Monoid w, MonadInterleave m) => MonadInterleave (StrictWriter.WriterT w m) where
interleave :: WriterT w m a -> WriterT w m a
interleave = (m (a, w) -> m (a, w)) -> WriterT w m a -> WriterT w m a
forall (m :: * -> *) a w (n :: * -> *) b w'.
(m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
StrictWriter.mapWriterT m (a, w) -> m (a, w)
forall (m :: * -> *) a. MonadInterleave m => m a -> m a
interleave
weighted :: (F.Foldable t, MonadRandom m) => t (a, Rational) -> m a
weighted :: t (a, Rational) -> m a
weighted t (a, Rational)
t = do
Maybe a
ma <- t (a, Rational) -> m (Maybe a)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadRandom m) =>
t (a, Rational) -> m (Maybe a)
weightedMay t (a, Rational)
t
case Maybe a
ma of
Maybe a
Nothing -> [Char] -> m a
forall a. HasCallStack => [Char] -> a
error [Char]
"Control.Monad.Random.Class.weighted: empty collection, or total weight <= 0"
Just a
a -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
weightedMay :: (F.Foldable t, MonadRandom m) => t (a, Rational) -> m (Maybe a)
weightedMay :: t (a, Rational) -> m (Maybe a)
weightedMay = [(a, Rational)] -> m (Maybe a)
forall (m :: * -> *) a.
MonadRandom m =>
[(a, Rational)] -> m (Maybe a)
fromListMay ([(a, Rational)] -> m (Maybe a))
-> (t (a, Rational) -> [(a, Rational)])
-> t (a, Rational)
-> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t (a, Rational) -> [(a, Rational)]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList
fromList :: (MonadRandom m) => [(a, Rational)] -> m a
fromList :: [(a, Rational)] -> m a
fromList [(a, Rational)]
ws = do
Maybe a
ma <- [(a, Rational)] -> m (Maybe a)
forall (m :: * -> *) a.
MonadRandom m =>
[(a, Rational)] -> m (Maybe a)
fromListMay [(a, Rational)]
ws
case Maybe a
ma of
Maybe a
Nothing -> [Char] -> m a
forall a. HasCallStack => [Char] -> a
error [Char]
"Control.Monad.Random.Class.fromList: empty list, or total weight = 0"
Just a
a -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
fromListMay :: (MonadRandom m) => [(a, Rational)] -> m (Maybe a)
fromListMay :: [(a, Rational)] -> m (Maybe a)
fromListMay [(a, Rational)]
xs = do
let s :: Double
s = Rational -> Double
forall a. Fractional a => Rational -> a
fromRational ([Rational] -> Rational
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (((a, Rational) -> Rational) -> [(a, Rational)] -> [Rational]
forall a b. (a -> b) -> [a] -> [b]
map (a, Rational) -> Rational
forall a b. (a, b) -> b
snd [(a, Rational)]
xs)) :: Double
cums :: [(a, Rational)]
cums = ((a, Rational) -> (a, Rational) -> (a, Rational))
-> [(a, Rational)] -> [(a, Rational)]
forall a. (a -> a -> a) -> [a] -> [a]
scanl1 (\ ~(a
_,Rational
q) ~(a
y,Rational
s') -> (a
y, Rational
s'Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+Rational
q)) [(a, Rational)]
xs
case Double
s Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0 of
Bool
True -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
Bool
_ -> do
Rational
p <- (Double -> Rational) -> m Double -> m Rational
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Double -> Rational
forall a. Real a => a -> Rational
toRational (m Double -> m Rational) -> m Double -> m Rational
forall a b. (a -> b) -> a -> b
$ (Double, Double) -> m Double
forall (m :: * -> *) a. (MonadRandom m, Random a) => (a, a) -> m a
getRandomR (Double
0, Double
s)
Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a))
-> ([(a, Rational)] -> Maybe a) -> [(a, Rational)] -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a)
-> ([(a, Rational)] -> a) -> [(a, Rational)] -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, Rational) -> a
forall a b. (a, b) -> a
fst ((a, Rational) -> a)
-> ([(a, Rational)] -> (a, Rational)) -> [(a, Rational)] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(a, Rational)] -> (a, Rational)
forall a. [a] -> a
head ([(a, Rational)] -> (a, Rational))
-> ([(a, Rational)] -> [(a, Rational)])
-> [(a, Rational)]
-> (a, Rational)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, Rational) -> Bool) -> [(a, Rational)] -> [(a, Rational)]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile ((Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
< Rational
p) (Rational -> Bool)
-> ((a, Rational) -> Rational) -> (a, Rational) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, Rational) -> Rational
forall a b. (a, b) -> b
snd) ([(a, Rational)] -> m (Maybe a)) -> [(a, Rational)] -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ [(a, Rational)]
cums
uniform :: (F.Foldable t, MonadRandom m) => t a -> m a
uniform :: t a -> m a
uniform t a
t = do
Maybe a
ma <- t a -> m (Maybe a)
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadRandom m) =>
t a -> m (Maybe a)
uniformMay t a
t
case Maybe a
ma of
Maybe a
Nothing -> [Char] -> m a
forall a. HasCallStack => [Char] -> a
error [Char]
"Control.Monad.Random.Class.uniform: empty collection"
Just a
a -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
uniformMay :: (F.Foldable t, MonadRandom m) => t a -> m (Maybe a)
uniformMay :: t a -> m (Maybe a)
uniformMay = [(a, Rational)] -> m (Maybe a)
forall (m :: * -> *) a.
MonadRandom m =>
[(a, Rational)] -> m (Maybe a)
fromListMay ([(a, Rational)] -> m (Maybe a))
-> (t a -> [(a, Rational)]) -> t a -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (a, Rational)) -> [a] -> [(a, Rational)]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> Rational -> (a, Rational)) -> Rational -> a -> (a, Rational)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (,) Rational
1) ([a] -> [(a, Rational)]) -> (t a -> [a]) -> t a -> [(a, Rational)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList