| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Proarrow.Testing
Description
Generic property-testing infrastructure for categories: Testable says how to generate and
enumerate the objects of a kind, TestableProfunctor and TestableType how to generate values
(using falsify generators), and TestingEqShow provides semantic equality and display for
values without useful structural Eq/Show (functions, opaque morphisms). Instances for your
own category plus the law checks in Proarrow.Testing.Laws give it a test suite.
Synopsis
- class (TestableProfunctor (Hom k), TestableTypeP (Hom k), CategoryOf k) => Testable k where
- class (Testable j, Testable k, Profunctor p, forall (a :: k) (b :: j). (TestOb a, TestOb b) => TestingEqShow (p a b)) => TestableProfunctor (p :: j +-> k) where
- genProfunctorElt :: String -> Property (SomeProfunctorElt p)
- class TestingEqShow a => TestableType a where
- class (forall (a :: k) (b :: j). (TestOb a, TestOb b) => TestableType (p a b)) => TestableTypeP (p :: j +-> k)
- class TestingEqShow a where
- class (Testable k, forall (a :: k). Ob a => TestOb' a) => TestObIsOb k
- class TestOb a => TestOb' (a :: k)
- testObFromOb :: forall {k} (a :: k) r. (TestObIsOb k, Ob a) => (TestOb a => r) -> r
- type WithTestOb k = forall (a :: k) r. Ob a => (TestOb a => r) -> r
- type WithTestOb2 k = forall (a :: k) (b :: k) r. (TestOb a, TestOb b) => (TestOb (a ** b) => r) -> r
- type WithTestObProd k = forall (a :: k) (b :: k) r. (TestOb a, TestOb b) => (TestOb (a && b) => r) -> r
- type WithTestObCoprod k = forall (a :: k) (b :: k) r. (TestOb a, TestOb b) => (TestOb (a || b) => r) -> r
- type WithTestObExp k = forall (a :: k) (b :: k) r. (TestOb a, TestOb b) => (TestOb (a ~~> b) => r) -> r
- type WithTestObDual k = forall (a :: k) r. TestOb a => (TestOb (Dual a) => r) -> r
- type WithTestObRep k1 (p :: k1 +-> k) = forall (a :: k1) r. TestOb a => (TestOb (p % a) => r) -> r
- type WithTestObCorep k1 (p :: k +-> k1) = forall (a :: k1) r. TestOb a => (TestOb (p %% a) => r) -> r
- withTestOb2Def :: (TestObIsOb k, Monoidal k) => WithTestOb2 k
- withTestObProdDef :: (TestObIsOb k, HasBinaryProducts k) => WithTestObProd k
- withTestObCoprodDef :: (TestObIsOb k, HasBinaryCoproducts k) => WithTestObCoprod k
- withTestObExpDef :: (TestObIsOb k, Closed k) => WithTestObExp k
- withTestObDualDef :: (TestObIsOb k, Dialogue k) => WithTestObDual k
- type Some k = SomeOf (TestOb' :: k -> Constraint)
- data SomeOf (c :: OB k) where
- mapSome :: forall (f :: j -> k) -> (forall (a :: j). TestOb a => TestOb' (f a)) => Some j -> Some k
- genOb :: Testable k => Property (Some k)
- genObSmall :: Testable k => Property (Some k)
- genObSuchThat :: Testable k => (Some k -> Bool) -> Property (Some k)
- genObSuchThatWith :: Testable k => Gen (Some k) -> (Some k -> Bool) -> Property (Some k)
- genSomeDef :: forall {k} (obs :: [k]). (Testable k, KnownListOf (TestOb' :: k -> Constraint) obs) => Gen (Some k)
- genSomeFinite :: (Enumerable k, TestObIsOb k) => Gen (Some k)
- genSomeList :: String -> [Some k] -> Gen (Some k)
- mkSomeList :: forall {k} (as :: [k]). KnownListOf (TestOb' :: k -> Constraint) as => [Some k]
- data SomeProfunctorElt (p :: j +-> k) where
- SomeP :: forall {k} {j} (a :: k) (b :: j) (p :: j +-> k). (TestOb a, TestOb b) => p a b -> SomeProfunctorElt p
- someP :: forall {k} {j} p (a :: j) (b :: k). (Profunctor p, TestObIsOb j, TestObIsOb k) => p a b -> SomeProfunctorElt p
- data GenTotal a where
- pattern GenNonEmpty :: Gen a -> GenTotal a
- invmap :: (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
- isGenNonEmpty :: TestableType a => Bool
- optGen :: [a] -> GenTotal a
- oneElem :: a -> GenTotal a
- genBoth :: (TestableType a, TestableType b) => (a -> b -> c) -> (c -> a) -> (c -> b) -> GenTotal c
- genElements :: forall {j} {k} p (a :: k) (b :: j). (Finitary p, Ob a, Ob b) => GenTotal (p a b)
- oneOfTotal :: [GenTotal a] -> GenTotal a
- genP :: TestableType a => Property a
- genNamed :: TestableType a => String -> Property a
- genWithNamed :: String -> (a -> Maybe String) -> Gen a -> Property a
- genSuchThat :: Gen key -> (key -> Bool) -> Gen key
- someElem :: Show a => [a] -> Property a
- someElemNamed :: Show a => String -> [a] -> Property a
- someElemWith :: (a -> String) -> [a] -> Property a
- newtype ShowP a = ShowP {
- unShowP :: a
- applyFunP :: Fun (ShowP a) (ShowP b) -> a -> b
- check :: String -> Bool -> Property ()
- expect :: (Eq a, Show a) => String -> a -> a -> Property ()
- testEq :: TestingEqShow a => String -> String -> a -> String -> a -> Property ()
- eqHask :: (TestableType a, TestingEqShow b) => (a -> b) -> (a -> b) -> Property Bool
- sampleT :: TestableType t => IO (Maybe String)
- sampleP :: forall {j} {k} (p :: j +-> k). (Testable j, Testable k, TestableProfunctor p) => IO (Maybe String)
- sampleK :: Testable k => IO (Maybe String)
Describing a category
class (TestableProfunctor (Hom k), TestableTypeP (Hom k), CategoryOf k) => Testable k where Source Github #
A kind whose objects can be enumerated and displayed.
Methods
showOb :: forall (a :: k). TestOb a => String Source Github #
genSome :: Gen (Some k) Source Github #
obFromTestOb :: forall (a :: k) r. TestOb a => (Ob a => r) -> r Source Github #
default obFromTestOb :: forall (a :: k) r. (TestOb a ~ Ob a, TestOb a) => (Ob a => r) -> r Source Github #
genSomeSmall :: Gen (Some k) Source Github #
The palette for properties whose cost grows steeply with object size: in practice those
that enumerate an internal hom, which is brute force over tables and doubly exponential (an
object with hom-sizes [2,4,2,4] has the hom into it at [1024,256,1024,256]). Defaults to
genSome. Override it only when genSome draws objects too big for
testClosed to terminate.
An instance that wraps another kind's palette must forward this too, as the wrapper instances
below do, or testClosed silently gets the wide one.
class (Testable j, Testable k, Profunctor p, forall (a :: k) (b :: j). (TestOb a, TestOb b) => TestingEqShow (p a b)) => TestableProfunctor (p :: j +-> k) where Source Github #
Minimal complete definition
Nothing
Methods
genProfunctorElt :: String -> Property (SomeProfunctorElt p) Source Github #
The default implementation generates an object a, then an object b for which p a b
has elements (see genObSuchThat), and then a value of type p a b.
default genProfunctorElt :: forall. TestableTypeP p => String -> Property (SomeProfunctorElt p) Source Github #
Instances
class TestingEqShow a => TestableType a where Source Github #
Instances
class (forall (a :: k) (b :: j). (TestOb a, TestOb b) => TestableType (p a b)) => TestableTypeP (p :: j +-> k) Source Github #
Instances
| (forall (a :: k) (b :: j). (TestOb a, TestOb b) => TestableType (p a b)) => TestableTypeP (p :: j +-> k) Source Github # | |
Defined in Proarrow.Testing | |
class TestingEqShow a where Source Github #
Minimal complete definition
Nothing
Instances
| TestingEqShow (Unit a b) Source Github # | |
| (TestableType a, TestingEqShow b) => TestingEqShow (a -> b) Source Github # | |
| (Finitary p, Finitary q, FiniteCat j, FiniteCat k) => TestingEqShow (Prof p q) Source Github # | A natural transformation between finitary profunctors, compared and shown by its table and
drawn from |
| (FiniteCat j, FiniteCat k, TestOb a, TestOb b) => TestingEqShow (Sieve a b) Source Github # | A sieve is a table of booleans over the points of the representable, and Drawing one enumerates every sieve at that pair of objects, a count exponential in the size of the representable, so this is the generator to look at first if a suite gets slow. The |
| TestingEqShow (TerminalProfunctor a b) Source Github # | The terminal profunctor has one element at every pair of objects. |
Defined in Proarrow.Testing Methods eqP :: TerminalProfunctor a b -> TerminalProfunctor a b -> Property Bool Source Github # | |
| (FiniteCat j, FiniteCat k, TestOb a, TestOb b) => TestingEqShow (ClosedSieve t a b) Source Github # | A closed sieve is a sieve, and is compared and shown as one. Drawing one is dearer still than
drawing a sieve: the closed ones are found by taking the |
Defined in Proarrow.Testing Methods eqP :: ClosedSieve t a b -> ClosedSieve t a b -> Property Bool Source Github # | |
| TestingEqShow (p b a) => TestingEqShow (Op p ('OP a) ('OP b)) Source Github # | |
| (Finitary p, Finitary q, FiniteCat j, FiniteCat k) => TestingEqShow (Sub (Prof :: (j +-> k) -> (j +-> k) -> Type) ('SUB p :: SUBCAT ob) ('SUB q :: SUBCAT ob)) Source Github # | A hom-set of a full subcategory of finitary profunctors ( |
Defined in Proarrow.Testing Methods eqP :: Sub (Prof :: (j +-> k) -> (j +-> k) -> Type) ('SUB p :: SUBCAT ob) ('SUB q :: SUBCAT ob) -> Sub (Prof :: (j +-> k) -> (j +-> k) -> Type) ('SUB p :: SUBCAT ob) ('SUB q :: SUBCAT ob) -> Property Bool Source Github # showP :: Sub (Prof :: (j +-> k) -> (j +-> k) -> Type) ('SUB p :: SUBCAT ob) ('SUB q :: SUBCAT ob) -> String Source Github # | |
| TestingEqShow (p a b) => TestingEqShow (Prod p ('PR a) ('PR b)) Source Github # | |
| (TestingEqShow ((f @ a) ~> b), Ob a) => TestingEqShow (Corep f a b) Source Github # | |
| TestingEqShow (f a ~> b) => TestingEqShow (Costar f a b) Source Github # | |
| TestingEqShow (a ~> f b) => TestingEqShow (Star f a b) Source Github # | The elements of |
| (TestingEqShow (a ~> (f @ b)), Ob b) => TestingEqShow (Rep f a b) Source Github # | |
| (HasFiniteCovers t k, Finitary p, FiniteCat j, FiniteCat k, TestOb a, TestOb b) => TestingEqShow (Plus t p a b) Source Github # | Compared by |
| (TestingEqShow (p a b), TestingEqShow (q a b)) => TestingEqShow ((p :+: q) a b) Source Github # | An element of a coproduct of profunctors is an element of one side, tagged. |
| (Finitary p, Finitary q, FiniteCat j, FiniteCat k, TestOb a, TestOb b) => TestingEqShow ((p :~>: q) a b) Source Github # | An element of the internal hom is a natural transformation out of a weight, which |
| (TestingEqShow (p a b), TestingEqShow (q a b)) => TestingEqShow ((p :*: q) a b) Source Github # | An element of a product of profunctors is a pair of elements. |
| (Testable j, Testable k, TestOb a, TestOb x, TestOb b, TestOb c) => TestingEqShow (Yo x ('OP b) a c) Source Github # | An element of the Yoneda embedding is an arrow into |
| TestingEqShow (Tabulated t lm rm a b) Source Github # | A |
| (Testable j, Testable k, Finitary v, Finitary p, FiniteCat i, FiniteCat k, TestOb a, TestOb b) => TestingEqShow (Ran ('OP v) p a b) Source Github # | |
| (Testable j, Testable k, Finitary w, Finitary p, FiniteCat i, FiniteCat j, TestOb a, TestOb b) => TestingEqShow (Rift ('OP w) p a b) Source Github # | The right Kan lift and extension of finitary profunctors, compared and shown by index, as the internal hom is. |
| (TestingEqShow (catk a1 b1), TestingEqShow (catj a2 b2)) => TestingEqShow ((catk :**: catj) '(a1, a2) '(b1, b2)) Source Github # | |
class (Testable k, forall (a :: k). Ob a => TestOb' a) => TestObIsOb k Source Github #
Instances
| (Testable k, forall (a :: k). Ob a => TestOb' a) => TestObIsOb k Source Github # | |
Defined in Proarrow.Testing | |
testObFromOb :: forall {k} (a :: k) r. (TestObIsOb k, Ob a) => (TestOb a => r) -> r Source Github #
Recover from TestOb a where Ob aTestObIsOb provides it: the converse of
'obFromTestOb @_', a function for the same reason.
Objecthood witnesses
type WithTestOb k = forall (a :: k) r. Ob a => (TestOb a => r) -> r Source Github #
How TestOb is closed under the structure a law-checker is about.
Every law-checker in Proarrow.Testing.Laws that needs one takes it as an explicit rank-2 argument, since in
general a category may make only some of its objects testable; the _-suffixed variants supply
the trivial witness. These synonyms only name the shapes, which would otherwise be spelled out
in forty-odd signatures.
type WithTestOb2 k = forall (a :: k) (b :: k) r. (TestOb a, TestOb b) => (TestOb (a ** b) => r) -> r Source Github #
is closed under the tensor.TestOb
type WithTestObProd k = forall (a :: k) (b :: k) r. (TestOb a, TestOb b) => (TestOb (a && b) => r) -> r Source Github #
is closed under the binary product.TestOb
type WithTestObCoprod k = forall (a :: k) (b :: k) r. (TestOb a, TestOb b) => (TestOb (a || b) => r) -> r Source Github #
is closed under the binary coproduct.TestOb
type WithTestObExp k = forall (a :: k) (b :: k) r. (TestOb a, TestOb b) => (TestOb (a ~~> b) => r) -> r Source Github #
is closed under the internal hom.TestOb
type WithTestObDual k = forall (a :: k) r. TestOb a => (TestOb (Dual a) => r) -> r Source Github #
is closed under dualization.TestOb
type WithTestObRep k1 (p :: k1 +-> k) = forall (a :: k1) r. TestOb a => (TestOb (p % a) => r) -> r Source Github #
is closed under a representable profunctor.TestOb
type WithTestObCorep k1 (p :: k +-> k1) = forall (a :: k1) r. TestOb a => (TestOb (p %% a) => r) -> r Source Github #
is closed under a corepresentable profunctor.TestOb
withTestOb2Def :: (TestObIsOb k, Monoidal k) => WithTestOb2 k Source Github #
withTestObProdDef :: (TestObIsOb k, HasBinaryProducts k) => WithTestObProd k Source Github #
withTestObCoprodDef :: (TestObIsOb k, HasBinaryCoproducts k) => WithTestObCoprod k Source Github #
withTestObExpDef :: (TestObIsOb k, Closed k) => WithTestObExp k Source Github #
withTestObDualDef :: (TestObIsOb k, Dialogue k) => WithTestObDual k Source Github #
Objects
data SomeOf (c :: OB k) where Github #
Some type with the evidence c, which one known only at runtime.
Instances
mapSome :: forall (f :: j -> k) -> (forall (a :: j). TestOb a => TestOb' (f a)) => Some j -> Some k Source Github #
genObSmall :: Testable k => Property (Some k) Source Github #
genOb from the small palette. See genSomeSmall.
genObSuchThat :: Testable k => (Some k -> Bool) -> Property (Some k) Source Github #
genOb, but resampled (see genSuchThat) until isUsable accepts the object.
genObSuchThatWith :: Testable k => Gen (Some k) -> (Some k -> Bool) -> Property (Some k) Source Github #
genObSuchThat with objects drawn from the given generator, e.g. genSomeSmall.
genSomeDef :: forall {k} (obs :: [k]). (Testable k, KnownListOf (TestOb' :: k -> Constraint) obs) => Gen (Some k) Source Github #
genSomeFinite :: (Enumerable k, TestObIsOb k) => Gen (Some k) Source Github #
The palette of a category that already knows its own objects:
is the list Objects kgenSomeDef would otherwise be given by hand, and writing it twice lets the two
drift apart. Only for kinds that really are finite categories. A palette like "four
cardinalities out of infinitely many" is a sample, not an enumeration, and has to stay
hand-picked.
mkSomeList :: forall {k} (as :: [k]). KnownListOf (TestOb' :: k -> Constraint) as => [Some k] Source Github #
The objects of a type-level list.
Profunctor elements
data SomeProfunctorElt (p :: j +-> k) where Source Github #
Constructors
| SomeP :: forall {k} {j} (a :: k) (b :: j) (p :: j +-> k). (TestOb a, TestOb b) => p a b -> SomeProfunctorElt p |
Instances
| (forall (a :: k) (b :: j). (TestOb a, TestOb b) => TestingEqShow (p a b), Testable k, Testable j) => Show (SomeProfunctorElt p) Source Github # | |
Defined in Proarrow.Testing | |
someP :: forall {k} {j} p (a :: j) (b :: k). (Profunctor p, TestObIsOb j, TestObIsOb k) => p a b -> SomeProfunctorElt p Source Github #
Generators
falsify generators, wrapped so that an empty type is a first-class case rather than a
generator that fails at run time: match GenEmpty first, then GenNonEmpty. The two are a
COMPLETE set. The representation behind GenNonEmpty is not exported on purpose. Go
through the pattern, which is total.
isGenNonEmpty :: TestableType a => Bool Source Github #
True if a type's generator is non-empty. A pure check on TestableTypes gen. It
doesn't sample anything, so it is cheap to call as often as convenient, e.g. once in a
genSuchThat predicate and again in the gen/genNamed call that produces a value.
genBoth :: (TestableType a, TestableType b) => (a -> b -> c) -> (c -> a) -> (c -> b) -> GenTotal c Source Github #
A generator for a two-component value: if either component has no values then neither does the pair, and otherwise the two are drawn independently.
GenEmpty carries its proof of emptiness as a function out of the empty type, so reusing a
component's proof for the pair means getting at that component first. Hence the two projections
alongside the constructor.
genElements :: forall {j} {k} p (a :: k) (b :: j). (Finitary p, Ob a, Ob b) => GenTotal (p a b) Source Github #
Draw from a finitary profunctor's own enumeration, an empty hom-set being GenEmpty rather than
an error: a profunctor built by the library can be empty at a pair of objects with nothing wrong.
For a hand-written fixture prefer a palette of its own. testFinitary
says why a generator that is the enumeration makes the round-trip law vacuous.
oneOfTotal :: [GenTotal a] -> GenTotal a Source Github #
Uniformly choose among any number of alternatives, dropping the empty ones. Plain <|>
only combines two generators at 50/50, so chaining it over more than two alternatives
associates pairwise and skews weight towards whichever branch ends up outermost in the
resulting tree. Use this whenever there are more than two alternatives to pick fairly among.
genSuchThat :: Gen key -> (key -> Bool) -> Gen key Source Github #
Resample genKey (cheaply, within Gen) up to maxTries times until isUsable accepts
the draw, before ever asking Property to commit to a choice.
A Property-level discard restarts the whole property and can trip falsify's discard-ratio
limit, aborting the run. So when a later dependent draw (e.g. "a morphism out of this
object") is likely to be empty for a bad choice, reject that choice here. After maxTries the
last draw is returned anyway, and the caller's own discard handles it.
Generating functions
ShowP supplies the Show instance falsify needs on both parameters of a generated
Fun, derived from showP; applyFunP unwraps on the way back out.
newtype ShowP a Source Github #
Supplies a Show instance derived from showP.
falsify's Show instance for Fun needs Show on both parameters, and we
only ever have TestingEqShow. Rather than reinterpret a at the
wrapped type after the fact, Fun a bGenFun generates at
from the start, so Fun (ShowP a) (ShowP b)show applies directly and
no coercion is involved. applyFunP unwraps on the way back out.
applyFunP :: Fun (ShowP a) (ShowP b) -> a -> b Source Github #
Apply a generated function, wrapping and unwrapping the ShowP it was
generated at. Both directions are ordinary newtype constructor applications.
Assertions
check :: String -> Bool -> Property () Source Github #
Fail with the message unless the condition holds. For the assertions a worked example makes, which no generic law-checking property covers.
expect :: (Eq a, Show a) => String -> a -> a -> Property () Source Github #
Check a measured value against the expected one, showing both.
testEq :: TestingEqShow a => String -> String -> a -> String -> a -> Property () Source Github #
Check that two values are semantically equal, naming both sides so a failure says which law broke and what the two sides came out as.
eqHask :: (TestableType a, TestingEqShow b) => (a -> b) -> (a -> b) -> Property Bool Source Github #
Interactive debugging
Run a generator once in ghci and print what it produced. These trace to stdout and are
for exploring a generator by hand, not for use inside a test.