Programming Languages and Compilers (CS 421)

Slides:



Advertisements
Similar presentations
4/11/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
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
Catriel Beeri Pls/Winter 2004/5 functional-language 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation.
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
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.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
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.
9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
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.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
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.
Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
-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
10/16/081 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
2/6/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
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,
A LISP interepreter for the Lambda Calculus Functional Programming
Functional Programming
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
Programming Languages and Compilers (CS 421)
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
CSE-321 Programming Languages Simply Typed -Calculus
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
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.
Programming Language Concepts
Programming Languages and Compilers (CS 421)
Lesson2 Lambda Calculus Basics
Programming Languages and Compilers (CS 421)
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Announcements Quiz 6 HW7 due Tuesday, October 30
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
Class 31: Universal Turing Machines CS200: Computer Science
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Programming Languages
Programming Languages and Compilers (CS 421)
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 12/2/2018

Lambda Calculus - Motivation Aim is to capture the essence of functions, function applications, and evaluation calculus is a theory of computation “The Lambda Calculus: Its Syntax and Semantics”. H. P. Barendregt. North Holland, 1984 12/2/2018

Lambda Calculus - Motivation All sequential programs may be viewed as functions from input (initial state and input values) to output (resulting state and output values). -calculus is a mathematical formalism of functions and functional computations Two flavors: typed and untyped 12/2/2018

Only three kinds of expressions: Untyped -Calculus Only three kinds of expressions: Variables: x, y, z, w, … Abstraction:  x. e (Function creation, think fun x -> e) Application: e1 e2 12/2/2018

Untyped -Calculus Grammar Formal BNF Grammar: <expression> ::= <variable> | <abstraction> | <application> | (<expression>) <abstraction> ::= <variable>.<expression> <application> ::= <expression> <expression> 12/2/2018

Untyped -Calculus Terminology Occurrence: a location of a subterm in a term Variable binding:  x. e is a binding of x in e Bound occurrence: all occurrences of x in  x. e Free occurrence: one that is not bound Scope of binding: in  x. e, all occurrences in e not in a subterm of the form  x. e’ (same x) Free variables: all variables having free occurrences in a term 12/2/2018

( x. y  y. y ( x. x y) x) x 1 2 3 4 5 6 7 8 9 Example Label occurrences and scope: ( x. y  y. y ( x. x y) x) x 1 2 3 4 5 6 7 8 9 12/2/2018

( x. y  y. y ( x. x y) x) x 1 2 3 4 5 6 7 8 9 Example Label occurrences and scope: free free ( x. y  y. y ( x. x y) x) x 1 2 3 4 5 6 7 8 9 12/2/2018

Untyped -Calculus How do you compute with the -calculus? Roughly speaking, by substitution: ( x. e1) e2 * e1 [e2 / x] * Modulo all kinds of subtleties to avoid free variable capture 12/2/2018

Transition Semantics for -Calculus E --> E’’ E E’ --> E’’ E’ Application (version 1 - Lazy Evaluation) ( x . E) E’ --> E[E’/x] Application (version 2 - Eager Evaluation) E’ --> E’’ ( x . E) E’ --> ( x . E) E’’ ( x . E) V --> E[V/x] V - variable or abstraction (value) 12/2/2018

How Powerful is the Untyped -Calculus? The untyped -calculus is Turing Complete Can express any sequential computation Problems: How to express basic data: booleans, integers, etc? How to express recursion? Constants, if_then_else, etc, are conveniences; can be added as syntactic sugar 12/2/2018

Typed vs Untyped -Calculus The pure -calculus has no notion of type: (f f) is a legal expression Types restrict which applications are valid Types are not syntactic sugar! They disallow some terms Simply typed -calculus is less powerful than the untyped -Calculus: NOT Turing Complete (no recursion) 12/2/2018

