{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
module Control.Effect.Random
( Random (..),
uniform,
uniformR,
normal,
standard,
exponential,
truncatedExp,
gamma,
chiSquare,
beta,
categorical,
logCategorical,
geometric0,
geometric1,
bernoulli,
dirichlet,
uniformPermutation,
uniformShuffle,
save,
Distrib (..),
MWC.Variate,
Has,
)
where
import Control.Algebra
import Data.Kind
import Data.Vector.Generic (Vector)
import qualified System.Random.MWC as MWC
data Distrib a where
Uniform :: MWC.Variate a => Distrib a
UniformR :: MWC.Variate a => (a, a) -> Distrib a
Normal :: Double -> Double -> Distrib Double
Standard :: Distrib Double
Exponential :: Double -> Distrib Double
TruncatedExp :: Double -> (Double, Double) -> Distrib Double
Gamma :: Double -> Double -> Distrib Double
ChiSquare :: Int -> Distrib Double
Beta :: Double -> Double -> Distrib Double
Categorical :: Vector v Double => v Double -> Distrib Int
LogCategorical :: Vector v Double => v Double -> Distrib Int
Geometric0 :: Double -> Distrib Int
Geometric1 :: Double -> Distrib Int
Bernoulli :: Double -> Distrib Bool
Dirichlet :: Traversable t => t Double -> Distrib (t Double)
Permutation :: Vector v Int => Int -> Distrib (v Int)
Shuffle :: Vector v a => v a -> Distrib (v a)
data Random (m :: Type -> Type) k where
Random :: Distrib a -> Random m a
Save :: Random m MWC.Seed
uniform :: (MWC.Variate a, Has Random sig m) => m a
uniform :: 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 (Distrib a -> Random m a
forall a (m :: * -> *). Distrib a -> Random m a
Random Distrib a
forall a. Variate a => Distrib a
Uniform)
{-# INLINE uniform #-}
uniformR :: (MWC.Variate a, Has Random sig m) => (a, a) -> m a
uniformR :: (a, a) -> m a
uniformR (a, a)
r = Random m a -> m a
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib a -> Random m a
forall a (m :: * -> *). Distrib a -> Random m a
Random ((a, a) -> Distrib a
forall a. Variate a => (a, a) -> Distrib a
UniformR (a, a)
r))
{-# INLINE uniformR #-}
normal ::
Has Random sig m =>
Double ->
Double ->
m Double
normal :: Double -> Double -> m Double
normal Double
m Double
d = Random m Double -> m Double
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Double -> Random m Double
forall a (m :: * -> *). Distrib a -> Random m a
Random (Double -> Double -> Distrib Double
Normal Double
m Double
d))
standard :: Has Random sig m => m Double
standard :: m Double
standard = Random m Double -> m Double
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Double -> Random m Double
forall a (m :: * -> *). Distrib a -> Random m a
Random Distrib Double
Standard)
exponential ::
Has Random sig m =>
Double ->
m Double
exponential :: Double -> m Double
exponential Double
s = Random m Double -> m Double
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Double -> Random m Double
forall a (m :: * -> *). Distrib a -> Random m a
Random (Double -> Distrib Double
Exponential Double
s))
truncatedExp ::
Has Random sig m =>
Double ->
(Double, Double) ->
m (Double)
truncatedExp :: Double -> (Double, Double) -> m Double
truncatedExp Double
s (Double, Double)
r = Random m Double -> m Double
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Double -> Random m Double
forall a (m :: * -> *). Distrib a -> Random m a
Random (Double -> (Double, Double) -> Distrib Double
TruncatedExp Double
s (Double, Double)
r))
gamma ::
Has Random sig m =>
Double ->
Double ->
m Double
gamma :: Double -> Double -> m Double
gamma Double
s Double
d = Random m Double -> m Double
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Double -> Random m Double
forall a (m :: * -> *). Distrib a -> Random m a
Random (Double -> Double -> Distrib Double
Gamma Double
s Double
d))
chiSquare ::
Has Random sig m =>
Int ->
m Double
chiSquare :: Int -> m Double
chiSquare Int
d = Random m Double -> m Double
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Double -> Random m Double
forall a (m :: * -> *). Distrib a -> Random m a
Random (Int -> Distrib Double
ChiSquare Int
d))
geometric0 ::
Has Random sig m =>
Double ->
m Int
geometric0 :: Double -> m Int
geometric0 Double
p = Random m Int -> m Int
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Int -> Random m Int
forall a (m :: * -> *). Distrib a -> Random m a
Random (Double -> Distrib Int
Geometric0 Double
p))
geometric1 ::
Has Random sig m =>
Double ->
m Int
geometric1 :: Double -> m Int
geometric1 Double
p = Random m Int -> m Int
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Int -> Random m Int
forall a (m :: * -> *). Distrib a -> Random m a
Random (Double -> Distrib Int
Geometric1 Double
p))
beta ::
Has Random sig m =>
Double ->
Double ->
m Double
beta :: Double -> Double -> m Double
beta Double
a Double
b = Random m Double -> m Double
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Double -> Random m Double
forall a (m :: * -> *). Distrib a -> Random m a
Random (Double -> Double -> Distrib Double
Beta Double
a Double
b))
{-# INLINE beta #-}
dirichlet ::
(Has Random sig m, Traversable t) =>
t Double ->
m (t Double)
{-# INLINE dirichlet #-}
dirichlet :: t Double -> m (t Double)
dirichlet t Double
t = Random m (t Double) -> m (t Double)
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib (t Double) -> Random m (t Double)
forall a (m :: * -> *). Distrib a -> Random m a
Random (t Double -> Distrib (t Double)
forall (t :: * -> *).
Traversable t =>
t Double -> Distrib (t Double)
Dirichlet t Double
t))
bernoulli ::
Has Random sig m =>
Double ->
m Bool
{-# INLINE bernoulli #-}
bernoulli :: Double -> m Bool
bernoulli Double
p = Random m Bool -> m Bool
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Bool -> Random m Bool
forall a (m :: * -> *). Distrib a -> Random m a
Random (Double -> Distrib Bool
Bernoulli Double
p))
categorical ::
(Has Random sig m, Vector v Double) =>
v Double ->
m Int
{-# INLINE categorical #-}
categorical :: v Double -> m Int
categorical v Double
v = Random m Int -> m Int
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Int -> Random m Int
forall a (m :: * -> *). Distrib a -> Random m a
Random (v Double -> Distrib Int
forall (v :: * -> *). Vector v Double => v Double -> Distrib Int
Categorical v Double
v))
logCategorical ::
(Has Random sig m, Vector v Double) =>
v Double ->
m Int
logCategorical :: v Double -> m Int
logCategorical v Double
v = Random m Int -> m Int
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib Int -> Random m Int
forall a (m :: * -> *). Distrib a -> Random m a
Random (v Double -> Distrib Int
forall (v :: * -> *). Vector v Double => v Double -> Distrib Int
LogCategorical v Double
v))
save :: Has Random sig m => m MWC.Seed
save :: m Seed
save = Random m Seed -> m Seed
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send Random m Seed
forall (m :: * -> *). Random m Seed
Save
uniformPermutation ::
(Has Random sig m, Vector v Int) =>
Int ->
m (v Int)
uniformPermutation :: Int -> m (v Int)
uniformPermutation Int
n = Random m (v Int) -> m (v Int)
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib (v Int) -> Random m (v Int)
forall a (m :: * -> *). Distrib a -> Random m a
Random (Int -> Distrib (v Int)
forall (v :: * -> *). Vector v Int => Int -> Distrib (v Int)
Permutation Int
n))
uniformShuffle ::
(Has Random sig m, Vector v a) =>
v a ->
m (v a)
uniformShuffle :: v a -> m (v a)
uniformShuffle v a
n = Random m (v a) -> m (v a)
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Distrib (v a) -> Random m (v a)
forall a (m :: * -> *). Distrib a -> Random m a
Random (v a -> Distrib (v a)
forall (v :: * -> *) a. Vector v a => v a -> Distrib (v a)
Shuffle v a
n))