CMSC 330: Organization of Programming Languages

Similar documents
CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

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

CMSC 330: Organization of Programming Languages

CMSC330. Objects, Functional Programming, and lambda calculus

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

Lecture 5: The Untyped λ-calculus

Introduction to the λ-calculus

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda Calculus and Type Inference

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

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

The Untyped Lambda Calculus

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

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

CIS 500 Software Foundations Fall October 2

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

dynamically typed dynamically scoped

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

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

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

The University of Nottingham

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

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

The Untyped Lambda Calculus

Lambda Calculus. Lecture 4 CS /26/10

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

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

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

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

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

Implementing functional languages with abstract machines

Lambda Calculus and Extensions as Foundation of Functional Programming

Denotational Semantics. Domain Theory

Type Systems Winter Semester 2006

Lambda Calculus as a Programming Language

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

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

1 Introduction. 3 Syntax

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

Lambda Calculus as a Programming Language

CIS 500 Software Foundations Fall September 25

Programming Languages Fall 2014

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Introductory Example

Lesson 4 Typed Arithmetic Typed Lambda Calculus

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

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

CSE 505: Concepts of Programming Languages

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

3.1 λ-calculus: Syntax

Introduction to the Lambda Calculus. Chris Lomont

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

Typed Lambda Calculus and Exception Handling

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

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

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

Formal Systems and their Applications

1.3. Conditional expressions To express case distinctions like

Chapter 5: The Untyped Lambda Calculus

Programming Language Concepts: Lecture 19

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.

Concepts of programming languages

CIS 500 Software Foundations Midterm I

Denotational semantics

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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.

International School on Rewriting (ISR 2015) Rewriting Techniques for Correctness of Program Transformations

Fundamentals and lambda calculus

Fundamental Concepts. Chapter 1

CSE-321 Programming Languages 2010 Midterm

Lambda Calculus and Computation

CMSC 330: Organization of Programming Languages. Operational Semantics

The Untyped Lambda Calculus

Programming Languages

Untyped Lambda Calculus

λ calculus is inconsistent

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

On Meaning Preservation of a Calculus of Records

Graphical Untyped Lambda Calculus Interactive Interpreter

14.1 Encoding for different models of computation

An Introduction to the Lambda Calculus

Lambda Calculus. Variables and Functions. cs3723 1

CMPSCI 250: Introduction to Computation. Lecture #1: Things, Sets and Strings David Mix Barrington 22 January 2014

Recursive Definitions, Fixed Points and the Combinator

Transcription:

CMSC 330: Organization of Programming Languages Lambda Calculus Encodings CMSC 330 Summer 2017 1

The Power of Lambdas Despite its simplicity, the lambda calculus is quite expressive: it is Turing complete! Means we can encode any computation we want If we re sufficiently clever... Examples Booleans Pairs Natural numbers & arithmetic Looping CMSC 330 Summer 2017 2

Booleans Church s encoding of mathematical logic true = λx.λy.x false = λx.λy.y if a then b else c Ø Defined to be the expression: a b c Examples if true then b else c = (λx.λy.x) b c (λy.b) c b if false then b else c = (λx.λy.y) b c (λy.y) c c CMSC 330 Summer 2017 3

Booleans (cont.) Other Boolean operations not = λx.x false true Ø not x = x false true = if x then false else true Ø not true (λx.x false true) true (true false true) false and = λx.λy.x y false Ø and x y = if x then y else false or = λx.λy.x true y Ø or x y = if x then true else y Given these operations Can build up a logical inference system CMSC 330 Summer 2017 4

Quiz #1 What is the lambda calculus encoding of xor x y? - xor true true = xor false false = false - xor true false = xor false true = true A. x x y B. x (y true false) y C. x (y false true) y D. y x y true = λx.λy.x false = λx.λy.y if a then b else c = a b c not = λx.x false true CMSC 330 Summer 2017 5

Quiz #1 What is the lambda calculus encoding of xor x y? - xor true true = xor false false = false - xor true false = xor false true = true A. x x y B. x (y true false) y C. x (y false true) y D. y x y true = λx.λy.x false = λx.λy.y if a then b else c = a b c not = λx.x false true CMSC 330 Summer 2017 6

Pairs Encoding of a pair a, b (a,b) = λx.if x then a else b fst = λf.f true snd = λf.f false Examples fst (a,b) = (λf.f true) (λx.if x then a else b) (λx.if x then a else b) true if true then a else b a snd (a,b) = (λf.f false) (λx.if x then a else b) (λx.if x then a else b) false if false then a else b b CMSC 330 Summer 2017 7

