"To a large extent it is true that the increase in power of programming languages has corresponded to the increase in the size and complexity of the right hand sides of their assignment commands for this is the situation in which expressions are most valuable."
Christopher Strachey in 1967 about the early history of programming languages, see Fundamental Concepts in Programming Languages
The distinction between syntax and semantics, invented by philosophers, logicians and linguists over 100 years ago, is crucial to computer scientists.
Programs are syntactic objects and we use techniques such as context-free grammars and parsing in order to check whether programs are syntactically correct, whether they are legitimate, well-formed programs.
On the other hand, programs have to be intepreted, evaluated, compiles to be executed. This gives us the meaning or semantics of a program. In the worst case, the semantics of a programming languages is only given to use via a compiler together with a target architecture. In programming languages as an area of computer science, we study more abstract methods independen of any particular machine or language in order to explain the meaning of programs.
The methods covered so far in this course include abstract reduction systems, equivalence relations and normal forms.
In this lecture, we add term-algebras, homomorphisms and initiality.
The general idea is that we want to have syntax and semantics in the same "category". Looking back at the lecture on syntax and semantics and the definition of
we will indentify in this lecture both the syntax
But syntax is special. While there can be many algebras to interpret
The answer will be initiality.
But before can come to this we need to explain how terms form an algebra and write down a formal definition of what a homomorphism is.
In the section on Abstract Data Types we argued that data types are not just sets of data, but algebras of data.
Similarly, in this section we will learn that terms form not just sets but algebras. These algebras are called term-algebras. Or absolutely free algebras but this terminology will be explained only later.
For each choice of signature there is one term algebra.
For example, the language
exp ::= 1 | exp + exp | exp * exp
is the term algebra for the signature
Activity: Can you translate any signature
The answer is "yes".
So this can serve as the first definition of term algebra for a signature
But, wait a moment, if
But this is easy, so easy that it almost feels like cheating (but it isn't). We simply define:
This is an inductive definition similar to others we have seen before (induction on the three cases that make up the definition of
So what is going on here?
Activity: Imagine we want to implement a calculator in the C langauge. Terms are trees. Each node in such a tree consists of some data with pointers to the children [1]. Picture
Summary: We have seen that algebra is tightly related to induction.
In a previous lecture we discussed structure preserving maps in detail.
Homomorphisms are the structure preserving maps of algebra.
But before coming to the formal definition of a homomorphism, I want to come back to one of the recurring topics of this course, namely how we can capture meaning in a computational way. This was the main topic of the lecture on meaning in syntax and then brought to a satisfying conclusion in the lecture on normalisation by evaluation.
I will now summarise the main insights of this story under the heading of
In our example of a language
exp ::= 1 | exp + exp | exp * exp
we defined the semantics as follows.
Quotienting
This establishes that, up to notation, terms and numbers are the same.
Or does it?
In fact, all countably infinite sets are bijective. For example, the natural numbers
Indeed, the semantics
To see this in more detail, by definition,
It maps
The definition of homomorphism is, as the definition of algebra, given wrt a signature.
Definition: Let
for all elements
Remark: The colour code is meant to highlight that
Example: In first section we have seen that the language
exp ::= 1 | exp + exp | exp * exp
is the term-algebra for the signature
and comparing with the definition of homomorphism
we see that they exactly agree after identifying
Remark: In other words, we turned our observation of
One of the most important and familiar examples of homomorphism is the clock. The signature is
Exercise: Show that
Activity: Convince yourself that the reason that the clock works so well is that the function
Let us come back to our previous discussion of when two structures are the same up to renaming of their elements. Certainly, we want that "being the same" implies that the elements of the structures can be mapped one-to-one, that is, that there is a bijection from one structure to the other. But as the example of the integers and the rationals showed this is not enough. So we will also ask that the bijection is structure preserving. This is then called a isomorphism.
Definition: An isomorphism of
There is a more elegant definition of isomorphism. It is based on the following observation. Recall that a function is bijective if it is injective (one-to-one) and surjective (onto). We write
Exercise: Let
The observation above emphasises that a bijection is a translation that gives you back what you started with if you translate back and forth (or forth and back).
This is the property that is taken to define isomorphism as it only requires that one has identity arrows and composition of arrows.
Definition:
Intuitively, two structures are isomorphic, if they are the same up renaming of their elements.
This does not sound very exciting, but we have seen some interesting examples.
Example:
Remark: It is important to understand that whether two structures are isomorphic depends on having agreed on what "structure" should consist of. In our current setting, universal algebra, structure is formalised by a signature, that is, the set of operation symbols and their arities.
The following example highlights how being isomorphic depends on the signature. (See the lecture on structure preserving maps for more details on the relationship of integers and rationals.)
Example: If the signature is empty ("no structure"), then
In this section, we will see that the term algebra is characterised up to isomorphism by a so-called universal property, called initiality. In other words, the initial algebras are isomorphic to the term algebra, but are defined without reference to syntax.
We make a big step now. So far we only looked at particular algebras.
Now we look at all algebras for a given signature, or, as one says, the category of
where, again, 1 is a constant and
We know at least two such algebras, namely
We also met a very special such algebra, namely the term-algebra.
Recall that the term-algebra interpretes
So what is the special property of the term-algbra? What makes the term-algebra interesting if it can't even do addition and multiplication?
The special property of the term-algebra
Remark: The property quantifies over all
Fact: The term-algebra is, up to isomorphism, the only initial
Note that the characterisation of the term-algebra as an initial algebra is completely syntax free.
Intial algebras have a simple definition. It only relies on the notions of signature, algebra, and homomorphism.
Definition: Let
Remark: Universal properties are interesting because they abstract away from the implementation, no terms, or operations, or equations appear in its definition. But this advantage also comes at a price. Just from stating this definition, we do not even know whether an initial algebra exists, nor how they can be implemented. Fortunatley, we have seen in the section on term-algebras above that term-algebras are initial algebras.
Examples:
Exercise: Verify the statements made in the examples above.
We often talk about "the" initial algebra, even if the initial algebra is not uniquely determined. But all initial algebras are isomorphic.
Proposition: Let
Exercise: Prove the proposition. (Remark: The proof is short and based on an important idea … but maybe not easy to find if you have never seen it … anyway, give it a try!)
We can think of the universal property of initial algebras as an abstract characterisation of induction.
In a nutshell:
Let us try to understand this better.
Induction means, in the example of natural numbers, that if I have an element
More generally, if
But now look again at the two conditions
Exercise: Verify the statement that
Exercise: Verify that the definition of
We are now ready to summarise one of the most important ideas in the semantics of programming languages, namely that initiality "is" induction.
Instead of starting with inductively defined term-algebras, we take their universal property and use it as a definition of initial algebras. We then use induction to prove the existence of initial algebras, the prove revealing initial algebras as term-algebras.
What is the advantage of this shift of perspective from the point of view of programming languages?
In general terms, starting with term-algebras means starting with the implementation. Starting with universal properties such as initiality means starting with the specification. Initiality is the property we want, the term-algebra provides an implementation.
So the abstract formalism we have been learning so far lines up with important principles of good software engineering: Starting from the specification in a language that is close to the specific problem domain instead of committing too early to a particular implementation.
More specifically, in this course, we are currently using term-rewriting based on universal algebra as our model of computation. It has the advantage of being simple, yet powerful. But not all features of programming languages can be modelled in this framework.
The abstract view of induction as initiality has the advantage that it carries over to more complicated models of computation, which go beyond the realm of universal algebra.
So everything we learn here, can be applied later ("scales to more complicated situations"), because we take care of formulating it at the approriate level of abstraction.
More precisely nodes are either triples
There is a geometric proof that shows that there is no isomorphism preserving addition. Assume that there was such an isomorphism, lining up integers against rationals. Pick two integers/rationals and consider the interval of numbers between them. Start subdividing this interval and continue until you reach a contradiction. ↩︎
Technically, what one needs is a collection
We will return to this below. But if you want to prove the claim now, you need to show that any two initial algebras are isomorphic. ↩︎