---
layout: default
title : "Setoid.Terms.Properties module (The Agda Universal Algebra Library)"
date : "2021-09-18"
author: "agda-algebras development team"
---
#### Basic properties of terms on setoids
This is the [Setoid.Terms.Properties][] module of the [Agda Universal Algebra Library][].
<!--
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
open import Overture using (π ; π₯ ; Signature)
module Setoid.Terms.Properties {π : Signature π π₯} where
open import Agda.Primitive using () renaming ( Set to Type )
open import Data.Product using ( _,_ )
open import Function.Bundles using () renaming ( Func to _βΆ_ )
open import Function.Base using ( _β_ )
open import Level using ( Level )
open import Relation.Binary using ( Setoid )
open import Relation.Binary.PropositionalEquality using ( setoid; cong; refl)
import Relation.Binary.Reasoning.Setoid as SetoidReasoning
open import Overture using ( projβ ; projβ )
open import Overture.Terms {π = π} using ( Term )
open import Setoid.Algebras {π = π} using ( Algebra ; π[_] ; π»[_] ; _^_ )
open import Setoid.Functions using ( Img_β_ ; eq ; isSurj ; IsSurjective
; isSurjβIsSurjective )
open import Setoid.Homomorphisms {π = π} using ( hom ; compatible-map ; IsHom ; β-hom )
open import Setoid.Terms.Basic {π = π} using ( π» ; _β_ ; β-isRefl )
open Term
open _βΆ_ using ( ) renaming ( to to _β¨$β©_ ; cong to βcong )
private variable
Ξ± Οα΅ Ξ² Οα΅ Ο Ο : Level
X : Type Ο
```
-->
The term algebra `π» X` is *absolutely free* (or *universal*, or *initial*) for
algebras in the signature `π`. That is, for every π-algebra `π¨`, the following hold.
1. Every function from `π` to `π[ π¨ ]` lifts to a homomorphism from `π» X` to `π¨`.
2. The homomorphism that exists by item 1 is unique.
We now prove this in [Agda][], starting with the fact that every map from `X` to
`π[ π¨ ]` lifts to a map from `π[ π» X ]` to `π[ π¨ ]` in a natural way, by induction
on the structure of the given term.
```agda
module _ {π¨ : Algebra Ξ± Ο}(h : X β π[ π¨ ]) where
open Algebra π¨ using ( Interp ) renaming ( Domain to A )
open Setoid A using ( _β_ ; reflexive )
open Algebra (π» X) using () renaming ( Domain to TX )
free-lift : π[ π» X ] β π[ π¨ ]
free-lift (β x) = h x
free-lift (node f t) = (f ^ π¨) (Ξ» i β free-lift (t i))
free-lift-of-surj-isSurj :
isSurj{π¨ = setoid X}{π© = A} h β isSurj{π¨ = TX}{π© = A} free-lift
free-lift-of-surj-isSurj hE {y} = mp p
where
p : Img h β y
p = hE
mp : Img h β y β Img free-lift β y
mp (eq a x) = eq (β a) x
free-lift-func : TX βΆ A
free-lift-func β¨$β© x = free-lift x
free-lift-func .βcong = flcong
where
open _β_
flcong : β {s t} β s β t β free-lift s β free-lift t
flcong (rfl xβ‘y) = reflexive (cong h xβ‘y)
flcong (gnl sβt) = βcong Interp (refl , flcong β sβt)
```
Naturally, at the base step of the induction, when the term has the form `generator`
x, the free lift of `h` agrees with `h`. For the inductive step, when the given term
has the form `node f t`, the free lift is defined as follows: Assuming (the induction
hypothesis) that we know the image of each subterm `t i` under the free lift of `h`,
define the free lift at the full term by applying `f ^ π¨` to the images of the subterms.
The free lift so defined is a homomorphism by construction. Indeed, here is the trivial proof.
```agda
lift-hom : hom (π» X) π¨
lift-hom = free-lift-func , hhom
where
hfunc : TX βΆ A
hfunc = free-lift-func
hcomp : compatible-map (π» X) π¨ free-lift-func
hcomp {f}{a} = βcong Interp (refl , (Ξ» i β (βcong free-lift-func){a i} β-isRefl))
hhom : IsHom (π» X) π¨ hfunc
hhom = record { compatible = Ξ»{f}{a} β hcomp{f}{a} }
```
If we further assume that each of the mappings from `X` to `π[ π¨ ]` is *surjective*,
then the homomorphisms constructed with `free-lift` and `lift-hom` are
*epimorphisms*, as we now prove.
```agda
lift-of-epi-is-epi : isSurj{π¨ = setoid X}{π© = A} h β IsSurjective free-lift-func
lift-of-epi-is-epi hE = isSurjβIsSurjective free-lift-func (free-lift-of-surj-isSurj hE)
```
Finally, we prove that the homomorphism is unique. Recall, when we proved this in the module
[Basic.Terms.Properties][], we needed function extensionality. Here, by using setoid equality,
we can omit the `swelldef` hypothesis we needed previously to prove `free-unique`.
```agda
module _ {π¨ : Algebra Ξ± Ο}{gh hh : hom (π» X) π¨} where
open Algebra π¨ using ( Interp ) renaming ( Domain to A )
open Setoid A using ( _β_ )
open Algebra (π» X) using () renaming ( Domain to TX )
open SetoidReasoning A
open _β_
open IsHom
private
g h : TX βΆ A
g = projβ gh
h = projβ hh
free-unique : (β x β g β¨$β© (β x) β h β¨$β© (β x)) β β (t : Term X) β g β¨$β© t β h β¨$β© t
free-unique p (β x) = p x
free-unique p (node f t) = begin
g β¨$β© (node f t) ββ¨ compatible (projβ gh) β©
(f ^ π¨)(Ξ» i β (g β¨$β© (t i))) ββ¨ βcong Interp (refl , Ξ» i β free-unique p (t i)) β©
(f ^ π¨)(Ξ» i β (h β¨$β© (t i))) βΛβ¨ compatible (projβ hh) β©
h β¨$β© (node f t) β
```
##### Naturality of the free lift
Existence (`lift-hom`) and uniqueness (`free-unique`) together say that `π» X` is a
*free* (initial) object, and freeness always brings a third, slightly less quotable
property: the assignment "generator map β¦ induced homomorphism" is *natural* in the
target algebra. Concretely, lifting `Ξ· : X β π[ π¨ ]` into `π¨` and then applying a
homomorphism `h : π¨ βΆ π©` is the same as lifting the composite map `h β Ξ·` into `π©`
directly:
```text
lift-hom Ξ·
π» X βββββββββββββββββββββ π¨
β² β
β² β h
lift-hom β² β
(h β Ξ·) β β
π©
```
The proof is a one-liner, and *that* is the point: both routes around the triangle
are homomorphisms `π» X βΆ π©` that agree on the generators (both send `β x` to
`h (Ξ· x)`, definitionally), so `free-unique` forces them to agree on every term. No
induction over terms appears here β it is already packaged inside `free-unique`.
This is the way category theory pays rent: theorems about *all* terms become
theorems about *generators only*.
(The same fact in environment form β `h (β¦ t β§ a) β β¦ t β§ (h β a)` β is
`comm-hom-term` in [Setoid.Terms.Operations][], proved there by direct induction;
`free-lift-interp`, also in that module, mediates between the two phrasings. The
companion naturality in the *signature* argument, where the algebra is fixed and the
signature varies along a morphism, is `reduct-interp` in
[Setoid.Varieties.Invariance][].)
```agda
module _ {π¨ : Algebra Ξ± Οα΅}{π© : Algebra Ξ² Οα΅}(h : hom π¨ π©)(Ξ· : X β π[ π¨ ]) where
open Setoid π»[ π© ] using () renaming ( _β_ to _βα΅_ ; refl to reflα΅ )
free-lift-natural : (t : Term X)
β projβ h β¨$β© free-lift{π¨ = π¨} Ξ· t βα΅ free-lift{π¨ = π©} (Ξ» x β projβ h β¨$β© Ξ· x) t
free-lift-natural =
free-unique {π¨ = π©} {gh = β-hom (lift-hom Ξ·) h} {hh = lift-hom (Ξ» x β projβ h β¨$β© Ξ· x)}
(Ξ» _ β reflα΅)
```