Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements Exam 2 on Friday, November 2nd Topics

Similar presentations


Presentation on theme: "Announcements Exam 2 on Friday, November 2nd Topics"— Presentation transcript:

1 Announcements Exam 2 on Friday, November 2nd Topics
You are allowed 2 “cheat” pages only Practice test under Course Materials on Submitty New rule: DO NOT LEAVE YOUR SEAT during last half hour 3:30 – 4pm Topics Binding and scoping Attribute grammars Scheme: lists, recursion, higher-order functions (especially map and fold), tail recursion, let expressions, scoping Lambda calculus

2 Announcements HW8 is posted, due Tuesday November 6th
Some lambda calculus questions Use as additional practice problems for test! Fall 18 CSCI 4430, A Milanova

3 Last Class Lambda calculus Introduction Syntax and semantics
Free and bound variables Substitution Fall 18 CSCI 4430, A Milanova/BG Ryder

4 Lambda Calculus Reading: Scott, Ch. 11 on CD

5 Today’s Lecture Outline
Lambda calculus, continued Substitution, review Rules of the lambda calculus Normal forms Reduction strategies Review for test Fall 18 CSCI 4430, A Milanova

6 Syntax of Pure Lambda Calculus
Convention: notation f, x, y, z for variables; E, M, N, P, Q for expressions E ::= x | ( x. E1 ) | ( E1 E2 ) A -expression is one of Variable: x Abstraction (i.e., function definition): x. E1 Application: E1 E2 -calculus formulae (e.g., ( x. (x y) )) are called expressions or terms ( x. (x y) ) corresponds to (lambda (x) (x y)) in Scheme! I use notation f, x, y, z for variables; E, M, N, P, Q for expressions Fall 18 CSCI 4430, A Milanova/BG Ryder

7 Syntactic Conventions
May drop parenthesis from ( E1 E2 ) or ( x. E ) E.g., ( f x ) may be written as f x Function application is left-associative I.e., it groups from left-to-right E.g., x y z abbreviates ( ( x y ) z ) E.g., E1 E2 E3 E4 abbreviates ( ( ( E1 E2 ) E3 ) E4 ) Application has higher precedence than abstraction Another way to say this is that the scope of the dot extends as far to the right as possible E.g., x. x y = x. ( x y ) = ( x. ( x y ) ) ≠ ( ( x. x ) y )

8 Free and Bound Variables
Abstraction ( x. E ) introduces a “binding” Variable x is said to be bound in x. E The set of free variables of E is the set of variables that are unbound in E Defined by cases on E Var x: App E1 E2: Abs x.E: free(x) = {x} free(E1 E2) = free(E1) U free(E2) free(x.E) = free(E) - {x} Fall 18 CSCI 4430, A Milanova

9 Free and Bound Variables
A variable x is bound if it is in the scope of a lambda abstraction: as in x. E Variable is free otherwise 1. (x. x) y 2. (z. z z) (x. x) 3. x.y.z. x z (y (u. u)) y is free. No free variables Fall 18 CSCI 4430, A Milanova

10 Substitution, formally
(x. E) M  E[M/x] replaces all free occurrences of x in E by M E[M/x] is defined by cases on E: Var: y[M/x] = y[M/x] = App: (E1 E2)[M/x] = Abs: (y. E1)[M/x] = (y. E1)[M/x] = M if x = y y otherwise (E1[M/x] E2[M/x]) y. E1 if x = y z. ((E1[z/y])[M/x]) otherwise, where z NOT in free(E1) U free(M) U {x} Fall 18 CSCI 4430, A Milanova

11 Substitution, formally
(x.y. x y) (y w) (y. x y)[(y w)/x] 1_. ( ((x y)[1_/y])[(y w)/x] ) 1_. ( (x 1_)[(y w)/x] ) 1_. ( (y w) 1_ ) 1_. y w 1_ /* same as z. y w z */ You will be implementing this exact substitution algorithm in Haskell! 1_ is a “fresh” variable. It is not free in either (x y), (y w) or x! Fall 18 CSCI 4430, A Milanova/BG Ryder