Natural Numbers (Church* Numerals) Encoding of non-negative integers 0 = λf.λy.y 1 = λf.λy.f y 2 = λf.λy.f (f y) 3 = λf.λy.f (f (f y)) i.e., n = λf.λy.<apply f n times to y> Formally: n+1 = λf.λy.f (n f y) *(Alonzo Church, of course) CMSC 330 Summer 2017 8

Quiz #2 n = λf.λy.<apply f n times to y> What OCaml type could you give to a Churchencoded numeral? A. ( a -> b) -> a -> b B. ( a -> a) -> a -> a C. ( a -> a) -> b -> int D. (int -> int) -> int -> int CMSC 330 Summer 2017 9

Quiz #2 n = λf.λy.<apply f n times to y> What OCaml type could you give to a Churchencoded numeral? A. ( a -> b) -> a -> b B. ( a -> a) -> a -> a C. ( a -> a) -> b -> int D. (int -> int) -> int -> int CMSC 330 Summer 2017 10

Operations On Church Numerals Successor succ = λz.λf.λy.f (z f y) Example succ 0 = (λz.λf.λy.f (z f y)) (λf.λy.y) λf.λy.f ((λf.λy.y) f y) λf.λy.f ((λy.y) y) λf.λy.f y = 1 0 = λf.λy.y 1 = λf.λy.f y Since (λx.y) z y CMSC 330 Summer 2017 11

Operations On Church Numerals (cont.) IsZero? iszero = λz.z (λy.false) true This is equivalent to λz.((z (λy.false)) true) Example iszero 0 = (λz.z (λy.false) true) (λf.λy.y) (λf.λy.y) (λy.false) true (λy.y) true true Since (λx.y) z y 0 = λf.λy.y CMSC 330 Summer 2017 12

Arithmetic Using Church Numerals If M and N are numbers (as λ expressions) Can also encode various arithmetic operations Addition M + N = λf.λy.m f (N f y) Equivalently: + = λm.λn.λf.λy.m f (N f y) Ø In prefix notation (+ M N) Multiplication M * N = λf.m N f Equivalently: * = λm.λn.λf.λy.m N f y Ø In prefix notation (* M N) CMSC 330 Summer 2017 13

Arithmetic (cont.) Prove 1+1 = 2 1+1 = λx.λy.(1 x) (1 x y) = λx.λy.((λf.λy.f y) x) (1 x y) λx.λy.(λy.x y) (1 x y) λx.λy.x (1 x y) λx.λy.x ((λf.λy.f y) x y) λx.λy.x ((λy.x y) y) λx.λy.x (x y) = 2 1 = λf.λy.f y 2 = λf.λy.f (f y) With these definitions Can build a theory of arithmetic CMSC 330 Summer 2017 14

Looping & Recursion Define D = λx.x x, then D D = (λx.x x) (λx.x x) (λx.x x) (λx.x x) = D D So D D is an infinite loop In general, self application is how we get looping CMSC 330 Summer 2017 15

The Fixpoint Combinator Y = λf.(λx.f (x x)) (λx.f (x x)) Then Y F = (λf.(λx.f (x x)) (λx.f (x x))) F (λx.f (x x)) (λx.f (x x)) F ((λx.f (x x)) (λx.f (x x))) = F (Y F) Y F is a fixed point (aka fixpoint) of F Thus Y F = F (Y F) = F (F (Y F)) =... We can use Y to achieve recursion for F CMSC 330 Summer 2017 16