Uses of -Calculus Typed and untyped -calculus used for theoretical study of sequential programming languages Sequential programming languages are essentially the -calculus, extended with predefined constructs, constants, types, and syntactic sugar Ocaml is close to the -Calculus: fun x -> exp -->  x. exp let x = e1 in e2 --> ( x. e2)e1 12/2/2018

 x. exp ---->  y. (exp [y/x])  Conversion -conversion:  x. exp ---->  y. (exp [y/x]) Provided that y is not free in exp No free occurrence of x in exp becomes bound in exp when replaced by y 12/2/2018

 Conversion Non-Examples 1. Error: y is not free in termsecond  x. x y ---->  y. y y 2. Error: free occurrence of x becomes bound in wrong way when replaced by y  x.  y. x y ---->  y.  y. y y exp exp[y/x] But  x. ( y. y) x ---->  y. ( y. y) y And  y. ( y. y) y ---->  x. ( y. y) x 12/2/2018

Let ~ be a relation on lambda terms. ~ is a congruence if it is an equivalence relation If e1 ~ e2 then (e e1) ~ (e e2) and (e1e) ~ (e2 e)  x. e1 ~  x. e2 12/2/2018

 Equivalence  equivalence is the smallest congruence containing  conversion One usually treats -equivalent terms as equal - i.e. use  equivalence classes of terms 12/2/2018

Example Show:  x. ( y. y x) x ~~  y. ( x. x y) y  x. ( y. y x) x ---->  z. ( y. y z) z so  x. ( y. y x) x ~~  z. ( y. y z) z ( y. y z) ----> ( x. x z) so ( y. y z) ~~ ( x. x z) so  z. ( y. y z) z ~~  z. ( x. x z) z  z. ( x. x z) z ---->  y. ( x. x y) y so  z. ( x. x z) z ~~  y. ( x. x y) y  x. ( y. y x) x ~~  y. ( x. x y) y 12/2/2018

Substitution Defined on -equivalence classes of terms P [N / x] means replace every free occurrence of x in P by N P called redex; N called residue Provided that no variable free in P becomes bound in P [N / x] Rename bound variables in P to avoid capturing free variables of N 12/2/2018

Rename y in redex if necessary Substitution x [N / x] = N y [N / x] = y if y  x (e1 e2) [N / x] = ((e1 [N / x] ) (e2 [N / x] )) ( x. e) [N / x] = ( x. e) ( y. e) [N / x] =  y. (e [N / x] ) provided y  x and y not free in N Rename y in redex if necessary 12/2/2018

Example ( y. y z) [( x. x y) / z] = ? Problems? z in redex in scope of y binding y free in the residue ( y. y z) [( x. x y) / z] ----> ( w.w z) [( x. x y) / z] =  w. w ( x. x y) 12/2/2018

Example Only replace free occurrences ( y. y z ( z. z)) [( x. x) / z] =  y. y ( x. x) ( z. z) Not  y. y ( x. x) ( z. ( x. x)) 12/2/2018

 reduction  Rule: ( x. P) N ----> P [N /x] Essence of computation in the lambda calculus Usually defined on -equivalence classes of terms 12/2/2018

----> ( x. x y) ( y. y z) ----> ( y. y z) y ----> y z Example ( z. ( x. x y) z) ( y. y z) ----> ( x. x y) ( y. y z) ----> ( y. y z) y ----> y z ( x. x x) ( x. x x) ----> ( x. x x) ( x. x x) ----> ( x. x x) ( x. x x) ----> …. 12/2/2018

  Equivalence equivalence is the smallest congruence containing  equivalence and  reduction A term is in normal form if no subterm is  equivalent to a term that can be  reduced Hard fact (Church-Rosser): if e1 and e2 are -equivalent and both are normal forms, then they are  equivalent 12/2/2018

Order of Evaluation Not all terms reduce to normal forms Not all reduction strategies will produce a normal form if one exists 12/2/2018

