---
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][].
<!--
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
open import Overture using (π ; π₯ ; Signature)
module Setoid.Terms.Basic {π : Signature π π₯} 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 ( 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
We take a different approach here, using 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 whose carrier is the setoid of terms.
```agda
module _ {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 : (X : Type Ο) β Setoid (ov Ο) (ov Ο)
TermSetoid X = record { Carrier = Term X ; _β_ = _β_ ; isEquivalence = β-isEquiv }
open Algebra
π» : (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 : 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 (π¨ : 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) Ο Ο)
```