{-# LANGUAGE
    MultiParamTypeClasses, FunctionalDependencies,
    FlexibleContexts, FlexibleInstances,
    UndecidableInstances, EmptyDataDecls,
    TemplateHaskell,
    BangPatterns
  #-}

module Data.Random.Distribution.Uniform
    ( Uniform(..)
    , uniform
    , uniformT

    , StdUniform(..)
    , stdUniform
    , stdUniformT
    , stdUniformPos
    , stdUniformPosT

    , integralUniform
    , realFloatUniform
    , floatUniform
    , doubleUniform
    , fixedUniform
    , enumUniform

    , boundedStdUniform
    , boundedEnumStdUniform
    , realFloatStdUniform
    , fixedStdUniform
    , floatStdUniform
    , doubleStdUniform

    , boundedStdUniformCDF
    , realStdUniformCDF
    , realUniformCDF
    , enumUniformCDF
    ) where


import Data.Random.Internal.Fixed

import Data.Random.Distribution
import Data.Random.RVar

import Data.Fixed
import Data.Word
import Data.Int

import Control.Monad.Loops

import qualified System.Random.Stateful as Random

-- |Compute a random 'Integral' value between the 2 values provided (inclusive).
{-# INLINE integralUniform #-}
integralUniform :: Random.UniformRange a => a -> a -> RVarT m a
integralUniform :: forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform !a
x !a
y = forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
Random.uniformRM (a
x, a
y) RGen
RGen
  -- Maybe switch to uniformIntegralM (requires exposing from `random` internals):
  -- Random.uniformIntegralM (x, y) RGen

integralUniformCDF :: (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF :: forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF a
a a
b a
x
    | a
b forall a. Ord a => a -> a -> Bool
< a
a     = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF a
b a
a a
x
    | a
x forall a. Ord a => a -> a -> Bool
< a
a     = b
0
    | a
x forall a. Ord a => a -> a -> Bool
> a
b     = b
1
    | Bool
otherwise = (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
x forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral a
a) forall a. Fractional a => a -> a -> a
/ (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
b forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral a
a)

-- |Compute a random value for a 'Bounded' type, between 'minBound' and 'maxBound'
-- (inclusive for 'Integral' or 'Enum' types, in ['minBound', 'maxBound') for Fractional types.)
boundedStdUniform :: (Distribution Uniform a, Bounded a) => RVar a
boundedStdUniform :: forall a. (Distribution Uniform a, Bounded a) => RVar a
boundedStdUniform = forall a. Distribution Uniform a => a -> a -> RVar a
uniform forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound

boundedStdUniformCDF :: (CDF Uniform a, Bounded a) => a -> Double
boundedStdUniformCDF :: forall a. (CDF Uniform a, Bounded a) => a -> Double
boundedStdUniformCDF = forall (d :: * -> *) t. CDF d t => d t -> t -> Double
cdf (forall t. t -> t -> Uniform t
Uniform forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound)

-- |Compute a random value for a 'Bounded' 'Enum' type, between 'minBound' and
-- 'maxBound' (inclusive)
boundedEnumStdUniform :: (Enum a, Bounded a) => RVarT m a
boundedEnumStdUniform :: forall a (m :: * -> *). (Enum a, Bounded a) => RVarT m a
boundedEnumStdUniform = forall a (m :: * -> *). Enum a => a -> a -> RVarT m a
enumUniform forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound

boundedEnumStdUniformCDF :: (Enum a, Bounded a, Ord a) => a -> Double
boundedEnumStdUniformCDF :: forall a. (Enum a, Bounded a, Ord a) => a -> Double
boundedEnumStdUniformCDF = forall a. (Enum a, Ord a) => a -> a -> a -> Double
enumUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound

-- |Compute a uniform random 'Float' value in the range [0,1)
floatStdUniform :: RVarT m Float
floatStdUniform :: forall (m :: * -> *). RVarT m Float
floatStdUniform = do
    Float
x <- forall a (m :: * -> *). UniformRange a => (a, a) -> RVarT m a
uniformRangeRVarT (Float
0, Float
1)
    -- exclude 1. TODO: come up with something smarter
    if Float
x forall a. Eq a => a -> a -> Bool
== Float
1 then forall (m :: * -> *). RVarT m Float
floatStdUniform else forall (f :: * -> *) a. Applicative f => a -> f a
pure Float
x

-- |Compute a uniform random 'Double' value in the range [0,1)
{-# INLINE doubleStdUniform #-}
doubleStdUniform :: RVarT m Double
doubleStdUniform :: forall (m :: * -> *). RVarT m Double
doubleStdUniform = do
    Double
x <- forall a (m :: * -> *). UniformRange a => (a, a) -> RVarT m a
uniformRangeRVarT (Double
0, Double
1)
    -- exclude 1. TODO: come up with something smarter
    if Double
x forall a. Eq a => a -> a -> Bool
== Double
1 then forall (m :: * -> *). RVarT m Double
doubleStdUniform else forall (f :: * -> *) a. Applicative f => a -> f a
pure Double
x

-- |Compute a uniform random value in the range [0,1) for any 'RealFloat' type
realFloatStdUniform :: RealFloat a => RVarT m a
realFloatStdUniform :: forall a (m :: * -> *). RealFloat a => RVarT m a
realFloatStdUniform = do
    let (Integer
b, Int
e) = forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
one

    Integer
x <- forall a (m :: * -> *).
Distribution Uniform a =>
a -> a -> RVarT m a
uniformT Integer
0 (Integer
bforall a. Num a => a -> a -> a
-Integer
1)
    if Integer
x forall a. Eq a => a -> a -> Bool
== Integer
0
        then forall (m :: * -> *) a. Monad m => a -> m a
return (a
0 forall a. a -> a -> a
`asTypeOf` a
one)
        else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. RealFloat a => Integer -> Int -> a
encodeFloat Integer
x Int
e)

    where one :: a
one = a
1

-- |Compute a uniform random 'Fixed' value in the range [0,1), with any
-- desired precision.
fixedStdUniform :: HasResolution r => RVarT m (Fixed r)
fixedStdUniform :: forall r (m :: * -> *). HasResolution r => RVarT m (Fixed r)
fixedStdUniform = forall {m :: * -> *}. RVarT m (Fixed r)
x
    where
        res :: Integer
res = forall r (f :: * -> *) (g :: * -> *).
HasResolution r =>
f (g r) -> Integer
resolutionOf2 RVarT m (Fixed r)
x
        x :: RVarT m (Fixed r)
x = do
            Integer
u <- forall a (m :: * -> *).
Distribution Uniform a =>
a -> a -> RVarT m a
uniformT Integer
0 (Integer
res)
            forall (m :: * -> *) a. Monad m => a -> m a
return (forall r. Integer -> Fixed r
mkFixed Integer
u)

-- |The CDF of the random variable 'realFloatStdUniform'.
realStdUniformCDF :: Real a => a -> Double
realStdUniformCDF :: forall a. Real a => a -> Double
realStdUniformCDF a
x
    | a
x forall a. Ord a => a -> a -> Bool
<= a
0    = Double
0
    | a
x forall a. Ord a => a -> a -> Bool
>= a
1    = Double
1
    | Bool
otherwise = forall a b. (Real a, Fractional b) => a -> b
realToFrac a
x

-- |The PDF of the random variable 'realFloatStdUniform'.
realStdUniformPDF :: Real a => a -> Double
realStdUniformPDF :: forall a. Real a => a -> Double
realStdUniformPDF a
x
    | a
x forall a. Ord a => a -> a -> Bool
<= a
0    = Double
0
    | a
x forall a. Ord a => a -> a -> Bool
>= a
1    = Double
0
    | Bool
otherwise = Double
1

-- |(internal) basic linear interpolation; @lerp x y@ is a linear function whose
-- value is @x@ at 0 and @y@ at 1
lerp :: Num a => a -> a -> a -> a
lerp :: forall a. Num a => a -> a -> a -> a
lerp a
x a
y a
a = (a
1forall a. Num a => a -> a -> a
-a
a)forall a. Num a => a -> a -> a
*a
x forall a. Num a => a -> a -> a
+ a
aforall a. Num a => a -> a -> a
*a
y

-- |@floatUniform a b@ computes a uniform random 'Float' value in the range [a,b)
floatUniform :: Float -> Float -> RVarT m Float
floatUniform :: forall (m :: * -> *). Float -> Float -> RVarT m Float
floatUniform Float
0 Float
1 = forall (m :: * -> *). RVarT m Float
floatStdUniform
floatUniform Float
a Float
b = do
    Float
x <- forall (m :: * -> *). RVarT m Float
floatStdUniform
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Num a => a -> a -> a -> a
lerp Float
a Float
b Float
x)

-- |@doubleUniform a b@ computes a uniform random 'Double' value in the range [a,b)
{-# INLINE doubleUniform #-}
doubleUniform :: Double -> Double -> RVarT m Double
doubleUniform :: forall (m :: * -> *). Double -> Double -> RVarT m Double
doubleUniform Double
0 Double
1 = forall (m :: * -> *). RVarT m Double
doubleStdUniform
doubleUniform Double
a Double
b = do
    Double
x <- forall (m :: * -> *). RVarT m Double
doubleStdUniform
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Num a => a -> a -> a -> a
lerp Double
a Double
b Double
x)

-- |@realFloatUniform a b@ computes a uniform random value in the range [a,b) for
-- any 'RealFloat' type
realFloatUniform :: RealFloat a => a -> a -> RVarT m a
realFloatUniform :: forall a (m :: * -> *). RealFloat a => a -> a -> RVarT m a
realFloatUniform a
0 a
1 = forall a (m :: * -> *). RealFloat a => RVarT m a
realFloatStdUniform
realFloatUniform a
a a
b = do
    a
x <- forall a (m :: * -> *). RealFloat a => RVarT m a
realFloatStdUniform
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Num a => a -> a -> a -> a
lerp a
a a
b a
x)

-- |@fixedUniform a b@ computes a uniform random 'Fixed' value in the range
-- [a,b), with any desired precision.
fixedUniform :: HasResolution r => Fixed r -> Fixed r -> RVarT m (Fixed r)
fixedUniform :: forall r (m :: * -> *).
HasResolution r =>
Fixed r -> Fixed r -> RVarT m (Fixed r)
fixedUniform Fixed r
a Fixed r
b = do
    Integer
u <- forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform (forall r. Fixed r -> Integer
unMkFixed Fixed r
a) (forall r. Fixed r -> Integer
unMkFixed Fixed r
b)
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall r. Integer -> Fixed r
mkFixed Integer
u)