Lazy evaluation: Always reduce the left-most application in a top-most series of applications (i.e. Do not perform reduction inside an abstraction) Stop when term is not an application, or left-most application is not an application of an abstraction to a term 12/2/2018

( z. ( x. x)) (( y. y y) ( y. y y)) Example 1 ( z. ( x. x)) (( y. y y) ( y. y y)) Lazy evaluation: Reduce the left-most application: ( z. ( x. x)) (( y. y y) ( y. y y)) ----> ( x. x) 12/2/2018

Eager evaluation (Eagerly) reduce left of top application to an abstraction Then (eagerly) reduce argument Then -reduce the application 12/2/2018

Example 1 ( z. ( x. x))(( y. y y) ( y. y y)) Eager evaluation: Reduce the rator of the top-most application to an abstraction: Done. Reduce the argument: ----> ( z. ( x. x))(( y. y y) ( y. y y)) ----> ( z. ( x. x))(( y. y y) ( y. y y))… 12/2/2018

Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: 12/2/2018

Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: 12/2/2018

(( y. y y ) ( z. z)) (( y. y y ) ( z. z)) Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: ( x. x x )(( y. y y) ( z. z)) ----> (( y. y y ) ( z. z)) (( y. y y ) ( z. z)) 12/2/2018

(( y. y y ) ( z. z)) (( y. y y ) ( z. z) Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: ( x. x x )(( y. y y) ( z. z)) ----> (( y. y y ) ( z. z)) (( y. y y ) ( z. z) 12/2/2018

(( y. y y ) ( z. z)) (( y. y y ) ( z. z)) Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: ( x. x x )(( y. y y) ( z. z)) ----> (( y. y y ) ( z. z)) (( y. y y ) ( z. z)) 12/2/2018

Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: (( y. y y ) ( z. z)) (( y. y y ) ( z. z)) ----> (( z. z ) ( z. z))(( y. y y ) ( z. z)) 12/2/2018

Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: (( y. y y ) ( z. z)) (( y. y y ) ( z. z)) ----> (( z. z ) ( z. z))(( y. y y ) ( z. z)) 12/2/2018

Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: (( y. y y ) ( z. z)) (( y. y y ) ( z. z)) ----> (( z. z ) ( z. z))(( y. y y ) ( z. z)) 12/2/2018

Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: (( y. y y ) ( z. z)) (( y. y y ) ( z. z)) ----> (( z. z ) ( z. z))(( y. y y ) ( z. z)) ----> ( z. z ) (( y. y y ) ( z. z)) 12/2/2018

(( y. y y ) ( z. z)) (( y. y y ) ( z. z)) Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: ( x. x x )(( y. y y) ( z. z)) ----> (( y. y y ) ( z. z)) (( y. y y ) ( z. z)) ----> (( z. z ) ( z. z))(( y. y y ) ( z. z)) ----> ( z. z ) (( y. y y ) ( z. z)) ----> ( y. y y ) ( z. z) 12/2/2018

Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: (( y. y y ) ( z. z) ) (( y. y y ) ( z. z)) ----> (( z. z ) ( z. z))(( y. y y ) ( z. z)) ----> ( z. z ) (( y. y y ) ( z. z)) ----> ( y. y y ) ( z. z) 12/2/2018

Example 2 ( x. x x)(( y. y y) ( z. z)) Lazy evaluation: (( y. y y ) ( z. z) ) (( y. y y ) ( z. z)) ----> (( z. z ) ( z. z))(( y. y y ) ( z. z)) ----> ( z. z ) (( y. y y ) ( z. z)) ----> ( y. y y ) ( z. z) ~~  z. z 12/2/2018

( x. x x) (( y. y y) ( z. z)) ----> Example 2 ( x. x x)(( y. y y) ( z. z)) Eager evaluation: ( x. x x) (( y. y y) ( z. z)) ----> ( x. x x) ((  z. z ) ( z. z)) ----> ( x. x x) ( z. z) ----> ( z. z) ( z. z) ---->  z. z 12/2/2018