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

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

CSE 505: Concepts of Programming Languages

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

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

CIS 500 Software Foundations Fall September 25

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

Chapter 5: The Untyped Lambda Calculus

Type Systems Winter Semester 2006

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

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.

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

More Lambda Calculus and Intro to Type Systems

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

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

Functional Languages and Higher-Order Functions

CIS 500 Software Foundations Midterm I

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

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

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

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

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

More Lambda Calculus and Intro to Type Systems

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

CMSC 330: Organization of Programming Languages

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

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

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

The Untyped Lambda Calculus

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

Elixir, functional programming and the Lambda calculus.

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

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

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

CMSC 330: Organization of Programming Languages

Lecture 5: The Untyped λ-calculus

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Concepts of programming languages

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

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

Organization of Programming Languages CS3200/5200N. Lecture 11

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Introduction to the λ-calculus

CSE-321 Programming Languages 2010 Midterm

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

CMSC330. Objects, Functional Programming, and lambda calculus

CSE-321 Programming Languages 2012 Midterm

The Substitution Model

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.

Fundamentals and lambda calculus

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Lambda Calculus. Variables and Functions. cs3723 1

Graphical Untyped Lambda Calculus Interactive Interpreter

Introduction to the Lambda Calculus

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Lambda Calculus. Lecture 4 CS /26/10

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

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

Lambda Calculus as a Programming Language

CMSC 330: Organization of Programming Languages. Lambda Calculus

More Lambda Calculus and Intro to Type Systems

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

CMSC 330, Fall 2018 Midterm 2

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

To figure this out we need a more precise understanding of how ML works

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330: Organization of Programming Languages

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

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

Functional Programming

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

The Substitution Model. Nate Foster Spring 2018

CSE505, Fall 2012, Midterm Examination October 30, 2012

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

CMSC 330: Organization of Programming Languages

Intro. Scheme Basics. scm> 5 5. scm>

Costly software bugs that could have been averted with type checking

Functional Programming

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSCI-GA Scripting Languages

Lambda Calculus as a Programming Language

Introduction to Scheme

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Programming Languages

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CMSC 330: Organization of Programming Languages. Lambda Calculus

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CS 6110 S14 Lecture 1 Introduction 24 January 2014

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

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

Lambda Calculus see notes on Lambda Calculus

Functional Programming. Big Picture. Design of Programming Languages

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

Transcription:

