This section presents the Base.Terms.Operations module of the Agda Universal Algebra Library.
Here we define term operations which are simply terms interpreted in a particular algebra, and we prove some compatibility properties of term operations.
{-# OPTIONS --without-K --exact-split --safe #-} open import Overture using ( š ; š„ ; Signature ) module Base.Terms.Operations {š : 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 ; Ī£ ) open import Function using ( _ā_ ) open import Level using ( Level ; _ā_ ) open import Relation.Binary.PropositionalEquality as ā” using ( _ā”_ ; module ā”-Reasoning ) open import Axiom.Extensionality.Propositional using () renaming (Extensionality to funext) -- Imports from Agda Universal Algebra Library ---------------------------------------------- open import Overture using ( _ā_ ; _ā»Ā¹ ; ā£_ā£ ; ā„_ā„ ; Ī ; Ī -syntax ; _ā_ ) open import Base.Relations using ( _|:_ ) open import Base.Equality using ( swelldef ) open import Base.Algebras {š = š} using ( Algebra ; _Ģ_ ; ov ; āØ ) using ( IsCongruence ; Con ) open import Base.Homomorphisms {š = š} using ( hom ) open import Base.Terms.Basic {š = š} using ( Term ; š» ) open import Base.Terms.Properties {š = š} using ( free-lift ) open Term private variable Ī± Ī² Ī³ Ļ Ļ : Level
When we interpret a term in an algebra we call the resulting function a
term operation. Given a term p
and an algebra šØ
, we denote by šØ ā¦ p ā§
the interpretation of p
in šØ
. This is defined inductively as follows.
If p
is a variable symbol x : X
and if a : X ā ā£ šØ ā£
is a tuple of
elements of ā£ šØ ā£
, then šØ ā¦ p ā§ a := a x
.
If p = f t
, where f : ā£ š ā£
is an operation symbol, if t : ā„ š ā„ f ā š» X
is a tuple of terms, and if a : X ā ā£ šØ ā£
is a tuple from šØ
, then we
define šØ ā¦ p ā§ a = šØ ā¦ f t ā§ a := (f Ģ šØ) (Ī» i ā šØ ā¦ t i ā§ a)
.
Thus the interpretation of a term is defined by induction on the structure of the term, and the definition is formally implemented in the agda-algebras library as follows.
_ā¦_ā§ : (šØ : Algebra Ī±){X : Type Ļ } ā Term X ā (X ā ā£ šØ ā£) ā ā£ šØ ā£ šØ ā¦ ā x ā§ = Ī» Ī· ā Ī· x šØ ā¦ node f t ā§ = Ī» Ī· ā (f Ģ šØ) (Ī» i ā (šØ ā¦ t i ā§) Ī·)
It turns out that the intepretation of a term is the same as the free-lift
(modulo argument order and assuming function extensionality).
free-lift-interp : swelldef š„ Ī± ā (šØ : Algebra Ī±){X : Type Ļ } (Ī· : X ā ā£ šØ ā£)(p : Term X) ā (šØ ā¦ p ā§) Ī· ā” (free-lift šØ Ī·) p free-lift-interp _ šØ Ī· (ā x) = ā”.refl free-lift-interp wd šØ Ī· (node f t) = wd (f Ģ šØ) (Ī» z ā (šØ ā¦ t z ā§) Ī·) ((free-lift šØ Ī·) ā t)((free-lift-interp wd šØ Ī·) ā t)
If the algebra in question happens to be š» X
, then we expect that ā s
we have (š» X)ā¦ p ā§ s ā” p s
. But what is (š» X)ā¦ p ā§ s
exactly? By
definition, it depends on the form of p
as follows:
if p = ā x
, then (š» X)ā¦ p ā§ s := (š» X)ā¦ ā x ā§ s ā” s x
if p = node f t
, then
(š» X)ā¦ p ā§ s := (š» X)ā¦ node f t ā§ s = (f Ģ š» X) Ī» i ā (š» X)ā¦ t i ā§ s
Now, assume Ļ : hom š» šØ
. Then by comm-hom-term
, we have
ā£ Ļ ā£ (š» X)ā¦ p ā§ s = šØ ā¦ p ā§ ā£ Ļ ā£ ā s
.
if p = ā x
(and t : X ā ā£ š» X ā£
), then
ā£ Ļ ā£ p ā” ā£ Ļ ā£ (ā x) ā” ā£ Ļ ā£ (Ī» t ā h t) ā” Ī» t ā (ā£ Ļ ā£ ā t) x
if p = node f t
, then
ā£ Ļ ā£ p ā” ā£ Ļ ā£ (š» X)ā¦ p ā§ s = (š» X)ā¦ node f t ā§ s = (f Ģ š» X) Ī» i ā (š» X)ā¦ t i ā§ s
We claim that for all p : Term X
there exists q : Term X
and t : X ā ā£ š» X ā£
such that p ā” (š» X)ā¦ q ā§ t
. We prove this fact as follows.
term-interp : {X : Type Ļ} (f : ā£ š ā£){s t : ā„ š ā„ f ā Term X} ā s ā” t ā node f s ā” (f Ģ š» X) t term-interp f {s}{t} st = ā”.cong (node f) st term-interp' : swelldef š„ (ov Ļ) ā {X : Type Ļ} (f : ā£ š ā£){s t : ā„ š ā„ f ā Term X} ā (ā i ā s i ā” t i) ā node f s ā” (f Ģ š» X) t term-interp' wd f {s}{t} st = wd (node f) s t st term-gen : swelldef š„ (ov Ļ) ā {X : Type Ļ}(p : ā£ š» X ā£) ā Ī£[ q ā ā£ š» X ā£ ] p ā” (š» X ā¦ q ā§) ā term-gen _ (ā x) = (ā x) , ā”.refl term-gen wd (node f t) = (node f (Ī» i ā ā£ term-gen wd (t i) ā£)) , term-interp' wd f Ī» i ā ā„ term-gen wd (t i) ā„ term-gen-agreement : (wd : swelldef š„ (ov Ļ)){X : Type Ļ}(p : ā£ š» X ā£) ā (š» X ā¦ p ā§) ā ā” (š» X ā¦ ā£ term-gen wd p ā£ ā§) ā term-gen-agreement _ (ā x) = ā”.refl term-gen-agreement wd {X} (node f t) = wd ( f Ģ š» X) (Ī» x ā (š» X ā¦ t x ā§) ā) (Ī» x ā (š» X ā¦ ā£ term-gen wd (t x) ā£ ā§) ā) Ī» i ā term-gen-agreement wd (t i) term-agreement : swelldef š„ (ov Ļ) ā {X : Type Ļ}(p : ā£ š» X ā£) ā p ā” (š» X ā¦ p ā§) ā term-agreement wd {X} p = ā„ term-gen wd p ā„ ā (term-gen-agreement wd p)ā»Ā¹
module _ (wd : swelldef š„ (Ī² ā Ī±)){X : Type Ļ }{I : Type Ī²} where interp-prod : (p : Term X)(š : I ā Algebra Ī±)(a : X ā Ī [ i ā I ] ā£ š i ā£) ā (āØ š ā¦ p ā§) a ā” Ī» i ā (š i ā¦ p ā§)(Ī» x ā (a x) i) interp-prod (ā _) š a = ā”.refl interp-prod (node f t) š a = wd ((f Ģ āØ š)) u v IH where u : ā x ā ā£ āØ š ā£ u = Ī» x ā (āØ š ā¦ t x ā§) a v : ā x i ā ā£ š i ā£ v = Ī» x i ā (š i ā¦ t x ā§)(Ī» j ā a j i) IH : ā i ā u i ā” v i IH = Ī» x ā interp-prod (t x) š a interp-prod2 : funext (Ī± ā Ī² ā Ļ) (Ī± ā Ī²) ā (p : Term X)(š : I ā Algebra Ī±) ā āØ š ā¦ p ā§ ā” (Ī» a i ā (š i ā¦ p ā§) Ī» x ā a x i) interp-prod2 _ (ā xā) š = ā”.refl interp-prod2 fe (node f t) š = fe Ī» a ā wd (f Ģ āØ š)(u a) (v a) (IH a) where u : ā a x ā ā£ āØ š ā£ u a = Ī» x ā (āØ š ā¦ t x ā§) a v : ā (a : X ā ā£ āØ š ā£) ā ā x i ā ā£ š i ā£ v a = Ī» x i ā (š i ā¦ t x ā§)(Ī» z ā (a z) i) IH : ā a x ā (āØ š ā¦ t x ā§) a ā” Ī» i ā (š i ā¦ t x ā§)(Ī» z ā (a z) i) IH a = Ī» x ā interp-prod (t x) š a
We now prove two important facts about term operations. The first of these, which is used very often in the sequel, asserts that every term commutes with every homomorphism.
open ā”-Reasoning comm-hom-term : swelldef š„ Ī² ā {šØ : Algebra Ī±} (š© : Algebra Ī²) (h : hom šØ š©){X : Type Ļ}(t : Term X)(a : X ā ā£ šØ ā£) ------------------------------------------------------ ā ā£ h ā£ ((šØ ā¦ t ā§) a) ā” (š© ā¦ t ā§) (ā£ h ā£ ā a) comm-hom-term _ š© h (ā x) a = ā”.refl comm-hom-term wd {šØ} š© h (node f t) a = ā£ h ā£((f Ģ šØ) Ī» i ā (šØ ā¦ t i ā§) a) ā”āØ i ā© (f Ģ š©)(Ī» i ā ā£ h ā£ ((šØ ā¦ t i ā§) a)) ā”āØ ii ā© (f Ģ š©)(Ī» r ā (š© ā¦ t r ā§) (ā£ h ā£ ā a)) ā where i = ā„ h ā„ f Ī» r ā (šØ ā¦ t r ā§) a ii = wd (f Ģ š©) ( Ī» iā ā ā£ h ā£ ((šØ ā¦ t iā ā§) a) ) ( Ī» r ā (š© ā¦ t r ā§) (Ī» x ā ā£ h ā£ (a x)) ) Ī» j ā comm-hom-term wd š© h (t j) a
To conclude this module, we prove that every term is compatible with every
congruence relation. That is, if t : Term X
and Īø : Con šØ
, then
a Īø b ā t(a) Īø t(b)
. (Recall, the compatibility relation |:
was defined in
[Relations.Discrete][].)
module _ {Ī± Ī² : Level}{X : Type Ī±} where open IsCongruence _ā£:_ : {šØ : Algebra Ī±}(t : Term X)(Īø : Con{Ī±}{Ī²} šØ) ā (šØ ā¦ t ā§) |: ā£ Īø ā£ ((ā x) ā£: Īø) p = p x ((node f t) ā£: Īø) p = (is-compatible ā„ Īø ā„) f Ī» x ā ((t x) ā£: Īø) p
WARNING! The compatibility relation for terms ā£:
is typed as |:, whereas
the compatibility type for functions |:
(defined in the
Base.Relations.Discrete module) is typed as |:
.
A substitution from Y
to X
is simply a function from Y
to X
, and the
application of a substitution is represented as follows.
_[_] : {Ļ : Level}{X Y : Type Ļ} ā Term Y ā (Y ā X) ā Term X (ā y) [ Ļ ] = ā (Ļ y) (node f t) [ Ļ ] = node f Ī» i ā t i [ Ļ ]
Alternatively, we may want a substitution that replaces each variable symbol in
Y
, not with an element of X
, but with a term from Term X
.
-- Substerm X Y, an inhabitant of which replaces each variable symbol in Y -- with a term from Term X. Substerm : (X Y : Type Ļ) ā Type (ov Ļ) Substerm X Y = (y : Y) ā Term X -- Application of a Substerm. _[_]t : {X Y : Type Ļ } ā Term Y ā Substerm X Y ā Term X (ā y) [ Ļ ]t = Ļ y (node f t) [ Ļ ]t = node f (Ī» z ā (t z) [ Ļ ]t )
Next we prove the important Substitution Theorem which asserts that an identity p
ā q
holds in an algebra šØ
iff it holds in šØ
after applying any substitution.
subst-lemma : swelldef š„ Ī± ā {X Y : Type Ļ }(p : Term Y)(Ļ : Y ā X) (šØ : Algebra Ī±)(Ī· : X ā ā£ šØ ā£) ā (šØ ā¦ p [ Ļ ] ā§) Ī· ā” (šØ ā¦ p ā§) (Ī· ā Ļ) subst-lemma _ (ā x) Ļ šØ Ī· = ā”.refl subst-lemma wd (node f t) Ļ šØ Ī· = wd (f Ģ šØ) ( Ī» i ā (šØ ā¦ (t i) [ Ļ ] ā§) Ī· ) ( Ī» i ā (šØ ā¦ t i ā§) (Ī· ā Ļ) ) Ī» i ā subst-lemma wd (t i) Ļ šØ Ī· subst-theorem : swelldef š„ Ī± ā {X Y : Type Ļ } (p q : Term Y)(Ļ : Y ā X)(šØ : Algebra Ī±) ā šØ ā¦ p ā§ ā šØ ā¦ q ā§ ā šØ ā¦ p [ Ļ ] ā§ ā šØ ā¦ q [ Ļ ] ā§ subst-theorem wd p q Ļ šØ Apq Ī· = (šØ ā¦ p [ Ļ ] ā§) Ī· ā”āØ subst-lemma wd p Ļ šØ Ī· ā© (šØ ā¦ p ā§) (Ī· ā Ļ) ā”āØ Apq (Ī· ā Ļ) ā© (šØ ā¦ q ā§) (Ī· ā Ļ) ā”āØ ā”.sym (subst-lemma wd q Ļ šØ Ī·) ā© (šØ ā¦ q [ Ļ ] ā§) Ī· ā