---
layout: default
title : "Setoid.Terms.Operations module (The Agda Universal Algebra Library)"
date : "2021-09-25"
author: "agda-algebras development team"
---
#### Term Operations for Setoid Algebras
This section presents the [Setoid.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 --without-K --exact-split --safe #-}
module Setoid.Terms.Operations where
open import Agda.Primitive using () renaming ( Set to Type )
open import Data.Product using ( _,_ )
open import Function.Base using ( _β_ )
open import Function.Bundles using () renaming ( Func to _βΆ_ )
open import Level using ( Level )
open import Relation.Binary using ( Setoid )
open import Relation.Binary.PropositionalEquality as β‘ using ( _β‘_ )
import Relation.Binary.Reasoning.Setoid as SetoidReasoning
open import Overture using ( OperationSymbolsOf ; ArityOf
; π ; π₯ ; Signature ; π )
open import Overture.Terms using ( Term )
open import Setoid.Algebras using ( Algebra ; _^_ ; ov ; β¨
; π»[_] ; π[_] )
open import Setoid.Homomorphisms using ( hom ; IsHom )
open import Setoid.Terms.Properties using ( free-lift )
open import Setoid.Terms.Basic using ( module Environment ; π» ; _β_ ; β-isRefl )
open Term
open _βΆ_ using ( cong ) renaming ( to to _β¨$β©_ )
private variable
Ξ± Οα΅ Ξ² Οα΅ Ο Ο ΞΉ : Level
X : Type Ο
```
-->
It turns out that the intepretation of a term is the same as the `free-lift`
(modulo argument order and assuming function extensionality).
```agda
module _ {π¨ : Algebra {π = π} Ξ± Οα΅} where
open Algebra π¨ using ( Interp )
open Setoid π»[ π¨ ] using ( _β_ ; refl )
open Environment π¨ using ( β¦_β§ )
free-lift-interp : (Ξ· : X β π[ π¨ ])(p : Term {π = π} X)
β β¦ p β§ β¨$β© Ξ· β (free-lift{π¨ = π¨} Ξ·) p
free-lift-interp Ξ· (β x) = refl
free-lift-interp Ξ· (node f t) = cong Interp (β‘.refl , (free-lift-interp Ξ·) β t)
module _ {π : Signature π π₯}{X : Type Ο} where
open Algebra (π» {π = π} X) using ( Interp ) renaming (Domain to TX )
open Setoid TX using ( _β_ ; refl )
open Environment (π» {π = π} X) using ( β¦_β§ ; ββEqual )
open SetoidReasoning TX
term-interp : (f : OperationSymbolsOf π){s t : ArityOf π f β Term {π = π} X} β (β i β s i β t i)
β β Ξ· β β¦ node f s β§ β¨$β© Ξ· β β¦ node f t β§ β¨$β© Ξ·
term-interp f {s}{t} st Ξ· = cong Interp (β‘.refl , Ξ» i β ββEqual (s i) (t i) (st i) Ξ· )
term-agreement : (p : Term {π = π} X) β p β β¦ p β§ β¨$β© β
term-agreement (β x) = refl
term-agreement (node f t) = cong Interp (β‘.refl , (Ξ» i β term-agreement (t i)))
```
#### Interpretation of terms in product algebras
`interp-prod`{.AgdaFunction} says that interpreting a term in a product is the
same as interpreting it in each factor and collecting the results: for every term
`p` and every environment `Ο`, the value of `p` in `β¨
π` under `Ο` agrees, at each
coordinate `i`, with the value of `p` in `π i` under the `i`-th component of `Ο`.
This is the term-level counterpart of the way `β¨
`{.AgdaFunction} of
[Setoid.Algebras.Products][] interprets operation *symbols* in the first place,
and the proof is the induction that says so: the variable case is reflexivity, and
the operation case pushes the induction hypothesis through the product's
`Interp`{.AgdaField}.
```agda
module _ {X : Type Ο }{I : Type ΞΉ}(π : I β Algebra {π = π} Ξ± Οα΅) where
open Algebra (β¨
π) using (Interp) renaming ( Domain to β¨
A )
open Setoid β¨
A using ( _β_ ; refl )
open Environment (β¨
π) using () renaming ( β¦_β§ to β¦_β§β )
open Environment using ( β¦_β§ ; ββEqual )
interp-prod : (p : Term {π = π} X)
β β Ο β β¦ p β§β β¨$β© Ο β Ξ» i β (β¦ π i β§ p) β¨$β© Ξ» x β (Ο x) i
interp-prod (β x) = Ξ» Ο i β ββEqual (π i) (β x) (β x) β-isRefl Ξ» x' β (Ο x) i
interp-prod (node f t) = Ξ» Ο i β cong Interp (β‘.refl , (Ξ» j k β interp-prod (t j) Ο k)) i
```
#### Compatibility of terms
We now prove the most important fact about term operations
(`comm-hom-term`{.AgdaFunction}): every term commutes with every homomorphism.
```agda
module _
{π : Signature π π₯}
{π¨ : Algebra {π = π} Ξ± Οα΅}
{π© : Algebra {π = π} Ξ² Οα΅}
((h , hhom) : hom π¨ π©)
where
open Algebra π© using (Interp)
open Setoid π»[ π© ] using ( _β_ ; refl )
open Environment π¨ using () renaming ( β¦_β§ to β¦_β§β )
open Environment π© using () renaming ( β¦_β§ to β¦_β§β )
open SetoidReasoning π»[ π© ]
open IsHom
comm-hom-term : (t : Term {π = π} X) (a : X β π[ π¨ ])
β h β¨$β© (β¦ t β§β β¨$β© a) β β¦ t β§β β¨$β© Ξ» i β h β¨$β© a i
comm-hom-term (β x) a = refl
comm-hom-term (node f t) a = goal
where
goal : h β¨$β© (β¦ node f t β§β β¨$β© a) β β¦ node f t β§β β¨$β© Ξ» i β h β¨$β© a i
goal = begin
h β¨$β© (β¦ node f t β§β β¨$β© a) ββ¨ compatible (hhom) β©
(f ^ π©)(Ξ» i β h β¨$β© (β¦ t i β§β β¨$β© a)) ββ¨ cong Interp (β‘.refl , Ξ» i β comm-hom-term (t i) a) β©
β¦ node f t β§β β¨$β© (Ξ» j β h β¨$β© a j) β
```
#### 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.
```agda
_[_]s : {Ο : Level}{X Y : Type Ο} β Term {π = π} Y β (Y β X) β Term {π = π} X
(β y) [ Ο ]s = β (Ο y)
(node f t) [ Ο ]s = node f Ξ» i β t i [ Ο ]s
```
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 : {π : Signature π π₯}(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
```