---
layout: default
title : "Setoid.Varieties.SoundAndComplete module (The Agda Universal Algebra Library)"
date : "2021-01-14"
author: "agda-algebras development team"
---
#### Entailment, derivation rules, soundness and completeness
This is the [Setoid.Varieties.SoundAndComplete][] module of the [Agda Universal Algebra Library][].
This module is based on [Andreas Abel's Agda formalization of Birkhoff's completeness theorem](http://www.cse.chalmers.se/~abela/agda/MultiSortedAlgebra.pdf).
<!--
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
open import Overture using (π ; π₯ ; Signature)
module Setoid.Varieties.SoundAndComplete {π : Signature π π₯} where
open import Agda.Primitive using () renaming ( Set to Type )
open import Data.Product using ( _,_ ; Ξ£-syntax ; _Γ_ )
open import Function using ( _β_ ; flip ) renaming ( Func to _βΆ_ )
open import Level using ( Level ; _β_ )
open import Relation.Binary using ( Setoid ; IsEquivalence )
open import Relation.Unary using ( Pred ; _β_ )
open import Relation.Binary.PropositionalEquality using ( refl )
import Relation.Binary.Reasoning.Setoid as SetoidReasoning
open import Overture using ( projβ ; projβ ; OperationSymbolsOf )
open import Overture.Terms {π = π} using ( Term )
open import Setoid.Algebras {π = π} using ( Algebra ; ov ; π»[_] )
open import Setoid.Signatures using ( β¨_β© )
open import Setoid.Terms {π = π} using ( module Environment ; Sub ; _[_] )
open Setoid using ( Carrier ; _β_ ; isEquivalence )
open _βΆ_ renaming ( to to _β¨$β©_ )
open Term
private variable
Ο Ξ± Οα΅ ΞΉ β : Level
X Ξ Ξ : Type Ο
f : OperationSymbolsOf π
I : Type ΞΉ
```
-->
```agda
record Eq : Type (ov Ο) where
constructor _βΜ_
field
{cxt} : Type Ο
lhs : Term cxt
rhs : Term cxt
infix 6 _βΜ_
open Eq public
toEq : {Ο ΞΉ : Level}{Idx : Type ΞΉ}{X : Type Ο}
β (Idx β Term X Γ Term X) β (Idx β Eq {Ο = Ο})
toEq β° i = projβ (β° i) βΜ projβ (β° i)
_β§_ : (π¨ : Algebra Ξ± Οα΅)(term-identity : Eq{Ο}) β Type _
π¨ β§ (p βΜ q) = Equal p q where open Environment π¨
record _β«_ (π¦ : Pred (Algebra Ξ± Οα΅) β)(eq : Eq{Ο}) : Type (β β Ο β ov(Ξ± β Οα΅)) where
constructor β«-intro
field β«-proof : β (π¨ : Algebra Ξ± Οα΅) β π¦ π¨ β π¨ β§ eq
open _β«_ public
infix 5 _β«_
_β¨_ : (π¨ : Algebra Ξ± Οα΅) β (I β Eq{Ο}) β Type _
π¨ β¨ β° = β i β Equal (lhs (β° i))(rhs (β° i)) where open Environment π¨
_β₯β_ : Pred (Algebra Ξ± Οα΅) β β (I β Eq{Ο}) β Type _
π¦ β₯β β° = β i β π¦ β« β° i
ModTuple : (I β Eq{Ο}) β Pred(Algebra Ξ± Οα΅) _
ModTuple β° = _β¨ β°
module _ {Ξ± Οα΅ β Ο : Level} {X : Type Ο} where
Mod : Pred(Term X Γ Term X) β β Pred (Algebra Ξ± Οα΅) _
Mod β° π¨ = β {p q} β (p , q) β β° β Equal p q
where open Environment π¨
Th : Pred (Algebra Ξ± Οα΅) β β Pred(Term X Γ Term X) _
Th π¦ = Ξ» (p , q) β π¦ β« (p βΜ q)
βTh : Pred(Term X Γ Term X) (β β Ο β ov (Ξ± β Οα΅)) β Type _
βTh P = Ξ£[ p β (Term _ Γ Term _) ] p β P
ThTuple : (π¦ : Pred (Algebra Ξ± Οα΅) β) β βTh (Th π¦) β Eq{Ο}
ThTuple π¦ = Ξ» i β projβ (projβ i) βΜ projβ (projβ i)
module _ {Ξ±}{Οα΅}{ΞΉ}{I : Type ΞΉ} where
_β_ : (E : I β Eq{Ο}) (eq : Eq{Ο}) β Type _
E β eq = (M : Algebra Ξ± Οα΅) β M β¨ E β M β§ eq
```
##### Derivations in a context
```agda
module _ {Ο ΞΉ : Level} where
data _β’_βΉ_β_ {I : Type ΞΉ}(E : I β Eq) : (X : Type Ο)(p q : Term X) β Type (ΞΉ β ov Ο) where
hyp : β (i : I) β let p βΜ q = E i in E β’ _ βΉ p β q
app : β {ps qs} β (β i β E β’ Ξ βΉ ps i β qs i) β E β’ Ξ βΉ node f ps β node f qs
sub : β {p q : Term Ξ} β E β’ Ξ βΉ p β q β β (Ο : Sub Ξ Ξ) β E β’ Ξ βΉ p [ Ο ] β q [ Ο ]
refl : β {p : Term Ξ} β E β’ Ξ βΉ p β p
sym : β {p q : Term Ξ} β E β’ Ξ βΉ p β q β E β’ Ξ βΉ q β p
trans : β {p q r : Term Ξ} β E β’ Ξ βΉ p β q β E β’ Ξ βΉ q β r β E β’ Ξ βΉ p β r
infix 4 _β’_βΉ_β_
β’βΉβIsEquiv : {I : Type ΞΉ}{E : I β Eq} β IsEquivalence (E β’ Ξ βΉ_β_)
β’βΉβIsEquiv = record { refl = refl ; sym = sym ; trans = trans }
```
##### Soundness of the inference rules
```agda
module Soundness
{Ο Ξ± ΞΉ : Level}{I : Type ΞΉ} (E : I β Eq{Ο})
(π¨ : Algebra Ξ± Οα΅)
(V : π¨ β¨ E)
where
open Algebra π¨ using ( Interp )
open SetoidReasoning π»[ π¨ ]
open Environment π¨ renaming ( β¦_β§s to βͺ_β« )
open IsEquivalence renaming ( refl to reflβ ; sym to symβ ; trans to transβ )
sound : β {p q} β E β’ X βΉ p β q β π¨ β§ (p βΜ q)
sound (hyp i) = V i
sound (app {f = f} es) Ο = Interp .cong (refl , Ξ» i β sound (es i) Ο)
sound (sub {p = p} {q} Epq Ο) Ο = begin
β¦ p [ Ο ] β§ β¨$β© Ο ββ¨ substitution p Ο Ο β©
β¦ p β§ β¨$β© βͺ Ο β« Ο ββ¨ sound Epq (βͺ Ο β« Ο) β©
β¦ q β§ β¨$β© βͺ Ο β« Ο βΛβ¨ substitution q Ο Ο β©
β¦ q [ Ο ] β§ β¨$β© Ο β
sound (refl {p = p}) = reflβ isEquiv {x = p}
sound (sym {p = p} {q} Epq) = symβ isEquiv {x = p}{q} (sound Epq)
sound (trans{p = p}{q}{r} Epq Eqr) = transβ isEquiv {i = p}{q}{r} (sound Epq)(sound Eqr)
```
The deductive closure of a set E is the set of equations modeled by all models of E;
that is, `Th Mod E`.
The soundness proof above shows
β X β E β’ X βΉ p β q β (p , q) β Th Mod β°.
That is, `β X β E β’ X βΉ p β q β Mod E β« p β q`.
The converse is Birkhoff's completeness theorem:
if `Mod E β« p β q`, then `E β’ X βΉ p β q`.
We will prove that result next.
##### Birkhoff's completeness theorem
The proof proceeds by constructing a relatively free algebra consisting of term
quotiented by derivable equality `E β’ X βΉ _β_`. It then suffices to prove that this
model satisfies all equations in `E`.
We denote by `π½[ X ]` the *relatively free algebra* over `X` (relative to `E`), which
is defined as `Term X` modulo `E β’ X βΉ _β_`. This algebra `π½[ X ]` is "free" or
"initial" in the variety of algebras satisfying the identities in `E` in the sense
that it satisfies the following universal property: for each algebra `π¨`, if `π¨ β§ E`,
then there is a unique homomorphism from `π½[ X ]` to `π¨`.
```agda
module FreeAlgebra {Ο : Level}{ΞΉ : Level}{I : Type ΞΉ}(E : I β Eq) where
open Algebra
FreeDomain : Type Ο β Setoid _ _
FreeDomain X = record { Carrier = Term X
; _β_ = E β’ X βΉ_β_
; isEquivalence = β’βΉβIsEquiv
}
FreeInterp : β {X} β (β¨ π β© (FreeDomain X)) βΆ (FreeDomain X)
FreeInterp β¨$β© (f , ts) = node f ts
FreeInterp .cong (refl , h) = app h
π½[_] : Type Ο β Algebra (ov Ο) (ΞΉ β ov Ο)
Domain π½[ X ] = FreeDomain X
Interp π½[ X ] = FreeInterp
Οβ : {X : Type Ο} β Sub X X
Οβ x = β x
identity : (t : Term X) β E β’ X βΉ t [ Οβ ] β t
identity (β x) = refl
identity (node f ts) = app (identity β ts)
```
Evaluation in the term model is substitution `E β’ X βΉ β¦tβ§Ο β t[Ο]`. (This would
hold "on the nose" if we had function extensionality.)
(We put this and the next two lemmas into their own submodules to emphasize
the fact that these results are independent of the chosen variable symbol
type `X` (or `Ξ`, or `Ξ`), which is an arbitrary inhabitant of `Type Ο`.)
```agda
module _ {X : Type Ο} where
open Environment π½[ X ]
evaluation : (t : Term Ξ) (Ο : Sub X Ξ) β E β’ X βΉ (β¦ t β§ β¨$β© Ο) β (t [ Ο ])
evaluation (β x) Ο = refl
evaluation (node f ts) Ο = app (flip (evaluation β ts) Ο)
module _ {Ξ : Type Ο} where
satisfies : π½[ Ξ ] β¨ E
satisfies i Ο = begin
β¦ p β§ β¨$β© Ο ββ¨ evaluation p Ο β©
p [ Ο ] ββ¨ sub (hyp i) Ο β©
q [ Ο ] βΛβ¨ evaluation q Ο β©
β¦ q β§ β¨$β© Ο β
where
open Environment π½[ Ξ ]
open SetoidReasoning (Domain π½[ Ξ ]) ; p = lhs (E i) ; q = rhs (E i)
```
We are finally ready to formally state and prove Birkhoff's Completeness Theorem,
which asserts that every valid consequence is derivable.
```agda
module _ {Ξ : Type Ο} where
completeness : β p q β ModTuple E β« (p βΜ q) β E β’ Ξ βΉ p β q
completeness p q V = begin
p βΛβ¨ identity p β©
p [ Οβ ] βΛβ¨ evaluation p Οβ β©
β¦ p β§ β¨$β© Οβ ββ¨ V .β«-proof π½[ Ξ ] satisfies Οβ β©
β¦ q β§ β¨$β© Οβ ββ¨ evaluation q Οβ β©
q [ Οβ ] ββ¨ identity q β©
q β
where
open Environment π½[ Ξ ]
open SetoidReasoning (Domain π½[ Ξ ])
```