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.
{-# OPTIONS --without-K --exact-split --safe #-} open import Overture using (π ; π₯ ; Signature) module Setoid.Varieties.SoundAndComplete {π : Signature π π₯} where -- imports from Agda and the Agda Standard Library ------------------------------- open import Agda.Primitive using () renaming ( Set to Type ) open import Data.Product using ( _,_ ; Ξ£-syntax ; _Γ_ ) renaming ( projβ to fst ; projβ to snd ) open import Function using ( _β_ ; flip ; id ) 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 -- Imports from the Agda Universal Algebra Library ------------------------------- open import Overture using ( β£_β£ ) open import Base.Terms {π = π} using ( Term ) open import Setoid.Algebras {π = π} using ( Algebra ; ov ; β¨_β© ) 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 : β£ π β£ I : Type ΞΉ -- Equations -- An equation is a pair (s , t) of terms in the same context. record Eq : Type (ov Ο) where constructor _βΜ_ field {cxt} : Type Ο lhs : Term cxt rhs : Term cxt open Eq public -- Equation p βΜ q holding in algebra M. (type \~~\^. to get βΜ; type \models to get β§) _β§_ : (π¨ : Algebra Ξ± Οα΅)(term-identity : Eq{Ο}) β Type _ π¨ β§ (p βΜ q) = Equal p q where open Environment π¨ _β«_ : Pred (Algebra Ξ± Οα΅) β β Eq{Ο} β Type (β β Ο β ov(Ξ± β Οα΅)) π¦ β« eq = β π¨ β π¦ π¨ β π¨ β§ eq -- (type \||= to get β«) -- An I-indexed set of equations inhabits the type I β Eq. -- For such `β° : I β Eq`... -- ...`π¨ β¨ β°` is the assertion that the algebra π¨ models every equation in β°. _β¨_ : (π¨ : Algebra Ξ± Οα΅) β (I β Eq{Ο}) β Type _ π¨ β¨ β° = β i β Equal (lhs (β° i))(rhs (β° i)) where open Environment π¨ -- (type \|= to get β¨) -- ...`π¦ β₯β β°` is the assertion that every algebra in π¦ models every equation in β°. _β₯β_ : Pred (Algebra Ξ± Οα΅) β β (I β Eq{Ο}) β Type _ π¦ β₯β β° = β i β π¦ β« β° i -- ...`Mod β°` is the class of algebras that model every equation in β°. ModTuple : (I β Eq{Ο}) β Pred(Algebra Ξ± Οα΅) _ ModTuple β° = _β¨ β° module _ {Ξ± Οα΅ β : Level} where Mod : Pred(Term X Γ Term X) β β Pred (Algebra Ξ± Οα΅) _ -- (π β π₯ β lsuc Ο β β β Ξ± β Οα΅) Mod β° π¨ = β {p q} β (p , q) β β° β Equal p q where open Environment π¨ Th : Pred (Algebra Ξ± Οα΅) β β Pred(Term X Γ Term X) _ -- (β β Ο β ov (Ξ± β Οα΅)) Th π¦ = Ξ» (p , q) β π¦ β« (p βΜ q) βTh : Pred(Term X Γ Term X) (β β Ο β ov (Ξ± β Οα΅)) β Type _ -- (β β ov (Ξ± β Οα΅ β Ο)) βTh P = Ξ£[ p β (Term _ Γ Term _) ] p β P module _ {Ο : Level}{X : Type Ο} where ThTuple : (π¦ : Pred (Algebra Ξ± Οα΅) β) β βTh{Ο = Ο} (Th{X = X} π¦) β Eq{Ο} ThTuple π¦ = Ξ» i β fst β£ i β£ βΜ snd β£ i β£ module _ {Ξ±}{Οα΅}{ΞΉ}{I : Type ΞΉ} where -- An entailment E β eq holds iff it holds in all models of E. _β_ : (E : I β Eq{Ο}) (eq : Eq{Ο}) β Type _ E β eq = (M : Algebra Ξ± Οα΅) β M β¨ E β M β§ eq
(Based on Andreas Abelβs Agda formalization of Birkhoffβs completeness theorem.)
module _ {Ο ΞΉ : Level} where data _β’_βΉ_β_ {I : Type ΞΉ}(E : I β Eq) : (X : Type Ο)(p q : Term X) β Type (ΞΉ β ov Ο) where hyp : β 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} β E β’ Ξ βΉ p β q β β (Ο : Sub Ξ Ξ) β E β’ Ξ βΉ (p [ Ο ]) β (q [ Ο ]) refl : β {p} β 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 β’βΉβIsEquiv : {I : Type ΞΉ}{E : I β Eq} β IsEquivalence (E β’ Ξ βΉ_β_) β’βΉβIsEquiv = record { refl = refl ; sym = sym ; trans = trans }
(Based on Andreas Abelβs Agda formalization of Birkhoffβs completeness theorem.)
module Soundness {Ο Ξ± ΞΉ : Level}{I : Type ΞΉ} (E : I β Eq{Ο}) (π¨ : Algebra Ξ± Οα΅) -- We assume an algebra M (V : π¨ β¨ E) -- that models all equations in E. where open Algebra π¨ using ( Interp ) renaming (Domain to A) -- In any model M that satisfies the equations E, derived equality is actual equality. open SetoidReasoning A 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.
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$.
(Based on Andreas Abelβs Agda formalization of Birkhoffβs completeness theorem.)
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 π¨
.
module FreeAlgebra {Ο : Level}{ΞΉ : Level}{I : Type ΞΉ}(E : I β Eq) where open Algebra -- Domain of the relatively free algebra. FreeDomain : Type Ο β Setoid _ _ FreeDomain X = record { Carrier = Term X ; _β_ = E β’ X βΉ_β_ ; isEquivalence = β’βΉβIsEquiv } -- The interpretation of an operation is simply the operation itself. -- This works since E β’ X βΉ_β_ is a congruence. FreeInterp : β {X} β (β¨ π β© (FreeDomain X)) βΆ (FreeDomain X) FreeInterp β¨$β© (f , ts) = node f ts cong FreeInterp (refl , h) = app h -- The relatively free algebra π½[_] : Type Ο β Algebra (ov Ο) (ΞΉ β ov Ο) Domain π½[ X ] = FreeDomain X Interp π½[ X ] = FreeInterp -- The identity substitution Οβ maps variables to themselves. Οβ : {X : Type Ο} β Sub X X Οβ x = β x -- Οβ acts indeed as identity. 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 Ο
.)
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 -- The term model satisfies all the equations it started out with. 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.
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 π½[ Ξ ] satisfies Οβ β© β¦ q β§ β¨$β© Οβ ββ¨ evaluation q Οβ β© q [ Οβ ] ββ¨ identity q β© q β where open Environment π½[ Ξ ] open SetoidReasoning (Domain π½[ Ξ ])