CSE 505: Concepts of Programming Languages

Similar documents
CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Type Systems Winter Semester 2006

CIS 500 Software Foundations Fall September 25

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Lecture 5: The Untyped λ-calculus

Concepts of programming languages

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CMSC 330: Organization of Programming Languages

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Fundamentals and lambda calculus

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Lambda Calculus. Lecture 4 CS /26/10

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Chapter 5: The Untyped Lambda Calculus

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Introduction to the Lambda Calculus

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

Elixir, functional programming and the Lambda calculus.

Untyped Lambda Calculus

1 Scope, Bound and Free Occurrences, Closed Terms

Untyped Lambda Calculus

More Lambda Calculus and Intro to Type Systems

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

dynamically typed dynamically scoped

Introduction to the λ-calculus

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

CSE505, Fall 2012, Midterm Examination October 30, 2012

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

CSE 341: Programming Languages

Functional Programming

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

Formal Systems and their Applications

CMSC 330: Organization of Programming Languages. Lambda Calculus

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

More Lambda Calculus and Intro to Type Systems

Pure Lambda Calculus. Lecture 17

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Functional Languages and Higher-Order Functions

CMSC330. Objects, Functional Programming, and lambda calculus

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Programming Languages Lecture 15: Recursive Types & Subtyping

CMSC 330: Organization of Programming Languages

CSE 505: Concepts of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

CSE-321 Programming Languages 2012 Midterm

CITS3211 FUNCTIONAL PROGRAMMING

The Untyped Lambda Calculus

Graphical Untyped Lambda Calculus Interactive Interpreter

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CIS 500 Software Foundations Midterm I

CSCI-GA Scripting Languages

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Programming Languages Lecture 14: Sum, Product, Recursive Types

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Programming Languages

Lambda Calculus. Lambda Calculus

Lecture Notes on Data Representation

Last class. CS Principles of Programming Languages. Introduction. Outline

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

From the λ-calculus to Functional Programming Drew McDermott Posted

CS152: Programming Languages. Lecture 5 Pseudo-Denotational Semantics. Dan Grossman Spring 2011

Lambda Calculus and Extensions as Foundation of Functional Programming

Chapter 5: The Untyped Lambda Calculus

CSE-321 Programming Languages 2010 Midterm

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lambda Calculus. Variables and Functions. cs3723 1

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CSE 505: Programming Languages. Lecture 10 Types. Zach Tatlock Winter 2015

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Recursive Definitions, Fixed Points and the Combinator

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable?

CSE-321 Programming Languages 2010 Final

Costly software bugs that could have been averted with type checking

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Transcription:

CSE 505: Concepts of Programming Languages Dan Grossman Fall 2003 Lecture 6 Lambda Calculus Dan Grossman CSE505 Fall 2003, Lecture 6 1

Where we are Done: Modeling mutation and local control-flow Proving operational equivalence (hard!) Now: Didn t IMP leave some things out? Time for a new model... (Pierce, chapter 5) Dan Grossman CSE505 Fall 2003, Lecture 6 2

What we forgot IMP is missing lots of things (threads, I/O, exceptions, strings,...), but here are two really basic and interesting ones: Scope (all global variables, no functions or objects) Data structures (only integers, no records or pairs) As we ll see, higher-order functions do both! (E.g., my heap implementation in hw1 used functions like a list) Dan Grossman CSE505 Fall 2003, Lecture 6 3

Adding data structures Extending IMP with data structures isn t too hard: e ::= c x e + e e e (e, e) e.1 e.2 v ::= c (v, v) H ::= H, x v H;e 1 v 1 H;e 2 v 2 H;(e 1, e 2 ) (v 1, v 2 ) H;e (v 1, v 2 ) H;e.1 v 1 H;e (v 1, v 2 ) H;e.2 v 2 Note: We allow pairs of values, not just pairs of integers. Note: We now have stuck programs (e.g., c.1) what would C do? Scheme? ML? Java? Dan Grossman CSE505 Fall 2003, Lecture 6 4

What about functions But adding functions (or objects) does not work well: e ::=... fun x -> s s ::=... e(e) H;fun x -> s fun x -> s H;e 1 fun x -> s H;e 2 v H ; e 1 (e 2 ) H ; x := v; s NO: Consider (fun x -> s)(y := x); ans := x. Scope matters; variable name doesn t. Dan Grossman CSE505 Fall 2003, Lecture 6 5

Another try H;e 1 fun x -> s H;e 2 v y fresh H ; e 1 (e 2 ) H ; y := x; x := v; s; x := y fresh is sloppy not a good match to how functions are implemented (round-peg, square-hole) yuck NO: wrong model for most functional and OO languages (okay for C, I think) Dan Grossman CSE505 Fall 2003, Lecture 6 6