12 Rules (Axioms) of Lambda Calculus
 rule (-conversion): renaming of parameter (choice of parameter name does not matter) x.E  z.(E[z/x]) provided that z is not free in E e.g., x. x x is the same as z. z z  rule (-reduction): function application (substitutes argument for parameter) (x. E) M  E[M/x] Note: E[M/x] as defined on previous slide! e.g., (x. x) z  z Fall 18 CSCI 4430, A Milanova

13 Rules of Lambda Calculus: Exercises
Use -conversion and/or β-reduction: (x. x) y  ? (x. x) (y. y)  ? (x.y.z. x z (y z)) (u. u) (v. v)  Notation:  denotes that expression on the left reduces to the expression on the right, through a sequence -conversions and β-reductions. Fall 18 CSCI 4430, A Milanova

14 Reductions An expression ( x.E ) M is called a redex (for reducible expression) An expression is in normal form if it cannot be β-reduced The normal form is the meaning of the term, the “answer” Fall 18 CSCI 4430, A Milanova

15 Questions Is z. z z in normal form?
Answer: yes, it cannot be beta-reduced Is (z. z z) (x. x) in normal form? Answer: no, it can be beta-reduced Fall 18 CSCI 4430, A Milanova/BG Ryder

16 Definitions of Normal Form
Normal form (NF): a term without redexes Head normal form (HNF) x is in HNF (x. E) is in HNF if E is in HNF (x E1 E2 … En) is in HNF Weak head normal form (WHNF) x is in WHNF (x. E) is in WHNF (x E1 E2 … En) is in WHNF Fall 18 CSCI 4430, A Milanova (from MIT’s 2015 Program Analysis OCW)

17 Questions z. z z is in NF, HNF, or WHNF? (z. z z) (x. x) is in?
x.y.z. x z (y (u. u)) is in? (x.y. x) z ((x. z x) (x. z x)) is in? z ((x. z x) (x. z x)) is in? z.(x.y. x) z ((x. z x) (x. z x)) is in? (We will be reducing to NF, mostly)

18 More Reduction Exercises
C = x.y.f. f x y H = f. f (x.y. x) T = f. f (x.y. y) What is H (C a b)? (f. f (x.y. x)) (C a b) (C a b) (x.y. x) ((x.y.f. f x y) a b) (x.y. x) (f. f a b) (x.y. x) (x.y. x) a b a Fall 18 CSCI 4430, A Milanova (from MIT 2015 Program Analysis OCW)

19 Exercise S = x.y.z. x z (y z) I = x. x What is S I I I?
An expression with no free variables is called combinator. S, I, C, H, T are combinators. S = x.y.z. x z (y z) I = x. x What is S I I I? ( x.y.z. x z (y z) ) I I I (y.z. I z (y z) ) I I (z. I z (I z) ) I I I (I I) = (x. x) I (I I) I (I I) = (x. x) (I I) I I = (x. x) I  I Reducible expression is underlined at each step. Fall 18 CSCI 4430, A Milanova

20 Today’s Lecture Outline
Lambda calculus, continued Substitution, review Rules of the lambda calculus Normal forms Reduction strategies Review for test Fall 18 CSCI 4430, A Milanova/BG Ryder

21 Reduction Strategy Look again at (x.y.z. x z (y z)) (u. u) (v. v)
Actually, there are (at least) two “reduction paths”: Path 1: (x.y.z. x z (y z)) (u. u) (v. v) β (y.z. (u. u) z (y z)) (v. v) β (z. (u. u) z ((v. v) z)) β (z. z ((v. v) z)) β z. z z Path 2: (x.y.z. x z (y z)) (u. u) (v. v) β (y.z. (u. u) z (y z)) (v. v) β (y.z. z (y z)) (v. v) β (z. z ((v. v) z)) β

