---
layout: default
title : "Legacy.Base.Terms.Operations module (The Agda Universal Algebra Library)"
date : "2021-01-14"
author: "agda-algebras development team"
---
### <a id="term-operations">Term Operations</a>
This section presents the [Legacy.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.
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
open import Overture using ( š ; š„ ; Signature )
module Legacy.Base.Terms.Operations {š : Signature š š„} where
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)
open import Overture using ( _ā_ ; _ā»Ā¹ ; ā£_⣠; ā„_ā„ ; Ī ; Ī -syntax ; _ā_ )
open import Legacy.Base.Relations using ( _|:_ )
open import Legacy.Base.Equality using ( swelldef )
open import Legacy.Base.Algebras {š = š} using ( Algebra ; _Ģ_ ; ov ; āØ
)
using ( IsCongruence ; Con )
open import Legacy.Base.Homomorphisms {š = š} using ( hom )
open import Legacy.Base.Terms.Basic {š = š} using ( Term ; š» )
open import Legacy.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.
1. If `p` is a variable symbol `x : X` and if `a : X ā ⣠šØ ā£` is a tuple of
elements of `⣠šØ ā£`, then `šØ ⦠p ā§ a := a x`.
2. 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.
```agda
_ā¦_ā§ : (šØ : 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).
```agda
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.
```agda
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)ā»Ā¹
```
#### <a id="interpretation-of-terms-in-product-algebras">Interpretation of terms in product algebras</a>
```agda
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
```
#### <a id="compatibility-of-terms">Compatibility of terms</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.
```agda
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][].)
```agda
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
[Legacy.Base.Relations.Discrete][] module) is typed as `|:`.
#### <a id="substitution">Substitution</a>
A substitution from `Y` to `X` is simply a function from `Y` to `X`, and the
application of a substitution is represented as follows.
```agda
_[_] : {Ļ : 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`.
```agda
Substerm : (X Y : Type Ļ) ā Type (ov Ļ)
Substerm X Y = (y : Y) ā Term X
_[_]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.
```agda
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 [ Ļ ] ā§) Ī· ā
```