---
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 )

-- Imports from the Agda Standard Library -----------------------------------
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β€² )

-- Imports from the Agda Universal Algebra Library ----------------------------
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)      -- decidable setoid equality carrier of 𝑨
      card      : β„•
      enum      : Fin card β†’ π•Œ[ 𝑨 ]       -- finite enumeration of the carrier
      enum-sur  : βˆ€ x β†’ βˆƒ[ i ] enum i β‰ˆ x  -- that hits every element, up to β‰ˆ
  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
-- The one-element algebra over the signature 𝑆.
  open Setoid

  𝟏 : Algebra {𝑆 = 𝑆} 0β„“ 0β„“
  𝟏 .Domain .Carrier        = ⊀
  𝟏 .Domain ._β‰ˆ_            = Ξ» _ _ β†’ ⊀
  𝟏 .Domain .isEquivalence  = record  { refl   = tt
                                      ; sym    = Ξ» _ β†’ tt
                                      ; trans  = Ξ» _ _ β†’ tt }
  𝟏 .Interp ⟨$⟩ _           = tt
  𝟏 .Interp .cong _         = tt

  -- The one-element algebra is a finite algebra.
  𝟏-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][].