{-# LANGUAGE BangPatterns         #-}
{-# LANGUAGE CPP                  #-}
{-# LANGUAGE DeriveDataTypeable   #-}
{-# LANGUAGE DeriveFoldable       #-}
{-# LANGUAGE DeriveFunctor        #-}
{-# LANGUAGE DeriveGeneric        #-}
{-# LANGUAGE DeriveTraversable    #-}
{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE InstanceSigs         #-}
{-# LANGUAGE ScopedTypeVariables  #-}
{-# LANGUAGE TupleSections        #-}
{-# LANGUAGE TypeApplications     #-}
{-# LANGUAGE UndecidableInstances #-}

-- |
-- Module      : System.Random.Stateful.OneLiner
-- Description : Derived methods for Random.
-- Copyright   : (c) Justin Le 2021
-- License     : BSD-3
-- Maintainer  : justin@jle.im
-- Stability   : unstable
-- Portability : portable
--
-- Derived methods for 'Uniform', using "Generics.OneLiner" and
-- "GHC.Generics".
--
-- Can be used for any types (deriving 'Generic') made with a single
-- constructor, where every field is an instance of 'Uniform'.
--
-- Also includes a newtype wrapper that imbues any such data type with
-- instant 'Uniform' instances, which can one day be used with /DerivingVia/
-- syntax to derive instances automatically.
--
-- @since 0.1.3.0
module System.Random.Stateful.OneLiner (
  -- * Single constructor
  -- ** Newtype wrapper
    GUniform(..)
  -- ** Generics-derived methods
  , gUniformM
  , gUniformRM
  , gUniformListM
  , gRandomM
  , gRandomRM
  -- * Multiple constructor
  -- ** Newtype wrapper
  , GUniformSum(..)
  -- ** Generics-derived methods
  , gUniformSumM
  , gUniformRSumM
  , gUniformSumListM
  , gRandomSumM
  , gRandomRSumM
  ) where

import           Control.Monad
import           Data.Data
import           Data.Functor.Compose
import           Data.List.NonEmpty              (NonEmpty(..))
import           Data.Maybe
import           GHC.Generics
import           Generics.OneLiner
import           System.Random
import           System.Random.OneLiner
import           System.Random.OneLiner.Internal (Pair(..), dePair)
import           System.Random.Stateful
import qualified Data.List.NonEmpty              as NE

-- | If @a@ is a data type with a single constructor whose fields are all
-- instances of 'Uniform', then @'GUniform' a@ has a 'Uniform' instance.
--
-- Will one day be able to be used with /DerivingVia/ syntax, to derive
-- instances automatically.
--
-- Only works with data types with single constructors.  If you need it to
-- work with types of multiple constructors, consider 'GUniformSum'.
newtype GUniform a = GUniform { GUniform a -> a
getGUniform :: a }
  deriving (GUniform a -> GUniform a -> Bool
(GUniform a -> GUniform a -> Bool)
-> (GUniform a -> GUniform a -> Bool) -> Eq (GUniform a)
forall a. Eq a => GUniform a -> GUniform a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GUniform a -> GUniform a -> Bool
$c/= :: forall a. Eq a => GUniform a -> GUniform a -> Bool
== :: GUniform a -> GUniform a -> Bool
$c== :: forall a. Eq a => GUniform a -> GUniform a -> Bool
Eq, Eq (GUniform a)
Eq (GUniform a)
-> (GUniform a -> GUniform a -> Ordering)
-> (GUniform a -> GUniform a -> Bool)
-> (GUniform a -> GUniform a -> Bool)
-> (GUniform a -> GUniform a -> Bool)
-> (GUniform a -> GUniform a -> Bool)
-> (GUniform a -> GUniform a -> GUniform a)
-> (GUniform a -> GUniform a -> GUniform a)
-> Ord (GUniform a)
GUniform a -> GUniform a -> Bool
GUniform a -> GUniform a -> Ordering
GUniform a -> GUniform a -> GUniform a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (GUniform a)
forall a. Ord a => GUniform a -> GUniform a -> Bool
forall a. Ord a => GUniform a -> GUniform a -> Ordering
forall a. Ord a => GUniform a -> GUniform a -> GUniform a
min :: GUniform a -> GUniform a -> GUniform a
$cmin :: forall a. Ord a => GUniform a -> GUniform a -> GUniform a
max :: GUniform a -> GUniform a -> GUniform a
$cmax :: forall a. Ord a => GUniform a -> GUniform a -> GUniform a
>= :: GUniform a -> GUniform a -> Bool
$c>= :: forall a. Ord a => GUniform a -> GUniform a -> Bool
> :: GUniform a -> GUniform a -> Bool
$c> :: forall a. Ord a => GUniform a -> GUniform a -> Bool
<= :: GUniform a -> GUniform a -> Bool
$c<= :: forall a. Ord a => GUniform a -> GUniform a -> Bool
< :: GUniform a -> GUniform a -> Bool
$c< :: forall a. Ord a => GUniform a -> GUniform a -> Bool
compare :: GUniform a -> GUniform a -> Ordering
$ccompare :: forall a. Ord a => GUniform a -> GUniform a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (GUniform a)
Ord, Int -> GUniform a -> ShowS
[GUniform a] -> ShowS
GUniform a -> String
(Int -> GUniform a -> ShowS)
-> (GUniform a -> String)
-> ([GUniform a] -> ShowS)
-> Show (GUniform a)
forall a. Show a => Int -> GUniform a -> ShowS
forall a. Show a => [GUniform a] -> ShowS
forall a. Show a => GUniform a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GUniform a] -> ShowS
$cshowList :: forall a. Show a => [GUniform a] -> ShowS
show :: GUniform a -> String
$cshow :: forall a. Show a => GUniform a -> String
showsPrec :: Int -> GUniform a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> GUniform a -> ShowS
Show, ReadPrec [GUniform a]
ReadPrec (GUniform a)
Int -> ReadS (GUniform a)
ReadS [GUniform a]
(Int -> ReadS (GUniform a))
-> ReadS [GUniform a]
-> ReadPrec (GUniform a)
-> ReadPrec [GUniform a]
-> Read (GUniform a)
forall a. Read a => ReadPrec [GUniform a]
forall a. Read a => ReadPrec (GUniform a)
forall a. Read a => Int -> ReadS (GUniform a)
forall a. Read a => ReadS [GUniform a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GUniform a]
$creadListPrec :: forall a. Read a => ReadPrec [GUniform a]
readPrec :: ReadPrec (GUniform a)
$creadPrec :: forall a. Read a => ReadPrec (GUniform a)
readList :: ReadS [GUniform a]
$creadList :: forall a. Read a => ReadS [GUniform a]
readsPrec :: Int -> ReadS (GUniform a)
$creadsPrec :: forall a. Read a => Int -> ReadS (GUniform a)
Read, Typeable (GUniform a)
DataType
Constr
Typeable (GUniform a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> GUniform a -> c (GUniform a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (GUniform a))
-> (GUniform a -> Constr)
-> (GUniform a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (GUniform a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (GUniform a)))
-> ((forall b. Data b => b -> b) -> GUniform a -> GUniform a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> GUniform a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> GUniform a -> r)
-> (forall u. (forall d. Data d => d -> u) -> GUniform a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> GUniform a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a))
-> Data (GUniform a)
GUniform a -> DataType
GUniform a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (GUniform a))
(forall b. Data b => b -> b) -> GUniform a -> GUniform a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniform a -> c (GUniform a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniform a)
forall a. Data a => Typeable (GUniform a)
forall a. Data a => GUniform a -> DataType
forall a. Data a => GUniform a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> GUniform a -> GUniform a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> GUniform a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> GUniform a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GUniform a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GUniform a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniform a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniform a -> c (GUniform a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (GUniform a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (GUniform a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> GUniform a -> u
forall u. (forall d. Data d => d -> u) -> GUniform a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GUniform a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GUniform a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniform a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniform a -> c (GUniform a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (GUniform a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (GUniform a))
$cGUniform :: Constr
$tGUniform :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
gmapMp :: (forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
gmapM :: (forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> GUniform a -> m (GUniform a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> GUniform a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> GUniform a -> u
gmapQ :: (forall d. Data d => d -> u) -> GUniform a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> GUniform a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GUniform a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GUniform a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GUniform a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GUniform a -> r
gmapT :: (forall b. Data b => b -> b) -> GUniform a -> GUniform a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> GUniform a -> GUniform a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (GUniform a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (GUniform a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (GUniform a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (GUniform a))
dataTypeOf :: GUniform a -> DataType
$cdataTypeOf :: forall a. Data a => GUniform a -> DataType
toConstr :: GUniform a -> Constr
$ctoConstr :: forall a. Data a => GUniform a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniform a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniform a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniform a -> c (GUniform a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniform a -> c (GUniform a)
$cp1Data :: forall a. Data a => Typeable (GUniform a)
Data, (forall x. GUniform a -> Rep (GUniform a) x)
-> (forall x. Rep (GUniform a) x -> GUniform a)
-> Generic (GUniform a)
forall x. Rep (GUniform a) x -> GUniform a
forall x. GUniform a -> Rep (GUniform a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (GUniform a) x -> GUniform a
forall a x. GUniform a -> Rep (GUniform a) x
$cto :: forall a x. Rep (GUniform a) x -> GUniform a
$cfrom :: forall a x. GUniform a -> Rep (GUniform a) x
Generic, a -> GUniform b -> GUniform a
(a -> b) -> GUniform a -> GUniform b
(forall a b. (a -> b) -> GUniform a -> GUniform b)
-> (forall a b. a -> GUniform b -> GUniform a) -> Functor GUniform
forall a b. a -> GUniform b -> GUniform a
forall a b. (a -> b) -> GUniform a -> GUniform b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> GUniform b -> GUniform a
$c<$ :: forall a b. a -> GUniform b -> GUniform a
fmap :: (a -> b) -> GUniform a -> GUniform b
$cfmap :: forall a b. (a -> b) -> GUniform a -> GUniform b
Functor, GUniform a -> Bool
(a -> m) -> GUniform a -> m
(a -> b -> b) -> b -> GUniform a -> b
(forall m. Monoid m => GUniform m -> m)
-> (forall m a. Monoid m => (a -> m) -> GUniform a -> m)
-> (forall m a. Monoid m => (a -> m) -> GUniform a -> m)
-> (forall a b. (a -> b -> b) -> b -> GUniform a -> b)
-> (forall a b. (a -> b -> b) -> b -> GUniform a -> b)
-> (forall b a. (b -> a -> b) -> b -> GUniform a -> b)
-> (forall b a. (b -> a -> b) -> b -> GUniform a -> b)
-> (forall a. (a -> a -> a) -> GUniform a -> a)
-> (forall a. (a -> a -> a) -> GUniform a -> a)
-> (forall a. GUniform a -> [a])
-> (forall a. GUniform a -> Bool)
-> (forall a. GUniform a -> Int)
-> (forall a. Eq a => a -> GUniform a -> Bool)
-> (forall a. Ord a => GUniform a -> a)
-> (forall a. Ord a => GUniform a -> a)
-> (forall a. Num a => GUniform a -> a)
-> (forall a. Num a => GUniform a -> a)
-> Foldable GUniform
forall a. Eq a => a -> GUniform a -> Bool
forall a. Num a => GUniform a -> a
forall a. Ord a => GUniform a -> a
forall m. Monoid m => GUniform m -> m
forall a. GUniform a -> Bool
forall a. GUniform a -> Int
forall a. GUniform a -> [a]
forall a. (a -> a -> a) -> GUniform a -> a
forall m a. Monoid m => (a -> m) -> GUniform a -> m
forall b a. (b -> a -> b) -> b -> GUniform a -> b
forall a b. (a -> b -> b) -> b -> GUniform a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: GUniform a -> a
$cproduct :: forall a. Num a => GUniform a -> a
sum :: GUniform a -> a
$csum :: forall a. Num a => GUniform a -> a
minimum :: GUniform a -> a
$cminimum :: forall a. Ord a => GUniform a -> a
maximum :: GUniform a -> a
$cmaximum :: forall a. Ord a => GUniform a -> a
elem :: a -> GUniform a -> Bool
$celem :: forall a. Eq a => a -> GUniform a -> Bool
length :: GUniform a -> Int
$clength :: forall a. GUniform a -> Int
null :: GUniform a -> Bool
$cnull :: forall a. GUniform a -> Bool
toList :: GUniform a -> [a]
$ctoList :: forall a. GUniform a -> [a]
foldl1 :: (a -> a -> a) -> GUniform a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> GUniform a -> a
foldr1 :: (a -> a -> a) -> GUniform a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> GUniform a -> a
foldl' :: (b -> a -> b) -> b -> GUniform a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> GUniform a -> b
foldl :: (b -> a -> b) -> b -> GUniform a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> GUniform a -> b
foldr' :: (a -> b -> b) -> b -> GUniform a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> GUniform a -> b
foldr :: (a -> b -> b) -> b -> GUniform a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> GUniform a -> b
foldMap' :: (a -> m) -> GUniform a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> GUniform a -> m
foldMap :: (a -> m) -> GUniform a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> GUniform a -> m
fold :: GUniform m -> m
$cfold :: forall m. Monoid m => GUniform m -> m
Foldable, Functor GUniform
Foldable GUniform
Functor GUniform
-> Foldable GUniform
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> GUniform a -> f (GUniform b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    GUniform (f a) -> f (GUniform a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> GUniform a -> m (GUniform b))
-> (forall (m :: * -> *) a.
    Monad m =>
    GUniform (m a) -> m (GUniform a))
-> Traversable GUniform
(a -> f b) -> GUniform a -> f (GUniform b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => GUniform (m a) -> m (GUniform a)
forall (f :: * -> *) a.
Applicative f =>
GUniform (f a) -> f (GUniform a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> GUniform a -> m (GUniform b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> GUniform a -> f (GUniform b)
sequence :: GUniform (m a) -> m (GUniform a)
$csequence :: forall (m :: * -> *) a. Monad m => GUniform (m a) -> m (GUniform a)
mapM :: (a -> m b) -> GUniform a -> m (GUniform b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> GUniform a -> m (GUniform b)
sequenceA :: GUniform (f a) -> f (GUniform a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
GUniform (f a) -> f (GUniform a)
traverse :: (a -> f b) -> GUniform a -> f (GUniform b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> GUniform a -> f (GUniform b)
$cp2Traversable :: Foldable GUniform
$cp1Traversable :: Functor GUniform
Traversable)

instance ( ADTRecord a
         , Constraints a Uniform
         )
      => Uniform (GUniform a) where
    uniformM :: forall g m. StatefulGen g m => g -> m (GUniform a)
    uniformM :: g -> m (GUniform a)
uniformM g
g = a -> GUniform a
forall a. a -> GUniform a
GUniform (a -> GUniform a) -> m a -> m (GUniform a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> g -> m a
forall a g (m :: * -> *).
(ADTRecord a, Constraints a Uniform, StatefulGen g m) =>
g -> m a
gUniformM g
g
    {-# INLINE uniformM #-}

instance ( ADTRecord a
         , Constraints a UniformRange
         )
      => UniformRange (GUniform a) where
    uniformRM :: forall g m. StatefulGen g m => (GUniform a, GUniform a) -> g -> m (GUniform a)
    uniformRM :: (GUniform a, GUniform a) -> g -> m (GUniform a)
uniformRM (GUniform a
l, GUniform a
r) g
g = a -> GUniform a
forall a. a -> GUniform a
GUniform (a -> GUniform a) -> m a -> m (GUniform a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a, a) -> g -> m a
forall a g (m :: * -> *).
(ADTRecord a, Constraints a UniformRange, StatefulGen g m) =>
(a, a) -> g -> m a
gUniformRM (a
l, a
r) g
g
    {-# INLINE uniformRM #-}

gUniformM
   :: forall a g m. (ADTRecord a, Constraints a Uniform, StatefulGen g m)
   => g
   -> m a
gUniformM :: g -> m a
gUniformM g
g = (forall s. Uniform s => m s) -> m a
forall (c :: * -> Constraint) t (f :: * -> *).
(ADTRecord t, Constraints t c, Applicative f) =>
(forall s. c s => f s) -> f t
createA' @Uniform (g -> m s
forall a g (m :: * -> *). (Uniform a, StatefulGen g m) => g -> m a
uniformM g
g)
{-# INLINE gUniformM #-}

gUniformRM
    :: forall a g m. (ADTRecord a, Constraints a UniformRange, StatefulGen g m)
    => (a, a) -> g -> m a
gUniformRM :: (a, a) -> g -> m a
gUniformRM (a
l, a
u) g
g = (forall s. UniformRange s => Pair s -> m s) -> Pair a -> m a
forall (c :: * -> Constraint) t (f :: * -> *) (g :: * -> *).
(ADTRecord t, Constraints t c, Functor f, Applicative g) =>
(forall s. c s => f s -> g s) -> f t -> g t
dialgebra @UniformRange
    (((s, s) -> g -> m s
forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
`uniformRM` g
g) ((s, s) -> m s) -> (Pair s -> (s, s)) -> Pair s -> m s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pair s -> (s, s)
forall a. Pair a -> (a, a)
dePair)
    (a -> a -> Pair a
forall a. a -> a -> Pair a
Pair a
l a
u)
{-# INLINE gUniformRM #-}

-- | Uses the 'Random' instance instead of the 'Uniform' instance.
gRandomM
    :: forall a g m r. (ADTRecord a, Constraints a Random, RandomGenM g r m)
    => g -> m a
gRandomM :: g -> m a
gRandomM = (r -> (a, r)) -> g -> m a
forall g r (m :: * -> *) a.
RandomGenM g r m =>
(r -> (a, r)) -> g -> m a
applyRandomGenM r -> (a, r)
forall a g.
(ADTRecord a, Constraints a Random, RandomGen g) =>
g -> (a, g)
gRandom

-- | Uses the 'Random' instance instead of the 'Uniform' instance.
gRandomRM
    :: forall a g m r. (ADTRecord a, Constraints a Random, RandomGenM g r m)
    => (a, a) -> g -> m a
gRandomRM :: (a, a) -> g -> m a
gRandomRM (a, a)
r = (r -> (a, r)) -> g -> m a
forall g r (m :: * -> *) a.
RandomGenM g r m =>
(r -> (a, r)) -> g -> m a
applyRandomGenM ((a, a) -> r -> (a, r)
forall a g.
(ADTRecord a, Constraints a Random, RandomGen g) =>
(a, a) -> g -> (a, g)
gRandomR (a, a)
r)

gUniformListM
   :: forall a g m. (ADTRecord a, Constraints a Uniform, StatefulGen g m)
   => Int
   -> g
   -> m [a]
gUniformListM :: Int -> g -> m [a]
gUniformListM Int
n g
g = Int -> m a -> m [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n (g -> m a
forall a g (m :: * -> *).
(ADTRecord a, Constraints a Uniform, StatefulGen g m) =>
g -> m a
gUniformM g
g)

-- | If @a@ is a data type whose fields are all instances of 'Uniform', then
-- @'GUniform' a@ has a 'Uniform' instance.
--
-- Will one day be able to be used with /DerivingVia/ syntax, to derive
-- instances automatically.
--
-- A version of 'GUniform' that works for data types with multiple
-- constructors.  If your type has only one constructor, it might be more
-- performant to use 'GUniform'.
--
-- Note that the "ranged" variants are partial: if given a range of items
-- made with different constructors, will be 'error'!
newtype GUniformSum a = GUniformSum { GUniformSum a -> a
getGUniformSum :: a }
  deriving (GUniformSum a -> GUniformSum a -> Bool
(GUniformSum a -> GUniformSum a -> Bool)
-> (GUniformSum a -> GUniformSum a -> Bool) -> Eq (GUniformSum a)
forall a. Eq a => GUniformSum a -> GUniformSum a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GUniformSum a -> GUniformSum a -> Bool
$c/= :: forall a. Eq a => GUniformSum a -> GUniformSum a -> Bool
== :: GUniformSum a -> GUniformSum a -> Bool
$c== :: forall a. Eq a => GUniformSum a -> GUniformSum a -> Bool
Eq, Eq (GUniformSum a)
Eq (GUniformSum a)
-> (GUniformSum a -> GUniformSum a -> Ordering)
-> (GUniformSum a -> GUniformSum a -> Bool)
-> (GUniformSum a -> GUniformSum a -> Bool)
-> (GUniformSum a -> GUniformSum a -> Bool)
-> (GUniformSum a -> GUniformSum a -> Bool)
-> (GUniformSum a -> GUniformSum a -> GUniformSum a)
-> (GUniformSum a -> GUniformSum a -> GUniformSum a)
-> Ord (GUniformSum a)
GUniformSum a -> GUniformSum a -> Bool
GUniformSum a -> GUniformSum a -> Ordering
GUniformSum a -> GUniformSum a -> GUniformSum a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (GUniformSum a)
forall a. Ord a => GUniformSum a -> GUniformSum a -> Bool
forall a. Ord a => GUniformSum a -> GUniformSum a -> Ordering
forall a. Ord a => GUniformSum a -> GUniformSum a -> GUniformSum a
min :: GUniformSum a -> GUniformSum a -> GUniformSum a
$cmin :: forall a. Ord a => GUniformSum a -> GUniformSum a -> GUniformSum a
max :: GUniformSum a -> GUniformSum a -> GUniformSum a
$cmax :: forall a. Ord a => GUniformSum a -> GUniformSum a -> GUniformSum a
>= :: GUniformSum a -> GUniformSum a -> Bool
$c>= :: forall a. Ord a => GUniformSum a -> GUniformSum a -> Bool
> :: GUniformSum a -> GUniformSum a -> Bool
$c> :: forall a. Ord a => GUniformSum a -> GUniformSum a -> Bool
<= :: GUniformSum a -> GUniformSum a -> Bool
$c<= :: forall a. Ord a => GUniformSum a -> GUniformSum a -> Bool
< :: GUniformSum a -> GUniformSum a -> Bool
$c< :: forall a. Ord a => GUniformSum a -> GUniformSum a -> Bool
compare :: GUniformSum a -> GUniformSum a -> Ordering
$ccompare :: forall a. Ord a => GUniformSum a -> GUniformSum a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (GUniformSum a)
Ord, Int -> GUniformSum a -> ShowS
[GUniformSum a] -> ShowS
GUniformSum a -> String
(Int -> GUniformSum a -> ShowS)
-> (GUniformSum a -> String)
-> ([GUniformSum a] -> ShowS)
-> Show (GUniformSum a)
forall a. Show a => Int -> GUniformSum a -> ShowS
forall a. Show a => [GUniformSum a] -> ShowS
forall a. Show a => GUniformSum a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GUniformSum a] -> ShowS
$cshowList :: forall a. Show a => [GUniformSum a] -> ShowS
show :: GUniformSum a -> String
$cshow :: forall a. Show a => GUniformSum a -> String
showsPrec :: Int -> GUniformSum a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> GUniformSum a -> ShowS
Show, ReadPrec [GUniformSum a]
ReadPrec (GUniformSum a)
Int -> ReadS (GUniformSum a)
ReadS [GUniformSum a]
(Int -> ReadS (GUniformSum a))
-> ReadS [GUniformSum a]
-> ReadPrec (GUniformSum a)
-> ReadPrec [GUniformSum a]
-> Read (GUniformSum a)
forall a. Read a => ReadPrec [GUniformSum a]
forall a. Read a => ReadPrec (GUniformSum a)
forall a. Read a => Int -> ReadS (GUniformSum a)
forall a. Read a => ReadS [GUniformSum a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GUniformSum a]
$creadListPrec :: forall a. Read a => ReadPrec [GUniformSum a]
readPrec :: ReadPrec (GUniformSum a)
$creadPrec :: forall a. Read a => ReadPrec (GUniformSum a)
readList :: ReadS [GUniformSum a]
$creadList :: forall a. Read a => ReadS [GUniformSum a]
readsPrec :: Int -> ReadS (GUniformSum a)
$creadsPrec :: forall a. Read a => Int -> ReadS (GUniformSum a)
Read, Typeable (GUniformSum a)
DataType
Constr
Typeable (GUniformSum a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> GUniformSum a -> c (GUniformSum a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (GUniformSum a))
-> (GUniformSum a -> Constr)
-> (GUniformSum a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (GUniformSum a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (GUniformSum a)))
-> ((forall b. Data b => b -> b) -> GUniformSum a -> GUniformSum a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r)
-> (forall u. (forall d. Data d => d -> u) -> GUniformSum a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> GUniformSum a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> GUniformSum a -> m (GUniformSum a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> GUniformSum a -> m (GUniformSum a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> GUniformSum a -> m (GUniformSum a))
-> Data (GUniformSum a)
GUniformSum a -> DataType
GUniformSum a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (GUniformSum a))
(forall b. Data b => b -> b) -> GUniformSum a -> GUniformSum a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniformSum a -> c (GUniformSum a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniformSum a)
forall a. Data a => Typeable (GUniformSum a)
forall a. Data a => GUniformSum a -> DataType
forall a. Data a => GUniformSum a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> GUniformSum a -> GUniformSum a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> GUniformSum a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> GUniformSum a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniformSum a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniformSum a -> c (GUniformSum a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (GUniformSum a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (GUniformSum a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> GUniformSum a -> u
forall u. (forall d. Data d => d -> u) -> GUniformSum a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniformSum a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniformSum a -> c (GUniformSum a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (GUniformSum a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (GUniformSum a))
$cGUniformSum :: Constr
$tGUniformSum :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
gmapMp :: (forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
gmapM :: (forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d)
-> GUniformSum a -> m (GUniformSum a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> GUniformSum a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> GUniformSum a -> u
gmapQ :: (forall d. Data d => d -> u) -> GUniformSum a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> GUniformSum a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> GUniformSum a -> r
gmapT :: (forall b. Data b => b -> b) -> GUniformSum a -> GUniformSum a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> GUniformSum a -> GUniformSum a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (GUniformSum a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (GUniformSum a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (GUniformSum a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (GUniformSum a))
dataTypeOf :: GUniformSum a -> DataType
$cdataTypeOf :: forall a. Data a => GUniformSum a -> DataType
toConstr :: GUniformSum a -> Constr
$ctoConstr :: forall a. Data a => GUniformSum a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniformSum a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (GUniformSum a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniformSum a -> c (GUniformSum a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> GUniformSum a -> c (GUniformSum a)
$cp1Data :: forall a. Data a => Typeable (GUniformSum a)
Data, (forall x. GUniformSum a -> Rep (GUniformSum a) x)
-> (forall x. Rep (GUniformSum a) x -> GUniformSum a)
-> Generic (GUniformSum a)
forall x. Rep (GUniformSum a) x -> GUniformSum a
forall x. GUniformSum a -> Rep (GUniformSum a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (GUniformSum a) x -> GUniformSum a
forall a x. GUniformSum a -> Rep (GUniformSum a) x
$cto :: forall a x. Rep (GUniformSum a) x -> GUniformSum a
$cfrom :: forall a x. GUniformSum a -> Rep (GUniformSum a) x
Generic, a -> GUniformSum b -> GUniformSum a
(a -> b) -> GUniformSum a -> GUniformSum b
(forall a b. (a -> b) -> GUniformSum a -> GUniformSum b)
-> (forall a b. a -> GUniformSum b -> GUniformSum a)
-> Functor GUniformSum
forall a b. a -> GUniformSum b -> GUniformSum a
forall a b. (a -> b) -> GUniformSum a -> GUniformSum b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> GUniformSum b -> GUniformSum a
$c<$ :: forall a b. a -> GUniformSum b -> GUniformSum a
fmap :: (a -> b) -> GUniformSum a -> GUniformSum b
$cfmap :: forall a b. (a -> b) -> GUniformSum a -> GUniformSum b
Functor, GUniformSum a -> Bool
(a -> m) -> GUniformSum a -> m
(a -> b -> b) -> b -> GUniformSum a -> b
(forall m. Monoid m => GUniformSum m -> m)
-> (forall m a. Monoid m => (a -> m) -> GUniformSum a -> m)
-> (forall m a. Monoid m => (a -> m) -> GUniformSum a -> m)
-> (forall a b. (a -> b -> b) -> b -> GUniformSum a -> b)
-> (forall a b. (a -> b -> b) -> b -> GUniformSum a -> b)
-> (forall b a. (b -> a -> b) -> b -> GUniformSum a -> b)
-> (forall b a. (b -> a -> b) -> b -> GUniformSum a -> b)
-> (forall a. (a -> a -> a) -> GUniformSum a -> a)
-> (forall a. (a -> a -> a) -> GUniformSum a -> a)
-> (forall a. GUniformSum a -> [a])
-> (forall a. GUniformSum a -> Bool)
-> (forall a. GUniformSum a -> Int)
-> (forall a. Eq a => a -> GUniformSum a -> Bool)
-> (forall a. Ord a => GUniformSum a -> a)
-> (forall a. Ord a => GUniformSum a -> a)
-> (forall a. Num a => GUniformSum a -> a)
-> (forall a. Num a => GUniformSum a -> a)
-> Foldable GUniformSum
forall a. Eq a => a -> GUniformSum a -> Bool
forall a. Num a => GUniformSum a -> a
forall a. Ord a => GUniformSum a -> a
forall m. Monoid m => GUniformSum m -> m
forall a. GUniformSum a -> Bool
forall a. GUniformSum a -> Int
forall a. GUniformSum a -> [a]
forall a. (a -> a -> a) -> GUniformSum a -> a
forall m a. Monoid m => (a -> m) -> GUniformSum a -> m
forall b a. (b -> a -> b) -> b -> GUniformSum a -> b
forall a b. (a -> b -> b) -> b -> GUniformSum a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: GUniformSum a -> a
$cproduct :: forall a. Num a => GUniformSum a -> a
sum :: GUniformSum a -> a
$csum :: forall a. Num a => GUniformSum a -> a
minimum :: GUniformSum a -> a
$cminimum :: forall a. Ord a => GUniformSum a -> a
maximum :: GUniformSum a -> a
$cmaximum :: forall a. Ord a => GUniformSum a -> a
elem :: a -> GUniformSum a -> Bool
$celem :: forall a. Eq a => a -> GUniformSum a -> Bool
length :: GUniformSum a -> Int
$clength :: forall a. GUniformSum a -> Int
null :: GUniformSum a -> Bool
$cnull :: forall a. GUniformSum a -> Bool
toList :: GUniformSum a -> [a]
$ctoList :: forall a. GUniformSum a -> [a]
foldl1 :: (a -> a -> a) -> GUniformSum a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> GUniformSum a -> a
foldr1 :: (a -> a -> a) -> GUniformSum a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> GUniformSum a -> a
foldl' :: (b -> a -> b) -> b -> GUniformSum a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> GUniformSum a -> b
foldl :: (b -> a -> b) -> b -> GUniformSum a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> GUniformSum a -> b
foldr' :: (a -> b -> b) -> b -> GUniformSum a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> GUniformSum a -> b
foldr :: (a -> b -> b) -> b -> GUniformSum a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> GUniformSum a -> b
foldMap' :: (a -> m) -> GUniformSum a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> GUniformSum a -> m
foldMap :: (a -> m) -> GUniformSum a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> GUniformSum a -> m
fold :: GUniformSum m -> m
$cfold :: forall m. Monoid m => GUniformSum m -> m
Foldable, Functor GUniformSum
Foldable GUniformSum
Functor GUniformSum
-> Foldable GUniformSum
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> GUniformSum a -> f (GUniformSum b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    GUniformSum (f a) -> f (GUniformSum a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> GUniformSum a -> m (GUniformSum b))
-> (forall (m :: * -> *) a.
    Monad m =>
    GUniformSum (m a) -> m (GUniformSum a))
-> Traversable GUniformSum
(a -> f b) -> GUniformSum a -> f (GUniformSum b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
GUniformSum (m a) -> m (GUniformSum a)
forall (f :: * -> *) a.
Applicative f =>
GUniformSum (f a) -> f (GUniformSum a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> GUniformSum a -> m (GUniformSum b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> GUniformSum a -> f (GUniformSum b)
sequence :: GUniformSum (m a) -> m (GUniformSum a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
GUniformSum (m a) -> m (GUniformSum a)
mapM :: (a -> m b) -> GUniformSum a -> m (GUniformSum b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> GUniformSum a -> m (GUniformSum b)
sequenceA :: GUniformSum (f a) -> f (GUniformSum a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
GUniformSum (f a) -> f (GUniformSum a)
traverse :: (a -> f b) -> GUniformSum a -> f (GUniformSum b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> GUniformSum a -> f (GUniformSum b)
$cp2Traversable :: Foldable GUniformSum
$cp1Traversable :: Functor GUniformSum
Traversable)

instance ( ADT a
         , Constraints a Uniform
         )
      => Uniform (GUniformSum a) where
    uniformM :: forall g m. StatefulGen g m => g -> m (GUniformSum a)
    uniformM :: g -> m (GUniformSum a)
uniformM g
g = a -> GUniformSum a
forall a. a -> GUniformSum a
GUniformSum (a -> GUniformSum a) -> m a -> m (GUniformSum a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> g -> m a
forall a g (m :: * -> *).
(ADT a, Constraints a Uniform, StatefulGen g m) =>
g -> m a
gUniformSumM g
g
    {-# INLINE uniformM #-}

-- | Note that this is partial: the range limits must have the same
-- constructors.
instance ( ADT a
         , Constraints a UniformRange
         )
      => UniformRange (GUniformSum a) where
    uniformRM :: forall g m. StatefulGen g m => (GUniformSum a, GUniformSum a) -> g -> m (GUniformSum a)
    uniformRM :: (GUniformSum a, GUniformSum a) -> g -> m (GUniformSum a)
uniformRM (GUniformSum a
l, GUniformSum a
u) g
g = a -> GUniformSum a
forall a. a -> GUniformSum a
GUniformSum (a -> GUniformSum a) -> m a -> m (GUniformSum a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a, a) -> g -> m a
forall a g (m :: * -> *).
(ADT a, Constraints a UniformRange, StatefulGen g m) =>
(a, a) -> g -> m a
gUniformRSumM (a
l, a
u) g
g
    {-# INLINE uniformRM #-}

-- | 'randomR' implemented by sequencing 'randomR' between all components.
--
-- If given a range of items made with different constructors, will be
-- 'error'!
gUniformRSumM
    :: forall a g m. (ADT a, Constraints a UniformRange, StatefulGen g m)
    => (a, a) -> g -> m a
gUniformRSumM :: (a, a) -> g -> m a
gUniformRSumM (a
l, a
u) g
g = m a -> Maybe (m a) -> m a
forall a. a -> Maybe a -> a
fromMaybe (String -> m a
forall a. HasCallStack => String -> a
error String
badbad) (Maybe (m a) -> m a)
-> (Compose Maybe m a -> Maybe (m a)) -> Compose Maybe m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose Maybe m a -> Maybe (m a)
forall k1 (f :: k1 -> *) k2 (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose (Compose Maybe m a -> m a) -> Compose Maybe m a -> m a
forall a b. (a -> b) -> a -> b
$
    (forall s. UniformRange s => s -> s -> Compose Maybe m s)
-> a -> a -> Compose Maybe m a
forall (c :: * -> Constraint) t (f :: * -> *).
(ADT t, Constraints t c, Alternative f) =>
(forall s. c s => s -> s -> f s) -> t -> t -> f t
zipWithA @UniformRange
      (\s
l' s
u' -> Maybe (m s) -> Compose Maybe m s
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (m s -> Maybe (m s)
forall a. a -> Maybe a
Just ((s, s) -> g -> m s
forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (s
l', s
u') g
g)))
      a
l a
u
  where
    badbad :: String
badbad = String
"gUniformRSum: Constructors in range do not match."
{-# INLINE gUniformRSumM #-}

-- | 'random' implemented by selecting a random constructor and sequencing
-- 'random' for all components.
gUniformSumM
    :: forall a g m. (ADT a, Constraints a Uniform, StatefulGen g m)
    => g -> m a
gUniformSumM :: g -> m a
gUniformSumM g
g = case Maybe (NonEmpty (m a))
options of
    Maybe (NonEmpty (m a))
Nothing   -> a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> a
forall a. HasCallStack => String -> a
error String
"gUniformSumM: Uninhabited type")
    Just NonEmpty (m a)
opts -> m (m a) -> m a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (m (m a) -> m a) -> m (m a) -> m a
forall a b. (a -> b) -> a -> b
$ NonEmpty (m a) -> g -> m (m a)
forall g (m :: * -> *) a. StatefulGen g m => NonEmpty a -> g -> m a
reservoir NonEmpty (m a)
opts g
g
  where
    options :: Maybe (NonEmpty (m a))
options = [m a] -> Maybe (NonEmpty (m a))
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty ([m a] -> Maybe (NonEmpty (m a)))
-> (Compose [] m a -> [m a])
-> Compose [] m a
-> Maybe (NonEmpty (m a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose [] m a -> [m a]
forall k1 (f :: k1 -> *) k2 (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose (Compose [] m a -> Maybe (NonEmpty (m a)))
-> Compose [] m a -> Maybe (NonEmpty (m a))
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *).
(ADT a, Constraints a Uniform, Alternative f) =>
(forall s. Uniform s => f s) -> f a
forall (c :: * -> Constraint) t (f :: * -> *).
(ADT t, Constraints t c, Alternative f) =>
(forall s. c s => f s) -> f t
createA @Uniform @a ((forall s. Uniform s => Compose [] m s) -> Compose [] m a)
-> (forall s. Uniform s => Compose [] m s) -> Compose [] m a
forall a b. (a -> b) -> a -> b
$
        [m s] -> Compose [] m s
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose [g -> m s
forall a g (m :: * -> *). (Uniform a, StatefulGen g m) => g -> m a
uniformM g
g]
{-# INLINE gUniformSumM #-}

-- | Uses the 'Random' instance instead of the 'Uniform' instance.
gRandomSumM
    :: forall a g m r. (ADT a, Constraints a Random, RandomGenM g r m)
    => g -> m a
gRandomSumM :: g -> m a
gRandomSumM = (r -> (a, r)) -> g -> m a
forall g r (m :: * -> *) a.
RandomGenM g r m =>
(r -> (a, r)) -> g -> m a
applyRandomGenM r -> (a, r)
forall a g.
(ADT a, Constraints a Random, RandomGen g) =>
g -> (a, g)
gRandomSum

-- | Uses the 'Random' instance instead of the 'Uniform' instance.
gRandomRSumM
    :: forall a g m r. (ADT a, Constraints a Random, RandomGenM g r m)
    => (a, a) -> g -> m a
gRandomRSumM :: (a, a) -> g -> m a
gRandomRSumM (a, a)
r = (r -> (a, r)) -> g -> m a
forall g r (m :: * -> *) a.
RandomGenM g r m =>
(r -> (a, r)) -> g -> m a
applyRandomGenM ((a, a) -> r -> (a, r)
forall a g.
(ADT a, Constraints a Random, RandomGen g) =>
(a, a) -> g -> (a, g)
gRandomRSum (a, a)
r)

gUniformSumListM
   :: forall a g m. (ADT a, Constraints a Uniform, StatefulGen g m)
   => Int
   -> g
   -> m [a]
gUniformSumListM :: Int -> g -> m [a]
gUniformSumListM Int
n g
g = Int -> m a -> m [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n (g -> m a
forall a g (m :: * -> *).
(ADT a, Constraints a Uniform, StatefulGen g m) =>
g -> m a
gUniformSumM g
g)

-- | Select a random item from a non-empty list in constant memory, using
-- only a single traversal, using reservoir sampling.
reservoir :: StatefulGen g m => NE.NonEmpty a -> g -> m a
reservoir :: NonEmpty a -> g -> m a
reservoir (a
x :| [a]
xs) g
g = Word64 -> a -> [a] -> m a
forall (f :: * -> *) t.
StatefulGen g f =>
Word64 -> t -> [t] -> f t
go Word64
1 a
x [a]
xs
  where
    go :: Word64 -> t -> [t] -> f t
go Word64
_  t
y []     = t -> f t
forall (f :: * -> *) a. Applicative f => a -> f a
pure t
y
    go !Word64
i t
y (t
z:[t]
zs) = do
      Word64
j <- Word64 -> g -> f Word64
forall g (m :: * -> *). StatefulGen g m => Word64 -> g -> m Word64
uniformWord64R Word64
i g
g
      if Word64
j Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0
        then Word64 -> t -> [t] -> f t
go (Word64
i Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
1) t
z [t]
zs
        else Word64 -> t -> [t] -> f t
go (Word64
i Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
1) t
y [t]
zs
{-# INLINE reservoir #-}