-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Generic random generators -- -- Please see the README. @package generic-random @version 0.5.0.0 module Generic.Random.Internal.Generic -- | Pick a constructor with a given distribution, and fill its fields -- recursively. genericArbitrary :: forall a. (Generic a, GA Unsized (Rep a)) => Weights a -> Gen a -- | Shorthand for genericArbitrary uniform. genericArbitraryU :: forall a. (Generic a, GA Unsized (Rep a), UniformWeight (Weights_ (Rep a))) => Gen a -- | Like genericArbitrary', with decreasing size to ensure -- termination for recursive types, looking for base cases once the size -- reaches 0. genericArbitrary' :: forall n a. (Generic a, GA (Sized n) (Rep a)) => n -> Weights a -> Gen a -- | Shorthand for genericArbitrary' Z -- uniform, using nullary constructors as the base cases. genericArbitraryU0 :: forall n a. (Generic a, GA (Sized Z) (Rep a), UniformWeight (Weights_ (Rep a))) => Gen a -- | Shorthand for genericArbitrary' (S Z) -- uniform, using nullary constructors and constructors whose -- fields are all nullary as base cases. genericArbitraryU1 :: forall n a. (Generic a, GA (Sized (S Z)) (Rep a), UniformWeight (Weights_ (Rep a))) => Gen a data (:|) a b N :: a -> Int -> b -> (:|) a b data L (c :: Symbol) L :: L -- | Trees of weights assigned to constructors of type a, rescaled -- to obtain a probability distribution. -- -- Two ways of constructing them. -- --
-- weights (x1 % x2 % ... % xn % ()) :: Weights a -- uniform :: Weights a ---- -- Using weights, there must be exactly as many weights as there -- are constructors. -- -- uniform is equivalent to weights (1 % ... -- % 1 % ()) (automatically fills out the right number -- of 1s). data Weights a Weights :: (Weights_ (Rep a)) -> Int -> Weights a -- | Type of a single weight, tagged with the name of the associated -- constructor for additional compile-time checking. -- --
-- weights ((9 :: W "Leaf") % (8 :: W "Node") % ()) --newtype W (c :: Symbol) W :: Int -> W -- | A smart constructor to specify a custom distribution. weights :: (Weights_ (Rep a), Int, ()) -> Weights a -- | Uniform distribution. uniform :: UniformWeight (Weights_ (Rep a)) => Weights a class WeightBuilder a where type Prec a r where { type family Prec a r; } -- | A binary constructor for building up trees of weights. (%) :: WeightBuilder a => W (First a) -> Prec a r -> (a, Int, r) class UniformWeight a uniformWeight :: UniformWeight a => (a, Int) newtype Gen' sized a Gen' :: Gen a -> Gen' sized a [unGen'] :: Gen' sized a -> Gen a data Sized n data Unsized sized' :: (Int -> Gen' sized a) -> Gen' sized a -- | Generic Arbitrary class GA sized f ga :: GA sized f => Weights_ f -> Int -> Gen' sized (f p) gArbitrarySingle :: forall sized f p c0. (GA sized f, Weights_ f ~ L c0) => Gen' sized (f p) gaSum' :: GASum sized f => Weights_ f -> Int -> Gen' sized (f p) class GASum sized f gaSum :: GASum sized f => Int -> Weights_ f -> Gen' sized (f p) class GAProduct f gaProduct :: GAProduct f => Gen (f p) newtype Tagged a b Tagged :: b -> Tagged a b [unTagged] :: Tagged a b -> b -- | Zero data Z Z :: Z -- | Successor data S n S :: n -> S n newtype Weighted a Weighted :: (Maybe (Int -> Gen a, Int)) -> Weighted a class BaseCases n f baseCases :: BaseCases n f => Weights_ f -> Int -> Tagged n (Weighted (f p)) -- | A ListBaseCases n (Rep a) constraint basically -- provides the list of values of type a with depth at most -- n. class ListBaseCases n f listBaseCases :: (ListBaseCases n f, Alternative u) => Tagged n (u (f p)) -- | For convenience. type BaseCases' n a = (Generic a, ListBaseCases n (Rep a)) instance GHC.Base.Functor Generic.Random.Internal.Generic.Weighted instance GHC.Base.Functor (Generic.Random.Internal.Generic.Tagged a) instance GHC.Base.Monad (Generic.Random.Internal.Generic.Gen' sized) instance GHC.Base.Applicative (Generic.Random.Internal.Generic.Gen' sized) instance GHC.Base.Functor (Generic.Random.Internal.Generic.Gen' sized) instance GHC.Num.Num (Generic.Random.Internal.Generic.W c) instance Generic.Random.Internal.Generic.WeightBuilder a => Generic.Random.Internal.Generic.WeightBuilder (a Generic.Random.Internal.Generic.:| b) instance Generic.Random.Internal.Generic.WeightBuilder (Generic.Random.Internal.Generic.L c) instance Generic.Random.Internal.Generic.WeightBuilder () instance (Generic.Random.Internal.Generic.UniformWeight a, Generic.Random.Internal.Generic.UniformWeight b) => Generic.Random.Internal.Generic.UniformWeight (a Generic.Random.Internal.Generic.:| b) instance Generic.Random.Internal.Generic.UniformWeight (Generic.Random.Internal.Generic.L c) instance Generic.Random.Internal.Generic.UniformWeight () instance Generic.Random.Internal.Generic.GA sized f => Generic.Random.Internal.Generic.GA sized (GHC.Generics.M1 GHC.Generics.D c f) instance Generic.Random.Internal.Generic.GAProduct f => Generic.Random.Internal.Generic.GA Generic.Random.Internal.Generic.Unsized (GHC.Generics.M1 GHC.Generics.C c f) instance (Generic.Random.Internal.Generic.GAProduct f, GHC.TypeLits.KnownNat (Generic.Random.Internal.Generic.Arity f)) => Generic.Random.Internal.Generic.GA (Generic.Random.Internal.Generic.Sized n) (GHC.Generics.M1 GHC.Generics.C c f) instance (Generic.Random.Internal.Generic.GASum (Generic.Random.Internal.Generic.Sized n) f, Generic.Random.Internal.Generic.GASum (Generic.Random.Internal.Generic.Sized n) g, Generic.Random.Internal.Generic.BaseCases n f, Generic.Random.Internal.Generic.BaseCases n g) => Generic.Random.Internal.Generic.GA (Generic.Random.Internal.Generic.Sized n) (f GHC.Generics.:+: g) instance (Generic.Random.Internal.Generic.GASum Generic.Random.Internal.Generic.Unsized f, Generic.Random.Internal.Generic.GASum Generic.Random.Internal.Generic.Unsized g) => Generic.Random.Internal.Generic.GA Generic.Random.Internal.Generic.Unsized (f GHC.Generics.:+: g) instance (Generic.Random.Internal.Generic.GASum sized f, Generic.Random.Internal.Generic.GASum sized g) => Generic.Random.Internal.Generic.GASum sized (f GHC.Generics.:+: g) instance Generic.Random.Internal.Generic.GAProduct f => Generic.Random.Internal.Generic.GASum sized (GHC.Generics.M1 i c f) instance Generic.Random.Internal.Generic.GAProduct GHC.Generics.U1 instance Test.QuickCheck.Arbitrary.Arbitrary c => Generic.Random.Internal.Generic.GAProduct (GHC.Generics.K1 i c) instance Generic.Random.Internal.Generic.GAProduct f => Generic.Random.Internal.Generic.GAProduct (GHC.Generics.M1 i c f) instance (Generic.Random.Internal.Generic.GAProduct f, Generic.Random.Internal.Generic.GAProduct g) => Generic.Random.Internal.Generic.GAProduct (f GHC.Generics.:*: g) instance GHC.Base.Applicative Generic.Random.Internal.Generic.Weighted instance GHC.Base.Alternative Generic.Random.Internal.Generic.Weighted instance (Generic.Random.Internal.Generic.BaseCases n f, Generic.Random.Internal.Generic.BaseCases n g) => Generic.Random.Internal.Generic.BaseCases n (f GHC.Generics.:+: g) instance Generic.Random.Internal.Generic.ListBaseCases n f => Generic.Random.Internal.Generic.BaseCases n (GHC.Generics.M1 i c f) instance Generic.Random.Internal.Generic.ListBaseCases n GHC.Generics.U1 instance Generic.Random.Internal.Generic.ListBaseCases n f => Generic.Random.Internal.Generic.ListBaseCases n (GHC.Generics.M1 i c f) instance Generic.Random.Internal.Generic.ListBaseCases Generic.Random.Internal.Generic.Z (GHC.Generics.K1 i c) instance (GHC.Generics.Generic c, Generic.Random.Internal.Generic.ListBaseCases n (GHC.Generics.Rep c)) => Generic.Random.Internal.Generic.ListBaseCases (Generic.Random.Internal.Generic.S n) (GHC.Generics.K1 i c) instance (Generic.Random.Internal.Generic.ListBaseCases n f, Generic.Random.Internal.Generic.ListBaseCases n g) => Generic.Random.Internal.Generic.ListBaseCases n (f GHC.Generics.:+: g) instance (Generic.Random.Internal.Generic.ListBaseCases n f, Generic.Random.Internal.Generic.ListBaseCases n g) => Generic.Random.Internal.Generic.ListBaseCases n (f GHC.Generics.:*: g) -- | Simple Generics-based arbitrary generators. -- -- Here is an example. Define your type. -- --
-- data Tree a = Leaf a | Node (Tree a) (Tree a) -- deriving Generic ---- -- Pick an arbitrary implementation. -- --
-- instance Arbitrary a => Arbitrary (Tree a) where -- arbitrary = genericArbitrary (weights (9 % 8 % ())) ---- -- arbitrary :: Gen (Tree a) picks a Leaf with -- probability 9/17, or a Node with probability 8/17, and -- recursively fills their fields with arbitrary. -- --
-- genericArbitrary :: Arbitrary a => Weights (Tree a) -> Gen (Tree a) -- genericArbitrary (weighted (x % y % ())) = -- frequency -- [ (x, Leaf <$> arbitrary) -- , (y, Node <$> arbitrary <*> arbitrary) -- ] ---- --
-- weighted ((x :: W "Leaf") % (y :: W "Node") % ()) :: Weights (Tree a) -- weighted (x % (y :: W "Node") % ()) :: Weights (Tree a) ---- -- This will not: the first requires an order of constructors different -- from the definition of the Tree type; the second doesn't have -- the right number of weights. -- --
-- weighted ((x :: W "Node") % y % ()) :: Weights (Tree a) -- weighted (x % y % z % ()) :: Weights (Tree a) ---- --
-- genericArbitrary' n (weights (...)) ---- -- With n = Z, the generator looks for a simple nullary -- constructor. If none exist at the current type, as is the case for our -- Tree type, it carries on as in genericArbitrary. -- --
-- genericArbitrary' Z :: Arbitrary a => Weights (Tree a) -> Gen (Tree a) -- genericArbitrary' Z (weights (x % y % ())) = -- frequency -- [ (x, Leaf <$> arbitrary) -- , (y, scale (`div` 2) $ Node <$> arbitrary <*> arbitrary) -- -- 2 because Node is 2-ary. -- ] ---- -- Here is another example with nullary constructors: -- --
-- data Tree' = Leaf1 | Leaf2 | Node3 Tree' Tree' Tree' -- deriving Generic -- -- instance Arbitrary Tree' where -- arbitrary = genericArbitrary' Z (weights (1 % 2 % 3 % ())) ---- -- Here, genericArbitrary' Z is equivalent to: -- --
-- genericArbitrary' Z :: Weights Tree' -> Gen Tree' -- genericArbitrary' Z (weights (x % y % z % ())) = -- sized $ n -> -- if n == 0 then -- -- If the size parameter is zero, the non-nullary alternative is discarded. -- frequency $ -- [ (x, return Leaf1) -- , (y, return Leaf2) -- ] -- else -- frequency $ -- [ (x, return Leaf1) -- , (y, return Leaf2) -- , (z, resize (n `div` 3) node) -- 3 because Node3 is 3-ary -- ] -- where -- node = Node3 <$> arbitrary <*> arbitrary <*> arbitrary ---- -- To increase the chances of termination when no nullary constructor is -- directly available, such as in Tree, we can pass a larger -- depth n. The effectiveness of this parameter depends on the -- concrete type the generator is used for. -- -- For instance, if we want to generate a value of type Tree (), -- there is a value of depth 1 (represented by S -- Z) that we can use to end recursion: Leaf (). -- --
-- genericArbitrary' (S Z) :: Weights (Tree ()) -> Gen (Tree ()) -- genericArbitrary' (S Z) (weights (x % y % ())) = -- sized $ n -> -- if n == 0 then -- return (Leaf ()) -- else -- frequency -- [ (x, Leaf <$> arbitrary) -- , (y, scale (`div` 2) $ Node <$> arbitrary <*> arbitrary) -- ] ---- -- Because the argument of Tree must be inspected in order to -- discover values of type Tree (), we incur some extra -- constraints if we want polymorphism. -- -- UndecidableInstances is also required. -- --
-- instance (Arbitrary a, Generic a, ListBaseCases Z (Rep a)) -- => Arbitrary (Tree a) where -- arbitrary = genericArbitrary' (S Z) (weights (1 % 2 % ())) ---- -- A synonym is provided for brevity. -- --
-- instance (Arbitrary a, BaseCases' Z a) => Arbitrary (Tree a) where -- arbitrary = genericArbitrary' (S Z) (weights (1 % 2 % ())) --module Generic.Random.Generic -- | Pick a constructor with a given distribution, and fill its fields -- recursively. genericArbitrary :: forall a. (Generic a, GA Unsized (Rep a)) => Weights a -> Gen a -- | Shorthand for genericArbitrary uniform. genericArbitraryU :: forall a. (Generic a, GA Unsized (Rep a), UniformWeight (Weights_ (Rep a))) => Gen a -- | Like genericArbitrary', with decreasing size to ensure -- termination for recursive types, looking for base cases once the size -- reaches 0. genericArbitrary' :: forall n a. (Generic a, GA (Sized n) (Rep a)) => n -> Weights a -> Gen a -- | Shorthand for genericArbitrary' Z -- uniform, using nullary constructors as the base cases. genericArbitraryU0 :: forall n a. (Generic a, GA (Sized Z) (Rep a), UniformWeight (Weights_ (Rep a))) => Gen a -- | Shorthand for genericArbitrary' (S Z) -- uniform, using nullary constructors and constructors whose -- fields are all nullary as base cases. genericArbitraryU1 :: forall n a. (Generic a, GA (Sized (S Z)) (Rep a), UniformWeight (Weights_ (Rep a))) => Gen a -- | Trees of weights assigned to constructors of type a, rescaled -- to obtain a probability distribution. -- -- Two ways of constructing them. -- --
-- weights (x1 % x2 % ... % xn % ()) :: Weights a -- uniform :: Weights a ---- -- Using weights, there must be exactly as many weights as there -- are constructors. -- -- uniform is equivalent to weights (1 % ... -- % 1 % ()) (automatically fills out the right number -- of 1s). data Weights a -- | Type of a single weight, tagged with the name of the associated -- constructor for additional compile-time checking. -- --
-- weights ((9 :: W "Leaf") % (8 :: W "Node") % ()) --data W (c :: Symbol) -- | A smart constructor to specify a custom distribution. weights :: (Weights_ (Rep a), Int, ()) -> Weights a -- | A binary constructor for building up trees of weights. (%) :: WeightBuilder a => W (First a) -> Prec a r -> (a, Int, r) -- | Uniform distribution. uniform :: UniformWeight (Weights_ (Rep a)) => Weights a -- | Zero data Z Z :: Z -- | Successor data S n S :: n -> S n -- | For convenience. type BaseCases' n a = (Generic a, ListBaseCases n (Rep a)) class BaseCases n f -- | A ListBaseCases n (Rep a) constraint basically -- provides the list of values of type a with depth at most -- n. class ListBaseCases n f