How to formalize dependent setoid morphisms?

At the bottom of the post you can find a formalization of “setoids” and “setoid morphisms” (nothing fancy except it’s u-polymorphic: but I am still on Coq 8.18 – and please don’t mind the naming too much, this is all still draft level).

With those definitions, I can e.g. start formalizing “wild categories” (by which I just mean that Ob and Mor are arbitrary “setoids”), and, in particular, I can make dom/cod “setoid morphisms”:

(** The type of "category data". *)
Structure CatData@{i o} : Type@{o} :=
  mkCatData {
    Ob : ESet@{i o};
    Mor : ESet@{i o};
    dom : Mor --> Ob;
    cod : Mor --> Ob;
  } as Cd.
Arguments dom {Cd}.
Arguments cod {Cd}.

Notation "x '=o=' y" := (weqv (eeqv (Ob _)) x y)
  (at level 70, no associativity).
Notation "f '=m=' g" := (weqv (eeqv (Mor _)) f g)
  (at level 70, no associativity).

Definition Hom@{i o} Cd (x y : Ob@{i o} Cd) : Type@{o} :=
  {f : Mor Cd |- dom f =o= x /\ cod f =o= y}.  (** u-poly as [psig] *)

Definition HomEqv@{i o} Cd (w x y z : Ob@{i o} Cd)
    (fH : Hom w x) (gH : Hom y z) : Prop :=
  psig_pi1@{i o} fH =m= psig_pi1@{i o} gH.

(* etc. etc. *)

But later (I think) I get to need the dependent version of “setoid morphisms”, e.g. here:

(** The type of "functor data" from [B] to [C]. *)
Structure FunData@{i o} (B C : Cat@{i o}) : Type@{o} :=
  mkFunData {
    fOb : Ob B --> Ob C;
    fMor : forall (f : Mor B),  (** not yet a (dep.) morphism: but what is that? *)
      Hom (fOb (dom f)) (fOb (cod f));
  } as Fd.

(I am not showing the whole code up to Cat to try and keep things short: but let me know if you’d rather want me to post it.)


That much for motivation. I’d have two questions:

  1. incidentally, am I right that I need “setoid morphisms”, not just “functions” for a formalization of these “wild categories” that is faithful and also works down the line?

  2. but, especially, I (seem to) get to needing a dependent version of “setoid morphisms”, but how should I formulate it?

This is what I have started writing, beginning with the idea that wfor must be a “dependent function”, but then wfor_prop doesn’t seem to make much sense, as it lands in two distinct WSets, FT x and FT y, so I seem to be missing some piece, maybe some kind of higher-order equivalence…?

(** The type of "dep. pre-morphisms" between [WSet]s with [WEqv]. *)
Structure WFor@{i o} (S : WSet@{i o}) (FT : S -> WSet@{i o})
    (ES : WEqv@{i o} S) (EFT : forall x, WEqv@{i o} (FT x)) : Type@{o} :=
  mkWFor {
    (** The "operation". *)
    wfor :> forall x, FT x;
    (** [wfor] is "coherent" with [weqv]. *)
    wfor_prop : forall x y,
        weqv ES x y -> weqv _ (wfor x) (wfor y);  (** this fails *)
  } as FFT.

I have also looked at the definition of respectful_hetero from the StdLib, but I cannot figure out if/how it would apply.

I think I am just missing the point somewhere: any help would be very much appreciated.

Thank you.


(********************************************************************)
(** ** SETUP                                                        *)
(********************************************************************)

Set Implicit Arguments.
Unset Strict Implicit.
Unset Strongly Strict Implicit.
Unset Contextual Implicit.
Unset Reversible Pattern Implicit.
Unset Printing Implicit Defensive.

Set Universe Polymorphism.
Set Polymorphic Inductive Cumulativity.
Set Cumulativity Weak Constraints.
Unset Universe Minimization ToSet.

Set Primitive Projections.

(********************************************************************)
(** ** Pre-sets                                                     *)
(********************************************************************)