-- |@realUniformCDF a b@ is the CDF of the random variable @realFloatUniform a b@.
realUniformCDF :: RealFrac a => a -> a -> a -> Double
realUniformCDF :: forall a. RealFrac a => a -> a -> a -> Double
realUniformCDF a
a a
b a
x
    | a
b forall a. Ord a => a -> a -> Bool
< a
a     = forall a. RealFrac a => a -> a -> a -> Double
realUniformCDF a
b a
a a
x
    | a
x forall a. Ord a => a -> a -> Bool
<= a
a    = Double
0
    | a
x forall a. Ord a => a -> a -> Bool
>= a
b    = Double
1
    | Bool
otherwise = forall a b. (Real a, Fractional b) => a -> b
realToFrac ((a
xforall a. Num a => a -> a -> a
-a
a) forall a. Fractional a => a -> a -> a
/ (a
bforall a. Num a => a -> a -> a
-a
a))

-- |@realFloatUniform a b@ computes a uniform random value in the range [a,b) for
-- any 'Enum' type
enumUniform :: Enum a => a -> a -> RVarT m a
enumUniform :: forall a (m :: * -> *). Enum a => a -> a -> RVarT m a
enumUniform a
a a
b = do
    Int
x <- forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform (forall a. Enum a => a -> Int
fromEnum a
a) (forall a. Enum a => a -> Int
fromEnum a
b)
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Enum a => Int -> a
toEnum Int
x)

