CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Similar documents
CMSC 330: Organization of Programming Languages

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,

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

The Untyped Lambda Calculus

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

Lambda Calculus and Type Inference

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

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

The Untyped Lambda Calculus

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

CIS 500 Software Foundations Fall October 2

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

The University of Nottingham

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

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

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

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

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

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

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 7 CS /08/09

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

Type Systems Winter Semester 2006

Denotational Semantics. Domain Theory

Lambda Calculus as a Programming Language

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

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...

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

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

Lambda Calculus as a Programming Language

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

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

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

CIS 500 Software Foundations Fall September 25

Programming Languages Fall 2014

Introductory Example

Lesson 4 Typed Arithmetic Typed Lambda Calculus

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

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

3.1 λ-calculus: Syntax

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

Introduction to the Lambda Calculus. Chris Lomont

CSE 505: Concepts of Programming Languages

Typed Lambda Calculus and Exception Handling

Programming Languages Lecture 14: Sum, Product, Recursive Types

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

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

1.3. Conditional expressions To express case distinctions like

Formal Systems and their Applications

Chapter 5: The Untyped Lambda Calculus

1 Introduction. 3 Syntax

Programming Language Concepts: Lecture 19

Concepts of programming languages

CIS 500 Software Foundations Midterm I

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.

Denotational semantics

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

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

Fundamentals and lambda calculus

CSE-321 Programming Languages 2010 Midterm

Fundamental Concepts. Chapter 1

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

Lambda Calculus and Computation

CMSC 330: Organization of Programming Languages. Operational Semantics

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.

The Untyped Lambda Calculus

Programming Languages

On Meaning Preservation of a Calculus of Records

14.1 Encoding for different models of computation

Untyped Lambda Calculus

An Introduction to the Lambda Calculus

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

CS 6110 S14 Lecture 1 Introduction 24 January 2014

More Lambda Calculus and Intro to Type Systems

Lambda Calculus. Variables and Functions. cs3723 1

Graphical Untyped Lambda Calculus Interactive Interpreter

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

Transcription:

CMSC 330: Organization of Programming Languages Lambda Calculus Encodings CMSC330 Spring 2018 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 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 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 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 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 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 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) 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 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 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 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 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) 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 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 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 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 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 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 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) 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 21

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 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 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 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 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 27