---
layout: default
title : "Legacy.Base.Subalgebras.Subuniverses module (The Agda Universal Algebra Library)"
date : "2021-01-14"
author: "agda-algebras development team"
---
### <a id="subuniverses">Subuniverses</a>
This is the [Legacy.Base.Subalgebras.Subuniverses][] module of the [Agda Universal Algebra Library][].
We start by defining a type that represents the important concept of *subuniverse*. Suppose `๐จ` is an algebra. A subset `B โ โฃ ๐จ โฃ` is said to be *closed under the operations of* `๐จ` if for each `๐ โ โฃ ๐ โฃ` and all tuples `๐ : โฅ ๐ โฅ ๐ โ ๐ต` the element `(๐ ฬ ๐จ) ๐` belongs to `B`. If a subset `B โ ๐ด` is closed under the operations of `๐จ`, then we call B a *subuniverse* of `๐จ`.
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
open import Overture using ( ๐ ; ๐ฅ ; Signature )
module Legacy.Base.Subalgebras.Subuniverses {๐ : Signature ๐ ๐ฅ} where
open import Agda.Primitive using () renaming ( Set to Type )
open import Function using ( _โ_ )
open import Level using ( Level ; _โ_ )
open import Relation.Unary using ( Pred ; _โ_ ; _โ_ ; โ )
open import Axiom.Extensionality.Propositional
using () renaming ( Extensionality to funext )
open import Relation.Binary.PropositionalEquality
using ( module โก-Reasoning ; _โก_ )
open import Overture using ( โฃ_โฃ ; โฅ_โฅ ; _โปยน )
open import Legacy.Base.Relations using ( Im_โ_ )
open import Legacy.Base.Equality using ( swelldef )
open import Legacy.Base.Algebras {๐ = ๐} using ( Algebra ; _ฬ_ ; ov )
open import Legacy.Base.Homomorphisms {๐ = ๐} using ( hom )
open import Legacy.Base.Terms {๐ = ๐} using ( Term ; โ ; node ; _โฆ_โง )
private variable ฮฑ ฮฒ ๐ง : Level
```
We first show how to represent in [Agda][] the collection of subuniverses of an
algebra `๐จ`. Since a subuniverse is viewed as a subset of the domain of `๐จ`,
we define it as a predicate on `โฃ ๐จ โฃ`. Thus, the collection of subuniverses
is a predicate on predicates on `โฃ ๐จ โฃ`.
```agda
Subuniverses : (๐จ : Algebra ฮฑ) โ Pred (Pred โฃ ๐จ โฃ ฮฒ) (๐ โ ๐ฅ โ ฮฑ โ ฮฒ)
Subuniverses ๐จ B = (๐ : โฃ ๐ โฃ)(๐ : โฅ ๐ โฅ ๐ โ โฃ ๐จ โฃ) โ Im ๐ โ B โ (๐ ฬ ๐จ) ๐ โ B
```
#### <a id="subuniverses-as-records">Subuniverses as records</a>
Next we define a type to represent a single subuniverse of an algebra. If `๐จ`
is the algebra in question, then a subuniverse of `๐จ` is a subset of (i.e.,
predicate over) the domain `โฃ ๐จ โฃ` that belongs to `Subuniverses ๐จ`.
```agda
record Subuniverse {๐จ : Algebra ฮฑ} : Type(ov ฮฒ โ ฮฑ) where
constructor mksub
field
sset : Pred โฃ ๐จ โฃ ฮฒ
sSub : sset โ Subuniverses ๐จ
```
#### <a id="subuniverse-generation">Subuniverse Generation</a>
If `๐จ` is an algebra and `X โ โฃ ๐จ โฃ` a subset of the domain of `๐จ`, then the
*subuniverse of* `๐จ` *generated by* `X` is typically denoted by
`Sg`<sup>`๐จ`</sup>`(X)` and defined to be the smallest subuniverse of `๐จ`
containing `X`. Equivalently,
`Sg`<sup>`๐จ`</sup>`(X)` = `โ` { `U` : `U` is a subuniverse of `๐จ` and `B โ U` }.
We define an inductive type, denoted by `Sg`, that represents the subuniverse
generated by a given subset of the domain of a given algebra, as follows.
```agda
data Sg (๐จ : Algebra ฮฑ)(X : Pred โฃ ๐จ โฃ ฮฒ) : Pred โฃ ๐จ โฃ (๐ โ ๐ฅ โ ฮฑ โ ฮฒ)
where
var : โ {v} โ v โ X โ v โ Sg ๐จ X
app : โ f a โ Im a โ Sg ๐จ X โ (f ฬ ๐จ) a โ Sg ๐จ X
```
(The inferred types in the `app` constructor are `f : โฃ ๐ โฃ` and `a : โฅ ๐ โฅ ๐ โ โฃ ๐จ โฃ`.)
Given an arbitrary subset `X` of the domain `โฃ ๐จ โฃ` of an `๐`-algebra `๐จ`, the type
`Sg X` does indeed represent a subuniverse of `๐จ`. Proving this using the inductive
type `Sg` is trivial, as we see here.
```agda
sgIsSub : {๐จ : Algebra ฮฑ}{X : Pred โฃ ๐จ โฃ ฮฒ} โ Sg ๐จ X โ Subuniverses ๐จ
sgIsSub = app
```
Next we prove by structural induction that `Sg X` is the smallest subuniverse
of `๐จ` containing `X`.
```agda
sgIsSmallest : {๐ก : Level}(๐จ : Algebra ฮฑ){X : Pred โฃ ๐จ โฃ ฮฒ}(Y : Pred โฃ ๐จ โฃ ๐ก)
โ Y โ Subuniverses ๐จ โ X โ Y โ Sg ๐จ X โ Y
sgIsSmallest _ _ _ XinY (var Xv) = XinY Xv
sgIsSmallest ๐จ Y YsubA XinY (app f a SgXa) = Yfa
where
IH : Im a โ Y
IH i = sgIsSmallest ๐จ Y YsubA XinY (SgXa i)
Yfa : (f ฬ ๐จ) a โ Y
Yfa = YsubA f a IH
```
When the element of `Sg X` is constructed as `app f a SgXa`, we may assume (the
induction hypothesis) that the arguments in the tuple `a` belong to `Y`. Then
the result of applying `f` to `a` also belongs to `Y` since `Y` is a subuniverse.
#### <a id="subuniverse-lemmas">Subuniverse Lemmas</a>
Here we formalize a few basic properties of subuniverses. First, the intersection
of subuniverses is again a subuniverse.
```agda
โs : {๐ : Level}{๐จ : Algebra ฮฑ}{I : Type ๐}{๐ : I โ Pred โฃ ๐จ โฃ ฮฒ}
โ (โ i โ ๐ i โ Subuniverses ๐จ) โ โ I ๐ โ Subuniverses ๐จ
โs ฯ f a ฮฝ = ฮป i โ ฯ i f a (ฮป x โ ฮฝ x i)
```
In the proof above, we assume the following typing judgments:
ฯ : โ i โ ๐ i โ Subuniverses ๐จ
f : โฃ ๐ โฃ
a : โฅ ๐ โฅ ๐ โ โฃ ๐จ โฃ
ฮฝ : Im ๐ โ โ I ๐
and we must prove `(f ฬ ๐จ) a โ โ I ๐`. In this case, Agda will fill in the proof
term `ฮป i โ ฯ i f a (ฮป x โ ฮฝ x i)` automatically with the command `C-c C-a`.
Next, subuniverses are closed under the action of term operations.
```agda
sub-term-closed : {๐ง : Level}{X : Type ๐ง}(๐จ : Algebra ฮฑ){B : Pred โฃ ๐จ โฃ ฮฒ}
โ (B โ Subuniverses ๐จ) โ (t : Term X)(b : X โ โฃ ๐จ โฃ)
โ ((x : X) โ (b x โ B)) โ (๐จ โฆ t โง)b โ B
sub-term-closed ๐จ AB (โ x) b Bb = Bb x
sub-term-closed ๐จ{B} ฯ (node f t)b ฮฝ =
ฯ f (ฮป z โ (๐จ โฆ t z โง) b) ฮป x โ sub-term-closed ๐จ{B} ฯ (t x) b ฮฝ
```
In the induction step of the foregoing proof, the typing judgments of the premise
are the following:
๐จ : Algebra ฮฑ
B : Pred โฃ ๐จ โฃ ฮฒ
ฯ : B โ Subuniverses ๐จ
f : โฃ ๐ โฃ
t : โฅ ๐ โฅ ๐ โ Term X
b : X โ โฃ ๐จ โฃ
ฮฝ : โ x โ b x โ B
and the given proof term establishes the goal `๐จ โฆ node f t โง b โ B`.
Alternatively, we could express the preceeding fact using an inductive type
representing images of terms.
```agda
data TermImage (๐จ : Algebra ฮฑ)(Y : Pred โฃ ๐จ โฃ ฮฒ) : Pred โฃ ๐จ โฃ (๐ โ ๐ฅ โ ฮฑ โ ฮฒ)
where
var : โ {y : โฃ ๐จ โฃ} โ y โ Y โ y โ TermImage ๐จ Y
app : โ ๐ ๐ก โ ((x : โฅ ๐ โฅ ๐) โ ๐ก x โ TermImage ๐จ Y) โ (๐ ฬ ๐จ) ๐ก โ TermImage ๐จ Y
```
By what we proved above, it should come as no surprise that `TermImage ๐จ Y` is a
subuniverse of `๐จ` that contains `Y`.
```agda
TermImageIsSub : {๐จ : Algebra ฮฑ}{Y : Pred โฃ ๐จ โฃ ฮฒ} โ TermImage ๐จ Y โ Subuniverses ๐จ
TermImageIsSub = app
Y-onlyif-TermImageY : {๐จ : Algebra ฮฑ}{Y : Pred โฃ ๐จ โฃ ฮฒ} โ Y โ TermImage ๐จ Y
Y-onlyif-TermImageY {a} Ya = var Ya
```
Since `Sg ๐จ Y` is the smallest subuniverse containing Y, we obtain the following inclusion.
```agda
SgY-onlyif-TermImageY : (๐จ : Algebra ฮฑ)(Y : Pred โฃ ๐จ โฃ ฮฒ) โ Sg ๐จ Y โ TermImage ๐จ Y
SgY-onlyif-TermImageY ๐จ Y = sgIsSmallest ๐จ (TermImage ๐จ Y) TermImageIsSub Y-onlyif-TermImageY
```
Next we prove the important fact that homomorphisms are uniquely determined by their
values on a generating set.
```agda
open โก-Reasoning
hom-unique : swelldef ๐ฅ ฮฒ โ {๐จ : Algebra ฮฑ}{๐ฉ : Algebra ฮฒ}
(X : Pred โฃ ๐จ โฃ ฮฑ) (g h : hom ๐จ ๐ฉ)
โ ((x : โฃ ๐จ โฃ) โ (x โ X โ โฃ g โฃ x โก โฃ h โฃ x))
โ (a : โฃ ๐จ โฃ) โ (a โ Sg ๐จ X โ โฃ g โฃ a โก โฃ h โฃ a)
hom-unique _ _ _ _ ฯ a (var x) = ฯ a x
hom-unique wd {๐จ}{๐ฉ} X g h ฯ fa (app ๐ a ฮฝ) = Goal
where
IH : โ x โ โฃ g โฃ (a x) โก โฃ h โฃ (a x)
IH x = hom-unique wd{๐จ}{๐ฉ} X g h ฯ (a x) (ฮฝ x)
Goal : โฃ g โฃ ((๐ ฬ ๐จ) a) โก โฃ h โฃ ((๐ ฬ ๐จ) a)
Goal = โฃ g โฃ ((๐ ฬ ๐จ) a) โกโจ โฅ g โฅ ๐ a โฉ
(๐ ฬ ๐ฉ)(โฃ g โฃ โ a ) โกโจ wd (๐ ฬ ๐ฉ) (โฃ g โฃ โ a) (โฃ h โฃ โ a) IH โฉ
(๐ ฬ ๐ฉ)(โฃ h โฃ โ a) โกโจ ( โฅ h โฅ ๐ a )โปยน โฉ
โฃ h โฃ ((๐ ฬ ๐จ) a ) โ
```
In the induction step, the following typing judgments are assumed:
wd : swelldef ๐ฅ ฮฒ
๐จ : Algebra ฮฑ
๐ฉ : Algebra ฮฒ
X : Pred โฃ ๐จ โฃ ฮฑ
g h : hom ๐จ ๐ฉ
ฯ : ฮ x ๊ โฃ ๐จ โฃ , (x โ X โ โฃ g โฃ x โก โฃ h โฃ x)
fa : โฃ ๐จ โฃ
fa = (๐ ฬ ๐จ) a
๐ : โฃ ๐ โฃ
a : โฅ ๐ โฅ ๐ โ โฃ ๐จ โฃ
ฮฝ : Im a โ Sg ๐จ X
and, under these assumptions, we proved `โฃ g โฃ ((๐ ฬ ๐จ) a) โก โฃ h โฃ ((๐ ฬ ๐จ) a)`.