22 Reduction Strategy A reduction strategy (also called evaluation order) is a strategy for choosing redexes How do we arrive at a normal form (answer)? Applicative order reduction chooses the leftmost-innermost redex in an expression Also referred to as call-by-value reduction Normal order reduction chooses the leftmost-outermost redex in an expression Also referred to as call-by-name reduction Fall 18 CSCI 4430, A Milanova

23 Reduction Strategy: Examples
Evaluate (x. x x) ( (y. y) (z. z) ) Using applicative order reduction: (x. x x) ( (y. y) (z. z) ) (x. x x) (z. z) (z. z) (z. z)  (z. z) Using normal order reduction (y. y) (z. z) ( (y. y) (z. z) ) (z. z) ( (y. y) (z. z) ) (y. y) (z. z)  (z. z) Both orders arrived at the same normal form (generally, but not always true!) Normal order took more work (generally true).

24 Reduction Strategy In our examples, both strategies 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 (z. y) ((x. x x) (x. x x)) Applicative order reduction – what happens? Normal order reduction – what happens? Applicative order never terminates! Normal order reduction terminates. It evaluates to y. Fall 18 CSCI 4430, A Milanova

25 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 Church-Rosser Theorem, more formally: 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

26 Reduction Strategy 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 programming languages use? Fall 18 CSCI 4430, A Milanova/BG Ryder

27 Exercises Evaluate (x.y. x y) ((z. z) w)
Using applicative order reduction Using normal order reduction Fall 18 CSCI 4430, A Milanova

28 Exercise 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 Remember function application is left-associative, S I I I stands for ((S I) I) I

29 Today’s Lecture Outline
Lambda calculus, continued Substitution, review Rules of the lambda calculus Normal forms Reduction strategies Review for test Fall 18 CSCI 4430, A Milanova/BG Ryder

30 Exam 2 Practice (Quiz 4) 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 Binds to global x. 101 Sometimes it binds to global x sometimes it binds to B’s x. 4. Under dynamic scoping (with shallow binding), what gets printed? Answer: 0 Fall 18 CSCI 4430, A Milanova

31 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 (c) Static semantic analysis Spring 18 CSCI 4430, A Milanova

32 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 100 101 Spring 18 CSCI 4430, A Milanova

33 Exam 2 Practice (Quiz 6) What does f compute? (define (f lis)
(foldl (lambda (x y) (if (> x y) x y)) lis (car lis)) ) max element in lis Answer: maximal element in lis Spring 18 CSCI 4430, A Milanova

34 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? Flatten then compare. Answer: flatten then compare: (define (same-fringe? lis1 lis2) (equal? (flatten lis1) (flatten lis2))) Spring 18 CSCI 4430, A Milanova

35 Exam 2 Practice (Quiz 6) (____ ((is_even? (lambda (n) (or (zero? n)
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

36 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))) ) Answer: GCD, it is tail-recursive Fall 18 CSCI 4430, A Milanova

37 Exam 2 Practice (Quiz 5) (define (atom? obj) (not (pair? obj)) )
Answer: 6 Fall 18 CSCI 4430, A Milanova

38 Attribute Grammars Syntax of the Lambda calculus E  x E  ( x. E )
E  ( E E ) 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? Fall 18 CSCI 4430, A Milanova

39 Attribute Grammars Syntax of the Lambda calculus E  x E  ( x. E )
E  ( E E ) Write an attribute grammar that translates lambda expressions into properly parenthesized expressions without redundant parentheses. (Use syntactic conventions we defined in class.) Fall 18 CSCI 4430, A Milanova

40 Fall 18 CSCI 4430, A Milanova/BG Ryder


Download ppt "Announcements Exam 2 on Friday, November 2nd Topics"

Similar presentations


Ads by Google