- Equalities
- Definitional Equality
- Computational Equality
- Propositional Equality
- Equivalence, with universe polymorphism
- Setoids

```
module Types.equality where
open import Lang.dataStructures using (
; true; false;
Bool; ⊤; ℕ; List;
⟂; two; three; four; five; six; seven; eight; nine; ten; zero; succ; _+_;
one_::_; [])
open import Agda.Primitive using (Level; _⊔_; lsuc; lzero)
open import Types.functions using (_on_; flip)
open import Types.relations
```

Equality is perhaps one of the most richest but most naively understood concepts. Here we try to provide some structural analysis as to what equality really means in various contexts of mathematics. Equality is treated as a relation in type theory and can be classified broadly as of three kinds:

- Propositional equality
- Computational equality
- Definitional equality

Definitional equality is the most basic notion of equality which appeals to our notion of equality being the sameness of meaning (by definition). For example, `9`

and `3 + 3`

represent the same thing and hence are definitionally equal `9 ≡ 3²`

. Similarly `two ≡ succ (succ zero)`

.

```
: ℕ
defEqual₁ = seven
defEqual₁
: ℕ
defEqual₂ = succ (succ five) defEqual₂
```

Here, `defEqual₁`

and `defEqual₂`

both are equal, with equality of the kind “definitional equality”.

This kind of equality describes the sameness of types that are not directly equal but can be reduced to be equal. “Reduction” here implies mathematical reduction, referring to rewriting expressions into simpler forms. An example of such an equality is applying a function \[(λ x.x+x)(2) ≡ 2 + 2\] Expansions of recursors also falls under this kind of equality: \[2 + 2 ≡ succ (succ zero) + succ (succ zero) ≡ succ (succ (succ (succ zero)))\] Practically, computational equality is included into definitional equality and is also known as “Judgemental equality”.

Definitional and computational equalities describe something intrinsic - a property that does not depend upon a proof. For example, `a + b ≡ b + a`

cannot be proven to be definitionally equal unless the concrete values of `a`

and `b`

are known. However, if they’re natural numbers `a, b ∈ ℕ`

, then the statement `a + b ≡ b + a`

requires a proof to claim its truthfulness. Given `a, b ∈ ℕ`

, we can prove that `a + b ≡ b + a`

or in other words that there exists an identity of type `Id : a + b ≡ b + a`

where `Id`

is a proposition − exhibiting a term belonging to such a type is exhibiting (i.e. proving) such a propositional equality.

However, other notions of equalities can be defined that do require proofs. Consider for example natural language - when we say “all flowers are beautiful” the “all flowers” part of the sentence implies all flowers are equal in some way. Or, consider natural numbers `a + b = b + a ∀ a, b ∈ ℕ`

. Here we would need to prove the symmetry of the `+`

operator in order to prove the equality. Such equalities that require to be specified come under the umbrella of propositional equality. Propositional equality is a kind of equality which requires a proof, and hence the equality itself is also a type `∼`

:

```
4 _∼_
infix
data _∼_ {A : Set}(a : A) : {B : Set} → B → Set where
: a ∼ a same
```

Reflexivity is defined with the definition of `∼`

by the keyword `same`

, the others being:

Symmetry is the property where binary a relation’s behavior does not depend upon its argument’s position (left or right):

```
: ∀ {A B}{a : A}{b : B}
symmetry → a ∼ b
→ b ∼ a
= same symmetry same
```

Transitivity is when a binary relation `_∼_`

and \(x ∼ y and y ∼ z ⟹ x ∼ z\)

```
: ∀ {A B C}{a : A}{b : B}{c : C}
transitivity → a ∼ b
→ b ∼ c
→ a ∼ c
= p transitivity same p
```

Functions that when applied to objects of a type, do not alter the operation of equality can be defined as:

```
: ∀ {A B : Set} (f : A → B) {x y : A}
congruence → x ∼ y
→ f x ∼ f y
= same congruence f same
```

If `a = b`

and if `predicate a = true`

⟹ `predicate b = true`

```
: ∀ {A : Set} {x y : A} (Predicate : A → Set)
substitution → x ∼ y
→ Predicate x
→ Predicate y
Predicate same p = p substitution
```

Any relation which satisfies the above properties of `reflexivity`

, `transitivity`

and `symmetry`

can be considered an equivalence relation and hence can judge a propositional equality.

We now present a more formal machinery for relations. We use universe polymorphism throughout to develop this machinery.

We first re-define propositional equality within the framework of universe polymorphism:

```
infix 4 _≡_
data _≡_ {a} {A : Set a} (x : A) : A → Set a where
instance refl : x ≡ x
```

And an equivalence relation for binary relations:

```
record IsEquivalence {a ℓ} {A : Set a}
(_≈_ : Rel A ℓ) : Set (a ⊔ ℓ) where
field
: Reflexive _≈_
rfl : Symmetric _≈_
sym : Transitive _≈_
trans
: _≡_ ⇒ _≈_
reflexive = rfl reflexive refl
```

We use the new structures to re-define the properties of propositional equality.

```
module ≡-properties {a} {A : Set a} where
: Symmetric {A = A} _≡_
sym-≡ = refl
sym-≡ refl
: Transitive {A = A} _≡_
trans-≡ = p
trans-≡ refl p
: IsEquivalence {A = A} _≡_
isEquivalence = record
isEquivalence { rfl = refl
; sym = sym-≡
; trans = trans-≡
}
: ∀ {a b} {A : Set a} {B : Set b} (f : A → B) {x y : A}
cong-≡ → x ≡ y
→ f x ≡ f y
= refl
cong-≡ f refl
: ∀ {a} {A : Set a}{x y : A} (Predicate : A → Set)
subs-≡ → x ≡ y
→ Predicate x
→ Predicate y
= p subs-≡ Predicate refl p
```

Equality, or specifically, equivalence is at the heart of mathematics. In order to build more complex structures, we introduce a new datatype, which essentially encapsulates any datatype and it’s equivalence operation:

```
record Setoid c ℓ : Set (lsuc (c ⊔ ℓ)) where
infix 4 _≈_
field
: Set c
Data _≈_ : Rel Data ℓ
: IsEquivalence _≈_
isEquivalence
open IsEquivalence isEquivalence public
```