Initial Algebra

Feb. 13, 2024

Let’s begin with the definition of a mathematical structure ‘algebra’.

Definition. (algebra) We call a set together with distinguished constants and operations as an algebra.

Example. (Z,0,+,)(\mathbb{Z}, 0, +, -), (R>0,1,×,()1)(\mathbb{R}_{>0}, 1, \times, (\cdot)^{-1}) are algebras.

If algebra is given, we can define ’type’ of that algebra, which is called as a signature. In Java, you can think signature as interface, and algebra as class. We can also define the signature for integer expressions.

Definition. (signature) Signature is a type of an algebra.

Example. (t,id:t,:t×tt,()1:tt)(t, id: t, \circ: t \times t \to t, (\circ)^{-1}: t \to t) is signature for (Z,0,+,)(\mathbb{Z}, 0, +, -), (R>0,1,×,()1)(\mathbb{R}_{>0}, 1, \times, (\cdot)^{-1}).

Moreover, we may regard a programming language as an algebra! Given grammar, the set of the language expressions represent the programming language itself. Suppose we have following abstract grammar for integer expressions:

intexp::=012varintexpintexpintexp,  {+,,×,÷} \begin{aligned} \langle \text{intexp} \rangle ::= &0 \vert 1 \vert 2 \vert \cdots \vert \\ & \langle\text{var} \rangle \vert - \langle \text{intexp} \rangle | \langle \text{intexp} \rangle \oplus \langle\text{intexp} \rangle, \; \oplus \in \{+, -, \times, \div \} \end{aligned}

The signature for the integer expression algebra is,

Sintexp=(t,0t,1t,,xt,yt,,:tt,:t×tt where {+,,×,÷}) \begin{aligned} S_{\text{intexp}} = (&t, 0 \in t, 1 \in t, \cdots, \\ & x \in t, y \in t , \cdots, -: t \to t, \oplus : t \times t \to t \text{ where } \oplus \in \{+, -, \times, \div \}) \end{aligned}

In this way, we can derive a signautre if an algebra is given.

Question. How many algebras would satisfy the signature SintexpS_{\text{intexp}}?

There can be many algebra having signature SintexpS_{\text{intexp}}. For instance, we can define a trivial algebra where the domain is a singleton set 0\\{0\\}. In this algebra, we cannot distinguish 0, 1, xx, yy, x+yx+y, … everything is collapsed to 0. The algebra we wanted - the algebra of abstract grammar for the integer expression is expressed below. We can think of the set of ’trees’ that collects all possible abstract syntax trees of the integer expression.

Figure 1

Indeed, you would have some feeling that AgrammarA_{\text{grammar}} is the most general algebra having signature SintexpS_{\text{intexp}}. To address it more formally, we define a mapping from ‘general algebra’ to ‘concrete algebra’.

Definition. (homomorphism) Homomorphism is a map between algebras that preserves constants and operations. For algebra U0U_0 and U1,U_1, h:U0U1h: U_0 \to U_1 is a homomorphism if

Exercise. (composition of homomorphisms) For given signature SS, there are algebras U0,U1,U2U_0, U_1, U_2. If h0:U0U1h_0: U_0 \to U_1 and h1:U1U2h_1: U_1 \to U_2 are homomorphisms, then h1h0:U0U2h_1 \circ h_0: U_0 \to U_2 is also homomorphism.

Keep in mind that homomorphism does not guarantee the existance of inverse homomorphism. Even we have homomorphism h:U0U1h: U_0 \to U_1, we may not able to find a homomorphism h1:U1U0h^{-1}: U_1 \to U_0. If we have such inverse homomorphism, we say that U0U_0 and U1U_1 are isomorphic.

Based on this idea, we can think of a tree-like hierarchy of algebras. Suppose for signature SS, there are algebras U0,U1,U2,U_0, U_1, U_2, \cdots. If we find all the homomorphisms between the algebras, it will be something like:

Figure 2

Then we can imagine that there are roots (U0U_0 and U3U_3) - most ‘general’ algebras of the given signature. We call them as an initial algebra, and this is algebraic foundation of programming language!

Definition. (initial algebra) We say AA is an initial algebra of signature SS if for all algebras AA^\prime of SS, there exists a unique homomorphism h:AAh: A \to A^\prime.

Exercise. Prove that AgrammarA_{\text{grammar}} is an initial algebra of SintexpS_{\text{intexp}}.

Exercise. If A1,A2A_1, A_2 are initial algebras, then there are homomorphisms f:A0A1f: A_0 \to A_1 and g:A1A0g: A_1 \to A_0 such that fg=idf \circ g = id and gf=idg \circ f = id. This means that all initial algebras of SS are essentially the same, i.e. isomorphic.