Martin Escardo, July 2026.

Setoids, for the development of free groups in a Spartan MLTT.

A setoid is a type equipped with an equivalence relation with values
given as data. We collect here the notion of setoid, together with the
generic setoid infrastructure that is not specific to groups.

\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 ๐“ค ๐“ฅ โ†’ ๐“ค ฬ‡
โˆฃ (X , _โ‰ˆ_ , r , s , t) โˆฃ = X

setoid-relation : (S : Setoid ๐“ค ๐“ฅ) โ†’ โˆฃ S โˆฃ โ†’ โˆฃ S โˆฃ โ†’ ๐“ฅ ฬ‡
setoid-relation (X , _โ‰ˆ_ , r , s , t) = _โ‰ˆ_

syntax setoid-relation S x y = x โ‰ˆโˆฃ S โˆฃ y

setoid-refl : (S : Setoid ๐“ค ๐“ฅ) โ†’ reflexive (setoid-relation S)
setoid-refl (X , _โ‰ˆ_ , r , s , t) = r

setoid-sym : (S : Setoid ๐“ค ๐“ฅ) โ†’ symmetric (setoid-relation S)
setoid-sym (X , _โ‰ˆ_ , r , s , t) = s

setoid-trans : (S : Setoid ๐“ค ๐“ฅ) โ†’ transitive (setoid-relation S)
setoid-trans (X , _โ‰ˆ_ , r , s , t) = t

\end{code}

Some general notions here stated with respect to an equivalence
relation, rather than the identity type.

We use the prefix `is-` for them, unlike their counterparts in
Notation.General. In the HoTT/UF setting that prefix indicates
property in the sense of being subsingleton-valued. Here property is
in the sense of MLTT with setoids, which is just propositions as
types, rather than propositions as types with at most one element, and
it is in this sense that we use the prefix.

\begin{code}

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

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

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

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

\end{code}

We develop 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}

We form the function setoid from a type A into a setoid T, whose
elements are the 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}

We form the setoid of setoid maps from S to T, again 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}