Announcements Quiz 6 HW7 due Tuesday, October 30

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

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.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
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.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
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.
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© 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.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Lesson 3 Formalizing and Implementing Pure Lambda Calculus 1/15/02 Chapters 5.3, 6, 7.
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.
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
Operational Semantics of Scheme
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lambda Calculus CSE 340 – Principles of Programming Languages
Theory of Computation Lecture 4: Programs and Computable Functions II
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.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Functional Programming
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
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.
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
Material in the textbook Sections to 1.2.1
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
6.001 SICP Variations on a Scheme
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Announcements HW12: Transaction server Due today at 2pm
Announcements Quiz 5 HW6 due October 23
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE S. Tanimoto Lambda Calculus
Introduction to the Lab
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

Announcements Quiz 6 HW7 due Tuesday, October 30 Questions? We’ll have grades for HWs 4 over the weekend Fall 18 CSCI 4430, A Milanova/BG Ryder

Announcements Exam 2 on Friday, November 2nd Topics You are allowed 2 “cheat” pages only Practice tests under Course Materials Topics Binding and scoping Attribute grammars Scheme Lists, recursion, higher-order functions (especially map and fold), tail recursion, let expressions, scoping Lambda calculus and reduction strategies (as known as evaluation order)

Last class Scheme Tail recursion Binding with let expressions Scoping in Scheme, closures Scoping, revisited Fall 18 CSCI 4430, A Milanova/BG Ryder

Today’s Lecture Outline Lambda calculus Introduction Syntax and semantics Free and bound variables Substitution, formally Rules of lambda calculus Normal forms Reduction strategies Fall 18 CSCI 4430, A Milanova/BG Ryder

Lambda Calculus Reading: Scott, Ch. 11 on CD

Lambda Calculus A theory of functions Theory behind functional programming Turing-complete: any computable function can be expressed and evaluated using the calculus “Lingua franca” of programming language research Lambda () calculus expresses function definition and function application 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 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 Parentheses may be dropped from ( E1 E2 ) or ( x.E ) E.g., ( f x ) may be written as f x Function application groups from left-to-right (i.e., it is left-associative) E.g., x y z abbreviates ( ( x y ) z ) E.g., E1 E2 E3 E4 abbreviates ( ( ( E1 E2 ) E3 ) E4 ) Parentheses in x (y z) are necessary! Why? Fall 18 CSCI 4430, A Milanova/BG Ryder

Syntactic Conventions 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 z = x. ( x z ) = ( x. ( x z ) ) = ( x. x z ) ≠ ( ( x. x ) z ) WARNING: This is the most common syntactic convention (e.g., Pierce 2002). However some books give abstraction higher precedence; you might have seen that different convention

Terminology Parameter (also, formal parameter) E.g., x is the parameter in x. x z Argument (also, actual argument) E.g., expression z. z is the argument in (x. x) (z. z) Can you guess what this evaluates to? Fall 18 CSCI 4430, A Milanova/BG Ryder

Currying In lambda calculus, all functions have one parameter How do we express n-ary functions? Currying expresses an n-ary function in terms of n unary functions f(x,y) = x+y, becomes (x.y. x + y) (x.y. x + y) 2 3  (y. 2 + y) 3  2 + 3 = 5 Fall 18 CSCI 4430, A Milanova/BG Ryder

Currying in Scheme (define curried-plus (lambda (a) (lambda (b) (+ a b)))) (curried-plus 3) returns what? Returns the plus-3 function (or more precisely, it returns a closure) ((curried-plus 3) 2) returns what? 5 Scheme departs from the lamda calculus and allows us to define n-ary functions. Of course, we can still defined curried functions in Scheme. Here is an example. Fall 18 CSCI 4430, A Milanova/BG Ryder

Currying f(x1, x2,…,xn) = g x1 x2 … xn Function g is said to be the curried form of f. g1 x2 g2 x3 … Fall 18 CSCI 4430, A Milanova/BG Ryder

Semantics of Pure Lambda Calculus An expression has as its meaning the value that results after evaluation is carried out Somewhat informally, evaluation is the process of reducing expressions E.g., (x.y. x + y) 3 2  (y. 3 + y) 2  3 + 2 = 5 (Note: this example is just an informal illustration. There is no + in the pure lambda calculus!) x.y. x is assigned the meaning of TRUE x.y. y is assigned the meaning of FALSE

Free and Bound Variables Reducing expressions Consider expression ( x.y. x y ) (y w) Try 1: Reducing this expression results in the following ( y. x y ) [(y w)/x] = ( y. (y w) y ) The above notation means: we substitute argument (y w) for every occurrence of parameter x in body ( y. x y ). But what is wrong here? ( x.y. x y ) (y w): different y’s! If we substitute (y w) for x, the “free” y will become “bound”!

Free and Bound Variables Try 2: Rename “bound” y in y. x y to z: z. x z (x.y. x y) (y w) => (x.z. x z) (y w) E.g., in C, int id(int p) { return p; } is exactly the same as int id(int q) { return q; } Applying the reduction rule results in ( z. x z ) [(y w)/x] => ( z. (y w) z ) Fall 18 CSCI 4430, A Milanova/BG Ryder

Free and Bound Variables Abstraction ( x. E ) is also referred as 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

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 (more precisely, we have to rename to a variable that is NOT free in (y w)) (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 ) = z. y w z

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_ You will have to implement this substitution algorithm in Haskell 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 Introduction Syntax and semantics Free and bound variables Substitution, formally Rules of the lambda calculus Normal forms Reduction strategies Fall 18 CSCI 4430, A Milanova/BG Ryder