The wrong model H;e 1 fun x -> s H;e 2 v y fresh H ; e 1 (e 2 ) H ; y := x; x := v; s; x := y f 1 := fun x -> f 2 := fun z -> y := x + z; f 1 (2); x := 3 f 2 (4) let f1 = fun x -> (fun z -> x + z) in let f2 = f1 2 in let x = 3 in f2 4 Dan Grossman CSE505 Fall 2003, Lecture 6 7

Punch line The way higher-order functions and objects work is not modeled by mutable global variables. So let s build a new model that focuses on this essential concept. (Or just borrow a model from Alonzo Church.) And drop mutation, conditionals, integers (!), and loops (!) The Lambda Calculus: e ::= λx. e x e e v ::= λx. e You apply a function by substituting the argument for the bound variable. Dan Grossman CSE505 Fall 2003, Lecture 6 8

Example Substitutions e ::= λx. e x e e v ::= λx. e Substitution is the key operation we were missing: (λx. x)(λy. y) (λy. y) (λx. λy. y x)(λz. z) (λy. y λz. z) (λx. x x)(λx. x x) (λx. x x)(λx. x x) After substitution, the bound variable is gone, so its name was irrelevant. (Good!) There are irreducible expressions (x e). (maybe a problem) Dan Grossman CSE505 Fall 2003, Lecture 6 9

A Programming Language Given substitution (e 1 [e 2 /x]), we can give a semantics: (λx. e) v e[v/x] e 1 e 1 e 1 e 2 e 1 e 2 e 2 e 2 v e 2 v e 2 A small-step, call-by-value (CBV), left-to-right semantics Gets stuck exactly when there s a free variable at top-level (Won t cheat because scope is what we re interested in) This is the heart of functional languages like O Caml (but real implementations don t substitute see hw3) Dan Grossman CSE505 Fall 2003, Lecture 6 10

Where are we Motivation for a new model (done) CBV lambda calculus using substitution (done) Notes on concrete syntax Simple Lambda encodings Other reduction strategies Defining substitution Dan Grossman CSE505 Fall 2003, Lecture 6 11

Syntax Revisited We resolve concrete-syntax ambiguities as follows: 1. λx. e 1 e 2 is (λx. e 1 e 2 ), not (λx. e 1 ) e 2 2. e 1 e 2 e 3 is (e 1 e 2 ) e 3, not e 1 (e 2 e 3 ) (Convince yourself application is not associative) More generally: 1. Function bodies extend to an unmatched right parenthesis (e.g., (λx. y(λz. z)w)q) 2. Application associates to the left. E.g., e 1 e 2 e 3 e 4 is (((e 1 e 2 ) e 3 ) e 4 ). These strange-at-first rules are convenient (see O Caml) Dan Grossman CSE505 Fall 2003, Lecture 6 12

Simple encodings It s fairly crazy we left out constants, conditionals, primitives, and data structures. In fact, we re Turing complete and can encode whatever we need. Motivation for encodings: It s fun and mind-expanding It shows we aren t oversimplifying the model ( numbers are syntactic sugar ) It can show languages are too expressive (e.g., unlimited C++ template instantiation) Dan Grossman CSE505 Fall 2003, Lecture 6 13

Encoding booleans There are two booleans and one conditional expression. The conditional takes 3 arguments (via currying). If the first is one boolean it evaluates to the second. If it s the other boolean it evaluates to the third. Any 3 expressions meeting this specification (of the boolean ADT ) is an encoding of booleans. true λx. λy. x false λx. λy. y if λb. λt. λf. b t f This is just one encoding. E.g.: if true v 1 v 2 v 1. Dan Grossman CSE505 Fall 2003, Lecture 6 14

Evaluation Order Matters Careful: With CBV we need to thunk... if true (λx. x) ((λx. x x)(λx. x x)) diverges, but ( if true (λz. λx. x) (λz. ((λx. x x)(λx. x x)))) v doesn t. Dan Grossman CSE505 Fall 2003, Lecture 6 15

Encoding pairs The pair ADT has a constructor taking two arguments and two selectors. The first selector returns the first argument passed to the constructor and the second selector returns the second. mkpair λx. λy. λz. z x y fst λp. p(λx. λy. x) snd λp. p(λx. λy. y) Example: snd ( fst ( mkpair ( mkpair v 1 v 2 ) v 3 )) v 2 Dan Grossman CSE505 Fall 2003, Lecture 6 16