Whereweare CS-XXX: Graduate Programming Languages Lecture 7 Lambda Calculus Done: Syntax, semantics, and equivalence For a language with little more than loops and global variables Now: Didn t IMP leave some things out? In particular: scope, functions, and data structures (Not to mention threads, I/O, exceptions, strings,...) Time for a new model... Dan Grossman 2012 Dan Grossman CS-XXX 2012, Lecture 7 2 Data + Code Higher-order functions work well for scope and data structures Scope: not all memory available to all code let x = 1 let add3 y = let z = 2 in x + y + z let seven = add3 4 Data: Function closures store data. Example: Association list let empty = (fun k -> raise Empty) let cons k v lst = (fun k -> if k =k then v else lst k ) let lookup k lst = lst k (Later: Objects do both too) Adding data structures Extending IMP with data structures is not 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 v all old rules plus: H ; e 1 v 1 H ; e 2 v 2 H ; (e 1,e 2 ) (v 1,v 2 ) Notice: H ; e (v 1,v 2 ) H ; e.1 v 1 H ; e (v 1,v 2 ) H ; e.2 v 2 We allow pairs of values, not just pairs of integers We now have stuck programs (e.g., c.1) What would C++ do? Scheme? ML? Java? Perl? Division also causes stuckness Dan Grossman CS-XXX 2012, Lecture 7 3 Dan Grossman CS-XXX 2012, Lecture 7 4 What about functions But adding functions (or objects) does not work well: e ::=... fun x -> s v ::=... fun x -> s s ::=... e(e) H ; e v H ; s H ; s Additions: 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 Does this match the semantics we want for function calls? What about functions But adding functions (or objects) does not work well: H ; fun x -> s fun x -> s e ::=... fun x -> s v ::=... fun x -> s s ::=... e(e) H ; e 1 fun x -> s H ; e 2 v H ; e 1 (e 2 ) H ; x := v; s NO: Consider x := 1; (fun x -> y := x)(2); ans := x. Scopematters;variablenamedoesnot.Thatis: Local variables should be local Choice of local-variable names should have only local ramifications Dan Grossman CS-XXX 2012, Lecture 7 5 Dan Grossman CS-XXX 2012, Lecture 7 6

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 not very IMP-like but okay (think malloc) not a good match to how functions are implemented yuck: the way we want to think about something as fundamental as a call? NO: wrong model for most functional and OO languages (Even wrong for C if s calls another function that accesses the global variable x) 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 -> ans := x + z)); f 1 (2); x := 3; f 2 (4) Should set ans to 6: f 1 (2) should assign to f 2 a function that adds 2 to its argument and stores result in ans Actually sets ans to 7: f 2 (2) assigns to f 2 a function that adds the current value of x to its argument Dan Grossman CS-XXX 2012, Lecture 7 7 Dan Grossman CS-XXX 2012, Lecture 7 8 Punch line Cannot properly model local scope via a global heap of integers. Functions are not syntactic sugar for assignments to globals So let s build a new model that focuses on this essential concept (can add back IMP features later) Or just borrow a model from Alonzo Church The Lambda Calculus The Lambda Calculus: e ::= λx. e x ee v ::= λx. e You apply a function by substituting the argument for the bound variable (There is an equivalent environment definition not unlike heap-copying; see future homework) And drop mutation, conditionals, integers (!), and loops (!) Dan Grossman CS-XXX 2012, Lecture 7 9 Dan Grossman CS-XXX 2012, Lecture 7 10 Example Substitutions e ::= λx. e x ee 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!) A Programming Language Given substitution (e 1 [e 2 /x] =e 3 ), we can give a semantics: e e e[v/x] =e (λx. e) v e e 1 e 1 e 1 e 2 e 1 e 2 e 2 e 2 ve 2 ve 2 A small-step, call-by-value (CBV), left-to-right semantics Terminates when the whole program is some λx. e But (also) gets stuck when there s a free variable at top-level Won t cheat like we did with H(x) in IMP because scope is what we are interested in This is the heart of functional languages like OCaml But real implementations do not substitute; they do something equivalent Dan Grossman CS-XXX 2012, Lecture 7 11 Dan Grossman CS-XXX 2012, Lecture 7 12

