Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- type GenTm = GenT (Reader GenEnv)
- data GenEnv = GenEnv {
- geAstSize ∷ Int
- geDatas ∷ Map TyName (Datatype TyName Name DefaultUni ())
- geTypes ∷ TypeCtx
- geTerms ∷ Map Name (Type TyName DefaultUni ())
- geUnboundUsedTyNames ∷ Set TyName
- geEscaping ∷ AllowEscape
- geCustomGen ∷ Maybe (Type TyName DefaultUni ()) → GenTm (Type TyName DefaultUni (), Term TyName Name DefaultUni DefaultFun ())
- geCustomFreq ∷ Int
- geDebug ∷ Bool
- suchThatMap ∷ Monad m ⇒ GenT m a → (a → Maybe b) → GenT m b
- withDebug ∷ GenTm a → GenTm a
- runGenTm ∷ GenTm a → Gen a
- runGenTmCustom ∷ Int → (Maybe (Type TyName DefaultUni ()) → GenTm (Type TyName DefaultUni (), Term TyName Name DefaultUni DefaultFun ())) → GenTm a → Gen a
- deliver ∷ Monad m ⇒ GenT m (Maybe a) → GenT m a
- withNoEscape ∷ GenTm a → GenTm a
- onAstSize ∷ (Int → Int) → GenTm a → GenTm a
- ifAstSizeZero ∷ GenTm a → GenTm a → GenTm a
- withAstSize ∷ Int → GenTm a → GenTm a
- astSizeSplit_ ∷ Int → Int → GenTm a → GenTm b → GenTm (a, b)
- astSizeSplit ∷ Int → Int → GenTm a → (a → GenTm b) → GenTm (a, b)
- getUniques ∷ GenTm (Set Unique)
- genLikelyFreshName ∷ String → GenTm Name
- genLikelyFreshNames ∷ [String] → GenTm [Name]
- genLikelyFreshTyName ∷ String → GenTm TyName
- genLikelyFreshTyNames ∷ [String] → GenTm [TyName]
- genLikelyNotFreshName ∷ String → GenTm Name
- genMaybeFreshName ∷ String → GenTm Name
- genMaybeFreshTyName ∷ String → GenTm TyName
- bindTyName ∷ TyName → Kind () → GenTm a → GenTm a
- bindTyNames ∷ [(TyName, Kind ())] → GenTm a → GenTm a
- registerTyName ∷ TyName → GenTm a → GenTm a
- bindTmName ∷ Name → Type TyName DefaultUni () → GenTm a → GenTm a
- bindTmNames ∷ [(Name, Type TyName DefaultUni ())] → GenTm a → GenTm a
- bindLikelyFreshTmName ∷ String → Type TyName DefaultUni () → (Name → GenTm a) → GenTm a
- bindDat ∷ Datatype TyName Name DefaultUni () → GenTm a → GenTm a
- bindBind ∷ Binding TyName Name DefaultUni DefaultFun () → GenTm a → GenTm a
- bindBinds ∷ Foldable f ⇒ f (Binding TyName Name DefaultUni DefaultFun ()) → GenTm a → GenTm a
- class (Applicative g, Monad g) ⇒ MonadGen (g ∷ Type → Type) where
- newtype GenT (m ∷ Type → Type) a = GenT {}
- suchThat ∷ MonadGen m ⇒ m a → (a → Bool) → m a
- suchThatMaybe ∷ MonadGen m ⇒ m a → (a → Bool) → m (Maybe a)
- oneof ∷ MonadGen m ⇒ [m a] → m a
- frequency ∷ MonadGen m ⇒ [(Int, m a)] → m a
- elements ∷ MonadGen m ⇒ [a] → m a
- growingElements ∷ MonadGen m ⇒ [a] → m a
- listOf ∷ MonadGen m ⇒ m a → m [a]
- listOf1 ∷ MonadGen m ⇒ m a → m [a]
- vectorOf ∷ MonadGen m ⇒ Int → m a → m [a]
- runGenT ∷ GenT m a → Gen (m a)
- oneofMay ∷ MonadGen m ⇒ [m a] → m (Maybe a)
- elementsMay ∷ MonadGen m ⇒ [a] → m (Maybe a)
- growingElementsMay ∷ MonadGen m ⇒ [a] → m (Maybe a)
- class Arbitrary a where
- data Gen a
Documentation
type GenTm = GenT (Reader GenEnv) Source #
Term generators carry around a context to know e.g. what types and terms are in scope.
GenEnv | |
|
suchThatMap ∷ Monad m ⇒ GenT m a → (a → Maybe b) → GenT m b Source #
Create a generator that runs the given generator and applies the given function to produced
values until the result is a Just y
, returning the y
.
runGenTm ∷ GenTm a → Gen a Source #
Run a `GenTm generator in a top-level empty context where we are allowed to generate datatypes.
runGenTmCustom ∷ Int → (Maybe (Type TyName DefaultUni ()) → GenTm (Type TyName DefaultUni (), Term TyName Name DefaultUni DefaultFun ())) → GenTm a → Gen a Source #
Run a GenTm
generator with a plug-in custom generator for terms that is included with
the other generators.
deliver ∷ Monad m ⇒ GenT m (Maybe a) → GenT m a Source #
Create a generator that runs the given generator until the result is a Just x
,
returning the x
.
withNoEscape ∷ GenTm a → GenTm a Source #
Don't allow types to escape from a generator.
ifAstSizeZero ∷ GenTm a → GenTm a → GenTm a Source #
Default to the first generator if the size is zero (or negative), use the second generator otherwise.
astSizeSplit_ ∷ Int → Int → GenTm a → GenTm b → GenTm (a, b) Source #
Split the size between two generators in the ratio specified by the first two arguments.
astSizeSplit ∷ Int → Int → GenTm a → (a → GenTm b) → GenTm (a, b) Source #
Split the size between two generators in the ratio specified by the first two arguments and use the result of the first generator in the second.
getUniques ∷ GenTm (Set Unique) Source #
Get all uniques we have generated and are used in the current context.
genLikelyFreshName ∷ String → GenTm Name Source #
Generate a likely fresh name. See Note [Warning about generating fresh names].
genLikelyFreshNames ∷ [String] → GenTm [Name] Source #
Generate one likely fresh name per string in the input list.
Note that this may not give you a fresh name, if it happens to generate a name that was removed
from the terms map in bindTyName
(due to referencing a now-shadowed type variable) but is still
in the scope.
genLikelyFreshTyName ∷ String → GenTm TyName Source #
Same as genLikelyFreshName
, except gives you a TyName
.
genLikelyFreshTyNames ∷ [String] → GenTm [TyName] Source #
Same as genLikelyFreshNames
, except gives you TyName
s.
genLikelyNotFreshName ∷ String → GenTm Name Source #
Generate a name that likely overlaps with existing names on purpose. If there are no existing
names, generate a fresh name. This function doesn't distinguish between the type- and term-level
scopes, hence it may generate a Name
"clashing" with a previously generated TyName
and not
clashing with any previously generated Name
. Which is a good thing, because we want to check
that Plutus is able to handle such spurious name clashes. Generating weird stuff is useful for a
testing machinery! We don't need any "definitely non-fresh name" anyway, since we get enough
non-fresh names already.
genMaybeFreshName ∷ String → GenTm Name Source #
Generate a fresh name most (roughly 75%) of the time and otherwise generate an already bound name. When there are no bound names generate a fresh name.
bindTyName ∷ TyName → Kind () → GenTm a → GenTm a Source #
Bind a type name to a kind and avoid capturing free type variables.
registerTyName ∷ TyName → GenTm a → GenTm a Source #
Remember that we have generated a type name locally but don't bind it. Useful for non-recursive definitions where we want to control name overlap.
bindTmName ∷ Name → Type TyName DefaultUni () → GenTm a → GenTm a Source #
Bind a term to a type in a generator.
bindTmNames ∷ [(Name, Type TyName DefaultUni ())] → GenTm a → GenTm a Source #
Bind term names
bindLikelyFreshTmName ∷ String → Type TyName DefaultUni () → (Name → GenTm a) → GenTm a Source #
Create a fresh term name, bind it to a type, and use it in a generator.
bindDat ∷ Datatype TyName Name DefaultUni () → GenTm a → GenTm a Source #
Bind a datatype declaration in a generator.
bindBind ∷ Binding TyName Name DefaultUni DefaultFun () → GenTm a → GenTm a Source #
Bind a binding.
bindBinds ∷ Foldable f ⇒ f (Binding TyName Name DefaultUni DefaultFun ()) → GenTm a → GenTm a Source #
Bind multiple bindings
class (Applicative g, Monad g) ⇒ MonadGen (g ∷ Type → Type) where Source #
liftGen ∷ Gen a → g a Source #
variant ∷ Integral n ⇒ n → g a → g a Source #
sized ∷ (Int → g a) → g a Source #
suchThat ∷ MonadGen m ⇒ m a → (a → Bool) → m a Source #
Generates a value that satisfies a predicate.
suchThatMaybe ∷ MonadGen m ⇒ m a → (a → Bool) → m (Maybe a) Source #
Tries to generate a value that satisfies a predicate.
oneof ∷ MonadGen m ⇒ [m a] → m a Source #
Randomly uses one of the given generators. The input list must be non-empty.
frequency ∷ MonadGen m ⇒ [(Int, m a)] → m a Source #
Chooses one of the given generators, with a weighted random distribution. The input list must be non-empty.
elements ∷ MonadGen m ⇒ [a] → m a Source #
Generates one of the given values. The input list must be non-empty.
growingElements ∷ MonadGen m ⇒ [a] → m a Source #
Takes a list of elements of increasing size, and chooses among an initial segment of the list. The size of this initial segment increases with the size parameter. The input list must be non-empty.
listOf ∷ MonadGen m ⇒ m a → m [a] Source #
Generates a list of random length. The maximum length depends on the size parameter.
listOf1 ∷ MonadGen m ⇒ m a → m [a] Source #
Generates a non-empty list of random length. The maximum length depends on the size parameter.
elementsMay ∷ MonadGen m ⇒ [a] → m (Maybe a) Source #
Generates one of the given values.
growingElementsMay ∷ MonadGen m ⇒ [a] → m (Maybe a) Source #
Takes a list of elements of increasing size, and chooses among an initial segment of the list. The size of this initial segment increases with the size parameter.
class Arbitrary a where Source #
Random generation and shrinking of values.
QuickCheck provides Arbitrary
instances for most types in base
,
except those which incur extra dependencies.
For a wider range of Arbitrary
instances see the
quickcheck-instances
package.
A generator for values of the given type.
It is worth spending time thinking about what sort of test data
you want - good generators are often the difference between
finding bugs and not finding them. You can use sample
,
label
and classify
to check the quality of your test data.
There is no generic arbitrary
implementation included because we don't
know how to make a high-quality one. If you want one, consider using the
testing-feat or
generic-random packages.
The QuickCheck manual goes into detail on how to write good generators. Make sure to look at it, especially if your type is recursive!
Produces a (possibly) empty list of all the possible immediate shrinks of the given value.
The default implementation returns the empty list, so will not try to
shrink the value. If your data type has no special invariants, you can
enable shrinking by defining shrink =
, but by customising
the behaviour of genericShrink
shrink
you can often get simpler counterexamples.
Most implementations of shrink
should try at least three things:
- Shrink a term to any of its immediate subterms.
You can use
subterms
to do this. - Recursively apply
shrink
to all immediate subterms. You can userecursivelyShrink
to do this. - Type-specific shrinkings such as replacing a constructor by a simpler constructor.
For example, suppose we have the following implementation of binary trees:
data Tree a = Nil | Branch a (Tree a) (Tree a)
We can then define shrink
as follows:
shrink Nil = [] shrink (Branch x l r) = -- shrink Branch to Nil [Nil] ++ -- shrink to subterms [l, r] ++ -- recursively shrink subterms [Branch x' l' r' | (x', l', r') <- shrink (x, l, r)]
There are a couple of subtleties here:
- QuickCheck tries the shrinking candidates in the order they
appear in the list, so we put more aggressive shrinking steps
(such as replacing the whole tree by
Nil
) before smaller ones (such as recursively shrinking the subtrees). - It is tempting to write the last line as
[Branch x' l' r' | x' <- shrink x, l' <- shrink l, r' <- shrink r]
but this is the wrong thing! It will force QuickCheck to shrinkx
,l
andr
in tandem, and shrinking will stop once one of the three is fully shrunk.
There is a fair bit of boilerplate in the code above.
We can avoid it with the help of some generic functions.
The function genericShrink
tries shrinking a term to all of its
subterms and, failing that, recursively shrinks the subterms.
Using it, we can define shrink
as:
shrink x = shrinkToNil x ++ genericShrink x where shrinkToNil Nil = [] shrinkToNil (Branch _ l r) = [Nil]
genericShrink
is a combination of subterms
, which shrinks
a term to any of its subterms, and recursivelyShrink
, which shrinks
all subterms of a term. These may be useful if you need a bit more
control over shrinking than genericShrink
gives you.
A final gotcha: we cannot define shrink
as simply
as this shrinks shrink
x = Nil:genericShrink
xNil
to Nil
, and shrinking will go into an
infinite loop.
If all this leaves you bewildered, you might try
to begin with,
after deriving shrink
= genericShrink
Generic
for your type. However, if your data type has any
special invariants, you will need to check that genericShrink
can't break those invariants.
Instances
A generator for values of type a
.
The third-party packages
QuickCheck-GenT
and
quickcheck-transformer
provide monad transformer versions of Gen
.