Upload
alfred-marshall
View
216
Download
1
Embed Size (px)
Citation preview
Types and Programming Languages
Lecture 6
Simon GayDepartment of Computing Science
University of Glasgow
2006/07
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 2
Lambda Calculus
Lambda calculus is a notation for functions, and can be viewedas a core functional programming language, either untyped(initially) or typed (later).
Syntax
Terms e are defined by this grammar, assuming a supply ofvariables x, y, z, … :
e ::= x (variable) | x.e (abstraction) | e e (application)
Convention: has the lowest priority.
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 3
Lambda Calculus
Examples
x.x is a function which returns its parameter
x.y.xy is a function which, given a parameter x, returns a function which, given a parameter y, returns the result of applying x to y.
To obtain more concrete examples, we can combine -notationwith the syntax of SEL:
x.x+1 x.y.x+y
x.y.if x==1 then y else y+1
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 4
Scope and Variable Binding
x declares a new variable x with its own scope. Occurrences ofx within this scope are said to be bound. Variables which are notbound are said to be free.
x.y+x
boundfree
The name of a bound variable is not important as long as it is thesame as the variable attached to the corresponding .
x.y+x is equivalent to z.y+z
Bound variables can be renamed as long as free variables arenot captured.
x.y+x is different from y.y+y
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 5
Scope and Variable Binding
Renaming bound variables is called alpha-conversion and termswhich differ only in the names of bound variables are said to bealpha-equivalent.
It is often convenient to assume that a term has beenalpha-converted so that the names of all bound variables aredifferent from each other and different from the names of allfree variables.
Variables are bound by the structurally nearest matching .
x.(y.x.xy)x is alpha-equivalent to x.(y.z.zy)x
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 6
Variable Binding
Variable binding is a fundamental concept which occurs in manyplaces:
1002dxx
n
ii1
2
and of course in any programming language:
int f(int x) { return x+1;}
{ int x = 1; { int y; y = x + 1; }}
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 7
Substitution
To define the operational semantics of the lambda calculus weneed to define substitution, in a similar way to SFL.
exex ][
xzex ][ if x and z are different variables
])[])([(])[( 3231321 xeexeexeee
])[.(])[.( 11 xeeyxeey if y is not free in e
In general it may be necessary to rename bound variables toavoid variable capture during substitution:
yyyxyyxy .?])[.( WRONG!
zyzxyzxz .])[.( CORRECT
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 8
Lambda Calculus: Operational Semantics
The reduction rule for function application:
]'[').( xeeeex
is similar to the rule in SFL. It is known as beta-reduction.
Example
(x.x+1)2 (x+1)[2/x] = 2+1 3
if we include the syntax and reduction rules of SEL.
(x.y.x+y)2 (y.x+y)[2/x] = y.2+y
an example of a function which returns a function.
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 9
Lambda Calculus: Reduction Strategies
Different reduction strategies are based on where in a term it ispossible to apply beta reduction.
2121
11''eeee
ee
We will concentrate on call by value reduction, which meansadding the following rules:
''veveee
Values v are -abstractions, and any other values which wemight incorporate from SEL.
Other reduction strategies are possible (e.g. call by name), butthis has little effect on typing (which is what we are interested in).
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 10
Lambda Calculus: what I’m not telling you
Lambda calculus is powerful enough to be a general-purposeprogramming language: any computation can be expressed.
Data can be encoded as functions, for example:
true x.y.x
false x.y.y
if c then t else e (c t) e
Non-terminating computation is possible, for example:
(x.xx)(x.xx) (x.xx)(x.xx)
and recursive functions can be encoded.
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 11
Lambda Calculus + Expressions
As a step towards a better functional language (BFL) we cancombine -calculus and SEL.
v ::= integer literal | true | false | x.ee ::= v | x | e + e | e == e | e & e | if e then e else e | ee
Syntax
“x” stands for any variable
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 12
Lambda Calculus + Expressions
Reductions
The rules for SEL, and the following extra rules:
2121
11''eeee
ee
''veveee
][).( xvevex (R-AppAbs) note: applied to a value
(R-AppL) (R-AppR)
Remember that a -abstraction is a value.
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 13
Do we have a Better Functional Language?
We have functions, e.g. x.x+2 is a function, but can we reallydo functional programming?
It’s essential to be able to define recursive functions, but ourlanguage has no direct support for recursive definitions. We haveno way of associating a name with a function.
It is possible to express nameless recursive functions in-calculus, and it’s interesting to see how this works.
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 14
Recursion in -calculus
Fixed points
If f is a function then a fixed point of f is a value v such that fv = v.
Fixed point combinators
It is possible to find a -term Y with the curious property thatfor any term f, Yf is a fixed point of f. In other words, f(Yf) = Yf.
For example, define Y = y.(x.y(xx))(x.y(xx))
Yf = (y.(x.y(xx))(x.y(xx)))f (x.f(xx))(x.f(xx)) f((x.f(xx))(x.f(xx))) = f(Yf)
(we haven’t said what ‘=’ means, but it should include reduction)
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 15
Recursion in -calculus
Recursive definitions and fixed points
Here is a recursive function which we would like to define, butcan’t because we have no mechanism for naming functions:
f = x.if x==0 then 1 else x*f(x-1) (1)
If we abstract on f we get the following, which we call F just forconvenience:
F = f.x.if x==0 then 1 else x*f(x-1)
Now imagine that g is a fixed point of F:
g = Fg x.if x==0 then 1 else x*g(x-1))
which strongly suggests that the meaning of the recursivedefinition (1) is given by a fixed point of F.
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 16
Recursion in -calculus
Conclusion
Given a recursive function definition such asf = x.if x==0 then 1 else x*f(x-1)
we can construct the corresponding functional:F = f.x.if x==0 then 1 else x*f(x-1)
whose fixed point is the function we want. This fixed point can beexpressed as YF and written as a -term without naming.
Example
YF = (y.(x.y(xx))(x.y(xx)))(f.x.if x==0 then 1 else x*f(x-1))
(YF)3 * 6
2006/07 Types and Programming Languages Lecture 6 - Simon Gay 17
Reading
Pierce: 5
Exercises
Pierce: 5.2.1, 5.3.3, 5.3.7, 5.3.8