Announcements Exam 2 on Friday, November 2nd Topics

Slides:



Advertisements
Similar presentations
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Advertisements

Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Foundations of Programming Languages: Introduction to Lambda Calculus
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 8 LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: exercises.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
A LISP interepreter for the Lambda Calculus Functional Programming
Functional Programming
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
Functional Programming
Describing Syntax and Semantics
Introduction to the λ-Calculus and Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
Names and Attributes Names are a key programming language feature
CS510 Compiler Lecture 4.
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
More on Lambda Calculus
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Corky Cartwright January 18, 2017
Closures and Streams cs784(Prasad) L11Clos
Compiler Construction (CS-636)
Reasoning About Code; Hoare Logic
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
FP Foundations, Scheme In Text: Chapter 14.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Announcements HW4 due today. There is no HW5. (It was merged into HW4.) HW6 (Scheme, Problem 1) out today, due October 23rd We’ll cover 100% of what you.
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
6.001 SICP Variations on a Scheme
L Calculus.
Announcements HW12: Transaction server Due today at 2pm
Announcements Quiz 5 HW6 due October 23
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

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

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

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

Lambda Calculus Reading: Scott, Ch. 11 on CD

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

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

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 )

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

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

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

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

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

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

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

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

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)

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)

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)

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

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

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)) β

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

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).

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Fall 18 CSCI 4430, A Milanova/BG Ryder