0% found this document useful (0 votes)
49 views

Announcements Announcements: Project 1 Is Graded

The document contains announcements and information about a computer science course. It discusses that Project 1 has been graded, with average and median scores provided. It also reminds students that Exam 2 will take place on November 1st and that practice problems will be posted. Upcoming topics that will be covered include functional programming with Scheme, recursive functions, scoping, lambda calculus, and let expressions. Students are also given guidance on how to write comments for Project 2.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Announcements Announcements: Project 1 Is Graded

The document contains announcements and information about a computer science course. It discusses that Project 1 has been graded, with average and median scores provided. It also reminds students that Exam 2 will take place on November 1st and that practice problems will be posted. Upcoming topics that will be covered include functional programming with Scheme, recursive functions, scoping, lambda calculus, and let expressions. Students are also given guidance on how to write comments for Project 2.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Announcements

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

Programming with lists and recursion Static and dynamic scoping

Scoping
n

Reminder: Exam 2 on November 1st


n

n n

Practice problems will be posted today off Announcement page

Attribute Grammars Scheme


n n n n

Recursive functions. Programming with lists and recursion Equality testing Higher order functions: map, foldl, foldr Scoping in Scheme
2

Fall 11 CSCI 4430, A Milanova/BG Ryder

Announcements
n

On Writing Comments for Project 2


Each function should have the following sections: ;; Contract: len : (list a) -> integer ;; Purpose: to compute length of a list l ;; Example: (len (1 (2 3) 4)) should return 3 ;; Definition: (define (len l) (if (null? l) 0 (+ 1 (len (cdr l)))))

Project 2
n

Functional correctness: 80%, comments: 20%

Due November 11th

On writing comments for Project 2


It will be very helpful to us (and to you) if you wrote comments in a stylized fashion n Style from How to Design Programs with some modifications by me
n

Fall 11 CSCI 4430, A Milanova/BG Ryder

Fall 11 CSCI 4430, A Milanova/BG Ryder

On Writing Comments for Project 2 (ii)


;; Contract: len : (list a) -> integer
n

On Writing Comments for Project 2 (iii)


;; Contract: ;; lcs_slow : (list a) * (list a) -> (list a) n lcs_slow is a function which consumes a list with elements of some type a, another list with elements of same type a, and returns a list with elements of type a ;; Purpose: to compute the longest common sublist of lists l1 and l2. It computes all sublists of one of the lists, and therefore, its complexity is O(2N)
Fall 11 CSCI 4430, A Milanova/BG Ryder 6

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

Fall 11 CSCI 4430, A Milanova/BG Ryder

Last class: Scheme


n

Functional Programming with Scheme


n

Recursive functions
n

Shallow and deep eq? vs. equal?

Equality testing
n

Functional Programming with Scheme


Reading: Scott, Chapter 10.1 - 10.5

Higher-Order Functions
n

map, foldr, foldl

Tail recursion

Fall 11 CSCI 4430, A Milanova/BG Ryder

Fall 11 CSCI 4430, A Milanova/BG Ryder

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

Functional Programming with Scheme


n n 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

Fall 11 CSCI 4430, A Milanova/BG Ryder

Fall 11 CSCI 4430, A Milanova/BG Ryder

Let Expressions: Examples


(let ((x 2)) (* x x)) yields 4 (let ((x 2)) (let ((y 1)) (+ x y) ) ) yields what? (let ((x 10) (y (* 2 x))) (* x y)) yields what? (let* ((x 10) (y (* 2 x))) (* x y)) yields what?

Let Expressions: Scope


(let ((x 10)) ;causes x to be bound to 10 (let ((f (lambda (a) (+ a x))) ;causes f to be bound to a lambda expression (let ((x 2)) (f 5) ) )) What does this expression yield?

(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

Scoping in Scheme: Two Choices


(let ((x 10)) (let ((f (lambda (a) (+ a x)))) (let ((x 2)) (* x (f 3) ) ) )

a is a bound variable

Scoping in Scheme: Two Choices (ii)


(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 }

x is a free variable; must be found in outer scope

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

A closure is a function value plus the environment in which it is to be evaluated


Function value: e.g., (lambda (x) (+ x y)) n Environment consists of bindings for variables not local to the function; thus, closure can eventually be evaluated: e.g., { y 2 }
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))

A closure can be used as a function


n n n

Applied to arguments Passed as an argument Returned as a value


15

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

Fall 11 CSCI 4430, A Milanova/BG Ryder

Fall 11 CSCI 4430, A Milanova/BG Ryder

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

Scoping, revisited (Scott, Ch. 3.6)


n

Scoping, revisited (ii)


n

Earlier, we considered the case when functions are third-class values (i.e., functions cannot be passed as arguments or returned from other functions)

Two choices for binding of non-local variables


Static scoping (early binding) or n Dynamic scoping (late binding) n Most languages choose static scoping. Why?
n n

Reference environment (i.e., closure bindings) is available on the stack. Function cannot outlive its referencing environment!
20

Fall 11 CSCI 4430, A Milanova/BG Ryder

19

Fall 11 CSCI 4430, A Milanova/BG Ryder

Functions as Third-Class and Static Scoping


program a, b, c: integer; procedure P c: integer; procedure S c, d: integer; procedure R end R; R(); end S; R(); S(); end P; procedure R a: integer; end R; ; P(); end program

Scoping, revisited (iii)


n

main ----a b c main.P c main.R a

Static Scoping: a bound to R.a, b to main.b, c to main.c

= 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

Fall 11 CSCI 4430, A Milanova/BG Ryder

Scoping, revisited (iv)


n

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

Thus, imperative languages typically disallow truly first-class function values

Fall 11 CSCI 4430, A Milanova/BG Ryder

Exercise: How does Python handle function values?

23

Fall 11 CSCI 4430, A Milanova/BG Ryder

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

Lambda () calculus expresses function definition and function application


n n n

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

Fall 11 CSCI 4430, A Milanova/BG Ryder

25

Fall 11 CSCI 4430, A Milanova/BG Ryder

Syntax of Pure Lambda Calculus


n

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

Too many parentheses! Parentheses may be dropped from (M N) or ( x.M )


n

E.g., (f x) may be written as f x

Function application groups from left-to-right (i.e., it is leftassociative)


n n

E.g., x y z abbreviates ((x y) z) Parentheses in x (y z) are necessary E.g., x. x z abbreviates x. (x z)

Application has higher precedence than abstraction


n

Variable: M ::= x Abstraction (or function definition): M ::= ( x.M ) Application: M ::= ( M1 M2 )
27

A sequence of consecutive abstractions can be written with a single lambda


n

E.g., xyz M abbreviates x.y.z M


28

Fall 11 CSCI 4430, A Milanova/BG Ryder

Fall 11 CSCI 4430, A Milanova/BG Ryder

Semantics
n

Currying
n

Semantics: the way of evaluating expressions


n

Defined by reduction rule: ( x.E ) M => E[M\x]


n n

In the lambda calculus, all functions have one argument


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 )

