-- | __Sieves__: a @'Sieve' a b@ is a set of pairs @(g :: c '~>' a, h :: b '~>' d)@, given as a
-- predicate, that is closed under composing on the outside: if @(g, h)@ is in, so is
-- @(g '.' l, r '.' h)@ for any @l@ and @r@. With @b@ ignored this is the textbook sieve on @a@, a
-- set of arrows into @a@ closed under precomposition.
--
-- __The constructor does not check this closure.__ @toIndex@ of
-- 'Proarrow.Category.Enriched.Finitary.Finitary' (failing with @\"not a sieve\"@) and
-- 'Proarrow.Category.Enriched.Finitary.Topos.withSubobject' check it. Others presuppose it, e.g.
-- 'Proarrow.Category.Enriched.Finitary.Sheaf.coveringCover' judges a sieve by a cover's legs, so
-- on a non-sieve the two kinds of function disagree. A hand-built 'Sieve' must be closed.
--
-- Sieves are the subobjects of the representable at @a@\/@b@, so they are the truth values of a
-- category of profunctors: "Proarrow.Category.Enriched.Finitary.Topos" makes them the
-- 'Proarrow.Category.Topos.HasSubobjectClassifier' of the finitary ones.
module Proarrow.Profunctor.Instance.Sieve where

import Prelude (Bool (..), (&&))

import Proarrow.Core (CategoryOf (..), Profunctor (..), Promonad (..), (//), type (+->))

type Sieve :: forall {j} {k}. j +-> k
data Sieve a b where
  Sieve :: (Ob a, Ob b) => (forall c d. c ~> a -> b ~> d -> Bool) -> Sieve a b

instance (CategoryOf j, CategoryOf k) => Profunctor (Sieve :: j +-> k) where
  dimap :: forall (c :: k) (a :: k) (b :: j) (d :: j).
(c ~> a) -> (b ~> d) -> Sieve a b -> Sieve c d
dimap c ~> a
l b ~> d
r (Sieve forall (c :: k) (d :: j). (c ~> a) -> (b ~> d) -> Bool
s) = c ~> a
l (c ~> a) -> ((Ob c, Ob a) => Sieve c d) -> Sieve c d
forall {k1} {k2} (p :: k1 +-> k2) (a :: k2) (b :: k1) r.
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
// b ~> d
r (b ~> d) -> ((Ob b, Ob d) => Sieve c d) -> Sieve c d
forall {k1} {k2} (p :: k1 +-> k2) (a :: k2) (b :: k1) r.
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
// (forall (c :: k) (d :: j). (c ~> c) -> (d ~> d) -> Bool)
-> Sieve c d
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
g d ~> d
h -> (c ~> a) -> (b ~> d) -> Bool
forall (c :: k) (d :: j). (c ~> a) -> (b ~> d) -> Bool
s (c ~> a
l (c ~> a) -> (c ~> c) -> c ~> 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
. c ~> c
g) (d ~> d
h (d ~> d) -> (b ~> d) -> b ~> d
forall (b :: j) (c :: j) (a :: j). (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 ~> d
r)
  (Ob a, Ob b) => r
r \\ :: forall (a :: k) (b :: j) r. ((Ob a, Ob b) => r) -> Sieve a b -> r
\\ Sieve{} = r
(Ob a, Ob b) => r
r

-- | The sieve that contains every arrow. 'Sieve' is the /object/ of truth values of a category of
-- profunctors, and this is its value @yes@, so it is the 'Proarrow.Category.Topos.true' of their
-- subobject classifier. A sieve is called /dense/ for a coverage when its closure is this one (see
-- 'Proarrow.Category.Enriched.Finitary.Sheaf.closure').
maximalSieve :: forall {j} {k} (a :: k) (b :: j). (CategoryOf j, CategoryOf k, Ob a, Ob b) => Sieve a b
maximalSieve :: forall {j} {k} (a :: k) (b :: j).
(CategoryOf j, CategoryOf k, Ob a, Ob b) =>
Sieve a b
maximalSieve = (forall (c :: k) (d :: j). (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 ~> a
_ b ~> d
_ -> Bool
True

-- | The sieve of arrows in both: the @and@ of the truth values the two stand for. This is
-- 'Proarrow.Category.Topos.and' at the classifier of
-- "Proarrow.Category.Enriched.Finitary.Topos", computed directly instead of as an arrow. Being
-- closed under composition is pointwise, so the meet of two sieves is again one.
-- 'Proarrow.Category.Enriched.Finitary.Sheaf.Plus' is computed on the meet of all the /dense/
-- sieves at a pair of objects.
sieveMeet :: Sieve a b -> Sieve a b -> Sieve a b
sieveMeet :: forall {k} {j} (a :: k) (b :: j).
Sieve a b -> Sieve a b -> Sieve a b
sieveMeet (Sieve forall (c :: k) (d :: j). (c ~> a) -> (b ~> d) -> Bool
s) (Sieve forall (c :: k) (d :: j). (c ~> a) -> (b ~> d) -> Bool
s') = (forall (c :: k) (d :: j). (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 ~> a
g b ~> d
h -> (c ~> a) -> (b ~> d) -> Bool
forall (c :: k) (d :: j). (c ~> a) -> (b ~> d) -> Bool
s c ~> a
g b ~> d
h Bool -> Bool -> Bool
&& (c ~> a) -> (b ~> d) -> Bool
forall (c :: k) (d :: j). (c ~> a) -> (b ~> d) -> Bool
s' c ~> a
g b ~> d
h