Module

Control.Monad.Gen.Trans

Package
purescript-generate-values
Repository
jordanmartinez/purescript-generate-values

This module defines the random generator monad using a linear congruential generator, as well as helper funtions for constructing random generators.

Depending on your underlying monad, stack-safety can be an issue. All functions (e.g. vectorOf) that do not end with ' are stack-safe, but sometimes this safety is guaranteed via MonadRec.

If your underlying monad (e.g. Aff) is already stack-safe, you can omit this extra constraint and use the variant that ends with ' (e.g. vectorOf').

#GenState Source

type GenState = { newSeed :: Seed, size :: Int }

The state of the random generator monad

The size label

Tests are parameterized by the size of the randomly-generated data, the meaning of which depends on the particular generator used.

#GenT Source

newtype GenT :: (Type -> Type) -> Type -> Typenewtype GenT m a

The random generator monad

GenT is a state monad which encodes a linear congruential generator.

Constructors

Instances

#runGenT Source

runGenT :: forall m a. GenT m a -> GenState -> m (Tuple a GenState)

Exposes the underlying function.

#runGenT' Source

runGenT' :: forall m a. GenT m a -> StateT GenState m a

Exposes the underlying StateT implementation.

#withGenT Source

withGenT :: forall m a. (GenState -> GenState) -> GenT m a -> GenT m a

Modify the final state in a GenT monad action.

#mapGenT Source

mapGenT :: forall m1 m2 a b. (m1 (Tuple a GenState) -> m2 (Tuple b GenState)) -> GenT m1 a -> GenT m2 b

Change the type of the result in a GenT action

#execGenT Source

execGenT :: forall m a. Functor m => GenT m a -> GenState -> m GenState

Run a random generator, keeping only the generator state.

#evalGenT Source

evalGenT :: forall m a. Functor m => GenT m a -> GenState -> m a

Run a random generator, keeping only the randomly-generated result

#perturbGenT Source

perturbGenT :: forall m a. Monad m => Number -> GenT m a -> GenT m a

Perturb a random generator by modifying the current seed

#resizeGenT Source

resizeGenT :: forall m a. Monad m => Int -> GenT m a -> GenT m a

Modify a random generator by setting a new size parameter.

#Gen Source

type Gen a = GenT Identity a

#runGen Source

runGen :: forall a. Gen a -> GenState -> Tuple a GenState

Run a random generator

#withGen Source

withGen :: forall a. (GenState -> GenState) -> Gen a -> Gen a

Modify the final state in a Gen action.

#mapGen Source

mapGen :: forall a b. (Tuple a GenState -> Tuple b GenState) -> Gen a -> Gen b

Change the type of the result in a Gen action

#execGen Source

execGen :: forall a. Gen a -> GenState -> GenState

Run a random generator, keeping only the generator state.

#evalGen Source

evalGen :: forall a. Gen a -> GenState -> a

Run a random generator, keeping only the randomly-generated result

#perturbGen Source

perturbGen :: forall m a. Monad m => Number -> Gen a -> Gen a

Perturb a random generator by modifying the current seed

#resizeGen Source

resizeGen :: forall a. Int -> Gen a -> Gen a

Modify a random generator by setting a new size parameter.

#repeatable Source

repeatable :: forall m a b. Monad m => (a -> Gen b) -> GenT m (a -> b)

Create a random generator for a function type. Note that the a -> Gen b cannot run effects.

#stateful Source

stateful :: forall m a. (GenState -> GenT m a) -> GenT m a

Create a random generator which uses the generator state explicitly.

#variant Source

variant :: forall m a. Seed -> GenT m a -> GenT m a

Modify a random generator by setting a new random seed.

#suchThat Source

suchThat :: forall m a. MonadRec m => GenT m a -> (a -> Boolean) -> GenT m a

Ensure that a generator only produces values that match a predicate. If the predicate always returns false the generator will loop forever. Stack-safety is guaranteed via the MonadRec constraint

#suchThat' Source

suchThat' :: forall m a. Monad m => GenT m a -> (a -> Boolean) -> GenT m a

Ensure that a generator only produces values that match a predicate. If the predicate always returns false the generator will loop forever. This is only stack-safe if the underlying monad is stack-safe.

#sized Source

sized :: forall m a. (Int -> GenT m a) -> GenT m a

Create a random generator which depends on the size parameter.

#choose Source

choose :: forall m. Monad m => Number -> Number -> GenT m Number

