-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Generic random generators -- -- Please see the README below. @package generic-random @version 0.1.0.0 -- | Solve systems of equations module Data.Random.Generics.Internal.Solver data SolveArgs SolveArgs :: Double -> Int -> SolveArgs [accuracy] :: SolveArgs -> Double [numIterations] :: SolveArgs -> Int defSolveArgs :: SolveArgs findZero :: SolveArgs -> (forall s. Vector (AD s (Forward R)) -> Vector (AD s (Forward R))) -> Vector R -> Maybe (Vector R) fixedPoint :: SolveArgs -> (forall a. (Mode a, Scalar a ~ R) => Vector a -> Vector a) -> Vector R -> Maybe (Vector R) -- | Assuming p . f is satisfied only for positive values in some -- interval (0, r], find f r. search :: (Double -> a) -> (a -> Bool) -> a instance GHC.Show.Show Data.Random.Generics.Internal.Solver.SolveArgs instance GHC.Classes.Ord Data.Random.Generics.Internal.Solver.SolveArgs instance GHC.Classes.Eq Data.Random.Generics.Internal.Solver.SolveArgs module Data.Random.Generics.Internal.Types data SomeData m SomeData :: m a -> SomeData m type SomeData' = SomeData Proxy -- | Dummy instance for debugging. data Alias m Alias :: !(m a -> m b) -> Alias m type AliasR m = Alias (RejectT m) -- | Dummy instance for debugging. -- | Main constructor for Alias. alias :: (Monad m, Data a, Data b) => (a -> m b) -> Alias m -- | Main constructor for AliasR. aliasR :: (Monad m, Data a, Data b) => (a -> m b) -> AliasR m -- |
-- coerceAlias :: Alias m -> Alias (AMonadRandom m) --coerceAlias :: Coercible m n => Alias m -> Alias n -- |
-- coerceAliases :: [Alias m] -> [Alias (AMonadRandom m)] --coerceAliases :: Coercible m n => [Alias m] -> [Alias n] -- |
-- composeCast f g = f . g --composeCastM :: (?loc :: CallStack, Typeable b, Typeable c) => (m c -> d) -> (a -> m b) -> (a -> d) castM :: (?loc :: CallStack, Typeable a, Typeable b) => m a -> m b unSomeData :: (?loc :: CallStack, Typeable a) => SomeData m -> m a applyCast :: (Typeable a, Data b) => (m a -> m b) -> SomeData m -> SomeData m castError :: (?loc :: CallStack, Typeable a, Typeable b) => proxy a -> proxy' b -> c withProxy :: (?loc :: CallStack) => (a -> b) -> proxy a -> b reproxy :: proxy a -> Proxy a proxyType :: m a -> proxy a -> m a someData' :: Data a => proxy a -> SomeData' -- | Size as the number of constructors. type Size = Int -- | Internal transformer for rejection sampling. -- --
-- ReaderT Size (StateT Size (MaybeT m)) a --newtype RejectT m a RejectT :: (forall r. Size -> Size -> m r -> (Size -> a -> m r) -> m r) -> RejectT m a [unRejectT] :: RejectT m a -> forall r. Size -> Size -> m r -> (Size -> a -> m r) -> m r -- | Set lower bound runRejectT :: Monad m => (Size, Size) -> RejectT m a -> m a newtype AMonadRandom m a AMonadRandom :: m a -> AMonadRandom m a [asMonadRandom] :: AMonadRandom m a -> m a -- | MonadRandomLike m defines basic components to build -- generators, allowing the implementation to remain abstract over both -- the Gen type and MonadRandom instances. -- -- For the latter, the wrapper AMonadRandom is provided to avoid -- overlapping instances. class Monad m => MonadRandomLike m where incr = return () -- | Called for every constructor. Counter for ceiled rejection sampling. incr :: MonadRandomLike m => m () -- | doubleR upperBound: generates values in [0, -- upperBound]. doubleR :: MonadRandomLike m => Double -> m Double -- | integerR upperBound: generates values in [0, -- upperBound-1]. integerR :: MonadRandomLike m => Integer -> m Integer -- | Default Int generator. int :: MonadRandomLike m => m Int -- | Default Double generator. double :: MonadRandomLike m => m Double -- | Default Char generator. char :: MonadRandomLike m => m Char instance GHC.Base.Monad m => GHC.Base.Monad (Data.Random.Generics.Internal.Types.AMonadRandom m) instance GHC.Base.Applicative m => GHC.Base.Applicative (Data.Random.Generics.Internal.Types.AMonadRandom m) instance GHC.Base.Functor m => GHC.Base.Functor (Data.Random.Generics.Internal.Types.AMonadRandom m) instance GHC.Show.Show (Data.Random.Generics.Internal.Types.SomeData m) instance GHC.Show.Show (Data.Random.Generics.Internal.Types.Alias m) instance GHC.Base.Functor (Data.Random.Generics.Internal.Types.RejectT m) instance GHC.Base.Applicative (Data.Random.Generics.Internal.Types.RejectT m) instance GHC.Base.Monad (Data.Random.Generics.Internal.Types.RejectT m) instance Control.Monad.Trans.Class.MonadTrans Data.Random.Generics.Internal.Types.RejectT instance Control.Monad.Trans.Class.MonadTrans Data.Random.Generics.Internal.Types.AMonadRandom instance Data.Random.Generics.Internal.Types.MonadRandomLike Test.QuickCheck.Gen.Gen instance Data.Random.Generics.Internal.Types.MonadRandomLike m => Data.Random.Generics.Internal.Types.MonadRandomLike (Data.Random.Generics.Internal.Types.RejectT m) instance Control.Monad.Random.Class.MonadRandom m => Data.Random.Generics.Internal.Types.MonadRandomLike (Data.Random.Generics.Internal.Types.AMonadRandom m) module Data.Random.Generics.Internal.Oracle -- | We build a dictionary which reifies type information in order to -- create a Boltzmann generator. -- -- We denote by n (or count) the number of types in the -- dictionary. -- -- Every type has an index 0 <= i < n; the variable X -- i represents its generating function C_i(x), and X -- (i + k*n) the GF of its k-th "pointing" -- C_i[k](x); we have -- --
-- C_i[0](x) = C_i(x) -- C_i[k+1](x) = x * C_i[k]'(x) ---- -- where C_i[k]' is the derivative of C_i[k]. See also -- point. -- -- The order (or valuation) of a power series is the index -- of the first non-zero coefficient, called the leading -- coefficient. data DataDef m DataDef :: Int -> Int -> HashMap TypeRep (Either Aliased Ix) -> HashMap Ix SomeData' -> HashMap Aliased (Ix, Alias m) -> HashMap C [(Integer, Constr, [C'])] -> HashMap Ix (Nat, Integer) -> HashMap Ix Int -> DataDef m -- | Number of registered types [count] :: DataDef m -> Int -- | Number of iterations of the pointing operator [points] :: DataDef m -> Int -- | Map from types to indices [index] :: DataDef m -> HashMap TypeRep (Either Aliased Ix) -- | Inverse map from indices to types [xedni] :: DataDef m -> HashMap Ix SomeData' -- | Inverse map to aliases [xedni'] :: DataDef m -> HashMap Aliased (Ix, Alias m) -- | Structure of types and their pointings (up to points, initially -- 0) -- -- Primitive types and empty types are mapped to an empty constructor -- list, and can be distinguished using dataTypeRep on the -- SomeData associated to it by xedni. -- -- The integer is a multiplicity which can be > 1 for pointings. [types] :: DataDef m -> HashMap C [(Integer, Constr, [C'])] -- | Leading term a * x ^ u of the generating functions -- C_i[k](x) in the form (u, a). -- --
-- -- Original type -- data Tree = Node Tree Tree | Leaf -- -- Pointing of Tree -- data Tree' -- = Tree' Tree -- Point at the root -- | Node'0 Tree' Tree -- Point to the left -- | Node'1 Tree Tree' -- Point to the right -- -- Pointing of the pointing -- -- Notice that the "points" introduced by both applications of pointing -- -- are considered different: exchanging their positions (when different) -- -- produces a different tree. -- data Tree'' -- = Tree'' Tree' -- Point 2 at the root, the inner Tree' places point 1 -- | Node'0' Tree' Tree -- Point 1 at the root, point 2 to the left -- | Node'1' Tree Tree' -- Point 1 at the root, point 2 to the right -- | Node'0'0 Tree'' Tree -- Points 1 and 2 to the left -- | Node'0'1 Tree' Tree' -- Point 1 to the left, point 2 to the right -- | Node'1'0 Tree' Tree' -- Point 1 to the right, point 2 to the left -- | Node'0'1 Tree Tree'' -- Points 1 and 2 to the right ---- -- If we ignore points, some constructors are equivalent. Thus we may -- simply calculate their multiplicity instead of duplicating them. -- -- Given a constructor with c arguments C x_1 ... x_c, -- and a sequence p_0 + p_1 + ... + p_c = k corresponding to a -- distribution of k points (p_0 are assigned to the -- constructor C itself, and for i > 0, p_i -- points are assigned within the i-th subterm), the -- multiplicity of the constructor paired with that distribution is the -- multinomial coefficient multinomial k [p_1, ..., p_c]. point :: DataDef m -> DataDef m -- | An oracle gives the values of the generating functions at some -- x. type Oracle = HashMap C Double -- | Find the value of x such that the average size of the -- generator for the k-1-th pointing is equal to size, -- and produce the associated oracle. If the size is Nothing, -- find the radius of convergence. -- -- The search evaluates the generating functions for some values of -- x in order to run a binary search. The evaluator is -- implemented using Newton's method, the convergence of which has been -- shown for relevant systems in Boltzmann Oracle for Combinatorial -- Systems, C. Pivoteau, B. Salvy, M. Soria. makeOracle :: DataDef m -> TypeRep -> Maybe Double -> Oracle -- | Generating function definition. This defines a Phi_i[k] -- function associated with the k-th pointing of the type at -- index i, such that: -- --
-- C_i[k](x) -- = Phi_i[k](x, C_0[0](x), ..., C_(n-1)[0](x), -- ..., C_0[k](x), ..., C_(n-1)[k](x)) ---- -- Primitive datatypes have C(x) = x: they are considered as -- having a single object (lCoef) of size 1 (order)). phi :: Num a => DataDef m -> C -> [(Integer, constr, [C'])] -> a -> Vector a -> a -- | Maps a key representing a type a (or one of its pointings) to -- a generator m a. type Generators m = (HashMap AC (SomeData m), HashMap C (SomeData m)) -- | Build all involved generators at once. makeGenerators :: MonadRandomLike m => DataDef m -> Oracle -> Generators m type SmallGenerators m = (HashMap Aliased (SomeData m), HashMap Ix (SomeData m)) -- | Generators of values of minimal sizes. smallGenerators :: MonadRandomLike m => DataDef m -> SmallGenerators m generate :: Applicative m => GUnfold (ReaderT [SomeData m] m) defGen :: (Data a, MonadRandomLike m) => m a (?) :: DataDef m -> C -> Int -- |
-- dd ? (listCs dd !! i) = i --listCs :: DataDef m -> [C] ix :: C -> Int -- |
-- dd ? (dd ?! i) = i --(?!) :: DataDef m -> Int -> C getGenerator :: (Functor m, Data a) => DataDef m -> Generators m -> proxy a -> Int -> m a getSmallGenerator :: (Functor m, Data a) => DataDef m -> SmallGenerators m -> proxy a -> m a frequencyWith :: (Show r, Ord r, Num r, Monad m) => (r -> m r) -> [(r, m a)] -> m a (#!) :: (?loc :: CallStack, Eq k, Hashable k) => HashMap k v -> k -> v -- | partitions k n: lists of non-negative integers of length -- n with sum less than or equal to k. partitions :: Int -> Int -> [[Int]] -- | Multinomial coefficient. -- --
-- multinomial n ps == factorial n `div` product [factorial p | p <- ps] --multinomial :: Int -> [Int] -> Integer -- | Binomial coefficient. -- --
-- binomial n k == factorial n `div` (factorial k * factorial (n-k)) --binomial :: Int -> Int -> Integer instance GHC.Generics.Constructor Data.Random.Generics.Internal.Oracle.C1_0C instance GHC.Generics.Datatype Data.Random.Generics.Internal.Oracle.D1C instance GHC.Generics.Constructor Data.Random.Generics.Internal.Oracle.C1_0AC instance GHC.Generics.Datatype Data.Random.Generics.Internal.Oracle.D1AC instance GHC.Generics.Constructor Data.Random.Generics.Internal.Oracle.C1_0Aliased instance GHC.Generics.Datatype Data.Random.Generics.Internal.Oracle.D1Aliased instance GHC.Show.Show (Data.Random.Generics.Internal.Oracle.DataDef m) instance GHC.Show.Show Data.Random.Generics.Internal.Oracle.Nat instance GHC.Classes.Ord Data.Random.Generics.Internal.Oracle.Nat instance GHC.Classes.Eq Data.Random.Generics.Internal.Oracle.Nat instance GHC.Generics.Generic Data.Random.Generics.Internal.Oracle.C instance GHC.Show.Show Data.Random.Generics.Internal.Oracle.C instance GHC.Classes.Ord Data.Random.Generics.Internal.Oracle.C instance GHC.Classes.Eq Data.Random.Generics.Internal.Oracle.C instance GHC.Generics.Generic Data.Random.Generics.Internal.Oracle.AC instance GHC.Show.Show Data.Random.Generics.Internal.Oracle.AC instance GHC.Classes.Ord Data.Random.Generics.Internal.Oracle.AC instance GHC.Classes.Eq Data.Random.Generics.Internal.Oracle.AC instance GHC.Generics.Generic Data.Random.Generics.Internal.Oracle.Aliased instance GHC.Show.Show Data.Random.Generics.Internal.Oracle.Aliased instance GHC.Classes.Ord Data.Random.Generics.Internal.Oracle.Aliased instance GHC.Classes.Eq Data.Random.Generics.Internal.Oracle.Aliased instance Data.Hashable.Class.Hashable Data.Random.Generics.Internal.Oracle.C instance Data.Hashable.Class.Hashable Data.Random.Generics.Internal.Oracle.AC instance Data.Hashable.Class.Hashable Data.Random.Generics.Internal.Oracle.Aliased instance GHC.Base.Monoid Data.Random.Generics.Internal.Oracle.Nat module Data.Random.Generics.Internal -- | Sized generator. data SG r SG :: Size -> Maybe Size -> (Points -> Maybe Double -> r) -> (Points -> r) -> SG r [minSize] :: SG r -> Size [maxSizeM] :: SG r -> Maybe Size [runSG] :: SG r -> Points -> Maybe Double -> r [runSmallG] :: SG r -> Points -> r -- | Number of pointing iterations. type Points = Int rangeSG :: SG r -> (Size, Maybe Size) -- | For documentation. applySG :: SG r -> Points -> Maybe Double -> r make :: (Data a, MonadRandomLike m) => [Alias m] -> proxy a -> SG (m a) makeR :: (Data a, MonadRandomLike m) => [AliasR m] -> proxy a -> SG ((Size, Size) -> m a) -- | The size of a value is its number of constructors. -- -- Here, however, the Size' type is interpreted differently to -- make better use of QuickCheck's size parameter provided by the -- sized combinator, so that we generate non-trivial data even at -- very small size values. -- -- For infinite types, with objects of unbounded sizes > -- minSize, given a parameter delta :: Size', the -- produced values have an average size close to minSize + -- delta. -- -- For example, values of type Either () [Bool] have at least -- two constructors, so -- --
-- generator delta :: Gen (Either () [Bool]) ---- -- will target sizes close to 2 + delta; the offset becomes less -- noticeable as delta grows to infinity. -- -- For finite types with sizes in [minSize, maxSize], the target -- expected size is obtained by clamping a Size' to [0, -- 99] and applying an affine mapping. type Size' = Int rescale :: SG r -> Size' -> Double apply :: SG r -> Points -> Maybe Size' -> r applyR :: SG ((Size, Size) -> r) -> Points -> Maybe Size' -> (Size', Size') -> r rescaleInterval :: SG r -> (Size', Size') -> (Size, Size) -- |
-- 'epsilon' = 0.1 ---- -- Default approximation ratio. epsilon :: Double -- |
-- (size * (1 - epsilon), size * (1 + epsilon)) --tolerance :: Double -> Int -> (Int, Int) memo :: (t -> [t2] -> SG r) -> (SG r -> t1 -> Maybe Int -> a) -> t -> t1 -> Int -> a sparseSized :: (Int -> a) -> Maybe Int -> Int -> a instance GHC.Base.Functor Data.Random.Generics.Internal.SG -- | Generic Boltzmann samplers. -- -- Here, the words "sampler" and "generator" are used -- interchangeably. -- -- Given an algebraic datatype: -- --
-- data A = A1 B C | A2 D ---- -- a Boltzmann sampler is recursively defined by choosing a constructor -- with some fixed distribution, and independently generating -- values for the corresponding fields with the same method. -- -- A key component is the aforementioned distribution, defined for every -- type such that the resulting generator produces a finite value in the -- end. These distributions are obtained from a precomputed object called -- oracle, which we will not describe further here. -- -- Oracles depend on the target size of the generated data (except for -- singular samplers), and can be fairly expensive to compute repeatedly, -- hence some of the functions below attempt to avoid (re)computing too -- many of them even when the required size changes. -- -- When these functions are specialized, oracles are memoized and will be -- reused for different sizes. module Data.Random.Generics -- | The size of a value is its number of constructors. -- -- Here, however, the Size' type is interpreted differently to -- make better use of QuickCheck's size parameter provided by the -- sized combinator, so that we generate non-trivial data even at -- very small size values. -- -- For infinite types, with objects of unbounded sizes > -- minSize, given a parameter delta :: Size', the -- produced values have an average size close to minSize + -- delta. -- -- For example, values of type Either () [Bool] have at least -- two constructors, so -- --
-- generator delta :: Gen (Either () [Bool]) ---- -- will target sizes close to 2 + delta; the offset becomes less -- noticeable as delta grows to infinity. -- -- For finite types with sizes in [minSize, maxSize], the target -- expected size is obtained by clamping a Size' to [0, -- 99] and applying an affine mapping. type Size' = Int -- |
-- generatorSR :: Int -> Gen a -- asMonadRandom . generatorSR :: MonadRandom m => Int -> m a ---- -- Singular ceiled rejection sampler. generatorSR :: (Data a, MonadRandomLike m) => Size' -> m a -- |
-- generatorP :: Int -> Gen a -- asMonadRandom . generatorP :: MonadRandom m => Int -> m a ---- -- Generator of pointed values. generatorP :: (Data a, MonadRandomLike m) => Size' -> m a -- | Pointed generator with rejection. generatorPR :: (Data a, MonadRandomLike m) => Size' -> m a -- | Generator with rejection and dynamic average size. generatorR :: (Data a, MonadRandomLike m) => Size' -> m a -- | Pointed generator. generatorP' :: (Data a, MonadRandomLike m) => Size' -> m a -- | Pointed generator with rejection. generatorPR' :: (Data a, MonadRandomLike m) => Size' -> m a -- | Ceiled rejection sampler with given average size. generatorR' :: (Data a, MonadRandomLike m) => Size' -> m a -- | Basic boltzmann sampler with no optimization. generator' :: (Data a, MonadRandomLike m) => Size' -> m a generatorSRWith :: (Data a, MonadRandomLike m) => [AliasR m] -> Size' -> m a generatorPWith :: (Data a, MonadRandomLike m) => [Alias m] -> Size' -> m a generatorPRWith :: (Data a, MonadRandomLike m) => [AliasR m] -> Size' -> m a generatorRWith :: (Data a, MonadRandomLike m) => [AliasR m] -> Size' -> m a generatorPWith' :: (Data a, MonadRandomLike m) => [Alias m] -> Size' -> m a generatorPRWith' :: (Data a, MonadRandomLike m) => [AliasR m] -> Size' -> m a generatorRWith' :: (Data a, MonadRandomLike m) => [AliasR m] -> Size' -> m a generatorWith' :: (Data a, MonadRandomLike m) => [Alias m] -> Size' -> m a -- | Number of pointing iterations. type Points = Int generatorM :: (Data a, MonadRandomLike m) => [Alias m] -> Points -> Size' -> m a generatorMR :: (Data a, MonadRandomLike m) => [AliasR m] -> Points -> Size' -> (Size', Size') -> m a -- | Boltzmann sampler without rejection. generator_ :: (Data a, MonadRandomLike m) => [Alias m] -> Points -> Maybe Size' -> m a -- | Boltzmann sampler with rejection. generatorR_ :: (Data a, MonadRandomLike m) => [AliasR m] -> Points -> Maybe Size' -> (Size', Size') -> m a -- | MonadRandomLike m defines basic components to build -- generators, allowing the implementation to remain abstract over both -- the Gen type and MonadRandom instances. -- -- For the latter, the wrapper AMonadRandom is provided to avoid -- overlapping instances. class Monad m => MonadRandomLike m where incr = return () -- | Called for every constructor. Counter for ceiled rejection sampling. incr :: MonadRandomLike m => m () -- | doubleR upperBound: generates values in [0, -- upperBound]. doubleR :: MonadRandomLike m => Double -> m Double -- | integerR upperBound: generates values in [0, -- upperBound-1]. integerR :: MonadRandomLike m => Integer -> m Integer -- | Default Int generator. int :: MonadRandomLike m => m Int -- | Default Double generator. double :: MonadRandomLike m => m Double -- | Default Char generator. char :: MonadRandomLike m => m Char newtype AMonadRandom m a AMonadRandom :: m a -> AMonadRandom m a [asMonadRandom] :: AMonadRandom m a -> m a -- | Main constructor for Alias. alias :: (Monad m, Data a, Data b) => (a -> m b) -> Alias m -- | Main constructor for AliasR. aliasR :: (Monad m, Data a, Data b) => (a -> m b) -> AliasR m -- |
-- coerceAlias :: Alias m -> Alias (AMonadRandom m) --coerceAlias :: Coercible m n => Alias m -> Alias n -- |
-- coerceAliases :: [Alias m] -> [Alias (AMonadRandom m)] --coerceAliases :: Coercible m n => [Alias m] -> [Alias n] data Alias m Alias :: !(m a -> m b) -> Alias m type AliasR m = Alias (RejectT m)