CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.

Slides:



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

Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
Foundations of Programming Languages: Introduction to Lambda Calculus
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc.,
Type Systems and Object- Oriented Programming John C. Mitchell Stanford University.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
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.
A Theory of Hygienic Macros David Herman, Mitchell Wand Northeastern University.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
© 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.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction.
Lesson2 Lambda Calculus Basics 1/10/02 Chapter 5.1, 5.2.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
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
Operational Semantics of Scheme
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
Introduction to the λ-Calculus and Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lecture 6: Lambda Calculus
Lambda Calculus CSE 340 – Principles of Programming Languages
CSE-321 Programming Languages Simply Typed -Calculus
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
More on Lambda Calculus
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lambda Calculus Revisited
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Typed Arithmetic Expressions
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
Prolog syntax + Unification
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
A-calculus with Constants and Let-blocks
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
Class 33: Making Recursion M.C. Escher, Ascending and Descending
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Class 31: Universal Turing Machines CS200: Computer Science
Recursion and Fixed-Point Theory
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:

CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Last time Introduced compact, powerful programming language: untyped lambda calculus (Church, 1930’s) All values are first-class, anonymous functions Syntax: e ::= x | e0 e1 | l x e0 var application abstraction Missing: multiple arguments  local variables  primitive values (booleans, integers, …)  data structures  recursive functions (this lecture) assignment CS 611—Semantics of Programming Languages—Andrew Myers

Lambda Calculus References Gunter (recommended text) Stoy, Denotational Semantics: the Scott-Strachey Approach to Programming Language Theory Davie, An Introduction to Functional Programming Systems using Haskell Barendregt, The Lamba Calculus: Its Syntax and Semantics CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Recursion How to express recursive functions? Consider factorial function: 1 if x = 0 x*fact(x-1) if x > 0 fact(x) = Can’t write this recursive definition! FACT  (x (IF (ZERO? x) 1 (* x (FACT (- x 1))))) ( ZERO?  ( n ((n ( x FALSE )) TRUE)) ) CS 611—Semantics of Programming Languages—Andrew Myers

Recursive definitions FACT  (x (IF (ZERO? x) 1 (* x (FACT (- x 1))))) This is an equation, not a definition! Meaning: FACT stands for a function that, if applied to an argument, gives the same result that (x (IF (ZERO? x) 1 (* x (FACT (- x 1))))) does. CS 611—Semantics of Programming Languages—Andrew Myers

Defining a recursive function Idea: introduce a function FACT  just like FACT except that it has an extra argument that should be passed a function f such that ((f f) x) computes factorial of x FACT   (f (x (IF (ZERO? x) 1 (* x ((f f) (- x 1)))))) Now define FACT  (FACT  FACT ) This expression diverges but its application to a number does not! CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Evaluation of FACT FACT   (f (x (IF (ZERO? x) 1 (* x ((f f) (- x 1)))))) FACT  (FACT  FACT ) (FACT 2)  ((FACT  FACT ) 2)  (IF (ZERO? 2) 1 (* 2 ((FACT  FACT ) (- 2 1))))  (* 2 ((FACT  FACT ) (- 2 1)))))) = (* 2 (FACT (- 2 1)))))) CS 611—Semantics of Programming Languages—Andrew Myers

Generalizing FACT   (f (x (IF (ZERO? x) 1 (* x ((f f) (- x 1)))))) The recursion-removal transformation: Add an extra argument variable (f) to the recursive function Replace all internal references to the recursive function with an application of argument variable to itself Replace all external references to the recursive function as application of it to itself Can this transformation itself be abstracted? CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Fixed point operator Suppose we had an operator Y that found the fixed point of functions: ((Y f) x) = (f ((Y f) x)) (Y f) = f (Y f) Now write a recursive function as a function that takes itself as an argument: FACTEQN  f (x (IF (ZERO? x) 1 (* x (f (- x 1)))))) Idea: FACT = (FACTEQN FACT ) FACT  (Y FACTEQN) = FACTEQN(Y FACTEQN) = FACTEQN(FACTEQN(FACTEQN(FACTEQN(...)))) CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Can we express the Y operator as a lambda expression? Maybe not! # functions from Z to boolean: 2|Z| # functions:  2|Z| Set of all functions is uncountably infinite Set of computable functions is the same size: countably infinite Only an infinitesimal fraction of all functions are computable No reason to expect an arbitrary function to be computable! (e.g., halting function is not) CS 611—Semantics of Programming Languages—Andrew Myers