Create a random generator which samples a range of Numbers i with uniform probability.

#chooseInt Source

chooseInt :: forall m. Monad m => Int -> Int -> GenT m Int

Create a random generator which chooses uniformly distributed integers from the closed interval [a, b]. Note that very large intervals will cause a loss of uniformity.

#oneOf Source

oneOf :: forall m a. Monad m => NonEmptyArray (GenT m a) -> GenT m a

Create a random generator which selects and executes a random generator from a non-empty array of random generators with uniform probability.

#frequency Source

frequency :: forall m a. Monad m => NonEmptyArray (Tuple Number (GenT m a)) -> GenT m a

Create a random generator which selects and executes a random generator from a non-empty, weighted list of random generators.

#arrayOf Source

arrayOf :: forall m a. MonadRec m => GenT m a -> GenT m (Array a)

Create a random generator which generates an array of random values. Stack-safety is guaranteed via the MonadRec constraint

#arrayOf' Source

arrayOf' :: forall m a. Monad m => GenT m a -> GenT m (Array a)

Create a random generator which generates an array of random values. This is only stack-safe if the underlying monad is stack-safe.

#arrayOf1 Source

arrayOf1 :: forall m a. MonadRec m => GenT m a -> GenT m (NonEmptyArray a)

Create a random generator which generates a non-empty array of random values. Stack-safety is guaranteed via the MonadRec constraint

#arrayOf1' Source

arrayOf1' :: forall m a. Monad m => GenT m a -> GenT m (NonEmptyArray a)

Create a random generator which generates a non-empty array of random values. This is only stack-safe if the underlying monad is stack-safe.

#enum Source

enum :: forall m a. Monad m => BoundedEnum a => GenT m a

Create a random generator for a finite enumeration. toEnum i must be well-behaved: It must return a value wrapped in Just for all Ints between fromEnum bottom and fromEnum top.

#listOf Source

listOf :: forall m a. MonadRec m => Int -> GenT m a -> GenT m (List a)

Create a random generator which generates a list of random values of the specified size. Stack-safety is guaranteed via the MonadRec constraint

#listOf' Source

listOf' :: forall m a. Monad m => Int -> GenT m a -> GenT m (List a)

Create a random generator which generates a list of random values of the specified size. This is only stack-safe if the underlying monad is stack-safe.

#vectorOf Source

vectorOf :: forall m a. MonadRec m => Int -> GenT m a -> GenT m (Array a)

Create a random generator which generates a vector of random values of a specified size. Stack-safety is guaranteed via the MonadRec constraint

#vectorOf' Source

vectorOf' :: forall m a. Monad m => Int -> GenT m a -> GenT m (Array a)

Create a random generator which generates a vector of random values of a specified size. This is only stack-safe if the underlying monad is stack-safe.

#elements Source

elements :: forall m a. Monad m => NonEmptyArray a -> GenT m a

Create a random generator which selects a value from a non-empty array with uniform probability.

#shuffle Source

shuffle :: forall m a. MonadRec m => Array a -> GenT m (Array a)

Generate a random permutation of the given array Stack-safety is guaranteed via the MonadRec constraint

#shuffle' Source

shuffle' :: forall m a. Monad m => Array a -> GenT m (Array a)

Generate a random permutation of the given array This is only stack-safe if the underlying monad is stack-safe.

#uniform Source

uniform :: forall m. Monad m => GenT m Number

A random generator which approximates a uniform random variable on [0, 1]

#sample Source

sample :: forall m a. MonadRec m => Seed -> Int -> GenT m a -> m (Array a)

Sample a random generator Stack-safety is guaranteed via the MonadRec constraint

#sample' Source

sample' :: forall m a. Monad m => Seed -> Int -> GenT m a -> m (Array a)

Sample a random generator This is only stack-safe if the underlying monad is stack-safe.

#randomSample1 Source

randomSample1 :: forall m a. MonadEffect m => GenT m a -> m a

Generate a single value using a randomly generated seed.

#randomSampleN Source

randomSampleN :: forall m a. MonadRec m => MonadEffect m => Int -> GenT m a -> m (Array a)

Sample a random generator, using a randomly generated seed Stack-safety is guaranteed via the MonadRec constraint

#randomSampleN' Source

randomSampleN' :: forall m a. MonadEffect m => Int -> GenT m a -> m (Array a)

Sample a random generator, using a randomly generated seed This is only stack-safe if the underlying monad is stack-safe.