---
layout: default
title : "Setoid.Varieties.Preservation (The Agda Universal Algebra Library)"
date : "2021-09-24"
author: "agda-algebras development team"
---
### Equation preservation for setoid algebras
This is the [Setoid.Varieties.Preservation][] module of the [Agda Universal Algebra Library][].
This module proves that the classes `H`{.AgdaFunction}` π¦`{.AgdaBound},
`S`{.AgdaFunction}` π¦`{.AgdaBound}, `P`{.AgdaFunction}` π¦`{.AgdaBound}, and
`V`{.AgdaFunction}` π¦`{.AgdaBound} all satisfy the same identities.
<!--
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
module Setoid.Varieties.Preservation where
open import Agda.Primitive using () renaming ( Set to Type )
open import Data.Product using ( _,_ ; projβ ; projβ )
open import Data.Unit.Polymorphic using ( β€ )
open import Function using () renaming ( Func to _βΆ_ )
open import Level using ( Level ; _β_ )
open import Relation.Binary using ( Setoid )
open import Relation.Unary using ( Pred ; _β_ ; _β_ )
open import Overture using ( π )
open import Overture.Terms using ( Term )
open import Setoid.Algebras using ( Algebra ; ov ; β¨
)
open import Setoid.Homomorphisms using ( β
β¨
βΊ-refl ; β
-refl
; IdHomImage ; β
-sym )
open import Setoid.Subalgebras using ( _β€_ ; β¨
-β€ ; β
-trans-β€
; β€-reflexive )
open import Setoid.Terms using ( module Environment)
open import Setoid.Varieties.Closure using ( H ; S ; P ; V ; S-expa ; H-expa
; P-expa ; V-expa ; Level-closure )
open import Setoid.Varieties.Properties using ( β§-H-invar ; β§-S-invar
; β§-P-invar ; β§-I-invar )
open import Setoid.Varieties.SoundAndComplete using ( _β§_ ; _β«_ ; β«-proof
; _βΜ_ ; _β’_βΉ_β_ ; Th)
open _βΆ_ using () renaming ( to to _β¨$β©_ )
open Algebra using ( Domain )
```
-->
#### Closure properties
The types defined above represent operators with useful closure properties. We now
prove a handful of such properties that we need later.
```agda
module _ {Ξ± Οα΅ β : Level}{π¦ : Pred(Algebra {π = π} Ξ± Οα΅) (Ξ± β Οα΅ β ov {π = π} β)} where
private
a = Ξ± β Οα΅
oaβ = ov {π = π} (a β β)
SβSP : β{ΞΉ} β S β π¦ β S {Ξ² = Ξ±}{Οα΅} (a β β β ΞΉ) (P {Ξ² = Ξ±}{Οα΅} β ΞΉ π¦)
SβSP {ΞΉ} (π¨ , (kA , Bβ€A )) = π¨ , (pA , Bβ€A)
where
pA : π¨ β P β ΞΉ π¦
pA = β€ , (Ξ» _ β π¨) , (Ξ» _ β kA) , β
β¨
βΊ-refl
PβSP : β{ΞΉ} β P β ΞΉ π¦ β S (a β β β ΞΉ) (P {Ξ² = Ξ±}{Οα΅}β ΞΉ π¦)
PβSP {ΞΉ} x = S-expa{β = a β β β ΞΉ} x
PβHSP : β{ΞΉ} β P {Ξ² = Ξ±}{Οα΅} β ΞΉ π¦ β H (a β β β ΞΉ) (S (a β β β ΞΉ) (P β ΞΉ π¦))
PβHSP {ΞΉ} x = H-expa{β = a β β β ΞΉ} (S-expa{β = a β β β ΞΉ} x)
PβV : β{ΞΉ} β P β ΞΉ π¦ β V β ΞΉ π¦
PβV = PβHSP
SPβV : β{ΞΉ} β S{Ξ² = Ξ±}{Οα΅ = Οα΅} (a β β β ΞΉ) (P {Ξ² = Ξ±}{Οα΅} β ΞΉ π¦) β V β ΞΉ π¦
SPβV {ΞΉ} x = H-expa{β = a β β β ΞΉ} x
```
Finally, we are in a position to prove that a product of subalgebras of algebras
in a class π¦ is a subalgebra of a product of algebras in π¦.
```agda
PSβSP : P (a β β) oaβ (S{Ξ² = Ξ±}{Οα΅} β π¦) β S oaβ (P β oaβ π¦)
PSβSP {π©} (I , ( π , sA , Bβ
β¨
A )) = Goal
where
β¬ : I β Algebra {π = π} Ξ± Οα΅
β¬ i = sA i .projβ
kB : (i : I) β β¬ i β π¦
kB i = sA i .projβ .projβ
β¨
Aβ€β¨
B : β¨
π β€ β¨
β¬
β¨
Aβ€β¨
B = β¨
-β€ Ξ» i β projβ (projβ (sA i))
Goal : π© β S{Ξ² = oaβ}{oaβ}oaβ (P {Ξ² = oaβ}{oaβ} β oaβ π¦)
Goal = β¨
β¬ , (I , (β¬ , (kB , β
-refl))) , (β
-trans-β€ Bβ
β¨
A β¨
Aβ€β¨
B)
```
#### H preserves identities
First we prove that the closure operator H is compatible with identities that hold in the given class.
```agda
module _ {Ξ± Οα΅ β Ο : Level}
{π¦ : Pred(Algebra {π = π} Ξ± Οα΅) (Ξ± β Οα΅ β ov {π = π} β)}
{X : Type Ο}
{p q : Term {π = π} X}
where
H-id1 : π¦ β« (p βΜ q) β H {Ξ² = Ξ±}{Οα΅}β π¦ β« (p βΜ q)
H-id1 Ο .β«-proof π© (π¨ , kA , BimgA) = β§-H-invar{p = p}{q} (Ο .β«-proof π¨ kA) BimgA
```
The converse of the foregoing result is almost too obvious to bother with. Nonetheless, we formalize it for completeness.
```agda
H-id2 : H β π¦ β« (p βΜ q) β π¦ β« (p βΜ q)
H-id2 Hpq .β«-proof π¨ kA = Hpq .β«-proof π¨ (π¨ , (kA , IdHomImage))
```
#### S preserves identities
Identities are inherited by subalgebras, so a class and its subalgebra closure
satisfy the same equations. `S-id1`{.AgdaFunction} is the substantive direction
and is immediate from `β§-S-invar`{.AgdaFunction} of
[Setoid.Varieties.Properties][]: an identity true in `π¨` is true in anything
embedded in `π¨`. `S-id2`{.AgdaFunction} is the converse, and holds for the same
reason `H-id2`{.AgdaFunction} above does, namely that `S`{.AgdaFunction} is
expansive: `π¦ β S β π¦` by `β€-reflexive`{.AgdaFunction}, so an identity holding
throughout the closure holds in particular on `π¦`.
```agda
S-id1 : π¦ β« (p βΜ q) β (S {Ξ² = Ξ±}{Οα΅} β π¦) β« (p βΜ q)
S-id1 Ο .β«-proof π© (π¨ , kA , Bβ€A) = β§-S-invar{p = p}{q} (Ο .β«-proof π¨ kA) Bβ€A
S-id2 : S β π¦ β« (p βΜ q) β π¦ β« (p βΜ q)
S-id2 Spq .β«-proof π¨ kA = Spq .β«-proof π¨ (π¨ , (kA , β€-reflexive))
```
#### P preserves identities
The same for products, with one extra step. `P-id1`{.AgdaFunction} combines
`β§-P-invar`{.AgdaFunction}, that a product of models is a model, with
`β§-I-invar`{.AgdaFunction}, that satisfaction transports across an isomorphism.
The second is needed because membership in `P β ΞΉ π¦` holds only *up to*
isomorphism: an inhabitant supplies a family `π` together with a proof
`π¨ β
β¨
π`, not an equality. `P-id2`{.AgdaFunction} is once more the
expansiveness converse, this time by way of `P-expa`{.AgdaFunction}.
```agda
P-id1 : β{ΞΉ} β π¦ β« (p βΜ q) β P {Ξ² = Ξ±}{Οα΅}β ΞΉ π¦ β« (p βΜ q)
P-id1 Ο .β«-proof π¨ (I , π , kA , Aβ
β¨
A) = β§-I-invar π¨ p q IH (β
-sym Aβ
β¨
A)
where
ih : β i β π i β§ (p βΜ q)
ih i = Ο .β«-proof (π i) (kA i)
IH : β¨
π β§ (p βΜ q)
IH = β§-P-invar {p = p}{q} π ih
P-id2 : β{ΞΉ} β P β ΞΉ π¦ β« (p βΜ q) β π¦ β« (p βΜ q)
P-id2{ΞΉ} PKpq .β«-proof π¨ kA = PKpq .β«-proof π¨ (P-expa {β = β}{ΞΉ} kA)
```
#### V preserves identities
Finally, we prove the analogous preservation lemmas for the closure operator `V`.
```agda
module _
{Ξ± Οα΅ β ΞΉ Ο : Level}
{π¦ : Pred(Algebra {π = π} Ξ± Οα΅) (Ξ± β Οα΅ β ov {π = π} β)}
{X : Type Ο}
{p q : Term {π = π} X}
where
private aβΞΉ = Ξ± β Οα΅ β β β ΞΉ
V-id1 : π¦ β« (p βΜ q) β V β ΞΉ π¦ β« (p βΜ q)
V-id1 Ο .β«-proof π© (π¨ , (β¨
A , pβ¨
A , Aβ€β¨
A) , BimgA) =
H-id1{β = aβΞΉ}{π¦ = S aβΞΉ (P {Ξ² = Ξ±}{Οα΅}β ΞΉ π¦)} spKβ§pq .β«-proof π© (π¨ , (spA , BimgA))
where
spA : π¨ β S aβΞΉ (P {Ξ² = Ξ±}{Οα΅}β ΞΉ π¦)
spA = β¨
A , (pβ¨
A , Aβ€β¨
A)
spKβ§pq : S aβΞΉ (P β ΞΉ π¦) β« (p βΜ q)
spKβ§pq = S-id1{β = aβΞΉ} (P-id1{β = β} {π¦ = π¦} Ο)
V-id2 : V β ΞΉ π¦ β« (p βΜ q) β π¦ β« (p βΜ q)
V-id2 Vpq .β«-proof π¨ kA = Vpq .β«-proof π¨ (V-expa β ΞΉ kA)
Lift-id1 : β{Ξ² Οα΅} β π¦ β« (p βΜ q) β Level-closure{Ξ±}{Οα΅}{Ξ²}{Οα΅} β π¦ β« (p βΜ q)
Lift-id1 pKq .β«-proof π¨ (π© , kB , Bβ
A) Ο = Goal
where
open Environment π¨
open Setoid (Domain π¨) using (_β_)
Goal : β¦ p β§ β¨$β© Ο β β¦ q β§ β¨$β© Ο
Goal = β§-I-invar π¨ p q (pKq .β«-proof π© kB) Bβ
A Ο
```
#### Class identities
From `V-id1` it follows that if π¦ is a class of structures, then the set of identities
modeled by all structures in `π¦` is equivalent to the set of identities modeled by all
structures in `V π¦`. In other terms, `Th (V π¦)` is precisely the set of identities
modeled by `π¦`. We formalize this observation as follows.
```agda
classIds-β-VIds : π¦ β« (p βΜ q) β (p , q) β Th (V β ΞΉ π¦)
classIds-β-VIds pKq = V-id1 pKq
VIds-β-classIds : (p , q) β Th (V β ΞΉ π¦) β π¦ β« (p βΜ q)
VIds-β-classIds Thpq = V-id2 Thpq
```