Transcript ppt

Announcements

HW6 due Monday, April 4th



Submission in HW Server is up
Questions?
We’ll have grades for HW4 in Rainbow
grades by tonight or tomorrow
Spring 16 CSCI 4430, A Milanova/BG Ryder
1
Announcements

Exam 2 on Thursday, April 7th



You are allowed 2 “cheat” pages only
Practice test on Announcements page
Topics



Binding and scoping
Attribute grammars
Scheme


Lists, recursion, higher-order functions (especially map
and fold), tail recursion, let expressions, scoping
Lambda calculus and evaluation order
Spring 16 CSCI 4430, A Milanova/BG Ryder
2
Last class

Scheme

Tail recursion


Binding with let and let* expressions
Scoping in Scheme, closures

Scoping, revisited
Spring 16 CSCI 4430, A Milanova/BG Ryder
3
Today’s Lecture Outline

Scoping, revisited

Lambda calculus




Introduction
Syntax and semantics
Free and bound variables
Rules of lambda calculus



α-conversion
β-reduction
Evaluation order
Spring 16 CSCI 4430, A Milanova/BG Ryder
4
Lambda Calculus
Reading: Scott, Ch. 10.6 on CD
5
Scoping, revisited (Scott, Ch. 3.6)
(let ((f (lambda (a) (+ x a)))) …
We discussed the two choices for binding of
non-local variables

Static scoping (early binding)
and
 Dynamic scoping (late binding)
 Most languages choose static scoping

Spring 16 CSCI 4430, A Milanova
6
Scoping, revisited


When we discussed scoping earlier, we
assumed that functions were third-class
values (i.e., functions cannot be passed as
arguments or returned from other functions)
When functions are third-class values…

When functions are third-class values, the
function’s static reference environment is
available on the stack! A function cannot outlive
its referencing environment.
Spring 16 CSCI 4430, A Milanova
7
Functions as Third-Class Values and Static
Scoping
program
a, b, c: integer;
procedure P
c: integer;
procedure S
d: integer;
procedure R
= a, c, d;
end R;
R();
end S;
R();
S();
end P;
procedure R
a: integer;
main
----a
b
c
main.P
Static Scoping:
a bound to R.a,
b to main.b,
c to main.c
c
main.R
= a, b, c;
end R;
…; P(); …
end program
a
8
Scoping, revisited

Functions as first-class values
 Static scoping is difficult. Function value
may outlive static referencing environment!
 Need “immortal” closure bindings
 In languages that choose static scoping,
local variables must have “unlimited
extent” (i.e., when stack frame is popped,
local variables do not disappear!)
Spring 16 CSCI 4430, A Milanova
9
Scoping, revisited

In functional languages local variables
typically have unlimited extent

In imperative languages local variables have
limited extent (i.e., when stack frame is
popped, local variables disappear)

Imperative languages typically disallow truly firstclass function values
Spring 16 CSCI 4430, A Milanova
10
More on Dynamic Scoping


Dynamic scoping with shallow binding and
Dynamic scoping with deep binding
Shallow binding


All examples of dynamic scoping we saw so far
used shallow binding
Reference environment for function/routine is not
created until the function is called


I.e., all non-local references are resolved using the
most-recent-frame-on-stack rule
Shallow binding is usually the default in
languages with dynamic scoping
11
More on Dynamic Scoping

Deep binding

When a function/routine is passed as an
argument, the code that passes the
function/routine has a particular reference
environment (the current one!) in mind. It passes
this reference environment along with the
function value (it passes a closure).
Spring 16 CSCI 4430, A Milanova
12
Exercise
v : integer := 10
people : database
print_routine (p : person)
if p.age > v
write_person(p)
other_routine (db : database, P : procedure)
v : integer := 5
foreach record r in db
P(r)
other_routine(people, print_routine)
/* call in main */
13
Exercise
(define A
(lambda ()
(let* ((x 2)
(C (lambda (P) (let ((x 4)) (P) )))
(D (lambda () x))
(B (lambda () (let ((x 3)) (C D)))))
(B))))
When we call (A) in the interpreter, what gets printed? What
would get printed if Scheme used dynamic scoping with
shallow binding? With deep binding?
14
Lecture Outline

Scoping, revisited

Lambda calculus




Introduction
Syntax and semantics
Free and bound variables
Rules of lambda calculus



α-conversion
β-reduction
Evaluation order
Spring 16 CSCI 4430, A Milanova/BG Ryder
15
Lambda Calculus

A theory of functions




Theory behind functional programming
Turing-complete: any computable function can be
expressed and evaluated using the calculus
Lambda calculus is the “lingua franca” of
programming language research
Lambda () calculus expresses function
definition and function application



f(x)=x*x becomes x. x*x
g(x)=x+1 becomes x. x+1
f(5)
becomes (x. x*x) 5 => 5*5 => 25
Spring 16 CSCI 4430, A Milanova/BG Ryder
16
Syntax of Pure Lambda Calculus


-calculus formulas (e.g., x. x y) are called
expressions or terms
M  x | ( x. M1 ) | ( M1 M2 )

A -expression is one of




Variable: M  x
Abstraction (or function definition): M  ( x. M1 )
Application: M  ( M1 M2 )
( x. (x y) ) corresponds to (lambda (x) (x y)) in
Scheme!
Spring 16 CSCI 4430, A Milanova/BG Ryder
17
Syntactic Conventions

Too many parentheses!

Parentheses may be dropped from (M N) or ( x.M )


Function application groups from left-to-right (i.e., it is leftassociative)



E.g., x y z abbreviates ((x y) z)
Parentheses in x (y z) are necessary! Why?
Application has higher precedence than abstraction


E.g., (f x) may be written as f x
E.g., x. x z abbreviates x. (x z)
A sequence of consecutive abstractions can be written with
a single lambda

E.g., xyz. M abbreviates x.y.z M
Spring 16 CSCI 4430, A Milanova/BG Ryder
18
Terminology

Parameter (also, formal parameter)


E.g., x is the parameter in x. (x z)
Argument (also, actual argument)
E.g., expression z. z is the argument in
(x. x) (z. z)
Can you guess what this evaluates to?

Spring 16 CSCI 4430, A Milanova/BG Ryder
19
Currying

In lambda calculus, all functions have one
parameter

How do we express n-ary functions?
Currying expresses an n-ary function with n
applications of unary functions
f(x,y) = x+y, becomes (x.y. x + y)

(x.y. x + y) 2 3 => (y. 2 + y) 3 => 2 + 3 = 5
Spring 16 CSCI 4430, A Milanova/BG Ryder
20
Currying in Scheme
(define curried-plus
(lambda (a) (lambda (b) (+ a b))))

(curried-plus 3) returns what?


Returns the plus-3 function (or more precisely, it
returns a closure)
((curried-plus 3) 2) returns what?

5
Spring 16 CSCI 4430, A Milanova/BG Ryder
21
Currying
f(x1, x2,…,xn) = g x1 x2 … xn
g1 x2
g2 x3
…
Function g is said to be the curried form of f.
(x.y. x + y) 3 2 => (y. 3 + y) 2 => 3 + 2 = 5
Spring 16 CSCI 4430, A Milanova/BG Ryder
22
Semantics of Lambda Calculus

An expression has as its meaning the value
that results after evaluation is carried out

(Somewhat informally) evaluation is the
process of reducing expressions
E.g., (x.y. x + y) 3 2 => (y. 3 + y) 2 => 3 + 2 = 5
Spring 16 CSCI 4430, A Milanova/BG Ryder
23
Free and Bound Variables



Reducing expressions
Consider expression ( x.y. x y ) (y w)
Try 1:
Reducing this expression results in the following
( y. x y ) [(y w)\x] = ( y. (y w) y )
The above notation means: we substitute argument (y w)
for every occurrence of parameter x in body ( y. x y ).
But what is wrong here?


( x.y. x y ) (y w): different y’s! If we substitute (y w)
for x, the “free” y will become “bound”!
24
Free and Bound Variables

Try 2:
Rename “bound” y in y. x y to z: z. x z
(x.y. x y) (y w) => (x.z. x z) (y w)
 Applying the reduction rule results in
( z. x z ) [(y w)\x] => ( z. (y w) z )

Spring 16 CSCI 4430, A Milanova/BG Ryder
25
Free and Bound Variables



Abstraction ( x. M ) is also referred as binding
Variable x is said to be bound in x. M
The set of free variables of M is the set of
variables that appear unbound in M



free(x) = {x}
free(M N) = free(M) U free(N)
free(x.M) = free(M) - {x}
Spring 16 CSCI 4430, A Milanova/BG Ryder
26
Free and Bound Variables
Intuitively, a variable x is bound if it is in the scope
of a lambda abstraction: as in x. M.
Variable is free otherwise.
1. (x. (y. (z.((x z) (y z)))))
2. y. z. x z ( y z )
3. x. ((x. x) y)
4. (x.y. x y) (y w)
5. (xyz. x z (y z)) (x. x) (x. x)
27
Substitution

Substitution E[M\x] (read: substitute every
occurrence of x with M in E) is defined as
follows:
1.
2.
If the free variables in M have no bound
occurrences in E, then replace all occurrences
of x in E by M.
Otherwise, suppose that y is free in M and
bound in E. Rename bound y in E into some
fresh variable z. Proceed until case 1 applies,
then proceed as in case 1.
Spring 16 CSCI 4430, A Milanova/BG Ryder
28
Rules (Axioms) of Lambda Calculus

 rule (-conversion): renaming (choice of
parameter name doesn’t matter)



x. M => z. M[z\x] provided that z is not free in
M
e.g., x. 2*x is the same as z. 2*z
 rule (-reduction): function application
(substitution of argument for parameter)


(x. E) M => E[M\x]
e.g., (x. 2*x) 4 = 2*4 = 8;
Spring 16 CSCI 4430, A Milanova/BG Ryder
29
Rules of Lambda Calculus: Exercises
Use -conversion and β-reduction to show
(x. x) y = ?

(x. x) (y. y) = ?
(xyz. x z (y z)) (u. u) (v. v) = z. z z
Notation: = stands for “alpha, beta equality”. This means
the expression on the left reduces to the expression on the
right, through a sequence of -conversions and β-reductions.
Spring 16 CSCI 4430, A Milanova/BG Ryder
30