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}