"In order to find the value of an expression it is necessary to know the value of its components. Thus to find the value of a + 5 + b/a we need to know the values of a and b. Thus we speak of evaluating an expression in an environment (or sometimes relative to an environment) which provides the values of components."
Christopher Strachey, Fundamental Concepts in Programming Languages
In the previous lecture we learned about
In this lecture we will see how to add
into the picture. We start with some general consideration about variables, then run again through our running example from the syntax and semantics lecture and think about how to extend it with variables. This then leads to the new notion of free algebras.
There are at least two possible answers.
Let us think of natural numbers with addition, that is, the algebra
For example, going back in school to when we first encountered variables, we wanted to express, for example, that
and then found that it is much easier to write instead
So, in the first answer, variables are place-holders for the elements of the algebra and just a clever notation to avoid writing infinitely many equations.
What distinguishes high-school algebra from primary school computations with numbers?
One of the main ideas introduced in high-school algebra is that of variables as first-class citizens. For example, when we do computations such as
to find the 0's of the quadratic equation
we treat the variables as part of the data ("first class citizens"), not just as place-holders that represent numbers.
For example, we say that
In programming languages we often call the programming language that is studied the object language, as opposed to the meta-language, which is the language that is used to study the programming language, typically mathematics or English or a mix of the two. We have seen this distinction of object-language and meta-language at work in the lecture on syntax and semantics where the language of expressions exp
is the object language but, for example, the numbers
In primary school the object languages consists of numbers and operations
For example, when we computed above
We will encounter below further examples of meta-variables and object-variables.
Of course, in programming languages variables also play a crucial role, both as mathematical variables and as names for memory cells and other resources.
Variables are sometimes called "names" or "nominals" or "identifiers" if the operation of interest is the equaltiy test
Mathematical variables are names that can have a value. They also can be substituted.
Programming variables are variables the value of which can change over time.
Both mathematical and programming variables can be free or bound, and can have scope. We will hear much more about this when we come to
In this lecture, we will only look at the simplest kind of mathematical variables.
To see what is at issue let us go back to the example of the first lecture.
Recall from the lecture on syntax and semantics that we started with an example of a formal language given by
exp ::= 1 | exp + exp | exp * exp
and we defined its semantics by
What happens if we add variabls to the language?
Pause the lecture and think about it.
First, we need to add variables to the syntax. We can add one variable x
by defining
exp ::= 1 | x | exp + exp | exp * exp
or we can have a set Var
of variables all of which we may want to use in expressions (Question: How many variables are there in your favourite programming language?) and we write
exp ::= 1 | v | exp + exp | exp * exp v in Var
Remark: In the first definition of exp
the symbol x
is the variable we want to use in the expressions. In the second definition v
is a place-holder that ranges over the set of variables Var
.[2] [3] For example, if Var
is the set consisting of x,y,z
, then the second definition of exp
is a short hand for
exp ::= 1 | x | y | z | exp + exp | exp * exp
In programming terminology we can think of the v in Var
as a macro that is expanded to turn the second definition of exp
into the third. In practice, the notational distinction between a variable x
and the place-holder v
that ranges over variables such as x
is usually not made. So don't worry too much about it now. But distinctions such as these will become important again when we will look at compilers next semester. (End of Remark)
How can we modify the semantics to account for variables? We need to add to the three clauses of
one clause for variables. But where do we map variables to? What should
Something is missing here … so let us just add it!
The solution is to introduce an "environment", that is a function
Remark: Notice that
Remark: It is typical for universal algebra that the
The next step is to look at the above definition of
The aim of this section is to show define so-called free algebras and to show how they are used to describe syntax and semantics of languages that inlcude variables.
There are at two different ways to introduce free algebras. A concrete one, via term-algebras, and an abstract one, generalising the universal property of initiality.
The first one emphasises the similarity of variables and constants: The free algebra over a set of variables
Example: Let
exp ::= 1 | exp + exp | exp * exp | x | y | z
with operations
It is clear that the example above generalises to arbitrary signatures. This leads to new
Notation: We denote by
Here we want to show that substitutions are homomorphisms on the term-algebra.
For concreteness let us fix a set
I claim that homomorphisms
are precisely substitutions.
First, every homomorphism
So a substitution just specifies for each variable
This means that we also have the operation of applying a substitution
Activity: Make some concrete examples and think about how to implement the operation that takes a term and a substitution and applies the substitution to the term. Think about why this operation is a homomorphism and why it is important that the signature does not contain the variables as constants.
We summarise the activity by
Proposition: There is a bijective correspondence between functions (substitutions)
Notice the similarity of the proposition with the definition of initiality.
The second way to introduce free algebras directly leads to the official definition of free algebra. Recall the universal property of initiality:
An algebra
How should we modify this universal property if, moving back to our example, instead of the initial algebra
Recall how we introduced the environments
The first three conditions say that
To summarise, we have seen that
For all algebras
We turn this now into a
Definition: Let
Remark: The notation
Remark on the difficulty of maintaining a uniform notation: Why do we change notation from Var to
…
Assigning unique names is a more subtle problem then it may look like at first sight.
(1) For example, if we want to have unique postal addresses, we use house numbers to identify a house in a street. We use street names to identify streets in a city. Etc. But note how uniqueness is achieved by fixing a context such as "in a street", "in a city", etc.
(2) It is common to use numbers as addresses (Activity: List 3 examples of this.) One property that makes numbers suitable for this is that there infinitely many numbers. So if one needs a new name, we know that there must be one available.
(3) It is not always convenient or practical to keep a list of all names that have been assigned. (Activity: List examples.) A common solution then is to use hash-functions. Hashes can be used as names because they are (almost) unique and they can be compared for equality, which is the basic operation one needs on names.
(4) Activity: Think about, in the spirit of our discussion about abstract data types, what are the operations that should be supported by an abstract data type of names. (As in the case of sequences, there may be more than one answer.) ↩︎
x
is an object-variable, v
is a meta-variable. ↩︎
I want to counter the impression that meta-variables are simpler than object-variables. This impression may be created by our examples which suggests that meta-variables are merely placeholders. In fact, meta-variables can carry the same complicated structure as object variables. For example, both exp
and Var
are meta-variables, with exp
being a bound variable and Var
a free variable. We also see that the scope of Var
changes in the remark: First, Var
refers to any set, later Var
is in a scope in which it has value