Transcript ppt
Announcements
Exam 2 on Thursday, April 7th
Binding and scoping
Attribute grammars
Scheme
Lists, recursion, higher-order functions (especially map
and fold), tail recursion, let expressions, closures,
scoping
Lambda calculus and evaluation order
2 “cheat” pages
Practice problems off Announcements page
1
Last Class
Lambda Calculus
Introduction
Syntax and semantics
Free and bound variables
Spring 16 CSCI 4430, A Milanova
2
Today’s Lecture Outline
Lambda Calculus
Free and bound variables
Rules of lambda calculus
α-conversion
β-reduction
Evaluation order
Review for test
Spring 16 CSCI 4430, A Milanova
3
Lambda Calculus
Reading: Scott, Ch. 10.6 on CD
4
Lambda Calculus
A theory of functions
Theory behind functional programming
Turing-complete: any computable function can be
expressed and evaluated using the calculus
Vehicle for studying programming languages
Syntax of pure lambda calculus is amazingly
simple!
M x | ( x. M1 ) | ( M1 M2 )
Spring 16 CSCI 4430, A Milanova
5
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(M1 M2) = free(M1) U free(M2)
free(x.M) = free(M) - {x}
Spring 16 CSCI 4430, A Milanova
6
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. x) y
2. (z. z z) (x. x)
3. xyz. x z (y (u. u))
7
Free and Bound Variables
We must take free and bound variables into
account when reducing expressions
E.g., (x.y. x y) (y w)
First, rename bound y in y. x y to z: z. x z
(x.y. x y) (y w) => (x.z. x z) (y w)
Second, apply the reduction rule that substitutes
(y w) for x in the body ( z. x z )
( z. x z ) [(y w)\x] => ( z. (y w) z )
Spring 16 CSCI 4430, A Milanova
8
Substitution
Substitution E[M\x] 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.
Why do we need to rename bound y in E
when y is free in M?
9
Rules (Axioms) of Lambda Calculus
rule (alpha conversion): renaming of
parameter (choice of parameter name does
not matter)
x. M => z. M[z\x] provided that z is not free in
M
e.g., x. x x is the same as z. z z
rule (beta reduction): function application
(substitutes argument for parameter)
(x. E) M => E[M\x]
e.g., (x. x) z => z
Spring 16 CSCI 4430, A Milanova
10
One More Rule
η rule (eta reduction): eliminates redundant
lambda abstractions
x. F x where F is a function value, and x has no
free occurrences in F, then x. F x =>η F
e.g., x. (z. z) x is the same as z. z
Spring 16 CSCI 4430, A Milanova
11
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
that the expression on the left reduces to the expression on
the right, through a sequence -conversions and β-reductions.
Spring 16 CSCI 4430, A Milanova
12
Reductions
An expression ( x.E ) M is called a redex
(for reducible expression)
An expression is in normal form if it cannot
be β-reduced. Can you give examples?
Spring 16 CSCI 4430, A Milanova
13
Questions
Is z. z z in normal form?
Is (z. z z) (x. x) in normal form?
Answer: yes, it cannot be beta-reduced
Answer: no, it can be beta-reduced
xyz. x z (y (u. u))
Answer: it cannot be beta-reduced
Spring 16 CSCI 4430, A Milanova
14
Exercise
Use -conversion and β-reduction to show
(xyz. x z (y z)) (x. x) (x. x) (x. x) = (x. x)
Notation: = stands for “alpha, beta equality”. This means
that the expression on the left reduces to the expression on
the right through a sequence -conversions and β-reductions.
Spring 16 CSCI 4430, A Milanova
15
Evaluation Order
Let us look at (xyz. x z (y z)) (u. u) (v. v)
Actually, there are (at least) two “reduction paths”:
Path 1: (xyz. x z (y z)) (u. u) (v. v) =>β
(yz. (u. u) z (y z)) (v. v) =>β
(z. (u. u) z ((v. v) z)) =>β (z. z ((v. v) z)) =>β
(z. z z) = z. z z
Path 2: (xyz. x z (y z)) (u. u) (v. v) =>β
(yz. (u. u) z (y z)) (v. v) =>β
(yz. z (y z)) (v. v) =>β (z. z ((v. v) z)) =>β
(z. z z) = z. z z
Spring 16 CSCI 4430, A Milanova
16
Evaluation Order
An evaluation order (or reduction strategy) is
a rule for choosing redexes
Applicative order reduction chooses the
leftmost-innermost redex in an expression
Also referred to as call-by-value reduction
Normal order reduction chooses the leftmostoutermost redex in an expression
Also referred to as call-by-name reduction
Spring 16 CSCI 4430, A Milanova
17
Evaluation Order: Examples
Evaluate (x. x x) ( (y. y) (z. z) )
Using applicative order reduction
Using normal order reduction
What observations can you make?
Spring 16 CSCI 4430, A Milanova
18
Evaluation Order: Examples
(x. x*x) ((x. x+1) 2)
Applicative order:
(x. x*x) ((x. x+1) 2) =>β (x. x*x) (2+1) =
(x. x*x) 3 =>β 3*3 = 9
Informally, it fully evaluates arguments before evaluating the
function itself
Normal order:
(x. x*x) ((x. x+1) 2) =>β
((x. x+1) 2)*((x. x+1) 2) =>β 3*((x. x+1) 2) =>β
3*3 = 9
Informally, it evaluates the function before evaluating the function
arguments
Spring 16 CSCI 4430, A Milanova
19
Evaluation Order
In our examples, both orders produced the
same result. This is not always the case
First, look at expression (x. x x) (x. x x). What
happens when we apply β-reduction to this
expression?
Then look at expression (z.y) ((x. x x) (x. x x))
Applicative order – what happens?
Normal order – what happens?
Spring 16 CSCI 4430, A Milanova
20
Church-Rosser Theorem
Normal form implies that there are no more
reductions possible
Church-Rosser Theorem, informally
If normal form exists, then it is unique (i.e., result
of computation does not depend on the order
that reductions are applied; i.e., no expression
can have two distinct normal forms)
If normal form exists, then normal order will find it
Spring 16 CSCI 4430, A Milanova
21
Evaluation Order
Intuitively:
Applicative order (call-by-value) is an eager
evaluation strategy. Also known as strict
Normal order (call-by-name) is a lazy
evaluation strategy
What order of evaluation do most PLs use?
Spring 16 CSCI 4430, A Milanova
22
Exercises
Evaluate (x.y. x y) ((z. z) w)
Using applicative order reduction
Using normal order reduction
Let S = xyz. x z (y z) and let I = x. x
Evaluate S I I I
Using applicative order reduction
Using normal order reduction
Remember function application is leftassociative, S I I I stands for ((S I) I) I
23
Spring 16 CSCI 4430, A Milanova
24
Announcements
Exam 2 on Thursday, April 7th
Binding and scoping
Attribute grammars
Scheme
Lists, recursion, higher-order functions (especially map
and fold), tail recursion, let expressions, closures,
scoping
Lambda calculus and evaluation order
Practice problems off Announcements page
25
Exam 2 Practice (Quiz 4)
1. Under static scoping, where
does x in A bind?
Answer: global x
2. Under static scoping, what
gets printed?
Answer: 101
3. Under dynamic scoping
(with shallow binding), where
does x in A bind?
Answer: global x and B’s x
Spring 16 CSCI 4430, A Milanova
4. Under dynamic scoping
(with shallow binding), what
gets printed?
Answer: 0
26
Exam 2 Practice (Quiz 4)
Willy Wazoo wants to write a verifying compiler, which
(among other things) would guarantee that if a program
compiled successfully, then when it executed all loops would
terminate. Willy's compiler would be an example of
(a) static syntax analysis
(b) dynamic syntax analysis
(c) static semantic analysis
(d) dynamic semantic analysis
Spring 16 CSCI 4430, A Milanova
27
Exam 2 Practice (Quiz 6)
1. What gets printed under
dynamic scoping with shallow
binding?
Answer: 100
2. What gets printed under
dynamic scoping with deep
binding?
Answer: 101
Spring 16 CSCI 4430, A Milanova
28
Exam 2 Practice (Quiz 6)
What does f compute?
(define (f lis)
(foldl (lambda (x y) (if (> x y) x y)) lis (car lis))
)
Answer: max element in lis
Spring 16 CSCI 4430, A Milanova
29
Exam 2 Practice (Quiz 6)
Consider the problem of figuring whether two trees
(lists in Scheme) have the same fringe, that is, the
same leaves, in the same order, regardless of structure.
E.g., ((1 2) 3) and (1 (2 3)) have the same fringe.
What is the obvious way to solve this problem?
Answer: flatten then compare
Spring 16 CSCI 4430, A Milanova
30
Exam 2 Practice (Quiz 6)
Which let term produces the expected result, #t: let, let*, letrec
(____ ((is_even?
(lambda (n) (or (zero? n)
(is_odd? (- n 1)) )
)
)
(is_odd?
(lambda (n) (and (not (zero? n))
(is_even? (- n 1)) )
)
))
(is_even? 10)
)
Answer: letrec
31
Exam 2 Practice (Quiz 5)
Scheme’s scoping discipline is?
Scheme’s typing discipline is?
What does f do? Is f tail-recursive?
(define (f a b)
(cond ((= a b) a)
((> a b) (f (- a b) b))
(else (f a (- b a)))
)
)Spring 16 CSCI 4430, A Milanova
Answer: GCD, tail-recursive
32
Exam 2 Practice (Quiz 5)
(define (atom? obj)
(not (pair? obj))
)
Answer: 6
Spring 16 CSCI 4430, A Milanova
33
Attribute Grammars
Syntax of the Lambda calculus
Mx
M ( x. M )
M ( M M)
Give an attribute grammar that associates
attribute free with each parse tree node M
such that free contains the set of free
variables in the expression represented by M.
Is your grammar S-attributed?
Spring 16 CSCI 4430, A Milanova
34
Spring 16 CSCI 4430, A Milanova
35