Title: Comp 205: Comparative Programming Languages
1Comp 205Comparative Programming Languages
- Semantics of Functional Languages
- Term- and Graph-Rewriting
- The ?-calculus
- Lecture notes, exercises, etc., can be found at
- www.csc.liv.ac.uk/grant/Teaching/COMP205/
2Term Rewriting
A straightforward way of implementing
a functional programming language is to
implement term-rewriting. The Haskell
interpreter evaluates expressions (terms) by
"substituting equals for equals".
3For example, given the following definitions
fib 0 1 fib 1 1 fib n fib (n-1) fib
(n-2), if ngt1
An evaluation might proceed as follows
fib 3 ? (fib 2) (fib 1) ? (fib 1) (fib
0) (fib 1) ? 1 (fib 0) (fib 1) ? 1 1
(fib 1) ? 1 1 1 ? 3
4Terms and Trees
A standard way of representing terms is as trees
5Term Rewriting
Term rewriting replaces (sub)trees
? (rewrites to)
1
6Rewriting Trees
... giving the new tree
1
Fn.Appl.
Fn.Appl.
fib
fib
1
0
7Side-Effects
In the imperative paradigm, all evaluation
can change the current state (e.g., by
side-effects).
int funnyCount0 int funny(int i) return
funnyCount i
funny(2) funny(2) // might be 02
8Functional Expressions
In the functional paradigm there is no state, so
an expression always denotes the same value, and
evaluation simply converts an expression to its
value.
This important property of functional
languages is referred to as "referential
transparency".
9Referential Transparency
Referential Transparency any expression
denotes a single value, irrespective of its
context. Consequently, (sub)expressions can be
replaced by their values without changing the
behaviour of a program. (Referential
transparency no side-effects)
10Graphs
Expressions can also be represented by graphs
Fn.Appl.
Fn.Appl.
fib
fib
1
0
11Graph Rewriting
This allows identical subexpressions to be
rewritten "in one go"
? (rewrites to)
12The ?-Calculus
The ?-calculus was developed by the mathematician
Alonzo Church as a tool to study functions and
computability. The ?-calculus provides a very
simple model of computable function, and inspired
the designers of the first functional
programming language, LISP. The ?-calculus also
provides an operational semantics for functional
languages.
13Computability
Alan Turing developed an abstract machine (the
Turing Machine) and showed that it provided a
universal model of computation. He showed
that the Turing-computable functions were
exactly the general recursive functions. Church
showed that the Turing-computable functions were
exactly those that could be represented in the
?-calculus (the ?-computable functions).
14Church-Turing Hypothesis
Both Turing and Church conjectured that
their model of computability formalised the
intuitive notion of mathematical
computability. The Church-Turing Hypothesis
states that the equivalent notions of Turing- and
?-computability capture precisely "every function
that would naturally be regarded as computable".
15?-Terms
- Church designed the ?-calculus as a tool to
- study the fundamental notion of computable
- function. He therefore sought to strip away all
- but the bare essentials.
- The syntax for ?-terms is accordingly very
simple. - ?-terms are built from
- variables
- function application
- ?-abstraction (declaring formal parameters)
- (and brackets)
16Syntax of ?-Terms
- The set ? of ?-terms is defined by
- ? Var ? ? ?Var. ? (?)
- where Var is a set of variables.
- Looking at each clause in turn
- variablesTypically, we use a, b, c, ..., x, y, z
for variablesif we run out of variable names,
we can use x', x'', x''', etc.
17Syntax of ?-Terms
- function applicationIf M and N are ?-terms, so
too is M N ,which represents the application of
M to N.(All ?-terms can be considered
functions.) - ?-abstractionIf M is a ?-term, so too is
?x.M,which can be thought of as a functionwith
formal parameter x,and with body M.
18Some Examples
- x
- x y
- ?y.(x y)
- ?x.?y.x y
- brackets?-abstraction binds less tightly than
functionapplication, so the last example above
shouldbe read as ?x.(?y.(x y)) not
(?x.(?y.x)) y .Also, x y z should be read as
(x y) z .
19Evaluation of ?-Terms
A ?-term ?x.M represents a function whose formal
parameter is x and whose body is M. When this
function is applied to another ?-term N, as in
the ?-term (?x.M) N , evaluation
proceeds by replacing x with N in the body M.
For example, (?x.?y.x) (?z.z) ?
?y.?z.z In order to make this precise, we need
the notions of free and bound variables.
20Free and Bound Variables
Given a ?-term ?x.M, we say that ? binds
ocurrences of x in M. We also say that x is
bound in (?x.M) . A free variable is one that
is not bound by a ?-abstraction. A ?-term is
closed if it has no free variables.
21Free Variables
- Given a ?-term M, we write FV(M) for the set of
- free variables in M. This set is defined as
follows - FV( x ) x In a ?-term consisting of just
a variable,that variable is free. - FV( M N ) FV(M) ? FV(N)Function application
doesn't bind variables. - FV( ?x.M ) FV(M) - x If x is a bound
variable, then x is not free.
22Evaluation of ?-Terms
Given a function application of the form
(?x.M) N , evaluation proceeds by replacing all
free occurrences of x in the body M with N.
For example, (?x.?y.x) ?z.z ?
?y.?z.z Here, the argument ?z.z replaces the
variable x, which is free in the body ?y.x of the
function being applied.
23Evaluation of ?-Terms
However, (?x.(?x.x)) ?z.z ?
?x.x Here, there is no substitution, since x is
not free in the body ?x.x.
24ß-Conversion
In general, we write (?x.M) N ?ß Mx
? N , where Mx ? N denotes the result of
replacing all free occurrences of x in M with
N. This form of evaluation is referred to
as ß-conversion, for which we use the symbol ?ß .
25Example
((?x. ?y. y x) (?z.z)) (?u. ?v. u) ?ß
( (?y. y x)x ? (?z.z) ) (?u. ?v.
u) (?y. y (?z.z)) (?u. ?v. u) ?ß
( (y (?z.z))x ? (?u. ?v. u) )
(?u. ?v. u) (?z.z) ?ß ?v. ?z. z
26a-Conversion
Just as with functions, formal parameters
simply serve as place-holders, representing
possible arguments. In the ?-calculus, formal
parameters (i.e., bound variables) can be
renamed. This is referred to as a-Conversion
(written ?a). For example, ?x. ?y. y
?a ?x. ?v. v ?a ?u. ?v. v
27ß-Conversion Again
In general, we write (?x.M) N ?ß Mx
? N , where Mx ? N denotes the result of
replacing all free occurrences of x in M with
N, provided that no free variables in N become
bound as a result of this substitution. For
example, (?x. ?y. y x) (?z.y) ?ß
?y. y (?z.y) is not allowed.
28a- and ß-Conversion
Sometimes it is necessary to apply a-conversion
before ß-conversion can be applied. For
example, (?x. ?y. y x) (?z.y) ?a
(?x. ?u. u x) (?z.y) ?ß
(?u. u x)x ? (?z.y)
?u. u (?z.y)
29?-Conversion
A final reduction relation on ?-terms is
?-conversion. This applies to ?-terms of the
form ?x. M x, where x is not free in M. Such a
function takes an argument and applies M to that
argument it is therefore equal to M itself.
This is the idea behind ?-conversion (??)
?x. M x ?? M provided that x is
not free in M.
30Example
- ?y. (?x. y x) ?? ?y. y
- But ?-conversion is not applicable to
- ?x. (?y. y x)
- ?y. (?x. (x y) x)
31Computing with ?-Terms
How does the ?-calculus relate to
real programming languages such as Haskell?
32Summary
- Key points
- ?-terms
- ß-conversion
- a-conversion
- ?-conversion
- Next Computing with ?-terms