{-# LANGUAGE GADTs #-}
module Control.Effect.Random
(
Random(..)
, uniform
, uniformR
, interleave
, exponential
, Algebra
, Has
, run
) where
import Control.Algebra
import qualified System.Random as R (Random(..))
data Random m k where
Uniform :: R.Random a => Random m a
UniformR :: R.Random a => (a, a) -> Random m a
Interleave :: m a -> Random m a
uniform :: (R.Random a, Has Random sig m) => m a
uniform :: forall a (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Random a, Has Random sig m) =>
m a
uniform = Random m a -> m a
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send Random m a
forall a (m :: * -> *). Random a => Random m a
Uniform
{-# INLINE uniform #-}
uniformR :: (R.Random a, Has Random sig m) => (a, a) -> m a
uniformR :: forall a (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Random a, Has Random sig m) =>
(a, a) -> m a
uniformR (a, a)
interval = Random m a -> m a
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send ((a, a) -> Random m a
forall a (m :: * -> *). Random a => (a, a) -> Random m a
UniformR (a, a)
interval)
{-# INLINE uniformR #-}
interleave :: Has Random sig m => m a -> m a
interleave :: forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has Random sig m =>
m a -> m a
interleave m a
m = Random m a -> m a
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (m a -> Random m a
forall (m :: * -> *) a. m a -> Random m a
Interleave m a
m)
{-# INLINE interleave #-}
exponential :: (R.Random a, Floating a, Has Random sig m) => a -> m a
exponential :: forall a (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Random a, Floating a, Has Random sig m) =>
a -> m a
exponential a
a = do
x <- m a
forall a (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Random a, Has Random sig m) =>
m a
uniform
pure $! -log x / a
{-# INLINE exponential #-}