Introduction to Functional Programming
Introduction to Functional Programming
Lecture 1
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Syllabus I
λ-calculus
■ type-free λ-calculus as an abstract programming language
(confluence, reduction strategies, datatype representation);
■ relation to computability (Turing-completeness, infinitary
datatypes);
■ simply typed λ-calculi (Church-style, Curry-style);
■ polymorphic λ-calculi (the λ-cube, logical interpretation);
■ proofs as programs.
2 of 472
Syllabus II
Category theory
■ categories (abstract and concrete categories, product categories,
subcategories);
■ basic constructions (morphisms, limits and colimits, exponentials,
subobject classifiers, power objects);
■ functors and natural transformations (definitions and examples);
■ adjunctions (definition and examples);
■ models of the λ-calculi (Lambek’s constructions).
3 of 472
Texts I
λ-calculus:
■ [HS] J.R. Hindley and J.P. Seldin, Lambda-calculus and
Combinators: an Introduction, Cambridge University Press (2008).
ISBN: 978-0521898850
The lectures are based on this book
■ [Bar] [Link] The Lambda Calculus: Its Syntax and
Semantics, 2nd edition, North-Holland Elsevier (1984).
ISBN: 978-0444875082
■ [Bar2] H.P. Barendregt, Lambda Calculus with Types, Chapter 2 of
S. Abramsky, D.M. Gabbay, T.S.E. Maibaum (editors), Handbook
of Logic in Computer Science, volume 2, Clarendon Press (1992).
ISBN: 978-0198537618
4 of 472
Texts II
λ-calculus:
■ [Pierce] B.C. Pierce, Types and Programming Languages, MIT
Press (2002).
ISBN: 978-0262162098
■ [Sel] Peter Selinger, Lecture Notes on the Lambda Calculus,
[Link]
5 of 472
Texts III
Category theory:
■ [Pierce2] B.C. Pierce, Basic Category Theory for Computer
Scientists, MIT Press (1991).
ISBN: 978-0262660716
The lectures are based on this book
■ [MacLane] S. Mac Lane, Categories for the Working
Mathematician, 2nd edition, Springer-Verlag (1998).
ISBN: 978-0387984032
■ [Goldblatt] R. Goldblatt, Topoi: The categorical analysis of logic,
Elsevier (1984).
ISBN: 978-0444867117
This book has been republished at a nicer price by Dover (2009).
ISBN: 978-0486450261.
6 of 472
Texts IV
Category theory:
■ [Lambek] L. Lambek and P.J. Scott, Introduction to Higher Order
Categorical Logic, Cambridge University Press (1988).
ISBN: 978-0521356534
■ [Cats] J. Adamek, H. Herrlich and G. Strecker, Abstract and
concrete categories: The joy of cats,
[Link]
■ [Rydeheard] D.E. Rydeheard, R.M. Burnstall, Computational
Category Theory, Prentice Hall (1988).
ISBN: 978-0131627369.
[Link]
7 of 472
Texts V
8 of 472
Examination
The examination will be oral. It covers the whole program, and the
student will be asked to answer to simple exercises, to prove results as
seen in lessons, and to show understanding of the basic concepts of
the course.
Examinations will take place six times per year, at prefixed dates.
Whoever wants to undertake the examination, must subscribe for the
date.
Students are required to bring their study material (books, handouts,
etc.) to the examination.
9 of 472
Introduction
10 of 472
An example: Quicksort I
11 of 472
An example: Quicksort II
12 of 472
An example: Quicksort III
13 of 472
An example: Quicksort IV
As you can see, the code is compact. There are no extra variables
which are not needed in the definition.
A simple test reveals that the code is fast, too. This is due to the fact
that partition is tail-recursive.
A simple test reveals that the code uses more space than the usual
iterative version in, let say, C language. This is not a problem on
modern computer with Gigabytes of RAM; in any case, a space
efficient version can be developed.
14 of 472
Another example: Summation I
15 of 472
Another example: Summation II
16 of 472
Another example: Summation III
17 of 472
References and Hints
In this section, students will find references for the material which has
been explained. Usually, they are textbooks or research articles where
the lesson has been taken from.
18 of 472
Fundamentals of Functional Programming
Lecture 2
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
This is the first real lesson in this course: our aim is to introduce the
λ-calculus in its untyped version.
20 of 472
A programming introduction to λ-calculus
21 of 472
Syntax I
22 of 472
Syntax II
To simplify notation, capital letters (M, N, P, . . . ) will denote
arbitrary λ-terms; also, x, y , z, u, v , w will denote variables.
Parentheses are suppressed according to the following rules:
■ we always omit outermost parentheses;
■ we write (λx . P Q) for (λx . (P Q));
■ we write (M1 M2 · · · Mn ) for ((. . . (M1 · M2 ) . . .) · Mn ), i.e., application
associates to the left;
■ we write (λx1 , x2 , . . . , xn . M) for (λx1 . (λx2 . (. . . (λxn . M) . . . )))
Finally, we write M ≡ N to indicate that M and N are syntactically
identical, i.e., they are equal as strings.
Evidently, M N ≡ P Q implies M ≡ P and N ≡ Q, and λx . M ≡ λy . N
implies x ≡ y and M ≡ N. The vice versa holds, too.
23 of 472
Syntax III
Most proofs and definitions are given by induction on the structure of
λ-terms. Here are some useful examples.
Definition 2.2 (Occurrence)
For λ-terms P and Q, we say that P occurs in Q iff one of the
following cases applies
■ P ≡Q (*);
■ Q ≡ M N and P occurs in M or P occurs in N;
■ Q ≡ λx . M and P occurs in M.
An occurrence of P in Q is whenever the clause (*) applies.
26 of 472
Syntax VI
Lemma 2.6
1. If P ≡α Q then FV(P) = FV(Q);
2. The relation ≡α is an equivalence, i.e.,
ä P ≡α P;
ä P ≡α Q implies Q ≡α P;
ä P ≡α Q and Q ≡α R implies P ≡α R.
Proof.
The first statement is by induction on the definition of α-conversion.
The second statement is easy, except for symmetry, which is treated
by induction on the definition of α-conversion.
[Exercise] Complete the details.
27 of 472
Operational Semantics I
28 of 472
Operational Semantics II
29 of 472
Operational Semantics III
30 of 472
References and Hints
Finally, do and redo the proofs until their logic is clear to you. They
are required to pass the examination and, most of all, what is
evaluated is your understanding rather than your memory.
31 of 472
Fundamentals of Functional Programming
Lecture 3
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
33 of 472
Confluence I
β-reduction behaves naturally with respect to substitution.
Lemma 3.1
1. If P Bβ Q then FV(Q) ⊆ FV(P);
2. If P Bβ P 0 and Q Bβ Q 0 then P[Q/x] Bβ P 0 [Q 0 /x].
Proof. (i)
First, we prove that, if M B1β N, then FV(N) ⊆ FV(M). So, let
M ≡ M 00 [(λx . M 0 ) N 0 /z] and N ≡ M 00 [(M 0 [N 0 /x])/z]. Thus,
FV(N) ⊆ (FV(M 00 ) \ { z }) ∪ (FV(M 0 ) \ { x }) ∪ FV(N 0 ) = FV(M).
Containment can be strict, e.g., consider M ≡ (λx . u) v and N ≡ u.
Since P ≡ P1 B1β P2 ≡α P20 B1β · · · B1β Pn ≡α Q, by induction on n, the
statement (1) holds because α-conversions do not change the set of
free variables. ,→
34 of 472
Confluence II
,→ Proof. (ii)
As for (2), by induction on the length of the reduction P Bβ P 0 , it
suffices to prove the cases P ≡ P 0 and P B1β P 0 .
The former case is evident, since the reduction Q Bβ Q 0 can be
replicated in the context P[Q/x] yielding P 0 [Q 0 /x].
The latter case is by induction on the length of the reduction Q Bβ Q 0 .
Again it suffices to prove the property when P B1β P 0 and Q B1β Q 0 . It
is safe to assume that P contains no bounded variables in FV(x Q) (if
not, we can take P ∗ ≡α P).
Hence, P[Q/x] Bβ P[Q 0 /x] by reducing the substituted occurrences of
Q. But every redex which is present in P, will also be present in
P[Q 0 /x], so we can contract it.
35 of 472
Confluence III
Theorem (Church-Rosser)
If P Bβ M and P Bβ N, then there exists a term T such that M Bβ T
and N Bβ T .
36 of 472
Confluence IV
37 of 472
Confluence V
38 of 472
Confluence VI
Again, this definition is the same as the one for λ-terms extended to
marked terms.
39 of 472
Confluence VII
Definition 3.5 (Forgetful map)
The function | · | : Λ∗ → Λ maps every marked λ-term into the
| |
corresponding unmarked term. We write M / N if |M | = N.
Proof.
By induction on the length n of M Bβ N. If n = 0, M ≡ N so N 0 ≡ M 0 .
If n = 1, N is obtained by contracting a redex in M and N 0 can be
obtained by contracting the same redex in M 0 . Otherwise, M ≡α N
and N 0 ≡α M 0 . If n > 1, the conclusion follows by transitivity.
41 of 472
Confluence IX
Lemma 3.8
Let M , M 0 , N , L ∈ Λ∗ . Then
1. If x , y ∈ V with x 6≡ y and x 6∈ FV(L), then
(M[N/x])[L/y ] = (M[L/y ])[N[L/y ]/x].
2. φ(M[N/x]) = φ(M)[φ(N)/x];
3. If M Bβ∗ N then φ(M) Bβ φ(N).
Proof.
(1) By induction on the structure of M.
(2) By induction on the structure of M, using (1) when
M ≡ (λ∗ y . P) Q.
(3) By induction on the length of the reduction, using (2).
[Exercise] Fill the details.
42 of 472
Confluence X
Lemma 3.9
If M ∈ Λ∗ then there is a reduction |M | Bβ φ(M).
Proof.
By induction on the structure of M.
Lemma 3.10 (Strip lemma)
Let M , N1 , N2 ∈ Λ. If M B1β N1 and M Bβ N2 , then there is N3 ∈ Λ such
that N1 Bβ N3 and N2 Bβ N3 . That is, the following diagram commutes
β
M / N2
1β β
N1 / N3
β
43 of 472
Confluence XI
Proof.
Since M B1β N1 , the redex R ≡ (λx . P) Q occurs in M and gets
contracted in N1 . Let M 0 ∈ Λ∗ be obtained by replacing R in M with
R 0 ≡ (λ∗ x . P) Q. Then |M 0 | = M and φ(M 0 ) = N1 .
By Lemma 3.7 there is N20 ∈ Λ∗ such that |N20 | = N2 and M 0 Bβ∗ N20 .
By Lemma 3.8, there is N3 ∈ Λ such that N3 = φ(N20 ) and N1 Bβ N3 .
Finally, by Lemma 3.9, it holds that N2 Bβ N3 . In diagrams:
M aBQQQ
BB QQQ β
BB QQQ
β BB QQQ
| | B QQQ
o 0
(
N1 M N2`A
φ AA
AA| |
AA
β
A
' o (
N3 N20
φ
44 of 472
Confluence XII
Proof.
By induction on the length of the reduction P Bβ M by means of the
Strip Lemma. In diagrams:
P / P1 +3 Pk /M
N / N1 +3 Nk /T
Two terms are equal if they are mutually reducible, modulo a suitable
renaming. If we think to terms as stages of a computation, two terms
are equal if they are different stages of the same computation.
46 of 472
β-equality II
Lemma 3.13
β-equality is an equivalence relation.
Proof.
Evident.
Lemma 3.14
If M =β M 0 and N =β N 0 then M[N/x] =β M 0 [N 0 /x].
Proof.
By induction on the definition of M =β M 0 , via Lemma 3.1.
47 of 472
β-equality III
Proof.
By induction on the generation of =β : Let P1 , . . . , Pn be a sequence
such that P1 ≡ M, Pn ≡ N and, for every i < n, P1 B1β Pi +1 or
Pi +1 B1β Pi or Pi ≡α Pi +1 .
The case n = 1 is trivial.
For 1 < i < n, the induction hypothesis says that M Bβ Ti and Pi Bβ Ti
since M =β Pi . If Pi B1β Pi +1 then Theorem 3.11 provides the required
Ti +1 such that M Bβ Ti +1 and Pi +1 Bβ Ti +1 . Otherwise, if Pi +1 B1β Pi
or Pi ≡α Pi +1 then Ti +1 ≡ Ti satisfies the statement.
48 of 472
β-equality IV
Corollary 3.16
■ If P =β Q and Q is a β-nf, then P Bβ Q;
■ If P =β Q and P and Q are β-nfs then P ≡α Q;
■ If P =β Q then either P and Q both have the same β-nf (modulo
α-conversion), or they both have no normal form;
■ A term P has at most one normal form, modulo α-conversion.
Proof.
Since β-nfs have no redexes, (1) and (2) are immediate. (3) is a direct
consequence of β-equality being an equivalence relation; (4) is evident
from (1) and (2).
49 of 472
References and Hints
Finally, get familiar with the style of exposing statements and proofs:
although somewhat difficult in the beginning, this is the standard way
to present mathematical results, and it is adopted in any (decent)
book.
50 of 472
Fundamentals of Functional Programming
Lecture 4
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
52 of 472
Combinators I
53 of 472
Combinators II
Important combinators are given special names. The following ones
are mostly used:
■ I = λx . x — Identity I(x) = x;
■ K = λx , y . x — Constant functions Ka (x) = K(a)(x) = a;
■ S = λx , y , z . x z (y z) — Stronger composition
S(f , g )(x) = f (x , g (x));
■ B = λx , y , z . x (y z) — Function composition B(f , g )(x) = f (g (x));
■ B0 = λx , y , z . y (x z) — Reversed composition B0 (f , g )(x) = g (f (x));
■ C = λx , y , z . x z y — Commutative operator C(f )(x , y ) = f (y , x);
■ W = λx , y . x y y — Doubling operator W(f )(x) = f (x , x);
■ Ω = (λx . x x)(λx . x x) — Non-terminating operator.
54 of 472
Combinators III
It is possible to show that only S and K are needed to build all the
listed combinators. For example, I =β S K K.
55 of 472
Combinators IV
56 of 472
Combinators V
57 of 472
Fixed Points I
In the λ-calculus,
Theorem 4.2 (Fixed-point)
There is a combinator Y such that Y x Bβ x (Y x).
58 of 472
Fixed Points II
Corollary 4.3
■ There is a combinator Y such that Y x =β x(Y x);
■ The equation M X =β X has X ≡ Y M as a solution;
■ The equation X y1 . . . yn =β Z with y1 , . . . , yn variables and Z a term,
can be solved for X , i.e., there is a term X such that
X y1 . . . yn =β Z [X /x].
Proof.
(1) and (2) are evident. For (3), choose X ≡ Y(λx , y1 , . . . , yn . Z ).
59 of 472
Reduction Strategies I
Definition 4.4 (Contraction)
Given a λ-term X , a contraction in X is a triple 〈X , R , C 〉 where R is a
redex occurrence in X and C is the result of contracting R in X . A
contraction is written as X BR C .
61 of 472
Reduction Strategies III
62 of 472
Reduction Strategies IV
63 of 472
Reduction Strategies V
64 of 472
Reduction Strategies VI
65 of 472
Reduction Strategies VII
66 of 472
Reduction Strategies VIII
67 of 472
References and Hints
68 of 472
Fundamentals of Functional Programming
Lecture 5 — Intermezzo
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
70 of 472
Data Structures I
71 of 472
Data Structures II
Every data structure D can be represented, that is, every term
constructed from the language D can be uniquely written as a λ-term
in a way that allows to manage the term and its information content.
The representation we use is a type-free version of a result due to
Corrado Böhm and Alessandro Berarducci.
Definition 5.2 (Representation of data structures)
© ª
Let D = 〈S , F 〉 a data structure, where F = f1 , . . . , fn . A term t on D
has the form f (t1 , . . . , tm ) with f : s1 × · · · × sm → s and t1 , . . . , tm terms
of type s1 , . . . , sm , respectively. This term is represented by a λ-term
t ≡ f t1 . . . tm where f ≡ fi for some index i and
f ≡ λx1 , . . . , xm , f1 , . . . , fn . fi s1 . . . sm with
½
xi if si is a parameter sort
si ≡
(xi f1 . . . fm ) otherwise
72 of 472
Booleans I
Booleans form the data structure
73 of 472
Booleans II
Since, in our representation, a constant c : s ∈ F is represented as
c ≡ λf1 , . . . , fn . c, i.e., the i-th projector, it follows that
if ≡ λp , x , y . p x y
if c then x else y ≡ if c x y ,
75 of 472
Enumerations I
76 of 472
Enumerations II
As we did for booleans, we can introduce a selector for enumerations:
case ≡ λp , x1 , . . . , xn . p x1 . . . xn ,
case e
e1 : a1
..
.
en : an
end
77 of 472
Tuples
Tuples are instances of the family of data structures:
A1 × · · · × An = 〈{ A1 , . . . , An , T }, { tuple : A1 × · · · × An → T }〉 .
i-th(tuple x1 . . . xn ) =β xi .
78 of 472
Natural numbers I
The natural numbers can be encoded in many ways. The most elegant
one is due to Alonzo Church. It naturally follows from the general
representation we adopted.
Natural numbers are the elements of the data structure
Nat ≡ 〈{ N }, { suc : N → N , 0 : N }〉 .
79 of 472
Natural numbers II
80 of 472
Natural numbers III
81 of 472
Lists I
The data structure of lists over a given type A is
85 of 472
Fundamentals of Functional Programming
Lecture 6
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
87 of 472
Representability
It is easy to check that the combinators suc, add, mult, and expt, as
previously defined, represent the corresponding operations.
88 of 472
Primitive recursive functions I
Definition 6.2 (Primitive recursive functions)
The set of primitive recursive functions on natural numbers is defined
by induction:
■ The successor function suc is primitive recursive;
■ The number 0 is primitive recursive;
■ For each n ≥ 1 and k ≤ n, the projection function Πnk is primitive
recursive, where
Πnk (m1 , . . . , mn ) = mk ;
■ If n, p ≥ 1 and ψ, ξ1 , . . . , ξp are primitive recursive, then so is φ
defined by composition
,→
89 of 472
Primitive recursive functions II
,→ (Primitive recursive functions)
■ If ψ and ξ are primitive recursive, then so is φ defined by recursion
as follows
φ(0, m1 , . . . , mn ) = ψ(m1 , . . . , mn ) ,
φ(k + 1, m1 , . . . , mn ) = ξ(k , φ(m1 , . . . , mn ), m1 , . . . , mn ) .
Example 6.3
The predecessor function pre defined by pre(0) = 0 and pre(k + 1) = k
is primitive recursive.
In fact, by recursion, pre(0) = 0 and pre(k + 1) = Π21 (k , pre(k)).
90 of 472
Primitive recursive functions III
Theorem 6.4
Every primitive recursive function φ can be represented by a
combinator φ.
Proof.
The combinator φ is defined by induction.
■ suc ≡ λu , x , y . x (u x y ), as before;
■ 0 ≡ λx , y . y , as before;
■ Πnk ≡ λx1 , . . . , xn . xk ;
■ (Composition) φ ≡ λx1 , . . . , xn . ψ (ξ1 x1 . . . xn ) . . . (ξp x1 . . . xn );
■ (Recursion) φ ≡ λu , x1 , . . . , xn . R (ψ x1 . . . xn )(λu , v . ξ u v x1 . . . xn ) u,
where R is a recursion combinator satisfying R X Y 0 =β X and
R X Y suck +1 (0) =β Y k(R X Y k).
91 of 472
Primitive recursive functions IV
DXY 0 Bβ X
D X Y k + 1 Bβ Y .
Q Y (D n X ) Bβ D n + 1 (Y n X )
(Q Y )k (D 0 X ) Bβ D k W
92 of 472
Primitive recursive functions V
Calculating, we get:
■ R X Y 0 Bβ 0 (Q Y ) (D 0 X ) 1
Bβ D 0 X 1
Bβ X
■ R X Y k + 1 Bβ k + 1 (Q Y ) (D 0 X ) 1
Bβ (Q Y )k +1 (D 0 X ) 1
Bβ (Q Y ) ((Q Y )k (D 0 X )) 1
Bβ (Q Y ) (D k W )) 1
Bβ D k + 1 (Y k W ) 1
Bβ Y k W
Thus, R X Y k + 1 =β Y k (R X Y k), as required .
93 of 472
Primitive recursive functions VI
94 of 472
Primitive recursive functions VII
95 of 472
Iteration I
96 of 472
Iteration II
Example 6.5
Consider the following code:
var x := 0; y := 0; z := 0;
α: x := x + 1;
β: if y < z then goto α else y := x + y ;
γ: if z > 0 then begin z := z − x; goto α; end else stop;
In a functional representation:
α(x , y , z) = β(x + 1, y , z);
β(x , y , z) = if y < z then α(x , y , z) else γ(x , x + y , z);
γ(x , y , z) = if z > 0 then α(x , y , z − x) else (x , y , z);
Executing α(0, 0, 0), we get exactly the same computation as for the
procedural code.
97 of 472
Iteration III
98 of 472
Partial recursive functions I
99 of 472
Partial recursive functions II
Theorem 6.7
Every partial recursive function can be represented by a combinator.
Proof. (i)
Let ψ and ξ be primitive recursive and, for all m1 , . . . , mn ∈ N, let
φ(m1 , . . . , mn ) = ψ(µk[ξ(m1 , . . . , mn , k) = 0]) .
,→
100 of 472
Partial recursive functions III
,→ Proof. (ii)
Thus, if φ(m1 , . . . , mn ) is defined, it is represented by
F ≡ λx1 , . . . , xn . ψ(H x1 . . . xn 0) .
For all m1 , . . . , mn ∈ N, we have F m1 . . . mn =β φ(m1 , . . . , mn ).
Let T ≡ λx . D 0 (λu , v . u (x (suc v )) u (suc v )) and
P ≡ λx , y . T x (x y )(T x) y . Then, if X Y =β 0, P X Y =β Y , and if
X Y =β m + 1, P X Y =β P X (suc Y ).
Define
φ ≡ λx1 , . . . , xn . P (ξ x1 . . . xn ) 0 I (F x1 . . . xn ) .
In the first place, the previous result shows that the λ-calculus is
Turing-complete, since it can compute all the computable functions,
and nothing more.
103 of 472
References and Hints
104 of 472
Fundamentals of Functional Programming
Lecture 7 — Intermezzo
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
We have seen that λ-calculus is Turing-complete. As a side effect, we
have seen that a very general form of recursion can be used. In fact,
as far as we are able to state a problem as a set of primitive recursive
equations, and we ask for a minimal solution, we know that there is a
λ-term which satisfies the equations.
107 of 472
Currying II
Currying seems easy, but it is powerful too. In fact, if add x y = x + y ,
we can define suc ≡ add 1. In general, we can partially apply a curried
function, leaving its last arguments free, to obtain new functions.
109 of 472
List manipulation II
More complex functions are written, starting from these basic bricks.
take [] i = []
take (x :: xs) i = if (i > 0) then x :: (take xs (i − 1)) else [] .
Here, and in the following, we write [] for nil, the empty list, and x :: xs
for cons x xs, following the ML syntax.
110 of 472
List manipulation III
The opposite of take is drop, which leaves out the first n elements
from a given list. It is defined as:
drop [] i = []
drop (x :: xs) i = if (i > 0) then (drop xs (i − 1)) else (x :: xs) .
[] @ L =L
(x :: xs) @ L = x :: (xs @ L) .
111 of 472
List manipulation IV
Reversing a list is easy. We show an efficient algorithm:
rev = revAux []
revAux L [] =L
revAux L (x :: xs) = revAux (x :: L) xs .
Notice how currying has been used to define rev from revAux. It is
often convenient to use additional arguments in a function, to
accumulate intermediate results. Then, we can get rid of them via
currying.
map f [] = []
map f (x :: xs) = (f x) :: (map f xs) .
Notice how currying map lifts a function f from elements to lists. So,
for example, double ≡ map (λx . x ∗ 2).
113 of 472
List functionals II
Another important example of functional is filter. Given a predicate p,
i.e., a function returning a boolean value, and a list L, it returns the
sublist of L whose elements make p true.
114 of 472
List functionals III
transpose ([] :: L) = []
transpose r = (map hd r ) :: (transpose (map tl r )) .
115 of 472
List functionals IV
Other interesting functionals on lists are exists and forall. They both
take as arguments a predicate p and a list L. The former functional,
exists, is true when there is an element in L satisfying p, while the
latter, forall, is true when every element in L satisfies p.
exists p [] = false
exists p (x :: xs) = (p x) or (exists p xs)
forall p [] = true
forall p (x :: xs) = (p x) and (forall p xs) .
116 of 472
List functionals V
As an application, list membership is readily expressed as:
But, since functional languages are typed, there is a class of types that
takes care of distinguishing types with an equality function from those
without. Functions like mem are, implicitly, defined only on “equality
types”.
117 of 472
List functionals VI
More complex functionals are possible. Two of them are fundamental:
foldl and foldr, “fold left” and “fold right”.
foldl f e [] =e
foldl f e (x :: xs) = foldl f (f x e) xs
foldr f e [] =e
foldr f e (x :: xs) = f x (foldr f e xs) .
118 of 472
List functionals VII
119 of 472
Sequences I
The idea behind infinite lists, or sequences as they are usually called, is
to store their values as a function. More technically, a sequence is a
data structure defined as:
〈{ A, S }, { Cons : A × (1 → S) → S , Nil : S }〉 .
Cons ≡ λx , y , u , v . u x (K (y u v ))
Nil ≡ λu , v . v .
It is immediate to define
hd (Cons x xs) = x
tl (Cons x xs) = xs
null Nil = true
null (Cons x xs) = false
where the solution for hd is λx . x K Nil, while, as before, tl and null are
complex λ-terms.
121 of 472
Sequences III
Consider the function
it evaluates as
If we assume that the reduction strategy does not expand K (from 2),
the reduction stops after one step.
take 0 s = []
take n (Cons x s) = x :: (take (n − 1) (s()))
interleave Nil y =y
interleave (Cons x s) y = Cons x (K (interleave y (s())))
124 of 472
Sequences VI
Most of the functionals on lists can be immediately redefined to
operate on sequences:
map f Nil = Nil
map f (Cons x s) = Cons (fx) (K (map f (s())))
filter p Nil = Nil
filter p (Cons x s) = if (p x) then (Cons x (K (filter p (x()))))
else (filter p (s()))
Clearly, exists and forall are not useful, since their full evaluation
requires to inspect to whole sequence.
126 of 472
Prime numbers II
The algorithm to generate the prime numbers is simple to encode
using sequences.
127 of 472
References and Hints
128 of 472
Fundamentals of Functional Programming
Lecture 8
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
Till now, we have worked within the pure λ-calculus, showing that it
can be seen as a Turing-complete programming language.
130 of 472
Church vs Curry Typing
131 of 472
Type Algebras
There are many significant typing algebras. The simplest one is due to
Church and its called simple (theory of) types. This algebra can be
used in both a Church-style and a Curry-style.
By adding more structure to the simple types, one can develop more
sophisticated type systems. If the additional structure is developed
with some care, these systems allow to model a computational
meaning for many logical system, thus adding depth to the functional
paradigm which becomes a special way to perform logic programming.
132 of 472
Polymorphism
In a simpler way, one can fix an algebra and add variables for types,
along with a notion of substitution. This idea leads to polymorphic
types.
Not all the typing system are “good”: in fact, even for very poor
algebras, deciding whether a term is correctly typed can be an
undecidable statement. Thus, the interesting algebras are limited by
the computational power needed to deal with them.
133 of 472
Types
The simple theory of types limits type construction to just one binary
operation, →, and assumes to have a predefined set T of constant
types. Polymorphic systems will have also type variables, and more
sophisticated systems will have more operations and axioms on them.
134 of 472
Terms I
135 of 472
Terms II
136 of 472
Terms III
Example 8.4
For every type σ, the following is a typed term:
Iσ ≡ (λx : σ. x : σ) : σ → σ .
137 of 472
Substitution I
138 of 472
Substitution II
Lemma 8.6
1. In M : τ, replacing a term P : σ by Q : σ, leads to a term of type τ;
2. (α-conversion) If (λx : σ. M : τ) : σ → τ is a typed term, then so is
(λy : σ. M[(y : σ)/(x : σ)]) : σ → τ;
3. (β-reduction) If ((λx : σ. M : τ) : σ → τ)(N : σ) is a typed term, then
so is (M : τ)[(N : σ)/(x : σ)].
Proof.
Straightforward inductions on the structure of typed terms.
[Exercise] Develop the details of the proof.
140 of 472
The formal system II
141 of 472
The formal system III
The notions of redex, contraction, β-reduction, β-conversion and β-nf
are defined on typed terms exactly as in pure λ-calculus. It is routine
to prove that
■ If M : σ B N : τ, then σ ≡ τ;
β
■ M : σ B N : σ iff M : σ B N : σ;
β
■ M : σ = N : σ iff M : σ = N : σ.
β
[Exercise] Fill the proofs.
Note that all other properties of reduction and equality hold, with the
same proof as before. In particular, the Church-Rosser Theorem and
the uniqueness of normal forms are true.
(λx : σ. x : σ) : (σ → σ) 6= (λx : τ. x : τ) : (τ → τ) .
142 of 472
Normalizability I
Lemma 8.13
1. Each type τ can be written in a unique way in the form
τ1 → τ2 → · · · → τn → θ where n ≥ 0 and θ is atomic;
2. If M : τ with τ ≡ τ1 → . . . τn → θ , then M is SC iff, for all SC terms
Y1 : τ1 , . . . , Yn : τn , (M Y1 . . . Yn ) : θ is SN.
3. If X : τ is SC (or SN) and M ≡α N, then N is SC (or SN);
4. If X : σ → ρ is SC and Y : σ is SC, then so is (XY ) : ρ ;
5. If X : τ is SN, then so is every subterm of X ;
6. If M[N : ρ /x : ρ ] : τ is SC then so is M : τ.
Proof.
Evident from the definition of SC and SN.
145 of 472
Normalizability IV
Lemma 8.14
1. every (a X1 . . . Xn ) : τ, with a an atom and X1 , . . . , Xn all SN, is SC;
2. every atomic term a : τ is SC;
3. every SC term of type τ is SN.
Proof.
(2) is an instance of (1). We prove (1) and (3) by induction on τ:
■ τ is atomic: since X1 , . . . , Xn are SN, so is aX1 . . . Xn , thus it is SC;
■ τ ≡ ρ → σ: let Y : ρ be SC, so by induction hypothesis (IH), it is
SN. Moreover, for the same reason, (a X1 . . . Xn Y ) : σ is SC. Thus,
so is a X1 . . . Xn by definition of SC.
Let X : τ and let x : ρ 6∈ FV(X : τ). By IH, x : ρ is SC, so by the
previous lemma (X x) : σ is SC. Thus, by IH, (X x) : σ is also SN.
But, by the previous lemma, X is SN, as well.
146 of 472
Normalizability V
Lemma 8.15
If (M : σ)[N : ρ /x : ρ ] is SC, then so is (λx : ρ. M : σ)(N : ρ ), provided
that N : ρ is SC when x : ρ 6∈ FV(M : σ).
Proof. (i)
Let σ ≡ σ1 → · · · → σn → θ with θ atomic and let M1 : σ1 , Mn : σn be SC
terms. Since (M : σ)[N : ρ /x : ρ ] is SC, it follows that
((M[N/x]) M1 . . . Mn ) : θ
147 of 472
Normalizability VI
,→ Proof. (ii)
So, an infinite reduction of ((λx . M) N M1 . . . Mn ) : θ has the form
where M Bβ M 0 , N Bβ N 0 . etc.
But, from M Bβ M 0 and N Bβ N 0 , we get that M[N/x] Bβ M 0 [N 0 /x],
hence, we can construct the following reduction:
Lemma 8.16
For every typed term M : τ:
1. M : τ is SC;
2. For all x1 : ρ 1 , . . . , xn : ρ n , with n ≥ 1, and all SC terms
N1 : ρ 1 , . . . , Nn : ρ n such that none of the x1 , . . . , xi −1 variables occurs
free in Ni , the term M ∗ ≡ M[N1 /x1 ] . . . [Nn /xn ] is SC.
Proof. (i)
(1) is an instance of (2), where Ni ≡ xi , since every xi is SC by
Lemma 8.14.
The proof of (2) is by induction on the structure of M:
■ M ≡ xi and τ ≡ ρ i . Then M ∗ ≡ Ni , which is SC by assumption.
,→
149 of 472
Normalizability VIII
,→ Proof. (ii)
■ M is an atom distinct from x1 , . . . , xn . Then M ∗ ≡ M which is SC by
Lemma 8.14.
■ M ≡ M1 M2 . Then M ∗ ≡ M1∗ M2∗ . By induction hypothesis, M1∗ and
M2∗ are SC, and so is M ∗ by Lemma 8.13.
■ M ≡ (λx : ρ. M1 : σ) and τ ≡ ρ → σ. By Lemma 8.14, we can safely
assume that x does not occur free in any N1 , . . . , Nn , x1 , . . . , xn .
Then, M ∗ ≡ λx . M1∗ . Let N : ρ be SC, then
151 of 472
Representability II
Theorem 8.18
All extended polynomials are representable in λβ → and, vice versa,
the only representable functions Nk → N are extended polynomials.
[Proof not required]
152 of 472
References and Hints
This lecture is based on Chapter 10 of[HS].
The proof of strong normalizability of λβ → can be found in Appendix
3 of the same text.
Theorem 8.18 has been proved by H. Schwichtenberg in 1976: H.
Schwichtenberg, Definierbare Funktionen im λ-Kalkül mit Typen,
Archiv für Mathematische Logik, 17 (1976) 113-114. An exposition of
the result can be found also in A.S. Troelstra, H. Schwichtenberg,
Basic Proof Theory, 2nd edition, Cambridge University Press (2000).
ISBN: 0521779111
The techniques used to operate on reductions are more general and
not limited to λ-calculi. An account can be found in F. Baader and T.
Nipkow, Term Rewriting and All That, Cambridge University Press
(1999). ISBN: 0521779200
153 of 472
Fundamentals of Functional Programming
Lecture 9
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
We have studied a Church-style typed system, λβ →. We have seen
that it is decidable, since it has the strong normalizability property,
and that its expressive power is limited to a class of computable
functions, the extended polynomials.
155 of 472
Types
156 of 472
Terms and Formulae
157 of 472
Type assignment system I
158 of 472
Type assignment system II
Example 9.4
We want to prove that ` K : σ → τ → σ.
The derivation is almost immediate:
w : σ, z : τ ` w : σ
→I
w : σ ` λy . w : τ → σ
→I
` λx , y . x : σ → τ → σ
159 of 472
Basic properties I
Proof.
By induction on the proof Γ ` M : τ:
■ if M ≡ x and x : τ ∈ Γ, then M ≡ N and the conclusion is obvious;
■ if Γ ` P : σ → τ, Γ ` Q : σ and M ≡ P Q, then N ≡ P 0 Q 0 and P ≡α P 0 ,
Q ≡α Q 0 , so, by induction hypothesis, Γ ` P 0 : σ → τ and Γ ` Q 0 : σ,
thus Γ ` N : τ by (→ E );
■ if Γ, z : σ ` P[z/x] : ρ and M ≡ (λx . P), τ ≡ σ → ρ , then N ≡ (λy . P 0 ),
P[z/x] ≡α P 0 [z/y ], with z new in P , P 0 , Γ. By induction hypothesis,
Γ, z : σ ` P 0 [z/y ] : ρ , thus Γ ` N : τ by (→ I ).
160 of 472
Basic properties II
Proof.
By induction on the derivation.
161 of 472
Basic properties III
Proof.
By induction on the structure of M. Each step in the induction
corresponds to an application of the generation lemma. Notice that
Γ ⊆ Γ0 .
162 of 472
Basic properties IV
Proof.
(1) follows by induction on the derivation Γ ` M : σ.
(2) follows by induction on the generation of Γ, x : σ ` M : τ.
163 of 472
Basic properties V
∆ ` M : τ.
Proof.
All statements are proved by induction on the generation of
Γ ` M : τ.
164 of 472
Basic properties VI
Theorem 9.10 (Subject reduction)
If Γ ` M : σ and M Bβ M 0 , then Γ ` M 0 : σ.
Proof.
By induction on the length of the reduction, it suffices to prove the
one-step case. If M ≡α M 0 , then the result is evident by the
α-invariance lemma.
So, let M ≡ U[((λx . P) Q)/z] and M 0 ≡ U[(P[Q/x])/z] with z
occurring only once in U. By the generation lemma, it suffices to
prove the case Γ ` (λx . P) Q : τ implies Γ ` P[Q/x] : τ.
But, if Γ ` (λx . P) Q : τ, by the generation lemma, Γ ` λx . P : σ → τ
and Γ ` Q : σ. Applying the generation lemma again, we get
Γ, z : σ ` P[z/x] : τ where z is new. Thus, by the substitution lemma,
Γ ` P[z/x][Q/z] : τ. Since z is new in P, P[z/x][Q/z] ≡ P[Q/x].
165 of 472
Basic properties VII
Notice that terms having a type are not closed under expansion, in
fact ` S K : (τ → σ) → (τ → τ), ` λx , y . y : τ → (σ → σ) and
S K Bβ λx , y . y but, evidently, S K 6` τ → (σ → σ).
166 of 472
Comparison with λβ → I
The forgetful map, as the name suggests, forgets the type decorations
of a typed term.
167 of 472
Comparison with λβ → II
Theorem 9.12
Let M : τ ∈ ΛT and let Γ = C ∪ (x : σ) : (x : σ) ∈ V and x ∈ FV(M) ,
© ª
■
168 of 472
Strong normalizability
Proof.
Since Γ ` M : τ, we know that there is a term N in λβ → such that
|N : τ| ≡ M and N : τ. Thus, by the strong normalizability theorem in
λβ →, |N : τ| ≡ M is SN.
169 of 472
References and Hints
170 of 472
Fundamentals of Functional Programming
Lecture 10
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
In the previous lectures, the simple theory of types has been introduced
both in the Church-style and in the Curry-style. In the second case,
type variables were present, although their use has been very limited.
The idea of a term depending on a type and vice versa is quite easy to
understand: a function f : A → B maps all the elements in the set A to
some elements in the set B. Sometimes, we are interested in
considering the image of f , i.e., the set f (A), as a type. In this case,
we want to write functions like g ≡ (λx ∈ f (A). . . . ). Evidently the type
f (A) depends on the term A.
The systems we are about to introduce allow to mix terms and types
in a controlled way. So, a term may have different types, which are
related to each other, and a type assumes a deeper meaning than
mere classification.
173 of 472
The λ-cube I
Definition 10.1 (Pseudoterms)
Assume to have a denumerable set of variables V and a set of type
constants C . Then pseudoterms are inductively defined as follows:
■ Every variable x ∈ V is a pseudoterm, and FV(x) = { x };
■ Every constant c ∈ C is a pseudoterm, and FV(c) = ;;
■ If M and N are pseudoterms, then so is (M N), and
FV(M N) = FV(M) ∪ FV(N);
■ If x ∈ V and M and N are pseudoterms, then so is (λx : M . N), and
FV(λx : M . N) = (FV(M) ∪ FV(N)) \ { x };
■ If M and N are pseudoterms and x ∈ V is such that x 6∈ FV(M),
then (Πx : M . N) is a pseudoterm, and
FV(Πx : M . N) = (FV(M) ∪ FV(N)) \ { x }.
174 of 472
The λ-cube II
(λx : A. M) N B M[N/x] .
The system we will consider will have two special constants ∗ and ,
called sorts.
175 of 472
The λ-cube III
176 of 472
The λ-cube IV
Definition 10.4 (The λ-cube)
The eight systems in the λ-cube are formed according to the following
derivation rules:
■ (axiom):
` ∗:
■ (start) x 6∈ FV(Γ) ∪ FV(A) and s is a sort:
Γ ` A:s
Γ, x : A ` x : A
178 of 472
The λ-cube VI
,→ (The λ-cube)
■ (conversion) s is a sort:
Γ ` M :A A =β B Γ ` B :s
Γ ` M :B
■ (α-conversion):
Γ ` M :A M ≡α N
Γ ` N :A
A pseudoterm M is a term iff there are a context Γ and a pseudoterm
A such that Γ ` M : A. A pseudoterm A is a type iff there are a context
Γ and a pseudoterm M such that Γ ` M : A.
,→
179 of 472
The λ-cube VII
,→ (The λ-cube)
The product rule characterises the systems in the λ-cube. Specifically,
the eight systems are the following:
System R
λ→ (∗, ∗)
λ2 (∗, ∗) (, ∗)
λP (∗, ∗) (∗, )
λP2 (∗, ∗) (, ∗) (∗, )
λω (∗, ∗) (, )
λω (∗, ∗) (, ∗) (, )
λP ω (∗, ∗) (∗, ) (, )
λP ω = λC (∗, ∗) (, ∗) (∗, ) (, )
180 of 472
The λ-cube VIII
The systems in the λ-cube are usually represented as:
λω λC
|| xx
||| xxx
| x
|| xx
λ2 λP2
λω λP ω
||| xxx
| x
|| xx
|| xx
λ→ λP
(,O ∗)
( , )
u:
uu
uu
uu
where directions have the meaning uuu
u / (∗, )
181 of 472
The λ-cube IX
182 of 472
The λ-cube X
The system λω allows polymorphic recursive types. For example
u : ∗ ` (λf : (∗ → ∗). f (f u)) : (∗ → ∗) → ∗: the term
(λf : (∗ → ∗). f (f u)) takes a function f , regarded as a type
constructor, and produces a type as its output.
Proof.
Induction on the derivation.
Proof.
Induction on the derivation x1 : A1 , . . . , xn : An ` M : A.
Proof.
By induction on the derivation of Γ, x : A, ∆ ` M : B.
Proof.
By induction on the derivation Γ ` M : A.
Proof.
Induction on the derivation of the main term.
187 of 472
Elementary properties IV
Proof.
Suppose A is a term, then Γ ` A : M for some Γ and M. By induction
on Γ ` A : M, via the generation lemma, one proves that B 0 =β B
appears as the subject of a derived term.
188 of 472
Elementary properties V
Corollary 10.14
If Γ ` M : A and A Bβ A0 , then Γ ` M : A0 .
Proof.
By induction on the derivation Γ ` M : A, one proves that A ≡ s or
Γ ` A : s for some sort s. In the first case, we are done; in the second
case, apply the subject-reduction theorem to obtain Γ ` A0 : s, thus an
application of the conversion rule proves the corollary.
189 of 472
Elementary properties VI
Proof.
By induction on the structure of A.
190 of 472
Elementary properties VII
Theorem 10.16 (Strong normalization)
If x1 : A1 , . . . , xn : An ` M : B, then A1 , . . . , An , M and B are SN.
[Proof not required]
The proof is a major result. It suffices to prove the result for λC , but
SN of λC reduces to SN in λω, modulo a suitable translation of types
and terms. Nevertheless, proving SN of λω is still not elementary.
Corollary 10.17
In the λ-cube, type checking (Is Γ ` M : A correct?) and typability
(Find A and Γ such that Γ ` M : A) are decidable.
192 of 472
Fundamentals of Functional Programming
Lecture 11
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
194 of 472
Universal quantification I
Definition 11.1 (Universal quantifier)
The universal quantifier ∀ is an abbreviation for the product (Π)
operator.
195 of 472
Universal quantification II
A universally quantified formula obeys two inference rules, one to
introduce the quantification, the other to eliminate it:
Γ, p : A ` B Γ ` ∀x : A. B
∀I ∀E
Γ ` ∀x : A. B Γ ` B[t/x]
Γ, x : A ` M : B Γ ` (Πx : A. B) : ∗
Γ ` (λx : A. M) : (Πx : A. B)
we see that the logical rule is the same thing as the type rule, as far as
we check that the conclusion is, indeed, a formula.
196 of 472
Universal quantification III
In the previous rule, the term λx : A. M encodes the proof of
Πx : A. B ≡ ∀x : A. B, where M is the proof of B assuming x : A. Note
how abstraction in the proof-term takes care of modelling
eigenvariables in the proof.
Γ ` M : (Πx : A. B) Γ ` t :A
Γ ` M t : B[t/x]
F ≡ λu : ∗, v : ∗. (Πx : u . v ) .
The formation rule for implications says that, if A and B are formulae,
then so is A ⊃ B.
198 of 472
Implication II
Lemma 11.3
In the λ-cube it holds that
Γ ` A:∗ Γ ` B :∗
Γ ` A → B :∗
Proof. (i)
We have to prove that Γ ` F A B : ∗. By a double use of the
application rule and hypotheses, it reduces to Γ ` F : (Πy : ∗, z : ∗. ∗).
By the thinning lemma, it suffices to prove
` (λu : ∗, v : ∗. (Πx : u . v )) : (Πy : ∗, z : ∗. ∗).
But, applying twice the abstraction rule and the thinning lemma, this
goal reduces to prove u : ∗, v : ∗ ` (Πx : u . v ) : ∗, ` (Πz : ∗. ∗) : ∗ and
` (Πy : ∗, z : ∗. ∗) : ∗. Applying the product rule to the first goal, we see
immediately that it holds. ,→
199 of 472
Implication III
,→ Proof. (ii)
The second goal reduces to the axiom, after the application of the
product rule. The third goal reduces to the second goal after thinning
the application of the product rule.
Lemma 11.4
The following rules hold in the λ-cube, when x 6∈ FV(Γ ∪ { B }):
Γ ` M :A → B Γ ` N :A Γ, x : A ` M : B Γ ` A → B :∗
Γ ` M N :B Γ ` (λx : A. M) : A → B
Proof.
Standard. (These proofs, as the previous one, are essentially
mechanical and we will not develop them in the future).
The first lemma corresponds to the formation rule for implication,
while the second lemma derives the ⊃ I and ⊃ E rules.
200 of 472
Conjunction I
Definition 11.5 (Conjunction)
Let
■ ∧ ≡ λu : ∗, v : ∗. (Πw : ∗. (u → v → w ) → w );
■ D ≡ λu : ∗, v : ∗, x : u , y : v , w : ∗, z : (u → v → w ). z x y ;
■ fst ≡ λu : ∗, v : ∗, x : (∧ u v ). x u (λy : u , z : v . y );
■ snd ≡ λu : ∗, v : ∗, x : (∧ u v ). x v (λy : u , z : v . z).
We write A ∧ B for ∧ A B when A and B are intended as logical
formulae, and A × B when A and B are intended as types.
The ∨ combinator represents the logical disjunction, while inl, inr and
case are the corresponding proof-terms. They model the datatype
representing the disjoint union of pairs of proofs, along with the
standard injections.
204 of 472
Disjunction II
Lemma 11.9
In λ2 and stronger systems, the following facts are true:
■ u : ∗, v : ∗ ` inl u v : u → u ∨ v ;
■ u : ∗, v : ∗ ` inr u v : v → u ∨ v ;
■ u : ∗, v : ∗ ` case u v : u ∨ v → (Πw : ∗. (u → w ) → ((v → w ) → w )).
Proof.
Standard.
205 of 472
Disjunction III
Similarly, case represents the proof of disjunction elimination:
Γ ` A∨B Γ, x : A ` C Γ, x : B ` C
∨E
Γ`C
The following lemma shows that these detours are correct and they
are eliminated by reducing the proof-terms.
206 of 472
Disjunction IV
Lemma 11.10
The following facts are true in λ2 and stronger systems:
■ u : ∗, v : ∗, x : u , y : v , f : u → w , g : v → w ` case u v (inl u v x) w f g : w ;
■ u : ∗, v : ∗, x : u , y : v , f : u → w , g : v → w ` case u v (inr u v y ) w f g : w ;
■ case u v (inl u v x) w f g =β f x;
■ case u v (inr u v y ) w f g =β g y .
Proof.
Standard.
207 of 472
Falsity I
Lemma 11.12
In every system in the λ-cube,
■ ` ⊥ : ∗;
■ x : ⊥, u : ∗ ` x u : u.
Proof.
Standard.
Proof. (i)
Suppose there is such an M. Then, by the application rule, we can
derive u : ∗ ` M u : u and, since the systems in the λ-cube are strong
normalising, M u =β N for some N in β-nf. ,→
209 of 472
Falsity III
,→ Proof. (ii)
By the generation lemma, N cannot be an abstraction, so
N ≡ a N1 . . . Nn , where a is a constant or a variable,
u : ∗ ` a : (Πy1 : A1 , . . . , yn : An . ∗) and, for each i, u : ∗ ` Ni : Ai .
If a is a constant then a : (Πy1 : A1 , . . . , yn : An . ∗) must be an axiom,
which is impossible. Otherwise, if a is a variable, then a ≡ u and n = 0,
but the only possible type for u is ∗, not u.
This result shows that all the systems in the λ-cube are logically
consistent, i.e., they cannot prove the false proposition. This is the
syntactical counterpart of soundness, which says that any provable
proposition is true.
210 of 472
Existential quantification I
Definition 11.14 (Existential quantifier)
Let
■ Σ ≡ λu : ∗, v : u → ∗. (Πw : ∗. (Πx : u . v x → w ) → w );
■ D 0 ≡ λu : ∗, v : u → ∗, x : u , y : v x , w : ∗, z : (Πx : u . v x → w ). z x y ;
■ proj ≡ λu : ∗, v : u → ∗, w : ∗, z : (Πx : u . v x → w ), y : (Πx : u . v x). y w z.
We write ∃x : A. B as an abbreviation for Σ A (λx : A. B).
Lemma 11.15
In λ2 and stronger systems it holds that
■ u : ∗, v : u → ∗ ` (∃x : u . v x) : ∗;
■ u : ∗, v : u → ∗ ` D 0 u v : (∀t : u . v t ⊃ (∃x : u . v x));
■ u : ∗, v :u → ∗ ` proju v :(Πw : ∗. (∀y :u . v y → w ) ⊃ ((∃x :u . v x) ⊃ w )).
Proof.
Standard.
As usual, the statements encode the formation rule and the two
inference rules, as it is immediate to see.
212 of 472
Existential quantification III
Lemma 11.16
In λ2 and stronger systems, it holds that
■ u : ∗, v : u → ∗, w : ∗, x : u , y : v x , z : Πx : u . v x → w `
proj u v w x (D 0 u v x y ) : w ;
■ proj u v w x (D 0 u v x y ) =β z x y .
Proof.
Standard.
213 of 472
Equality I
Definition 11.17 (Equality)
The typed equality M =A N is defined to be Q A M N where
Q ≡ λu : ∗, x : u , y : u . (Πz : u → ∗. z x ⊃ z y ) .
Γ`t =s Γ ` B(t)
refl subst
`x =x Γ ` B(s)
214 of 472
Equality II
Lemma 11.18
In λ2 and stronger systems, it holds that
■ ` Q : Πu : ∗. u → u → ∗;
■ u : ∗, x : u ` (λz : u → ∗, w : z x . w ) : (x =u x);
■ u : ∗, x : u , y : u , m : (x =u y ), z : u → ∗, n : z x ` m z n : z y .
Proof.
Standard.
The first statement encodes the formation rule, while the second and
the third statements are the formalisation of the refl and subst
inference rules, respectively.
215 of 472
Expressivity I
The expressive power of the systems in the λ-cube is given by the
expressive power of the corresponding logical system. The logical
systems are intuitionistic, and precisely
216 of 472
Expressivity II
In λ2 and stronger systems, for the same argument, all the partial
recursive functions can be represented. On the other hand, since these
systems are subsystems of the pure λ-calculus, no other functions can
be represented. Thus, λ2 and stronger systems are Turing-complete.
217 of 472
References and Hints
The content of this lesson has been taken from [HS], Chapter 13G.
218 of 472
Fundamentals of Functional Programming
Lecture 12 — Intermezzo
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
This lesson, which concludes the first part of this course, wants to
introduce exceptions and their implementation in a functional
language.
Exceptions, as in Java, are used in a variety of ways, although they are
mainly employed for error reporting. This highly procedural feature
can be smoothly incorporated in a functional language by means of a
proper evaluation semantics.
What is surprising is that the semantics has a logical counterpart,
relating classical proofs with intuitionistic ones, in a way which is
“natural” with respect to the formulae-as-types interpretation of typed
programs.
220 of 472
Exceptions I
catch j in M;
throw j N
221 of 472
Exceptions II
222 of 472
Syntax I
The set of types is the same as for the simply typed λ-calculus except
for the requirement of a special atomic type, ⊥.
223 of 472
Syntax II
A (N) ≡ C (λd . N) ,
with d 6∈ FV(N).
225 of 472
Syntax IV
226 of 472
Semantics I
Lemma 12.4
Any closed term M is either a value or it can be written in a unique
way as M = E [R] where R is a β-redex.
Proof.
Choose R as the outermost-leftmost redex of M.
228 of 472
Semantics III
229 of 472
Semantics IV
This reduction relation allows to evaluate in a call-by-value every term,
even if containing a C constructor, yielding a value.
If the term M does not contain the C operator, it reduces in the usual
way, following β-reductions until it reaches a value: in this case the
cleanup rule is applied, yielding the result.
230 of 472
Catch and throw I
231 of 472
Catch and throw II
If there is no throw expression j N in the scope of a catch, the
corresponding term reduces as follows:
as expected.
If there is a throw j Nexpression, it reduces to N, as required.
P ≡ λx . catch e in
if x = 0 then throw e “error”
else 1000/x;
234 of 472
Logical interpretation III
235 of 472
Logical interpretation IV
Theorem 12.7
If M : α is a term in ΛC , then M has type (α∗ → ⊥) → ⊥ in the simple
theory of types.
[Proof not required]
236 of 472
Logical interpretation V
Corollary 12.9
The CPS-translation is an embedding of the implicational fragment of
classical propositional logic into the implicational fragment of
intuitionistic propositional logic.
Proof.
Evident from the theorem and the fact that ` A ↔ ¬¬A holds in the
classical fragment.
Corollary 12.10
The evaluation of every well-typed term M : A in ΛC is finite.
Proof.
Suppose there is an infinite reduction of M : A. Then, by the theorem,
there is an infinite reduction of M : A∗ , which is impossible, being
every simply-typed term SN.
237 of 472
Conclusion
As a result of the analysis, we have coded the exception mechanism in
a suitable extension of a functional language, defining an appropriate
reduction strategy. Then, we have interpreted the language in logical
terms, obtaining a bijective correspondence with the implicational
fragment of classical propositional logic. Finally, via the
CPS-translation, we have shown that the whole language can be
embedded into the simple theory of types, via its logical interpretation
as the implicational fragment of intuitionistic propositional logic. So,
as a side result, we have constructed an implementation of the
exception mechanism inside the simple theory of types.
The material in this lesson has been taken from T.G. Griffin, A
Formulae-as-Types Notion of Control, in Proceedings of the 17th
ACM SIGPLAN-SIGACT symposium on Principles of programming
languages, ACM Press, pp. 47–58 (1990).
In that paper, the interested reader may find an outline of the omitted
proof.
239 of 472
Fundamentals of Functional Programming
Lecture 13
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline I
In the second part of this course, we will study Category Theory.
242 of 472
Categories I
Definition 13.1 (Category)
A category C is a structure C = 〈O , A, dom, cod, ◦, id〉 such that
■ O is a collection of objects, denoted as Obj C;
■ A is a collection of arrows;
■ dom is an operation assigning to each arrow f an object dom f , its
domain;
■ cod is an operation assigning to each arrow f an object cod f , its
codomain;
■ ◦ is an operation, called composition, assigning to each pair of
arrows f and g such that cod f = dom g , an arrow g ◦ f such that
dom(g ◦ f ) = dom f and cod(g ◦ f ) = cod g ; Moreover, ◦ satisfies the
associative law: for any arrows f , g , h such that the following
composition is defined, h ◦ (g ◦ f ) = (h ◦ g ) ◦ f ;
,→
243 of 472
Categories II
,→ (Category)
■ id is an operation, called identity, assigning to each object P an
arrow idP such that dom(idP ) = P = cod(idP ); Moreover, idP
satisfies the identity law: for any arrow f with dom f = P and
cod f = Q, idQ ◦ f = f = f ◦ idP .
If P and Q are objects, we write Hom(P , Q) or C(P , Q) for the
collection of arrows whose domain is P and whose codomain is Q. We
write f : P → Q if f ∈ Hom(P , Q).
Definition 13.2
Given a category C = 〈O , A, dom, cod, ◦, id〉, we say that
■ C is small if O and A are sets;
■ C is locally small if, for every P , Q ∈ O, Hom(P , Q) is a set;
■ C is large otherwise.
244 of 472
Concrete categories I
Example 13.3 (Set)
The category Set has sets as objects and (total) functions between
them as arrows. Specifically, Set = 〈O , A, dom, cod, ◦, id〉 and
■ O is the proper class of all sets;
■ A is the proper class of f : D → C where f is a total function from
D, its domain, to C , its codomain;
■ ◦ is the usual composition of functions: given f : D → E and
g : E → C , g ◦ f : D → C is (g ◦ f )(x) = g (f (x)), for all x ∈ D;
■ idP : P → P is the identity on P, i.e., for all x ∈ P, idP (x) = x.
It is immediate to see that the associative law and the identity law
both hold.
Usually, arrows are defined along with their domain and codomain, as
we did. Also, most of the times, composition and identities are
obvious from the context; in these cases, it is customary to define the
category specifying only the objects and the arrows. Sometimes, when
also the arrows (objects) are clear from the context, just the objects
(arrows) are specified.
246 of 472
Concrete categories III
247 of 472
Concrete categories IV
248 of 472
Concrete Categories V
Example 13.7 (Mon)
The category Mon has all the monoids as objects and their
homomorphisms as arrows. Thus, an object of Mon has the form
〈M , ·M , eM 〉, where ·M is a binary operation on M, and eM ∈ M, such
that (i) ·M is associative and (ii) eM is the unit of ·M . An arrow
f : 〈M , ·M , eM 〉 → 〈N , ·N , eN 〉 of Mon is a function f : M → N preserving
the product and the unit, i.e., (i) f (x ·M y ) = f (x) ·N f (y ) for all
x , y ∈ M, and (ii) f (eM ) = eN .
Note that 0 and 1 are discrete categories. Note also that every set is a
discrete category whose objects are its elements
252 of 472
Abstract Categories III
253 of 472
Abstract Categories IV
254 of 472
Opposite category
256 of 472
Product category and subcategories II
257 of 472
Product category and subcategories III
258 of 472
References and Hints
259 of 472
Fundamentals of Functional Programming
Lecture 14
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
The definition of category is very flexible and it captures most of the
mathematical theories, especially the ones of interest for Computer
Science. Category Theory, at the most superficial level, provides a
uniform language to describe Mathematics, offering a unifying view of
its problems and techniques.
263 of 472
Monics and epics II
264 of 472
Monics and epics III
In Set, it is easy to verify that monomorphisms are exactly the
injective functions, and that epimorphisms are exactly the surjective
functions. [Exercise] Check it.
In this way, one can try to replicate the proofs involving the abstracted
concept in the categorical framework. This exercise allows to transport
the result in other categories, apparently unconnected.
So, this abstraction process leads to very general notions, which may
behave unexpectedly.
265 of 472
Monics and epics IV
Example 14.4
Both 〈Z, +, 0〉 and 〈N, +, 0〉 are objects in Mon. Consider
i : 〈N, +, 0〉 → 〈Z, +, 0〉
n 7→ n
267 of 472
Isomorphisms II
In general, Category Theory considers objects and arrows up to
isomorphisms, meaning that isomorphic objects are considered
indistinguishable.
268 of 472
Initial and terminal objects I
Dually,
Definition 14.8 (Terminal object)
Given a category C, 1 ∈ Obj C is terminal if, for every A ∈ Obj C, there
is a unique arrow !: A → 1.
269 of 472
Initial and terminal objects II
In Grp, any trivial group T containing just the unit, is the initial
object, as well as the terminal object. An object which is both initial
and terminal is said to be a zero object. Notice that, in Grp, this
phenomenon can be spelt out as 0 = 1!
270 of 472
Products and coproducts I
Definition 14.9 (Product)
Given a category C, the product of A, B ∈ Obj C is an object A × B,
together with two arrows π1 : A × B → A and π2 : A × B → B, its
projections, such that the diagram
π1 π2
Ao A×B /B
is universal, that is, for any object C and pairs of arrows f : C → A and
g : C → B, there is a unique arrow 〈f , g 〉 : C → A × B making the
following diagram to commute
C
y EE
f yyy
EE g
EE
yyy 〈f ,g 〉 EE
|y y E"
A π1 A × B π2 / B
o
271 of 472
Products and coproducts II
Definition 14.10 (Coproduct)
Given a category C, the coproduct of A, B ∈ Obj C is an object A + B,
together with two arrows i1 : A → A + B and i2 : B → A + B, its
injections, such that the following diagram is co-universal
i1
A / A + B o i2 B
273 of 472
Products and coproducts IV
Definition 14.11 (Product)
The product of a family {Ai }i ∈I of objects indexed by the set I is an
object Πi ∈I Ai and a family {πi : Πi ∈I Ai → Ai }i ∈I of arrows such that the
following diagram is universal:
Πi ∈I AFi
yy FF
πj
yy FFπk
yy FF
y FF
|yy "
Aj ··· Ak
275 of 472
Limits and colimits I
In the definitions of products we used the notion of universal
construction. Properly speaking, universal constructions are limits.
X@
~~ @ @@ fj
fi
~~~ @@
~ @
~
~
Di / Dj
g
276 of 472
Limits and colimits II
X0 A
k /X
AA ~~
AA ~
A
f 0 AA ~~~f
i ~
i
Di
277 of 472
Limits and colimits III
Example 14.14
Given A, B ∈ Obj C, consider the diagram D whose vertexes are A and
B, with no edges. The limit of D is, if it exists, the product A × B.
Similarly, the product ΠU, for U ⊆ Obj C, if it exists, is the limit of the
diagram D with U as the set of vertexes and no edges.
Example 14.15
Consider the empty diagram D. Hence, a cone for D is any object
and, thus, its limit is a terminal object, if it exists.
278 of 472
Limits and colimits IV
279 of 472
References and Hints
This lesson follows Chapter 1.2, 1.3, 1.4, 1.5, 1.6 and 1.9 of [Pierce2].
280 of 472
Fundamentals of Functional Programming
Lecture 15
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
282 of 472
Equalisers and coequalisers I
// B
f
A g
283 of 472
Equalisers and coequalisers II
Example 15.3
© ª
Let f , g : A → B in Set, and let X = x ∈ A: f (x) = g (x) . Then, the
inclusion e : X© → A is an equaliserª of f , g .
Also, let S = (f (x), g (x)): x ∈ A ⊆ A × B and let R be the minimal
equivalence relation containing S. Call [y ]R the equivalence class
containing y ∈ B. Then, the map fR : B → B/R given by b 7→ [b]R is
the coequaliser of f , g .
284 of 472
Equalisers and coequalisers III
As usual, equalisers and coequalisers are unique up to isomorphisms.
Lemma 15.4
Every equaliser is monic.
Proof.
Suppose i : E → A equalises f , g : A → B. Let i ◦ j = i ◦ l where
j , l : C → E and let h : C → A be h = i ◦ j. We have
f ◦ h = f ◦ i ◦ j = g ◦ i ◦ j = g ◦ h and so there is a unique k : C → E with
i ◦ k = h. But h = i ◦ j, so k must be j. However, i ◦ l = i ◦ j = h, so k = l ,
and j = l .
Corollary 15.5
Every coequaliser is epic.
285 of 472
Existence of limits I
Theorem 15.6 (Limit)
Let D be a diagram in a category C, with sets V of vertexes and E of
edges. If every V -indexed and every E -indexed family of objects in C
has a product and every parallel pair of arrows in C has an equaliser,
then D has a limit.
Proof. (i)
For any Di ∈ V and (e : Di → Dj ) ∈ E , consider the diagram:
Di o πi
ΠDi ∈V Di
MMM
MMMπj
e MMM
MMM
&
Dj o πe
Π(e : Di →Dj )∈E Dj
πe
/ Dj
,→
286 of 472
Existence of limits II
,→ Proof. (ii)
Since Π(e : Di →Dj )∈E Dj is a product, there is a unique
,→ Proof. (iii)
Let h : X → ΠDi ∈V Di be an equaliser of p , q, and call fi = πi ◦ h.
pX
p
fipppp
p
ppp h
xoppp fj
Di ΠDi ∈V Di
πi MMM
MMMπj
e q p MMM
MMM
&
Dj o Π(e : Di →Dj )∈E Dj / Dj
πe πe
= πe ◦ p ◦ h = πj ◦ h = fj , so fi : X → Di Di ∈V is cone for D. ,→
© ª
288 of 472
Existence of limits IV
,→ Proof. (iv)
Assume that fi 0 : X 0 → Di D ∈V is a cone for D. By the universal
© ª
i
property of products, there is a unique arrow h0 : X 0 → ΠDi ∈V Di such
that πi ◦ h0 = fi 0 for each Di ∈ V .
So, for any (e : Di → Dj ) ∈ E , πe ◦ p ◦ h0 = πj ◦ h0 = fj0 = e ◦ fi 0 = e ◦πi ◦ h0 =
= πe ◦ q ◦ h0 . Thus, the following diagram commutes:
fj0
X0 /D
rrr8 j
p ◦h0 q ◦h0 rrr
rrr
rrr π e
XO II
II fi
IIh
II
I$
k ΠDi ∈V Di
πi
/&8 Di
:
vvv
vv
vv 0
vv h fi0
X0
πi ◦ h0 = fi 0 = fi ◦ k 0 = πi ◦ h ◦ k 0
Corollary 15.7
A category with equalisers and arbitrary products has all limits.
Corollary 15.8
A category with equalisers and finite products has all finite limits.
291 of 472
Pullbacks and pushouts I
Dually,
Definition 15.10 (Pushout)
The pushout of a pair of arrows f : C → A and g : C → B is the colimit
of the diagram
g
Ao /B .
f
C
292 of 472
Pullbacks and pushouts II
Example 15.11
Let f : B → C in Set and let A ⊆ C . Then the following is a pullback:
f −1 (A)
⊆ /B
f|f −1 (A) f
A /C
⊆
293 of 472
Pullbacks and pushouts III
Example 15.12
In Set, the following is a pullback which defines intersection:
A∩B
⊆ /B
⊆ ⊆
A /C
⊆
Example 15.13
In Set, let f : A → C and let g : B → C . Then,
© ª
P = (a, b) ∈ A × B : f (a) = g (b)
π1 !
A /1
!
Example 15.15
In any category, if
X
e /A
e g
A /B
f
• /• /•
α β
• /• /•
■ If both the inner squares α and β are pullbacks, then so is the outer
rectangle;
■ If the β square and the outer rectangle are pullbacks, then so is the
α square.
296 of 472
Pullbacks and pushouts VI
Proof. (i)
First, notice that, in both cases, the diagram is commutative.
For (1), consider the following commutative diagram:
X / UUUUU
// UUUU
// UUfUU
UUUU
// UUUU
/ / U*/
g / P C B
//
// α β
/
D e /A /•
X@ P /( C i /B
@ @@
c
@
g @@ d
α j β
a /
D A /•
Proof.
Let h, k : X → P be such that g 0 ◦ h = g 0 ◦ k.
Then, g ◦ f 0 ◦ h = f ◦ g 0 ◦ h = f ◦ g 0 ◦ k = g ◦ f 0 ◦ k. But, g is mono, so
f 0 ◦ h = f 0 ◦ k.
Since the square is a pullback and g 0 ◦ h = g 0 ◦ k : X → A,
f 0 ◦ h = f 0 ◦ k : X → B, there is a unique l such that
g 0 ◦ h = g 0 ◦ k = g 0 ◦ l , thus h = k.
299 of 472
Pullbacks and pushouts IX
300 of 472
References and Hints
301 of 472
Fundamentals of Functional Programming
Lecture 16
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
303 of 472
Kernels I
Definition 16.1 (Kernel relation)
In Set, let f : A → B be a function. The kernel relation Rf associated
to f is defined as: x Rf y iff f (x) = f (y ).
π1 f
A /B
f
K /A
! f
0 /B
!
In Mon, Grp ©and Vect, as inª many other categories derived form
Algebra, K = x ∈ A: f (x) = e , where e is the unit of the algebraic
structure.
305 of 472
Exponentiation I
Definition 16.3 (Exponentiation)
A category C with all products has exponentiation if, for any
A, B ∈ Obj C, there is B A ∈ Obj C, the exponential object, and
ev : B A × A → B, the evaluation arrow, such that, for any C ∈ Obj C
and g : C × A → B, there is a unique h : C → B A making the following
diagram to commute:
BA ×
O A
F FF
FFev
FF
FF
#
〈h,idA 〉
w ;B
w ww
w
ww g
ww
C ×A
306 of 472
Exponentiation II
307 of 472
Exponentiation III
308 of 472
Exponentiation IV
Example 16.5
The category Set is Cartesian closed since it is finitely complete and
B A = Hom(A, B). It is also complete and co-complete.
Example 16.6
The category Grp is finitely complete, since it has all products and a
terminal object, but it does not admit exponentiation, thus is it not
Cartesian closed.
309 of 472
Exponentiation V
Theorem 16.7
A category C having a terminal object and all pullbacks is finitely
complete.
Proof. (i)
Considering the pullback
π2
A×B /B
π1 !
A /1
!
,→ Proof. (ii)
Let f , g : A → B, then 〈idA , f 〉, 〈idA , g 〉 : A → A × B. Forming their
pullback
p
E /A
q 〈idA ,g 〉
A 〈id ,f 〉/ A × B
A
311 of 472
Subobject classifiers I
Definition 16.8 (Subobject classifier)
In a category C with a terminal object, a subobject classifier is an
object Ω with an arrow > : 1 → Ω satisfying the Ω-axiom: for each
subobject f : A B, there is a unique χf : B → Ω, the characteristic
arrow, such that
A / /B
f
! χf
1 /Ω
>
is a pullback square
314 of 472
Subobject classifiers IV
Example 16.11
Set is a topos since it is Cartesian closed and its subobject classifier is
the set Ω = { 0, 1 } with > : 1 → Ω defined by >(x) = 1.
In fact, let A ⊆ B, thus i : A → B with i(x) = x:
A / /B
i
! χi
1 /Ω
>
½
1 if x ∈ A
with χi (x) = .
0 otherwise
R / / B ×A
r
〈fr ,idA 〉
²A / / ℘(A) × A
²
316 of 472
Power objects II
Example 16.13
In Set, ℘(A) =©{ U : U ⊆ A }, ²A is the relation x ∈ U with U ⊆ A, which
is the set ²A = (U , x) ∈ ℘(A) × A: x ∈ U , and ² is the canonical
ª
inclusion. © ª
In fact, given a relation R ⊆ B × A, we can define fR (x) = y ∈ A: x r y
since R can be thought to as x ∈A fR−1 (x), or, equivalently, as
S
R / / B ×A
h 〈fR ,idA 〉
²A / / ℘(A) × A
²
Theorem 16.14
A category E is a topos iff E is finitely complete and has power objects.
[Proof not required]
318 of 472
Slice categories I
B?
f /C
? ??
??
g ? h
A
319 of 472
Slice categories II
Example 16.16
Let C be a small discrete category with I = Obj C. Then Set/I is a
slice category corresponding to the bundles on I .
Let p : A → I be a function in Set, then the fibre of p on i ∈ I is the set
Ai = p −1 ({ i }), and the bundle of p on I is the set { Ai : i ∈ I }, i.e., the
set A partitioned by the inverse images of p. Thus, the slice category
Set/I is the category whose objects are partitioned sets and whose
arrows are partition-preserving functions.
320 of 472
References and Hints
321 of 472
Fundamentals of Functional Programming
Lecture 17 — Intermezzo
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
323 of 472
Categories I
Our first task is to define categories as elements of a datatype. We
adopt an ML-like syntax.
datatype (o,a)Cat = cat (a → o) × (a → o) ×
× (o → a) × (a → a → a);
We have used the keyword fun to indicate the solution in the first
variable of the equation in the λ-calculus behind the functional syntax.
So, fixing X , and considering dom a variable, the first definition is
equivalent to asking the solution of the equation
dom (cat X ) = π1 X
325 of 472
Categories III
326 of 472
FinSet
As an example, we can code FinSet, the category of finite sets as:
datatype o SetArrow = setarrow (o Set) × (o → o) × (o Set);
let
fun setdom (setarrow(x,f ,y )) = x;
fun setcod (setarrow(x,f ,y )) = y ;
fun setid (A : o Set) = setarrow(A, λx . x, A));
fun setcomp (setarrow(c,g ,d )) (setarrow(a,f ,b)) =
if b = c then setarrow(a,λx . g (f (x)),d )
else raise non-composable-pair;
in FinSet = cat(setdom, setcod, setid, setcomp);
Since limits are colimits in the opposite category, we can construct the
formers from the latters, or vice versa, as it is simpler to code.
where the first component is the initial object and the second
component is the family of arrows departing from the initial object to
the index, where the index ranges over all the objects in the category.
329 of 472
Initial object II
Since initial objects are always isomorphic, we can code this fact as a
function returning an isomorphism, i.e., a pair of arrows, one the
inverse of the other:
fun isoinitial(initialobject(A,univA ), initialobject(A0 ,univA0 )) =
(univA (A0 ), univA0 (A));
330 of 472
FinSet
In FinSet, the initial object is the empty set, and the arrow f : ; → A,
for every A, is the function nowhere defined.
331 of 472
Binary coproducts I
Similarly to initial objects, we can code binary coproducts:
datatype (o,a)CoproductCocone =
coproductcocone (o × a × a) × (o × a × a → a);
datatype (o,a)Coproduct =
coproduct (o × o → (o , a)CoproductCocone);
333 of 472
Diagrams and colimits I
334 of 472
Diagrams and colimits II
A cocone is represented as
datatype (o,a)Cocone = cocone o × (o,a)Diagram × (N → a);
335 of 472
Diagrams and colimits III
336 of 472
Calculating colimits I
337 of 472
Calculating colimits II
338 of 472
Calculating colimits III
The logic is as follows: if the diagram has no edges, its colimit is the
finite coproduct of its objects; otherwise, we obtain the colimit by a
construction (addedge) which adds an edge e to the colimit of the
diagram deprived of e.
339 of 472
Calculating colimits IV
where
nildiagram = diagram(graph(;, ;, nilfn, nilfn), nilfn, nilfn);
coapex (cocone(a,-,-)) = a;
340 of 472
Calculating colimits V
When the diagram D = diagram(graph(N,E ,s,t),fo,fa) is non-empty,
finitecoproduct operates as:
let { n } ∪ N1 = N;
colimcocone(c,uc) =
finitecoproduct (C ,init,bcoprod)
(diagram(graph(N1 ,E ,s,t),fo,fa));
coproductcocone((b,f ,g ), ucp) = bcoprod(coapex c,fo n);
resultcocone =
cocone(b,D,λm. if m = n then g
else compose(C )(f , sides c n);
universal = colimcocone(λc . let u = coapexarrow(u c);
v = ucp(coapex c,u,sides c n);
in coconearrow(resultcocone,v ,c))
in colimitingarrow(resultcocone,universal);
341 of 472
Calculating colimits VI
Apart the complexity of the details in constructing all the pieces of the
resulting cocone, its logic is clear: recursively constructing finite
coproducts as binary coproducts until there are no more nodes and
thus the colimit is the initial object.
342 of 472
Calculating colimits VII
The function addedge is defined as
fun addedge (C ,coeq) ((c,u),e) =
let diagram(graph(N,E ,s,t),fo,fa) = base c;
((b,h),ceu) =
coeq(sides c (s e), compose C (sides c (t e,fa e)));
resultdiagram = diagram(graph(N,{ e } ∪ E ,s,t),fo,fa);
resultcocone =
cocone(b,resultdiagram, λn. compose C (h,sides c n));
universal = λc1 . let w = coapexarrow (u c);
v = ceu (coapex c1 ,w );
in coconearrow(resultcocone,v ,c1 );
in (resultcocone,universal);
343 of 472
Calculating colimits VIII
344 of 472
References and Hints
345 of 472
Fundamentals of Functional Programming
Lecture 18
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
347 of 472
Functors I
348 of 472
Functors II
349 of 472
Functors III
350 of 472
Functors IV
351 of 472
Functors V
352 of 472
Functors VI
353 of 472
Functors VII
D / /A
h
f
C / g
/B
354 of 472
Functors VIII
355 of 472
Functors IX
356 of 472
Functors X
357 of 472
Functors XI
358 of 472
Natural transformations I
Definition 18.13 (Natural transformation)
Let C and D be categories and let F , G : C → D be functors. A natural
.
transformation α from F to G , notation α : F −→ G , is a family of
arrows α : F (A) → G (A) A∈Obj C , indexed by the objects of C such
© ª
that, for any f ∈ HomC (A, B), the following diagram commutes in D:
αA
F (A) / G (A)
F (f ) G (f )
F (B) / G (B)
αB
360 of 472
Natural transformations III
Theorem 18.16
A functor is part of an equivalence of categories iff it is full, faithful
and essentially surjective.
[Proof not required — It uses the Axiom of Choice]
361 of 472
Natural transformations IV
362 of 472
Natural transformations V
363 of 472
Natural transformations VI
Definition 18.19 (Horizontal composition)
Let C, D and E be categories, and let S , T : C → D and S 0 , T 0 : D → E
. .
be functors. If σ : S −→ T and τ : S 0 −→ T 0 are natural
transformations, then the following diagram commutes:
τS(A)
(S 0 ◦ S)(A) / (T 0 ◦ S)(A)
.
The horizontal composition (σ • τ): S 0 ◦ S −→ T 0 ◦ T is a natural
transformation defined as the diagonal of the above square:
(σ • τ)A = T 0 (τA ) ◦ τS(A) = τT (A) ◦ S 0 (σA ).
364 of 472
Natural transformations VII
365 of 472
Natural transformations VIII
Example 18.20 (Evaluation)
Let C be a category with exponentiation, and let A ∈ Obj C. Then
FA : C → C defined as FA (B) = B A × A for each B ∈ Obj C, and
FA (f ) = 〈(f ◦ −), idA 〉 for each f ∈ HomC (B , C ), is a functor.
.
Thus, ev : FA −→ IdC , the evaluation transformation, is a natural
transformation, since the following diagram commutes for every
g : C → B:
evC
FA (C ) = C A × A / C = IdC (C )
366 of 472
Natural transformations IX
367 of 472
Natural transformations X
Theorem 18.22
The category Cat is Cartesian closed.
Proof.
The category 1 is a terminal object in Cat; it has binary products, so
it has all the finite products, as well; also, it has equalisers, as it is
easy to verify. Exponentiation is given by the functor category.
368 of 472
Natural transformations XI
369 of 472
References and Hints
370 of 472
Fundamentals of Functional Programming
Lecture 19 — Intermezzo
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
372 of 472
Functors I
Functors, consisting as they do of two functions, one on objects, the
other on arrows, can be represented quite simply.
datatype (oA,aA,oB,aB)Functor =
functor (oA,aA)Cat × (oA → oB) × (aA → aB) × (oB,aB)Cat;
374 of 472
Natural transformations I
Natural transformations are easily coded as a datatype:
datatype (oA,aA,oB,aB)NatTransform =
nattransform (oA,aA,oB,aB)Functor ×
× (oA → aB) ×
× (oA,aA,oB,aB)Functor;
376 of 472
Natural transformations III
377 of 472
Natural transformations IV
378 of 472
A Different Application I
379 of 472
A Different Application II
380 of 472
Metrics I
381 of 472
Metrics II
Definition 19.4 (Complete lattice, bounded lattice)
Given a lattice 〈O , ≤〉, let U ⊆ O . Then U and U are, respectively,
W V
the lub and the glb of the elements in U, when they exist.
A lattice is complete if every subset U ⊆ O has a lub and a glb.
A lattice is bounded if there are two distinct elements > and ⊥ in O
such that ⊥ = O and > = O .
V W
by duality.
Lemma 19.5
A finite lattice is bounded and complete.
[Proof not required]
382 of 472
Metrics III
383 of 472
The problem
The problem we would like to solve is: given two metrics A and B
where some values are identified via e1 : E → A and e2 : E → B, we
want to find the most general metric, up to isomorphisms, containing
both A and B where the elements e1 (x) and e2 (x) are identified for
each x ∈ E .
We will prove that such a pushout does not always exist, and we will
provide a way to construct it whenever it is possible.
384 of 472
The solution I
Lemma 19.8
Met has an initial object and binary coproducts.
Proof.
Let 0 = 〈{ ⊥, > }, ≤〉 with ⊥ ≤ >. Then 0 is a metric and it is obviously
initial.
Also, let A and B be metrics and define C = 〈A t⊥,> B , ≤〉 where
A t⊥,> B is the disjoint union of A and B with tops and bottoms
identified, and the order is naturally defined as the union of the orders
on A and B. Then C is a metric and it is immediate to show that the
embedding jA : A C and jB : B C are its injections, forcing C to
be the coproduct of A and B.
385 of 472
The solution II
Lemma 19.9
In a category having initial objects, binary coproducts and
coequalisers, every pushout is the coequaliser of a coproduct.
Proof.
Immediate from colimit construction.
jA ◦e1
then P is the coequaliser of the diagram E // A + B .
jB ◦e2
386 of 472
The solution III
Lemma 19.10
Let U : Met → Set be the forgetful functor. Then every pushout
g
B /P o C of the diagram B o
f
A / C in Met yields a
pushout U(B) / U(P) o U(C ) of the diagram
U(B) o U(A) / U(C ) in Set
Proof.
Elementary calculation.
Hence, it suffices to show that Met does not have all the coequalisers
to prove that Met has not all the pushouts, that is, our initial problem
is unsolvable.
388 of 472
The solution V
Consider the diagram
jA ◦e1
//
E A+B
jB ◦e2
389 of 472
The solution VI
To construct a counterexample, take as E the lattice
>=
==
α= β
==
⊥
Also, take as A and B two copies of
>
c
@@@
a=
== b
d
⊥
390 of 472
The solution VII
Thus, A + B is
ll > RRRRRR
lll RR
lll
cA D cB
{ DD z DDD
{{ zz
aA A bA aB B bB
AA BB
||| |||
dA QQQ d
QQQ mmm B
QQ mmmmm
⊥
Let eY : E → Y , with Y either A or B, be
> when x =>
x =α
a when
Y
eY (x) =
bY when x =β
⊥ when x =⊥ .
391 of 472
The solution VIII
So, the pushout is fully determined as the order
>?
??
· PPP nn ·
Pn
nnnPPP
· PnPP nn ·
Pn
nnnPPP
· ?n? ·
?
⊥
which is not a lattice. Thus, there is no coequaliser for the diagram
jA ◦eA
//
E A+B
jB ◦eA
The content of the first part of this lesson has been taken from
[Rydeheard] Chapters 3 and 5.
393 of 472
Fundamentals of Functional Programming
Lecture 20
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
395 of 472
Adjunctions I
396 of 472
Adjunctions II
397 of 472
Adjunctions III
The situation is formalised by naturality of
θ A, B
HomD (F (A), B) / HomC (A, G (B))
−◦F (f ) −◦f
HomD (F (A0 ), B) / HomC (A0 , G (B))
θ A0 , B
The unit and the co-unit of an adjunction are strictly linked to each
other: their constructions from θA,B are symmetric, so we can focus
on unit, leaving the properties of co-unit to be derived by symmetry.
399 of 472
Adjunctions V
Lemma 20.3
Given an adjunction 〈F , G , θ〉, for each f ∈ HomD (F (A), B), it holds
that θA,B (f ) = G (f ) ◦ η A .
Proof. (i)
For each f ∈ HomD (F (A), B), the following diagram commutes, by
naturality of θ :
θA,F (A)
HomD (F (A), F (A)) / HomC (A, G (F (A)))
f ◦− G (f )◦−
HomD (F (A), B) / HomC (A, G (B))
θ A, B
,→
400 of 472
Adjunctions VI
,→ Proof. (ii)
Specialising it for idF (A) ∈ HomD (F (A), F (A)), we obtain
θA,F (A)
idF (A) / ηA
f ◦− G (f )◦−
f / θA,B (f ) = G (f ) ◦ η A
θA,B
401 of 472
Adjunctions VII
Lemma 20.4
Given an adjunction 〈F , G , θ〉, for each g ∈ HomC (A, G (B)), there is a
unique f ∈ HomD (F (A), B) such that g = G (f ) ◦ η A .
Proof.
Since θA,B is a bijection, to g must correspond a unique arrow
f ∈ HomD (F (A), B) such that g = θA,B (f ). So g = G (F ) ◦ η A by the
previous lemma.
402 of 472
Adjunctions VIII
Theorem 20.5
Let F : C → D and G : D → C be functors. If there are natural
. .
transformations η : IdC −→ G ◦ F and ² : F ◦ G −→ IdD such that, for
every f ∈ HomD (F (A), B), there is a unique g ∈ HomC (A, G (B)) such
that f = ²B ◦ F (g ), and for each g ∈ HomC (A, G (B)), there is a unique
f ∈ HomD (F (A), B) such that g = G (f ) ◦ η A , then 〈F , G , θ〉, with
θA,B (f ) = G (f ) ◦ η A , is an adjunction.
Proof.
Let τA,B (g ) = ²B ◦ F (g ) for every g ∈ HomC (A, G (B)). Then, it follows
that τA,B and θA,B are inverses to each other, so θA,B is an
isomorphism.
Also, it is immediate to show that it is natural in both A and B.
403 of 472
Adjunctions IX
Theorem 20.7
If F a G then G preserves limits, while F preserves colimits.
[Proof not required]
404 of 472
Adjunctions X
405 of 472
Adjunctions XI
It can be shown that the limit and the colimit of any type of diagram
in a category C arise, when they exist, from right and left adjoints of a
diagonal functor C → CJ , where J is a canonical category having the
“shape” of the diagram.
The unit for the left adjoint is the universal co-cone, while the co-unit
of the right adjoint is the universal cone.
HomC (C × A, B) ∼
= HomC (C , B A ) .
Thus, the right product functor (− × A) has a right adjoint, the functor
(−A ).
The converse also holds: if the right product functor has a right
adjoint F , then the category has exponentiation and F (B) is the
exponential object, for each B ∈ Obj C.
Note that the co-unit ²B is precisely the evaluation arrow.
407 of 472
Adjunctions XIII
Example 20.11
Consider the forgetful functor U : Mon → Set. Its left adjoint F exists
and it is the free monoid F (A) generated by the set A of elements.
Note that we should always check that the forgetful functor admits a
left adjoint: for example, the category of fields has an obvious
forgetful functor to Set, but it does not have a left adjoint. In fact,
there is no such a thing as a “free field” in Algebra.
408 of 472
Yoneda Lemma I
When we consider functors from a locally small category to Set, there
is a nice result that allows to characterise their natural
transformations. It is also very useful, as we will see.
θF ,A : Nat(Hom(A, −), F ) ∼
= F (A) .
Proof. (i)
.
For a given natural transformation α : Hom(A, −) −→ F , we define
θF ,A = αA (idA ). Moreover, given a ∈ F (A), for each B ∈ Obj C,
τ(a)B : HomC (A, B) → F (B) is defined as τ(a)B (f ) = F (f )(a), with
f ∈ HomC (A, B). ,→
409 of 472
Yoneda Lemma II
,→ Proof. (ii)
This class of mappings defines a natural transformation
.
τ(a): Hom(A, −) −→ F since, for every g ∈ HomC (B , C ) and
f ∈ HomC (A, B), (F (g ) ◦ τ(a)B )(f ) = F (g )(τ(a)B (f )) =
= F (g )(F (f )(a)) = (F (g ) ◦ F (f ))(a) = F (g ◦ f )(a) = τ(a)C (g ◦ f ) =
= τ(a)C (Hom(A, g )(f )) = (τ(a)C ◦ Hom(A, g ))(f ). In a diagram:
τ(a)B
HomC (A, B) / F (B)
Hom(A,g )=g ◦− F (g )
HomC (A, C ) / F (C )
τ(a)C
,→
410 of 472
Yoneda Lemma III
,→ Proof. (iii)
But, θF ,A and τ are inverses to each other. In fact, letting a ∈ F (A),
(θF ,A ◦ τ)(a) = θF ,A (τ(a)) = τ(a)A (idA ) = F (idA )(a) = idF (A) (a) = a.
.
Also, if α : Hom(A, −) −→ F and f ∈ HomC (A, B), (τ ◦ θF ,A )(αB (f )) =
= τ(θF ,A (α))B (f ) = τ(αA (idA ))B (f ) = F (f )(αA (idA )) =
= αB (Hom(A, f )(idA )) = αB (f ◦ idA ) = αB (f ).
Corollary 20.13
The bijections θF ,A of the Yoneda lemma are natural in A. Moreover,
if C is small, they are also natural in F .
[Proof not required]
411 of 472
Yoneda Lemma IV
412 of 472
Yoneda Lemma V
Lemma 20.15
The Yoneda functors are full and faithful.
Proof.
Direct consequence of the Yoneda lemma.
The importance of the Yoneda functors lies in the fact that, given any
small category C, we can “complete” it. In fact, the image of C
through Y is an isomorphic copy of C, with no extra arrows, being Y
full and faithful.
op
But the category SetC is a topos, so it has all the finite limits and
co-limits, as well as exponentials and a subobject classifier. So, we can
op
think to C as a full subcategory of SetC , and we can work in the
larger category; in this way, we are “adding” to C the finite categorical
constructions it may lack.
413 of 472
References and Hints
The Yoneda Lemma is taken from [MacLane], while its proof can be
found in F. Borceux, Handbook of Categorical Algebra I: Basic
Category Theory, Cambridge University Press (1994). ISBN:
978-0521061193
414 of 472
Fundamentals of Functional Programming
Lecture 21 — Intermezzo
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
416 of 472
Relational semantics I
417 of 472
Relational semantics II
Definition 21.1 (Predicate)
Given a set X , a predicate (over X ) is a statement taking a value true,
denoted by >, or a value false, denoted by ⊥. Equivalently, a predicate
p is a function from X to { >, ⊥ }.
We write P(X ) for the set of predicates on X and order it by
implication: for p , q ∈ P(X ),
© ª © ª
p→q iff x ∈ X : p(x) = > ⊆ x ∈ X : q(x) = > .
419 of 472
Weakest precondition I
Now, fix G and M, initial and final states, and consider a relation
R ∈ ℘(G × M) modelling a program P, possibly non-deterministic but
always terminating.
420 of 472
Weakest precondition II
The map wpR preservesª implication:
© ©
in fact, if Yª ⊆ Z then wpR (Y ) =
x ∈ X : ∃y ∈ Y . R(x , y ) ⊆ x ∈ X : ∃z ∈ Z . R(x , y ) = wpR (Z ).
Rτ (x , y ) ≡ ∀Y ∈ ℘(M). x ∈ τ(Y ) → y ∈ Y ,
Taking these ideas one step further, the program itself may be
modelled by a predicate transformer, namely its weakest precondition.
Thus, we can move backward and forward between the relational and
the predicate transformer semantics by means of the connection
R ⊆ Rτ iff τ → wpR .
422 of 472
Galois connections I
Bp ≤ q iff p ≤ Cq .
The map B is called the lower adjoint of C, and the map C is called
the upper adjoint of B.
423 of 472
Galois connections II
424 of 472
Galois connections III
HomQ (Bp , q) ∼
= HomP (p , Cq) ,
425 of 472
Galois connections IV
Lemma 21.3
Assume (B, C) is a Galois connection between ordered sets P and Q.
Let p , p1 , p2 ∈ P and q , q1 , q2 ∈ Q. Then
1. p ≤ C ◦ Bp and B ◦ Cq ≤ q;
2. p1 ≤ p2 implies Bp1 ≤ Bp2 and q1 ≤ q2 implies Cq1 ≤ Cq2 ;
3. Bp = B ◦ C ◦ Bp and Cq = C ◦ B ◦ Cq.
Conversely, a pair of maps B: P → Q and C: Q → P satisfying (1) and
(2) for all p , p1 , p2 ∈ P and for all q , q1 , q2 ∈ Q sets up a Galois
connection between P and Q.
Proof. (i)
For p ∈ P, we have Bp ≤ Bp by reflexivity so, being (B, C) a Galois
connection, p ≤ C ◦ Bp. Dually for B ◦ Cq ≤ q. This establishes (1). ,→
426 of 472
Galois connections V
,→ Proof. (ii)
For (2), consider p1 ≤ p2 , then p1 ≤ C ◦ Bp2 by (1) and transitivity,
which is equivalent to Bp1 ≤ Bp2 , being (B, C) a Galois connection.
Dually for q1 ≤ q2 .
For (3), from (1), p ≤ C ◦ Bp, we obtain Bp ≤ B ◦ C ◦ Bp by (2). But
(B, C) is a Galois connection, so C ◦ Bp ≤ C ◦ Bp, which holds by
reflexivity, implies B ◦ C ◦ Bp ≤ Bp. Thus, Bp = B ◦ C ◦ Bp by
symmetry. Dually for q.
Lastly, assume (1) and (2) hold universally. Let Bp ≤ q. By (2),
C ◦ Bp ≤ Cq, but p ≤ C ◦ Bp by (1), so p ≤ Cq by transitivity. The
reverse implication follows in the same way.
427 of 472
Galois connections VI
Definition 21.4
Let φ : P → Q be a map between ordered sets. We say that φ
preserves existing joins if whenever S exists in P for some S ⊆ P,
W
Lemma 21.5
Let (B, C) be a Galois connection between P and Q. Then B
preserves existing joins and C preserves existing meets.
Proof.
Since a join is a categorical colimit, namely the coproduct of the
objects in S, and a meet is a limit, namely the product of the objects
in S, then the left adjoint C preserves limits and the right adjoint B
preserves colimits when they exist, as for Theorem 20.7.
428 of 472
Refinement I
429 of 472
Refinement II
430 of 472
Refinement III
431 of 472
Refinement IV
The v relation denotes the idea of “is refined by”. It is clearly reflexive
and transitive, and by a suitable quotient of the specification space, it
becomes also anti-symmetric, when we identify specifications with the
same end result.
432 of 472
Refinement V
433 of 472
Refinement VI
434 of 472
References and Hints
435 of 472
Fundamentals of Functional Programming
Lecture 22
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
437 of 472
Typed λ-calculus I
So, our λ-calculus is the simple theory of types extended with explicit
product types.
438 of 472
Typed λ-calculus II
Definition 22.2 (Terms)
Given a class of types, a set V of typed variables such that there is a
denumerable quantity of them for each type, and a set F of typed
constants, terms are defined as
■ if x : A ∈ V then x : A and FV(x) = { x };
■ if f : A → B ∈ F and t : A is a term, then f (t) : B and
FV(f (t)) = FV(t);
■ ∗ : 1 is a term and FV(∗) = ;;
■ if s : A and t : B are terms, then 〈s , t 〉 : A × B is a term and
FV(〈s , t 〉) = FV(s) ∪ FV(t);
■ if t : A × B is a term then fst(t) : A and snd(t) : B are terms and
FV(fst(t)) = FV(snd(t)) = FV(t);
,→
439 of 472
Typed λ-calculus III
,→ (Terms)
■ if t : B is a term and x : A ∈ V then (λx : A. t) : A → B is a term and
FV(λx : A. t) = FV(t) \ { x };
■ if s : A → B and t : A are terms, then (s t) : B is a term and
FV(s t) = FV(s) ∪ FV(t).
440 of 472
Typed λ-calculus IV
441 of 472
Typed λ-calculus V
Definition 22.4 (Calculus)
The rules of inference of the λ-calculus are:
■
X .s = t
(subst1) ;
Y . s[r /x] = t[r /x]
■
(refl) ;
X .x = x
■
X .x = y
(sym) ;
X .y = x
■
X .x = y X .y = z
(trans) ;
X .x = z
,→
442 of 472
Typed λ-calculus VI
,→ (Calculus)
■
X .s = t
(subst2) ;
Y . r [s/x] = r [t/x]
■
(unit) ;
X . x =1 ∗
■
(fst) ;
X . fst(〈x , y 〉) = x
■
(snd) ;
X . snd(〈x , y 〉) = y
■
(pair) ;
X . 〈fst(z), snd(z)〉 = z
,→
443 of 472
Typed λ-calculus VI
,→ (Calculus)
■
(β) ,
X . (λy : A. s) t = s[t/y ]
where y 6∈ FV(t);
■
(η) ,
X . λy : A. t y = t
where y 6∈ FV(t);
■
X , y : A. s =B t
(λ) .
X . (λy : A. s) = (λy : A. t)
444 of 472
Interpretation I
Definition 22.5 (λ-structure)
Let C be a Cartesian closed category. A λ-structure M in C is defined
by
■ a function M : K → Obj C mapping the type constants to objects of
C;
■ a function M from F , the set of function symbols, to the arrows of
C such that M(f : A → B): M A → M B.
The function M : K → Obj C is extended to arbitrary types as
■ M 1 = 1C , the terminal object of C;
■ M(A × B) = M A ×C M B, the product of C;
■ M(A → B) = (M B)M A , the exponential object of C.
We omit subscripts, for clarity.
445 of 472
Interpretation II
Definition 22.6 (Interpretation)
If M is a λ-structure in a Cartesian closed category C, we assign to
each term in a context X . t : B an interpretation
[[X . t]]M : M A1 × · · · × M An → M B
(M B)M C × M C
NNN O
NNev
NNN
〈h,idM C 〉
NNN
&
M A×M C /D ;
0
[[X ,z:A.t ]]
,→
447 of 472
Interpretation IV
,→ (Interpretation)
■ if t ≡ t 0 t 00 then [[X . t]] = ev ◦〈[[X . t 0 ]], [[X . t 00 ]]〉.
Moreover, [[X . t =A t 0 ]] is the equaliser
[[X . t]]
//
[[X . t =A t 0 ]] / / MA MB .
[[X . t 0 ]]
448 of 472
Interpretation V
449 of 472
Soundness
Theorem 22.9 (Soundness)
If X . s =A t is derivable in a λ-theory T , then it is valid in all models
for T in every Cartesian closed category.
Proof.
We need to check that the rules of the λ-calculus preserve validity:
■ the axioms (refl), (sym), (unit) and the rule (trans) are obvious;
■ rules (subst1) and (subst2) are proved to preserve validity by a
trivial induction which shows that [[X : t[s/y ]] = [[Y . t]] ◦ [[X . s]];
■ rules (fst), (snd) and (pair) are straightforward from the definition
of interpretation and the properties of product;
■ rules (β) and (η) are straightforward from the definition of
interpretation and the properties of evaluation.
[Exercise] Fill the details.
450 of 472
Completeness I
Definition 22.10 (Syntactic category)
Let T be a λ-theory. We define a category CT as follows:
■ the objects of CT are the types of the language of T ;
■ the arrows of CT are equivalence classes [x : A. t] of terms in
contexts where [x . s] = [x . t] iff x . t = s is provable in T , or
[x . t] = [y .t[y /x]]. The substitution and equality rules ensure that
this definition does not depend on the choice of t;
■ the identity morphism is [x . x]: A → A;
■ composition is given by substitution: given [x . t]: A → B and
[y . s]: B → C , [y . s] ◦ [x . t] = [z .s[t/y ]].
Note that we do not need contexts with more that one variable,
having product types.
451 of 472
Completeness II
Lemma 22.11
The category CT is Cartesian closed.
Proof.
■ the terminal object is the type 1 and, for each object A, the unique
arrow A → 1 is [x : A. ∗];
■ the product of A and B is the type A × B, with projections
[z . fst(z)] and [z . snd(z)], and the morphism C → A × B induced by
[w . s]: C → A and [w . t]: C → B is [w . 〈s , t 〉];
■ the exponential B A is the type A → B, with evaluation map
(A → B) × A → B defined as [w . fst(w ) snd(w )], and, given any
[z . t]: C × A → B, its exponential transpose C → (A → B) is
[w . λx : A. t[〈w , x 〉/z]].
452 of 472
Completeness III
Proof. (i)
The structure MT sends types to themselves and each primitive
function symbol f : A → B to [x : A. f (x)]. By an easy induction we get
that [[x . t]]MT = [x .t]. Hence, the equalities in a context valid in MT
are exactly those provable in T . ,→
453 of 472
Completeness IV
,→ Proof. (ii)
Given a model N in D, the corresponding functor FN : CT → D sends A
to N A for each type A, and [x . t] to [[x . t]]N . It is clear that FN is a
Cartesian closed functor and that FN (MT ) = N.
In the opposite direction, since any Cartesian closed functor
F : CT → D must preserve interpretations of arbitrary terms in a
context, it is easily seen to be naturally isomorphic to FN where
N = F (MT ).
454 of 472
References and Hints
455 of 472
Fundamentals of Functional Programming
Lecture 23
Prof. M. Benini
[Link]@[Link]
[Link]
a.a. 2010/11
Outline
With respect to the typed λ-calculus, in the pure version we will follow
an algebraic approach instead of using a purely categorical
construction. This way is less abstract, but slightly more difficult, at
least from the technical point of view.
457 of 472
C-monoids I
The categorical structures that we will use as models, are called
C-monoids. Intuitively, they are Cartesian closed categories deprived of
the terminal object.
The intuition behind the definition is that 〈−, −〉, π1 and π2 define the
product with its projections, while ² stands for a pairing operation,
precisely ² = λz . 〈fst z , snd z 〉, and (−)∗ stands for functional
application, precisely h∗ = λx , y . h 〈x , y 〉.
459 of 472
C-monoids III
Definition 23.2 (Product and exponential)
In any C-monoid, a × b ≡ 〈a · π1 , b · π2 〉 and g f ≡ (g · ² · 〈π1 , f · π2 〉)∗ .
Proof. (i)
Define ρ x · φ(x) by induction on the structure of φ(x):
■ ρ x · k ≡ k · π2 if k is an element of M ;
■ ρ x · x ≡ ²;
■ ρ x · (ξ(x) · ψ(x)) ≡ ρ x · ξ(x) · 〈π1 , ρ x · ψ(x)〉;
■ ρ x · 〈ψ(x), ξ(x)〉 ≡ 〈ρ x · ψ(x), ρ x · ξ(x)〉;
■ ρ x · ψ(x)∗ ≡ (ρ x · ψ(x) · α)∗ , where α ≡ 〈π1 · π1 , 〈π1 · π1 , π2 〉〉.
First, we show that, if φ(x) = ψ(x), then ρ x · φ(x) = ρ x · ψ(x). ,→
461 of 472
C-monoids V
,→ Proof. (ii)
It suffices to prove the fact for the C-monoid axioms since = is the
smallest congruence relation satisfying them. But, if A = B is a
C-monoid axiom, it is easy to check that ρ x · A = ρ x · B.
Also, ρ x · φ(x) · 〈(x · π2 )∗ , 1〉 = φ(x) by direct calculation.
So, ρ x · φ(x) satisfies the statement of the Theorem.
Finally, suppose f · 〈(x · π2 )∗ , 1〉 = φ(x) for some constant f of M , then
ρ x · φ(x) = ρ x · f · 〈(x · π2 )∗ , 1〉 = f · π2 · 〈(x · π2 )∗ , 1〉 = f · 1 = f .
So ρ · φ(x) is unique, as required.
462 of 472
C-monoids VI
Corollary 23.6
If φ(x) is a polynomial in the variable x over a C-monoid M , then
there exists a unique g in M such that g • x = φ(x).
Proof.
Take g = λx . φ(x).
463 of 472
λ-calculus I
465 of 472
λ-calculus III
Lemma 23.9
Every C-monoid M gives rise to a λ-calculus L(M ).
Proof.
The constants of L(M ) are the elements of M, variables are chosen
from a denumerable set V , and the terms of L(M ) are constructed as
follows
■ fst(t) ≡ π1 · t and snd(t) ≡ π2 · t;
■ 〈a, b 〉 ≡ 〈λx . a, λx . b 〉 • 1, where x 6∈ FV(a) ∪ FV(b);
■ application and abstraction are defined as before.
Finally, a = b holds in L(M ) iff a = b holds in M [FV(a) ∪ FV(b)].
It is immediate to show that L(M ) is a λ-calculus from the properties
of C-monoids.
466 of 472
λ-calculus IV
Lemma 23.10
Every λ-calculus L gives rise to a C-monoid M(L ).
Proof. (i)
The elements of M(L ) are equivalence classes of equivalent closed
terms of L ,and
■ 1 ≡ λx . x;
■ g · f ≡ λx . g (f x);
■ π1 = λx . fst(x) and π2 = λx . snd(x);
■ 〈f , g 〉 ≡ λx . 〈f x , g x 〉;
■ ² ≡ λz . 〈fst(z), snd(z)〉;
■ h∗ ≡ λx , y . h 〈x , y 〉.
Moreover, a = b in M(L ) iff a = b holds in L . ,→
467 of 472
λ-calculus V
,→ Proof. (ii)
The axioms for a C-monoid as easily checked. For example:
² · 〈h∗ · π1 , π2 〉 · x = ² · 〈h∗ · π1 · x , π2 · x 〉 = 〈h∗ · fst(x), snd(x)〉 =
h · 〈fst(x), snd(x)〉 = h · x, so ² · 〈h∗ · π1 , π2 〉 = h.
The other axioms are left as exercises.
468 of 472
λ-calculus VI
Theorem 23.11
The maps M and L of the previous lemmas establish a one-to-one
correspondence between C-monoids M and λ-calculi L :
M ◦ L(M ) = M and L ◦ M(L ) = L .
[Proof not required]
469 of 472
λ-calculus VII
Definition 23.12 (CMon)
The category CMon has C-monoids as objects and C-homomorphisms
as arrows. A C-homomorphism is a monoid homomorphism preserving
the C-monoid structure.
Corollary 23.14
The category CMon is isomorphic to the category λ-Calc.
[Proof not required]
470 of 472
References and Hints
The material of this lecture is taken from [Lambek], Chapter I.15 and
I.17.
In that book, the interested reader may find the omitted proof.
471 of 472
Conclusion
This lecture is the last one in this course.
Who has been interested in the subjects developed in this course, may
consider to do her/his dissertation on them: the lecturer is doing
active research on some of these themes. If you just want to deepen
the subject, ask the lecturer: he may point you books, articles and
other references which may satisfy your interests.
The End
472 of 472