---
layout: default
title : "Setoid.Terms.Basic module (The Agda Universal Algebra Library)"
date : "2021-09-18"
author: "agda-algebras development team"
---
### Basic definitions
This is the [Setoid.Terms.Basic][] module of the [Agda Universal Algebra Library][].
Terms themselves are inherited from [Overture.Terms][]; what this module adds is
everything needed to treat them *as a setoid algebra*.
`_β_`{.AgdaDatatype} is the inductive equality of terms,
`TermSetoid`{.AgdaFunction} bundles it, and `π»`{.AgdaFunction} is the resulting
**term algebra**, whose operations are the term constructors themselves and so are
trivially compatible with `_β_`{.AgdaDatatype}.
The rest is the machinery of interpretation.
+ `Sub`{.AgdaFunction} and `_[_]`{.AgdaFunction} are substitutions and their
action on terms;
+ `Env`{.AgdaFunction} is the setoid of environments, that is, assignments of
elements of an algebra to variables;
+ `β¦_β§`{.AgdaFunction} is the value of a term in an environment, as a setoid
function so that it respects equality of environments;
+ `Equal`{.AgdaFunction} with `isEquiv`{.AgdaFunction} is the resulting notion of
two terms having the same value in a given algebra under every environment.
The `substitution`{.AgdaFunction} lemma, that interpreting a substituted term is
interpreting the original under a reinterpreted environment, is the workhorse of
the soundness proof in [Setoid.Varieties.SoundAndComplete][].
<!--
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
module Setoid.Terms.Basic where
open import Agda.Primitive using () renaming ( Set to Type )
open import Data.Product using ( _,_ )
open import Function using ( Func )
open import Level using ( Level ; _β_ )
open import Relation.Binary using ( Setoid ; IsEquivalence )
using ( Reflexive ; Symmetric ; Transitive )
open import Relation.Binary.PropositionalEquality using ( _β‘_ ; refl ; sym ; trans )
open import Overture using ( π ; π₯ ; Signature ; π
; ArityOf ; OperationSymbolsOf )
open import Setoid.Algebras using ( Algebra ; ov ; _^_ ; π»[_] ; π[_] )
open import Overture.Terms using ( Term )
open Func renaming ( to to _β¨$β©_ )
open Term
private variable
Ο Ξ± β : Level
X Y : Type Ο
```
-->
#### Equality of terms
The approach we take to equality of terms uses setoids instead of quotient types.
That is, we will define the collection of terms in a signature as a setoid with a
particular equality-of-terms relation, which we must define. Ultimately we will
use this to define the (absolutely free) term algebra as a `Algebra`{.AgdaRecord}
whose carrier is the setoid of terms.
```agda
module _ {π : Signature π π₯}{X : Type Ο } where
data _β_ : Term {π = π} X β Term {π = π} X β Type (ov {π = π} Ο) where
rfl : {x y : X} β x β‘ y β β x β β y
gnl : {f : OperationSymbolsOf π}{s t : ArityOf π f β Term {π = π} X}
β (β i β s i β t i) β node f s β node f t
infix 4 _β_
β-isRefl : Reflexive _β_
β-isRefl {β _} = rfl refl
β-isRefl {node _ _} = gnl Ξ» _ β β-isRefl
β-isSym : Symmetric _β_
β-isSym (rfl x) = rfl (sym x)
β-isSym (gnl x) = gnl Ξ» i β β-isSym (x i)
β-isTrans : Transitive _β_
β-isTrans (rfl x) (rfl y) = rfl (trans x y)
β-isTrans (gnl x) (gnl y) = gnl Ξ» i β β-isTrans (x i) (y i)
β-isEquiv : IsEquivalence _β_
β-isEquiv = record { refl = β-isRefl ; sym = β-isSym ; trans = β-isTrans }
TermSetoid : {π : Signature π π₯}(X : Type Ο) β Setoid (ov {π = π} Ο) (ov {π = π} Ο)
TermSetoid {π = π} X = record { Carrier = Term {π = π} X ; _β_ = _β_ ; isEquivalence = β-isEquiv }
open Algebra
π» : {π : Signature π π₯}(X : Type Ο) β Algebra {π = π} (ov {π = π} Ο) (ov {π = π} Ο)
Domain (π» {π = π} X) = TermSetoid {π = π} X
Interp (π» X) β¨$β© (f , ts) = node f ts
cong (Interp (π» X)) (refl , ssβts) = gnl ssβts
```
#### Interpretation of terms in setoid algebras
The approach to terms and their interpretation in this module was inspired by
[Andreas Abel's formal proof of Birkhoff's completeness theorem](http://www.cse.chalmers.se/~abela/agda/MultiSortedAlgebra.pdf).
A substitution from `Ξ` to `Ξ` associates a term in `Ξ` with each variable in `Ξ`.
```agda
Sub : {π : Signature π π₯} β Type Ο β Type Ο β Type (ov {π = π} Ο)
Sub {π = π} X Y = (y : Y) β Term {π = π} X
_[_] : (t : Term {π = π} Y) (Ο : Sub {π = π} X Y) β Term {π = π} X
(β x) [ Ο ] = Ο x
(node f ts) [ Ο ] = node f (Ξ» i β ts i [ Ο ])
infix 30 _[_]
```
An environment for `Ξ` maps each variable `x : Ξ` to an element of `A`, and equality
of environments is defined pointwise.
```agda
module Environment {π : Signature π π₯}(π¨ : Algebra {π = π} Ξ± β) where
open Algebra π¨ using() renaming(Interp to InterpA )
open Setoid π»[ π¨ ] using ( _β_ )
renaming ( refl to βrefl ; sym to βsym ; trans to βtrans)
Env : Type Ο β Setoid _ _
Env X = record { Carrier = X β π[ π¨ ]
; _β_ = Ξ» Ο Ο' β (x : X) β Ο x β Ο' x
; isEquivalence = record { refl = Ξ» _ β βrefl
; sym = Ξ» h x β βsym (h x)
; trans = Ξ» g h x β βtrans (g x) (h x)
}
}
open Algebra using ( Domain ; Interp )
EnvAlgebra : Type Ο β Algebra {π = π} (Ξ± β Ο) (β β Ο)
Domain (EnvAlgebra X) = Env X
Interp (EnvAlgebra X) β¨$β© (f , aΟ) = Ξ» x β (f ^ π¨) Ξ» i β aΟ i x
cong (Interp (EnvAlgebra X)) {f , a} {.f , b} (refl , aibi) x = cong InterpA (refl , Ξ» i β aibi i x)
```
Interpretation of terms is iteration on the W-type. The standard library offers
`iter' (on sets), but we need this to be a setoid function.
```agda
β¦_β§ : {X : Type Ο}(t : Term {π = π} X) β Func (Env X) π»[ π¨ ]
β¦ β x β§ β¨$β© Ο = Ο x
β¦ node f args β§ β¨$β© Ο = InterpA β¨$β© (f , Ξ» i β β¦ args i β§ β¨$β© Ο)
cong β¦ β x β§ uβv = uβv x
cong β¦ node f args β§ xβy = cong InterpA (refl , Ξ» i β cong β¦ args i β§ xβy )
open Setoid using ( Carrier )
Equal : β {X : Type Ο} (s t : Term {π = π} X) β Type _
Equal {X = X} s t = β (Ο : Carrier (Env X)) β β¦ s β§ β¨$β© Ο β β¦ t β§ β¨$β© Ο
ββEqual : {X : Type Ο}(s t : Term {π = π} X) β s β t β Equal s t
ββEqual .(β _) .(β _) (rfl refl) = Ξ» _ β βrefl
ββEqual (node _ s) (node _ t) (gnl x) =
Ξ» Ο β cong InterpA (refl , Ξ» i β ββEqual (s i) (t i) (x i) Ο)
isEquiv : {Ξ : Type Ο} β IsEquivalence (Equal {X = Ξ})
isEquiv .IsEquivalence.refl = Ξ» _ β βrefl
isEquiv .IsEquivalence.sym = Ξ» x=y Ο β βsym (x=y Ο)
isEquiv .IsEquivalence.trans = Ξ» ij jk Ο β βtrans (ij Ο) (jk Ο)
β¦_β§s : {X Y : Type Ο} β Sub {π = π} X Y β Carrier (Env X) β Carrier (Env Y)
β¦ Ο β§s Ο x = β¦ Ο x β§ β¨$β© Ο
substitution : {X Y : Type Ο} β (t : Term {π = π} Y) (Ο : Sub {π = π} X Y) (Ο : Carrier (Env X))
β β¦ t [ Ο ] β§ β¨$β© Ο β β¦ t β§ β¨$β© (β¦ Ο β§s Ο)
substitution (β x) Ο Ο = βrefl
substitution (node f ts) Ο Ο = cong InterpA (refl , Ξ» i β substitution (ts i) Ο Ο)
```