{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE RequiredTypeArguments #-}

-- | 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.
module Proarrow.Testing
  ( -- * Describing a category
    Testable (..)
  , TestableProfunctor (..)
  , TestableType (..)
  , TestableTypeP
  , TestingEqShow (..)
  , TestObIsOb
  , TestOb'
  , testObFromOb

    -- * Objecthood witnesses
  , WithTestOb
  , WithTestOb2
  , WithTestObProd
  , WithTestObCoprod
  , WithTestObExp
  , WithTestObDual
  , WithTestObRep
  , WithTestObCorep
  , withTestOb2Def
  , withTestObProdDef
  , withTestObCoprodDef
  , withTestObExpDef
  , withTestObDualDef

    -- * Objects
  , Some
  , SomeOf (..)
  , mapSome
  , genOb
  , genObSmall
  , genObSuchThat
  , genObSuchThatWith
  , genSomeDef
  , genSomeFinite
  , genSomeList
  , mkSomeList

    -- * Profunctor elements
  , SomeProfunctorElt (..)
  , someP

    -- * 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.
  , GenTotal (GenEmpty)
  , pattern GenNonEmpty
  , invmap
  , isGenNonEmpty
  , optGen
  , oneElem
  , genBoth
  , genElements
  , oneOfTotal
  , genP
  , genNamed
  , genWithNamed
  , genSuchThat
  , someElem
  , someElemNamed
  , someElemWith

    -- * Generating functions

    -- | 'ShowP' supplies the 'Show' instance @falsify@ needs on both parameters of a generated
    -- 'Test.Falsify.Fun', derived from 'showP'; 'applyFunP' unwraps on the way back out.
  , ShowP (..)
  , applyFunP

    -- * Assertions
  , check
  , expect
  , testEq
  , eqHask

    -- * 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.
  , sampleT
  , sampleP
  , sampleK
  ) where

import Data.Falsify.ConcreteFun qualified as ConcreteFun
import Data.Kind (Constraint, Type)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Maybe (mapMaybe)
import GHC.Exts qualified as GHC
import Test.Falsify (Fun, Property, applyFun, discard, genWith, testFailed)
import Test.Falsify.Generator (Function (..), Gen, elem, fun, minimalValue, oneof)
import Prelude hiding (elem, fst, id, snd, (.), (>>))

import Control.Applicative (Alternative (..))
import Control.Monad (ap, unless)
import Debug.Trace (traceM, traceShowM)
import Proarrow.Category.Enriched.Finitary (Finitary (..), FiniteCat, foreachOb)
import Proarrow.Category.Enriched.Finitary.Sheaf (ClosedSieve (..), Plus, plusTable, samePlus)
import Proarrow.Category.Enriched.Finitary.Topos (KnownTables, Tabulated (..), natTable, natTransformations, sieveTable)
import Proarrow.Category.Enriched.Thin (Enumerable)
import Proarrow.Category.Instance.Opposite (OPPOSITE (..), Op (..))
import Proarrow.Category.Instance.Product (Fst, Snd, (:**:) (..))
import Proarrow.Category.Instance.Prof (Prof (..))
import Proarrow.Category.Instance.Sub (SUBCAT (..), Sub (..))
import Proarrow.Category.Instance.Unit (Unit (..))
import Proarrow.Category.Monoidal qualified as M
import Proarrow.Category.Monoidal.Closed qualified as Exponential
import Proarrow.Category.Monoidal.Dialogue qualified as SA
import Proarrow.Category.Sheaf (HasFiniteCovers)
import Proarrow.Colimit.BinaryCoproduct qualified as BinaryCoproduct
import Proarrow.Core (CAT, CategoryOf (..), Hom, Is, OB, Profunctor (..), Promonad (..), UN, type (+->))
import Proarrow.Functor (type (@))
import Proarrow.Functor qualified as Rep
import Proarrow.Limit.BinaryProduct (PROD (..), Prod (..))
import Proarrow.Limit.BinaryProduct qualified as BinaryProduct
import Proarrow.Object (KnownListOf (..), SomeOf (..), someOfList)
import Proarrow.Profunctor.Corepresentable (Corep (..), type (%%))
import Proarrow.Profunctor.Instance.Coproduct ((:+:) (..))
import Proarrow.Profunctor.Instance.Costar (Costar, pattern Costar)
import Proarrow.Profunctor.Instance.Exponential ((:~>:) (..))
import Proarrow.Profunctor.Instance.Product (fstP, sndP, (:*:) (..))
import Proarrow.Profunctor.Instance.Ran (Ran (..))
import Proarrow.Profunctor.Instance.Rift (Rift (..))
import Proarrow.Profunctor.Instance.Sieve (Sieve)
import Proarrow.Profunctor.Instance.Star (Star, pattern Star)
import Proarrow.Profunctor.Instance.Terminal (TerminalProfunctor (..))
import Proarrow.Profunctor.Instance.Yoneda (Yo (..))
import Proarrow.Profunctor.Representable (Rep (..), type (%))
import Test.Falsify.Interactive (falsify)

data GenTotal a where
  GenEmpty :: ~(forall x. a -> x) -> GenTotal a
  GenNENonFun :: Gen a -> GenTotal a
  GenFun :: (TestableType a, TestableType b) => ((a -> b) -> p) -> Gen (Fun (ShowP a) (ShowP b)) -> GenTotal p

invmap :: (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap :: forall a b. (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap a -> b
_ b -> a
f' (GenEmpty forall x. a -> x
g) = (forall x. b -> x) -> GenTotal b
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty (a -> x
forall x. a -> x
g (a -> x) -> (b -> a) -> b -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. b -> a
f')
invmap a -> b
f b -> a
_ (GenNENonFun Gen a
g) = Gen b -> GenTotal b
forall a. Gen a -> GenTotal a
GenNENonFun ((a -> b) -> Gen a -> Gen b
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Gen a
g)
invmap a -> b
f b -> a
_ (GenFun (a -> b) -> a
f' Gen (Fun (ShowP a) (ShowP b))
g) = ((a -> b) -> b) -> Gen (Fun (ShowP a) (ShowP b)) -> GenTotal b
forall a b p.
(TestableType a, TestableType b) =>
((a -> b) -> p) -> Gen (Fun (ShowP a) (ShowP b)) -> GenTotal p
GenFun (a -> b
f (a -> b) -> ((a -> b) -> a) -> (a -> b) -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. (a -> b) -> a
f') Gen (Fun (ShowP a) (ShowP b))
g

flatten :: GenTotal a -> Gen a
flatten :: forall a. GenTotal a -> Gen a
flatten (GenNENonFun Gen a
g) = Gen a
g
flatten (GenFun (a -> b) -> a
f Gen (Fun (ShowP a) (ShowP b))
g) = (a -> b) -> a
f ((a -> b) -> a)
-> (Fun (ShowP a) (ShowP b) -> a -> b)
-> Fun (ShowP a) (ShowP b)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Fun (ShowP a) (ShowP b) -> a -> b
forall a b. Fun (ShowP a) (ShowP b) -> a -> b
applyFunP (Fun (ShowP a) (ShowP b) -> a)
-> Gen (Fun (ShowP a) (ShowP b)) -> Gen a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Fun (ShowP a) (ShowP b))
g
flatten (GenEmpty forall x. a -> x
_) = String -> Gen a
forall a. HasCallStack => String -> a
error String
"flatten: Match on GenEmpty first"

pattern GenNonEmpty :: Gen a -> GenTotal a
pattern $mGenNonEmpty :: forall {r} {a}. GenTotal a -> (Gen a -> r) -> ((# #) -> r) -> r
$bGenNonEmpty :: forall a. Gen a -> GenTotal a
GenNonEmpty g <- (flatten -> g)
  where
    GenNonEmpty Gen a
g = Gen a -> GenTotal a
forall a. Gen a -> GenTotal a
GenNENonFun Gen a
g

{-# COMPLETE GenEmpty, GenNonEmpty #-}

instance Functor GenTotal where
  fmap :: forall a b. (a -> b) -> GenTotal a -> GenTotal b
fmap a -> b
f = (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
forall a b. (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap a -> b
f (String -> b -> a
forall a. HasCallStack => String -> a
error String
"fmap GenTotal")

instance Applicative GenTotal where
  pure :: forall a. a -> GenTotal a
pure a
a = Gen a -> GenTotal a
forall a. Gen a -> GenTotal a
GenNENonFun (a -> Gen a
forall a. a -> Gen a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure a
a)
  <*> :: forall a b. GenTotal (a -> b) -> GenTotal a -> GenTotal b
(<*>) = GenTotal (a -> b) -> GenTotal a -> GenTotal b
forall (m :: Type -> Type) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Alternative GenTotal where
  empty :: forall a. GenTotal a
empty = (forall x. a -> x) -> GenTotal a
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty (String -> a -> x
forall a. HasCallStack => String -> a
error String
"empty")
  GenEmpty forall x. a -> x
f <|> :: forall a. GenTotal a -> GenTotal a -> GenTotal a
<|> GenEmpty forall x. a -> x
_ = (forall x. a -> x) -> GenTotal a
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty a -> x
forall x. a -> x
f
  GenEmpty forall x. a -> x
_ <|> GenTotal a
g = GenTotal a
g
  GenTotal a
f <|> GenEmpty forall x. a -> x
_ = GenTotal a
f
  GenNonEmpty Gen a
g <|> GenNonEmpty Gen a
h = Gen a -> GenTotal a
forall a. Gen a -> GenTotal a
GenNonEmpty (NonEmpty (Gen a) -> Gen a
forall a. NonEmpty (Gen a) -> Gen a
oneof (Gen a
g Gen a -> [Gen a] -> NonEmpty (Gen a)
forall a. a -> [a] -> NonEmpty a
:| [Gen a
h]))

-- | 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.
oneOfTotal :: [GenTotal a] -> GenTotal a
oneOfTotal :: forall a. [GenTotal a] -> GenTotal a
oneOfTotal [GenTotal a]
gts = case (GenTotal a -> Maybe (Gen a)) -> [GenTotal a] -> [Gen a]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe GenTotal a -> Maybe (Gen a)
forall {a}. GenTotal a -> Maybe (Gen a)
toGen [GenTotal a]
gts of
  [] -> GenTotal a
forall a. GenTotal a
forall (f :: Type -> Type) a. Alternative f => f a
empty
  Gen a
g : [Gen a]
gs -> Gen a -> GenTotal a
forall a. Gen a -> GenTotal a
GenNonEmpty (NonEmpty (Gen a) -> Gen a
forall a. NonEmpty (Gen a) -> Gen a
oneof (Gen a
g Gen a -> [Gen a] -> NonEmpty (Gen a)
forall a. a -> [a] -> NonEmpty a
:| [Gen a]
gs))
  where
    toGen :: GenTotal a -> Maybe (Gen a)
toGen (GenEmpty forall x. a -> x
_) = Maybe (Gen a)
forall a. Maybe a
Nothing
    toGen (GenNonEmpty Gen a
g) = Gen a -> Maybe (Gen a)
forall a. a -> Maybe a
Just Gen a
g

instance Monad GenTotal where
  GenEmpty forall x. a -> x
_ >>= :: forall a b. GenTotal a -> (a -> GenTotal b) -> GenTotal b
>>= a -> GenTotal b
_ = (forall x. b -> x) -> GenTotal b
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty (String -> b -> x
forall a. HasCallStack => String -> a
error String
">>= GenEmpty")
  GenNonEmpty Gen a
g >>= a -> GenTotal b
f = case a -> GenTotal b
f (Gen a -> a
forall a. Gen a -> a
minimalValue Gen a
g) of
    GenEmpty forall x. b -> x
x -> (forall x. b -> x) -> GenTotal b
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty b -> x
forall x. b -> x
x
    GenTotal b
_ -> Gen b -> GenTotal b
forall a. Gen a -> GenTotal a
GenNonEmpty do
      p <- (a -> GenTotal b) -> Gen a -> Gen (GenTotal b)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> GenTotal b
f Gen a
g
      case p of
        GenEmpty forall x. b -> x
_ -> String -> Gen b
forall a. HasCallStack => String -> a
error String
">>= GenEmpty"
        GenNonEmpty Gen b
g' -> Gen b
g'

class TestingEqShow a where
  eqP :: a -> a -> Property Bool
  default eqP :: (Eq a) => a -> a -> Property Bool
  eqP a
l a
r = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a
l a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
r)
  showP :: a -> String
  default showP :: (Show a) => a -> String
  showP = a -> String
forall a. Show a => a -> String
show

class (TestingEqShow a) => TestableType a where
  gen :: GenTotal a

-- | 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 @'Fun' a b@ at the
-- wrapped type after the fact, @GenFun@ generates at
-- @'Fun' ('ShowP' a) ('ShowP' b)@ from the start, so 'show' applies directly and
-- no coercion is involved. 'applyFunP' unwraps on the way back out.
newtype ShowP a = ShowP {forall a. ShowP a -> a
unShowP :: a}

instance (TestingEqShow a) => Show (ShowP a) where
  show :: ShowP a -> String
show (ShowP a
a) = a -> String
forall a. TestingEqShow a => a -> String
showP a
a

instance (Function a) => Function (ShowP a) where
  function :: forall b. Gen b -> Gen (ShowP a :-> b)
function = ((a :-> b) -> ShowP a :-> b)
-> Gen (a :-> b) -> Gen (ShowP a :-> b)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ShowP a -> a) -> (a -> ShowP a) -> (a :-> b) -> ShowP a :-> b
forall b a c. (b -> a) -> (a -> b) -> (a :-> c) -> b :-> c
ConcreteFun.map ShowP a -> a
forall a. ShowP a -> a
unShowP a -> ShowP a
forall a. a -> ShowP a
ShowP) (Gen (a :-> b) -> Gen (ShowP a :-> b))
-> (Gen b -> Gen (a :-> b)) -> Gen b -> Gen (ShowP a :-> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Gen b -> Gen (a :-> b)
forall b. Gen b -> Gen (a :-> b)
forall a b. Function a => Gen b -> Gen (a :-> b)
function

-- | Apply a generated function, wrapping and unwrapping the 'ShowP' it was
-- generated at. Both directions are ordinary newtype constructor applications.
applyFunP :: Fun (ShowP a) (ShowP b) -> a -> b
applyFunP :: forall a b. Fun (ShowP a) (ShowP b) -> a -> b
applyFunP Fun (ShowP a) (ShowP b)
f = ShowP b -> b
forall a. ShowP a -> a
unShowP (ShowP b -> b) -> (a -> ShowP b) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Fun (ShowP a) (ShowP b) -> ShowP a -> ShowP b
forall a b. Fun a b -> a -> b
applyFun Fun (ShowP a) (ShowP b)
f (ShowP a -> ShowP b) -> (a -> ShowP a) -> a -> ShowP b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a -> ShowP a
forall a. a -> ShowP a
ShowP

genP :: (TestableType a) => Property a
genP :: forall a. TestableType a => Property a
genP = case GenTotal a
forall a. TestableType a => GenTotal a
gen of
  GenNENonFun Gen a
g -> (a -> Maybe String) -> Gen a -> Property a
forall a e.
HasCallStack =>
(a -> Maybe String) -> Gen a -> Property' e a
genWith (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> (a -> String) -> a -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a -> String
forall a. TestingEqShow a => a -> String
showP) Gen a
g
  GenFun (a -> b) -> a
f Gen (Fun (ShowP a) (ShowP b))
g -> (a -> b) -> a
f ((a -> b) -> a)
-> (Fun (ShowP a) (ShowP b) -> a -> b)
-> Fun (ShowP a) (ShowP b)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Fun (ShowP a) (ShowP b) -> a -> b
forall a b. Fun (ShowP a) (ShowP b) -> a -> b
applyFunP (Fun (ShowP a) (ShowP b) -> a)
-> Property' String (Fun (ShowP a) (ShowP b)) -> Property a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> (Fun (ShowP a) (ShowP b) -> Maybe String)
-> Gen (Fun (ShowP a) (ShowP b))
-> Property' String (Fun (ShowP a) (ShowP b))
forall a e.
HasCallStack =>
(a -> Maybe String) -> Gen a -> Property' e a
genWith (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String)
-> (Fun (ShowP a) (ShowP b) -> String)
-> Fun (ShowP a) (ShowP b)
-> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Fun (ShowP a) (ShowP b) -> String
forall a. Show a => a -> String
show) Gen (Fun (ShowP a) (ShowP b))
g
  GenEmpty forall x. a -> x
_ -> Property a
forall e a. Property' e a
discard

genNamed :: (TestableType a) => String -> Property a
genNamed :: forall a. TestableType a => String -> Property a
genNamed String
nm = case GenTotal a
forall a. TestableType a => GenTotal a
gen of
  GenNENonFun Gen a
g -> String -> (a -> Maybe String) -> Gen a -> Property a
forall a. String -> (a -> Maybe String) -> Gen a -> Property a
genWithNamed String
nm (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> (a -> String) -> a -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a -> String
forall a. TestingEqShow a => a -> String
showP) Gen a
g
  GenFun (a -> b) -> a
f Gen (Fun (ShowP a) (ShowP b))
g -> (a -> b) -> a
f ((a -> b) -> a)
-> (Fun (ShowP a) (ShowP b) -> a -> b)
-> Fun (ShowP a) (ShowP b)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Fun (ShowP a) (ShowP b) -> a -> b
forall a b. Fun (ShowP a) (ShowP b) -> a -> b
applyFunP (Fun (ShowP a) (ShowP b) -> a)
-> Property' String (Fun (ShowP a) (ShowP b)) -> Property a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> String
-> (Fun (ShowP a) (ShowP b) -> Maybe String)
-> Gen (Fun (ShowP a) (ShowP b))
-> Property' String (Fun (ShowP a) (ShowP b))
forall a. String -> (a -> Maybe String) -> Gen a -> Property a
genWithNamed String
nm (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String)
-> (Fun (ShowP a) (ShowP b) -> String)
-> Fun (ShowP a) (ShowP b)
-> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Fun (ShowP a) (ShowP b) -> String
forall a. Show a => a -> String
show) Gen (Fun (ShowP a) (ShowP b))
g
  GenEmpty forall x. a -> x
_ -> Property a
forall e a. Property' e a
discard

-- | Fail with the message unless the condition holds. For the assertions a worked example makes,
-- which no generic law-checking property covers.
check :: String -> Bool -> Property ()
check :: String -> Bool -> Property ()
check String
msg Bool
ok = Bool -> Property () -> Property ()
forall (f :: Type -> Type). Applicative f => Bool -> f () -> f ()
unless Bool
ok (String -> Property ()
forall e a. e -> Property' e a
testFailed String
msg)

-- | Check a measured value against the expected one, showing both.
expect :: (Eq a, Show a) => String -> a -> a -> Property ()
expect :: forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect String
what a
want a
got = String -> Bool -> Property ()
check (String
what String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", found " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
got String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", expected " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
want) (a
got a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
want)

-- | 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.
testEq :: (TestingEqShow a) => String -> String -> a -> String -> a -> Property ()
testEq :: forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
nm String
sl a
l String
sr a
r = do
  isEq <- a -> a -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP a
l a
r
  unless isEq $
    testFailed $
      "Failed "
        ++ nm
        ++ ":\n"
        ++ sl
        ++ " = "
        ++ showP l
        ++ "\n"
        ++ sr
        ++ " = "
        ++ showP r

genWithNamed :: String -> (a -> Maybe String) -> Gen a -> Property a
genWithNamed :: forall a. String -> (a -> Maybe String) -> Gen a -> Property a
genWithNamed String
nm a -> Maybe String
f = (a -> Maybe String) -> Gen a -> Property' String a
forall a e.
HasCallStack =>
(a -> Maybe String) -> Gen a -> Property' e a
genWith (ShowS -> Maybe String -> Maybe String
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap ShowS
named (Maybe String -> Maybe String)
-> (a -> Maybe String) -> a -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a -> Maybe String
f)
  where
    named :: ShowS
named String
s = String
"for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
nm String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s

-- | 'True' if a type's generator is non-empty. A pure check on 'TestableType's '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.
isGenNonEmpty :: forall a. (TestableType a) => Bool
isGenNonEmpty :: forall a. TestableType a => Bool
isGenNonEmpty = case forall a. TestableType a => GenTotal a
gen @a of
  GenEmpty forall x. a -> x
_ -> Bool
False
  GenTotal a
_ -> Bool
True

-- | 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.
genSuchThat :: Gen key -> (key -> Bool) -> Gen key
genSuchThat :: forall key. Gen key -> (key -> Bool) -> Gen key
genSuchThat Gen key
genKey key -> Bool
isUsable = Int -> Gen key
go Int
maxTries
  where
    go :: Int -> Gen key
go Int
n = do
      k <- Gen key
genKey
      if isUsable k || n <= (0 :: Int) then pure k else go (n - 1)

-- | How many times 'genSuchThat' resamples before giving up. There is no principled formula
-- for this, since it depends on how sparse the requirement being searched for is, which
-- 'genSuchThat' cannot know in advance. 100 is comfortably more than the number of candidates a
-- small test object palette usually offers, so a single unlucky pick is very unlikely to exhaust
-- it. It is still cheap, since each attempt is a plain 'Gen' sample and not a 'Property'-level
-- 'discard'.
maxTries :: Int
maxTries :: Int
maxTries = Int
100

-- | 'genOb', but resampled (see 'genSuchThat') until @isUsable@ accepts the object.
genObSuchThat :: forall k. (Testable k) => (Some k -> Bool) -> Property (Some k)
genObSuchThat :: forall k. Testable k => (Some k -> Bool) -> Property (Some k)
genObSuchThat = Gen (Some k) -> (Some k -> Bool) -> Property (Some k)
forall k.
Testable k =>
Gen (Some k) -> (Some k -> Bool) -> Property (Some k)
genObSuchThatWith (forall k. Testable k => Gen (Some k)
genSome @k)

-- | 'genObSuchThat' with objects drawn from the given generator, e.g. 'genSomeSmall'.
genObSuchThatWith :: forall k. (Testable k) => Gen (Some k) -> (Some k -> Bool) -> Property (Some k)
genObSuchThatWith :: forall k.
Testable k =>
Gen (Some k) -> (Some k -> Bool) -> Property (Some k)
genObSuchThatWith Gen (Some k)
objects = (Some k -> Maybe String) -> Gen (Some k) -> Property (Some k)
forall a e.
HasCallStack =>
(a -> Maybe String) -> Gen a -> Property' e a
genWith (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String)
-> (Some k -> String) -> Some k -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Some k -> String
forall a. Show a => a -> String
show) (Gen (Some k) -> Property (Some k))
-> ((Some k -> Bool) -> Gen (Some k))
-> (Some k -> Bool)
-> Property (Some k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Gen (Some k) -> (Some k -> Bool) -> Gen (Some k)
forall key. Gen key -> (key -> Bool) -> Gen key
genSuchThat Gen (Some k)
objects

type SomeProfunctorElt :: (j +-> k) -> Type
data SomeProfunctorElt p where
  SomeP :: (TestOb a, TestOb b) => p a b -> SomeProfunctorElt p

someP :: forall {k} {j} (p :: k +-> j) a b. (Profunctor p, TestObIsOb j, TestObIsOb k) => p a b -> SomeProfunctorElt p
someP :: forall {k} {j} (p :: k +-> j) (a :: j) (b :: k).
(Profunctor p, TestObIsOb j, TestObIsOb k) =>
p a b -> SomeProfunctorElt p
someP p a b
p = forall (a :: j) r. (TestObIsOb j, Ob a) => (TestOb a => r) -> r
forall {k} (a :: k) r. (TestObIsOb k, Ob a) => (TestOb a => r) -> r
testObFromOb @a (forall (a :: k) r. (TestObIsOb k, Ob a) => (TestOb a => r) -> r
forall {k} (a :: k) r. (TestObIsOb k, Ob a) => (TestOb a => r) -> r
testObFromOb @b (p a b -> SomeProfunctorElt p
forall {k} {j} (a :: k) (b :: j) (p :: j +-> k).
(TestOb a, TestOb b) =>
p a b -> SomeProfunctorElt p
SomeP p a b
p)) ((Ob a, Ob b) => SomeProfunctorElt p)
-> p a b -> SomeProfunctorElt p
forall (a :: j) (b :: k) r. ((Ob a, Ob b) => r) -> p a b -> r
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j) r.
Profunctor p =>
((Ob a, Ob b) => r) -> p a b -> r
\\ p a b
p

instance
  (forall a b. (TestOb (a :: k), TestOb (b :: j)) => TestingEqShow (p a b), Testable k, Testable j)
  => Show (SomeProfunctorElt p)
  where
  show :: SomeProfunctorElt p -> String
show (SomeP @a @b p a b
p) = p a b -> String
forall a. TestingEqShow a => a -> String
showP p a b
p String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" @" String -> ShowS
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @a String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" @" String -> ShowS
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @j @b

type TestableTypeP :: (j +-> k) -> Constraint
class (forall a b. (TestOb (a :: k), TestOb (b :: j)) => TestableType (p a b)) => TestableTypeP (p :: j +-> k)
instance (forall a b. (TestOb (a :: k), TestOb (b :: j)) => TestableType (p a b)) => TestableTypeP (p :: j +-> k)

type TestableProfunctor :: forall {j} {k}. j +-> k -> Constraint
class
  (Testable j, Testable k, Profunctor p, forall a b. (TestOb (a :: k), TestOb (b :: j)) => TestingEqShow (p a b)) =>
  TestableProfunctor (p :: j +-> k)
  where
  -- | 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@.
  genProfunctorElt :: String -> Property (SomeProfunctorElt p)
  default genProfunctorElt :: (TestableTypeP p) => String -> Property (SomeProfunctorElt p)
  genProfunctorElt String
nm = do
    Some @a <- forall k. Testable k => Property (Some k)
genOb @k
    Some @b <- genObSuchThat @j \(Some @b') -> forall a. TestableType a => Bool
isGenNonEmpty @(p a b')
    p <- genNamed @(p a b) nm
    pure $ SomeP p

-- | A kind whose objects can be enumerated and displayed.
class (TestableProfunctor (Hom k), TestableTypeP (Hom k), CategoryOf k) => Testable k where
  type TestOb (a :: k) :: GHC.Constraint
  type TestOb a = Ob a
  showOb :: forall (a :: k). (TestOb a) => String
  genSome :: Gen (Some k)
  obFromTestOb :: forall (a :: k) r. (TestOb a) => ((Ob a) => r) -> r
  default obFromTestOb :: forall (a :: k) r. (TestOb a ~ Ob a, TestOb a) => ((Ob a) => r) -> r
  obFromTestOb Ob a => r
r = r
Ob a => r
r

  -- | 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
  -- 'Proarrow.Testing.Laws.testClosed' to terminate.
  --
  -- An instance that wraps another kind's palette must forward this too, as the wrapper instances
  -- below do, or 'Proarrow.Testing.Laws.testClosed' silently gets the wide one.
  genSomeSmall :: Gen (Some k)
  genSomeSmall = Gen (Some k)
forall k. Testable k => Gen (Some k)
genSome

  {-# MINIMAL showOb, genSome #-}

genOb :: (Testable k) => Property (Some k)
genOb :: forall k. Testable k => Property (Some k)
genOb = (Some k -> Maybe String)
-> Gen (Some k) -> Property' String (Some k)
forall a e.
HasCallStack =>
(a -> Maybe String) -> Gen a -> Property' e a
genWith (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String)
-> (Some k -> String) -> Some k -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Some k -> String
forall a. Show a => a -> String
show) Gen (Some k)
forall k. Testable k => Gen (Some k)
genSome

-- | 'genOb' from the small palette. See 'genSomeSmall'.
genObSmall :: (Testable k) => Property (Some k)
genObSmall :: forall k. Testable k => Property (Some k)
genObSmall = (Some k -> Maybe String)
-> Gen (Some k) -> Property' String (Some k)
forall a e.
HasCallStack =>
(a -> Maybe String) -> Gen a -> Property' e a
genWith (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String)
-> (Some k -> String) -> Some k -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Some k -> String
forall a. Show a => a -> String
show) Gen (Some k)
forall k. Testable k => Gen (Some k)
genSomeSmall

instance (TestableProfunctor p) => TestableProfunctor (Op p) where
  genProfunctorElt :: String -> Property (SomeProfunctorElt (Op p))
genProfunctorElt String
nm = do
    SomeP p <- forall {j} {k} (p :: j +-> k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
forall (p :: j +-> k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
genProfunctorElt @p String
nm
    pure $ SomeP (Op p)
instance (Testable k) => Testable (OPPOSITE k) where
  type TestOb a = (Is OP a, TestOb (UN OP a))
  obFromTestOb :: forall (a :: OPPOSITE k) r. TestOb a => (Ob a => r) -> r
obFromTestOb @(OP a) Ob a => r
r = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a r
Ob (UN OP a) => r
Ob a => r
r
  showOb :: forall (a :: OPPOSITE k). TestOb a => String
showOb @(OP a) = String
"OP (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @a String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
  genSome :: Gen (Some (OPPOSITE k))
genSome = mapSome OP (Some k -> Some (OPPOSITE k))
-> Gen (Some k) -> Gen (Some (OPPOSITE k))
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Some k)
forall k. Testable k => Gen (Some k)
genSome
  genSomeSmall :: Gen (Some (OPPOSITE k))
genSomeSmall = mapSome OP (Some k -> Some (OPPOSITE k))
-> Gen (Some k) -> Gen (Some (OPPOSITE k))
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Some k)
forall k. Testable k => Gen (Some k)
genSomeSmall

-- | The 'PROD' wrapper changes only which tensor a kind carries, so everything transports across it.
instance (TestableProfunctor p) => TestableProfunctor (Prod p) where
  genProfunctorElt :: String -> Property (SomeProfunctorElt (Prod p))
genProfunctorElt String
nm = do
    SomeP p <- forall {j} {k} (p :: j +-> k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
forall (p :: j +-> k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
genProfunctorElt @p String
nm
    pure $ SomeP (Prod p)

instance (Testable k) => Testable (PROD k) where
  type TestOb a = (Is PR a, TestOb (UN PR a))
  obFromTestOb :: forall (a :: PROD k) r. TestOb a => (Ob a => r) -> r
obFromTestOb @(PR a) Ob a => r
r = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a r
Ob (UN PR a) => r
Ob a => r
r
  showOb :: forall (a :: PROD k). TestOb a => String
showOb @(PR a) = String
"PR (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @a String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
  genSome :: Gen (Some (PROD k))
genSome = mapSome PR (Some k -> Some (PROD k)) -> Gen (Some k) -> Gen (Some (PROD k))
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Some k)
forall k. Testable k => Gen (Some k)
genSome
  genSomeSmall :: Gen (Some (PROD k))
genSomeSmall = mapSome PR (Some k -> Some (PROD k)) -> Gen (Some k) -> Gen (Some (PROD k))
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Some k)
forall k. Testable k => Gen (Some k)
genSomeSmall

instance TestableProfunctor Unit
instance Testable () where
  showOb :: forall (a :: ()). TestOb a => String
showOb = String
"()"
  genSome :: Gen (Some ())
genSome = Some () -> Gen (Some ())
forall a. a -> Gen a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall {k} {c :: OB k} (x :: k). c x => SomeOf c
forall (x :: ()). TestOb' x => Some ()
Some @'())

instance (TestableProfunctor p, TestableProfunctor q) => TestableProfunctor (p :**: q) where
  genProfunctorElt :: String -> Property (SomeProfunctorElt (p :**: q))
genProfunctorElt String
nm = do
    SomeP p <- forall {j} {k} (p :: j +-> k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
forall (p :: j1 +-> k1).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
genProfunctorElt @p (String
nm String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"_0")
    SomeP q <- genProfunctorElt @q (nm ++ "_1")
    pure (SomeP (p :**: q) \\ p \\ q)
instance (Testable j, Testable k) => Testable (j, k) where
  type TestOb a = (a ~ '(Fst @ a, Snd @ a), TestOb (Fst @ a), TestOb (Snd @ a))
  obFromTestOb :: forall (a :: (j, k)) r. TestOb a => (Ob a => r) -> r
obFromTestOb @'(a, b) Ob a => r
r = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a ((Ob (Fst @ a) => r) -> r) -> (Ob (Fst @ a) => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b r
Ob (Snd @ a) => r
Ob a => r
r
  showOb :: forall (a :: (j, k)). TestOb a => String
showOb @'(a, b) = String
"(" String -> ShowS
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @j @a String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", " String -> ShowS
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @b String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
  genSome :: Gen (Some (j, k))
genSome = do
    Some @a <- forall k. Testable k => Gen (Some k)
genSome @j
    Some @b <- genSome @k
    pure $ obFromTestOb @_ @a $ obFromTestOb @_ @b $ Some @'(a, b)
  genSomeSmall :: Gen (Some (j, k))
genSomeSmall = do
    Some @a <- forall k. Testable k => Gen (Some k)
genSomeSmall @j
    Some @b <- genSomeSmall @k
    pure $ obFromTestOb @_ @a $ obFromTestOb @_ @b $ Some @'(a, b)

class (TestOb a) => TestOb' a
instance (TestOb a) => TestOb' a

class (Testable k, forall (a :: k). (Ob a) => TestOb' a) => TestObIsOb k
instance (Testable k, forall (a :: k). (Ob a) => TestOb' a) => TestObIsOb k

-- | Recover @'TestOb' a@ from @'Ob' a@ where 'TestObIsOb' provides it: the converse of
-- 'obFromTestOb @_', a function for the same reason.
testObFromOb :: forall {k} (a :: k) r. (TestObIsOb k, Ob a) => ((TestOb a) => r) -> r
testObFromOb :: forall {k} (a :: k) r. (TestObIsOb k, Ob a) => (TestOb a => r) -> r
testObFromOb TestOb a => r
r = r
TestOb a => r
r

-- * Objecthood witnesses

-- | 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 WithTestOb k = forall (a :: k) r. (Ob a) => ((TestOb a) => r) -> r

-- | @'TestOb'@ is closed under the tensor.
type WithTestOb2 k = forall (a :: k) b r. (TestOb a, TestOb b) => ((TestOb (a M.** b)) => r) -> r

withTestOb2Def :: forall {k}. (TestObIsOb k, M.Monoidal k) => WithTestOb2 k
withTestOb2Def :: forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b TestOb (a ** b) => r
r = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a ((Ob a => r) -> r) -> (Ob a => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b ((Ob b => r) -> r) -> (Ob b => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
M.withOb2 @_ @a @b r
Ob (a ** b) => r
TestOb (a ** b) => r
r

-- | @'TestOb'@ is closed under the binary product.
type WithTestObProd k = forall (a :: k) b r. (TestOb a, TestOb b) => ((TestOb (a BinaryProduct.&& b)) => r) -> r

withTestObProdDef :: forall {k}. (TestObIsOb k, BinaryProduct.HasBinaryProducts k) => WithTestObProd k
withTestObProdDef :: forall {k}. (TestObIsOb k, HasBinaryProducts k) => WithTestObProd k
withTestObProdDef @a @b TestOb (a && b) => r
r = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a ((Ob a => r) -> r) -> (Ob a => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b ((Ob b => r) -> r) -> (Ob b => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) (b :: k) r.
(HasBinaryProducts k, Ob a, Ob b) =>
(Ob (a && b) => r) -> r
BinaryProduct.withObProd @_ @a @b r
Ob (a && b) => r
TestOb (a && b) => r
r

-- | @'TestOb'@ is closed under the binary coproduct.
type WithTestObCoprod k = forall (a :: k) b r. (TestOb a, TestOb b) => ((TestOb (a BinaryCoproduct.|| b)) => r) -> r

withTestObCoprodDef :: forall {k}. (TestObIsOb k, BinaryCoproduct.HasBinaryCoproducts k) => WithTestObCoprod k
withTestObCoprodDef :: forall {k}.
(TestObIsOb k, HasBinaryCoproducts k) =>
WithTestObCoprod k
withTestObCoprodDef @a @b TestOb (a || b) => r
r = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a ((Ob a => r) -> r) -> (Ob a => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b ((Ob b => r) -> r) -> (Ob b => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) (b :: k) r.
(HasBinaryCoproducts k, Ob a, Ob b) =>
(Ob (a || b) => r) -> r
BinaryCoproduct.withObCoprod @_ @a @b r
Ob (a || b) => r
TestOb (a || b) => r
r

-- | @'TestOb'@ is closed under the internal hom.
type WithTestObExp k = forall (a :: k) b r. (TestOb a, TestOb b) => ((TestOb (a Exponential.~~> b)) => r) -> r

withTestObExpDef :: forall {k}. (TestObIsOb k, Exponential.Closed k) => WithTestObExp k
withTestObExpDef :: forall {k}. (TestObIsOb k, Closed k) => WithTestObExp k
withTestObExpDef @a @b TestOb (a ~~> b) => r
r = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a ((Ob a => r) -> r) -> (Ob a => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b ((Ob b => r) -> r) -> (Ob b => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) (b :: k) r.
(Closed k, Ob a, Ob b) =>
(Ob (a ~~> b) => r) -> r
Exponential.withObExp @_ @a @b r
Ob (a ~~> b) => r
TestOb (a ~~> b) => r
r

-- | @'TestOb'@ is closed under dualization.
type WithTestObDual k = forall (a :: k) r. (TestOb a) => ((TestOb (SA.Dual a)) => r) -> r

withTestObDualDef :: forall {k}. (TestObIsOb k, SA.Dialogue k) => WithTestObDual k
withTestObDualDef :: forall {k}. (TestObIsOb k, Dialogue k) => WithTestObDual k
withTestObDualDef @a TestOb (Dual a) => r
r = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a ((Ob a => r) -> r) -> (Ob a => r) -> r
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. (Dialogue k, Ob a) => (Ob (Dual a) => r) -> r
SA.withObDual @_ @a r
Ob (Dual a) => r
TestOb (Dual a) => r
r

-- | @'TestOb'@ is closed under a representable profunctor.
type WithTestObRep k p = forall (a :: k) r. (TestOb a) => ((TestOb (p % a)) => r) -> r

-- | @'TestOb'@ is closed under a corepresentable profunctor.
type WithTestObCorep k p = forall (a :: k) r. (TestOb a) => ((TestOb (p %% a)) => r) -> r

-- | A testable object of @k@.
type Some :: Type -> Type
type Some k = SomeOf (TestOb' :: OB k)

mapSome :: forall {j} {k}. forall (f :: j -> k) -> (forall a. (TestOb a) => TestOb' (f a)) => Some j -> Some k
mapSome :: forall {j} {k}.
forall (f :: j -> k) ->
(forall (a :: j). TestOb a => TestOb' (f a)) => Some j -> Some k
mapSome f (Some @a) = forall (x :: k). TestOb' x => SomeOf TestOb'
forall {k} {c :: OB k} (x :: k). c x => SomeOf c
Some @(f a)

-- | The objects of a type-level list.
mkSomeList :: forall {k} (as :: [k]). (KnownListOf TestOb' as) => [Some k]
mkSomeList :: forall {k} (as :: [k]). KnownListOf TestOb' as => [Some k]
mkSomeList = ListOf TestOb' as -> [SomeOf TestOb']
forall {k} (c :: OB k) (xs :: [k]). ListOf c xs -> [SomeOf c]
someOfList (forall {k} (c :: OB k) (xs :: [k]). KnownListOf c xs => ListOf c xs
forall (c :: OB k) (xs :: [k]). KnownListOf c xs => ListOf c xs
listOf @TestOb' @as)

instance (Testable k) => Show (Some k) where
  show :: Some k -> String
show (Some @a) = forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @a

someElem :: (Show a) => [a] -> Property a
someElem :: forall a. Show a => [a] -> Property a
someElem = (a -> String) -> [a] -> Property a
forall a. (a -> String) -> [a] -> Property a
someElemWith a -> String
forall a. Show a => a -> String
show

someElemNamed :: (Show a) => String -> [a] -> Property a
someElemNamed :: forall a. Show a => String -> [a] -> Property a
someElemNamed String
nm = (a -> String) -> [a] -> Property a
forall a. (a -> String) -> [a] -> Property a
someElemWith (\a
a -> String
"for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
nm String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
a)

someElemWith :: (a -> String) -> [a] -> Property a
someElemWith :: forall a. (a -> String) -> [a] -> Property a
someElemWith a -> String
_ [] = Property' String a
forall e a. Property' e a
discard
someElemWith a -> String
f (a
x : [a]
xs) = (a -> Maybe String) -> Gen a -> Property' String a
forall a e.
HasCallStack =>
(a -> Maybe String) -> Gen a -> Property' e a
genWith (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> (a -> String) -> a -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a -> String
f) (NonEmpty a -> Gen a
forall a. NonEmpty a -> Gen a
elem (a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
xs))

genSomeDef :: forall {k} (obs :: [k]). (Testable k, KnownListOf TestOb' obs) => Gen (Some k)
genSomeDef :: forall {k} (obs :: [k]).
(Testable k, KnownListOf TestOb' obs) =>
Gen (Some k)
genSomeDef = String -> [Some k] -> Gen (Some k)
forall k. String -> [Some k] -> Gen (Some k)
genSomeList String
"the palette is empty" (forall (as :: [k]). KnownListOf TestOb' as => [Some k]
forall {k} (as :: [k]). KnownListOf TestOb' as => [Some k]
mkSomeList @obs)

-- | The palette of a category that already knows its own objects: @'Proarrow.Category.Enriched.Thin.Objects' k@
-- is the list 'genSomeDef' 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.
genSomeFinite :: forall k. (Enumerable k, TestObIsOb k) => Gen (Some k)
genSomeFinite :: forall k. (Enumerable k, TestObIsOb k) => Gen (Some k)
genSomeFinite = String -> [Some k] -> Gen (Some k)
forall k. String -> [Some k] -> Gen (Some k)
genSomeList String
"the category has no objects" (forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
foreachOb @k \ @a -> [forall (x :: k). TestOb' x => Some k
forall {k} {c :: OB k} (x :: k). c x => SomeOf c
Some @a])

genSomeList :: String -> [Some k] -> Gen (Some k)
genSomeList :: forall k. String -> [Some k] -> Gen (Some k)
genSomeList String
what [] = String -> Gen (Some k)
forall a. HasCallStack => String -> a
error (String
"genSome: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
what)
genSomeList String
_ (Some k
x : [Some k]
xs) = NonEmpty (Some k) -> Gen (Some k)
forall a. NonEmpty a -> Gen a
elem (Some k
x Some k -> [Some k] -> NonEmpty (Some k)
forall a. a -> [a] -> NonEmpty a
:| [Some k]
xs)

-- | 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.
genBoth
  :: forall a b c. (TestableType a, TestableType b) => (a -> b -> c) -> (c -> a) -> (c -> b) -> GenTotal c
genBoth :: forall a b c.
(TestableType a, TestableType b) =>
(a -> b -> c) -> (c -> a) -> (c -> b) -> GenTotal c
genBoth a -> b -> c
mk c -> a
outl c -> b
outr = case (forall a. TestableType a => GenTotal a
gen @a, forall a. TestableType a => GenTotal a
gen @b) of
  (GenEmpty forall x. a -> x
f, GenTotal b
_) -> (forall x. c -> x) -> GenTotal c
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty (\c
c -> a -> x
forall x. a -> x
f (c -> a
outl c
c))
  (GenTotal a
_, GenEmpty forall x. b -> x
g) -> (forall x. c -> x) -> GenTotal c
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty (\c
c -> b -> x
forall x. b -> x
g (c -> b
outr c
c))
  (GenNonEmpty Gen a
ga, GenNonEmpty Gen b
gb) -> Gen c -> GenTotal c
forall a. Gen a -> GenTotal a
GenNonEmpty ((a -> b -> c) -> Gen a -> Gen b -> Gen c
forall a b c. (a -> b -> c) -> Gen a -> Gen b -> Gen c
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
mk Gen a
ga Gen b
gb)

optGen :: [a] -> GenTotal a
optGen :: forall a. [a] -> GenTotal a
optGen [] = String -> GenTotal a
forall a. HasCallStack => String -> a
error String
"optGen: empty list"
optGen (a
x : [a]
xs) = Gen a -> GenTotal a
forall a. Gen a -> GenTotal a
GenNonEmpty (NonEmpty a -> Gen a
forall a. NonEmpty a -> Gen a
elem (a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
xs))

-- | 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. 'Proarrow.Testing.Laws.testFinitary'
-- says why a generator that /is/ the enumeration makes the round-trip law vacuous.
genElements :: forall {j} {k} (p :: j +-> k) (a :: k) (b :: j). (Finitary p, Ob a, Ob b) => GenTotal (p a b)
genElements :: forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements = case forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
[p a b]
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
[p a b]
elements @p @a @b of
  [] -> (forall x. p a b -> x) -> GenTotal (p a b)
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty \p a b
_ -> String -> x
forall a. HasCallStack => String -> a
error String
"genElements: no elements at these objects"
  [p a b]
xs -> [p a b] -> GenTotal (p a b)
forall a. [a] -> GenTotal a
optGen [p a b]
xs

oneElem :: a -> GenTotal a
oneElem :: forall a. a -> GenTotal a
oneElem a
x = Gen a -> GenTotal a
forall a. Gen a -> GenTotal a
GenNonEmpty (a -> Gen a
forall a. a -> Gen a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure a
x)

instance (TestableType a, TestingEqShow b) => TestingEqShow (a -> b) where
  eqP :: (a -> b) -> (a -> b) -> Property Bool
eqP = (a -> b) -> (a -> b) -> Property Bool
forall a b.
(TestableType a, TestingEqShow b) =>
(a -> b) -> (a -> b) -> Property Bool
eqHask
  showP :: (a -> b) -> String
showP a -> b
_ = String
"<function>"

instance (Function a, TestableType a, TestableType b) => TestableType (a -> b) where
  gen :: GenTotal (a -> b)
gen = case forall a. TestableType a => GenTotal a
gen @b of
    GenEmpty forall x. b -> x
absurd -> case forall a. TestableType a => GenTotal a
gen @a of
      GenEmpty forall x. a -> x
absurda -> (a -> b) -> GenTotal (a -> b)
forall a. a -> GenTotal a
oneElem a -> b
forall x. a -> x
absurda
      GenNonEmpty Gen a
g -> (forall x. (a -> b) -> x) -> GenTotal (a -> b)
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty \a -> b
ab -> b -> x
forall x. b -> x
absurd (a -> b
ab (Gen a -> a
forall a. Gen a -> a
minimalValue Gen a
g))
    GenNonEmpty Gen b
gb -> ((a -> b) -> a -> b)
-> Gen (Fun (ShowP a) (ShowP b)) -> GenTotal (a -> b)
forall a b p.
(TestableType a, TestableType b) =>
((a -> b) -> p) -> Gen (Fun (ShowP a) (ShowP b)) -> GenTotal p
GenFun (a -> b) -> a -> b
forall a. Ob a => a -> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id (Gen (ShowP b) -> Gen (Fun (ShowP a) (ShowP b))
forall a b. Function a => Gen b -> Gen (Fun a b)
fun (b -> ShowP b
forall a. a -> ShowP a
ShowP (b -> ShowP b) -> Gen b -> Gen (ShowP b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen b
gb))

eqHask :: (TestableType a, TestingEqShow b) => (a -> b) -> (a -> b) -> Property Bool
eqHask :: forall a b.
(TestableType a, TestingEqShow b) =>
(a -> b) -> (a -> b) -> Property Bool
eqHask a -> b
l a -> b
r =
  case GenTotal a
forall a. TestableType a => GenTotal a
gen of
    GenEmpty forall x. a -> x
_ -> Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
True -- There can only be one function of a type with no values
    GenNonEmpty Gen a
ga -> do
      a <- (a -> Maybe String) -> Gen a -> Property' String a
forall a e.
HasCallStack =>
(a -> Maybe String) -> Gen a -> Property' e a
genWith (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> (a -> String) -> a -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. a -> String
forall a. TestingEqShow a => a -> String
showP) Gen a
ga
      eqP (l a) (r a)

instance (TestableType (p a b)) => TestableType (Prod p (PR a) (PR b)) where
  gen :: GenTotal (Prod p (PR a) (PR b))
gen = (p a b -> Prod p (PR a) (PR b))
-> (Prod p (PR a) (PR b) -> p a b)
-> GenTotal (p a b)
-> GenTotal (Prod p (PR a) (PR b))
forall a b. (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap p a b -> Prod p (PR a) (PR b)
forall {j} {k} (p :: j +-> k) (a1 :: k) (b1 :: j).
p a1 b1 -> Prod p (PR a1) (PR b1)
Prod Prod p (PR a) (PR b) -> p a b
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
Prod p (PR a) (PR b) -> p a b
unProd GenTotal (p a b)
forall a. TestableType a => GenTotal a
gen
instance (TestingEqShow (p a b)) => TestingEqShow (Prod p (PR a) (PR b)) where
  eqP :: Prod p (PR a) (PR b) -> Prod p (PR a) (PR b) -> Property Bool
eqP (Prod p a1 b1
l) (Prod p a1 b1
r) = p a1 b1 -> p a1 b1 -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP p a1 b1
l p a1 b1
p a1 b1
r
  showP :: Prod p (PR a) (PR b) -> String
showP (Prod p a1 b1
p) = String
"Prod (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ p a1 b1 -> String
forall a. TestingEqShow a => a -> String
showP p a1 b1
p String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"

instance (TestableType (p b a)) => TestableType (Op p (OP a) (OP b)) where
  gen :: GenTotal (Op p (OP a) (OP b))
gen = (p b a -> Op p (OP a) (OP b))
-> (Op p (OP a) (OP b) -> p b a)
-> GenTotal (p b a)
-> GenTotal (Op p (OP a) (OP b))
forall a b. (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap p b a -> Op p (OP a) (OP b)
forall {j} {k} (p :: j +-> k) (b1 :: k) (a1 :: j).
p b1 a1 -> Op p (OP a1) (OP b1)
Op Op p (OP a) (OP b) -> p b a
forall {j} {k} (p :: j +-> k) (b :: k) (a :: j).
Op p (OP a) (OP b) -> p b a
unOp GenTotal (p b a)
forall a. TestableType a => GenTotal a
gen
instance (TestingEqShow (p b a)) => TestingEqShow (Op p (OP a) (OP b)) where
  eqP :: Op p (OP a) (OP b) -> Op p (OP a) (OP b) -> Property Bool
eqP (Op p b1 a1
l) (Op p b1 a1
r) = p b1 a1 -> p b1 a1 -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP p b1 a1
l p b1 a1
p b1 a1
r
  showP :: Op p (OP a) (OP b) -> String
showP (Op p b1 a1
p) = String
"Op (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ p b1 a1 -> String
forall a. TestingEqShow a => a -> String
showP p b1 a1
p String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"

-- | The elements of 'Star' and 'Costar' are arrows, and are compared, shown and drawn as those.
instance (TestingEqShow (a ~> f b)) => TestingEqShow (Star f a b) where
  eqP :: Star f a b -> Star f a b -> Property Bool
eqP (Star a ~> f b
l) (Star a ~> f b
r) = (a ~> f b) -> (a ~> f b) -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP a ~> f b
l a ~> f b
r
  showP :: Star f a b -> String
showP (Star a ~> f b
f) = (a ~> f b) -> String
forall a. TestingEqShow a => a -> String
showP a ~> f b
f

instance (Ob b, TestableType (a ~> f b)) => TestableType (Star f a b) where
  gen :: GenTotal (Star f a b)
gen = ((a ~> f b) -> Star f a b)
-> (Star f a b -> a ~> f b)
-> GenTotal (a ~> f b)
-> GenTotal (Star f a b)
forall a b. (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap (a ~> f b) -> Star f a b
forall {j} {k} (b :: j) (a :: k) (f :: j -> k).
Ob b =>
(a ~> f b) -> Star f a b
Star (\(Star a ~> f b
f) -> a ~> f b
f) GenTotal (a ~> f b)
forall a. TestableType a => GenTotal a
gen

instance (TestingEqShow (f a ~> b)) => TestingEqShow (Costar f a b) where
  eqP :: Costar f a b -> Costar f a b -> Property Bool
eqP (Costar f a ~> b
l) (Costar f a ~> b
r) = (f a ~> b) -> (f a ~> b) -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP f a ~> b
l f a ~> b
r
  showP :: Costar f a b -> String
showP (Costar f a ~> b
f) = (f a ~> b) -> String
forall a. TestingEqShow a => a -> String
showP f a ~> b
f

instance (Ob a, TestableType (f a ~> b)) => TestableType (Costar f a b) where
  gen :: GenTotal (Costar f a b)
gen = ((f a ~> b) -> Costar f a b)
-> (Costar f a b -> f a ~> b)
-> GenTotal (f a ~> b)
-> GenTotal (Costar f a b)
forall a b. (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap (f a ~> b) -> Costar f a b
forall {j} {k} (a :: j) (f :: j -> k) (b :: k).
Ob a =>
(f a ~> b) -> Costar f a b
Costar (\(Costar f a ~> b
f) -> f a ~> b
f) GenTotal (f a ~> b)
forall a. TestableType a => GenTotal a
gen

instance (TestableType (a ~> (f Rep.@ b)), Ob b) => TestableType (Rep f a b) where
  gen :: GenTotal (Rep f a b)
gen = ((a ~> (f @ b)) -> Rep f a b)
-> (Rep f a b -> a ~> (f @ b))
-> GenTotal (a ~> (f @ b))
-> GenTotal (Rep f a b)
forall a b. (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap (a ~> (f @ b)) -> Rep f a b
forall {j} {k} (b :: j) (f :: j +-> k) (a :: k).
Ob b =>
(a ~> (f @ b)) -> Rep f a b
Rep Rep f a b -> a ~> (f @ b)
forall {j} {k} (b :: j) (f :: j +-> k) (a :: k).
Rep f a b -> a ~> (f @ b)
unRep (forall a. TestableType a => GenTotal a
gen @(a ~> f Rep.@ b))
instance (TestingEqShow (a ~> (f Rep.@ b)), Ob b) => TestingEqShow (Rep f a b) where
  eqP :: Rep f a b -> Rep f a b -> Property Bool
eqP (Rep a ~> (f @ b)
l) (Rep a ~> (f @ b)
r) = (a ~> (f @ b)) -> (a ~> (f @ b)) -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP a ~> (f @ b)
l a ~> (f @ b)
r
  showP :: Rep f a b -> String
showP (Rep a ~> (f @ b)
p) = (a ~> (f @ b)) -> String
forall a. TestingEqShow a => a -> String
showP a ~> (f @ b)
p

instance (TestableType ((f Rep.@ a) ~> b), Ob a) => TestableType (Corep f a b) where
  gen :: GenTotal (Corep f a b)
gen = (((f @ a) ~> b) -> Corep f a b)
-> (Corep f a b -> (f @ a) ~> b)
-> GenTotal ((f @ a) ~> b)
-> GenTotal (Corep f a b)
forall a b. (a -> b) -> (b -> a) -> GenTotal a -> GenTotal b
invmap ((f @ a) ~> b) -> Corep f a b
forall {j} {k} (a :: j) (f :: j +-> k) (b :: k).
Ob a =>
((f @ a) ~> b) -> Corep f a b
Corep Corep f a b -> (f @ a) ~> b
forall {j} {k} (a :: j) (f :: j +-> k) (b :: k).
Corep f a b -> (f @ a) ~> b
unCorep (forall a. TestableType a => GenTotal a
gen @(f Rep.@ a ~> b))
instance (TestingEqShow ((f Rep.@ a) ~> b), Ob a) => TestingEqShow (Corep f a b) where
  eqP :: Corep f a b -> Corep f a b -> Property Bool
eqP (Corep (f @ a) ~> b
l) (Corep (f @ a) ~> b
r) = ((f @ a) ~> b) -> ((f @ a) ~> b) -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP (f @ a) ~> b
l (f @ a) ~> b
r
  showP :: Corep f a b -> String
showP (Corep (f @ a) ~> b
p) = ((f @ a) ~> b) -> String
forall a. TestingEqShow a => a -> String
showP (f @ a) ~> b
p

instance (TestingEqShow (catk a1 b1), TestingEqShow (catj a2 b2)) => TestingEqShow ((catk :**: catj) '(a1, a2) '(b1, b2)) where
  eqP :: (:**:) catk catj '(a1, a2) '(b1, b2)
-> (:**:) catk catj '(a1, a2) '(b1, b2) -> Property Bool
eqP (catk a1 b1
l1 :**: catj a2 b2
l2) (catk a1 b1
r1 :**: catj a2 b2
r2) = (Bool -> Bool -> Bool)
-> Property Bool -> Property Bool -> Property Bool
forall a b c.
(a -> b -> c)
-> Property' String a -> Property' String b -> Property' String c
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(&&) (catk a1 b1 -> catk a1 b1 -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP catk a1 b1
l1 catk a1 b1
catk a1 b1
r1) (catj a2 b2 -> catj a2 b2 -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP catj a2 b2
l2 catj a2 b2
catj a2 b2
r2)
  showP :: (:**:) catk catj '(a1, a2) '(b1, b2) -> String
showP (catk a1 b1
l1 :**: catj a2 b2
l2) = String
"(" String -> ShowS
forall a. [a] -> [a] -> [a]
++ catk a1 b1 -> String
forall a. TestingEqShow a => a -> String
showP catk a1 b1
l1 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
") :**: (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ catj a2 b2 -> String
forall a. TestingEqShow a => a -> String
showP catj a2 b2
l2 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
instance (TestableType (catk a1 b1), TestableType (catj a2 b2)) => TestableType ((catk :**: catj) '(a1, a2) '(b1, b2)) where
  gen :: GenTotal ((:**:) catk catj '(a1, a2) '(b1, b2))
gen = (catk a1 b1 -> catj a2 b2 -> (:**:) catk catj '(a1, a2) '(b1, b2))
-> ((:**:) catk catj '(a1, a2) '(b1, b2) -> catk a1 b1)
-> ((:**:) catk catj '(a1, a2) '(b1, b2) -> catj a2 b2)
-> GenTotal ((:**:) catk catj '(a1, a2) '(b1, b2))
forall a b c.
(TestableType a, TestableType b) =>
(a -> b -> c) -> (c -> a) -> (c -> b) -> GenTotal c
genBoth catk a1 b1 -> catj a2 b2 -> (:**:) catk catj '(a1, a2) '(b1, b2)
forall {j1} {k1} {j2} {k2} (c :: j1 +-> k1) (a1 :: k1) (b1 :: j1)
       (d :: j2 +-> k2) (a2 :: k2) (b2 :: j2).
c a1 b1 -> d a2 b2 -> (:**:) c d '(a1, a2) '(b1, b2)
(:**:) (:**:) catk catj '(a1, a2) '(b1, b2) -> catk a1 b1
forall {j1} {k1} {j2} {k2} (c :: j1 +-> k1) (a1 :: k1) (b1 :: j1)
       (d :: j2 +-> k2) (a2 :: k2) (b2 :: j2).
(:**:) c d '(a1, a2) '(b1, b2) -> c a1 b1
fstK (:**:) catk catj '(a1, a2) '(b1, b2) -> catj a2 b2
forall {j1} {k1} {j2} {k2} (c :: j1 +-> k1) (a1 :: k1) (b1 :: j1)
       (d :: j2 +-> k2) (a2 :: k2) (b2 :: j2).
(:**:) c d '(a1, a2) '(b1, b2) -> d a2 b2
sndK

-- | An element of a product of profunctors is a pair of elements.
instance (TestingEqShow (p a b), TestingEqShow (q a b)) => TestingEqShow ((p :*: q) a b) where
  eqP :: (:*:) p q a b -> (:*:) p q a b -> Property Bool
eqP (p a b
l1 :*: q a b
l2) (p a b
r1 :*: q a b
r2) = (Bool -> Bool -> Bool)
-> Property Bool -> Property Bool -> Property Bool
forall a b c.
(a -> b -> c)
-> Property' String a -> Property' String b -> Property' String c
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(&&) (p a b -> p a b -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP p a b
l1 p a b
r1) (q a b -> q a b -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP q a b
l2 q a b
r2)
  showP :: (:*:) p q a b -> String
showP (p a b
l :*: q a b
r) = String
"(" String -> ShowS
forall a. [a] -> [a] -> [a]
++ p a b -> String
forall a. TestingEqShow a => a -> String
showP p a b
l String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
") :*: (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ q a b -> String
forall a. TestingEqShow a => a -> String
showP q a b
r String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"

instance (TestableType (p a b), TestableType (q a b)) => TestableType ((p :*: q) a b) where
  gen :: GenTotal ((:*:) p q a b)
gen = (p a b -> q a b -> (:*:) p q a b)
-> ((:*:) p q a b -> p a b)
-> ((:*:) p q a b -> q a b)
-> GenTotal ((:*:) p q a b)
forall a b c.
(TestableType a, TestableType b) =>
(a -> b -> c) -> (c -> a) -> (c -> b) -> GenTotal c
genBoth p a b -> q a b -> (:*:) p q a b
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j) (q :: j +-> k).
p a b -> q a b -> (:*:) p q a b
(:*:) (:*:) p q a b -> p a b
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j) (q :: j +-> k).
(:*:) p q a b -> p a b
fstP (:*:) p q a b -> q a b
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j) (q :: j +-> k).
(:*:) p q a b -> q a b
sndP

instance
  (TestableProfunctor p, TestableProfunctor q, TestableTypeP p, TestableTypeP q)
  => TestableProfunctor (p :*: q)

-- | An element of a coproduct of profunctors is an element of one side, tagged.
instance (TestingEqShow (p a b), TestingEqShow (q a b)) => TestingEqShow ((p :+: q) a b) where
  eqP :: (:+:) p q a b -> (:+:) p q a b -> Property Bool
eqP (InjL p a b
l) (InjL p a b
r) = p a b -> p a b -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP p a b
l p a b
r
  eqP (InjR q a b
l) (InjR q a b
r) = q a b -> q a b -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP q a b
l q a b
r
  eqP (:+:) p q a b
_ (:+:) p q a b
_ = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
False
  showP :: (:+:) p q a b -> String
showP (InjL p a b
l) = String
"InjL (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ p a b -> String
forall a. TestingEqShow a => a -> String
showP p a b
l String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
  showP (InjR q a b
r) = String
"InjR (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ q a b -> String
forall a. TestingEqShow a => a -> String
showP q a b
r String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"

instance (TestableType (p a b), TestableType (q a b)) => TestableType ((p :+: q) a b) where
  gen :: GenTotal ((:+:) p q a b)
gen = case (forall a. TestableType a => GenTotal a
gen @(p a b), forall a. TestableType a => GenTotal a
gen @(q a b)) of
    (GenEmpty forall x. p a b -> x
f, GenEmpty forall x. q a b -> x
g) -> (forall x. (:+:) p q a b -> x) -> GenTotal ((:+:) p q a b)
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty \case InjL p a b
l -> p a b -> x
forall x. p a b -> x
f p a b
l; InjR q a b
r -> q a b -> x
forall x. q a b -> x
g q a b
r
    (GenEmpty forall x. p a b -> x
_, GenNonEmpty Gen (q a b)
gr) -> Gen ((:+:) p q a b) -> GenTotal ((:+:) p q a b)
forall a. Gen a -> GenTotal a
GenNonEmpty (q a b -> (:+:) p q a b
forall {j} {k} (q :: j +-> k) (a :: k) (b :: j) (p :: j +-> k).
q a b -> (:+:) p q a b
InjR (q a b -> (:+:) p q a b) -> Gen (q a b) -> Gen ((:+:) p q a b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (q a b)
gr)
    (GenNonEmpty Gen (p a b)
gl, GenEmpty forall x. q a b -> x
_) -> Gen ((:+:) p q a b) -> GenTotal ((:+:) p q a b)
forall a. Gen a -> GenTotal a
GenNonEmpty (p a b -> (:+:) p q a b
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j) (q :: j +-> k).
p a b -> (:+:) p q a b
InjL (p a b -> (:+:) p q a b) -> Gen (p a b) -> Gen ((:+:) p q a b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (p a b)
gl)
    (GenNonEmpty Gen (p a b)
gl, GenNonEmpty Gen (q a b)
gr) -> Gen ((:+:) p q a b) -> GenTotal ((:+:) p q a b)
forall a. Gen a -> GenTotal a
GenNonEmpty (NonEmpty (Gen ((:+:) p q a b)) -> Gen ((:+:) p q a b)
forall a. NonEmpty (Gen a) -> Gen a
oneof ((p a b -> (:+:) p q a b
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j) (q :: j +-> k).
p a b -> (:+:) p q a b
InjL (p a b -> (:+:) p q a b) -> Gen (p a b) -> Gen ((:+:) p q a b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (p a b)
gl) Gen ((:+:) p q a b)
-> [Gen ((:+:) p q a b)] -> NonEmpty (Gen ((:+:) p q a b))
forall a. a -> [a] -> NonEmpty a
:| [q a b -> (:+:) p q a b
forall {j} {k} (q :: j +-> k) (a :: k) (b :: j) (p :: j +-> k).
q a b -> (:+:) p q a b
InjR (q a b -> (:+:) p q a b) -> Gen (q a b) -> Gen ((:+:) p q a b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (q a b)
gr]))

instance
  (TestableProfunctor p, TestableProfunctor q, TestableTypeP p, TestableTypeP q)
  => TestableProfunctor (p :+: q)

-- | A 'Tabulated' value is its index, so equality and display are the index's.
instance TestingEqShow (Tabulated t lm rm a b) where
  eqP :: Tabulated t lm rm a b -> Tabulated t lm rm a b -> Property Bool
eqP (Tabulated Natural
i) (Tabulated Natural
j) = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Natural
i Natural -> Natural -> Bool
forall a. Eq a => a -> a -> Bool
== Natural
j)
  showP :: Tabulated t lm rm a b -> String
showP (Tabulated Natural
i) = Natural -> String
forall a. Show a => a -> String
show Natural
i

instance
  ( Testable j
  , Testable k
  , FiniteCat j
  , FiniteCat k
  , KnownTables j k lm rm
  , TestOb (a :: k)
  , TestOb (b :: j)
  )
  => TestableType (Tabulated t lm rm a b)
  where
  gen :: GenTotal (Tabulated t lm rm a b)
gen = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements @(Tabulated t lm rm)))

instance
  ( Testable j
  , Testable k
  , FiniteCat j
  , FiniteCat k
  , KnownTables j k lm rm
  )
  => TestableProfunctor (Tabulated t lm rm :: j +-> k)

-- | The terminal profunctor has one element at every pair of objects.
instance TestingEqShow (TerminalProfunctor a b) where
  -- forcing is the one thing left to check
  eqP :: TerminalProfunctor a b -> TerminalProfunctor a b -> Property Bool
eqP TerminalProfunctor a b
l TerminalProfunctor a b
r = TerminalProfunctor a b
l TerminalProfunctor a b -> Property Bool -> Property Bool
forall a b. a -> b -> b
`seq` TerminalProfunctor a b
r TerminalProfunctor a b -> Property Bool -> Property Bool
forall a b. a -> b -> b
`seq` Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
True
  showP :: TerminalProfunctor a b -> String
showP TerminalProfunctor a b
_ = String
"TerminalProfunctor"

instance (Testable j, Testable k, TestOb (a :: k), TestOb (b :: j)) => TestableType (TerminalProfunctor a b) where
  gen :: GenTotal (TerminalProfunctor a b)
gen = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (TerminalProfunctor a b -> GenTotal (TerminalProfunctor a b)
forall a. a -> GenTotal a
oneElem TerminalProfunctor a b
forall {j} {k} (a :: j) (b :: k).
(CategoryOf j, CategoryOf k, Ob a, Ob b) =>
TerminalProfunctor a b
TerminalProfunctor))

instance (Testable j, Testable k) => TestableProfunctor (TerminalProfunctor :: j +-> k)

-- | An element of the Yoneda embedding is an arrow into @x@ paired with an arrow out of @b@, so it
-- is testable wherever both categories are. So a representable can be used as a test fixture, at
-- either variance.
instance
  (Testable j, Testable k, TestOb (a :: k), TestOb (x :: k), TestOb (b :: j), TestOb (c :: j))
  => TestingEqShow (Yo x (OP b) a c)
  where
  eqP :: Yo x (OP b) a c -> Yo x (OP b) a c -> Property Bool
eqP (Yo a ~> x
f b1 ~> c
h) (Yo a ~> x
g b1 ~> c
i) = (Bool -> Bool -> Bool)
-> Property Bool -> Property Bool -> Property Bool
forall a b c.
(a -> b -> c)
-> Property' String a -> Property' String b -> Property' String c
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(&&) ((a ~> x) -> (a ~> x) -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP a ~> x
f a ~> x
g) ((b ~> c) -> (b ~> c) -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP b ~> c
b1 ~> c
h b ~> c
b1 ~> c
i)
  showP :: Yo x (OP b) a c -> String
showP (Yo a ~> x
f b1 ~> c
h) = String
"Yo (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ (a ~> x) -> String
forall a. TestingEqShow a => a -> String
showP a ~> x
f String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
") (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ (b ~> c) -> String
forall a. TestingEqShow a => a -> String
showP b ~> c
b1 ~> c
h String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"

instance
  (Testable j, Testable k, TestOb (a :: k), TestOb (x :: k), TestOb (b :: j), TestOb (c :: j))
  => TestableType (Yo x (OP b) a c)
  where
  gen :: GenTotal (Yo x (OP b) a c)
gen = ((a ~> x) -> (b ~> c) -> Yo x (OP b) a c)
-> (Yo x (OP b) a c -> a ~> x)
-> (Yo x (OP b) a c -> b ~> c)
-> GenTotal (Yo x (OP b) a c)
forall a b c.
(TestableType a, TestableType b) =>
(a -> b -> c) -> (c -> a) -> (c -> b) -> GenTotal c
genBoth (a ~> x) -> (b ~> c) -> Yo x (OP b) a c
forall {k} {j} (c :: k) (a :: k) (b1 :: j) (d :: j).
(c ~> a) -> (b1 ~> d) -> Yo a (OP b1) c d
Yo (\(Yo a ~> x
l b1 ~> c
_) -> a ~> x
l) (\(Yo a ~> x
_ b1 ~> c
r) -> b ~> c
b1 ~> c
r)

instance (Testable j, Testable k, TestOb (x :: k), TestOb (b :: j)) => TestableProfunctor (Yo x (OP b))

-- | A sieve is a table of booleans over the points of the representable, and 'Finitary' numbers the
-- sieves at each pair of objects. So a sieve can be generated by picking one, and compared and
-- shown by its table. Without this, nothing that quantifies over sieves as elements of a
-- profunctor (such as 'Proarrow.Testing.Laws.propNaturalTransformation') can run at 'Sieve'.
--
-- 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 'TestOb' constraints pin @j@ and @k@, which @'Sieve' a b@ does not mention.
instance (FiniteCat j, FiniteCat k, TestOb (a :: k), TestOb (b :: j)) => TestingEqShow (Sieve a b) where
  eqP :: Sieve a b -> Sieve a b -> Property Bool
eqP Sieve a b
s Sieve a b
t = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Sieve a b -> [Bool]
forall {j} {k} (a :: k) (b :: j).
(FiniteCat j, FiniteCat k) =>
Sieve a b -> [Bool]
sieveTable Sieve a b
s [Bool] -> [Bool] -> Bool
forall a. Eq a => a -> a -> Bool
== Sieve a b -> [Bool]
forall {j} {k} (a :: k) (b :: j).
(FiniteCat j, FiniteCat k) =>
Sieve a b -> [Bool]
sieveTable Sieve a b
t)
  showP :: Sieve a b -> String
showP Sieve a b
s = [Bool] -> String
forall a. Show a => a -> String
show (Sieve a b -> [Bool]
forall {j} {k} (a :: k) (b :: j).
(FiniteCat j, FiniteCat k) =>
Sieve a b -> [Bool]
sieveTable Sieve a b
s)

instance
  (Testable j, Testable k, FiniteCat j, FiniteCat k, TestOb (a :: k), TestOb (b :: j))
  => TestableType (Sieve a b)
  where
  gen :: GenTotal (Sieve a b)
gen = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements @(Sieve :: j +-> k)))

instance (Testable j, Testable k, FiniteCat j, FiniteCat k) => TestableProfunctor (Sieve :: j +-> k)

-- | An element of the internal hom is a natural transformation out of a weight, which 'Finitary'
-- numbers; compare and show it by that number, as 'Tabulated' is. Drawing one enumerates them
-- all, so an exponential is expensive to quantify over.
instance
  (Finitary p, Finitary q, FiniteCat j, FiniteCat k, TestOb (a :: k), TestOb (b :: j))
  => TestingEqShow ((p :~>: q) a b)
  where
  -- matching on 'Exp' brings the objects into scope, as it does for 'Sieve'
  eqP :: (:~>:) p q a b -> (:~>:) p q a b -> Property Bool
eqP x :: (:~>:) p q a b
x@Exp{} (:~>:) p q a b
y = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex @(p :~>: q) (:~>:) p q a b
x Natural -> Natural -> Bool
forall a. Eq a => a -> a -> Bool
== (:~>:) p q a b -> Natural
forall (a :: k) (b :: j). (Ob a, Ob b) => (:~>:) p q a b -> Natural
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex (:~>:) p q a b
y)
  showP :: (:~>:) p q a b -> String
showP x :: (:~>:) p q a b
x@Exp{} = Natural -> String
forall a. Show a => a -> String
show (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex @(p :~>: q) (:~>:) p q a b
x)

instance
  (Testable j, Testable k, Finitary p, Finitary q, FiniteCat j, FiniteCat k, TestOb (a :: k), TestOb (b :: j))
  => TestableType ((p :~>: q) a b)
  where
  gen :: GenTotal ((:~>:) p q a b)
gen = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements @(p :~>: q)))

instance
  (Testable j, Testable k, Finitary p, Finitary q, FiniteCat j, FiniteCat k)
  => TestableProfunctor (p :~>: q :: j +-> k)

-- | A natural transformation between finitary profunctors, compared and shown by its table and
-- drawn from 'natTransformations'. The hom-sets of a category of finitary profunctors, with or
-- without the 'SUBCAT' wrapper.
instance (Finitary p, Finitary q, FiniteCat j, FiniteCat k) => TestingEqShow (Prof (p :: j +-> k) q) where
  eqP :: Prof p q -> Prof p q -> Property Bool
eqP (Prof p :~> q
f) (Prof p :~> q
g) = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
(p :~> q) -> [Natural]
forall (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
(p :~> q) -> [Natural]
natTable @p @q p a b -> q a b
p :~> q
f [Natural] -> [Natural] -> Bool
forall a. Eq a => a -> a -> Bool
== forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
(p :~> q) -> [Natural]
forall (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
(p :~> q) -> [Natural]
natTable @p @q p a b -> q a b
p :~> q
g)
  showP :: Prof p q -> String
showP (Prof p :~> q
f) = [Natural] -> String
forall a. Show a => a -> String
show (forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
(p :~> q) -> [Natural]
forall (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
(p :~> q) -> [Natural]
natTable @p @q p a b -> q a b
p :~> q
f)

instance (Finitary p, Finitary q, FiniteCat j, FiniteCat k) => TestableType (Prof (p :: j +-> k) q) where
  gen :: GenTotal (Prof p q)
gen = case forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
[p ~> q]
forall (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
[p ~> q]
natTransformations @p @q of
    [] -> (forall x. Prof p q -> x) -> GenTotal (Prof p q)
forall a. (forall x. a -> x) -> GenTotal a
GenEmpty \Prof p q
_ -> String -> x
forall a. HasCallStack => String -> a
error String
"no natural transformations between these profunctors"
    [p ~> q]
fs -> [Prof p q] -> GenTotal (Prof p q)
forall a. [a] -> GenTotal a
optGen [p ~> q]
[Prof p q]
fs

-- | The right Kan lift and extension of finitary profunctors, compared and shown by index, as the
-- internal hom is.
instance
  (Testable j, Testable k, Finitary w, Finitary p, FiniteCat i, FiniteCat j, TestOb (a :: k), TestOb (b :: j))
  => TestingEqShow (Rift (OP (w :: k +-> i)) p a b)
  where
  eqP :: Rift (OP w) p a b -> Rift (OP w) p a b -> Property Bool
eqP x :: Rift (OP w) p a b
x@Rift{} Rift (OP w) p a b
y = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex @(Rift (OP w) p) Rift (OP w) p a b
x Natural -> Natural -> Bool
forall a. Eq a => a -> a -> Bool
== Rift (OP w) p a b -> Natural
forall (a :: k) (b :: j).
(Ob a, Ob b) =>
Rift (OP w) p a b -> Natural
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex Rift (OP w) p a b
y)
  showP :: Rift (OP w) p a b -> String
showP x :: Rift (OP w) p a b
x@Rift{} = Natural -> String
forall a. Show a => a -> String
show (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex @(Rift (OP w) p) Rift (OP w) p a b
x)

instance
  (Testable j, Testable k, Finitary w, Finitary p, FiniteCat i, FiniteCat j, TestOb (a :: k), TestOb (b :: j))
  => TestableType (Rift (OP (w :: k +-> i)) p a b)
  where
  gen :: GenTotal (Rift (OP w) p a b)
gen = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements @(Rift (OP w) p)))

instance
  (Testable j, Testable k, Finitary w, Finitary p, FiniteCat i, FiniteCat j)
  => TestableProfunctor (Rift (OP (w :: k +-> i)) p :: j +-> k)

instance
  (Testable j, Testable k, Finitary v, Finitary p, FiniteCat i, FiniteCat k, TestOb (a :: k), TestOb (b :: j))
  => TestingEqShow (Ran (OP (v :: i +-> j)) p a b)
  where
  eqP :: Ran (OP v) p a b -> Ran (OP v) p a b -> Property Bool
eqP x :: Ran (OP v) p a b
x@Ran{} Ran (OP v) p a b
y = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex @(Ran (OP v) p) Ran (OP v) p a b
x Natural -> Natural -> Bool
forall a. Eq a => a -> a -> Bool
== Ran (OP v) p a b -> Natural
forall (a :: k) (b :: j).
(Ob a, Ob b) =>
Ran (OP v) p a b -> Natural
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex Ran (OP v) p a b
y)
  showP :: Ran (OP v) p a b -> String
showP x :: Ran (OP v) p a b
x@Ran{} = Natural -> String
forall a. Show a => a -> String
show (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
toIndex @(Ran (OP v) p) Ran (OP v) p a b
x)

instance
  (Testable j, Testable k, Finitary v, Finitary p, FiniteCat i, FiniteCat k, TestOb (a :: k), TestOb (b :: j))
  => TestableType (Ran (OP (v :: i +-> j)) p a b)
  where
  gen :: GenTotal (Ran (OP v) p a b)
gen = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements @(Ran (OP v) p)))

instance
  (Testable j, Testable k, Finitary v, Finitary p, FiniteCat i, FiniteCat k)
  => TestableProfunctor (Ran (OP (v :: i +-> j)) p :: j +-> k)

-- | 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 'closure' of every sieve at the pair.
instance (FiniteCat j, FiniteCat k, TestOb (a :: k), TestOb (b :: j)) => TestingEqShow (ClosedSieve t a b) where
  eqP :: ClosedSieve t a b -> ClosedSieve t a b -> Property Bool
eqP (ClosedSieve Sieve a b
s) (ClosedSieve Sieve a b
u) = Sieve a b -> Sieve a b -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP Sieve a b
s Sieve a b
u
  showP :: ClosedSieve t a b -> String
showP (ClosedSieve Sieve a b
s) = Sieve a b -> String
forall a. TestingEqShow a => a -> String
showP Sieve a b
s

instance
  ( Testable j
  , Testable k
  , HasFiniteCovers t k
  , FiniteCat j
  , FiniteCat k
  , TestOb (a :: k)
  , TestOb (b :: j)
  )
  => TestableType (ClosedSieve t a b)
  where
  gen :: GenTotal (ClosedSieve t a b)
gen = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements @(ClosedSieve t :: j +-> k)))

instance
  (Testable j, Testable k, HasFiniteCovers t k, FiniteCat j, FiniteCat k)
  => TestableProfunctor (ClosedSieve t :: j +-> k)

-- | Compared by 'samePlus' and shown by 'plusTable'. See 'Plus' for what a value stands for.
instance
-- as for 'Sieve', the 'TestOb's are what pin @j@ and @k@
  (HasFiniteCovers t k, Finitary p, FiniteCat j, FiniteCat k, TestOb (a :: k), TestOb (b :: j))
  => TestingEqShow (Plus t p a b)
  where
  eqP :: Plus t p a b -> Plus t p a b -> Property Bool
eqP Plus t p a b
x Plus t p a b
y = Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Plus t p a b -> Plus t p a b -> Bool
forall t {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(HasFiniteCovers t k, Finitary p, FiniteCat j, FiniteCat k) =>
Plus t p a b -> Plus t p a b -> Bool
samePlus Plus t p a b
x Plus t p a b
y)
  showP :: Plus t p a b -> String
showP Plus t p a b
x = [Natural] -> String
forall a. Show a => a -> String
show (Plus t p a b -> [Natural]
forall t {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(HasFiniteCovers t k, Finitary p, FiniteCat j, FiniteCat k) =>
Plus t p a b -> [Natural]
plusTable Plus t p a b
x)

instance
  (Testable j, Testable k, HasFiniteCovers t k, Finitary p, FiniteCat j, FiniteCat k, TestOb (a :: k), TestOb (b :: j))
  => TestableType (Plus t p a b)
  where
  gen :: GenTotal (Plus t p a b)
gen = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements @(Plus t p :: j +-> k)))

instance
  (Testable j, Testable k, HasFiniteCovers t k, Finitary p, FiniteCat j, FiniteCat k)
  => TestableProfunctor (Plus t p :: j +-> k)

-- | A hom-set of a full subcategory of finitary profunctors ('FINITARY', or the sheaves of
-- "Proarrow.Category.Enriched.Finitary.Sheaf") is enumerable, by 'natTransformations', so it can
-- be generated. Without that a category of profunctors would not be testable at all. Equality and
-- display go through the table of indices, there being nothing else to see of a natural
-- transformation. (The table is cheaper than the index into 'elements' would be, which has to
-- search for it.)
instance
  (Finitary p, Finitary q, FiniteCat j, FiniteCat k)
  => TestingEqShow (Sub Prof (SUB p :: SUBCAT (ob :: OB (j +-> k))) (SUB q))
  where
  eqP :: Sub Prof (SUB p) (SUB q)
-> Sub Prof (SUB p) (SUB q) -> Property Bool
eqP (Sub Prof a1 b1
l) (Sub Prof a1 b1
r) = Prof a1 b1 -> Prof a1 b1 -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP Prof a1 b1
l Prof a1 b1
Prof a1 b1
r
  showP :: Sub Prof (SUB p) (SUB q) -> String
showP (Sub Prof a1 b1
f) = Prof a1 b1 -> String
forall a. TestingEqShow a => a -> String
showP Prof a1 b1
f

instance
  (Finitary (Sub Prof :: CAT (SUBCAT ob)), Finitary p, Finitary q, FiniteCat j, FiniteCat k, ob p, ob q)
  => TestableType (Sub Prof (SUB p :: SUBCAT (ob :: OB (j +-> k))) (SUB q))
  where
  -- a hom-set is empty whenever @q@ runs out of elements where @p@ has some, and then the
  -- properties discard rather than fail
  gen :: GenTotal (Sub Prof (SUB p) (SUB q))
gen = forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
forall (p :: SUBCAT ob +-> SUBCAT ob) (a :: SUBCAT ob)
       (b :: SUBCAT ob).
(Finitary p, Ob a, Ob b) =>
GenTotal (p a b)
genElements @(Sub Prof) @(SUB p) @(SUB q)

instance (Ob a, Ob b) => TestableType (Unit a b) where
  gen :: GenTotal (Unit a b)
gen = Unit a b -> GenTotal (Unit a b)
forall a. a -> GenTotal a
oneElem Unit a b
Unit '() '()
Unit
instance TestingEqShow (Unit a b) where
  showP :: Unit a b -> String
showP Unit a b
_ = String
"Unit"

  -- a singleton, so equality is free; forcing is the one thing left to check
  eqP :: Unit a b -> Unit a b -> Property Bool
eqP Unit a b
l Unit a b
r = Unit a b
l Unit a b -> Property Bool -> Property Bool
forall a b. a -> b -> b
`seq` Unit a b
r Unit a b -> Property Bool -> Property Bool
forall a b. a -> b -> b
`seq` Bool -> Property Bool
forall a. a -> Property' String a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
True

sampleT :: forall t. (TestableType t) => IO (Maybe String)
sampleT :: forall t. TestableType t => IO (Maybe String)
sampleT = Property () -> IO (Maybe String)
forall e a. Property' e a -> IO (Maybe e)
falsify (Property () -> IO (Maybe String))
-> Property () -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ do
  p <- forall a. TestableType a => Property a
genP @t
  traceM (showP p)

sampleP :: forall {j} {k} (p :: j +-> k). (Testable j, Testable k, TestableProfunctor p) => IO (Maybe String)
sampleP :: forall {j} {k} (p :: j +-> k).
(Testable j, Testable k, TestableProfunctor p) =>
IO (Maybe String)
sampleP = Property () -> IO (Maybe String)
forall e a. Property' e a -> IO (Maybe e)
falsify (Property () -> IO (Maybe String))
-> Property () -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ do
  p <- forall {j} {k} (p :: j +-> k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
forall (p :: j +-> k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
genProfunctorElt @p String
"p"
  traceShowM p

sampleK :: forall k. (Testable k) => IO (Maybe String)
sampleK :: forall k. Testable k => IO (Maybe String)
sampleK = forall e a. Property' e a -> IO (Maybe e)
falsify @_ @() (Property () -> IO (Maybe String))
-> Property () -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  traceM $ showOb @k @a