Term Operations

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.

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.

```_ā¦_ā§ : (šØ : 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)ā»Ā¹
```

Interpretation of terms in product algebras

```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
```

Compatibility of terms

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 `|:`.

Substitution

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 [ Ļ ] ā§) Ī·  ā
```