How do we express n-ary functions?

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

(x.y. x + y) 2 3 => (y. 2 + y) 3 => 2 + 3 = 5


Fall 11 CSCI 4430, A Milanova/BG Ryder 30

Currying, more formally


f(x1, x2,,xn) = g x1 x2 xn g1 x2
g2 x3

Free and Bound Variables


n

Reducing expressions is more complicated Consider expression ( x.y. x y ) (y w) Try 1:


Applying the reduction rule results in y. x y [(y w)\x] = ( y. (y w) y ) But what is wrong here?
n

n n

Function g is said to be a curried form of f.


(xy. x * y) 2 3 => (y. 2 * y) 3 => 2 * 3 = 6
Fall 11 CSCI 4430, A Milanova/BG Ryder 31

( x.y. x y ) (y w): different y s! If we substitute (y w) for x, the free y will become bound !
32

Fall 11 CSCI 4430, A Milanova/BG Ryder

Free and Bound Variables (ii)


n

Free and Bound Variables (iii)


n

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

free(x) = {x} free(M N) = free(M) U free(N) free(x.M) = free(M)-{x}


34

Fall 11 CSCI 4430, A Milanova/BG Ryder

33

Fall 11 CSCI 4430, A Milanova/BG Ryder

Free and Bound Variables: Examples


n

Substitution
n

What are the free variables in expressions


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)

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 into some fresh variable z. Proceed until case 1 applies, then proceed as in case 1.

Fall 11 CSCI 4430, A Milanova/BG Ryder

35

Fall 11 CSCI 4430, A Milanova/BG Ryder

36

Rules (Axioms) of Lambda Calculus


n

Rules of Lambda Calculus: Exercises


n

rule (-conversion): renaming (choice of parameter names doesn t matter)


x. E => z. E[z\x] provided that z is not free in E n e.g., x. 2*x is the same as z. 2*z
n

Use -conversion and -reduction to show

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)

rule (-reduction): function application (substitution of argument for parameter)


n n

(x. E) M => E[M\x] e.g., (x. 2*x) 4 = 2*4 = 4;


37

Notation: = is alpha, beta equality. The first expression reduces to the second expression through a sequence -conversions and -reductions.

Fall 11 CSCI 4430, A Milanova/BG Ryder

Fall 11 CSCI 4430, A Milanova/BG Ryder

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

Recall (xyz. x z (y z)) (u. u) (v. v)

Fall 11 CSCI 4430, A Milanova/BG Ryder

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

Evaluation Order (ii)


n

Evaluation Order: Examples


n

An evaluation order (or reduction strategy) is a rule for choosing redexes Applicative order reduction chooses the leftmost-innermost redex in an expression
n

Evaluate (x. x x) ( (y. y) (z. z) )

Using applicative order reduction

Also referred as call-by-value reduction


n

Normal order reduction chooses the leftmostoutermost redex in an expression


n

Using normal order reduction

Also referred as call-by-name reduction


41 Fall 11 CSCI 4430, A Milanova/BG Ryder 42

Fall 11 CSCI 4430, A Milanova/BG Ryder

Evaluation Order: Examples (ii)


n

Evaluation Order: Church-Rosser Theorem


n

(x. x*x) ((x. x+1) 2)


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

Applicative order what happens? Normal order what happens?

Another example: (x. x+x) ((z. 3+z) 2)


43 Fall 11 CSCI 4430, A Milanova/BG Ryder 44

Fall 11 CSCI 4430, A Milanova/BG Ryder

Evaluation Order: Church-Rosser Theorem (ii)


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

Church-Rosser Theorem, formally:


n

Fall 11 CSCI 4430, A Milanova/BG Ryder

Fall 11 CSCI 4430, A Milanova/BG Ryder

You might also like