enumUniformCDF :: (Enum a, Ord a) => a -> a -> a -> Double
enumUniformCDF :: forall a. (Enum a, Ord a) => a -> a -> a -> Double
enumUniformCDF a
a a
b a
x
    | a
b forall a. Ord a => a -> a -> Bool
< a
a     = forall a. (Enum a, Ord a) => a -> a -> a -> Double
enumUniformCDF a
b a
a a
x
    | a
x forall a. Ord a => a -> a -> Bool
<= a
a    = Double
0
    | a
x forall a. Ord a => a -> a -> Bool
>= a
b    = Double
1
    | Bool
otherwise = (a -> Double
e2f a
x forall a. Num a => a -> a -> a
- a -> Double
e2f a
a) forall a. Fractional a => a -> a -> a
/ (a -> Double
e2f a
b forall a. Num a => a -> a -> a
- a -> Double
e2f a
a)

    where e2f :: a -> Double
e2f = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum

-- @uniform a b@ is a uniformly distributed random variable in the range
-- [a,b] for 'Integral' or 'Enum' types and in the range [a,b) for 'Fractional'
-- types.  Requires a @Distribution Uniform@ instance for the type.
uniform :: Distribution Uniform a => a -> a -> RVar a
uniform :: forall a. Distribution Uniform a => a -> a -> RVar a
uniform a
a a
b = forall (d :: * -> *) t. Distribution d t => d t -> RVar t
rvar (forall t. t -> t -> Uniform t
Uniform a
a a
b)

