---
layout: default
file: "src/Examples/Setoid/HSPCommutativeMonoid.lagda.md"
title: "Examples.Setoid.HSPCommutativeMonoid module"
date: "2026-05-31"
author: "the agda-algebras development team"
---
### Worked example: Birkhoff's HSP theorem specialized to `(β, +, 0)` {#examples-setoid-hsp-commutativemonoid}
This is the [Examples.Setoid.HSPCommutativeMonoid][] module of the [Agda Universal Algebra Library][].
[Setoid.Varieties.HSP][] proves Birkhoff's variety theorem for an *arbitrary* class
`π¦`{.AgdaBound} of algebras over an arbitrary signature. This module instantiates
that theorem at the singleton class `π¦β = { (β, +, 0) }` over the
monoid signature `Sig-Monoid`{.AgdaFunction}, reusing the commutative monoid
`β-commutativeMonoid` of [Examples.Classical.CommutativeMonoid][] as
the generating algebra.
We record four facts about the variety `V π¦β` generated by
`(β, +, 0)`:
+ the generating algebra belongs to its own variety (`ββV`, from expansiveness of `V`);
+ every identity true in `(β, +, 0)` β in particular commutativity β is an identity
of the whole variety (`V-commutative`{.AgdaFunction}, from identity preservation);
+ Birkhoff's theorem, specialized: every model of the equational theory of
`V π¦β` lies in `V π¦β` (`Birkhoff-β`{.AgdaFunction});
+ and its converse (`Birkhoff-converse-β`{.AgdaFunction}).
<!--
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
module Examples.Setoid.HSPCommutativeMonoid where
open import Data.Fin.Base using ( Fin )
open import Data.Fin.Patterns using ( 0F ; 1F )
open import Data.Nat.Properties using ( +-comm )
open import Data.Product using ( projβ )
open import Level using ( 0β ; Level ) renaming (suc to lsuc)
open import Relation.Unary using ( Pred ; _β_ )
open import Examples.Classical.CommutativeMonoid using ( β-commutativeMonoid )
open import Classical.Signatures.Monoid using ( Sig-Monoid ; β-Op )
open import Overture.Terms {π = Sig-Monoid} using ( Term ; β ; node )
open import Setoid.Algebras {π = Sig-Monoid} using ( Algebra )
open import Setoid.Varieties.Closure {π = Sig-Monoid} using ( V ; Vβ² ; V-expaβ² )
open import Setoid.Varieties.Preservation {π = Sig-Monoid} using ( V-id1 )
open import Setoid.Varieties.SoundAndComplete {π = Sig-Monoid} using ( _β«_ ; β«-proof
; _βΜ_ ; Mod ; Th )
open import Setoid.Varieties.HSP {π = Sig-Monoid} using ( Birkhoff
; Birkhoff-converse )
```
-->
```agda
1β 2β : Level
1β = lsuc 0β
2β = lsuc 1β
```
#### The generating algebra and the singleton class {#generating-algebra}
`β-commutativeMonoid`{.AgdaFunction} is a Ξ£-pair of a `Sig-Monoid`{.AgdaFunction}
algebra and a proof that it satisfies the commutative-monoid equations; its first
projection is the underlying `(β, +, 0)` setoid algebra.
```agda
π¨β : Algebra 0β 0β
π¨β = projβ β-commutativeMonoid
data π¦β : Pred (Algebra 0β 0β) 1β where
inβ : π¦β π¨β
π¨ββπ¦β : π¨β β π¦β
π¨ββπ¦β = inβ
```
The variety `Vβ² π¦β` generated by `π¨β`. Because every
universe in play here is `0β`, we use `Vβ²`{.AgdaFunction} β the common-case
specialization of `V`{.AgdaFunction} that collapses its eight level parameters to
those of the generating class β giving a single concrete predicate
`π`{.AgdaFunction} that the corollaries below can refer to without pinning any
level by hand or leaving unsolved level metavariables.
```agda
π : Pred (Algebra 0β 0β) 2β
π = Vβ² 0β 1β π¦β
```
#### Corollary 1: the generator lies in its own variety {#in-its-variety}
`V`{.AgdaFunction} is expansive, so `(β, +, 0)` belongs to the variety
it generates. We obtain the membership from `V-expaβ²`{.AgdaFunction}, the
explicit-class form of expansiveness: the class `π¦β`{.AgdaFunction} is passed
positionally, so nothing has to be inferred and the intermediate levels are pinned
by unification with the `Vβ²`{.AgdaFunction} goal `π`{.AgdaFunction}.
```agda
ββV : π¨β β π
ββV = V-expaβ² 0β 1β π¦β π¨ββπ¦β
```
#### Corollary 2: the variety is commutative {#variety-commutative}
Commutativity of `(β, +, 0)` is exactly `+-comm`: the
term identity `x Β· y β y Β· x` holds under every environment. Since
`π¦β` is the singleton `{ π¨β }`, the class models this
identity; identity preservation (`V-id1`) then lifts it to the whole
variety.
```agda
xΒ·y yΒ·x : Term (Fin 2)
xΒ·y = node β-Op Ξ» { 0F β β 0F ; 1F β β 1F }
yΒ·x = node β-Op Ξ» { 0F β β 1F ; 1F β β 0F }
π¦ββ«comm : π¦β β« (xΒ·y βΜ yΒ·x)
π¦ββ«comm .β«-proof π© inβ Ο = +-comm (Ο 0F) (Ο 1F)
V-commutative : π β« (xΒ·y βΜ yΒ·x)
V-commutative = V-id1 π¦ββ«comm
```
#### Birkhoff's theorem, specialized {#birkhoff-specialized}
Finally, the theorem itself and its converse, instantiated at `π¦β`.
```agda
Birkhoff-β : {π¨ : Algebra 0β 0β}
β π¨ β Mod (Th (V 0β 1β π¦β)) β π¨ β V 0β 1β π¦β
Birkhoff-β = Birkhoff {π¦ = π¦β}
Birkhoff-converse-β : {π¨ : Algebra 0β 0β}
β π¨ β (V 0β 1β π¦β) β π¨ β Mod (Th (V 0β 1β π¦β))
Birkhoff-converse-β = Birkhoff-converse {π¦ = π¦β}
Birkhoff-β' : {π¨ : Algebra 0β 0β}
β π¨ β Mod (Th π) β π¨ β V 0β 1β π¦β
Birkhoff-β' = Birkhoff {π¦ = π¦β}
Birkhoff-converse-β' : {π¨ : Algebra 0β 0β}
β π¨ β π β π¨ β Mod (Th π)
Birkhoff-converse-β' = Birkhoff-converse {π¦ = π¦β}
```