Martin Escardo, July 2026.

Setoids, for the development of groups in pure MLTT (see EGroups.Type
and EGroups.FreeOnType).

A setoid is a type equipped with an equivalence relation, given as
data. We do *not* require the relation to be proposition-valued,
because the relation _โˆฟ_ used to construct free groups is not, which
is precisely why Groups.Free truncates it before quotienting, and here
we want to use it as it is.

This module collects the notion of setoid and the generic setoid
infrastructure that is not specific to groups, namely the
relation-relative algebraic predicates, equational reasoning, setoid
maps, setoid isomorphism, and the function setoid.

\begin{code}

{-# OPTIONS --safe --without-K #-}

module EGroups.Setoid where

open import MLTT.Spartan

is-equivalence-relation : {X : ๐“ค ฬ‡ } โ†’ (X โ†’ X โ†’ ๐“ฅ ฬ‡ ) โ†’ ๐“ค โŠ” ๐“ฅ ฬ‡
is-equivalence-relation _โ‰ˆ_ = reflexive  _โ‰ˆ_
                            ร— symmetric  _โ‰ˆ_
                            ร— transitive _โ‰ˆ_

Setoid : (๐“ค ๐“ฅ : Universe) โ†’ (๐“ค โŠ” ๐“ฅ)โบ ฬ‡
Setoid ๐“ค ๐“ฅ = ฮฃ X ๊ž‰ ๐“ค ฬ‡ , ฮฃ R ๊ž‰ (X โ†’ X โ†’ ๐“ฅ ฬ‡ ) , is-equivalence-relation R

\end{code}

We write โˆฃ S โˆฃ for the underlying type of a setoid S and x โ‰ˆโŸฆ S โŸง y
for its equivalence relation.

\begin{code}

โˆฃ_โˆฃ : Setoid ๐“ค ๐“ฅ โ†’ ๐“ค ฬ‡
โˆฃ S โˆฃ = prโ‚ S

setoid-relation : (S : Setoid ๐“ค ๐“ฅ) โ†’ โˆฃ S โˆฃ โ†’ โˆฃ S โˆฃ โ†’ ๐“ฅ ฬ‡
setoid-relation S = prโ‚ (prโ‚‚ S)

syntax setoid-relation S x y = x โ‰ˆโŸฆ S โŸง y

setoid-refl : (S : Setoid ๐“ค ๐“ฅ) โ†’ reflexive (setoid-relation S)
setoid-refl S = prโ‚ (prโ‚‚ (prโ‚‚ S))

setoid-sym : (S : Setoid ๐“ค ๐“ฅ) โ†’ symmetric (setoid-relation S)
setoid-sym S = prโ‚ (prโ‚‚ (prโ‚‚ (prโ‚‚ S)))

setoid-trans : (S : Setoid ๐“ค ๐“ฅ) โ†’ transitive (setoid-relation S)
setoid-trans S = prโ‚‚ (prโ‚‚ (prโ‚‚ (prโ‚‚ S)))

\end{code}

The group laws and the congruence condition, stated up to a given
relation _โ‰ˆ_ rather than up to the identity type ๏ผ. These are the
relation-relative counterparts of associative, left-neutral,
right-neutral from Notation.General. In the HoTT/UF setting the
congruence condition is automatic, via ap, but here it is data.

\begin{code}

is-congruence : {X : ๐“ค ฬ‡ } โ†’ (X โ†’ X โ†’ ๐“ฅ ฬ‡ ) โ†’ (X โ†’ X โ†’ X) โ†’ ๐“ค โŠ” ๐“ฅ ฬ‡
is-congruence _โ‰ˆ_ _ยท_ = {x x' y y' : _} โ†’ x โ‰ˆ x' โ†’ y โ‰ˆ y' โ†’ (x ยท y) โ‰ˆ (x' ยท y')

โ‰ˆ-left-neutral : {X : ๐“ค ฬ‡ } โ†’ (X โ†’ X โ†’ ๐“ฅ ฬ‡ ) โ†’ X โ†’ (X โ†’ X โ†’ X) โ†’ ๐“ค โŠ” ๐“ฅ ฬ‡
โ‰ˆ-left-neutral _โ‰ˆ_ e _ยท_ = โˆ€ x โ†’ (e ยท x) โ‰ˆ x

โ‰ˆ-right-neutral : {X : ๐“ค ฬ‡ } โ†’ (X โ†’ X โ†’ ๐“ฅ ฬ‡ ) โ†’ X โ†’ (X โ†’ X โ†’ X) โ†’ ๐“ค โŠ” ๐“ฅ ฬ‡
โ‰ˆ-right-neutral _โ‰ˆ_ e _ยท_ = โˆ€ x โ†’ (x ยท e) โ‰ˆ x

โ‰ˆ-associative : {X : ๐“ค ฬ‡ } โ†’ (X โ†’ X โ†’ ๐“ฅ ฬ‡ ) โ†’ (X โ†’ X โ†’ X) โ†’ ๐“ค โŠ” ๐“ฅ ฬ‡
โ‰ˆ-associative _โ‰ˆ_ _ยท_ = โˆ€ x y z โ†’ ((x ยท y) ยท z) โ‰ˆ (x ยท (y ยท z))

\end{code}

Equational reasoning up to an equivalence relation, parameterized by
reflexivity and transitivity.

\begin{code}

module โ‰ˆ-reasoning
        {X : ๐“ค ฬ‡ }
        (_โ‰ˆ_ : X โ†’ X โ†’ ๐“ฅ ฬ‡ )
        (โ‰ˆr : reflexive  _โ‰ˆ_)
        (โ‰ˆt : transitive _โ‰ˆ_)
       where

 infixr 0 _โ‰ˆ[_]_
 infix  1 _โ‰ˆโˆŽ

 _โ‰ˆ[_]_ : (x : X) {y z : X} โ†’ x โ‰ˆ y โ†’ y โ‰ˆ z โ†’ x โ‰ˆ z
 x โ‰ˆ[ p ] q = โ‰ˆt x _ _ p q

 _โ‰ˆโˆŽ : (x : X) โ†’ x โ‰ˆ x
 x โ‰ˆโˆŽ = โ‰ˆr x

 ๏ผ-to-โ‰ˆ : {x y : X} โ†’ x ๏ผ y โ†’ x โ‰ˆ y
 ๏ผ-to-โ‰ˆ {x} refl = โ‰ˆr x

\end{code}

A setoid map is a function that respects the equivalence relations.

\begin{code}

is-setoid-map : (S : Setoid ๐“ค ๐“ฅ) (T : Setoid ๐“ค' ๐“ฅ')
              โ†’ (โˆฃ S โˆฃ โ†’ โˆฃ T โˆฃ) โ†’ ๐“ค โŠ” ๐“ฅ โŠ” ๐“ฅ' ฬ‡
is-setoid-map S T f = {x y : โˆฃ S โˆฃ} โ†’ x โ‰ˆโŸฆ S โŸง y โ†’ f x โ‰ˆโŸฆ T โŸง f y

\end{code}

A setoid isomorphism is a pair of setoid maps that are mutually inverse
up to the equivalence relations.

\begin{code}

record _โ‰…หข_ (S : Setoid ๐“ค ๐“ฅ) (T : Setoid ๐“ค' ๐“ฅ') : ๐“ค โŠ” ๐“ฅ โŠ” ๐“ค' โŠ” ๐“ฅ' ฬ‡ where
 field
  to        : โˆฃ S โˆฃ โ†’ โˆฃ T โˆฃ
  from      : โˆฃ T โˆฃ โ†’ โˆฃ S โˆฃ
  to-resp   : is-setoid-map S T to
  from-resp : is-setoid-map T S from
  to-from   : (y : โˆฃ T โˆฃ) โ†’ to (from y) โ‰ˆโŸฆ T โŸง y
  from-to   : (x : โˆฃ S โˆฃ) โ†’ from (to x) โ‰ˆโŸฆ S โŸง x

\end{code}

The function setoid from a type A into a setoid T, namely functions
A โ†’ โˆฃ T โˆฃ with the pointwise equivalence relation.

\begin{code}

function-setoid : (A : ๐“ค ฬ‡ ) (T : Setoid ๐“ฅ ๐“ฆ) โ†’ Setoid (๐“ค โŠ” ๐“ฅ) (๐“ค โŠ” ๐“ฆ)
function-setoid A T =
   (A โ†’ โˆฃ T โˆฃ)
 , (ฮป f g โ†’ (a : A) โ†’ f a โ‰ˆโŸฆ T โŸง g a)
 , (ฮป f a โ†’ setoid-refl T (f a))
 , (ฮป f g p a โ†’ setoid-sym T (f a) (g a) (p a))
 , (ฮป f g h p q a โ†’ setoid-trans T (f a) (g a) (h a) (p a) (q a))

\end{code}

The setoid of setoid maps from S to T (the codomain of the free-egroup
adjunction on setoids), with the pointwise equivalence relation.

\begin{code}

setoid-map-setoid : (S : Setoid ๐“ค ๐“ฅ) (T : Setoid ๐“ค' ๐“ฅ')
                  โ†’ Setoid (๐“ค โŠ” ๐“ฅ โŠ” ๐“ค' โŠ” ๐“ฅ') (๐“ค โŠ” ๐“ฅ')
setoid-map-setoid S T =
   (ฮฃ f ๊ž‰ (โˆฃ S โˆฃ โ†’ โˆฃ T โˆฃ) , is-setoid-map S T f)
 , (ฮป u v โ†’ (x : โˆฃ S โˆฃ) โ†’ prโ‚ u x โ‰ˆโŸฆ T โŸง prโ‚ v x)
 , (ฮป u x โ†’ setoid-refl T (prโ‚ u x))
 , (ฮป u v p x โ†’ setoid-sym T (prโ‚ u x) (prโ‚ v x) (p x))
 , (ฮป u v w p q x โ†’ setoid-trans T (prโ‚ u x) (prโ‚ v x) (prโ‚ w x) (p x) (q x))

\end{code}