Definition of Y Y is a solution to this equation: Y = (f (f (Y f ))) Now, apply our recursion-removal trick: Y  (y (f (f ( (y y) f )))) Y  (Y Y ) Traditional form for Y: Y  (f ((x (f (x x)) (x (f (x x))) CS 611—Semantics of Programming Languages—Andrew Myers

Variable binding Which variable is denoted by an identifier? Lexical scope: (l x (l x x) x) (l p (l q ((l p (p q)) (l r (p r))))) lp lq (l p (l q ((l p (p q)) (l r (p r))))) a (l • (l • ((l • (• • )) (l • (• •))))) lp lr Stoy diagram a a CS 611—Semantics of Programming Languages—Andrew Myers

Problems with substitution Rule for evaluating an application: (( x e1) e2)  e1[e2 / x] Can’t just stick e2 in for every occurrence of variable x : (x (l x x)) [ (b a) / x ] = ((b a) (l x (b a))) Can’t just stick e2 in for every occurrence of variable x outside any lambda over x : (y (l x (x y))) [x / y] = (x (l x (x x))) Variable capture CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers A recurring problem! Nobody gets the substitution rule right: Church, Hilbert, Gödel, Quine, Newton, etc. Substitution problem also comes up most PL’s, even in integral calculus: e.g. how to substitute y := x in y +  xy dx Need to distinguish between free and bound identifiers—variable capture occurs when we substitute an expression into a context where its variables are bound CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Free identifiers The function FV e gives the set of all free (unbound) identifiers in e Special brackets   are called semantic brackets distinguish functions that operate on the result of expression e and functions that operate on abstract syntax tree for e traditional FV x = { x } FV e0 e1 = FV e0  FV e1 FV l x e = FV e – { x } CS 611—Semantics of Programming Languages—Andrew Myers

Defining substitution inductively x[e / x ]  e y[e / x ]  y (y  x) (e0 e1) [e2 / x ]  (e0[e2/x ] e1[e2/x ] ) (l x e0)[e1 / x ]  (l x e0) CS 611—Semantics of Programming Languages—Andrew Myers

Substitution into abstraction (l y e0) [e1 / x ]  (l y’ e0[y’/y][e1/x ]) where y’  FV e0 y’  FV e1 y’  x CS 611—Semantics of Programming Languages—Andrew Myers

Renaming Intuitively, meaning of lambda expression does not depend on name of argument variable: (l x x) =(l y y) = (l  ) (l x (l y (y x)) = (l p (l q (q p)) = (l y (l x (x y)) = (l  (l  ( )) (l y e0) [e1 / x ]  (l y’ e0[y’/y][e1/x ]) a-reduction: (l y e0)  (l y’ e0[y’/y]) where y’  FV e0 (does not change Stoy diagram) a CS 611—Semantics of Programming Languages—Andrew Myers

Extensionality Two functions are equal by extension if they give the same result when applied to the same argument Therefore, the expressions (l x (e x)) and e are equal by extension ((l x (e0 x)) e1) = e0 e1 if (x FV e0) h-reduction: (l x (e x))  e if (x FV e) h CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Reductions Three reductions that preserve the meaning of a lambda calculus expression: (l x e)  (l x’ e[x’/x]) if (x’ FV e) (( x e1) e2)  e1[e2 / x] (l x (e x))  e if (x  FV e) Reductions can be applied to sub-expressions: if X reduces to Y then (X Z) reduces to (Y Z) for any Z (Z X) reduces to (Z Y) for any Z (l z X) reduces to (l z Y) for any identifier z a b h CS 611—Semantics of Programming Languages—Andrew Myers

Equivalence Two lambda expressions are a-equivalent if they can be converted to each other using a-reductions / have the same Stoy diagrams (l p (l q (q p))  (l x (l q (q x))  (l x (l y (y x)) (l • (l • (• •))  (l • (l • (• •))  (l • (l • (• •)) Lambda expressions form equivalence classes defined by their Stoy diagrams a a CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Normal Form A lambda expression is in normal form when no reductions can be performed on it or on any of its sub-expressions Reducible expressions are called redexes All normal forms for an expression are in the same equivalence class! What is the normal form for Y ? CS 611—Semantics of Programming Languages—Andrew Myers