Announcements Announcements: Project 1 Is Graded
Announcements Announcements: Project 1 Is Graded
Announcements
n
Project 1 is graded
n n
Exam 2
n
Avg. 78, Med. 83. There are 42 As. If you have questions, see me
Prolog
n
Scoping
n
n n
Recursive functions. Programming with lists and recursion Equality testing Higher order functions: map, foldl, foldr Scoping in Scheme
2
Announcements
n
Project 2
n
Has two parts. The first part, to the left of the colon, is the name of the function. The second part, to the right of the colon states what type of data it consumes and produces We shall use a, b, c, etc. to denote type parameters, and list to denote type list Thus, len is a function which consumes a list whose elements are of some type a, and produces an integer.
5
Recursive functions
n
Equality testing
n
Higher-Order Functions
n
Tail recursion
Lecture Outline
n
Let expressions
Let-expr ::= ( let ( Binding-list ) S-expr1 ) Let*-expr ::= ( let* ( Binding-list ) S-expr1 ) Binding-list ::= ( Var S-expr) { (Var S-expr) }
n
Binding with let and let* Scoping in Scheme, Closures Scoping, revisited Syntax and semantics Free and bound variables Rules of lambda calculus
n n
Lambda Calculus
n n n
-conversion -reduction
let and let* expressions define a binding between each Var and the S-expr value, which holds during execution of Sexpr1 let evaluates the S-expr s in parallel; let* evaluates them from left to right. Associate values with variables for a local computation Follow static scoping rules
10
Evaluation order
9
(define (f z) (let* ((x 5) (f (lambda (z) (* x z)))) (map f z))) What does this function do?
Fall 11 CSCI 4430, A Milanova/BG Ryder 11 Fall 11 CSCI 4430, A Milanova/BG Ryder 12
a is a bound variable
With static scoping will it evaluate to (* x (lambda (a)(+ a x) 3)) --> (* 2 ((lambda (a)(+ a 10) 3) ) --> ??? With dynamic scoping will evaluate to (* x (lambda (a)(+ a x) 3)) --> (* 2 ((lambda (a)(+ a 2) 3) ) --> ???
Fall 11 CSCI 4430, A Milanova/BG Ryder 13
Scheme chose static scoping: (* x (lambda (a)(+ a x) 3)) --> (* 2 ((lambda (a)(+ a 10) 3) ) --> 26
Fall 11 CSCI 4430, A Milanova/BG Ryder 14
Closures
n
Closures (ii)
(define (gg z) (let* ((x 2) (f (lambda(y) (+ x y)))) (map f z)))
n
gg is a closure:
n n
Function value: (lambda (z) (map f z)) Environment: { x 2; f (lambda (y) (+ x y)) }
> (gg
(1 2 3))
1. gg is evaluated to its function value: (lambda (z) (map f z)) 2. closure environment is expanded with parameter binding: { x 2; f (lambda (y) (+ x y)); z (1 2 3) } 3. evaluation occurs and (3 4 5) is returned
16
Closures (iii)
n
Closures (iv)
(let ((x 10)) (let ((f (lambda (a) (+ a x)))) (let ((x 2)) (* x (f 3) ) ) )
f is a closure: The function value: (lambda (a) (+ a x)) The environment: { x 10 }
Normally, when let exits, its bindings disappear Closure bindings (bindings part of a closure)
When let exits, bindings become inactive but they do not disappear n When closure is called, bindings become active n Closure bindings are immortal
n
(let ((x 5)) (let (( f (let ((x 10)) (lambda () x ) ) )) (list x (f) x (f)) ) )
Fall 11 CSCI 4430, A Milanova/BG Ryder 17
Scheme chose static scoping: (* x (lambda (a)(+ a x) 3)) --> (* 2 ((lambda (a)(+ a 10) 3) ) --> ???
Fall 11 CSCI 4430, A Milanova/BG Ryder 18
Earlier, we considered the case when functions are third-class values (i.e., functions cannot be passed as arguments or returned from other functions)
Reference environment (i.e., closure bindings) is available on the stack. Function cannot outlive its referencing environment!
20
19
= a, b, c;
Functions as first-class values n Static scoping is difficult. Function value may outlive static referencing environment! n Need immortal closure bindings n In languages that choose static scoping local variables must have unlimited extent (i.e., when stack frame is popped, local variables do not disappear)
22
21
In functional languages local variables typically have unlimited extent Imperative languages local variables have limited extent (i.e., when stack frame is popped local variables disappear)
n
Lambda Calculus
Reading: Scott, CD
23
24
Topics
n
Lambda Calculus
n
Lambda Calculus
n n n n
A theory of functions
Theory behind functional programming Turing-complete: any computable function can be expressed and evaluated using the calculus n Vehicle for studying programming languages
n n
Introduction Syntax and semantics Free and bound variables Rules of lambda calculus
n n
-conversion -reduction
Evaluation order
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
26
25
Syntactic Conventions
n
-calculus formulas (e.g., x. x y) are called expressions or terms We use notation f, x, y, z for variables; E, M, N, P, Q for expressions M ::= x | ( x.M ) | ( M1 M2 )
( x. (x z) ) corresponds to (lambda (x) (x z)) in Scheme n A -expression is one of
n
n n n
Variable: M ::= x Abstraction (or function definition): M ::= ( x.M ) Application: M ::= ( M1 M2 )
27
Semantics
n
Currying
n
Application of abstraction ( x.E ) on M results in E[M\x] Substitution of expression M for variable x in E is written as E[M\x] (some authors use {M/x}E )
E.g., (x. x*x) 2. Here E is x*x and M is 2. Result is x*x [2\x] => 2*2 = 4 n Expressions of the form ( x.E ) M called redex (reducible expression)
n
Fall 11 CSCI 4430, A Milanova/BG Ryder 29
Currying: n-ary function is expressed as n applications of unary functions f(x,y) = x+y, becomes (x.y. x + y)
n
n n
( x.y. x y ) (y w): different y s! If we substitute (y w) for x, the free y will become bound !
32
Try2:
Rename bound y in y. x y to z: z. x z (x.y. x y) (y w) => (x.z. x z) (y w) n Applying the reduction rule results in z. x z [(y w)\x]= ( z. (y w) z )
n
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
n n n
n n
33
Substitution
n
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 into some fresh variable z. Proceed until case 1 applies, then proceed as in case 1.
35
36
1. (xyz. x z (y z)) (x. x) (x. x) = z. z z 2. (xyz. x z (y z)) (x. x) (x. x) (x. x) = (x. x)
Notation: = is alpha, beta equality. The first expression reduces to the second expression through a sequence -conversions and -reductions.
38
Reductions
n
Evaluation Order
n
Recall that an expression ( x.E ) M is called a redex (for reducible expression) A reduction is any sequence of -conversions and -reductions. An expression is in normal form if it cannot be -reduced. Can you give examples?
39
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
n
Fall 11 CSCI 4430, A Milanova/BG Ryder
40
An evaluation order (or reduction strategy) is a rule for choosing redexes Applicative order reduction chooses the leftmost-innermost redex in an expression
n
Applicative order:
n
(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 (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
In our examples, both orders produced the same result. This is not always the case
n
Normal order:
n
Consider expression (x. x x) (x. x x) what happens when we apply -reduction to this expression? Consider expression (z.y) ((x. x x) (x. x x))
n n
Evaluation Order
n
Normal form implies that there are no more reductions possible Church-Rosser Theorem, informally
n
Intuitively: Applicative order (call-by-value) is an eager evaluation strategy Normal order (call-by-name) is a lazy evaluation strategy What order of evaluation do most PLs use?
46
Result of computation does not depend on the order that reductions are applied (i.e., if a normal form exists, it is unique) If there exists normal form, normal order will find it For all pure -expressions M, P and Q, if M =>* P and M =>* Q, then there must exist an expression R such that P =>* R and Q =>* R.
45