Roadmap Motivation for a new model (done) CBV lambda calculus using substitution (done) Notes on concrete syntax Simple Lambda encodings (it is Turing complete!) Other reduction strategies Defining substitution Dan Grossman CS-XXX 2012, Lecture 7 13 Concrete-Syntax Notes We (and OCaml) 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,note 1 (e 2 e 3 ) Convince yourself application is not associative More generally: 1. Function bodies extend to an unmatched right parenthesis Example: (λx. y(λz. z)w)q 2. Application associates to the left Example: e 1 e 2 e 3 e 4 is (((e 1 e 2 ) e 3 ) e 4 ) Like in IMP, assume we really have ASTs (with non-leaves labeled λ or application ) Rules may seem strange at first, but it is the most convenient concrete syntax Based on 70 years experience Dan Grossman CS-XXX 2012, Lecture 7 14 Lambda Encodings Fairly crazy: we left out constants, conditionals, primitives, and data structures In fact, we are Turing complete and can encode whatever we need (just like assembly language can) Motivation for encodings: Fun and mind-expanding Shows we are not oversimplifying the model ( numbers are syntactic sugar ) Can show languages are too expressive (e.g., unlimited C++ template instantiation) Encodings are also just (re)definition via translation Encoding booleans The Boolean ADT There are two booleans and one conditional expression. The conditional takes 3 arguments (e.g., via currying). If the first is one boolean it evaluates to the second. If it is the other boolean it evaluates to the third. Any set of three expressions meeting this specification is a proper encoding of booleans Here is one of an infinite number of encodings: true false if λx. λy. x λx. λy. y λb. λt. λf. b t f Example: if true v 1 v 2 v 1 Dan Grossman CS-XXX 2012, Lecture 7 15 Dan Grossman CS-XXX 2012, Lecture 7 16 Evaluation Order Matters Careful: With CBV we need to thunk... diverges, but does not if true (λx. x) ((λx. x x)(λx. x x)) }{{} an infinite loop if true (λx. x) (λz. ((λx. x x)(λx. x x)) z)) }{{} a value that when called diverges Encoding Pairs The pair ADT : There is 1 constructor (taking 2 arguments) and 2 selectors 1st selector returns the 1st arg passed to the constructor 2nd selector returns the 2nd arg passed to the constructor Example: mkpair λx. λy. λz. z x y fst λp. p(λx. λy. x) snd λp. p(λx. λy. y) snd ( fst ( mkpair ( mkpair v 1 v 2 ) v 3 )) v 2 Dan Grossman CS-XXX 2012, Lecture 7 17 Dan Grossman CS-XXX 2012, Lecture 7 18

Reusing Lambdas Is it weird that the encodings of Booleans and pairs both used λx. λy. x and λx. λy. y for different purposes? Is it weird that the same bit-pattern in binary code can represent an int, a float, an instruction, or a pointer? Von Neumann: Bits can represent (all) code and data Church (?): Lambdas can represent (all) code and data Beware the Turing tarpit Encoding Lists Rather than start from scratch, notice that booleans and pairs are enough to encode lists: Empty list is mkpair false false Non-empty list is λh. λt. mkpair true ( mkpair ht) Is-empty is... Head is... Tail is... Note: Not too far from how lists are implemented Taking tail ( tail empty ) will produce some lambda Just like, without page-protection hardware, null->tail->tail would produce some bit-pattern Dan Grossman CS-XXX 2012, Lecture 7 19 Dan Grossman CS-XXX 2012, Lecture 7 20 Encoding Recursion Some programs diverge, but can we write useful loops? Yes! Write a function that takes an f and calls it in place of recursion Example (in enriched language): λf. λx. if (x =0)then 1 else (x f(x 1)) Then apply fix to it to get a recursive function: fix λf. λx. if (x =0)then 1 else (x f(x 1)) fix λf. e reduces to something roughly equivalent to e[( fix λf. e)/f], which is unrolling the recursion once (and further unrollings will happen as necessary) The details, especially for CBV, are icky; the point is it is possible and you define fix only once Not on exam: fix λg. (λx. g (λy. x x y))(λx. g (λy. x x y)) Dan Grossman CS-XXX 2012, Lecture 7 21 Encoding Arithmetic Over Natural Numbers How about arithmetic? Focus on non-negative numbers, addition, is-zero, etc. How I would do it based on what we have so far: Lists of booleans for binary numbers Zero can be the empty list Use fix to implement adders, etc. Like in hardware except fixed-width avoids recursion Or just use list length for a unary encoding Addition is list append But instead everybody always teaches Church numerals. Why? Tradition? Some sense of professional obligation? Better reason: You do not need fix: Basic arithmetic is often encodable in languages where all programs terminate In any case, we will show some basics just for fun Dan Grossman CS-XXX 2012, Lecture 7 22... Numbers encoded with two-argument functions The number i composes the first argument i times, starting with the second argument z stands for zero and s for (think unary) The trick is implementing arithmetic by cleverly passing the right arguments for s and z successor: take a number and return a number that (when called) applies s onemoretime Dan Grossman CS-XXX 2012, Lecture 7 23 Dan Grossman CS-XXX 2012, Lecture 7 24

plus: take two numbers and return a number that uses one number as the zero argument for the other λn. λm. m ( n) zero times: take two numbers m and n and pass to m a function that adds n to its argument (so this will happen m times) and zero (where to start the m iterations of addition) Dan Grossman CS-XXX 2012, Lecture 7 25 Dan Grossman CS-XXX 2012, Lecture 7 26 iszero λn. λm. m ( n) zero λn. n (λx. false ) true iszero λn. λm. m ( n) zero λn. n (λx. false ) true iszero: an easy one, see how the two arguments will lead to the correct answer predecessor minus isequal (with 0 sticky) the hard one; see Wikipedia similar to times with pred instead of plus subtract and test for zero Dan Grossman CS-XXX 2012, Lecture 7 27 Dan Grossman CS-XXX 2012, Lecture 7 28 Roadmap Motivation for a new model (done) CBV lambda calculus using substitution (done) Notes on concrete syntax (done) Simple Lambda encodings (it is Turing complete!) (done) Other reduction strategies Defining substitution Then start type systems Later take a break from types to consider first-class continuations and related topics Dan Grossman CS-XXX 2012, Lecture 7 29