More encodings (see the text) Church numerals : A natural number is 0 or the successor of a natural (and maybe a test for equality) Given the encoding, can define math operations (plus, exponentiation,...) lists : in an untyped world, booleans and pairs are enough for lists, one approach: Empty list is mkpair false false Non-empty list is mkpair true ( mkpair h t) (Not too far from how lists are implemented.) recursion : useful looping by applying a function to itself and an argument (too intricate for lecture) Dan Grossman CSE505 Fall 2003, Lecture 6 17

Reduction Strategies Suppose we allowed any substitution to take place in any order: e 1 e 1 (λx. e) e e[e /x] e 2 e 2 e 1 e 2 e 1 e 2 e 1 e 2 e 1 e 2 e e λx. e λx. e Programming languages don t typically do this, but it has uses... Dan Grossman CSE505 Fall 2003, Lecture 6 18

Full Reduction Prove programs equivalent algebraically (also use λx. e λy. e where e is e with free x replaced with y and λx. e x e where x not free in e) Optimize/pessimize/partially-evaluate (even avoid an infinite loop) What order you reduce is a strategy ; equivalence is undecidable Non-obvious fact ( Church-Rosser ): In this pure calculus, if e e 1 and e e 2, then there exists an e 3 such that e 1 e 3 and e 2 e 3. No strategy gets painted into a corner Dan Grossman CSE505 Fall 2003, Lecture 6 19

Some other common strategies We have seen full and left-to-right CBV. (O Caml is unspecified order, but actually right-to-left.) Claim: Without assignment, I/O, exceptions,... you cannot distinguish evaluation order. Another option is call-by-name (CBN): (λx. e) e e[e /x] e 1 e 1 e 1 e 2 e 1 e 2 Even smaller than CBV! Diverges strictly less often than CBV, e.g., (λy. λz. z)e. Can be faster (fewer steps), but not usually (reuse args). Dan Grossman CSE505 Fall 2003, Lecture 6 20

More on evaluation order In purely functional code, evaluation order only matters for performance and termination. Example: Imagine CBV for conditionals! let rec f n = if n=0 then 1 else n*(f (n-1)) Call-by-need or lazy evaluation : Best of both worlds? (E.g.: Haskell) Evaluate the argument the first time it s used. Memoize the result. (Useful idiom for coders too.) Can be formalized, but it s not pretty. For purely functional code, total equivalence with CBN and same asymptotic time as CBV. (Note: asymptotic!) Hard to reason about with effects. Dan Grossman CSE505 Fall 2003, Lecture 6 21

Formalism not done yet For rest of course, assume CBV: (λx. e) v e[v/x] e 1 e 1 e 1 e 2 e 1 e 2 e 2 e2 v e 2 v e 2 Need to define substitution shockingly subtle. Attempt 1: x[e/x] = e y x y[e/x] = y e 1 [e/x] = e 1 (λy. e 1 )[e/x] = λy. e 1 e 1 [e/x] = e 1 e 2 [e/x] = e 2 (e 1 e 2 )[e/x] = e 1 e 2 Dan Grossman CSE505 Fall 2003, Lecture 6 22

Getting substitution right Attempt 2: e 1 [e/x] = e 1 y x (λy. e 1 )[e/x] = λy. e 1 (λx. e 1 )[e/x] = λx. e 1 What if e is y or λz. y or, in general y is free in e? This mistake is called capture. It doesn t happen under CBV/CBN if our source program has no free variables. Can happen under full reduction. Dan Grossman CSE505 Fall 2003, Lecture 6 23

Another Try Attempt 3 (define F V (e) inductively on e): e 1 [e/x] = e 1 y x y F V (e) (λy. e 1 )[e/x] = λy. e 1 (λx. e 1 )[e/x] = λx. e 1 A partial definition because of the syntactic accident that y was used as a binder (should not be visible). So we allow implicit systematic renaming (of a binding and all its bound occurrences). So the top rule can always apply. In general, we never distinguish terms that differ only in the names of variables. (A key language-design principle!) Dan Grossman CSE505 Fall 2003, Lecture 6 24

Summary and some jargon If everything is a function, every step is an application: (λx. e)e e[e /x]) (called β-reduction) Substitution avoids capture via implicit renaming (called α-conversion) With full reduction, λx. e x e makes sense if x F V (e) (called η-reduction), for CBV it s unthunking Most languages use CBV application, some use call-by-need. Our Turing-complete language models functions and encodes everything else. Dan Grossman CSE505 Fall 2003, Lecture 6 25

Where we re going You should now have a better understanding of higher-order functions, functional languages, and evaluation order. Which will make you a better programmer in all languages. But the untyped lambda-calculus is like an assembly language where everything looks the same: closed programs always do something and everything is a function (cf. bits). Next: We ll distinguish constants from functions and use a type system to prevent run-time errors. Dan Grossman CSE505 Fall 2003, Lecture 6 26