CS 611: Lecture 10 More Lambda Calculus September 20, 1999

Slides:



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

Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
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
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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.
Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc.,
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
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.
© 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.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Lesson2 Lambda Calculus Basics 1/10/02 Chapter 5.1, 5.2.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Lesson 3 Formalizing and Implementing Pure Lambda Calculus 1/15/02 Chapters 5.3, 6, 7.
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
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lecture 6: Lambda Calculus
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Context-Sensitive Analysis
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
Syntax versus Semantics
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
The Metacircular Evaluator
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Class 36: The Meaning of Truth CS200: Computer Science
Dynamic Scoping Lazy Evaluation
Programming Languages and Compilers (CS 421)
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
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)
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE S. Tanimoto Lambda Calculus
Introduction to the Lab
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Programming Languages
Lecture 2 מבוא מורחב.
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 10 More Lambda Calculus September 20, 1999 Cornell University Computer Science Department Andrew Myers normal form call-by-name/normal order: s-s op.sem. call-by-value/applicative order: s-s op.sem Church-Rosser theorem why is call-by-name hard to implement? de Bruijn indices combinators

Why is substitution hard? Substitution on lambda expressions: (l y e0) [e1 / x ]  (l y’ e0[y’/y][e1/x ]) where y’  FV e0 y’  FV e1 Abstract syntax DAGs without names: apply l (e0 e1) (l x e) e0 e1 e var CS 611—Semantics of Programming Languages—Andrew Myers

Example (l y e0) [e1 / x ]  (l y’ e0[y’/y][e1/x ]) On expressions: (l y e0) [e1 / x ]  (l y’ e0[y’/y][e1/x ]) (l a (b (l b a))) [ (l a a) / b ] = (l a’ (b (l b a’)) [(l a a)/ b]) = (l a’ (b (l b a’)) [(l a a) / b]) = (l a’ ((l a a) (l b a’))) On graphs: l l ... ... apply apply l var l var var l l var var var var var CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Holes in scope Reuse of an identifier creates a hole in scope (l a ((b (l a (b a))) a)) scope of outer “a” Variable is inaccessible within the region in which it is shadowed Common source of programming errors CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Reductions Lambda calculus reductions alpha (change of variable) (l x e)  (l x’ e[x’/x]) if (x’ FV e) beta (application) (( x e1) e2)  e1[e2 / x] eta (removal of extra abstraction) (l x (e x))  e if (x  FV e) Expressions are equivalent if they have the same Stoy diagram/abstract DAG can be converted to each other using alpha renaming on sub-expressions Evaluation is a sequence of reductions a b h CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Normal order Currently defined operational semantics: lazy evaluation (call-by-name) e0  (l x e2) (e0 e1)  e2 [ x / e1 ] Normal order evaluation: apply b (or h) reductions to leftmost redex till no reductions can be applied (normal form) Always finds a normal form if there is one Substitutes unevaluated form of actual parameters Hard to understand, implement with imperative lang. CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Applicative order Only b-substitute when the argument is fully reduced: argument evaluated before call e1 1 e’1 (e0 e1) 1 (e0 e’1) e0 1 e’0 (e0 v) 1 (e’0 v) ((l x e) v) 1 e[v/x] Almost call-by-value CS 611—Semantics of Programming Languages—Andrew Myers

Applicative order Applicative order may diverge even when a normal form exists Example: ((b c) ((a (a a)) (a (a a)))) Need special non-strict if form: (IF TRUE 0 Y) What if we allow any arbitrary order of evaluation? CS 611—Semantics of Programming Languages—Andrew Myers

Non-deterministic evaluation e0 1 e’0 (e0 e1) 1 (e’0 e1) e 1 e’ (l x e) 1 (l x e’) e1 1 e’1 (e0 e1) 1 (e0 e’1) (( x e1) e2) 1 e1[e2 / x] (b) x  FV e (l x (e x))  1 e (h) CS 611—Semantics of Programming Languages—Andrew Myers

Church-Rosser theorem Non-determinism in evaluation order does not result in non-determinism of result Formally: (e0 * e1  e0 * e2 )   e3 . e1 * e3  e2 * e’3  e3 = e’3 Implies: only one normal form for an expression Transition relation has the Church-Rosser property or diamond property if this theorem is true e0 e1 e2 e3 a CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers Concurrency Transition rules for application permit parallel evaluation of operator and operand Church-Rosser: any interleaving of computation yields same result Many commonly-used languages do not have Church-Rosser property C: int x=1, y = (x = 2)+x Intuition: lambda calculus is functional; value of expression determined locally (no store) e1 1 e’1 (e0 e1) 1 (e0 e’1) e0 1 e’0 (e0 e1) 1 (e’0 e1) CS 611—Semantics of Programming Languages—Andrew Myers

Simplifying lambda calculus Can we capture essential properties of lambda calculus in an even simpler language? Can we get rid of (or restrict) variables? S & K combinators: closed expressions are trees of applications of only S and K (no variables or abstractions!) de-Bruijn indices: all variable names are integers CS 611—Semantics of Programming Languages—Andrew Myers

CS 611—Semantics of Programming Languages—Andrew Myers DeBruijn indices Idea: name of formal argument of abstraction is not needed e ::= l e0 | e0 e1 | n Variable name n tells how many lambdas to walk up in AST IDENTITY  (l a a) = (l 0) TRUE  (l x (l y x)) = (l (l 1)) FALSE = 0  (l x (l y y)) = (l (l 0)) 2  (l f (l a (f (f a))) = (l (l (1 (1 0)))) CS 611—Semantics of Programming Languages—Andrew Myers

Translating to DeBruijn indices A function DB e that compiles a closed lambda expression e into DeBruijn index representation (closed = no free identifiers) Need extra argument N : symbol  integer to keep track of indices of each identifier DB e = T e, Ø T (e0 e1), N  = (T e0 , N T e1 , N) T x , N = N(x) T (l x e) , N = (l T e, (l y . if x = y then 0 else 1+N(y))) CS 611—Semantics of Programming Languages—Andrew Myers