(** The type of "pre-sets". *)
Structure WSet@{i o} : Type@{o} :=
  mkWSet {
    wset :> Type@{i};
  } as S.

(** The type of "pre-relations" on a [WSet]. *)
Definition WRel@{i o} (S : WSet@{i o}) : Type@{o} :=
  S -> S -> Prop.

(** Represents "is reflexive" of a [WRel]. *)
Definition WRelRefl@{i o} S (RS : WRel@{i o} S) : Type@{o} :=
  forall x, RS x x.

(** Represents "is symmetric" of a [WRel]. *)
Definition WRelSymm@{i o} S (RS : WRel@{i o} S) : Type@{o} :=
  forall x y, RS x y -> RS y x.

(** Represents "is transitive" of a [WRel]. *)
Definition WRelTran@{i o} S (RS : WRel@{i o} S) : Type@{o} :=
  forall x y z, RS x y -> RS y z -> RS x z.

(** The type of "pre-equivalences" on a [WSet]. *)
Structure WEqv@{i o} (S : WSet@{i o}) : Type@{o} :=
  mkWEqv {
    (** The "predicate". *)
    weqv :> WRel@{i o} S;
    (** [weq] is "reflexive". *)
    weqv_refl : WRelRefl weqv;
    (** [weq] is "symmetric". *)
    weqv_symm : WRelSymm weqv;
    (** [weq] is "transitive". *)
    weqv_tran : WRelTran weqv;
  } as ES.

(** The type of "pre-morphisms" between [WSet]s with [WEqv]. *)
Structure WMor@{i o} S T (ES : WEqv@{i o} S)
                         (ET : WEqv@{i o} T) : Type@{o} :=
  mkWMor {
    (** The "operation". *)
    wfun :> S -> T;
    (** [wfun] is "coherent" with [weqv]. *)
    wfun_prop : forall x y,
      weqv ES x y -> weqv ET (wfun x) (wfun y);
  } as MST.

(********************************************************************)
(** ** Setoids                                                      *)
(********************************************************************)

(** The type of "setoids". *)
Structure ESet@{i o} : Type@{o} :=
  mkESet {
    eset :> WSet@{i o};
    eeqv : WEqv@{i o} eset;
  } as S.

(** The type of "setoid morphisms". *)
Definition EMor@{i o} (S T : ESet@{i o}) : Type@{o} :=
  WMor@{i o} (eeqv S) (eeqv T).

Notation "S '-->' T" := (EMor S T)
  (at level 99, no associativity).

After staring at the definition of Difunctional for some while, I think I start getting the idea…

Will post the code if/when I get to the bottom of it.

For the chronicle, I almost have it, though I am finding it quite tricky, but I have also meanwhile realised/learned (*) that I need my morphisms to not just preserve equivalences but also be embeddings/be injective (right?).

And then I was thinking, couldn’t I achieve the same with just apartness relations (instead of equivalences), and morphisms that simply preserve apartness? I think so, though I’d have to prove it…

Anyway, that leads me to two side-questions:

  • Does a type endowed with an apartness relation have a specific name in the literature? (I’d guess it’s not still called a “setoid”, but what is it called?)

  • Also, I am reading about apartness relation on nlab, then about strongly extensional function, and I do not understand the last sentence of the definition there: initially, it says (slightly adapted) “f is strongly extensional if a <> b whenever f a <> f b”, but then, in the very last paragraph, it says “a strongly extensional function is strongly injective if it also preserves <>”, but I do not see the difference, is that not already what a “strongly extensional” function preserves?

I suppose that is all quite basic, but there I am. Any help/clarification would be most appreciated.

(*) N. Favier’s answer on SE to the question
“transport over equality of f a ≡ f a' in Agda”
https://proofassistants.stackexchange.com/a/5141

Ah, sorry, I think I got that part: the difference between “preserving” and “reflecting”…

Still, how would I call a type endowed with an apartness relation as opposed to an equivalence one? – ChatGTP suggests “constructive set” in the tradition of Bishop and constructive mathematics: which is the best I have been able to find so far…

