{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ImpredicativeTypes #-}

{- HLINT ignore "Redundant id" -}

-- | Reusable law-checking properties, parameterized over any 'Testable' kind: 'testCategory',
-- 'testMonoidal', 'testBinaryProducts', 'testClosed', and friends. Wiring a new category into a
-- test suite is a 'Testable' instance plus calls to these; the @test/Props@ directory of
-- proarrow's source repository has many examples.
--
-- A @test@ returns a 'TestTree', ready for a 'Test.Tasty.testGroup'. A @prop@ returns a
-- @'Property' ()@, to be composed into a property of your own. Where both exist (e.g. 'testMonoid'
-- and 'propMonoid'), the @test@ one wraps the @prop@ one.
--
-- Many of these take an explicit witness that 'TestOb' is closed under the structure being tested
-- (e.g. that @'TestOb' (a '**' b)@ follows from @'TestOb' a@ and @'TestOb' b@). The @_@-suffixed
-- variant (e.g. 'testMonoidal_') supplies it from a 'TestObIsOb' constraint, for categories where
-- every object is a 'TestOb', typically those that leave 'TestOb' at its @'Ob'@ default.
module Proarrow.Testing.Laws where

import Control.Monad (when)
import Data.Default (def)
import Data.Foldable (for_)
import Data.List (genericLength, sort)
import Numeric.Natural (Natural)
import Test.Falsify (Property)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.Falsify (TestOptions, testProperty)
import Prelude hiding (elem, fst, id, snd, (.), (>>))

import Proarrow.Adjunction (Adjunction)
import Proarrow.Adjunction qualified as Adj
import Proarrow.Category.Enriched.Dagger qualified as Dagger
import Proarrow.Category.Enriched.Finitary qualified as Finitary
import Proarrow.Category.Enriched.Finitary.Sheaf qualified as FinSheaf
import Proarrow.Category.Enriched.Finitary.Topos qualified as FinTopos
import Proarrow.Category.Enriched.Thin qualified as Thin
import Proarrow.Category.Instance.Opposite (OPPOSITE (..))
import Proarrow.Category.Instance.Prof (Prof (..))
import Proarrow.Category.Instance.Sub (Sub (..))
import Proarrow.Category.Monoidal qualified as M
import Proarrow.Category.Monoidal.Cartesian qualified as Cartesian
import Proarrow.Category.Monoidal.Closed qualified as Exponential
import Proarrow.Category.Monoidal.CompactClosed qualified as CC
import Proarrow.Category.Monoidal.CopyDiscard qualified as CopyDiscard
import Proarrow.Category.Monoidal.Dialogue qualified as SA
import Proarrow.Category.Monoidal.Distributive qualified as Distributive
import Proarrow.Category.Monoidal.Hypergraph qualified as Hypergraph
import Proarrow.Category.Monoidal.IsoMix qualified as IsoMix
import Proarrow.Category.Monoidal.StarAutonomous qualified as SA
import Proarrow.Category.Monoidal.Strength qualified as Strength
import Proarrow.Category.Sheaf qualified as Sheaf
import Proarrow.Category.Topos qualified as Topos
import Proarrow.Colimit.BinaryCoproduct qualified as BinaryCoproduct
import Proarrow.Colimit.Coequalizer qualified as Coequalizer
import Proarrow.Colimit.Initial qualified as Initial
import Proarrow.Colimit.Pushout qualified as Pushout
import Proarrow.Core
  ( CategoryOf (..)
  , Hom
  , Profunctor (..)
  , Promonad (..)
  , lmap
  , obj
  , rmap
  , (//)
  , (:~>)
  , type (+->)
  )
import Proarrow.Functor qualified as Functor
import Proarrow.Limit.BinaryProduct qualified as BinaryProduct
import Proarrow.Limit.Equalizer qualified as Equalizer
import Proarrow.Limit.Pullback qualified as Pullback
import Proarrow.Limit.Terminal qualified as Terminal
import Proarrow.Monoid qualified as Monoid
import Proarrow.Object (pattern Objs)
import Proarrow.Optic (ExOptic, Flip, Optic)
import Proarrow.Optic.Getter (GetterFl, review, view)
import Proarrow.Profunctor.Corepresentable (Corepresentable, coindex, withObCorep, type (%%))
import Proarrow.Profunctor.Instance.Composition ((:.:) (..))
import Proarrow.Profunctor.Instance.Ran (Ran (..))
import Proarrow.Profunctor.Instance.Rift (Rift (..))
import Proarrow.Profunctor.Instance.Sieve (Sieve (..))
import Proarrow.Profunctor.Instance.Yoneda (Yo (..))
import Proarrow.Profunctor.Representable (Representable, withObRep, type (%))
import Proarrow.Promonad qualified as Promonad
import Proarrow.Testing
import Proarrow.Testing.Laws.Run
  ( CorepresentedBy
  , RepresentedBy
  , TESTED
  , TestedP
  , Witness (..)
  , Witnesses (..)
  , testLaws
  , testLawsWith
  , testProLaws
  )
import Proarrow.Tools.Laws qualified as Laws

-- * Isomorphisms

-- | Two arrows are mutually inverse: @f . g = id@ and @g . f = id@.
propIso :: forall {k} (a :: k) b. (Testable k, TestOb a, TestOb b) => a ~> b -> b ~> a -> Property ()
propIso :: forall {k} (a :: k) (b :: k).
(Testable k, TestOb a, TestOb b) =>
(a ~> b) -> (b ~> a) -> Property ()
propIso a ~> b
f b ~> a
g = forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a ((Ob a => Property ()) -> Property ())
-> (Ob a => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b ((Ob b => Property ()) -> Property ())
-> (Ob b => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$ do
  String -> String -> (b ~> b) -> String -> (b ~> b) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"right inverse" String
"f . g" (a ~> b
f (a ~> b) -> (b ~> a) -> b ~> b
forall (b :: k) (c :: k) (a :: k). (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
g) String
"id" b ~> b
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id
  String -> String -> (a ~> a) -> String -> (a ~> a) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"left inverse" String
"g . f" (b ~> a
g (b ~> a) -> (a ~> b) -> a ~> a
forall (b :: k) (c :: k) (a :: k). (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
f) String
"id" a ~> a
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id

-- | An optic is an isomorphism: its 'view' and 'review' are mutually inverse, by 'propIso'.
propIso'
  :: forall {k} c (a :: k) b
   . (Testable k, TestOb a, TestOb b, (Ob b) => c (ExOptic GetterFl b b), (Ob b) => c (ExOptic (Flip GetterFl) b b))
  => Optic c a a b b -> Property ()
propIso' :: forall {k} (c :: (k -> k -> Type) -> Constraint) (a :: k) (b :: k).
(Testable k, TestOb a, TestOb b, Ob b => c (ExOptic GetterFl b b),
 Ob b => c (ExOptic (Flip GetterFl) b b)) =>
Optic c a a b b -> Property ()
propIso' Optic c a a b b
o = (a ~> b) -> (b ~> a) -> Property ()
forall {k} (a :: k) (b :: k).
(Testable k, TestOb a, TestOb b) =>
(a ~> b) -> (b ~> a) -> Property ()
propIso (Optic c a a b b -> a ~> b
forall {j} {k} (c :: (k -> j -> Type) -> Constraint) (s :: k)
       (t :: j) (a :: k) (b :: j).
(CategoryOf j, CategoryOf k,
 (Ob a, Ob b) => c (ExOptic GetterFl a b)) =>
Optic c s t a b -> s ~> a
view Optic c a a b b
o) (Optic c a a b b -> b ~> a
forall {j} {k} (c :: (k -> j -> Type) -> Constraint) (s :: k)
       (t :: j) (a :: k) (b :: j).
(CategoryOf j, CategoryOf k,
 (Ob a, Ob b) => c (ExOptic (Flip GetterFl) a b)) =>
Optic c s t a b -> b ~> t
review Optic c a a b b
o)

-- | Two functions between the elements of @p a b@ and of @q c d@ are mutually inverse, at
-- generated elements.
propIsoP
  :: forall p q a b c d
   . (TestableTypeP p, TestableTypeP q, TestOb a, TestOb b, TestOb c, TestOb d)
  => (p a b -> q c d) -> (q c d -> p a b) -> Property ()
propIsoP :: forall {j} {k} {j} {k} (p :: j +-> k) (q :: j +-> k) (a :: k)
       (b :: j) (c :: k) (d :: j).
(TestableTypeP p, TestableTypeP q, TestOb a, TestOb b, TestOb c,
 TestOb d) =>
(p a b -> q c d) -> (q c d -> p a b) -> Property ()
propIsoP p a b -> q c d
f q c d -> p a b
g = do
  p <- forall a. TestableType a => String -> Property a
genNamed @(p a b) String
"p"
  testEq "left inverse" "g (f p)" (g (f p)) "p" p
  q <- genNamed @(q c d) "q"
  testEq "right inverse" "f (g q)" (f (g q)) "q" q

-- | Two natural transformations are mutually inverse: 'propIsoP' at generated objects, and each is
-- natural ('propNaturalTransformation').
propNaturalIsoP
  :: forall {j} {k} (p :: j +-> k) q
   . (TestableProfunctor p, TestableTypeP p, TestableProfunctor q, TestableTypeP q)
  => (p :~> q) -> (q :~> p) -> Property ()
propNaturalIsoP :: forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(TestableProfunctor p, TestableTypeP p, TestableProfunctor q,
 TestableTypeP q) =>
(p :~> q) -> (q :~> p) -> Property ()
propNaturalIsoP p :~> q
f q :~> p
g = do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  Some @b <- genOb @j
  propIsoP @p @q @a @b f g
  propNaturalTransformation f
  propNaturalTransformation g

-- * Categories

-- | The category laws: 'id' is a left and right identity for @(.)@, and @(.)@ is associative.
testCategory :: forall k. (Testable k) => TestTree
testCategory :: forall k. Testable k => TestTree
testCategory = forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[CategoryOf] @k String
"Category" (Witness CategoryOf k
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf k
-> Witnesses '[] k -> Witnesses '[CategoryOf] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

-- | The laws of a dagger category: 'Laws.ProLaws' 'Dagger.DaggerProfunctor' at the hom profunctor,
-- so 'Dagger.dagger' is an involution and reverses composition.
testDagger :: forall k. (Testable k, Dagger.Dagger k) => TestTree
testDagger :: forall k. (Testable k, Dagger k) => TestTree
testDagger = forall {k} (p :: k +-> k).
(DaggerProfunctor p, TestableProfunctor p) =>
TestTree
forall (p :: k +-> k).
(DaggerProfunctor p, TestableProfunctor p) =>
TestTree
testDaggerProfunctor @(Hom k)

-- | The 'Dagger.DaggerProfunctor' laws of @p@ stated as code: 'Dagger.dagger' is an involution that
-- reverses 'dimap'.
testDaggerProfunctor :: forall {k} (p :: k +-> k). (Dagger.DaggerProfunctor p, TestableProfunctor p) => TestTree
testDaggerProfunctor :: forall {k} (p :: k +-> k).
(DaggerProfunctor p, TestableProfunctor p) =>
TestTree
testDaggerProfunctor =
  forall {j} {k}
       (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
forall (cl :: (TESTED '[CategoryOf] k +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: k +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
testCategoryProLaws @Dagger.DaggerProfunctor @p TestOptions
defaultTestOptions String
"Dagger"

-- * Profunctors

-- | 'testProLaws' for a profunctor class whose laws need only the categories of @p@, with the given
-- options.
testCategoryProLaws
  :: forall {j} {k} cl (p :: j +-> k)
   . (Laws.ProLaws cl, TestableProfunctor p, cl (TestedP p :: TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k))
  => TestOptions -> String -> TestTree
testCategoryProLaws :: forall {j} {k}
       (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
testCategoryProLaws TestOptions
opts String
name = forall (csj :: [Type -> Constraint]) (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
forall {j} {k} (csj :: [Type -> Constraint])
       (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
testProLaws @'[CategoryOf] @'[CategoryOf] @cl @p TestOptions
opts Gen (Some i)
forall i. Testable i => Gen (Some i)
genSome String
name (Witness CategoryOf j
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf j
-> Witnesses '[] j -> Witnesses '[CategoryOf] j
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] j
forall k. Witnesses '[] k
WNil) (Witness CategoryOf k
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf k
-> Witnesses '[] k -> Witnesses '[CategoryOf] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

-- | The falsify options of a plain 'testProperty', to adjust for one test, e.g. a larger
-- 'overrideMaxRatio' where a law's arrows rarely exist.
defaultTestOptions :: TestOptions
defaultTestOptions :: TestOptions
defaultTestOptions = TestOptions
forall a. Default a => a
def

-- | The profunctor laws of @p@ stated as code ('Laws.ProLaws' 'Profunctor').
testProfunctor :: forall {j} {k} (p :: j +-> k). (TestableProfunctor p) => TestTree
testProfunctor :: forall {j} {k} (p :: j +-> k). TestableProfunctor p => TestTree
testProfunctor = forall {j} {k} (p :: j +-> k).
TestableProfunctor p =>
TestOptions -> TestTree
forall (p :: j +-> k).
TestableProfunctor p =>
TestOptions -> TestTree
testProfunctorWith @p TestOptions
defaultTestOptions

-- | 'testProfunctor' with the given falsify options for each law.
testProfunctorWith :: forall {j} {k} (p :: j +-> k). (TestableProfunctor p) => TestOptions -> TestTree
testProfunctorWith :: forall {j} {k} (p :: j +-> k).
TestableProfunctor p =>
TestOptions -> TestTree
testProfunctorWith TestOptions
opts =
  forall {j} {k}
       (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
forall (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
testCategoryProLaws @Profunctor @p TestOptions
opts String
"Profunctor"

-- | 'Thin.decide' agrees with the generator: an element of @p a b@ can be generated exactly
-- when @'Thin.Holds' p a b@ decides to 'Proarrow.Category.Instance.Bool.TRU', and then (the
-- profunctor being thin) it is the decided element.
propDecidable
  :: forall {j} {k} (p :: j +-> k)
   . (Thin.DecidableProfunctor p, Testable j, Testable k, TestableTypeP p)
  => Property ()
propDecidable :: forall {j} {k} (p :: j +-> k).
(DecidableProfunctor p, Testable j, Testable k, TestableTypeP p) =>
Property ()
propDecidable = do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  Some @b <- genOb @j
  obFromTestOb @_ @a $
    obFromTestOb @_ @b $
      case Thin.decide @p @a @b of
        Thin.Yes p x x
x -> do
          String -> Bool -> Property ()
check String
"decide: TRU, but no element can be generated" (forall a. TestableType a => Bool
isGenNonEmpty @(p a b))
          y <- forall a. TestableType a => String -> Property a
genNamed @(p a b) String
"y"
          testEq "decide" "decide" x "y" y
        Decision p x x (Holds p x x)
Thin.No -> String -> Bool -> Property ()
check String
"decide: FLS, but an element can be generated" (Bool -> Bool
not (forall a. TestableType a => Bool
isGenNonEmpty @(p a b)))

-- | A transformation @n :: p ':~>' q@ is natural: @n ('dimap' f g p) = 'dimap' f g (n p)@.
propNaturalTransformation
  :: forall {j} {k} (p :: j +-> k) q. (TestableProfunctor p, TestableProfunctor q) => p :~> q -> Property ()
propNaturalTransformation :: forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(TestableProfunctor p, TestableProfunctor q) =>
(p :~> q) -> Property ()
propNaturalTransformation p :~> q
n = do
  SomeP @a @b 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"
  -- an object with no arrow to @a@ discards the run
  Some @c <- genObSuchThat @k \(Some @c) -> forall a. TestableType a => Bool
isGenNonEmpty @(c ~> a)
  Some @d <- genObSuchThat @j \(Some @d) -> forall a. TestableType a => Bool
isGenNonEmpty @(b ~> d)
  f <- genNamed @(c ~> a) "f"
  g <- genNamed @(b ~> d) "g"
  testEq "naturality" "n (dimap f g p)" (n (dimap f g p)) "dimap f g (n p)" (dimap f g (n p))

-- | The numbering laws of a 'Finitary.Finitary' profunctor: 'Finitary.elements' has
-- 'Finitary.size' entries and is numbered in order, and 'Finitary.fromIndex' recovers any element
-- from its index ('Laws.ProLaws' 'Finitary.Finitary'), including elements the instance did not
-- itself produce. That law, @fromIndex . toIndex@, checks that 'Finitary.size' is correct and not
-- merely self-consistent, but only as far as the 'TestableType' generator is independent of the
-- instance. One defined as
-- @optGen 'Finitary.elements'@ makes it vacuous. The label names the profunctor, which nothing in
-- its type can supply.
testFinitary
  :: forall {j} {k} (p :: j +-> k)
   . (Finitary.Finitary p, TestableProfunctor p, TestableTypeP p)
  => String
  -> TestTree
testFinitary :: forall {j} {k} (p :: j +-> k).
(Finitary p, TestableProfunctor p, TestableTypeP p) =>
String -> TestTree
testFinitary String
nm =
  String -> [TestTree] -> TestTree
testGroup
    (String
"Finitary " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
nm)
    [ String -> Property () -> TestTree
testProperty String
"numbering" (forall {j} {k} (p :: j +-> k).
(Finitary p, TestableProfunctor p, TestableTypeP p) =>
Property ()
forall (p :: j +-> k).
(Finitary p, TestableProfunctor p, TestableTypeP p) =>
Property ()
propNumbering @p)
    , forall {j} {k}
       (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
forall (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
testCategoryProLaws @Finitary.Finitary @p TestOptions
defaultTestOptions String
"laws"
    ]

-- | 'Finitary.elements' has 'Finitary.size' entries, numbered in order, and every index is below
-- 'Finitary.size'.
propNumbering
  :: forall {j} {k} (p :: j +-> k). (Finitary.Finitary p, TestableProfunctor p, TestableTypeP p) => Property ()
propNumbering :: forall {j} {k} (p :: j +-> k).
(Finitary p, TestableProfunctor p, TestableTypeP p) =>
Property ()
propNumbering = do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  Some @b <- genOb @j
  obFromTestOb @_ @a $ obFromTestOb @_ @b $ do
    let n = forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
Natural
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
Natural
Finitary.size @p @a @b
        es = 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]
Finitary.elements @p @a @b
    check
      ("size is " ++ show n ++ " but elements has " ++ show (genericLength es :: Natural) ++ " entries")
      (genericLength es == n)
    check
      ("elements should be numbered in order, found " ++ show (map (Finitary.toIndex @p @a @b) es))
      (map (Finitary.toIndex @p @a @b) es == Finitary.indices n)
    x <- genNamed @(p a b) "x"
    -- The numbering claims every index is below 'Finitary.size', which a @Fin@-typed index would
    -- have given for free. Without this check an undersized 'Finitary.size' goes unnoticed, since
    -- the other laws only ever look at the elements it admits.
    check
      ("toIndex " ++ showP x ++ " is " ++ show (Finitary.toIndex x) ++ ", not below size " ++ show n)
      (Finitary.toIndex x < n)

-- * Functors, representability and adjunctions

-- | Check the functor laws of a 'Functor.Functor' @f@: @map id = id@ and @map (g . f) = map g . map
-- f@. The witness lifts 'TestOb' along @f@ (usually @\\ \@a r -> r@ when @'TestOb' (f a)@ follows
-- from @'TestOb' a@). Functors encoded as representable profunctors ('Functor.FunctorForRep') are
-- instead tested via their @'Proarrow.Profunctor.Representable.Rep'@ with 'testProfunctor', since
-- the profunctor laws on @Rep f@ are the functor laws on @f@.
propFunctor
  :: forall {k1} {k2} (f :: k1 -> k2)
   . (Functor.Functor f, Testable k1, Testable k2)
  => (forall (a :: k1) r. (TestOb a) => ((TestOb (f a)) => r) -> r)
  -> Property ()
propFunctor :: forall {k1} {k2} (f :: k1 -> k2).
(Functor f, Testable k1, Testable k2) =>
(forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r)
-> Property ()
propFunctor forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r
withTestObF = do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k1
  Some @b <- genObSuchThat @k1 \(Some @b) -> forall a. TestableType a => Bool
isGenNonEmpty @(a ~> b)
  Some @c <- genObSuchThat @k1 \(Some @c) -> forall a. TestableType a => Bool
isGenNonEmpty @(b ~> c)
  f <- genNamed @(a ~> b) "f"
  g <- genNamed @(b ~> c) "g"
  withTestObF @a $
    withTestObF @c $
      obFromTestOb @_ @a $
        obFromTestOb @_ @c $
          -- 'Functor.withObF' recovers @Ob (f a)@\/@Ob (f c)@ from the functor
          Functor.withObF @f @a $
            Functor.withObF @f @c $ do
              testEq "identity" "map id" (Functor.map @f (obj @a)) "id" (obj @(f a))
              testEq
                "composition"
                "map (g . f)"
                (Functor.map @f (g . f))
                "map g . map f"
                (Functor.map @f g . Functor.map @f f)

-- | The functor laws of @f@ ('propFunctor') as a ready-made test.
testFunctor
  :: forall {k1} {k2} (f :: k1 -> k2)
   . (Functor.Functor f, Testable k1, Testable k2)
  => (forall (a :: k1) r. (TestOb a) => ((TestOb (f a)) => r) -> r)
  -> TestTree
testFunctor :: forall {k1} {k2} (f :: k1 -> k2).
(Functor f, Testable k1, Testable k2) =>
(forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r)
-> TestTree
testFunctor forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r
withTestObF = String -> Property () -> TestTree
testProperty String
"Functor" (forall {k1} {k2} (f :: k1 -> k2).
(Functor f, Testable k1, Testable k2) =>
(forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r)
-> Property ()
forall (f :: k1 -> k2).
(Functor f, Testable k1, Testable k2) =>
(forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r)
-> Property ()
propFunctor @f (\ @a TestOb (f a) => r
r -> forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r
withTestObF @a r
TestOb (f a) => r
r))

testFunctor_
  :: forall {k1} {k2} (f :: k1 -> k2)
   . (Functor.Functor f, Testable k1, Testable k2, forall (a :: k1). (TestOb a) => TestOb' (f a))
  => TestTree
testFunctor_ :: forall {k1} {k2} (f :: k1 -> k2).
(Functor f, Testable k1, Testable k2,
 forall (a :: k1). TestOb a => TestOb' (f a)) =>
TestTree
testFunctor_ = forall {k1} {k2} (f :: k1 -> k2).
(Functor f, Testable k1, Testable k2) =>
(forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r)
-> TestTree
forall (f :: k1 -> k2).
(Functor f, Testable k1, Testable k2) =>
(forall (a :: k1) r. TestOb a => (TestOb (f a) => r) -> r)
-> TestTree
testFunctor @f (\TestOb (f a) => r
r -> r
TestOb (f a) => r
r)

-- | The 'M.MonoidalProfunctor' laws of @p@ stated as code ('Laws.ProLaws' 'M.MonoidalProfunctor').
-- The witnesses say how 'TestOb' is closed under the tensor of @j@ and of @k@.
testMonoidalProfunctor
  :: forall {j} {k} (p :: j +-> k)
   . (M.MonoidalProfunctor p, TestableProfunctor p, TestOb (M.Unit :: j), TestOb (M.Unit :: k))
  => WithTestOb2 j
  -> WithTestOb2 k
  -> TestTree
testMonoidalProfunctor :: forall {j} {k} (p :: j +-> k).
(MonoidalProfunctor p, TestableProfunctor p, TestOb Unit,
 TestOb Unit) =>
WithTestOb2 j -> WithTestOb2 k -> TestTree
testMonoidalProfunctor WithTestOb2 j
withTestOb2J WithTestOb2 k
withTestOb2K =
  forall (csj :: [Type -> Constraint]) (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
forall {j} {k} (csj :: [Type -> Constraint])
       (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
testProLaws @'[CategoryOf, M.Monoidal] @'[CategoryOf, M.Monoidal] @M.MonoidalProfunctor @p
    TestOptions
defaultTestOptions
    Gen (Some i)
forall i. Testable i => Gen (Some i)
genSome
    String
"MonoidalProfunctor"
    (Witness CategoryOf j
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf j
-> Witnesses '[Monoidal] j -> Witnesses '[CategoryOf, Monoidal] j
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestOb2 j -> Witness Monoidal j
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 j
withTestOb2J @a @b r
TestOb (a ** b) => r
r) Witness Monoidal j -> Witnesses '[] j -> Witnesses '[Monoidal] j
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] j
forall k. Witnesses '[] k
WNil)
    (Witness CategoryOf k
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf k
-> Witnesses '[Monoidal] k -> Witnesses '[CategoryOf, Monoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2K @a @b r
TestOb (a ** b) => r
r) Witness Monoidal k -> Witnesses '[] k -> Witnesses '[Monoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

-- | The laws of strength of @p@ for the tensor acting on its own category, stated as code
-- ('Laws.ProLaws' @('Strength.Strong' 'M.Tensor')@). The witness says how 'TestOb' is closed under
-- the tensor.
testMonStrong
  :: forall {k} (p :: k +-> k)
   . (Strength.Strong M.Tensor p, M.Monoidal k, TestableProfunctor p, TestOb (M.Unit :: k))
  => WithTestOb2 k
  -> TestTree
testMonStrong :: forall {k} (p :: k +-> k).
(Strong Tensor p, Monoidal k, TestableProfunctor p, TestOb Unit) =>
WithTestOb2 k -> TestTree
testMonStrong WithTestOb2 k
withTestOb2 =
  forall (csj :: [Type -> Constraint]) (csk :: [Type -> Constraint])
       (cl :: (TESTED csj k +-> TESTED csk k) -> Constraint)
       (p :: k +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj k
-> Witnesses csk k
-> TestTree
forall {j} {k} (csj :: [Type -> Constraint])
       (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
testProLaws @'[CategoryOf, M.Monoidal] @'[CategoryOf, M.Monoidal] @(Strength.Strong M.Tensor) @p
    TestOptions
defaultTestOptions
    Gen (Some i)
forall i. Testable i => Gen (Some i)
genSome
    String
"Strong Tensor"
    Witnesses '[CategoryOf, Monoidal] k
ws
    Witnesses '[CategoryOf, Monoidal] k
ws
  where
    ws :: Witnesses '[CategoryOf, Monoidal] k
ws = Witness CategoryOf k
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf k
-> Witnesses '[Monoidal] k -> Witnesses '[CategoryOf, Monoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r) Witness Monoidal k -> Witnesses '[] k -> Witnesses '[Monoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil

testMonStrong_
  :: forall {k} (p :: k +-> k). (Strength.Strong M.Tensor p, M.Monoidal k, TestableProfunctor p, TestObIsOb k) => TestTree
testMonStrong_ :: forall {k} (p :: k +-> k).
(Strong Tensor p, Monoidal k, TestableProfunctor p,
 TestObIsOb k) =>
TestTree
testMonStrong_ = forall {k} (p :: k +-> k).
(Strong Tensor p, Monoidal k, TestableProfunctor p, TestOb Unit) =>
WithTestOb2 k -> TestTree
forall (p :: k +-> k).
(Strong Tensor p, Monoidal k, TestableProfunctor p, TestOb Unit) =>
WithTestOb2 k -> TestTree
testMonStrong @p (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The laws of costrength of @p@ for the tensor acting on its own category, stated as code
-- ('Laws.ProLaws' @('Strength.Costrong' 'M.Tensor')@). The witness says how 'TestOb' is closed
-- under the tensor.
testMonCostrong
  :: forall {k} (p :: k +-> k)
   . (Strength.Costrong M.Tensor p, M.Monoidal k, TestableProfunctor p, TestOb (M.Unit :: k))
  => WithTestOb2 k
  -> TestTree
testMonCostrong :: forall {k} (p :: k +-> k).
(Costrong Tensor p, Monoidal k, TestableProfunctor p,
 TestOb Unit) =>
WithTestOb2 k -> TestTree
testMonCostrong WithTestOb2 k
withTestOb2 =
  -- small objects: 'coact tensor' asks for arrows into a tensor of three of them, and a relation
  -- or matrix between such tensors grows with the product of their sizes
  forall (csj :: [Type -> Constraint]) (csk :: [Type -> Constraint])
       (cl :: (TESTED csj k +-> TESTED csk k) -> Constraint)
       (p :: k +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj k
-> Witnesses csk k
-> TestTree
forall {j} {k} (csj :: [Type -> Constraint])
       (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
testProLaws @'[CategoryOf, M.Monoidal] @'[CategoryOf, M.Monoidal] @(Strength.Costrong M.Tensor) @p
    TestOptions
defaultTestOptions
    Gen (Some i)
forall i. Testable i => Gen (Some i)
genSomeSmall
    String
"Costrong Tensor"
    Witnesses '[CategoryOf, Monoidal] k
ws
    Witnesses '[CategoryOf, Monoidal] k
ws
  where
    ws :: Witnesses '[CategoryOf, Monoidal] k
ws = Witness CategoryOf k
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf k
-> Witnesses '[Monoidal] k -> Witnesses '[CategoryOf, Monoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r) Witness Monoidal k -> Witnesses '[] k -> Witnesses '[Monoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil

testMonCostrong_
  :: forall {k} (p :: k +-> k). (Strength.Costrong M.Tensor p, M.Monoidal k, TestableProfunctor p, TestObIsOb k) => TestTree
testMonCostrong_ :: forall {k} (p :: k +-> k).
(Costrong Tensor p, Monoidal k, TestableProfunctor p,
 TestObIsOb k) =>
TestTree
testMonCostrong_ = forall {k} (p :: k +-> k).
(Costrong Tensor p, Monoidal k, TestableProfunctor p,
 TestOb Unit) =>
WithTestOb2 k -> TestTree
forall (p :: k +-> k).
(Costrong Tensor p, Monoidal k, TestableProfunctor p,
 TestOb Unit) =>
WithTestOb2 k -> TestTree
testMonCostrong @p (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The 'Representable' laws of @p@ stated as code ('Laws.ProLaws' 'Representable'): 'index' and
-- 'tabulate' are inverse and natural. The witness lifts 'TestOb' along @p '%' -@.
testRepresentable
  :: forall {j} {k} (p :: j +-> k)
   . (Representable p, TestableProfunctor p)
  => WithTestObRep j p
  -> TestTree
testRepresentable :: forall {j} {k} (p :: j +-> k).
(Representable p, TestableProfunctor p) =>
WithTestObRep j p -> TestTree
testRepresentable WithTestObRep j p
withTestObRep =
  forall (csj :: [Type -> Constraint]) (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
forall {j} {k} (csj :: [Type -> Constraint])
       (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
testProLaws @'[CategoryOf] @'[CategoryOf, RepresentedBy '[CategoryOf] p] @Representable @p
    TestOptions
defaultTestOptions
    Gen (Some i)
forall i. Testable i => Gen (Some i)
genSome
    String
"Representable"
    (Witness CategoryOf j
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf j
-> Witnesses '[] j -> Witnesses '[CategoryOf] j
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] j
forall k. Witnesses '[] k
WNil)
    (Witness CategoryOf k
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf k
-> Witnesses '[RepresentedBy '[CategoryOf] p] k
-> Witnesses '[CategoryOf, RepresentedBy '[CategoryOf] p] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[CategoryOf] j
-> WithTestObRep j p -> Witness (RepresentedBy '[CategoryOf] p) k
forall (csj :: [Type -> Constraint]) j k (p :: j +-> k) k'.
Witnesses csj j
-> WithTestObRep j p -> Witness (RepresentedBy csj p) k'
RepresentedW (Witness CategoryOf j
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf j
-> Witnesses '[] j -> Witnesses '[CategoryOf] j
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] j
forall k. Witnesses '[] k
WNil) (\ @b TestOb (p % a) => r
r -> WithTestObRep j p
withTestObRep @b r
TestOb (p % a) => r
r) Witness (RepresentedBy '[CategoryOf] p) k
-> Witnesses '[] k -> Witnesses '[RepresentedBy '[CategoryOf] p] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

testRepresentable_ :: forall {j} {k} (p :: j +-> k). (Representable p, TestableProfunctor p, TestObIsOb k) => TestTree
testRepresentable_ :: forall {j} {k} (p :: j +-> k).
(Representable p, TestableProfunctor p, TestObIsOb k) =>
TestTree
testRepresentable_ = forall {j} {k} (p :: j +-> k).
(Representable p, TestableProfunctor p) =>
WithTestObRep j p -> TestTree
forall (p :: j +-> k).
(Representable p, TestableProfunctor p) =>
WithTestObRep j p -> TestTree
testRepresentable @p (\ @b TestOb (p % a) => r
r -> forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: j) r.
(Representable p, Ob a) =>
(Ob (p % a) => r) -> r
forall (p :: j +-> k) (a :: j) r.
(Representable p, Ob a) =>
(Ob (p % a) => r) -> r
withObRep @p @b r
Ob (p % a) => r
TestOb (p % a) => r
r))

-- | The 'Corepresentable' laws of @p@ stated as code ('Laws.ProLaws' 'Corepresentable'): 'coindex'
-- and 'cotabulate' are inverse and natural. The witness lifts 'TestOb' along @p '%%' -@.
testCorepresentable
  :: forall {j} {k} (p :: j +-> k)
   . (Corepresentable p, TestableProfunctor p)
  => WithTestObCorep k p
  -> TestTree
testCorepresentable :: forall {j} {k} (p :: j +-> k).
(Corepresentable p, TestableProfunctor p) =>
WithTestObCorep k p -> TestTree
testCorepresentable WithTestObCorep k p
withTestObCorep =
  forall (csj :: [Type -> Constraint]) (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
forall {j} {k} (csj :: [Type -> Constraint])
       (csk :: [Type -> Constraint])
       (cl :: (TESTED csj j +-> TESTED csk k) -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions
-> (forall i. Testable i => Gen (Some i))
-> String
-> Witnesses csj j
-> Witnesses csk k
-> TestTree
testProLaws @'[CategoryOf, CorepresentedBy '[CategoryOf] p] @'[CategoryOf] @Corepresentable @p
    TestOptions
defaultTestOptions
    Gen (Some i)
forall i. Testable i => Gen (Some i)
genSome
    String
"Corepresentable"
    (Witness CategoryOf j
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf j
-> Witnesses '[CorepresentedBy '[CategoryOf] p] j
-> Witnesses '[CategoryOf, CorepresentedBy '[CategoryOf] p] j
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[CategoryOf] k
-> WithTestObCorep k p
-> Witness (CorepresentedBy '[CategoryOf] p) j
forall (csk :: [Type -> Constraint]) j k (p :: j +-> k) j'.
Witnesses csk k
-> WithTestObCorep k p -> Witness (CorepresentedBy csk p) j'
CorepresentedW (Witness CategoryOf k
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf k
-> Witnesses '[] k -> Witnesses '[CategoryOf] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil) (\ @a TestOb (p %% a) => r
r -> WithTestObCorep k p
withTestObCorep @a r
TestOb (p %% a) => r
r) Witness (CorepresentedBy '[CategoryOf] p) j
-> Witnesses '[] j
-> Witnesses '[CorepresentedBy '[CategoryOf] p] j
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] j
forall k. Witnesses '[] k
WNil)
    (Witness CategoryOf k
forall k. Witness CategoryOf k
CategoryW Witness CategoryOf k
-> Witnesses '[] k -> Witnesses '[CategoryOf] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

testCorepresentable_
  :: forall {j} {k} (p :: j +-> k)
   . (Corepresentable p, TestableProfunctor p, TestObIsOb j)
  => TestTree
testCorepresentable_ :: forall {j} {k} (p :: j +-> k).
(Corepresentable p, TestableProfunctor p, TestObIsOb j) =>
TestTree
testCorepresentable_ = forall {j} {k} (p :: j +-> k).
(Corepresentable p, TestableProfunctor p) =>
WithTestObCorep k p -> TestTree
forall (p :: j +-> k).
(Corepresentable p, TestableProfunctor p) =>
WithTestObCorep k p -> TestTree
testCorepresentable @p (\ @a TestOb (p %% a) => r
r -> forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall {k1} {k2} (p :: k1 +-> k2) (a :: k2) r.
(Corepresentable p, Ob a) =>
(Ob (p %% a) => r) -> r
forall (p :: j +-> k) (a :: k) r.
(Corepresentable p, Ob a) =>
(Ob (p %% a) => r) -> r
withObCorep @p @a r
Ob (p %% a) => r
TestOb (p %% a) => r
r))

-- | The 'Promonad' laws of @p@ stated as code: 'id' is a unit for composition, which is
-- associative, and both are natural.
testPromonad :: forall {k} (p :: k +-> k). (Promonad p, TestableProfunctor p) => TestTree
testPromonad :: forall {k} (p :: k +-> k).
(Promonad p, TestableProfunctor p) =>
TestTree
testPromonad =
  forall {j} {k}
       (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
forall (cl :: (TESTED '[CategoryOf] k +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: k +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
testCategoryProLaws @Promonad @p TestOptions
defaultTestOptions String
"Promonad"

-- | The 'Promonad.Procomonad' laws of @p@ stated as code: 'Promonad.proextract' is natural and a
-- counit for 'Promonad.produplicate'. The middle object of 'Promonad.produplicate' is only known to
-- be an object, hence 'TestObIsOb'.
testProcomonad :: forall {k} (p :: k +-> k). (Promonad.Procomonad p, TestableProfunctor p, TestObIsOb k) => TestTree
testProcomonad :: forall {k} (p :: k +-> k).
(Procomonad p, TestableProfunctor p, TestObIsOb k) =>
TestTree
testProcomonad =
  forall {j} {k}
       (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
forall (cl :: (TESTED '[CategoryOf] k +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: k +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
testCategoryProLaws @Promonad.Procomonad @p TestOptions
defaultTestOptions String
"Procomonad"

-- | The zigzag laws of the adjunction between @p@ and @q@ stated as code, for elements of each:
-- 'Laws.ProLaws' @('Adj.LeftProadjoint' q)@ and @('Adj.Proadjunction' p)@. The middle object of
-- the unit is only known to be an object, hence 'TestObIsOb'.
testProadjunction
  :: forall {j} {k} (p :: j +-> k) (q :: k +-> j)
   . (Adj.Proadjunction p q, TestableProfunctor p, TestableProfunctor q, TestObIsOb j, TestObIsOb k)
  => TestTree
testProadjunction :: forall {j} {k} (p :: j +-> k) (q :: k +-> j).
(Proadjunction p q, TestableProfunctor p, TestableProfunctor q,
 TestObIsOb j, TestObIsOb k) =>
TestTree
testProadjunction =
  String -> [TestTree] -> TestTree
testGroup
    String
"Proadjunction"
    [ forall {j} {k}
       (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
forall (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
testCategoryProLaws @(Adj.LeftProadjoint (TestedP q)) @p TestOptions
defaultTestOptions String
"left adjoint"
    , forall {j} {k}
       (cl :: (TESTED '[CategoryOf] j +-> TESTED '[CategoryOf] k)
              -> Constraint)
       (p :: j +-> k).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
forall (cl :: (TESTED '[CategoryOf] k +-> TESTED '[CategoryOf] j)
              -> Constraint)
       (p :: k +-> j).
(ProLaws cl, TestableProfunctor p, cl (TestedP p)) =>
TestOptions -> String -> TestTree
testCategoryProLaws @(Adj.Proadjunction (TestedP p)) @q TestOptions
defaultTestOptions String
"right adjoint"
    ]

-- | Check the adjunction laws of an 'Adjunction' @p@. An adjunction here is a profunctor that is
-- both 'Representable' and 'Corepresentable', with left adjoint @L = p '%%' -@ and right adjoint
-- @R = p '%' -@. It carries no laws of its own beyond theirs ('leftAdjunct'\/'rightAdjunct' are just
-- @'index' '.' 'cotabulate'@ and @'coindex' '.' 'tabulate'@), so this groups 'testCorepresentable'
-- (for @L@) and 'testRepresentable' (for @R@). The two witnesses lift 'TestOb' along @L@ and @R@
-- respectively.
testAdjunction
  :: forall {j} {k} (p :: j +-> k)
   . (Adjunction p, TestableProfunctor p)
  => WithTestObCorep k p
  -> WithTestObRep j p
  -> TestTree
testAdjunction :: forall {j} {k} (p :: j +-> k).
(Adjunction p, TestableProfunctor p) =>
WithTestObCorep k p -> WithTestObRep j p -> TestTree
testAdjunction WithTestObCorep k p
withTestObL WithTestObRep j p
withTestObR =
  String -> [TestTree] -> TestTree
testGroup
    String
"Adjunction"
    [ forall {j} {k} (p :: j +-> k).
(Corepresentable p, TestableProfunctor p) =>
WithTestObCorep k p -> TestTree
forall (p :: j +-> k).
(Corepresentable p, TestableProfunctor p) =>
WithTestObCorep k p -> TestTree
testCorepresentable @p (\ @a TestOb (p %% a) => r
r -> WithTestObCorep k p
withTestObL @a r
TestOb (p %% a) => r
r)
    , forall {j} {k} (p :: j +-> k).
(Representable p, TestableProfunctor p) =>
WithTestObRep j p -> TestTree
forall (p :: j +-> k).
(Representable p, TestableProfunctor p) =>
WithTestObRep j p -> TestTree
testRepresentable @p (\ @b TestOb (p % a) => r
r -> WithTestObRep j p
withTestObR @b r
TestOb (p % a) => r
r)
    ]

testAdjunction_
  :: forall {j} {k} (p :: j +-> k)
   . (Adjunction p, TestableProfunctor p, TestObIsOb j, TestObIsOb k)
  => TestTree
testAdjunction_ :: forall {j} {k} (p :: j +-> k).
(Adjunction p, TestableProfunctor p, TestObIsOb j, TestObIsOb k) =>
TestTree
testAdjunction_ =
  forall {j} {k} (p :: j +-> k).
(Adjunction p, TestableProfunctor p) =>
WithTestObCorep k p -> WithTestObRep j p -> TestTree
forall (p :: j +-> k).
(Adjunction p, TestableProfunctor p) =>
WithTestObCorep k p -> WithTestObRep j p -> TestTree
testAdjunction @p
    (\ @a TestOb (p %% a) => r
r -> forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (forall {k1} {k2} (p :: k1 +-> k2) (a :: k2) r.
(Corepresentable p, Ob a) =>
(Ob (p %% a) => r) -> r
forall (p :: j +-> k) (a :: k) r.
(Corepresentable p, Ob a) =>
(Ob (p %% a) => r) -> r
withObCorep @p @a (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 @(p %% a) r
TestOb (p %% a) => r
r)))
    (\ @b TestOb (p % a) => r
r -> forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @b (forall {j} {k} (p :: j +-> k) (a :: j) r.
(Representable p, Ob a) =>
(Ob (p % a) => r) -> r
forall (p :: j +-> k) (a :: j) r.
(Representable p, Ob a) =>
(Ob (p % a) => r) -> r
withObRep @p @b (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 @(p % b) r
TestOb (p % a) => r
r)))

-- * Limits and colimits

-- | Every arrow into the 'Terminal.TerminalObject' is 'Terminal.terminate', from
-- @'Proarrow.Tools.Laws.Laws' '['Terminal.HasTerminalObject']@.
testTerminalObject
  :: forall k
   . (Testable k, Terminal.HasTerminalObject k, TestOb (Terminal.TerminalObject :: k))
  => TestTree
testTerminalObject :: forall k.
(Testable k, HasTerminalObject k, TestOb TerminalObject) =>
TestTree
testTerminalObject = forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[Terminal.HasTerminalObject] String
"Terminal object" (forall k. Witness HasTerminalObject k
TerminalW @k Witness HasTerminalObject k
-> Witnesses '[] k -> Witnesses '[HasTerminalObject] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

-- | Every arrow out of the 'Initial.InitialObject' is 'Initial.initiate', from
-- @'Proarrow.Tools.Laws.Laws' '['Initial.HasInitialObject']@.
testInitialObject :: forall k. (Testable k, Initial.HasInitialObject k, TestOb (Initial.InitialObject :: k)) => TestTree
testInitialObject :: forall k.
(Testable k, HasInitialObject k, TestOb InitialObject) =>
TestTree
testInitialObject = forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[Initial.HasInitialObject] String
"Initial object" (forall k. Witness HasInitialObject k
InitialW @k Witness HasInitialObject k
-> Witnesses '[] k -> Witnesses '[HasInitialObject] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

-- | The universal property of the binary product, from
-- @'Proarrow.Tools.Laws.Laws' '['BinaryProduct.HasBinaryProducts']@: the projections recover the
-- components of @f '&&&' g@, pairing commutes with precomposition, and pairing the projections is
-- the identity.
testBinaryProducts :: forall k. (Testable k, BinaryProduct.HasBinaryProducts k) => WithTestObProd k -> TestTree
testBinaryProducts :: forall k.
(Testable k, HasBinaryProducts k) =>
WithTestObProd k -> TestTree
testBinaryProducts WithTestObProd k
withTestObProd =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[BinaryProduct.HasBinaryProducts] String
"Binary products" (WithTestObProd k -> Witness HasBinaryProducts k
forall k. WithTestObProd k -> Witness HasBinaryProducts k
ProductsW (\ @a @b TestOb (a && b) => r
r -> WithTestObProd k
withTestObProd @a @b r
TestOb (a && b) => r
r) Witness HasBinaryProducts k
-> Witnesses '[] k -> Witnesses '[HasBinaryProducts] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

testBinaryProducts_ :: forall k. (Testable k, BinaryProduct.HasBinaryProducts k, TestObIsOb k) => TestTree
testBinaryProducts_ :: forall k.
(Testable k, HasBinaryProducts k, TestObIsOb k) =>
TestTree
testBinaryProducts_ = forall k.
(Testable k, HasBinaryProducts k) =>
WithTestObProd k -> TestTree
testBinaryProducts @k (\ @a @b -> WithTestObProd k
forall {k}. (TestObIsOb k, HasBinaryProducts k) => WithTestObProd k
withTestObProdDef @a @b)

-- | The universal property of the binary coproduct, dual to 'testBinaryProducts', from
-- @'Proarrow.Tools.Laws.Laws' '['BinaryCoproduct.HasBinaryCoproducts']@.
testBinaryCoproducts :: forall k. (Testable k, BinaryCoproduct.HasBinaryCoproducts k) => WithTestObCoprod k -> TestTree
testBinaryCoproducts :: forall k.
(Testable k, HasBinaryCoproducts k) =>
WithTestObCoprod k -> TestTree
testBinaryCoproducts WithTestObCoprod k
withTestObCoprod =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[BinaryCoproduct.HasBinaryCoproducts]
    String
"Binary coproducts"
    (WithTestObCoprod k -> Witness HasBinaryCoproducts k
forall k. WithTestObCoprod k -> Witness HasBinaryCoproducts k
CoproductsW (\ @a @b TestOb (a || b) => r
r -> WithTestObCoprod k
withTestObCoprod @a @b r
TestOb (a || b) => r
r) Witness HasBinaryCoproducts k
-> Witnesses '[] k -> Witnesses '[HasBinaryCoproducts] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

testBinaryCoproducts_ :: forall k. (Testable k, BinaryCoproduct.HasBinaryCoproducts k, TestObIsOb k) => TestTree
testBinaryCoproducts_ :: forall k.
(Testable k, HasBinaryCoproducts k, TestObIsOb k) =>
TestTree
testBinaryCoproducts_ = forall k.
(Testable k, HasBinaryCoproducts k) =>
WithTestObCoprod k -> TestTree
testBinaryCoproducts @k (\ @a @b -> WithTestObCoprod k
forall {k}.
(TestObIsOb k, HasBinaryCoproducts k) =>
WithTestObCoprod k
withTestObCoprodDef @a @b)

-- | Check that composing with an arrow /reflects/ equality: the composites agree exactly when the
-- two arrows already did. @eqComposed@ is the caller\'s comparison of the composites (a
-- conjunction, where two projections have to be checked together), and @desc@ names it for the
-- failure message.
--
-- This is the mono half of an equalizer, the epi half of a coequalizer, and the jointly-monic and
-- jointly-epic halves of a pullback and a pushout.
propReflectsEq :: (TestingEqShow x) => String -> String -> Bool -> x -> x -> Property ()
propReflectsEq :: forall x.
TestingEqShow x =>
String -> String -> Bool -> x -> x -> Property ()
propReflectsEq String
label String
desc Bool
eqComposed x
k1 x
k2 = do
  eqDirect <- x -> x -> Property Bool
forall a. TestingEqShow a => a -> a -> Property Bool
eqP x
k1 x
k2
  check
    ("Failed " ++ label ++ ": (" ++ desc ++ ") = " ++ show eqComposed ++ " but (k1 == k2) = " ++ show eqDirect)
    (eqComposed == eqDirect)

-- | Checks the equalizer laws: the equalizer arrow @e@ equalizes @f@ and @g@; any @h@ that factors
-- through @e@ (generated as @e . p@) is recovered by 'Equalizer.factorEqualizer'; and @e@ is mono.
--
-- The equalizer object is not computed by a type family, so its 'Ob' comes from the arrow via
-- 'Objs', and @withTestOb@ only has to bridge that one 'Ob' to 'TestOb'.
testEqualizers :: forall k. (Testable k, Equalizer.HasEqualizers k) => WithTestOb k -> TestTree
testEqualizers :: forall k. (Testable k, HasEqualizers k) => WithTestOb k -> TestTree
testEqualizers WithTestOb k
withTestOb = String -> Property () -> TestTree
testProperty String
"Equalizers" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  Some @b <- genOb @k
  f <- genNamed @(a ~> b) "f"
  g <- genNamed @(a ~> b) "g"
  Equalizer.equalize f g \ @e ee :: e ~> x
ee@e ~> x
Objs -> WithTestOb k
withTestOb @e do
    String -> String -> (e ~> x) -> String -> (e ~> x) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"equalizing" String
"f . e" (x ~> x
f (x ~> x) -> (e ~> x) -> e ~> x
forall (b :: k) (c :: k) (a :: k). (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
. e ~> x
ee) String
"g . e" (x ~> x
g (x ~> x) -> (e ~> x) -> e ~> x
forall (b :: k) (c :: k) (a :: k). (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
. e ~> x
ee)
    Some @z <- forall k. Testable k => Property (Some k)
genOb @k
    p <- genNamed @(z ~> e) "p"
    let h = e ~> x
ee (e ~> x) -> (x ~> e) -> x ~> x
forall (b :: k) (c :: k) (a :: k). (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
. x ~> e
p
        factored = (e ~> x) -> (x ~> x) -> x ~> e
forall (e :: k) (x :: k) (e' :: k).
(e ~> x) -> (e' ~> x) -> e' ~> e
forall k (e :: k) (x :: k) (e' :: k).
HasEqualizers k =>
(e ~> x) -> (e' ~> x) -> e' ~> e
Equalizer.factorEqualizer e ~> x
ee x ~> x
h
    testEq "factorization" "e . factored" (ee . factored) "h" h
    -- The half the constructed @h@ cannot reach: an /arbitrary/ arrow that happens to equalize
    -- must factor too. Without it an undersized equalizer (one keeping too few elements)
    -- satisfies everything above, since every arrow it is ever handed was built through it.
    m <- genNamed @(z ~> a) "m"
    equalizes <- eqP (f . m) (g . m)
    when equalizes $
      testEq "existence" "e . factorEqualizer e m" (ee . Equalizer.factorEqualizer ee m) "m" m
    k1 <- genNamed @(z ~> e) "k1"
    k2 <- genNamed @(z ~> e) "k2"
    eqComposed <- eqP (ee . k1) (ee . k2)
    propReflectsEq "mono" "e . k1 == e . k2" eqComposed k1 k2

testEqualizers_ :: forall k. (Testable k, Equalizer.HasEqualizers k, TestObIsOb k) => TestTree
testEqualizers_ :: forall k. (Testable k, HasEqualizers k, TestObIsOb k) => TestTree
testEqualizers_ = forall k. (Testable k, HasEqualizers k) => WithTestOb k -> TestTree
testEqualizers @k (\TestOb a => r
r -> r
TestOb a => r
r)

-- | Checks the coequalizer laws, dual to 'testEqualizers': the coequalizer arrow @c@ coequalizes @f@
-- and @g@; any @h@ that factors through @c@ (built here as @p . c@ for an arbitrary @p@, so the
-- precondition holds by construction) is correctly recovered by 'Coequalizer.factorCoequalizer'; and
-- @c@ is epi (post-composing with it on the right reflects equality).
testCoequalizers :: forall k. (Testable k, Coequalizer.HasCoequalizers k) => WithTestOb k -> TestTree
testCoequalizers :: forall k.
(Testable k, HasCoequalizers k) =>
WithTestOb k -> TestTree
testCoequalizers WithTestOb k
withTestOb = String -> Property () -> TestTree
testProperty String
"Coequalizers" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  Some @b <- genOb @k
  f <- genNamed @(a ~> b) "f"
  g <- genNamed @(a ~> b) "g"
  Coequalizer.coequalize f g \ @c cq :: x ~> c
cq@x ~> c
Objs -> WithTestOb k
withTestOb @c do
    String -> String -> (x ~> c) -> String -> (x ~> c) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"coequalizing" String
"c . f" (x ~> c
cq (x ~> c) -> (x ~> x) -> x ~> c
forall (b :: k) (c :: k) (a :: k). (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
. x ~> x
f) String
"c . g" (x ~> c
cq (x ~> c) -> (x ~> x) -> x ~> c
forall (b :: k) (c :: k) (a :: k). (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
. x ~> x
g)
    Some @z <- forall k. Testable k => Property (Some k)
genOb @k
    p <- genNamed @(c ~> z) "p"
    let h = c ~> x
p (c ~> x) -> (x ~> c) -> x ~> x
forall (b :: k) (c :: k) (a :: k). (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
. x ~> c
cq
        factored = (x ~> c) -> (x ~> x) -> c ~> x
forall (c :: k) (x :: k) (c' :: k).
(x ~> c) -> (x ~> c') -> c ~> c'
forall k (c :: k) (x :: k) (c' :: k).
HasCoequalizers k =>
(x ~> c) -> (x ~> c') -> c ~> c'
Coequalizer.factorCoequalizer x ~> c
cq x ~> x
h
    testEq "factorization" "factored . c" (factored . cq) "h" h
    -- As in 'testEqualizers': an arbitrary arrow that coequalizes must factor, not only one
    -- built by composing through @cq@.
    m <- genNamed @(b ~> z) "m"
    coequalizes <- eqP (m . f) (m . g)
    when coequalizes $
      testEq "existence" "factorCoequalizer c m . c" (Coequalizer.factorCoequalizer cq m . cq) "m" m
    k1 <- genNamed @(c ~> z) "k1"
    k2 <- genNamed @(c ~> z) "k2"
    eqComposed <- eqP (k1 . cq) (k2 . cq)
    propReflectsEq "epi" "k1 . c == k2 . c" eqComposed k1 k2

testCoequalizers_ :: forall k. (Testable k, Coequalizer.HasCoequalizers k, TestObIsOb k) => TestTree
testCoequalizers_ :: forall k. (Testable k, HasCoequalizers k, TestObIsOb k) => TestTree
testCoequalizers_ = forall k.
(Testable k, HasCoequalizers k) =>
WithTestOb k -> TestTree
testCoequalizers @k (\TestOb a => r
r -> r
TestOb a => r
r)

-- | Checks the pullback laws: the pullback cone commutes; it's jointly monic (composing with both
-- legs at once reflects equality); and any compatible cone (built here as @(p1 . j, p2 . j)@ for an
-- arbitrary @j@, so compatibility holds by construction) is correctly recovered by
-- 'Pullback.factorPullback'.
testPullbacks :: forall k. (Testable k, Pullback.HasPullbacks k) => WithTestOb k -> TestTree
testPullbacks :: forall k. (Testable k, HasPullbacks k) => WithTestOb k -> TestTree
testPullbacks WithTestOb k
withTestOb = String -> Property () -> TestTree
testProperty String
"Pullbacks" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
  Some @o <- forall k. Testable k => Property (Some k)
genOb @k
  Some @a <- genOb @k
  Some @b <- genOb @k
  f <- genNamed @(a ~> o) "f"
  g <- genNamed @(b ~> o) "g"
  Pullback.pullback f g \ @p p1 :: p ~> x
p1@p ~> x
Objs p ~> x
p2 -> WithTestOb k
withTestOb @p do
    String -> String -> (p ~> x) -> String -> (p ~> x) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"commutes" String
"f . p1" (x ~> x
f (x ~> x) -> (p ~> x) -> p ~> x
forall (b :: k) (c :: k) (a :: k). (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
. p ~> x
p1) String
"g . p2" (x ~> x
g (x ~> x) -> (p ~> x) -> p ~> x
forall (b :: k) (c :: k) (a :: k). (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
. p ~> x
p2)
    Some @z <- forall k. Testable k => Property (Some k)
genOb @k
    k1' <- genNamed @(z ~> p) "k1"
    k2' <- genNamed @(z ~> p) "k2"
    eq1 <- eqP (p1 . k1') (p1 . k2')
    eq2 <- eqP (p2 . k1') (p2 . k2')
    propReflectsEq "jointly monic" "p1 . k1 == p1 . k2 && p2 . k1 == p2 . k2" (eq1 && eq2) k1' k2'
    j <- genNamed @(z ~> p) "j"
    let k1 = p ~> x
p1 (p ~> x) -> (x ~> p) -> x ~> x
forall (b :: k) (c :: k) (a :: k). (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
. x ~> p
j
        k2 = p ~> x
p2 (p ~> x) -> (x ~> p) -> x ~> x
forall (b :: k) (c :: k) (a :: k). (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
. x ~> p
j
        factored = (p ~> x) -> (p ~> x) -> (x ~> x) -> (x ~> x) -> x ~> p
forall (a :: k) (b :: k) (p :: k) (q :: k).
(p ~> a) -> (p ~> b) -> (q ~> a) -> (q ~> b) -> q ~> p
forall k (a :: k) (b :: k) (p :: k) (q :: k).
HasPullbacks k =>
(p ~> a) -> (p ~> b) -> (q ~> a) -> (q ~> b) -> q ~> p
Pullback.factorPullback p ~> x
p1 p ~> x
p2 x ~> x
k1 x ~> x
k2
    testEq "factorization (1)" "p1 . factored" (p1 . factored) "k1" k1
    testEq "factorization (2)" "p2 . factored" (p2 . factored) "k2" k2
    -- And the half those cannot reach: an arbitrary commuting cone must factor too.
    x <- genNamed @(z ~> a) "x"
    y <- genNamed @(z ~> b) "y"
    commutes <- eqP (f . x) (g . y)
    when commutes $ do
      let fac = (p ~> x) -> (p ~> x) -> (x ~> x) -> (x ~> x) -> x ~> p
forall (a :: k) (b :: k) (p :: k) (q :: k).
(p ~> a) -> (p ~> b) -> (q ~> a) -> (q ~> b) -> q ~> p
forall k (a :: k) (b :: k) (p :: k) (q :: k).
HasPullbacks k =>
(p ~> a) -> (p ~> b) -> (q ~> a) -> (q ~> b) -> q ~> p
Pullback.factorPullback p ~> x
p1 p ~> x
p2 x ~> x
x x ~> x
y
      testEq "existence (1)" "p1 . factorPullback p1 p2 x y" (p1 . fac) "x" x
      testEq "existence (2)" "p2 . factorPullback p1 p2 x y" (p2 . fac) "y" y

testPullbacks_ :: forall k. (Testable k, Pullback.HasPullbacks k, TestObIsOb k) => TestTree
testPullbacks_ :: forall k. (Testable k, HasPullbacks k, TestObIsOb k) => TestTree
testPullbacks_ = forall k. (Testable k, HasPullbacks k) => WithTestOb k -> TestTree
testPullbacks @k (\TestOb a => r
r -> r
TestOb a => r
r)

-- | Checks the pushout laws, dual to 'testPullbacks': the pushout cocone commutes; it's jointly epic
-- (post-composing with both legs at once reflects equality); and any compatible cocone (built here as
-- @(j . p1, j . p2)@ for an arbitrary @j@, so compatibility holds by construction) is correctly
-- recovered by 'Pushout.factorPushout'.
testPushouts :: forall k. (Testable k, Pushout.HasPushouts k) => WithTestOb k -> TestTree
testPushouts :: forall k. (Testable k, HasPushouts k) => WithTestOb k -> TestTree
testPushouts WithTestOb k
withTestOb = String -> Property () -> TestTree
testProperty String
"Pushouts" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
  Some @o <- forall k. Testable k => Property (Some k)
genOb @k
  Some @a <- genOb @k
  Some @b <- genOb @k
  f <- genNamed @(o ~> a) "f"
  g <- genNamed @(o ~> b) "g"
  Pushout.pushout f g \ @p p1 :: x ~> p
p1@x ~> p
Objs x ~> p
p2 -> WithTestOb k
withTestOb @p do
    String -> String -> (x ~> p) -> String -> (x ~> p) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"commutes" String
"p1 . f" (x ~> p
p1 (x ~> p) -> (x ~> x) -> x ~> p
forall (b :: k) (c :: k) (a :: k). (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
. x ~> x
f) String
"p2 . g" (x ~> p
p2 (x ~> p) -> (x ~> x) -> x ~> p
forall (b :: k) (c :: k) (a :: k). (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
. x ~> x
g)
    Some @z <- forall k. Testable k => Property (Some k)
genOb @k
    k1' <- genNamed @(p ~> z) "k1"
    k2' <- genNamed @(p ~> z) "k2"
    eq1 <- eqP (k1' . p1) (k2' . p1)
    eq2 <- eqP (k1' . p2) (k2' . p2)
    propReflectsEq "jointly epic" "k1 . p1 == k2 . p1 && k1 . p2 == k2 . p2" (eq1 && eq2) k1' k2'
    j <- genNamed @(p ~> z) "j"
    let k1 = p ~> x
j (p ~> x) -> (x ~> p) -> x ~> x
forall (b :: k) (c :: k) (a :: k). (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
. x ~> p
p1
        k2 = p ~> x
j (p ~> x) -> (x ~> p) -> x ~> x
forall (b :: k) (c :: k) (a :: k). (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
. x ~> p
p2
        factored = (x ~> p) -> (x ~> p) -> (x ~> x) -> (x ~> x) -> p ~> x
forall (a :: k) (b :: k) (p :: k) (q :: k).
(a ~> p) -> (b ~> p) -> (a ~> q) -> (b ~> q) -> p ~> q
forall k (a :: k) (b :: k) (p :: k) (q :: k).
HasPushouts k =>
(a ~> p) -> (b ~> p) -> (a ~> q) -> (b ~> q) -> p ~> q
Pushout.factorPushout x ~> p
p1 x ~> p
p2 x ~> x
k1 x ~> x
k2
    testEq "factorization (1)" "factored . p1" (factored . p1) "k1" k1
    testEq "factorization (2)" "factored . p2" (factored . p2) "k2" k2
    -- And the half those cannot reach: an arbitrary commuting cocone must factor too.
    x <- genNamed @(a ~> z) "x"
    y <- genNamed @(b ~> z) "y"
    commutes <- eqP (x . f) (y . g)
    when commutes $ do
      let fac = (x ~> p) -> (x ~> p) -> (x ~> x) -> (x ~> x) -> p ~> x
forall (a :: k) (b :: k) (p :: k) (q :: k).
(a ~> p) -> (b ~> p) -> (a ~> q) -> (b ~> q) -> p ~> q
forall k (a :: k) (b :: k) (p :: k) (q :: k).
HasPushouts k =>
(a ~> p) -> (b ~> p) -> (a ~> q) -> (b ~> q) -> p ~> q
Pushout.factorPushout x ~> p
p1 x ~> p
p2 x ~> x
x x ~> x
y
      testEq "existence (1)" "factorPushout p1 p2 x y . p1" (fac . p1) "x" x
      testEq "existence (2)" "factorPushout p1 p2 x y . p2" (fac . p2) "y" y

testPushouts_ :: forall k. (Testable k, Pushout.HasPushouts k, TestObIsOb k) => TestTree
testPushouts_ :: forall k. (Testable k, HasPushouts k, TestObIsOb k) => TestTree
testPushouts_ = forall k. (Testable k, HasPushouts k) => WithTestOb k -> TestTree
testPushouts @k (\TestOb a => r
r -> r
TestOb a => r
r)

-- | Checks the epi-mono factorization laws: 'Topos.factorize' splits @f@ as @m . e@ through an
-- image object, with @e@ epi and @m@ mono.
--
-- As with 'testEqualizers' the image object is revealed at runtime rather than computed by a type
-- family, so @withTestOb@ bridges its recovered 'Ob' to 'TestOb'. The epi and mono halves are the
-- two directions of 'propReflectsEq': composing on the right with @e@, and on the left with @m@.
testEpiMonoFactorization
  :: forall k. (Testable k, Topos.HasEpiMonoFactorization k) => WithTestOb k -> TestTree
testEpiMonoFactorization :: forall k.
(Testable k, HasEpiMonoFactorization k) =>
WithTestOb k -> TestTree
testEpiMonoFactorization WithTestOb k
withTestOb = String -> Property () -> TestTree
testProperty String
"Epi-mono factorization" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  Some @b <- genOb @k
  f <- genNamed @(a ~> b) "f"
  case Topos.factorize f of
    (:.:) @x e :: Hom k x b
e@Hom k x b
Objs Hom k b x
m -> WithTestOb k
withTestOb @x do
      String -> String -> (x ~> x) -> String -> (x ~> x) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"factorization" String
"m . e" (Hom k b x
m Hom k b x -> Hom k x b -> x ~> x
forall (b :: k) (c :: k) (a :: k). (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
. Hom k x b
e) String
"f" x ~> x
f
      Some @z <- forall k. Testable k => Property (Some k)
genOb @k
      k1 <- genNamed @(x ~> z) "k1"
      k2 <- genNamed @(x ~> z) "k2"
      eqEpi <- eqP (k1 . e) (k2 . e)
      propReflectsEq "epi" "k1 . e == k2 . e" eqEpi k1 k2
      j1 <- genNamed @(z ~> x) "k1"
      j2 <- genNamed @(z ~> x) "k2"
      eqMono <- eqP (m . j1) (m . j2)
      propReflectsEq "mono" "m . k1 == m . k2" eqMono j1 j2

testEpiMonoFactorization_
  :: forall k. (Testable k, Topos.HasEpiMonoFactorization k, TestObIsOb k) => TestTree
testEpiMonoFactorization_ :: forall k.
(Testable k, HasEpiMonoFactorization k, TestObIsOb k) =>
TestTree
testEpiMonoFactorization_ = forall k.
(Testable k, HasEpiMonoFactorization k) =>
WithTestOb k -> TestTree
testEpiMonoFactorization @k (\TestOb a => r
r -> r
TestOb a => r
r)

-- * Monoidal structure

-- | The monoidal laws, from @'Proarrow.Tools.Laws.Laws' '['M.Monoidal']@: the unitors and the
-- associator are natural isomorphisms satisfying the triangle and pentagon identities.
testMonoidal :: forall k. (Testable k, M.Monoidal k, TestOb (M.Unit @k)) => WithTestOb2 k -> TestTree
testMonoidal :: forall k.
(Testable k, Monoidal k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testMonoidal WithTestOb2 k
withTestOb2 = forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[M.Monoidal] String
"Monoidal" (WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r) Witness Monoidal k -> Witnesses '[] k -> Witnesses '[Monoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

testMonoidal_ :: forall k. (Testable k, M.Monoidal k, TestObIsOb k) => TestTree
testMonoidal_ :: forall k. (Testable k, Monoidal k, TestObIsOb k) => TestTree
testMonoidal_ = forall k.
(Testable k, Monoidal k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testMonoidal @k (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The laws of a symmetric monoidal category, from
-- @'Proarrow.Tools.Laws.Laws' 'M.SymMonoidalStructures'@: 'M.swap' is a natural
-- self-inverse satisfying the hexagon identity.
testSymMonoidal :: forall k. (Testable k, M.SymMonoidal k, TestOb (M.Unit @k)) => WithTestOb2 k -> TestTree
testSymMonoidal :: forall k.
(Testable k, SymMonoidal k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testSymMonoidal WithTestOb2 k
withTestOb2 =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @M.SymMonoidalStructures
    String
"Symmetric monoidal"
    (WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r) Witness Monoidal k
-> Witnesses '[SymMonoidal] k -> Witnesses SymMonoidalStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW Witness SymMonoidal k
-> Witnesses '[] k -> Witnesses '[SymMonoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

testSymMonoidal_ :: forall k. (Testable k, M.SymMonoidal k, TestObIsOb k) => TestTree
testSymMonoidal_ :: forall k. (Testable k, SymMonoidal k, TestObIsOb k) => TestTree
testSymMonoidal_ = forall k.
(Testable k, SymMonoidal k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testSymMonoidal @k (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The laws of a copy-discard category: every object is a cocommutative comonoid (the laws of its
-- supply, in "Proarrow.Monoid"), and 'CopyDiscard.copy' and 'CopyDiscard.discard' are that comonoid
-- and respect the tensor ('CopyDiscard.CopyDiscardStructures').
testCopyDiscard
  :: forall k. (Testable k, CopyDiscard.CopyDiscard k, TestOb (M.Unit @k)) => WithTestOb2 k -> TestTree
testCopyDiscard :: forall k.
(Testable k, CopyDiscard k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testCopyDiscard WithTestOb2 k
withTestOb2 =
  String -> [TestTree] -> TestTree
testGroup
    String
"CopyDiscard"
    [ forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[M.Monoidal, Monoid.Supplies Monoid.Comonoid] @k
        String
"Comonoids"
        (Witness Monoidal k
monoidal Witness Monoidal k
-> Witnesses '[Supplies Comonoid] k
-> Witnesses '[Monoidal, Supplies Comonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness (Supplies Comonoid) k
forall k. Witness (Supplies Comonoid) k
ComonoidSupplyW Witness (Supplies Comonoid) k
-> Witnesses '[] k -> Witnesses '[Supplies Comonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)
    , forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[M.Monoidal, M.SymMonoidal, Monoid.Supplies Monoid.CocommutativeComonoid] @k
        String
"Cocommutative comonoids"
        (Witness Monoidal k
monoidal Witness Monoidal k
-> Witnesses '[SymMonoidal, Supplies CocommutativeComonoid] k
-> Witnesses
     '[Monoidal, SymMonoidal, Supplies CocommutativeComonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW Witness SymMonoidal k
-> Witnesses '[Supplies CocommutativeComonoid] k
-> Witnesses '[SymMonoidal, Supplies CocommutativeComonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness (Supplies CocommutativeComonoid) k
forall k. Witness (Supplies CocommutativeComonoid) k
CocommutativeComonoidSupplyW Witness (Supplies CocommutativeComonoid) k
-> Witnesses '[] k -> Witnesses '[Supplies CocommutativeComonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)
    , forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @CopyDiscard.CopyDiscardStructures String
"Copy and discard" (Witness Monoidal k
monoidal Witness Monoidal k
-> Witnesses '[SymMonoidal, CopyDiscard] k
-> Witnesses CopyDiscardStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW Witness SymMonoidal k
-> Witnesses '[CopyDiscard] k
-> Witnesses '[SymMonoidal, CopyDiscard] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness CopyDiscard k
forall k. Witness CopyDiscard k
CopyDiscardW Witness CopyDiscard k
-> Witnesses '[] k -> Witnesses '[CopyDiscard] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)
    ]
  where
    monoidal :: Witness Monoidal k
monoidal = WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r)

-- | 'testCopyDiscard' where 'TestOb' is 'Ob'.
testCopyDiscard_ :: forall k. (Testable k, CopyDiscard.CopyDiscard k, TestObIsOb k) => TestTree
testCopyDiscard_ :: forall k. (Testable k, CopyDiscard k, TestObIsOb k) => TestTree
testCopyDiscard_ = forall k.
(Testable k, CopyDiscard k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testCopyDiscard @k (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The coherence law tying 'Cartesian.Cartesian' to its 'CopyDiscard.CopyDiscard' superclass
-- (Fox's theorem): the comonoid supplied on every object is the natural one, @copy = id &&& id@
-- and @discard = terminate@.
testCartesian
  :: forall k
   . (Testable k, Cartesian.Cartesian k, TestOb (M.Unit @k))
  => (forall (a :: k) r. (TestOb a) => ((Ob a) => r) -> r)
  -> WithTestOb2 k
  -> TestTree
testCartesian :: forall k.
(Testable k, Cartesian k, TestOb Unit) =>
(forall (a :: k) r. TestOb a => (Ob a => r) -> r)
-> WithTestOb2 k -> TestTree
testCartesian forall (a :: k) r. TestOb a => (Ob a => r) -> r
withOb WithTestOb2 k
withTestOb2 = String -> Property () -> TestTree
testProperty String
"Cartesian" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  withOb @a (withTestOb2 @a @a (propCartesianAt @a))

-- Hoisted so that @a ** a ~ a && a@ is an ordinary given ('Cartesian.TensorIsProduct'), which the
-- quantified superclass of 'Cartesian.Cartesian' can't supply as a rewrite on its own.
propCartesianAt
  :: forall {k} (a :: k)
   . ( Testable k
     , Cartesian.Cartesian k
     , Cartesian.TensorIsProduct a a
     , TestOb (M.Unit @k)
     , TestOb a
     , Ob a
     , TestOb (a M.** a)
     )
  => Property ()
propCartesianAt :: forall {k} (a :: k).
(Testable k, Cartesian k, TensorIsProduct a a, TestOb Unit,
 TestOb a, Ob a, TestOb (a ** a)) =>
Property ()
propCartesianAt = do
  String
-> String
-> (a ~> (a ** a))
-> String
-> (a ~> (a ** a))
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"copy" String
"copy" (forall k (a :: k). (CopyDiscard k, Ob a) => a ~> (a ** a)
CopyDiscard.copy @k @a) String
"id &&& id" (forall (a :: k). (HasBinaryProducts k, Ob a) => a ~> (a && a)
forall {k} (a :: k). (HasBinaryProducts k, Ob a) => a ~> (a && a)
BinaryProduct.diag @a)
  String
-> String -> (a ~> Unit) -> String -> (a ~> Unit) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"discard" String
"discard" (forall k (a :: k). (CopyDiscard k, Ob a) => a ~> Unit
CopyDiscard.discard @k @a) String
"terminate" (forall k (a :: k).
(HasTerminalObject k, Ob a) =>
a ~> TerminalObject
Terminal.terminate @k @a)

testCartesian_ :: forall k. (Testable k, Cartesian.Cartesian k, TestObIsOb k, TestOb (M.Unit @k)) => TestTree
testCartesian_ :: forall k.
(Testable k, Cartesian k, TestObIsOb k, TestOb Unit) =>
TestTree
testCartesian_ =
  forall k.
(Testable k, Cartesian k, TestOb Unit) =>
(forall (a :: k) r. TestOb a => (Ob a => r) -> r)
-> WithTestOb2 k -> TestTree
testCartesian @k (\ @a Ob a => r
r -> forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a r
Ob a => r
r) (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The tensor distributes over coproducts and is absorbed by the initial object, from
-- @'Proarrow.Tools.Laws.Laws' 'Distributive.DistributiveStructures'@: 'Distributive.distL',
-- 'Distributive.distR', 'Distributive.absorbL' and 'Distributive.absorbR' are isomorphisms, with
-- the inverses 'Distributive.distLInv', 'Distributive.distRInv' and 'Initial.initiate'.
testDistributive
  :: forall k
   . (Testable k, Distributive.Distributive k, TestOb (M.Unit @k), TestOb (Initial.InitialObject :: k))
  => WithTestOb2 k
  -> WithTestObCoprod k
  -> TestTree
testDistributive :: forall k.
(Testable k, Distributive k, TestOb Unit, TestOb InitialObject) =>
WithTestOb2 k -> WithTestObCoprod k -> TestTree
testDistributive WithTestOb2 k
withTestOb2 WithTestObCoprod k
withTestObCoprod =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @Distributive.DistributiveStructures
    String
"Distributive"
    ( WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r)
        Witness Monoidal k
-> Witnesses
     '[HasInitialObject, HasBinaryCoproducts, Distributive] k
-> Witnesses DistributiveStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness HasInitialObject k
forall k. Witness HasInitialObject k
InitialW
        Witness HasInitialObject k
-> Witnesses '[HasBinaryCoproducts, Distributive] k
-> Witnesses
     '[HasInitialObject, HasBinaryCoproducts, Distributive] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestObCoprod k -> Witness HasBinaryCoproducts k
forall k. WithTestObCoprod k -> Witness HasBinaryCoproducts k
CoproductsW (\ @a @b TestOb (a || b) => r
r -> WithTestObCoprod k
withTestObCoprod @a @b r
TestOb (a || b) => r
r)
        Witness HasBinaryCoproducts k
-> Witnesses '[Distributive] k
-> Witnesses '[HasBinaryCoproducts, Distributive] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness Distributive k
forall k. Witness Distributive k
DistributiveW
        Witness Distributive k
-> Witnesses '[] k -> Witnesses '[Distributive] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil
    )

testDistributive_ :: forall k. (Testable k, Distributive.Distributive k, TestObIsOb k) => TestTree
testDistributive_ :: forall k. (Testable k, Distributive k, TestObIsOb k) => TestTree
testDistributive_ =
  forall k.
(Testable k, Distributive k, TestOb Unit, TestOb InitialObject) =>
WithTestOb2 k -> WithTestObCoprod k -> TestTree
testDistributive @k
    (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)
    (\ @a @b -> WithTestObCoprod k
forall {k}.
(TestObIsOb k, HasBinaryCoproducts k) =>
WithTestObCoprod k
withTestObCoprodDef @a @b)

-- | The laws of a closed monoidal category, from
-- @'Proarrow.Tools.Laws.Laws' 'Exponential.ClosedStructures'@: 'Exponential.apply' undoes
-- 'Exponential.curry' and every arrow into an exponential is the 'Exponential.curry' of one,
-- 'Exponential.curry' is natural, and 'Exponential.^^^' is defined from 'Exponential.curry' and
-- 'Exponential.apply'.
testClosed
  :: forall k
   . (Testable k, Exponential.Closed k, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> WithTestObExp k
  -> TestTree
testClosed :: forall k.
(Testable k, Closed k, TestOb Unit) =>
WithTestOb2 k -> WithTestObExp k -> TestTree
testClosed WithTestOb2 k
withTestOb2 WithTestObExp k
withTestObExp =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
Property (Some k) -> String -> Witnesses cs k -> TestTree
testLawsWith @Exponential.ClosedStructures
    (forall k. Testable k => Property (Some k)
genObSmall @k)
    String
"Closed"
    (WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r) Witness Monoidal k
-> Witnesses '[Closed] k -> Witnesses ClosedStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestObExp k -> Witness Closed k
forall k. WithTestObExp k -> Witness Closed k
ClosedW (\ @a @b TestOb (a ~~> b) => r
r -> WithTestObExp k
withTestObExp @a @b r
TestOb (a ~~> b) => r
r) Witness Closed k -> Witnesses '[] k -> Witnesses '[Closed] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

testClosed_ :: forall k. (Testable k, Exponential.Closed k, TestObIsOb k) => TestTree
testClosed_ :: forall k. (Testable k, Closed k, TestObIsOb k) => TestTree
testClosed_ =
  forall k.
(Testable k, Closed k, TestOb Unit) =>
WithTestOb2 k -> WithTestObExp k -> TestTree
testClosed @k
    (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)
    (\ @a @b -> WithTestObExp k
forall {k}. (TestObIsOb k, Closed k) => WithTestObExp k
withTestObExpDef @a @b)

-- | Laws of a dialogue category, from @'Proarrow.Tools.Laws.Laws' 'SA.DialogueStructures'@:
-- 'SA.dual' is a contravariant functor, 'SA.linDist' is a natural bijection
-- @Hom(a ** b, Dual c) ≅ Hom(a, Dual (b ** c))@ with inverse 'SA.linDistInv', and
-- 'SA.doubleNegInv' is the one they give.
testDialogue
  :: forall k
   . (Testable k, SA.Dialogue k, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> WithTestObDual k
  -> TestTree
testDialogue :: forall k.
(Testable k, Dialogue k, TestOb Unit) =>
WithTestOb2 k -> WithTestObDual k -> TestTree
testDialogue WithTestOb2 k
withTestOb2 WithTestObDual k
withTestObDual =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @SA.DialogueStructures
    String
"Dialogue"
    ( WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r)
        Witness Monoidal k
-> Witnesses '[SymMonoidal, Dialogue] k
-> Witnesses DialogueStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW
        Witness SymMonoidal k
-> Witnesses '[Dialogue] k -> Witnesses '[SymMonoidal, Dialogue] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestObDual k -> Witness Dialogue k
forall k. WithTestObDual k -> Witness Dialogue k
DialogueW (\ @a TestOb (Dual a) => r
r -> WithTestObDual k
withTestObDual @a r
TestOb (Dual a) => r
r)
        Witness Dialogue k -> Witnesses '[] k -> Witnesses '[Dialogue] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil
    )

testDialogue_ :: forall k. (Testable k, SA.Dialogue k, TestObIsOb k) => TestTree
testDialogue_ :: forall k. (Testable k, Dialogue k, TestObIsOb k) => TestTree
testDialogue_ = forall k.
(Testable k, Dialogue k, TestOb Unit) =>
WithTestOb2 k -> WithTestObDual k -> TestTree
testDialogue @k (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b) (\ @a -> WithTestObDual k
forall {k}. (TestObIsOb k, Dialogue k) => WithTestObDual k
withTestObDualDef @a)

-- | Laws of a *-autonomous category, from
-- @'Proarrow.Tools.Laws.Laws' 'SA.StarAutonomousStructures'@: 'SA.dual' is bijective on hom-sets
-- with inverse 'SA.dualInv', and 'SA.doubleNeg' is an isomorphism. The rest is 'testDialogue'.
-- The exponential witness is needed because 'Exponential.Closed' is a superclass, although no
-- law builds an exponential.
testStarAutonomous
  :: forall k
   . (Testable k, SA.StarAutonomous k, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> WithTestObExp k
  -> WithTestObDual k
  -> TestTree
testStarAutonomous :: forall k.
(Testable k, StarAutonomous k, TestOb Unit) =>
WithTestOb2 k -> WithTestObExp k -> WithTestObDual k -> TestTree
testStarAutonomous WithTestOb2 k
withTestOb2 WithTestObExp k
withTestObExp WithTestObDual k
withTestObDual =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @SA.StarAutonomousStructures
    String
"*-autonomous"
    ( WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r)
        Witness Monoidal k
-> Witnesses '[SymMonoidal, Closed, Dialogue, StarAutonomous] k
-> Witnesses StarAutonomousStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW
        Witness SymMonoidal k
-> Witnesses '[Closed, Dialogue, StarAutonomous] k
-> Witnesses '[SymMonoidal, Closed, Dialogue, StarAutonomous] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestObExp k -> Witness Closed k
forall k. WithTestObExp k -> Witness Closed k
ClosedW (\ @a @b TestOb (a ~~> b) => r
r -> WithTestObExp k
withTestObExp @a @b r
TestOb (a ~~> b) => r
r)
        Witness Closed k
-> Witnesses '[Dialogue, StarAutonomous] k
-> Witnesses '[Closed, Dialogue, StarAutonomous] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestObDual k -> Witness Dialogue k
forall k. WithTestObDual k -> Witness Dialogue k
DialogueW (\ @a TestOb (Dual a) => r
r -> WithTestObDual k
withTestObDual @a r
TestOb (Dual a) => r
r)
        Witness Dialogue k
-> Witnesses '[StarAutonomous] k
-> Witnesses '[Dialogue, StarAutonomous] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness StarAutonomous k
forall k. Witness StarAutonomous k
StarAutonomousW
        Witness StarAutonomous k
-> Witnesses '[] k -> Witnesses '[StarAutonomous] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil
    )

testStarAutonomous_ :: forall k. (Testable k, SA.StarAutonomous k, TestObIsOb k) => TestTree
testStarAutonomous_ :: forall k. (Testable k, StarAutonomous k, TestObIsOb k) => TestTree
testStarAutonomous_ =
  forall k.
(Testable k, StarAutonomous k, TestOb Unit) =>
WithTestOb2 k -> WithTestObExp k -> WithTestObDual k -> TestTree
testStarAutonomous @k
    (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)
    (\ @a @b -> WithTestObExp k
forall {k}. (TestObIsOb k, Closed k) => WithTestObExp k
withTestObExpDef @a @b)
    (\ @a -> WithTestObDual k
forall {k}. (TestObIsOb k, Dialogue k) => WithTestObDual k
withTestObDualDef @a)

-- | Laws of an isomix category, from @'Proarrow.Tools.Laws.Laws' 'IsoMix.IsoMixStructures'@:
-- 'IsoMix.dualUnit' and 'IsoMix.dualUnitInv' are inverses, and 'IsoMix.dualityCounit' is the
-- one the dialogue structure gives.
testIsoMix
  :: forall k
   . (Testable k, IsoMix.IsoMix k, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> WithTestObDual k
  -> TestTree
testIsoMix :: forall k.
(Testable k, IsoMix k, TestOb Unit) =>
WithTestOb2 k -> WithTestObDual k -> TestTree
testIsoMix WithTestOb2 k
withTestOb2 WithTestObDual k
withTestObDual =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @IsoMix.IsoMixStructures
    String
"Isomix"
    ( WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r)
        Witness Monoidal k
-> Witnesses '[SymMonoidal, Dialogue, IsoMix] k
-> Witnesses IsoMixStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW
        Witness SymMonoidal k
-> Witnesses '[Dialogue, IsoMix] k
-> Witnesses '[SymMonoidal, Dialogue, IsoMix] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestObDual k -> Witness Dialogue k
forall k. WithTestObDual k -> Witness Dialogue k
DialogueW (\ @a TestOb (Dual a) => r
r -> WithTestObDual k
withTestObDual @a r
TestOb (Dual a) => r
r)
        Witness Dialogue k
-> Witnesses '[IsoMix] k -> Witnesses '[Dialogue, IsoMix] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness IsoMix k
forall k. Witness IsoMix k
IsoMixW
        Witness IsoMix k -> Witnesses '[] k -> Witnesses '[IsoMix] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil
    )

testIsoMix_ :: forall k. (Testable k, IsoMix.IsoMix k, TestObIsOb k) => TestTree
testIsoMix_ :: forall k. (Testable k, IsoMix k, TestObIsOb k) => TestTree
testIsoMix_ = forall k.
(Testable k, IsoMix k, TestOb Unit) =>
WithTestOb2 k -> WithTestObDual k -> TestTree
testIsoMix @k (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b) (\ @a -> WithTestObDual k
forall {k}. (TestObIsOb k, Dialogue k) => WithTestObDual k
withTestObDualDef @a)

-- | Laws of a compact closed category, from
-- @'Proarrow.Tools.Laws.Laws' 'CC.CompactClosedStructures'@:
-- 'CC.distribDual' and 'IsoMix.dualUnit' are isomorphisms (so 'SA.Dual' is strong monoidal), and
-- 'CC.dualityUnit' and 'CC.dualityCounit' satisfy the zigzag identities. See
-- 'testStarAutonomous' for the exponential witness.
testCompactClosed
  :: forall k
   . (Testable k, CC.CompactClosed k, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> WithTestObExp k
  -> WithTestObDual k
  -> TestTree
testCompactClosed :: forall k.
(Testable k, CompactClosed k, TestOb Unit) =>
WithTestOb2 k -> WithTestObExp k -> WithTestObDual k -> TestTree
testCompactClosed WithTestOb2 k
withTestOb2 WithTestObExp k
withTestObExp WithTestObDual k
withTestObDual =
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @CC.CompactClosedStructures
    String
"Compact closed"
    ( WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r)
        Witness Monoidal k
-> Witnesses
     '[SymMonoidal, Closed, Dialogue, StarAutonomous, IsoMix,
       CompactClosed]
     k
-> Witnesses CompactClosedStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW
        Witness SymMonoidal k
-> Witnesses
     '[Closed, Dialogue, StarAutonomous, IsoMix, CompactClosed] k
-> Witnesses
     '[SymMonoidal, Closed, Dialogue, StarAutonomous, IsoMix,
       CompactClosed]
     k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestObExp k -> Witness Closed k
forall k. WithTestObExp k -> Witness Closed k
ClosedW (\ @a @b TestOb (a ~~> b) => r
r -> WithTestObExp k
withTestObExp @a @b r
TestOb (a ~~> b) => r
r)
        Witness Closed k
-> Witnesses '[Dialogue, StarAutonomous, IsoMix, CompactClosed] k
-> Witnesses
     '[Closed, Dialogue, StarAutonomous, IsoMix, CompactClosed] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& WithTestObDual k -> Witness Dialogue k
forall k. WithTestObDual k -> Witness Dialogue k
DialogueW (\ @a TestOb (Dual a) => r
r -> WithTestObDual k
withTestObDual @a r
TestOb (Dual a) => r
r)
        Witness Dialogue k
-> Witnesses '[StarAutonomous, IsoMix, CompactClosed] k
-> Witnesses '[Dialogue, StarAutonomous, IsoMix, CompactClosed] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness StarAutonomous k
forall k. Witness StarAutonomous k
StarAutonomousW
        Witness StarAutonomous k
-> Witnesses '[IsoMix, CompactClosed] k
-> Witnesses '[StarAutonomous, IsoMix, CompactClosed] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness IsoMix k
forall k. Witness IsoMix k
IsoMixW
        Witness IsoMix k
-> Witnesses '[CompactClosed] k
-> Witnesses '[IsoMix, CompactClosed] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness CompactClosed k
forall k. Witness CompactClosed k
CompactClosedW
        Witness CompactClosed k
-> Witnesses '[] k -> Witnesses '[CompactClosed] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil
    )

testCompactClosed_ :: forall k. (Testable k, CC.CompactClosed k, TestObIsOb k) => TestTree
testCompactClosed_ :: forall k. (Testable k, CompactClosed k, TestObIsOb k) => TestTree
testCompactClosed_ =
  forall k.
(Testable k, CompactClosed k, TestOb Unit) =>
WithTestOb2 k -> WithTestObExp k -> WithTestObDual k -> TestTree
testCompactClosed @k
    (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)
    (\ @a @b -> WithTestObExp k
forall {k}. (TestObIsOb k, Closed k) => WithTestObExp k
withTestObExpDef @a @b)
    (\ @a -> WithTestObDual k
forall {k}. (TestObIsOb k, Dialogue k) => WithTestObDual k
withTestObDualDef @a)

-- | The laws of a category that supplies special commutative Frobenius algebras, stated for
-- every object: the monoid and comonoid laws of its points, their commutativity, and the Frobenius laws of
-- 'Hypergraph.FrobeniusStructures'.
testHypergraph
  :: forall k
   . ( Testable k
     , M.SymMonoidal k
     , Monoid.Supplies Monoid.CommutativeMonoid k
     , Monoid.Supplies Monoid.CocommutativeComonoid k
     , TestOb (M.Unit @k)
     )
  => WithTestOb2 k
  -> TestTree
testHypergraph :: forall k.
(Testable k, SymMonoidal k, Supplies CommutativeMonoid k,
 Supplies CocommutativeComonoid k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testHypergraph WithTestOb2 k
withTestOb2 =
  String -> [TestTree] -> TestTree
testGroup
    String
"Hypergraph (Frobenius supply)"
    [ forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[M.Monoidal, Monoid.Supplies Monoid.Monoid] @k
        String
"Monoids"
        (Witness Monoidal k
monoidal Witness Monoidal k
-> Witnesses '[Supplies Monoid] k
-> Witnesses '[Monoidal, Supplies Monoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness (Supplies Monoid) k
forall k. Witness (Supplies Monoid) k
MonoidSupplyW Witness (Supplies Monoid) k
-> Witnesses '[] k -> Witnesses '[Supplies Monoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)
    , forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[M.Monoidal, Monoid.Supplies Monoid.Comonoid] @k
        String
"Comonoids"
        (Witness Monoidal k
monoidal Witness Monoidal k
-> Witnesses '[Supplies Comonoid] k
-> Witnesses '[Monoidal, Supplies Comonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness (Supplies Comonoid) k
forall k. Witness (Supplies Comonoid) k
ComonoidSupplyW Witness (Supplies Comonoid) k
-> Witnesses '[] k -> Witnesses '[Supplies Comonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)
    , forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[M.Monoidal, M.SymMonoidal, Monoid.Supplies Monoid.CommutativeMonoid] @k
        String
"Commutative monoids"
        (Witness Monoidal k
monoidal Witness Monoidal k
-> Witnesses '[SymMonoidal, Supplies CommutativeMonoid] k
-> Witnesses '[Monoidal, SymMonoidal, Supplies CommutativeMonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW Witness SymMonoidal k
-> Witnesses '[Supplies CommutativeMonoid] k
-> Witnesses '[SymMonoidal, Supplies CommutativeMonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness (Supplies CommutativeMonoid) k
forall k. Witness (Supplies CommutativeMonoid) k
CommutativeMonoidSupplyW Witness (Supplies CommutativeMonoid) k
-> Witnesses '[] k -> Witnesses '[Supplies CommutativeMonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)
    , forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @'[M.Monoidal, M.SymMonoidal, Monoid.Supplies Monoid.CocommutativeComonoid] @k
        String
"Cocommutative comonoids"
        (Witness Monoidal k
monoidal Witness Monoidal k
-> Witnesses '[SymMonoidal, Supplies CocommutativeComonoid] k
-> Witnesses
     '[Monoidal, SymMonoidal, Supplies CocommutativeComonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW Witness SymMonoidal k
-> Witnesses '[Supplies CocommutativeComonoid] k
-> Witnesses '[SymMonoidal, Supplies CocommutativeComonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness (Supplies CocommutativeComonoid) k
forall k. Witness (Supplies CocommutativeComonoid) k
CocommutativeComonoidSupplyW Witness (Supplies CocommutativeComonoid) k
-> Witnesses '[] k -> Witnesses '[Supplies CocommutativeComonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)
    , forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
String -> Witnesses cs k -> TestTree
testLaws @Hypergraph.FrobeniusStructures @k
        String
"Frobenius"
        (Witness Monoidal k
monoidal Witness Monoidal k
-> Witnesses '[SymMonoidal, Supplies Monoid, Supplies Comonoid] k
-> Witnesses FrobeniusStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW Witness SymMonoidal k
-> Witnesses '[Supplies Monoid, Supplies Comonoid] k
-> Witnesses '[SymMonoidal, Supplies Monoid, Supplies Comonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness (Supplies Monoid) k
forall k. Witness (Supplies Monoid) k
MonoidSupplyW Witness (Supplies Monoid) k
-> Witnesses '[Supplies Comonoid] k
-> Witnesses '[Supplies Monoid, Supplies Comonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness (Supplies Comonoid) k
forall k. Witness (Supplies Comonoid) k
ComonoidSupplyW Witness (Supplies Comonoid) k
-> Witnesses '[] k -> Witnesses '[Supplies Comonoid] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)
    ]
  where
    monoidal :: Witness Monoidal k
monoidal = WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r)

testHypergraph_
  :: forall k
   . ( Testable k
     , M.SymMonoidal k
     , TestObIsOb k
     , Monoid.Supplies Monoid.CommutativeMonoid k
     , Monoid.Supplies Monoid.CocommutativeComonoid k
     )
  => TestTree
testHypergraph_ :: forall k.
(Testable k, SymMonoidal k, TestObIsOb k,
 Supplies CommutativeMonoid k, Supplies CocommutativeComonoid k) =>
TestTree
testHypergraph_ = forall k.
(Testable k, SymMonoidal k, Supplies CommutativeMonoid k,
 Supplies CocommutativeComonoid k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testHypergraph @k (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- * Traced monoidal categories

-- | The trace laws of "Proarrow.Category.Monoidal.Strength" ('Strength.TracedStructures').
testTraced
  :: forall k. (Testable k, Strength.TracedMonoidal k, TestOb (M.Unit @k)) => WithTestOb2 k -> TestTree
testTraced :: forall k.
(Testable k, TracedMonoidal k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testTraced WithTestOb2 k
withTestOb2 =
  -- small objects: the laws tensor up to three of them together, and a relation or matrix between
  -- such tensors grows with the product of their sizes
  forall (cs :: [Type -> Constraint]) k.
(Laws cs, Testable k, All cs (TESTED cs k)) =>
Property (Some k) -> String -> Witnesses cs k -> TestTree
testLawsWith @Strength.TracedStructures
    (forall k. Testable k => Property (Some k)
genObSmall @k)
    String
"Traced"
    (WithTestOb2 k -> Witness Monoidal k
forall k. WithTestOb2 k -> Witness Monoidal k
MonoidalW (\ @a @b TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @a @b r
TestOb (a ** b) => r
r) Witness Monoidal k
-> Witnesses '[SymMonoidal, TracedMonoidal] k
-> Witnesses TracedStructures k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness SymMonoidal k
forall k. Witness SymMonoidal k
SymMonoidalW Witness SymMonoidal k
-> Witnesses '[TracedMonoidal] k
-> Witnesses '[SymMonoidal, TracedMonoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witness TracedMonoidal k
forall k. Witness TracedMonoidal k
TracedW Witness TracedMonoidal k
-> Witnesses '[] k -> Witnesses '[TracedMonoidal] k
forall (c :: Type -> Constraint) k (cs1 :: [Type -> Constraint]).
Witness c k -> Witnesses cs1 k -> Witnesses (c : cs1) k
:& Witnesses '[] k
forall k. Witnesses '[] k
WNil)

testTraced_ :: forall k. (Testable k, Strength.TracedMonoidal k, TestObIsOb k) => TestTree
testTraced_ :: forall k. (Testable k, TracedMonoidal k, TestObIsOb k) => TestTree
testTraced_ = forall k.
(Testable k, TracedMonoidal k, TestOb Unit) =>
WithTestOb2 k -> TestTree
testTraced @k (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- * Monoids and comonoids

-- | The monoid laws of @m@: 'Monoid.mempty' is a left and right unit for 'Monoid.mappend' (up to the
-- unitors), and 'Monoid.mappend' is associative (up to the associator).
propMonoid
  :: forall {k} m
   . (Testable k, Monoid.Monoid (m :: k), TestOb m, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> Property ()
propMonoid :: forall {k} (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propMonoid WithTestOb2 k
withTestOb2 =
  WithTestOb2 k
withTestOb2 @M.Unit @m ((TestOb (Unit ** m) => Property ()) -> Property ())
-> (TestOb (Unit ** m) => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$
    WithTestOb2 k
withTestOb2 @m @M.Unit ((TestOb (m ** Unit) => Property ()) -> Property ())
-> (TestOb (m ** Unit) => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$ do
      String
-> String
-> ((Unit ** m) ~> m)
-> String
-> ((Unit ** m) ~> m)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
        String
"left identity"
        String
"μ . (η ⊗ 1)"
        ((m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend ((m ** m) ~> m) -> ((Unit ** m) ~> (m ** m)) -> (Unit ** m) ~> m
forall (b :: k) (c :: k) (a :: k). (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
. (forall (m :: k). Monoid m => Unit ~> m
forall {k} (m :: k). Monoid m => Unit ~> m
Monoid.mempty @m (Unit ~> m) -> (m ~> m) -> (Unit ** m) ~> (m ** m)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
M.** forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m))
        String
"λ"
        (forall k (a :: k). (Monoidal k, Ob a) => (Unit ** a) ~> a
M.leftUnitor @k @m)
      String
-> String
-> ((m ** Unit) ~> m)
-> String
-> ((m ** Unit) ~> m)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
        String
"right identity"
        String
"μ . (1 ⊗ η)"
        ((m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend ((m ** m) ~> m) -> ((m ** Unit) ~> (m ** m)) -> (m ** Unit) ~> m
forall (b :: k) (c :: k) (a :: k). (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
. (forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m (m ~> m) -> (Unit ~> m) -> (m ** Unit) ~> (m ** m)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
M.** forall (m :: k). Monoid m => Unit ~> m
forall {k} (m :: k). Monoid m => Unit ~> m
Monoid.mempty @m))
        String
"ρ"
        (forall k (a :: k). (Monoidal k, Ob a) => (a ** Unit) ~> a
M.rightUnitor @k @m)
      WithTestOb2 k
withTestOb2 @m @m ((TestOb (m ** m) => Property ()) -> Property ())
-> (TestOb (m ** m) => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$ WithTestOb2 k
withTestOb2 @(m M.** m) @m ((TestOb ((m ** m) ** m) => Property ()) -> Property ())
-> (TestOb ((m ** m) ** m) => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$ do
        String
-> String
-> (((m ** m) ** m) ~> m)
-> String
-> (((m ** m) ** m) ~> m)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
          String
"associativity"
          String
"μ . (μ ⊗ 1)"
          (forall (m :: k). Monoid m => (m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend @m ((m ** m) ~> m)
-> (((m ** m) ** m) ~> (m ** m)) -> ((m ** m) ** m) ~> m
forall (b :: k) (c :: k) (a :: k). (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
. (forall (m :: k). Monoid m => (m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend @m ((m ** m) ~> m) -> (m ~> m) -> ((m ** m) ** m) ~> (m ** m)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
M.** forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m))
          String
"μ . (1 ⊗ μ) . α"
          ((m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend ((m ** m) ~> m)
-> (((m ** m) ** m) ~> (m ** m)) -> ((m ** m) ** m) ~> m
forall (b :: k) (c :: k) (a :: k). (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
. (forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m (m ~> m) -> ((m ** m) ~> m) -> (m ** (m ** m)) ~> (m ** m)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
M.** forall (m :: k). Monoid m => (m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend @m) ((m ** (m ** m)) ~> (m ** m))
-> (((m ** m) ** m) ~> (m ** (m ** m)))
-> ((m ** m) ** m) ~> (m ** m)
forall (b :: k) (c :: k) (a :: k). (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
. forall k (a :: k) (b :: k) (c :: k).
(Monoidal k, Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
M.associator @k @m @m @m)

-- | The laws of a commutative monoid: 'propMonoid', and 'Monoid.mappend' is unchanged by 'M.swap'.
propCommutativeMonoid
  :: forall {k} m
   . (Testable k, Monoid.CommutativeMonoid (m :: k), TestOb m, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> Property ()
propCommutativeMonoid :: forall {k} (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propCommutativeMonoid WithTestOb2 k
withTestOb2 = do
  forall (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall {k} (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propMonoid @m (\ @x @y TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @x @y r
TestOb (a ** b) => r
r)
  WithTestOb2 k
withTestOb2 @m @m ((TestOb (m ** m) => Property ()) -> Property ())
-> (TestOb (m ** m) => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$
    String
-> String
-> ((m ** m) ~> m)
-> String
-> ((m ** m) ~> m)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
      String
"commutativity"
      String
"mappend . swap"
      (forall (m :: k). Monoid m => (m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend @m ((m ** m) ~> m) -> ((m ** m) ~> (m ** m)) -> (m ** m) ~> m
forall (b :: k) (c :: k) (a :: k). (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
. forall k (a :: k) (b :: k).
(SymMonoidal k, Ob a, Ob b) =>
(a ** b) ~> (b ** a)
M.swap @k @m @m)
      String
"mappend"
      (forall (m :: k). Monoid m => (m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend @m)

-- | The laws of a cocommutative comonoid, as those of a commutative monoid in the opposite category.
propCocommutativeComonoid
  :: forall {k} m
   . (Testable k, Monoid.CocommutativeComonoid (m :: k), TestOb m, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> Property ()
propCocommutativeComonoid :: forall {k} (m :: k).
(Testable k, CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propCocommutativeComonoid WithTestOb2 k
withTestOb2 = do
  forall {k} (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall (m :: OPPOSITE k).
(Testable (OPPOSITE k), CommutativeMonoid m, TestOb m,
 TestOb Unit) =>
WithTestOb2 (OPPOSITE k) -> Property ()
propCommutativeMonoid @(OP m) (\ @(OP x) @(OP y) TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @x @y r
TestOb (UN OP a ** UN OP b) => r
TestOb (a ** b) => r
r)

-- | Check that the object @m@ is a special commutative 'Hypergraph.Frobenius' algebra: it is a
-- 'Monoid.CommutativeMonoid' (via 'propCommutativeMonoid') and a 'Monoid.CocommutativeComonoid'
-- (via 'propCocommutativeComonoid'), and satisfies speciality (@mappend . comult = id@) and the
-- Frobenius condition. A 'Hypergraph.Hypergraph' category supplies this structure, and
-- @'testHypergraph'@ samples an object and delegates here.
propFrobenius
  :: forall {k} m
   . ( Testable k
     , M.SymMonoidal k
     , Monoid.CommutativeMonoid (m :: k)
     , Monoid.CocommutativeComonoid m
     , TestOb m
     , TestOb (M.Unit @k)
     )
  => WithTestOb2 k
  -> Property ()
propFrobenius :: forall {k} (m :: k).
(Testable k, SymMonoidal k, CommutativeMonoid m,
 CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propFrobenius WithTestOb2 k
withTestOb2 = do
  forall (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall {k} (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propCommutativeMonoid @m (\ @x @y TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @x @y r
TestOb (a ** b) => r
r)
  forall (m :: k).
(Testable k, CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall {k} (m :: k).
(Testable k, CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propCocommutativeComonoid @m (\ @x @y TestOb (a ** b) => r
r -> WithTestOb2 k
withTestOb2 @x @y r
TestOb (a ** b) => r
r)
  WithTestOb2 k
withTestOb2 @m @m ((TestOb (m ** m) => Property ()) -> Property ())
-> (TestOb (m ** m) => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$
    WithTestOb2 k
withTestOb2 @(m M.** m) @m ((TestOb ((m ** m) ** m) => Property ()) -> Property ())
-> (TestOb ((m ** m) ** m) => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$ do
      let mu :: (m ** m) ~> m
mu = forall (m :: k). Monoid m => (m ** m) ~> m
forall {k} (m :: k). Monoid m => (m ** m) ~> m
Monoid.mappend @m
          delta :: m ~> (m ** m)
delta = forall (c :: k). Comonoid c => c ~> (c ** c)
forall {k} (c :: k). Comonoid c => c ~> (c ** c)
Monoid.comult @m
      String -> String -> (m ~> m) -> String -> (m ~> m) -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
        String
"speciality"
        String
"mappend . comult"
        ((m ** m) ~> m
mu ((m ** m) ~> m) -> (m ~> (m ** m)) -> m ~> m
forall (b :: k) (c :: k) (a :: k). (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
. m ~> (m ** m)
delta)
        String
"id"
        (forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m)
      String
-> String
-> ((m ** m) ~> (m ** m))
-> String
-> ((m ** m) ~> (m ** m))
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
        String
"Frobenius condition (left)"
        String
"(mappend ** id) . associatorInv . (id ** comult)"
        (((m ** m) ~> m
mu ((m ** m) ~> m) -> (m ~> m) -> ((m ** m) ** m) ~> (m ** m)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
M.** forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m) (((m ** m) ** m) ~> (m ** m))
-> ((m ** m) ~> ((m ** m) ** m)) -> (m ** m) ~> (m ** m)
forall (b :: k) (c :: k) (a :: k). (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
. forall k (a :: k) (b :: k) (c :: k).
(Monoidal k, Ob a, Ob b, Ob c) =>
(a ** (b ** c)) ~> ((a ** b) ** c)
M.associatorInv @k @m @m @m ((m ** (m ** m)) ~> ((m ** m) ** m))
-> ((m ** m) ~> (m ** (m ** m))) -> (m ** m) ~> ((m ** m) ** m)
forall (b :: k) (c :: k) (a :: k). (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
. (forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m (m ~> m) -> (m ~> (m ** m)) -> (m ** m) ~> (m ** (m ** m))
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
M.** m ~> (m ** m)
delta))
        String
"comult . mappend"
        (m ~> (m ** m)
delta (m ~> (m ** m)) -> ((m ** m) ~> m) -> (m ** m) ~> (m ** m)
forall (b :: k) (c :: k) (a :: k). (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
. (m ** m) ~> m
mu)
      String
-> String
-> ((m ** m) ~> (m ** m))
-> String
-> ((m ** m) ~> (m ** m))
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
        String
"Frobenius condition (right)"
        String
"(id ** mappend) . associator . (comult ** id)"
        ((forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m (m ~> m) -> ((m ** m) ~> m) -> (m ** (m ** m)) ~> (m ** m)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
M.** (m ** m) ~> m
mu) ((m ** (m ** m)) ~> (m ** m))
-> ((m ** m) ~> (m ** (m ** m))) -> (m ** m) ~> (m ** m)
forall (b :: k) (c :: k) (a :: k). (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
. forall k (a :: k) (b :: k) (c :: k).
(Monoidal k, Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
M.associator @k @m @m @m (((m ** m) ** m) ~> (m ** (m ** m)))
-> ((m ** m) ~> ((m ** m) ** m)) -> (m ** m) ~> (m ** (m ** m))
forall (b :: k) (c :: k) (a :: k). (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
. (m ~> (m ** m)
delta (m ~> (m ** m)) -> (m ~> m) -> (m ** m) ~> ((m ** m) ** m)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
M.** forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @m))
        String
"comult . mappend"
        (m ~> (m ** m)
delta (m ~> (m ** m)) -> ((m ** m) ~> m) -> (m ** m) ~> (m ** m)
forall (b :: k) (c :: k) (a :: k). (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
. (m ** m) ~> m
mu)

-- | The monoid laws of @m@ ('propMonoid') as a ready-made test.
testMonoid
  :: forall {k} m
   . (Testable k, Monoid.Monoid (m :: k), TestOb m, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> TestTree
testMonoid :: forall {k} (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testMonoid WithTestOb2 k
f = String -> Property () -> TestTree
testProperty (String
"Monoid " String -> String -> String
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @m) (forall (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall {k} (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propMonoid @m \ @a @b -> WithTestOb2 k
f @a @b)

testMonoid_ :: forall {k} m. (Testable k, Monoid.Monoid (m :: k), TestObIsOb k) => TestTree
testMonoid_ :: forall {k} (m :: k).
(Testable k, Monoid m, TestObIsOb k) =>
TestTree
testMonoid_ = forall (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
forall {k} (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testMonoid @m (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The comonoid laws of @m@, as the monoid laws of @m@ in the opposite category.
testComonoid
  :: forall {k} m
   . (Testable k, Monoid.Comonoid (m :: k), TestOb m, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> TestTree
testComonoid :: forall {k} (m :: k).
(Testable k, Comonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testComonoid WithTestOb2 k
f = String -> Property () -> TestTree
testProperty (String
"Comonoid " String -> String -> String
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @m) (forall {k} (m :: k).
(Testable k, Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall (m :: OPPOSITE k).
(Testable (OPPOSITE k), Monoid m, TestOb m, TestOb Unit) =>
WithTestOb2 (OPPOSITE k) -> Property ()
propMonoid @(OP m) \ @(OP a) @(OP b) TestOb (a ** b) => r
r -> WithTestOb2 k
f @a @b r
TestOb (UN OP a ** UN OP b) => r
TestOb (a ** b) => r
r)

testComonoid_ :: forall {k} m. (Testable k, Monoid.Comonoid (m :: k), TestObIsOb k) => TestTree
testComonoid_ :: forall {k} (m :: k).
(Testable k, Comonoid m, TestObIsOb k) =>
TestTree
testComonoid_ = forall (m :: k).
(Testable k, Comonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
forall {k} (m :: k).
(Testable k, Comonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testComonoid @m (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The laws of a commutative monoid ('propCommutativeMonoid') as a ready-made test.
testCommutativeMonoid
  :: forall {k} m
   . (Testable k, Monoid.CommutativeMonoid (m :: k), TestOb m, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> TestTree
testCommutativeMonoid :: forall {k} (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testCommutativeMonoid WithTestOb2 k
f = String -> Property () -> TestTree
testProperty (String
"CommutativeMonoid " String -> String -> String
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @m) (forall (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall {k} (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propCommutativeMonoid @m \ @a @b -> WithTestOb2 k
f @a @b)

testCommutativeMonoid_ :: forall {k} m. (Testable k, Monoid.CommutativeMonoid (m :: k), TestObIsOb k) => TestTree
testCommutativeMonoid_ :: forall {k} (m :: k).
(Testable k, CommutativeMonoid m, TestObIsOb k) =>
TestTree
testCommutativeMonoid_ = forall (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
forall {k} (m :: k).
(Testable k, CommutativeMonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testCommutativeMonoid @m (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The laws of a cocommutative comonoid ('propCocommutativeComonoid') as a ready-made test.
testCocommutativeComonoid
  :: forall {k} m
   . (Testable k, Monoid.CocommutativeComonoid (m :: k), TestOb m, TestOb (M.Unit @k))
  => WithTestOb2 k
  -> TestTree
testCocommutativeComonoid :: forall {k} (m :: k).
(Testable k, CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testCocommutativeComonoid WithTestOb2 k
f = String -> Property () -> TestTree
testProperty (String
"CocommutativeComonoid " String -> String -> String
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @m) (forall (m :: k).
(Testable k, CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall {k} (m :: k).
(Testable k, CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propCocommutativeComonoid @m \ @a @b -> WithTestOb2 k
f @a @b)

testCocommutativeComonoid_
  :: forall {k} m
   . (Testable k, Monoid.CocommutativeComonoid (m :: k), TestObIsOb k)
  => TestTree
testCocommutativeComonoid_ :: forall {k} (m :: k).
(Testable k, CocommutativeComonoid m, TestObIsOb k) =>
TestTree
testCocommutativeComonoid_ = forall (m :: k).
(Testable k, CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
forall {k} (m :: k).
(Testable k, CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testCocommutativeComonoid @m (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- | The laws of a special commutative Frobenius algebra ('propFrobenius') as a ready-made test.
testFrobenius
  :: forall {k} (m :: k)
   . ( Testable k
     , Monoid.CommutativeMonoid m
     , Monoid.CocommutativeComonoid m
     , TestOb m
     , TestOb (M.Unit @k)
     )
  => WithTestOb2 k
  -> TestTree
testFrobenius :: forall {k} (m :: k).
(Testable k, CommutativeMonoid m, CocommutativeComonoid m,
 TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testFrobenius WithTestOb2 k
f = String -> Property () -> TestTree
testProperty (String
"Frobenius " String -> String -> String
forall a. [a] -> [a] -> [a]
++ forall k (a :: k). (Testable k, TestOb a) => String
showOb @k @m) (forall (m :: k).
(Testable k, SymMonoidal k, CommutativeMonoid m,
 CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
forall {k} (m :: k).
(Testable k, SymMonoidal k, CommutativeMonoid m,
 CocommutativeComonoid m, TestOb m, TestOb Unit) =>
WithTestOb2 k -> Property ()
propFrobenius @m \ @a @b -> WithTestOb2 k
f @a @b)

testFrobenius_
  :: forall {k} (m :: k)
   . ( Testable k
     , Monoid.CommutativeMonoid m
     , Monoid.CocommutativeComonoid m
     , TestObIsOb k
     )
  => TestTree
testFrobenius_ :: forall {k} (m :: k).
(Testable k, CommutativeMonoid m, CocommutativeComonoid m,
 TestObIsOb k) =>
TestTree
testFrobenius_ = forall (m :: k).
(Testable k, CommutativeMonoid m, CocommutativeComonoid m,
 TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
forall {k} (m :: k).
(Testable k, CommutativeMonoid m, CocommutativeComonoid m,
 TestOb m, TestOb Unit) =>
WithTestOb2 k -> TestTree
testFrobenius @m (\ @a @b -> WithTestOb2 k
forall {k}. (TestObIsOb k, Monoidal k) => WithTestOb2 k
withTestOb2Def @a @b)

-- * Toposes

-- | Checks the subobject classifier @'Topos.Omega'@, the object of truth values: a map into it is a
-- predicate, and each mono @m@ has one classifying map, 'Topos.true' on @m@ and nowhere else. The
-- laws quantify over generalized elements (arrows into the object):
--
-- * @'Topos.classifyGraph' f@ is 'Topos.true' at @(x, y)@ iff @y = f . x@. This is the pullback
--   condition for the graph @\<id, f\>@. At @f = 'id'@ it is 'Topos.isEq', so equality testing is
--   pinned down too.
-- * Distinct arrows get distinct classifiers, the testable consequence of the classifying map
--   being unique.
-- * @'Topos.classifyKernelPair' f@ is true at @(x, x\')@ iff @f@ identifies them, in both directions.
-- * @'Topos.classifyImage' f@ is true on the image of @f@ and nowhere else: every element it calls
--   true factors through the image mono, by 'Pullback.factorPullback'. This is the law that
--   separates a subobject classifier from an arbitrary map into 'Topos.Omega'.
--
-- The elements come from the 'Testable' palette, which is sound iff that palette generates: true
-- for the concrete finite categories, not for a presheaf topos.
testSubobjectClassifier
  :: forall k
   . ( Testable k
     , Topos.HasSubobjectClassifier k
     , Topos.HasEpiMonoFactorization k
     , Pushout.HasPushouts k
     , Pullback.HasPullbacks k
     , TestOb (Topos.Omega :: k)
     )
  => WithTestObProd k
  -> TestTree
testSubobjectClassifier :: forall k.
(Testable k, HasSubobjectClassifier k, HasEpiMonoFactorization k,
 HasPushouts k, HasPullbacks k, TestOb Omega) =>
WithTestObProd k -> TestTree
testSubobjectClassifier WithTestObProd k
withTestObProd = String -> Property () -> TestTree
testProperty String
"Subobject classifier" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
  Some @a <- forall k. Testable k => Property (Some k)
genOb @k
  Some @b <- genOb @k
  Some @z <- genOb @k
  f <- genNamed @(a ~> b) "f"
  x@Objs <- genNamed @(z ~> a) "x"
  y <- genNamed @(z ~> b) "y"
  inGraph <- eqP (f . x) y
  classified <-
    eqP (Topos.classifyGraph f . (x BinaryProduct.&&& y)) (Terminal.const Topos.true)
  expect "classifyGraph is true exactly on the graph of f" inGraph classified
  g <- genNamed @(a ~> b) "g"
  withTestObProd @a @b @(Property ()) do
    eqChi <- eqP (Topos.classifyGraph f) (Topos.classifyGraph g)
    propReflectsEq "classifier injective" "classifyGraph f == classifyGraph g" eqChi f g
  x' <- genNamed @(z ~> a) "x'"
  identified <- eqP (f . x) (f . x')
  kernelPair <-
    eqP (Topos.classifyKernelPair f . (x BinaryProduct.&&& x')) (Terminal.const Topos.true)
  expect "classifyKernelPair is true exactly when f identifies the pair" identified kernelPair
  -- bound once: in the sheaves this is a pushout, which sheafifies and tabulates its apex
  let chi = (x ~> x) -> x ~> Omega
forall {k} (a :: k) (b :: k).
(HasSubobjectClassifier k, HasPushouts k) =>
(a ~> b) -> b ~> Omega
Topos.classifyImage x ~> x
f
  onImage <- eqP (chi . (f . x)) (Terminal.const Topos.true)
  expect "classifyImage f is true on the image of f" True onImage
  -- The converse, which makes this a /subobject/ classifier: anything the classifier calls true
  -- factors through the image mono. Mirrors the existence half of 'testEqualizers'.
  case Topos.factorize f of
    (:.:) Hom k x b
_ m :: Hom k b x
m@Hom k b x
Objs -> do
      w <- forall a. TestableType a => String -> Property a
genNamed @(z ~> b) String
"w"
      classifiedTrue <- eqP (chi . w) (Terminal.const Topos.true)
      when classifiedTrue $
        testEq
          "image factorization"
          "m . factorPullback m terminate w terminate"
          (m . Pullback.factorPullback m Terminal.terminate w Terminal.terminate)
          "w"
          w

testSubobjectClassifier_
  :: forall k
   . ( Testable k
     , Topos.HasSubobjectClassifier k
     , Topos.HasEpiMonoFactorization k
     , Pushout.HasPushouts k
     , Pullback.HasPullbacks k
     , TestObIsOb k
     , TestOb (Topos.Omega :: k)
     )
  => TestTree
testSubobjectClassifier_ :: forall k.
(Testable k, HasSubobjectClassifier k, HasEpiMonoFactorization k,
 HasPushouts k, HasPullbacks k, TestObIsOb k, TestOb Omega) =>
TestTree
testSubobjectClassifier_ =
  forall k.
(Testable k, HasSubobjectClassifier k, HasEpiMonoFactorization k,
 HasPushouts k, HasPullbacks k, TestOb Omega) =>
WithTestObProd k -> TestTree
testSubobjectClassifier @k (\ @a @b -> WithTestObProd k
forall {k}. (TestObIsOb k, HasBinaryProducts k) => WithTestObProd k
withTestObProdDef @a @b)

-- | Negation is implication into false:
-- @'Topos.not' = 'Topos.implies' . (id '&&&' 'Terminal.const' 'Topos.false')@. A theorem of
-- every topos; 'Topos.not' is defined as the classifying map of 'Topos.false' instead, so this
-- checks the two agree.
testNegation :: forall k. (Testable k, Topos.ElementaryTopos k, TestOb (Topos.Omega :: k)) => TestTree
testNegation :: forall k. (Testable k, ElementaryTopos k, TestOb Omega) => TestTree
testNegation =
  String -> Property () -> TestTree
testProperty String
"negation is implication into false" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    String
-> String
-> (Omega ~> Omega)
-> String
-> (Omega ~> Omega)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
      String
"not"
      String
"not"
      (forall k. ElementaryTopos k => Omega ~> Omega
Topos.not @k)
      String
"implies . (id &&& const false)"
      ((Omega && Omega) ~> Omega
forall k. ElementaryTopos k => (Omega && Omega) ~> Omega
Topos.implies ((Omega && Omega) ~> Omega)
-> (Omega ~> (Omega && Omega)) -> Omega ~> Omega
forall (b :: k) (c :: k) (a :: k). (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
. (Omega ~> Omega
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id (Omega ~> Omega) -> (Omega ~> Omega) -> Omega ~> (Omega && Omega)
forall (a :: k) (x :: k) (y :: k).
(a ~> x) -> (a ~> y) -> a ~> (x && y)
forall k (a :: k) (x :: k) (y :: k).
HasBinaryProducts k =>
(a ~> x) -> (a ~> y) -> a ~> (x && y)
BinaryProduct.&&& El Omega -> Omega ~> Omega
forall {k} (a :: k) (b :: k).
(HasTerminalObject k, Ob a) =>
El b -> a ~> b
Terminal.const El Omega
forall k. ElementaryTopos k => TerminalObject ~> Omega
Topos.false))

-- | The three equations a Lawvere–Tierney topology satisfies, for an arrow
-- @j :: 'Topos.Omega' '~>' 'Topos.Omega'@: it fixes @true@, is idempotent, and preserves meets.
--
-- Such a @j@ is the same data as a Grothendieck topology: the covering sieves are the ones @j@
-- sends to @true@. 'FinSheaf.lawvereTierney' is the @j@ a coverage induces, so this is how a
-- coverage's stability and composition get checked, without quantifying over arrows the coverage
-- was never handed.
testLawvereTierney
  :: forall k
   . (Testable k, Topos.ElementaryTopos k, TestOb (Topos.Omega :: k), TestOb (Terminal.TerminalObject :: k))
  => WithTestObProd k
  -> (Topos.Omega :: k) ~> Topos.Omega
  -> TestTree
testLawvereTierney :: forall k.
(Testable k, ElementaryTopos k, TestOb Omega,
 TestOb TerminalObject) =>
WithTestObProd k -> (Omega ~> Omega) -> TestTree
testLawvereTierney WithTestObProd k
withTestObProd Omega ~> Omega
j =
  String -> [TestTree] -> TestTree
testGroup
    String
"Lawvere-Tierney topology"
    [String -> Property () -> TestTree
testProperty String
name ((Omega ~> Omega) -> Property ()
law Omega ~> Omega
j) | (String
name, (Omega ~> Omega) -> Property ()
law) <- forall k.
(Testable k, ElementaryTopos k, TestOb Omega,
 TestOb TerminalObject) =>
WithTestObProd k -> [(String, (Omega ~> Omega) -> Property ())]
lawvereTierneyLaws @k (\ @a @b TestOb (a && b) => r
r -> WithTestObProd k
withTestObProd @a @b r
TestOb (a && b) => r
r)]

-- | The three laws of 'testLawvereTierney', by name, for an arrow given later. Shared by it and
-- 'testLawvereTierneyFamily'.
lawvereTierneyLaws
  :: forall k
   . (Testable k, Topos.ElementaryTopos k, TestOb (Topos.Omega :: k), TestOb (Terminal.TerminalObject :: k))
  => WithTestObProd k
  -> [(String, (Topos.Omega :: k) ~> Topos.Omega -> Property ())]
lawvereTierneyLaws :: forall k.
(Testable k, ElementaryTopos k, TestOb Omega,
 TestOb TerminalObject) =>
WithTestObProd k -> [(String, (Omega ~> Omega) -> Property ())]
lawvereTierneyLaws WithTestObProd k
withTestObProd =
  [ (String
"fixes true", \Omega ~> Omega
j -> String
-> String
-> (TerminalObject ~> Omega)
-> String
-> (TerminalObject ~> Omega)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"true" String
"j . true" (Omega ~> Omega
j (Omega ~> Omega)
-> (TerminalObject ~> Omega) -> TerminalObject ~> Omega
forall (b :: k) (c :: k) (a :: k). (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
. TerminalObject ~> Omega
forall k. HasSubobjectClassifier k => TerminalObject ~> Omega
Topos.true) String
"true" TerminalObject ~> Omega
forall k. HasSubobjectClassifier k => TerminalObject ~> Omega
Topos.true)
  , (String
"idempotent", \Omega ~> Omega
j -> String
-> String
-> (Omega ~> Omega)
-> String
-> (Omega ~> Omega)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"idempotent" String
"j . j" (Omega ~> Omega
j (Omega ~> Omega) -> (Omega ~> Omega) -> Omega ~> Omega
forall (b :: k) (c :: k) (a :: k). (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
. Omega ~> Omega
j) String
"j" Omega ~> Omega
j)
  ,
    ( String
"preserves meets"
    , \Omega ~> Omega
j ->
        WithTestObProd k
withTestObProd @Topos.Omega @Topos.Omega @(Property ()) ((TestOb (Omega && Omega) => Property ()) -> Property ())
-> (TestOb (Omega && Omega) => Property ()) -> Property ()
forall a b. (a -> b) -> a -> b
$
          String
-> String
-> ((Omega && Omega) ~> Omega)
-> String
-> ((Omega && Omega) ~> Omega)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq String
"meets" String
"j . and" (Omega ~> Omega
j (Omega ~> Omega)
-> ((Omega && Omega) ~> Omega) -> (Omega && Omega) ~> Omega
forall (b :: k) (c :: k) (a :: k). (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
. (Omega && Omega) ~> Omega
forall k. ElementaryTopos k => (Omega && Omega) ~> Omega
Topos.and) String
"and . (j *** j)" ((Omega && Omega) ~> Omega
forall k. ElementaryTopos k => (Omega && Omega) ~> Omega
Topos.and ((Omega && Omega) ~> Omega)
-> ((Omega && Omega) ~> (Omega && Omega))
-> (Omega && Omega) ~> Omega
forall (b :: k) (c :: k) (a :: k). (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
. (Omega ~> Omega
j (Omega ~> Omega)
-> (Omega ~> Omega) -> (Omega && Omega) ~> (Omega && Omega)
forall (a :: k) (b :: k) (x :: k) (y :: k).
(a ~> x) -> (b ~> y) -> (a && b) ~> (x && y)
forall k (a :: k) (b :: k) (x :: k) (y :: k).
HasBinaryProducts k =>
(a ~> x) -> (b ~> y) -> (a && b) ~> (x && y)
BinaryProduct.*** Omega ~> Omega
j))
    )
  ]

-- | 'testLawvereTierney' for a family of arrows indexed by the truth values, at a generated one:
-- 'Topos.openTopology' and 'Topos.closedTopology' are the ones the internal logic gives.
testLawvereTierneyFamily
  :: forall k
   . (Testable k, Topos.ElementaryTopos k, TestOb (Topos.Omega :: k), TestOb (Terminal.TerminalObject :: k))
  => String
  -> WithTestObProd k
  -> ((Terminal.TerminalObject :: k) ~> Topos.Omega -> (Topos.Omega :: k) ~> Topos.Omega)
  -> TestTree
testLawvereTierneyFamily :: forall k.
(Testable k, ElementaryTopos k, TestOb Omega,
 TestOb TerminalObject) =>
String
-> WithTestObProd k
-> ((TerminalObject ~> Omega) -> Omega ~> Omega)
-> TestTree
testLawvereTierneyFamily String
name WithTestObProd k
withTestObProd (TerminalObject ~> Omega) -> Omega ~> Omega
family =
  String -> [TestTree] -> TestTree
testGroup
    String
name
    [ String -> Property () -> TestTree
testProperty String
lawName (String -> Property (TerminalObject ~> Omega)
forall a. TestableType a => String -> Property a
genNamed String
"u" Property (TerminalObject ~> Omega)
-> ((TerminalObject ~> Omega) -> Property ()) -> Property ()
forall a b.
Property' String a
-> (a -> Property' String b) -> Property' String b
forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Omega ~> Omega) -> Property ()
law ((Omega ~> Omega) -> Property ())
-> ((TerminalObject ~> Omega) -> Omega ~> Omega)
-> (TerminalObject ~> Omega)
-> Property ()
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
. (TerminalObject ~> Omega) -> Omega ~> Omega
family)
    | (String
lawName, (Omega ~> Omega) -> Property ()
law) <- forall k.
(Testable k, ElementaryTopos k, TestOb Omega,
 TestOb TerminalObject) =>
WithTestObProd k -> [(String, (Omega ~> Omega) -> Property ())]
lawvereTierneyLaws @k (\ @a @b TestOb (a && b) => r
r -> WithTestObProd k
withTestObProd @a @b r
TestOb (a && b) => r
r)
    ]

testLawvereTierney_
  :: forall k
   . ( Testable k
     , Topos.ElementaryTopos k
     , TestObIsOb k
     , TestOb (Topos.Omega :: k)
     , TestOb (Terminal.TerminalObject :: k)
     )
  => (Topos.Omega :: k) ~> Topos.Omega
  -> TestTree
testLawvereTierney_ :: forall k.
(Testable k, ElementaryTopos k, TestObIsOb k, TestOb Omega,
 TestOb TerminalObject) =>
(Omega ~> Omega) -> TestTree
testLawvereTierney_ =
  forall k.
(Testable k, ElementaryTopos k, TestOb Omega,
 TestOb TerminalObject) =>
WithTestObProd k -> (Omega ~> Omega) -> TestTree
testLawvereTierney @k (\ @a @b -> WithTestObProd k
forall {k}. (TestObIsOb k, HasBinaryProducts k) => WithTestObProd k
withTestObProdDef @a @b)

testLawvereTierneyFamily_
  :: forall k
   . ( Testable k
     , Topos.ElementaryTopos k
     , TestObIsOb k
     , TestOb (Topos.Omega :: k)
     , TestOb (Terminal.TerminalObject :: k)
     )
  => String
  -> ((Terminal.TerminalObject :: k) ~> Topos.Omega -> (Topos.Omega :: k) ~> Topos.Omega)
  -> TestTree
testLawvereTierneyFamily_ :: forall k.
(Testable k, ElementaryTopos k, TestObIsOb k, TestOb Omega,
 TestOb TerminalObject) =>
String -> ((TerminalObject ~> Omega) -> Omega ~> Omega) -> TestTree
testLawvereTierneyFamily_ String
name =
  forall k.
(Testable k, ElementaryTopos k, TestOb Omega,
 TestOb TerminalObject) =>
String
-> WithTestObProd k
-> ((TerminalObject ~> Omega) -> Omega ~> Omega)
-> TestTree
testLawvereTierneyFamily @k String
name (\ @a @b -> WithTestObProd k
forall {k}. (TestObIsOb k, HasBinaryProducts k) => WithTestObProd k
withTestObProdDef @a @b)

-- * Sites and sheaves

-- | The three laws a listable, stable coverage owes, as one group: 'testStableSite',
-- 'testGeneratedSieveIsSieve' and 'testDenseIsCovering'. Checking a site starts here.
--
-- The rest of this section checks what is built on the coverage: 'testGluesBack' and
-- 'testGluesBackAt' the sheaf condition, 'testEqualizersAreSheaves' the category of sheaves, and
-- 'testSheafification' (with 'testPlusFixes') the reflector. 'testLawvereTierney', under Toposes,
-- checks the generated topology and with it 'Sheaf.HasFiniteCovers'\'s Composition law.
testSiteLaws
  :: forall t j k
   . (Sheaf.StableSite t k, Sheaf.HasFiniteCovers t k, Finitary.FiniteCat j, Finitary.FiniteCat k)
  => TestTree
testSiteLaws :: forall t j k.
(StableSite t k, HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
TestTree
testSiteLaws =
  String -> [TestTree] -> TestTree
testGroup
    String
"site laws"
    [forall t k.
(StableSite t k, HasFiniteCovers t k, FiniteCat k) =>
TestTree
testStableSite @t @k, forall t j k.
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
TestTree
testGeneratedSieveIsSieve @t @j @k, forall t j k.
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
TestTree
testDenseIsCovering @t @j @k]

-- | 'Sheaf.StableSite'\'s law: pulling a cover back along @f@ gives a cover. For every leg @l'@ of
-- the cover 'Sheaf.pullbackCover' returns, the instance names a leg @l@ of the original and a
-- factor @u@ with @f '.' 'Sheaf.legArrow' l' = 'Sheaf.legArrow' l '.' u@. This checks that
-- equation, and that the pulled-back legs generate a covering sieve, which matters for covers that
-- are built instead of listed, like the meets of 'Proarrow.Category.Sheaf.Joins'. Covering is
-- checked instead of dense so that the check does not assume covers compose. It runs at
-- @j ~ ()@, since whether a sieve covers does not depend on @j@.
--
-- On a thin site (at most one arrow between two objects) the equation holds as soon as it
-- typechecks. It bites only where two legs share a source and target: the ends of an edge in the
-- graph schema @E ⇉ V@, or 'Proarrow.Category.Sheaf.ByImage', whose legs are named by elements.
testStableSite
  :: forall t k. (Sheaf.StableSite t k, Sheaf.HasFiniteCovers t k, Finitary.FiniteCat k) => TestTree
testStableSite :: forall t k.
(StableSite t k, HasFiniteCovers t k, FiniteCat k) =>
TestTree
testStableSite =
  String -> Property () -> TestTree
testProperty String
"covers pull back" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    [Property ()] -> Property ()
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
      ( forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @k @(Property ()) \ @a -> forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @k @(Property ()) \ @b ->
          [ case Cover t k a c -> (a ~> a) -> PulledBack t k a c a
forall (b :: k) (a :: k) c.
Ob b =>
Cover t k a c -> (b ~> a) -> PulledBack t k a c b
forall t k (b :: k) (a :: k) c.
(StableSite t k, Ob b) =>
Cover t k a c -> (b ~> a) -> PulledBack t k a c b
Sheaf.pullbackCover Cover t k a c
c a ~> a
f of
              -- the arrow itself factors, so the pullback is @b@\'s implicit identity cover
              Sheaf.AlreadyFactors Factors t k a c a
fs -> forall t {k} (a :: k) c (x :: k).
(Site t k, FiniteCat k, Ob a) =>
(x ~> a) -> Factors t k a c x -> Property ()
propFactorsThroughLeg @t a ~> a
f Factors t k a c a
fs
              Sheaf.PulledBack Cover t k a c'
c' forall (x :: k). Leg t k a c' x -> Factors t k a c x
fs -> do
                String -> Bool -> Bool -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
                  (String
"the pulled-back cover of object " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: k). (Enumerable k, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @b) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" covers it")
                  Bool
True
                  (forall t {j} {k} (a :: k) (b :: j).
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
Sieve a b -> Bool
FinSheaf.isCovering @t (forall t {j} {k} (a :: k) (b :: j) c.
(Site t k, LocallyFinite k, CategoryOf j, Ob a, Ob b) =>
Cover t k a c -> Sieve a b
FinSheaf.generatedSieve @t @b @'() Cover t k a c'
c'))
                [Property ()] -> Property ()
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ [forall t {k} (a :: k) c (x :: k).
(Site t k, FiniteCat k, Ob a) =>
(x ~> a) -> Factors t k a c x -> Property ()
propFactorsThroughLeg @t (a ~> a
f (a ~> a) -> (x ~> a) -> x ~> a
forall (b :: k) (c :: k) (a :: k). (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
. Leg t k a c' x -> x ~> a
forall (a :: k) c (x :: k). Leg t k a c x -> x ~> a
forall t k (a :: k) c (x :: k). Site t k => Leg t k a c x -> x ~> a
Sheaf.legArrow Leg t k a c' x
l) (Leg t k a c' x -> Factors t k a c x
forall (x :: k). Leg t k a c' x -> Factors t k a c x
fs Leg t k a c' x
l) | Sheaf.SomeLeg Leg t k a c' x
l <- Cover t k a c' -> [SomeLeg t k a c']
forall (a :: k) c. Cover t k a c -> [SomeLeg t k a c]
forall t k (a :: k) c.
Site t k =>
Cover t k a c -> [SomeLeg t k a c]
Sheaf.legs Cover t k a c'
c']
          | Sheaf.SomeCover Cover t k a c
c <- forall t k (a :: k).
(HasFiniteCovers t k, Ob a) =>
[SomeCover t k a]
Sheaf.covers @t @k @a
          , a ~> a
f <- forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
[p a b]
forall (p :: k +-> k) (a :: k) (b :: k).
(Finitary p, Ob a, Ob b) =>
[p a b]
Finitary.elements @(Hom k) @b @a
          ]
      )

-- | One leg\'s half of 'testStableSite': the arrow is the leg it factors through, composed with
-- the factor.
propFactorsThroughLeg
  :: forall t {k} (a :: k) c x
   . (Sheaf.Site t k, Finitary.FiniteCat k, Ob a)
  => x ~> a
  -> Sheaf.Factors t k a c x
  -> Property ()
propFactorsThroughLeg :: forall t {k} (a :: k) c (x :: k).
(Site t k, FiniteCat k, Ob a) =>
(x ~> a) -> Factors t k a c x -> Property ()
propFactorsThroughLeg x ~> a
f (Sheaf.Factors Leg t k a c y
l x ~> y
u) =
  -- the factor is what brings its own source into scope, and the result type is known here, where
  -- at the call site it would be under an untouchable variable
  x ~> y
u (x ~> y) -> ((Ob x, Ob y) => Property ()) -> Property ()
forall {k1} {k2} (p :: k1 +-> k2) (a :: k2) (b :: k1) r.
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
//
    String -> Natural -> Natural -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
      String
"the arrow factors through the leg the pullback names"
      (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: k +-> k) (a :: k) (b :: k).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
Finitary.toIndex @(Hom k) @x @a x ~> a
f)
      ((x ~> a) -> Natural
forall (a :: k) (b :: k). (Ob a, Ob b) => Hom k a b -> Natural
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
Finitary.toIndex (Leg t k a c y -> y ~> a
forall (a :: k) c (x :: k). Leg t k a c x -> x ~> a
forall t k (a :: k) c (x :: k). Site t k => Leg t k a c x -> x ~> a
Sheaf.legArrow Leg t k a c y
l (y ~> a) -> (x ~> y) -> x ~> a
forall (b :: k) (c :: k) (a :: k). (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
. x ~> y
u))

-- | A cover's generated sieve (the arrows that factor through one of its legs) is a sieve: closed
-- under composing on either side, as 'FinTopos.closedUnder' decides. That holds for any coverage,
-- lawful or not, so this checks 'Finitary.factorsThrough' and the hom-profunctor's
-- 'Finitary.elements', which every verdict in "Proarrow.Category.Enriched.Finitary.Sheaf" is read
-- off. It catches, for instance, an argument-swapped 'Finitary.factorsThrough'.
testGeneratedSieveIsSieve
  :: forall t j k. (Sheaf.HasFiniteCovers t k, Finitary.FiniteCat j, Finitary.FiniteCat k) => TestTree
testGeneratedSieveIsSieve :: forall t j k.
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
TestTree
testGeneratedSieveIsSieve =
  String -> Property () -> TestTree
testProperty String
"generated sieves are sieves" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    [Property ()] -> Property ()
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
      ( forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @k @(Property ()) \ @a -> forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @j @(Property ()) \ @b ->
          [ case forall t {j} {k} (a :: k) (b :: j) c.
(Site t k, LocallyFinite k, CategoryOf j, Ob a, Ob b) =>
Cover t k a c -> Sieve a b
FinSheaf.generatedSieve @t @a @b Cover t k a c
c of
              Sieve forall (c :: k) (d :: j). (c ~> a) -> (a ~> d) -> Bool
inSieve ->
                String -> Bool -> Bool -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
                  (String
"the sieve a cover of object " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: k). (Enumerable k, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @a) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" generates")
                  Bool
True
                  (forall {j} {k} (p :: j +-> k).
(Finitary p, FiniteCat j, FiniteCat k) =>
(forall (a :: k) (b :: j). (Ob a, Ob b) => p a b -> Bool) -> Bool
forall (p :: j +-> k).
(Finitary p, FiniteCat j, FiniteCat k) =>
(forall (a :: k) (b :: j). (Ob a, Ob b) => p a b -> Bool) -> Bool
FinTopos.closedUnder @(Yo a (OP b)) \(Yo a ~> a
g b1 ~> b
h) -> (a ~> a) -> (a ~> b) -> Bool
forall (c :: k) (d :: j). (c ~> a) -> (a ~> d) -> Bool
inSieve a ~> a
g a ~> b
b1 ~> b
h)
          | Sheaf.SomeCover Cover t k a c
c <- forall t k (a :: k).
(HasFiniteCovers t k, Ob a) =>
[SomeCover t k a]
Sheaf.covers @t @k @a
          ]
      )

-- | On a category with pullbacks (which give the Ore condition), the topology 'Sheaf.Atomic'
-- generates is the double-negation one: 'FinSheaf.lawvereTierney' and 'Topos.doubleNegation' are
-- the same arrow, one computed by closing sieves and one from the internal logic. At @j ~ ()@
-- only: over a non-trivial @j@, @¬¬@ is the dense topology along @j@ as well, while the coverage
-- acts on @k@ alone.
testAtomicIsDoubleNegation
  :: forall k
   . ( Pullback.HasPullbacks k
     , Finitary.FiniteCat k
     , Testable (BinaryProduct.PROD (FinTopos.FINITARY () k))
     , TestOb (Topos.Omega :: BinaryProduct.PROD (FinTopos.FINITARY () k))
     )
  => TestTree
testAtomicIsDoubleNegation :: forall k.
(HasPullbacks k, FiniteCat k, Testable (PROD (FINITARY () k)),
 TestOb Omega) =>
TestTree
testAtomicIsDoubleNegation =
  String -> Property () -> TestTree
testProperty String
"double negation is the atomic topology" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    String
-> String
-> (Omega ~> Omega)
-> String
-> (Omega ~> Omega)
-> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
      String
"¬¬"
      String
"doubleNegation"
      (forall k. ElementaryTopos k => Omega ~> Omega
Topos.doubleNegation @(BinaryProduct.PROD (FinTopos.FINITARY () k)))
      String
"lawvereTierney @Atomic"
      (forall t j k.
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
Omega ~> Omega
FinSheaf.lawvereTierney @Sheaf.Atomic)

-- | A profunctor @p :: j +-> k@ is fully faithful in the sense of 'Ran' when the functor from @k@ to
-- copresheaves on @j@ sending @a@ to @p a (-)@ is: each hom-set @a ~> a'@ is in bijection with the
-- natural transformations @p a' (-) -> p a (-)@, which are @(p '|>' p) a a'@. For a corepresentable
-- @p@ that is the functor @p '%%' -@ being fully faithful, which is
-- 'Proarrow.Category.Sheaf.ByImage'\'s Fully faithful law. 'testRiftFullyFaithful' is the other
-- side, and neither implies the other.
testRanFullyFaithful
  :: forall {j} {k} (p :: j +-> k). (Finitary.Finitary p, Finitary.FiniteCat j, Finitary.FiniteCat k) => TestTree
testRanFullyFaithful :: forall {j} {k} (p :: j +-> k).
(Finitary p, FiniteCat j, FiniteCat k) =>
TestTree
testRanFullyFaithful =
  String -> Property () -> TestTree
testProperty String
"fully faithful into copresheaves" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    [Property ()] -> Property ()
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
      ( forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @k @(Property ()) \ @a -> forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @k @(Property ()) \ @a' ->
          let ix :: Ran (OP p) p a a -> Natural
ix = forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: k +-> k) (a :: k) (b :: k).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
Finitary.toIndex @(Ran (OP p) p) @a @a'
          in [ String -> [Natural] -> [Natural] -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
                 ( String
"each arrow from object "
                     String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: k). (Enumerable k, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @a)
                     String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" to object "
                     String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: k). (Enumerable k, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @a')
                     String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is one transformation"
                 )
                 (Natural -> [Natural]
Finitary.indices (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
Natural
forall (p :: k +-> k) (a :: k) (b :: k).
(Finitary p, Ob a, Ob b) =>
Natural
Finitary.size @(Ran (OP p) p) @a @a'))
                 ([Natural] -> [Natural]
forall a. Ord a => [a] -> [a]
sort [Ran (OP p) p a a -> Natural
ix ((forall (x :: j). p a x -> p a x) -> Ran (OP p) p a a
forall {k} {j} {i} (a :: k) (b :: j) (j2 :: i +-> j)
       (p :: i +-> k).
(Ob a, Ob b) =>
(forall (x :: i). j2 b x -> p a x) -> Ran (OP j2) p a b
Ran ((a ~> a) -> p a x -> p a x
forall (c :: k) (a :: k) (b :: j). (c ~> a) -> p a b -> p c b
forall {j} {k} (p :: j +-> k) (c :: k) (a :: k) (b :: j).
Profunctor p =>
(c ~> a) -> p a b -> p c b
lmap a ~> a
f)) | a ~> a
f <- forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
[p a b]
forall (p :: k +-> k) (a :: k) (b :: k).
(Finitary p, Ob a, Ob b) =>
[p a b]
Finitary.elements @(Hom k) @a @a'])
             ]
      )

-- | A profunctor @p :: j +-> k@ is fully faithful in the sense of 'Rift' when the functor from @j@ to
-- presheaves on @k@ sending @b@ to @p (-) b@ is: each hom-set @b ~> b'@ is in bijection with the
-- natural transformations @p (-) b -> p (-) b'@, which are @(p '<|' p) b b'@. For a representable
-- @p@ that is the functor @p '%' -@ being fully faithful. For a corepresentable @p@ it says the
-- image of @p '%%' -@ is dense. That makes 'Proarrow.Category.Sheaf.ByImage' subcanonical, and
-- when @p '%%' -@ is fully faithful the converse holds too. The comparison lemma asks for something else, 'testCoveredByImage'.
testRiftFullyFaithful
  :: forall {j} {k} (p :: j +-> k). (Finitary.Finitary p, Finitary.FiniteCat j, Finitary.FiniteCat k) => TestTree
testRiftFullyFaithful :: forall {j} {k} (p :: j +-> k).
(Finitary p, FiniteCat j, FiniteCat k) =>
TestTree
testRiftFullyFaithful =
  String -> Property () -> TestTree
testProperty String
"fully faithful into presheaves" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    [Property ()] -> Property ()
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
      ( forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @j @(Property ()) \ @b -> forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @j @(Property ()) \ @b' ->
          let ix :: Rift (OP p) p a a -> Natural
ix = forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
forall (p :: j +-> j) (a :: j) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
Finitary.toIndex @(Rift (OP p) p) @b @b'
          in [ String -> [Natural] -> [Natural] -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
                 ( String
"each arrow from object "
                     String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: j). (Enumerable j, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @b)
                     String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" to object "
                     String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: j). (Enumerable j, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @b')
                     String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is one transformation"
                 )
                 (Natural -> [Natural]
Finitary.indices (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
Natural
forall (p :: j +-> j) (a :: j) (b :: j).
(Finitary p, Ob a, Ob b) =>
Natural
Finitary.size @(Rift (OP p) p) @b @b'))
                 ([Natural] -> [Natural]
forall a. Ord a => [a] -> [a]
sort [Rift (OP p) p a a -> Natural
ix ((forall (x :: k). p x a -> p x a) -> Rift (OP p) p a a
forall {k} {j} {i} (a :: k) (b :: j) (j2 :: k +-> i)
       (p :: j +-> i).
(Ob a, Ob b) =>
(forall (x :: i). j2 x a -> p x b) -> Rift (OP j2) p a b
Rift ((a ~> a) -> p x a -> p x a
forall (b :: j) (d :: j) (a :: k). (b ~> d) -> p a b -> p a d
forall {j} {k} (p :: j +-> k) (b :: j) (d :: j) (a :: k).
Profunctor p =>
(b ~> d) -> p a b -> p a d
rmap a ~> a
f)) | a ~> a
f <- forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
[p a b]
forall (p :: j +-> j) (a :: j) (b :: j).
(Finitary p, Ob a, Ob b) =>
[p a b]
Finitary.elements @(Hom j) @b @b'])
             ]
      )

-- | Every object of @j@ is covered, for the coverage @t@, by the arrows into it from the image of
-- the functor @w '%%' -@: the sieve they generate is covering. With @w '%%' -@ fully faithful
-- ('testRanFullyFaithful') this is the hypothesis of the comparison lemma, see
-- 'Proarrow.Category.Sheaf.Induced'. Neither this nor 'testRiftFullyFaithful' implies the other.
testCoveredByImage
  :: forall t {j} {k} (w :: j +-> k)
   . (Sheaf.HasFiniteCovers t j, Corepresentable w, Finitary.Finitary w, Finitary.FiniteCat j, Finitary.FiniteCat k)
  => TestTree
testCoveredByImage :: forall t {j} {k} (w :: j +-> k).
(HasFiniteCovers t j, Corepresentable w, Finitary w, FiniteCat j,
 FiniteCat k) =>
TestTree
testCoveredByImage =
  String -> Property () -> TestTree
testProperty String
"every object is covered by the image" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    [Property ()] -> Property ()
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
      ( forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @j @(Property ()) \ @c ->
          [ String -> Bool -> Bool -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
              (String
"object " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: j). (Enumerable j, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @c) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is covered")
              Bool
True
              (forall t {j} {k} (a :: k) (b :: j).
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
Sieve a b -> Bool
FinSheaf.isCovering @t (forall (a :: j) (b :: ()).
(Ob a, Ob b) =>
(forall (c :: j) (d :: ()). (c ~> a) -> (b ~> d) -> Bool)
-> Sieve a b
forall {k} {j} (a :: k) (b :: j).
(Ob a, Ob b) =>
(forall (c :: k) (d :: j). (c ~> a) -> (b ~> d) -> Bool)
-> Sieve a b
Sieve @c @'() \c ~> a
g '() ~> d
_ -> [Bool] -> Bool
forall (t :: Type -> Type). Foldable t => t Bool -> Bool
or ((c ~> a) -> [Bool]
forall (c :: j) (x :: j). (Ob c, Ob x) => (x ~> c) -> [Bool]
fromImage c ~> a
g) ((Ob c, Ob a) => Bool) -> (c ~> a) -> Bool
forall (a :: j) (b :: j) r. ((Ob a, Ob b) => r) -> (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
\\ c ~> a
g))
          ]
      )
  where
    fromImage :: forall (c :: j) x. (Ob c, Ob x) => x ~> c -> [Bool]
    fromImage :: forall (c :: j) (x :: j). (Ob c, Ob x) => (x ~> c) -> [Bool]
fromImage x ~> c
g =
      forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @k \ @e -> [(x ~> c) -> ((w %% a) ~> c) -> Bool
forall {k} (x :: k) (y :: k) (a :: k).
(LocallyFinite k, Ob x, Ob y, Ob a) =>
(x ~> a) -> (y ~> a) -> Bool
Finitary.factorsThrough x ~> c
g (w %% a) ~> c
f ((Ob (w %% a), Ob c) => Bool) -> ((w %% a) ~> c) -> Bool
forall (a :: j) (b :: j) r. ((Ob a, Ob b) => r) -> (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
\\ (w %% a) ~> c
f | w a c
x <- 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]
Finitary.elements @w @e @c, let f :: (w %% a) ~> c
f = w a c -> (w %% a) ~> c
forall (a :: k) (b :: j). w a b -> (w %% a) ~> b
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
Corepresentable p =>
p a b -> (p %% a) ~> b
coindex w a c
x]

-- | For every sieve at every pair of objects of a finite site: it is covering exactly when it is dense,
-- that is when its 'FinSheaf.closure' is the maximal sieve. Two independent computations of one
-- fact: 'FinSheaf.isCovering' reads it off the coverage, 'FinSheaf.closure' off the induced
-- topology.
testDenseIsCovering
  :: forall t j k. (Sheaf.HasFiniteCovers t k, Finitary.FiniteCat j, Finitary.FiniteCat k) => TestTree
testDenseIsCovering :: forall t j k.
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
TestTree
testDenseIsCovering =
  String -> Property () -> TestTree
testProperty String
"covering sieves are the dense ones" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    [Property ()] -> Property ()
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
      ( forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @k @(Property ()) \ @a -> forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @j @(Property ()) \ @b ->
          [ String -> Bool -> Bool -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
              (String
"sieve " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (Sieve a a -> Natural
forall (a :: k) (b :: j). (Ob a, Ob b) => Sieve a b -> Natural
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
p a b -> Natural
Finitary.toIndex Sieve a a
s) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" at object " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: k). (Enumerable k, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @a))
              (forall t {j} {k} (a :: k) (b :: j).
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
Sieve a b -> Bool
FinSheaf.isCovering @t Sieve a a
s)
              (forall t {j} {k} (a :: k) (b :: j).
(HasFiniteCovers t k, FiniteCat j, FiniteCat k) =>
Sieve a b -> Bool
FinSheaf.isDense @t Sieve a a
s)
          | Sieve a a
s <- 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]
Finitary.elements @(Sieve :: j +-> k) @a @b
          ]
      )

-- | The uniqueness half of the sheaf condition at one cover: an element @x@ at the covered object
-- is the gluing of its own restrictions to the legs.
--
-- The other half (a glued element restricts back to the family) has no generic test: the only
-- matching family generic code can build is an element's own restrictions, and there it follows
-- from uniqueness. Other families come from the site, so that test is written per site. At a
-- finite site 'Proarrow.Category.Enriched.Finitary.Sheaf.isSheaf' decides both halves at once, by
-- checking that restriction is a bijection onto the matching families.
propGluesBack
  :: forall {j} {k} t (p :: j +-> k) (a :: k) (b :: j) c
   . (Sheaf.Sheaf t p, Ob a, Ob b, TestingEqShow (p a b))
  => Sheaf.Cover t k a c
  -> p a b
  -> Property ()
propGluesBack :: forall {j} {k} t (p :: j +-> k) (a :: k) (b :: j) c.
(Sheaf t p, Ob a, Ob b, TestingEqShow (p a b)) =>
Cover t k a c -> p a b -> Property ()
propGluesBack Cover t k a c
c p a b
x =
  String -> String -> p a b -> String -> p a b -> Property ()
forall a.
TestingEqShow a =>
String -> String -> a -> String -> a -> Property ()
testEq
    String
"uniqueness"
    String
"glue c (\\g -> lmap (legArrow g) x)"
    (forall {j} {k} t (p :: j +-> k) (a :: k) c (b :: j).
(Sheaf t p, Ob a, Ob b) =>
Cover t k a c -> (forall (x :: k). Leg t k a c x -> p x b) -> p a b
forall t (p :: j +-> k) (a :: k) c (b :: j).
(Sheaf t p, Ob a, Ob b) =>
Cover t k a c -> (forall (x :: k). Leg t k a c x -> p x b) -> p a b
Sheaf.glue @t Cover t k a c
c \Leg t k a c x
g -> (x ~> a) -> p a b -> p x b
forall (c :: k) (a :: k) (b :: j). (c ~> a) -> p a b -> p c b
forall {j} {k} (p :: j +-> k) (c :: k) (a :: k) (b :: j).
Profunctor p =>
(c ~> a) -> p a b -> p c b
lmap (Leg t k a c x -> x ~> a
forall (a :: k) c (x :: k). Leg t k a c x -> x ~> a
forall t k (a :: k) c (x :: k). Site t k => Leg t k a c x -> x ~> a
Sheaf.legArrow Leg t k a c x
g) p a b
x)
    String
"x"
    p a b
x

-- | 'propGluesBack' at every cover of a random element's object. Named for the law and not for the
-- class, since it is half of what 'Sheaf.Sheaf' asks for (see 'propGluesBack' for the other half).
--
-- The object is drawn from those that actually have a cover: on a site where only some objects are
-- covered, drawing uniformly would leave most runs asserting nothing while reporting successes.
testGluesBack
  :: forall {j} {k} t (p :: j +-> k)
   . (Sheaf.HasFiniteCovers t k, Sheaf.Sheaf t p, TestableProfunctor p, TestableTypeP p, TestObIsOb k)
  => TestTree
testGluesBack :: forall {j} {k} t (p :: j +-> k).
(HasFiniteCovers t k, Sheaf t p, TestableProfunctor p,
 TestableTypeP p, TestObIsOb k) =>
TestTree
testGluesBack = String -> Property () -> TestTree
testProperty String
"glues back" do
  Some @a <- forall k. Testable k => (Some k -> Bool) -> Property (Some k)
genObSuchThat @k \(Some @a) -> forall k (a :: k) r. (Testable k, TestOb a) => (Ob a => r) -> r
obFromTestOb @_ @a (Bool -> Bool
not ([SomeCover t k x] -> Bool
forall a. [a] -> Bool
forall (t :: Type -> Type) a. Foldable t => t a -> Bool
null (forall t k (a :: k).
(HasFiniteCovers t k, Ob a) =>
[SomeCover t k a]
Sheaf.covers @t @k @a)))
  Some @b <- genOb @j
  x <- genNamed @(p a b) "x"
  obFromTestOb @_ @a $
    obFromTestOb @_ @b $
      for_ (Sheaf.covers @t @k @a) \(Sheaf.SomeCover Cover t k x c
c) -> forall {j} {k} t (p :: j +-> k) (a :: k) (b :: j) c.
(Sheaf t p, Ob a, Ob b, TestingEqShow (p a b)) =>
Cover t k a c -> p a b -> Property ()
forall t (p :: j +-> k) (a :: k) (b :: j) c.
(Sheaf t p, Ob a, Ob b, TestingEqShow (p a b)) =>
Cover t k a c -> p a b -> Property ()
propGluesBack @t Cover t k x c
c p x x
x

-- | 'propGluesBack' at one named cover, for a site whose covers cannot be listed. The label names
-- the profunctor and cover, which nothing in the type can supply.
testGluesBackAt
  :: forall {j} {k} t (p :: j +-> k) (a :: k) c
   . (Sheaf.Sheaf t p, TestableProfunctor p, TestableTypeP p, TestOb a)
  => String
  -> Sheaf.Cover t k a c
  -> TestTree
testGluesBackAt :: forall {j} {k} t (p :: j +-> k) (a :: k) c.
(Sheaf t p, TestableProfunctor p, TestableTypeP p, TestOb a) =>
String -> Cover t k a c -> TestTree
testGluesBackAt String
lbl Cover t k a c
c = String -> Property () -> TestTree
testProperty (String
"glues back at " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
lbl) do
  Some @b <- forall k. Testable k => Property (Some k)
genOb @j
  x <- genNamed @(p a b) "x"
  obFromTestOb @_ @a $ obFromTestOb @_ @b $ propGluesBack @t c x

-- | An equalizer of sheaves is a sheaf, for every coverage: the 'Sheaf.Sheaf' instance for
-- 'FinTopos.Reindex' presupposes that the table cuts out a /subsheaf/, and this decides it, by
-- 'FinSheaf.isSheaf', on the equalizer of every pair of parallel arrows the palette of
-- @'FinSheaf.SHEAVES' t j k@ can form.
testEqualizersAreSheaves
  :: forall t j k
   . (Testable (FinSheaf.SHEAVES t j k), Sheaf.HasFiniteCovers t k, Finitary.FiniteCat j, Finitary.FiniteCat k)
  => TestTree
testEqualizersAreSheaves :: forall t j k.
(Testable (SHEAVES t j k), HasFiniteCovers t k, FiniteCat j,
 FiniteCat k) =>
TestTree
testEqualizersAreSheaves = String -> Property () -> TestTree
testProperty String
"equalizers are sheaves" do
  SomeP @a @b f <- forall {j} {k} (p :: j +-> k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
forall (p :: SHEAVES t j k +-> SHEAVES t j k).
TestableProfunctor p =>
String -> Property (SomeProfunctorElt p)
genProfunctorElt @(Hom (FinSheaf.SHEAVES t j k)) String
"f"
  g <- genNamed @(a ~> b) "g"
  Equalizer.equalize f g \(Sub (Prof @e a1 :~> b1
_)) -> String -> Bool -> Bool -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect String
"isSheaf of the equalizer" Bool
True (forall t {j} {k} (p :: j +-> k).
(HasFiniteCovers t k, Finitary p, FiniteCat j, FiniteCat k) =>
Bool
FinSheaf.isSheaf @t @e)

-- | Sheafification is the reflector into the sheaves: it turns a presheaf @p@ into the closest
-- sheaf, and every map from @p@ to a sheaf factors uniquely through it. For a finitary @p@ and a
-- sheaf @q@, decided at a finite site:
--
-- * @'FinSheaf.unitPlus'@ is natural;
-- * @'FinSheaf.Sheafify' t p@ is a sheaf, by 'FinSheaf.isSheaf';
-- * one plus construction fixes @q@ ('testPlusFixes');
-- * maps @'FinSheaf.Sheafify' t p ~> q@ correspond to maps @p ~> q@. The hom-sets of
--   @'FinTopos.FINITARY' j k@ are finite, so this is a count, made a bijection by
--   'FinSheaf.extendSheafify': extending every map @p ~> q@ gives every map out of the
--   sheafification once, and restricting an extension along the unit gives the map back.
--
-- This also makes 'FinSheaf.extendPlus'\'s choice of cover safe to leave unspecified: another
-- choice would show up here as an extension that is not one of the maps.
testSheafification
  :: forall t {j} {k} (p :: j +-> k) (q :: j +-> k)
   . ( Sheaf.HasFiniteCovers t k
     , Sheaf.Sheaf t q
     , Finitary.Finitary p
     , Finitary.Finitary q
     , Finitary.FiniteCat j
     , Finitary.FiniteCat k
     , Testable j
     , Testable k
     , TestableProfunctor p
     )
  => TestTree
testSheafification :: forall t {j} {k} (p :: j +-> k) (q :: j +-> k).
(HasFiniteCovers t k, Sheaf t q, Finitary p, Finitary q,
 FiniteCat j, FiniteCat k, Testable j, Testable k,
 TestableProfunctor p) =>
TestTree
testSheafification =
  String -> [TestTree] -> TestTree
testGroup
    String
"sheafification"
    [ String -> Property () -> TestTree
testProperty String
"unit is natural" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(TestableProfunctor p, TestableProfunctor q) =>
(p :~> q) -> Property ()
forall (p :: j +-> k) (q :: j +-> k).
(TestableProfunctor p, TestableProfunctor q) =>
(p :~> q) -> Property ()
propNaturalTransformation @p @(FinSheaf.Plus t p) (forall t {j} {k} (p :: j +-> k). Profunctor p => p :~> Plus t p
FinSheaf.unitPlus @t)
    , String -> Property () -> TestTree
testProperty String
"Sheafify p is a sheaf" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$ String -> Bool -> Bool -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect String
"isSheaf" Bool
True (forall t {j} {k} (p :: j +-> k).
(HasFiniteCovers t k, Finitary p, FiniteCat j, FiniteCat k) =>
Bool
FinSheaf.isSheaf @t @(FinSheaf.Sheafify t p))
    , forall t {j} {k} (q :: j +-> k).
(HasFiniteCovers t k, Finitary q, FiniteCat j, FiniteCat k) =>
TestTree
testPlusFixes @t @q
    , String -> Property () -> TestTree
testProperty String
"left adjoint to inclusion" do
        -- both enumerations once: each is a full walk of the finitary hom-set
        let maps :: [p ~> q]
maps = 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]
FinTopos.natTransformations @p @q
            exts :: [[Natural]]
exts = forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
[[Natural]]
forall (p :: j +-> k) (q :: j +-> k).
(Finitary p, Finitary q, FiniteCat j, FiniteCat k) =>
[[Natural]]
FinTopos.natElements @(FinSheaf.Sheafify t p) @q
        -- a hom-set is empty whenever @q@ runs out of elements where @p@ has some, and then every
        -- assertion below holds of nothing
        String -> Bool -> Bool -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect String
"there are maps to extend" Bool
True (Bool -> Bool
not ([p ~> q] -> Bool
forall a. [a] -> Bool
forall (t :: Type -> Type) a. Foldable t => t a -> Bool
null [p ~> q]
maps))
        String -> Int -> Int -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect String
"as many maps out of the sheafification as out of p" ([p ~> q] -> Int
forall a. [a] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [p ~> q]
maps) ([[Natural]] -> Int
forall a. [a] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [[Natural]]
exts)
        String -> [[Natural]] -> [[Natural]] -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
          String
"the extensions are exactly the maps out of the sheafification"
          ([[Natural]] -> [[Natural]]
forall a. Ord a => [a] -> [a]
sort [[Natural]]
exts)
          ([[Natural]] -> [[Natural]]
forall a. Ord a => [a] -> [a]
sort [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]
FinTopos.natTable @(FinSheaf.Sheafify t p) @q (forall t {j} {k} (p :: j +-> k) (q :: j +-> k).
(HasFiniteCovers t k, Sheaf t q) =>
(p :~> q) -> Sheafify t p :~> q
FinSheaf.extendSheafify @t p a b -> q a b
p :~> q
n) | Prof p :~> q
n <- [p ~> q]
maps])
        [p ~> q] -> ((p ~> q) -> Property ()) -> Property ()
forall (t :: Type -> Type) (f :: Type -> Type) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [p ~> q]
maps \(Prof p :~> q
n) ->
          String -> [Natural] -> [Natural] -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
            String
"restricting an extension along the unit gives the map back"
            (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]
FinTopos.natTable @p @q p a b -> q a b
p :~> q
n)
            (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]
FinTopos.natTable @p @q \p a b
x -> forall t {j} {k} (p :: j +-> k) (q :: j +-> k).
(HasFiniteCovers t k, Sheaf t q) =>
(p :~> q) -> Sheafify t p :~> q
FinSheaf.extendSheafify @t p a b -> q a b
p :~> q
n (forall t {j} {k} (p :: j +-> k). Profunctor p => p :~> Sheafify t p
FinSheaf.unitSheafify @t p a b
x))
    ]

-- | One plus leaves a sheaf as it was: 'FinSheaf.unitPlus' is a bijection at every pair of objects.
-- Stated for any finitary @q@ (the property needs no 'Sheaf.Sheaf' instance, only 'FinSheaf.isSheaf'
-- to be true of @q@), so it also serves at a coverage no profunctor has an instance for, such as the
-- trivial one, which fixes everything.
testPlusFixes
  :: forall t {j} {k} (q :: j +-> k)
   . (Sheaf.HasFiniteCovers t k, Finitary.Finitary q, Finitary.FiniteCat j, Finitary.FiniteCat k)
  => TestTree
testPlusFixes :: forall t {j} {k} (q :: j +-> k).
(HasFiniteCovers t k, Finitary q, FiniteCat j, FiniteCat k) =>
TestTree
testPlusFixes =
  String -> Property () -> TestTree
testProperty String
"one plus fixes q" (Property () -> TestTree) -> Property () -> TestTree
forall a b. (a -> b) -> a -> b
$
    [Property ()] -> Property ()
forall (t :: Type -> Type) (m :: Type -> Type) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
      ( forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @k @(Property ()) \ @a -> forall k r. Enumerable k => (forall (a :: k). Ob a => [r]) -> [r]
Finitary.foreachOb @j @(Property ()) \ @b ->
          -- bound once for the hom-set: at 'FinSheaf.Plus' one 'Finitary.toIndex' is an enumeration
          let ix :: Plus t q a a -> Natural
ix = 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
Finitary.toIndex @(FinSheaf.Plus t q) @a @b
          in [ String -> [Natural] -> [Natural] -> Property ()
forall a. (Eq a, Show a) => String -> a -> a -> Property ()
expect
                 (String
"unit is a bijection at object " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Natural -> String
forall a. Show a => a -> String
show (forall (a :: k). (Enumerable k, Ob a) => Natural
forall {k} (a :: k). (Enumerable k, Ob a) => Natural
Finitary.objIndex @a))
                 (Natural -> [Natural]
Finitary.indices (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
Natural
forall (p :: j +-> k) (a :: k) (b :: j).
(Finitary p, Ob a, Ob b) =>
Natural
Finitary.size @(FinSheaf.Plus t q) @a @b))
                 ([Natural] -> [Natural]
forall a. Ord a => [a] -> [a]
sort [Plus t q a a -> Natural
ix (forall t {j} {k} (p :: j +-> k). Profunctor p => p :~> Plus t p
FinSheaf.unitPlus @t q a a
x) | q a a
x <- 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]
Finitary.elements @q @a @b])
             ]
      )