-- @uniformT a b@ is a uniformly distributed random process in the range
-- [a,b] for 'Integral' or 'Enum' types and in the range [a,b) for 'Fractional'
-- types.  Requires a @Distribution Uniform@ instance for the type.
uniformT :: Distribution Uniform a => a -> a -> RVarT m a
uniformT :: forall a (m :: * -> *).
Distribution Uniform a =>
a -> a -> RVarT m a
uniformT a
a a
b = forall (d :: * -> *) t (n :: * -> *).
Distribution d t =>
d t -> RVarT n t
rvarT (forall t. t -> t -> Uniform t
Uniform a
a a
b)

-- |Get a \"standard\" uniformly distributed variable.
-- For integral types, this means uniformly distributed over the full range
-- of the type (there is no support for 'Integer').  For fractional
-- types, this means uniformly distributed on the interval [0,1).
{-# SPECIALIZE stdUniform :: RVar Double #-}
{-# SPECIALIZE stdUniform :: RVar Float #-}
stdUniform :: (Distribution StdUniform a) => RVar a
stdUniform :: forall a. Distribution StdUniform a => RVar a
stdUniform = forall (d :: * -> *) t. Distribution d t => d t -> RVar t
rvar forall t. StdUniform t
StdUniform

-- |Get a \"standard\" uniformly distributed process.
-- For integral types, this means uniformly distributed over the full range
-- of the type (there is no support for 'Integer').  For fractional
-- types, this means uniformly distributed on the interval [0,1).
{-# SPECIALIZE stdUniformT :: RVarT m Double #-}
{-# SPECIALIZE stdUniformT :: RVarT m Float #-}
stdUniformT :: (Distribution StdUniform a) => RVarT m a
stdUniformT :: forall a (m :: * -> *). Distribution StdUniform a => RVarT m a
stdUniformT = forall (d :: * -> *) t (n :: * -> *).
Distribution d t =>
d t -> RVarT n t
rvarT forall t. StdUniform t
StdUniform

-- |Like 'stdUniform', but returns only positive or zero values.  Not
-- exported because it is not truly uniform: nonzero values are twice
-- as likely as zero on signed types.
stdUniformNonneg :: (Distribution StdUniform a, Num a, Eq a) => RVarT m a
stdUniformNonneg :: forall a (m :: * -> *).
(Distribution StdUniform a, Num a, Eq a) =>
RVarT m a
stdUniformNonneg = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
abs forall a (m :: * -> *). Distribution StdUniform a => RVarT m a
stdUniformT

-- |Like 'stdUniform' but only returns positive values.
stdUniformPos :: (Distribution StdUniform a, Num a, Eq a) => RVar a
stdUniformPos :: forall a. (Distribution StdUniform a, Num a, Eq a) => RVar a
stdUniformPos = forall a (m :: * -> *).
(Distribution StdUniform a, Num a, Eq a) =>
RVarT m a
stdUniformPosT

-- |Like 'stdUniform' but only returns positive values.
stdUniformPosT :: (Distribution StdUniform a, Num a, Eq a) => RVarT m a
stdUniformPosT :: forall a (m :: * -> *).
(Distribution StdUniform a, Num a, Eq a) =>
RVarT m a
stdUniformPosT = forall (m :: * -> *) a. Monad m => (a -> Bool) -> m a -> m a
iterateUntil (forall a. Eq a => a -> a -> Bool
/= a
0) forall a (m :: * -> *).
(Distribution StdUniform a, Num a, Eq a) =>
RVarT m a
stdUniformNonneg

-- |A definition of a uniform distribution over the type @t@.  See also 'uniform'.
data Uniform t =
    -- |A uniform distribution defined by a lower and upper range bound.
    -- For 'Integral' and 'Enum' types, the range is inclusive.  For 'Fractional'
    -- types the range includes the lower bound but not the upper.
    Uniform !t !t

-- |A name for the \"standard\" uniform distribution over the type @t@,
-- if one exists.  See also 'stdUniform'.
--
-- For 'Integral' and 'Enum' types that are also 'Bounded', this is
-- the uniform distribution over the full range of the type.
-- For un-'Bounded' 'Integral' types this is not defined.
-- For 'Fractional' types this is a random variable in the range [0,1)
-- (that is, 0 to 1 including 0 but not including 1).
data StdUniform t = StdUniform

instance Distribution Uniform Integer where rvarT :: forall (n :: * -> *). Uniform Integer -> RVarT n Integer
rvarT (Uniform Integer
a Integer
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Integer
a Integer
b
instance CDF Uniform Integer          where cdf :: Uniform Integer -> Integer -> Double
cdf   (Uniform Integer
a Integer
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Integer
a Integer
b
instance Distribution Uniform Int     where rvarT :: forall (n :: * -> *). Uniform Int -> RVarT n Int
rvarT (Uniform Int
a Int
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Int
a Int
b
instance CDF Uniform Int              where cdf :: Uniform Int -> Int -> Double
cdf   (Uniform Int
a Int
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Int
a Int
b
instance Distribution Uniform Int8    where rvarT :: forall (n :: * -> *). Uniform Int8 -> RVarT n Int8
rvarT (Uniform Int8
a Int8
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Int8
a Int8
b
instance CDF Uniform Int8             where cdf :: Uniform Int8 -> Int8 -> Double
cdf   (Uniform Int8
a Int8
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Int8
a Int8
b
instance Distribution Uniform Int16   where rvarT :: forall (n :: * -> *). Uniform Int16 -> RVarT n Int16
rvarT (Uniform Int16
a Int16
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Int16
a Int16
b
instance CDF Uniform Int16            where cdf :: Uniform Int16 -> Int16 -> Double
cdf   (Uniform Int16
a Int16
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Int16
a Int16
b
instance Distribution Uniform Int32   where rvarT :: forall (n :: * -> *). Uniform Int32 -> RVarT n Int32
rvarT (Uniform Int32
a Int32
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Int32
a Int32
b
instance CDF Uniform Int32            where cdf :: Uniform Int32 -> Int32 -> Double
cdf   (Uniform Int32
a Int32
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Int32
a Int32
b
instance Distribution Uniform Int64   where rvarT :: forall (n :: * -> *). Uniform Int64 -> RVarT n Int64
rvarT (Uniform Int64
a Int64
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Int64
a Int64
b
instance CDF Uniform Int64            where cdf :: Uniform Int64 -> Int64 -> Double
cdf   (Uniform Int64
a Int64
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Int64
a Int64
b
instance Distribution Uniform Word    where rvarT :: forall (n :: * -> *). Uniform Word -> RVarT n Word
rvarT (Uniform Word
a Word
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Word
a Word
b
instance CDF Uniform Word             where cdf :: Uniform Word -> Word -> Double
cdf   (Uniform Word
a Word
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Word
a Word
b
instance Distribution Uniform Word8   where rvarT :: forall (n :: * -> *). Uniform Word8 -> RVarT n Word8
rvarT (Uniform Word8
a Word8
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Word8
a Word8
b
instance CDF Uniform Word8            where cdf :: Uniform Word8 -> Word8 -> Double
cdf   (Uniform Word8
a Word8
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Word8
a Word8
b
instance Distribution Uniform Word16  where rvarT :: forall (n :: * -> *). Uniform Word16 -> RVarT n Word16
rvarT (Uniform Word16
a Word16
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Word16
a Word16
b
instance CDF Uniform Word16           where cdf :: Uniform Word16 -> Word16 -> Double
cdf   (Uniform Word16
a Word16
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Word16
a Word16
b
instance Distribution Uniform Word32  where rvarT :: forall (n :: * -> *). Uniform Word32 -> RVarT n Word32
rvarT (Uniform Word32
a Word32
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Word32
a Word32
b
instance CDF Uniform Word32           where cdf :: Uniform Word32 -> Word32 -> Double
cdf   (Uniform Word32
a Word32
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Word32
a Word32
b
instance Distribution Uniform Word64  where rvarT :: forall (n :: * -> *). Uniform Word64 -> RVarT n Word64
rvarT (Uniform Word64
a Word64
b) = forall a (m :: * -> *). UniformRange a => a -> a -> RVarT m a
integralUniform Word64
a Word64
b
instance CDF Uniform Word64           where cdf :: Uniform Word64 -> Word64 -> Double
cdf   (Uniform Word64
a Word64
b) = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF Word64
a Word64
b

instance Distribution StdUniform Word8      where rvarT :: forall (n :: * -> *). StdUniform Word8 -> RVarT n Word8
rvarT StdUniform Word8
_ = forall g (m :: * -> *). StatefulGen g m => g -> m Word8
Random.uniformWord8 RGen
RGen
instance Distribution StdUniform Word16     where rvarT :: forall (n :: * -> *). StdUniform Word16 -> RVarT n Word16
rvarT StdUniform Word16
_ = forall g (m :: * -> *). StatefulGen g m => g -> m Word16
Random.uniformWord16 RGen
RGen
instance Distribution StdUniform Word32     where rvarT :: forall (n :: * -> *). StdUniform Word32 -> RVarT n Word32
rvarT StdUniform Word32
_ = forall g (m :: * -> *). StatefulGen g m => g -> m Word32
Random.uniformWord32 RGen
RGen
instance Distribution StdUniform Word64     where rvarT :: forall (n :: * -> *). StdUniform Word64 -> RVarT n Word64
rvarT StdUniform Word64
_ = forall g (m :: * -> *). StatefulGen g m => g -> m Word64
Random.uniformWord64 RGen
RGen
instance Distribution StdUniform Word       where rvarT :: forall (n :: * -> *). StdUniform Word -> RVarT n Word
rvarT StdUniform Word
_ = forall a (m :: * -> *). Uniform a => RVarT m a
uniformRVarT

instance Distribution StdUniform Int8       where rvarT :: forall (n :: * -> *). StdUniform Int8 -> RVarT n Int8
rvarT StdUniform Int8
_ = forall a (m :: * -> *). Uniform a => RVarT m a
uniformRVarT
instance Distribution StdUniform Int16      where rvarT :: forall (n :: * -> *). StdUniform Int16 -> RVarT n Int16
rvarT StdUniform Int16
_ = forall a (m :: * -> *). Uniform a => RVarT m a
uniformRVarT
instance Distribution StdUniform Int32      where rvarT :: forall (n :: * -> *). StdUniform Int32 -> RVarT n Int32
rvarT StdUniform Int32
_ = forall a (m :: * -> *). Uniform a => RVarT m a
uniformRVarT
instance Distribution StdUniform Int64      where rvarT :: forall (n :: * -> *). StdUniform Int64 -> RVarT n Int64
rvarT StdUniform Int64
_ = forall a (m :: * -> *). Uniform a => RVarT m a
uniformRVarT

instance Distribution StdUniform Int        where rvarT :: forall (n :: * -> *). StdUniform Int -> RVarT n Int
rvarT StdUniform Int
_ = forall a (m :: * -> *). Uniform a => RVarT m a
uniformRVarT


-- Integer has no StdUniform...

instance CDF StdUniform Word8   where cdf :: StdUniform Word8 -> Word8 -> Double
cdf StdUniform Word8
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Word16  where cdf :: StdUniform Word16 -> Word16 -> Double
cdf StdUniform Word16
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Word32  where cdf :: StdUniform Word32 -> Word32 -> Double
cdf StdUniform Word32
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Word64  where cdf :: StdUniform Word64 -> Word64 -> Double
cdf StdUniform Word64
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Word    where cdf :: StdUniform Word -> Word -> Double
cdf StdUniform Word
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Int8    where cdf :: StdUniform Int8 -> Int8 -> Double
cdf StdUniform Int8
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Int16   where cdf :: StdUniform Int16 -> Int16 -> Double
cdf StdUniform Int16
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Int32   where cdf :: StdUniform Int32 -> Int32 -> Double
cdf StdUniform Int32
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Int64   where cdf :: StdUniform Int64 -> Int64 -> Double
cdf StdUniform Int64
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound
instance CDF StdUniform Int     where cdf :: StdUniform Int -> Int -> Double
cdf StdUniform Int
_ = forall a b. (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound


instance Distribution Uniform Float         where rvarT :: forall (n :: * -> *). Uniform Float -> RVarT n Float
rvarT (Uniform Float
a Float
b) = forall (m :: * -> *). Float -> Float -> RVarT m Float
floatUniform  Float
a Float
b
instance Distribution Uniform Double        where rvarT :: forall (n :: * -> *). Uniform Double -> RVarT n Double
rvarT (Uniform Double
a Double
b) = forall (m :: * -> *). Double -> Double -> RVarT m Double
doubleUniform Double
a Double
b
instance CDF Uniform Float                  where cdf :: Uniform Float -> Float -> Double
cdf   (Uniform Float
a Float
b) = forall a. RealFrac a => a -> a -> a -> Double
realUniformCDF Float
a Float
b
instance CDF Uniform Double                 where cdf :: Uniform Double -> Double -> Double
cdf   (Uniform Double
a Double
b) = forall a. RealFrac a => a -> a -> a -> Double
realUniformCDF Double
a Double
b

instance Distribution StdUniform Float      where rvarT :: forall (n :: * -> *). StdUniform Float -> RVarT n Float
rvarT StdUniform Float
_ = forall (m :: * -> *). RVarT m Float
floatStdUniform
instance Distribution StdUniform Double     where rvarT :: forall (n :: * -> *). StdUniform Double -> RVarT n Double
rvarT StdUniform Double
_ = forall a (m :: * -> *). UniformRange a => (a, a) -> RVarT m a
uniformRangeRVarT (Double
0, Double
1)
instance CDF StdUniform Float               where cdf :: StdUniform Float -> Float -> Double
cdf   StdUniform Float
_ = forall a. Real a => a -> Double
realStdUniformCDF
instance CDF StdUniform Double              where cdf :: StdUniform Double -> Double -> Double
cdf   StdUniform Double
_ = forall a. Real a => a -> Double
realStdUniformCDF
instance PDF StdUniform Float               where pdf :: StdUniform Float -> Float -> Double
pdf   StdUniform Float
_ = forall a. Real a => a -> Double
realStdUniformPDF
instance PDF StdUniform Double              where pdf :: StdUniform Double -> Double -> Double
pdf   StdUniform Double
_ = forall a. Real a => a -> Double
realStdUniformPDF


instance HasResolution r =>
         Distribution Uniform (Fixed r)     where rvarT :: forall (n :: * -> *). Uniform (Fixed r) -> RVarT n (Fixed r)
rvarT (Uniform Fixed r
a Fixed r
b) = forall r (m :: * -> *).
HasResolution r =>
Fixed r -> Fixed r -> RVarT m (Fixed r)
fixedUniform  Fixed r
a Fixed r
b
instance HasResolution r =>
         CDF Uniform (Fixed r)              where cdf :: Uniform (Fixed r) -> Fixed r -> Double
cdf   (Uniform Fixed r
a Fixed r
b) = forall a. RealFrac a => a -> a -> a -> Double
realUniformCDF Fixed r
a Fixed r
b
instance HasResolution r =>
         Distribution StdUniform (Fixed r)  where rvarT :: forall (n :: * -> *). StdUniform (Fixed r) -> RVarT n (Fixed r)
rvarT ~StdUniform (Fixed r)
StdUniform = forall r (m :: * -> *). HasResolution r => RVarT m (Fixed r)
fixedStdUniform
instance HasResolution r =>
         CDF StdUniform (Fixed r)           where cdf :: StdUniform (Fixed r) -> Fixed r -> Double
cdf   ~StdUniform (Fixed r)
StdUniform = forall a. Real a => a -> Double
realStdUniformCDF

instance Distribution Uniform ()            where rvarT :: forall (n :: * -> *). Uniform () -> RVarT n ()
rvarT (Uniform ()
_ ()
_) = forall (m :: * -> *) a. Monad m => a -> m a
return ()
instance CDF Uniform ()                     where cdf :: Uniform () -> () -> Double
cdf   (Uniform ()
_ ()
_) = forall (m :: * -> *) a. Monad m => a -> m a
return Double
1

instance Distribution Uniform Char     where rvarT :: forall (n :: * -> *). Uniform Char -> RVarT n Char
rvarT (Uniform Char
a Char
b) = forall a (m :: * -> *). Enum a => a -> a -> RVarT m a
enumUniform Char
a Char
b
instance CDF Uniform Char              where cdf :: Uniform Char -> Char -> Double
cdf   (Uniform Char
a Char
b) = forall a. (Enum a, Ord a) => a -> a -> a -> Double
enumUniformCDF Char
a Char
b
instance Distribution Uniform Bool     where rvarT :: forall (n :: * -> *). Uniform Bool -> RVarT n Bool
rvarT (Uniform Bool
a Bool
b) = forall a (m :: * -> *). Enum a => a -> a -> RVarT m a
enumUniform Bool
a Bool
b
instance CDF Uniform Bool              where cdf :: Uniform Bool -> Bool -> Double
cdf   (Uniform Bool
a Bool
b) = forall a. (Enum a, Ord a) => a -> a -> a -> Double
enumUniformCDF Bool
a Bool
b
instance Distribution Uniform Ordering where rvarT :: forall (n :: * -> *). Uniform Ordering -> RVarT n Ordering
rvarT (Uniform Ordering
a Ordering
b) = forall a (m :: * -> *). Enum a => a -> a -> RVarT m a
enumUniform Ordering
a Ordering
b
instance CDF Uniform Ordering          where cdf :: Uniform Ordering -> Ordering -> Double
cdf   (Uniform Ordering
a Ordering
b) = forall a. (Enum a, Ord a) => a -> a -> a -> Double
enumUniformCDF Ordering
a Ordering
b

instance Distribution StdUniform ()         where rvarT :: forall (n :: * -> *). StdUniform () -> RVarT n ()
rvarT ~StdUniform ()
StdUniform = forall (m :: * -> *) a. Monad m => a -> m a
return ()
instance CDF StdUniform ()                  where cdf :: StdUniform () -> () -> Double
cdf   ~StdUniform ()
StdUniform = forall (m :: * -> *) a. Monad m => a -> m a
return Double
1
instance Distribution StdUniform Bool       where rvarT :: forall (n :: * -> *). StdUniform Bool -> RVarT n Bool
rvarT ~StdUniform Bool
StdUniform = forall a (m :: * -> *). Uniform a => RVarT m a
uniformRVarT
instance CDF StdUniform Bool                where cdf :: StdUniform Bool -> Bool -> Double
cdf   ~StdUniform Bool
StdUniform = forall a. (Enum a, Bounded a, Ord a) => a -> Double
boundedEnumStdUniformCDF

instance Distribution StdUniform Char       where rvarT :: forall (n :: * -> *). StdUniform Char -> RVarT n Char
rvarT ~StdUniform Char
StdUniform = forall a (m :: * -> *). (Enum a, Bounded a) => RVarT m a
boundedEnumStdUniform
instance CDF StdUniform Char                where cdf :: StdUniform Char -> Char -> Double
cdf   ~StdUniform Char
StdUniform = forall a. (Enum a, Bounded a, Ord a) => a -> Double
boundedEnumStdUniformCDF
instance Distribution StdUniform Ordering   where rvarT :: forall (n :: * -> *). StdUniform Ordering -> RVarT n Ordering
rvarT ~StdUniform Ordering
StdUniform = forall a (m :: * -> *). (Enum a, Bounded a) => RVarT m a
boundedEnumStdUniform
instance CDF StdUniform Ordering            where cdf :: StdUniform Ordering -> Ordering -> Double
cdf   ~StdUniform Ordering
StdUniform = forall a. (Enum a, Bounded a, Ord a) => a -> Double
boundedEnumStdUniformCDF