---
layout: default
file: "src/Setoid/Algebras/Finite.lagda.md"
title: "Setoid.Algebras.Finite module (The Agda Universal Algebra Library)"
date: "2026-07-12"
author: "the agda-algebras development team"
---
### Finite setoid algebras
This is the [Setoid.Algebras.Finite][] module of the [Agda Universal Algebra Library][].
This module defines the finiteness interface for setoid algebras. The record
`FiniteAlgebra`{.AgdaRecord} `π¨` bundles decidable setoid equality on the carrier of
`π¨` with a finite, surjective enumeration of that carrier. (It says nothing about
the congruences of `π¨`; it is carrier-level data only.)
Two design points deserve comment.
+ **The enumeration is surjective, not bijective**. A map `Fin card β π[ π¨ ]`
hitting every element up to `β` is exactly what downstream constructions need in
order to *search* the carrier and to *count*; e.g., counting the pairs related by a
congruence in [Setoid.Subalgebras.Subdirect.Finite][]. Demanding injectivity as
well would burden every instance with distinctness proofs that no consumer uses,
so `card`{.AgdaField} is an upper bound on, not the exact size of, the carrier.
+ **Decidable `β` is a separate field**. Surjective enumerability of a setoid
carrier does not imply decidability of its equality, so the interface must ask
for both.
The name `FiniteAlgebra`{.AgdaRecord} is deliberately reserved for this bare
interface. Finite-algebra theorems about the *congruence lattice* β for instance
Birkhoff's subdirect representation theorem for finite algebras β need strictly more
than carrier finiteness: a complete, decidable enumeration of the congruences, which
carrier finiteness cannot supply constructively. That stronger, logically
independent interface is `FiniteCongruences`{.AgdaRecord}, defined in
[Setoid.Congruences.Finite][]; the two are consumed together in
[Setoid.Subalgebras.Subdirect.Finite][].
<!--
```agda
{-# OPTIONS --without-K --exact-split --safe #-}
module Setoid.Algebras.Finite where
open import Agda.Primitive using () renaming ( Set to Type )
open import Data.Fin.Base using ( Fin ; zero )
open import Data.Fin.Properties using ( all? ; any? )
open import Data.Nat.Base using ( β )
open import Data.Product using ( _,_ ; β-syntax ; projβ ; projβ )
open import Data.Unit.Base using ( β€ ; tt )
open import Function using ( Func )
open import Level using ( Level ; _β_ ; 0β )
open import Relation.Binary using ( Setoid )
open import Relation.Nullary using ( Dec ; yes )
open import Relation.Nullary.Decidable.Core using ( mapβ² )
open import Overture using ( π ; π₯ ; Signature )
open import Setoid.Algebras.Basic using ( Algebra ; π[_] ; π»[_] )
open Algebra using ( Domain ; Interp )
open Func using ( cong ) renaming ( to to _β¨$β©_ )
open Relation.Binary.IsEquivalence
private variable Ξ± Ο β : Level
```
-->
#### The bare finiteness interface
A **finite algebra** is an algebra together with a decision procedure for its setoid
equality and a surjective enumeration of its carrier by a finite index type.
```agda
module _ {π : Signature π π₯} where
record FiniteAlgebra (π¨ : Algebra {π = π} Ξ± Ο) : Type (Ξ± β Ο) where
open Setoid π»[ π¨ ] using ( _β_ )
field
_β_ : β x y β Dec (x β y)
card : β
enum : Fin card β π[ π¨ ]
enum-sur : β x β β[ i ] enum i β x
open FiniteAlgebra
```
#### Non-vacuity: the one-element algebra is finite
The record is genuine, computational data, so we exhibit an inhabitant. The
one-element algebra `π`{.AgdaFunction} over the ambient signature has carrier `β€`,
trivial setoid equality, and each operation constantly `tt`; its finiteness witness
is immediate.[^1]
```agda
open Setoid
π : Algebra {π = π} 0β 0β
π .Domain .Carrier = β€
π .Domain ._β_ = Ξ» _ _ β β€
π .Domain .isEquivalence = record { refl = tt
; sym = Ξ» _ β tt
; trans = Ξ» _ _ β tt }
π .Interp β¨$β© _ = tt
π .Interp .cong _ = tt
π-FiniteAlgebra : FiniteAlgebra π
π-FiniteAlgebra ._β_ = Ξ» _ _ β yes tt
π-FiniteAlgebra .card = 1
π-FiniteAlgebra .enum = Ξ» _ β tt
π-FiniteAlgebra .enum-sur = Ξ» _ β zero , tt
```
#### Deciding a universal statement by sweeping the enumeration
The enumeration is what turns a universally quantified property of a finite algebra
into a finite computation. Let `P`{.AgdaBound} be a property of triples of elements
that *respects* `β`: whenever `a β a'`, `b β b'`, and `c β c'`, the instance
`P a b c` implies `P a' b' c'`. If `P`{.AgdaBound} holds at every triple of
enumerated elements, then it holds at every triple, since every element is equal
to an enumerated one (`enum-allΒ³`{.AgdaFunction}). Conversely, a property that
holds everywhere holds at the enumerated elements. So when each instance of
`P`{.AgdaBound} is decidable, the universal statement is decided by a sweep of
`cardΒ³` instances (`allΒ³?`{.AgdaFunction}), three nested calls of the standard
library's `all?`{.AgdaFunction} on `Fin card`, whose answer is transported along
the two implications. The binary form `allΒ²?`{.AgdaFunction} decides a
universal statement about pairs, and `exists?`{.AgdaFunction} decides an
existential one, `β a, P a`, by the standard library's `any?`{.AgdaFunction}:
a witness among the enumerated elements is a witness, and any witness `a` has an
enumerated stand-in `enum i β a`. The quadruple forms `enum-allβ΄`{.AgdaFunction} and
`allβ΄?`{.AgdaFunction} are the same with one more quantifier; they decide, for
instance, Whitman's condition on a finite lattice (in the library
ualib/agda-lattices, which builds on this one). Because
surjectivity is all the sweep uses, an enumeration that repeats elements only
repeats work.
```agda
module _ {π : Signature π π₯} {π¨ : Algebra {π = π} Ξ± Ο} (π : FiniteAlgebra π¨) where
open FiniteAlgebra π using ( card ; enum ; enum-sur )
open Setoid π»[ π¨ ] using ( _β_ ) renaming ( sym to βsym )
enum-allΒ² : (P : π[ π¨ ] β π[ π¨ ] β Type β)
β (β {a a' b b'} β a β a' β b β b' β P a b β P a' b')
β (β i j β P (enum i) (enum j)) β β a b β P a b
enum-allΒ² P resp h a b =
resp (projβ (enum-sur a)) (projβ (enum-sur b)) (h (projβ (enum-sur a)) (projβ (enum-sur b)))
allΒ²? : (P : π[ π¨ ] β π[ π¨ ] β Type β)
β (β {a a' b b'} β a β a' β b β b' β P a b β P a' b')
β (β a b β Dec (P a b)) β Dec (β a b β P a b)
allΒ²? P resp P? =
mapβ² (enum-allΒ² P resp) (Ξ» h i j β h (enum i) (enum j)) (all? Ξ» i β all? Ξ» j β P? (enum i) (enum j))
exists? : (P : π[ π¨ ] β Type β) β (β {a a'} β a β a' β P a β P a')
β (β a β Dec (P a)) β Dec (β[ a ] P a)
exists? P resp P? =
mapβ² (Ξ» (i , p) β enum i , p) (Ξ» (a , p) β projβ (enum-sur a) , resp (βsym (projβ (enum-sur a))) p)
(any? Ξ» i β P? (enum i))
enum-allΒ³ : (P : π[ π¨ ] β π[ π¨ ] β π[ π¨ ] β Type β)
β (β {a a' b b' c c'} β a β a' β b β b' β c β c' β P a b c β P a' b' c')
β (β i j k β P (enum i) (enum j) (enum k)) β β a b c β P a b c
enum-allΒ³ P resp h a b c =
resp (projβ (enum-sur a)) (projβ (enum-sur b)) (projβ (enum-sur c))
(h (projβ (enum-sur a)) (projβ (enum-sur b)) (projβ (enum-sur c)))
allΒ³? : (P : π[ π¨ ] β π[ π¨ ] β π[ π¨ ] β Type β)
β (β {a a' b b' c c'} β a β a' β b β b' β c β c' β P a b c β P a' b' c')
β (β a b c β Dec (P a b c)) β Dec (β a b c β P a b c)
allΒ³? P resp P? =
mapβ² (enum-allΒ³ P resp) (Ξ» h i j k β h (enum i) (enum j) (enum k))
(all? Ξ» i β all? Ξ» j β all? Ξ» k β P? (enum i) (enum j) (enum k))
enum-allβ΄ : (P : π[ π¨ ] β π[ π¨ ] β π[ π¨ ] β π[ π¨ ] β Type β)
β (β {a a' b b' c c' d d'} β a β a' β b β b' β c β c' β d β d' β P a b c d β P a' b' c' d')
β (β i j k l β P (enum i) (enum j) (enum k) (enum l)) β β a b c d β P a b c d
enum-allβ΄ P resp h a b c d =
resp (projβ (enum-sur a)) (projβ (enum-sur b)) (projβ (enum-sur c)) (projβ (enum-sur d))
(h (projβ (enum-sur a)) (projβ (enum-sur b)) (projβ (enum-sur c)) (projβ (enum-sur d)))
allβ΄? : (P : π[ π¨ ] β π[ π¨ ] β π[ π¨ ] β π[ π¨ ] β Type β)
β (β {a a' b b' c c' d d'} β a β a' β b β b' β c β c' β d β d' β P a b c d β P a' b' c' d')
β (β a b c d β Dec (P a b c d)) β Dec (β a b c d β P a b c d)
allβ΄? P resp P? =
mapβ² (enum-allβ΄ P resp) (Ξ» h i j k l β h (enum i) (enum j) (enum k) (enum l))
(all? Ξ» i β all? Ξ» j β all? Ξ» k β all? Ξ» l β P? (enum i) (enum j) (enum k) (enum l))
```
--------------------------------------
[^1]: The finiteness witness of the congruence lattice of π is
`π-FiniteCongruences`{.AgdaFunction} in [Setoid.Congruences.Finite][] which,
together with the witness `π-FiniteAlgebra`{.AgdaFunction}, is consumed by
Birkhoff's theorem for finite algebras in [Setoid.Subalgebras.Subdirect.Finite][].