If I am reading this correctly, these are still called “setoids”, maybe “constructive setoids” if one cares about the distinction:

G Barthe, V Capretta, O Pons, “Setoids in type theory” (2003)
https://www.cambridge.org/core/journals/journal-of-functional-programming/article/setoids-in-type-theory/6A223F72737E421BD9D642C14EB5600B

BTW, not getting into an infinite regress is the hardest problem I have been facing in my formalization efforts all along: e.g. mainly following nLab, I was trying to formalize categories and find myself formalizing setoids, then I was trying to formalize setoids and find myself formalizing relations, and in fact I find no bottom to it, since most definitions are eventually themselves in categorical terms, which is simply circular…

More generally, the problem is that of formalizing an encyclopedic compendium (which is especially the form of problem domains and informal discourse, as opposed to the solution domains and the formalizations). “Inversion of control” solves circularity (circular dependencies) in engineering, where we abstract over a dependency and make the abstraction a parameter: which here becomes, I suppose, some notion of (abstractly parametric) pure theories… – Comments/suggestions?

Work in progress…

I apologise for lots of imprecisions in my previous posts: among others, I should have called those “H-Categories”, itself an extension of “E-categories”, not “wild categories”, etc. etc. That said, I think the answer to my question here is eventually not that difficult, i.e. once realised that a setoid-indexed family of setoids is the partition of a specific “trivial” cover… or something along that line, plus I have been using apartness instead of equivalence.

But I am giving up on this, because I find myself having to rewrite the whole standard Prelude in order to get universe polymorphism (I am on Coq 8.18, anyway that target is still moving as of Rocq 9, AIUI): not only I better not reinvent that wheel (and with tools that are possibly not even yet fully adequate), but things are getting farther and farther away from my immediate interest, which was just that of “formalizing category theory while following along the course(s) by Bartosz Milewski”. For now I suppose I will just stick with my initial “elementary” (strict?) categories with some classical axioms (still hoping the monomorphic universes creeping in won’t be a show stopper down the line), and will hack the constructive side of things another day…

Anyway, this was quite an interesting aside, about extensionality in constructive settings, and here are few links to info particularly on setoids that I have found quite useful (there is not much else publicly available, AFAICT: indeed, as I get it, this is still an open area of research), mainly along the lines of Bishop’s constructive mathematics:

https://en.wikipedia.org/wiki/Setoid

https://ncatlab.org/nlab/show/setoid
https://ncatlab.org/nlab/show/preset#in_type_theory
https://ncatlab.org/nlab/show/Bishop+set

Bishop’s set theory
by E Palmgren (2005)
https://ncatlab.org/nlab/files/Palmgren-BishopSetTheory.pdf

Bishop-style constructive mathematics in type theory
by E Palmgren (2013)
https://staff.math.su.se/palmgren/Palmgren_Nis.pdf

From type theory to setoids and back
by E Palmgren (2018) - YouTube
https://www.youtube.com/watch?v=se22z_YzD2g

Setoids in type theory
by G Barthe, V Capretta (2003)
https://www.cambridge.org/core/journals/journal-of-functional-programming/article/setoids-in-type-theory/6A223F72737E421BD9D642C14EB5600B

Ch. 3: Representation of Bishop set theory in type theory
Towards Constructive Homological Algebra in Type Theory
by T Coquand, A Spiwack (2007)
https://inria.hal.science/inria-00432525

Extensional concepts in intensional type theory
by M Hofmann (1995)
https://ncatlab.org/nlab/files/HofmannExtensionalIntensionalTypeTheory.pdf

Equivalents of “union” and “intersection” for setoids?
answer by user860795 (2022) - Mathematics Stack Exchange
https://math.stackexchange.com/questions/4475020/4475046#4475046

Equivalence relation & partition definitions …
answer by N Schweber (2019) - Mathematics Stack Exchange
https://math.stackexchange.com/questions/3328928/3328947#3328947