↑ Top


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.


{-# 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 ( f 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
Derivations in a context

(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 }
Soundness of the inference rules

(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.

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$.

(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 𝔽[ Ξ“ ])

↑ Setoid.Varieties Setoid.Varieties.Closure β†’