Example fact = λf.λn.if n = 0 then 1 else n * (f (n-1)) The second argument to fact is the integer The first argument is the function to call in the body Ø We ll use Y to make this recursively call fact (Y fact) 1 = (fact (Y fact)) 1 if 1 = 0 then 1 else 1 * ((Y fact) 0) 1 * ((Y fact) 0) = 1 * (fact (Y fact) 0) 1 * (if 0 = 0 then 1 else 0 * ((Y fact) (-1)) 1 * 1 1 CMSC 330 Summer 2017 17

Call-by-name vs. Call-by-value Sometimes we have a choice about where to apply beta reduction. Before call (i.e., argument): (λz.z) ((λy.y) x) (λz.z) x x Or after the call: (λz.z) ((λy.y) x) (λy.y) x x The former strategy is called call-by-value Evaluate any arguments before calling the function The latter is called call-by-name Delay evaluating arguments as long as possible CMSC 330 Summer 2017 18

Confluence No matter what evaluation order you choose, you get the same answer Assuming the evaluation always terminates Surprising result! However, termination behavior differs between call-by-value and call-by-name if true then true else (D D) true under call-by-name Ø true true (D D) = (λx.λy.x) true (D D) (λy.true) (D D) true if true then true else (D D) under call-by-value Ø (λx.λy.x) true (D D) (λy.true) (D D) (λy.true) (D D) never terminates CMSC 330 Summer 2017 19

Quiz #3 Y is a fixed point combinator under which evaluation order? A. Call-by-value B. Call-by-name C. Both D. Neither Y = λf.(λx.f (x x)) (λx.f (x x)) Y F = (λf.(λx.f (x x)) (λx.f (x x))) F (λx.f (x x)) (λx.f (x x)) F ((λx.f (x x)) (λx.f (x x))) = F (Y F) CMSC 330 Summer 2017 20

Quiz #3 Y is a fixed point combinator under which evaluation order? A. Call-by-value B. Call-by-name C. Both D. Neither Y = λf.(λx.f (x x)) (λx.f (x x)) Y F = (λf.(λx.f (x x)) (λx.f (x x))) F (λx.f (x x)) (λx.f (x x)) F ((λx.f (x x)) (λx.f (x x))) = F (Y F) In CBV, we expand Y F = F (Y F) = F (F (Y F)) indefinitely, for any F CMSC 330 Summer 2017 21

The Z Combinator: For CBV languages Z = λf.(λx.f (λv.x x v)) (λx.f (λv.x x v)) Then Z F x = (λf.(λx.f (λv.x x v)) (λx.f (λv.x x v))) F (λx.f (λv.x x v)) (λx.f (λv.x x v)) F (λv. (λx.f (λv.x x v)) (λx.f (λv.x x v)) v) F ((λx.f (λv.x x v)) (λx.f (λv.x x v))) = F (Z F) CMSC 330 Summer 2017 22

Discussion Lambda calculus is Turing-complete Most powerful language possible Can represent pretty much anything in real language Ø Using clever encodings But programs would be Pretty slow (10000 + 1 thousands of function calls) Pretty large (10000 + 1 hundreds of lines of code) Pretty hard to understand (recognize 10000 vs. 9999) In practice We use richer, more expressive languages That include built-in primitives CMSC 330 Summer 2017 23

The Need For Types Consider the untyped lambda calculus false = λx.λy.y 0 = λx.λy.y Since everything is encoded as a function... We can easily misuse terms Ø false 0 λy.y Ø if 0 then... because everything evaluates to some function The same thing happens in assembly language Everything is a machine word (a bunch of bits) All operations take machine words to machine words CMSC 330 Summer 2017 24

Simply-Typed Lambda Calculus (STLC) e ::= n x λx:t.e e e Added integers n as primitives Ø Need at least two distinct types (integer & function) Ø to have type errors Functions now include the type t of their argument t ::= int t t int is the type of integers t1 t2 is the type of a function Ø That takes arguments of type t1 and returns result of type t2 CMSC 330 Summer 2017 25

Types are limiting STLC will reject some terms as ill-typed, even if they will not produce a run-time error Cannot type check Y in STLC Ø Or in OCaml, for that matter! Surprising theorem: All (well typed) simply-typed lambda calculus terms are strongly normalizing A normal form is one that cannot be reduced further Ø A value is a kind of normal form Strong normalization means STLC terms always terminate Ø Proof is not by straightforward induction: Applications increase term size CMSC 330 Summer 2017 26

Summary Lambda calculus is a core model of computation We can encode familiar language constructs using only functions Ø These encodings are enlightening make you a better (functional) programmer Useful for understanding how languages work Ideas of types, evaluation order, termination, proof systems, etc. can be developed in lambda calculus, Ø then scaled to full languages CMSC 330 Summer 2017 27

Quiz 4 What is a normal form? a) The point at which an expression cannot reduce any further b) The point at which it is clear that an expression will reduce infinitely c) The original form of the lambda expression d) The form reached after one reduction CMSC 330 Summer 2017 28

Quiz 4 What is a normal form? a) The point at which an expression cannot reduce any further b) The point at which it is clear that an expression will reduce infinitely c) The original form of the lambda expression d) The form reached after one reduction CMSC 330 Summer 2017 29

Quiz 5 What feature does the (untyped) lambda calculus require to make it Turing complete? a) Types b) Natural numbers c) Fixed point combinator d) It already is Turing complete CMSC 330 Summer 2017 30

Quiz 5 What feature does the (untyped) lambda calculus require to make it Turing complete? a) Types b) Natural numbers c) Fixed point combinator d) It already is Turing complete CMSC 330 Summer 2017 31