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

Similar documents
Type Systems. Lecture 2 Oct. 27th, 2004 Sebastian Maneth.

Programming Languages

Review. Principles of Programming Languages. Equality. The Diamond Property. The Church-Rosser Theorem. Corollaries. CSE 230: Winter 2007

The Lambda Calculus. Stephen A. Edwards. Fall Columbia University

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

Type Systems Winter Semester 2006

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

Typed Arithmetic Expressions

Type Systems. Today. 1. What is the Lambda Calculus. 1. What is the Lambda Calculus. Lecture 2 Oct. 27th, 2004 Sebastian Maneth

λ Slide 1 Content Exercises from last time λ-calculus COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification

1. Object Calculus. Object calculus is to OO languages what lambda calculus is to functional languages

CMSC 336: Type Systems for Programming Languages Lecture 10: Polymorphism Acar & Ahmed 19 February 2008

1 Introduction. 2 Recap The Typed λ-calculus λ. 3 Simple Data Structures

CSE 505, Fall 2009, Midterm Examination 5 November Please do not turn the page until everyone is ready.

Lambda-Calculus (cont): Fixpoints, Naming. Lecture 10 CS 565 2/10/08

CSE 505, Fall 2005, Midterm Examination 8 November Please do not turn the page until everyone is ready.

COMP6463: λ-calculus

Formal Techniques for Software Engineering: Denotational Semantics

Typing λ-terms. Types. Typed λ-terms. Base Types. The Typing Relation. Advanced Formal Methods. Lecture 3: Simply Typed Lambda calculus

ITP Programming Languages Lambda Calculus Lesson 0 Functional Programming and Lambda Calculus Lesson 1 Introduction to the Lambda Calculus

Lambda-Calculus (I) 2nd Asian-Pacific Summer School on Formal Methods Tsinghua University, August 23, 2010

Type Systems Winter Semester 2006

Normalization by Evaluation

Models of computation

Extending the Lambda Calculus: An Eager Functional Language

PROBLEM SET 3: PROOF TECHNIQUES

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

Programming Language Concepts: Lecture 16

Equivalent Computers. Lecture 39: Lambda Calculus. Lambda Calculus. What is Calculus? Real Definition. Why?

Lambda Calculus. Syntax. i ::= a b c d e... etc L ::= i i L L L ( L ) A Few Examples. x x y a x a (b a) c d (c e (d e)) (x (x x)) (y (y a))

Categories, Proofs and Programs

On the Correctness and Efficiency of the Krivine Machine

CS 6110 Lecture 28 Subtype Polymorphism 3 April 2013 Lecturer: Andrew Myers

CS611 Lecture 25 Solving Domain Equations 22 October 2007 Lecturer: Andrew Myers

Simple Type Extensions

Computability Theory. CS215, Lecture 6,

The Knaster-Tarski Fixed Point Theorem for Complete Partial Orders

CS 6110 Lecture 35 Solving Domain Equations 19 April 2013 Lecturer: Andrew Myers

Type Systems. Lecture 9: Classical Logic. Neel Krishnaswami University of Cambridge

Propositional Logic: Semantics and an Example

First-Order Logic First-Order Theories. Roopsha Samanta. Partly based on slides by Aaron Bradley and Isil Dillig

CIS 500 Software Foundations Midterm II Answer key November 17, 2004

CIS 500 Software Foundations Final Exam Answer key December 20, 2004

Discrete Mathematics for CS Fall 2003 Wagner Lecture 3. Strong induction

Structural Induction

Non deterministic classical logic: the λµ ++ -calculus

Element x is R-minimal in X if y X. R(y, x).

G54FOP: Lecture 17 & 18 Denotational Semantics and Domain Theory III & IV

First-Order Logic (FOL)

Formal Methods Lecture 8. (B. Pierce's slides for the book Types and Programming Languages )

Lambda Calculus. Andrés Sicard-Ramírez. Semester Universidad EAFIT

CSCI3390-Lecture 6: An Undecidable Problem

Turing Machines Part II

(2) (15pts) Using Prolog, implement a type-checker for the following small subset of System F:

Kleene realizability and negative translations

Recursion Theorem. Ziv Scully Ziv Scully Recursion Theorem / 28

Functional Programming with Coq. Yuxin Deng East China Normal University

Lecture Notes on Data Abstraction

Mathematical Foundations of Programming. Nicolai Kraus. Draft of February 15, 2018

Semantics of Higher-Order Functional Programming

Lecture 2. Lambda calculus. Iztok Savnik, FAMNIT. March, 2018.

(a) Definition of TMs. First Problem of URMs

Lambda Calculus! Gunnar Gotshalks! LC-1

Logic and Probability Lecture 3: Beyond Boolean Logic

CS 361 Meeting 26 11/10/17

CS 2800: Logic and Computation Fall 2010 (Lecture 13)

Topic 1: Propositional logic

Programming Language Concepts: Lecture 18

Lecture 2: Syntax. January 24, 2018

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

CS156: The Calculus of Computation Zohar Manna Winter 2010

Turing Machine Recap

Programming Languages and Types

CS 301. Lecture 17 Church Turing thesis. Stephen Checkoway. March 19, 2018

September 14. Fall Software Foundations CIS 500

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

forty-two XLII Equivalent Computers? Lecture 40: Computing with Glue and Photons What is 42? cuarenta y dos Meaning of Numbers Meaning of Numbers

Decision Problems with TM s. Lecture 31: Halting Problem. Universe of discourse. Semi-decidable. Look at following sets: CSCI 81 Spring, 2012

Logic: The Big Picture

A λ-calculus with Constants and Let-blocks

Static Program Analysis

Chapter 7 Propositional Satisfiability Techniques

Applied Logic. Lecture 1 - Propositional logic. Marcin Szczuka. Institute of Informatics, The University of Warsaw

Turing machines and linear bounded automata

INTRODUCTION TO LOGIC. Propositional Logic. Examples of syntactic claims

Models of Computation,

Decidability. Human-aware Robotics. 2017/10/31 Chapter 4.1 in Sipser Ø Announcement:

Propositions. c D. Poole and A. Mackworth 2010 Artificial Intelligence, Lecture 5.1, Page 1

20.1 2SAT. CS125 Lecture 20 Fall 2016

A Lambda Calculus for Quantum Computation

1 Problem 1. (20 pts)

Database Theory VU , SS Complexity of Query Evaluation. Reinhard Pichler

Turing machines and linear bounded automata

Consistency of a Programming Logic for a Version of PCF Using Domain Theory

T Reactive Systems: Temporal Logic LTL

CS20a: Turing Machines (Oct 29, 2002)

Most General computer?

Introduction to Probabilistic Programming Language (with Church as an example) Presenter: Enrique Rosales, Xing Zeng

COMPUTER SCIENCE TRIPOS

CITS2211 Discrete Structures (2017) Cardinality and Countability

Transcription:

CS 4110 Programming Languages & Logics Lecture 16 Programming in the λ-calculus 30 September 2016

Review: Church Booleans 2 We can encode TRUE, FALSE, and IF, as: TRUE λx. λy. x FALSE λx. λy. y IF λb. λt. λf. b t f This way, IF behaves how it ought to: IF TRUE v t v f v t IF FALSE v t v f v f

Review: Church Numerals 3 Church numerals encode a number n as a function that takes f and x, and applies f to x n times. 0 λf. λx. x 1 λf. λx. f x 2 λf. λx. f (f x) We can define other functions on integers: SUCC λn. λf. λx. f (n f x)

Review: Church Numerals 3 Church numerals encode a number n as a function that takes f and x, and applies f to x n times. 0 λf. λx. x 1 λf. λx. f x 2 λf. λx. f (f x) We can define other functions on integers: SUCC λn. λf. λx. f (n f x) PLUS λn 1. λn 2. n 1 SUCC n 2

Review: Church Numerals 3 Church numerals encode a number n as a function that takes f and x, and applies f to x n times. 0 λf. λx. x 1 λf. λx. f x 2 λf. λx. f (f x) We can define other functions on integers: SUCC λn. λf. λx. f (n f x) PLUS λn 1. λn 2. n 1 SUCC n 2 TIMES λn 1. λn 2. n 1 (PLUS n 2 ) 0

Review: Church Numerals 3 Church numerals encode a number n as a function that takes f and x, and applies f to x n times. 0 λf. λx. x 1 λf. λx. f x 2 λf. λx. f (f x) We can define other functions on integers: SUCC λn. λf. λx. f (n f x) PLUS λn 1. λn 2. n 1 SUCC n 2 TIMES λn 1. λn 2. n 1 (PLUS n 2 ) 0 ISZERO λn. n (λz. FALSE) TRUE

Recursive Functions 4 How would we write recursive functions like factorial?

Recursive Functions 4 How would we write recursive functions like factorial? We d like to write it like this... FACT λn. IF (ISZERO n) 1 (TIMES n (FACT (PRED n)))

Recursive Functions 4 How would we write recursive functions like factorial? We d like to write it like this... FACT λn. IF (ISZERO n) 1 (TIMES n (FACT (PRED n))) In slightly more readable notation this is... FACT λn. if n = 0 then 1 else n FACT (n 1)...but this is an equation, not a definition!

Recursion removal trick 5 We can perform a trick to define a function FACT that satisfies the recursive equation on the previous slide.

Recursion removal trick 5 We can perform a trick to define a function FACT that satisfies the recursive equation on the previous slide. Define a new function FACT : FACT λf. λn. if n = 0 then 1 else n (f f (n 1))

Recursion removal trick 5 We can perform a trick to define a function FACT that satisfies the recursive equation on the previous slide. Define a new function FACT : FACT λf. λn. if n = 0 then 1 else n (f f (n 1)) Then define FACT as FACT applied to itself: FACT FACT FACT

Example 6 Let s try evaluating FACT on 3... FACT 3

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3 (λn. if n = 0 then 1 else n (FACT FACT (n 1))) 3

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3 (λn. if n = 0 then 1 else n (FACT FACT (n 1))) 3 if 3 = 0 then 1 else 3 (FACT FACT (3 1))

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3 (λn. if n = 0 then 1 else n (FACT FACT (n 1))) 3 if 3 = 0 then 1 else 3 (FACT FACT (3 1)) 3 (FACT FACT (3 1))

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3 (λn. if n = 0 then 1 else n (FACT FACT (n 1))) 3 if 3 = 0 then 1 else 3 (FACT FACT (3 1)) 3 (FACT FACT (3 1)) = 3 (FACT (3 1))

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3 (λn. if n = 0 then 1 else n (FACT FACT (n 1))) 3 if 3 = 0 then 1 else 3 (FACT FACT (3 1)) 3 (FACT FACT (3 1)) = 3 (FACT (3 1))... 3 2 1 1

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3 (λn. if n = 0 then 1 else n (FACT FACT (n 1))) 3 if 3 = 0 then 1 else 3 (FACT FACT (3 1)) 3 (FACT FACT (3 1)) = 3 (FACT (3 1))... 3 2 1 1 6

Example 6 Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3 (λn. if n = 0 then 1 else n (FACT FACT (n 1))) 3 if 3 = 0 then 1 else 3 (FACT FACT (3 1)) 3 (FACT FACT (3 1)) = 3 (FACT (3 1))... 3 2 1 1 6

Example Let s try evaluating FACT on 3... FACT 3 = (FACT FACT ) 3 = ((λf. λn. if n = 0 then 1 else n (f f (n 1))) FACT ) 3 (λn. if n = 0 then 1 else n (FACT FACT (n 1))) 3 if 3 = 0 then 1 else 3 (FACT FACT (3 1)) 3 (FACT FACT (3 1)) = 3 (FACT (3 1))... 3 2 1 1 6 So we have a technique for writing recursive functions: write a function f that takes itself as an argument and define f as f f. 6

Fixed point combinators 7 There is another way: fixed points!

Fixed point combinators 7 There is another way: fixed points! Consider factorial again. It is a fixed point of the following: G λf. λn. if n = 0 then 1 else n (f (n 1))

Fixed point combinators 7 There is another way: fixed points! Consider factorial again. It is a fixed point of the following: G λf. λn. if n = 0 then 1 else n (f (n 1)) Recall that if g if a fixed point of G, then G g = g. To see that any fixed point g is a real factorial function, try evaluating it: g 5

Fixed point combinators 7 There is another way: fixed points! Consider factorial again. It is a fixed point of the following: G λf. λn. if n = 0 then 1 else n (f (n 1)) Recall that if g if a fixed point of G, then G g = g. To see that any fixed point g is a real factorial function, try evaluating it: g 5 = (G g) 5

Fixed point combinators 7 There is another way: fixed points! Consider factorial again. It is a fixed point of the following: G λf. λn. if n = 0 then 1 else n (f (n 1)) Recall that if g if a fixed point of G, then G g = g. To see that any fixed point g is a real factorial function, try evaluating it: g 5 = (G g) 5 5 (g 4)

Fixed point combinators 7 There is another way: fixed points! Consider factorial again. It is a fixed point of the following: G λf. λn. if n = 0 then 1 else n (f (n 1)) Recall that if g if a fixed point of G, then G g = g. To see that any fixed point g is a real factorial function, try evaluating it: g 5 = (G g) 5 5 (g 4) = 5 ((G g) 4)

Fixed point combinators 8 How can we generate the fixed point of G? In denotational semantics, finding fixed points took a lot of math. In the λ-calculus, we just need a suitable combinator...

Y Combinator 9 The (infamous) Y combinator is defined as Y λf. (λx. f (x x)) (λx. f (x x)) We say that Y is a fixed point combinator because Y f is a fixed point of f (for any lambda term f).

Y Combinator 9 The (infamous) Y combinator is defined as Y λf. (λx. f (x x)) (λx. f (x x)) We say that Y is a fixed point combinator because Y f is a fixed point of f (for any lambda term f). What happens when we evaluate Y G under CBV?

Z Combinator 10 To avoid this issue, we ll use a slight variant of the Y combinator, called Z, which is easier to use under CBV.

Z Combinator 10 To avoid this issue, we ll use a slight variant of the Y combinator, called Z, which is easier to use under CBV. Z λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))

Example 11 Let s see Z in action, on our function G. FACT

Example 11 Let s see Z in action, on our function G. FACT = Z G

Example 11 Let s see Z in action, on our function G. FACT = Z G = (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) G

Example 11 Let s see Z in action, on our function G. FACT = Z G = (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) G (λx. G (λy. x x y)) (λx. G (λy. x x y))

Example 11 Let s see Z in action, on our function G. FACT = Z G = (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) G (λx. G (λy. x x y)) (λx. G (λy. x x y)) G (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y)

Example 11 Let s see Z in action, on our function G. FACT = Z G = (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) G (λx. G (λy. x x y)) (λx. G (λy. x x y)) G (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) = (λf. λn. if n = 0 then 1 else n (f (n 1))) (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y)

Example 11 Let s see Z in action, on our function G. FACT = Z G = (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) G (λx. G (λy. x x y)) (λx. G (λy. x x y)) G (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) = (λf. λn. if n = 0 then 1 else n (f (n 1))) (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) λn. if n = 0 then 1 else n ((λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) (n 1))

Example 11 Let s see Z in action, on our function G. FACT = Z G = (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) G (λx. G (λy. x x y)) (λx. G (λy. x x y)) G (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) = (λf. λn. if n = 0 then 1 else n (f (n 1))) (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) λn. if n = 0 then 1 else n ((λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) (n 1)) = β λn. if n = 0 then 1 else n (λy. (Z G) y) (n 1)

Example 11 Let s see Z in action, on our function G. FACT = Z G = (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) G (λx. G (λy. x x y)) (λx. G (λy. x x y)) G (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) = (λf. λn. if n = 0 then 1 else n (f (n 1))) (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) λn. if n = 0 then 1 else n ((λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) (n 1)) = β λn. if n = 0 then 1 else n (λy. (Z G) y) (n 1) = β λn. if n = 0 then 1 else n (Z G (n 1))

Example 11 Let s see Z in action, on our function G. FACT = Z G = (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) G (λx. G (λy. x x y)) (λx. G (λy. x x y)) G (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) = (λf. λn. if n = 0 then 1 else n (f (n 1))) (λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) λn. if n = 0 then 1 else n ((λy. (λx. G (λy. x x y)) (λx. G (λy. x x y)) y) (n 1)) = β λn. if n = 0 then 1 else n (λy. (Z G) y) (n 1) = β λn. if n = 0 then 1 else n (Z G (n 1)) = λn. if n = 0 then 1 else n (FACT (n 1))

Other fixed point combinators 12 There are many (indeed infinitely many) fixed-point combinators. Here s a cute one: where Y k (L L L L L L L L L L L L L L L L L L L L L L L L L L) L λabcdefghijklmnopqstuvwxyzr. (r (t h i s i s a f i x e d p o i n t c o m b i n a t o r))

Turing s Fixed Point Combinator 13 To gain some more intuition for fixed point combinators, let s derive a combinator Θ originally discovered by Turing.

Turing s Fixed Point Combinator 13 To gain some more intuition for fixed point combinators, let s derive a combinator Θ originally discovered by Turing. We know that Θ f is a fixed point of f, so we have Θ f = f (Θ f).

Turing s Fixed Point Combinator 13 To gain some more intuition for fixed point combinators, let s derive a combinator Θ originally discovered by Turing. We know that Θ f is a fixed point of f, so we have Θ f = f (Θ f). We can write the following recursive equation: Θ = λf. f (Θ f).

Turing s Fixed Point Combinator 13 To gain some more intuition for fixed point combinators, let s derive a combinator Θ originally discovered by Turing. We know that Θ f is a fixed point of f, so we have Θ f = f (Θ f). We can write the following recursive equation: Θ = λf. f (Θ f). Now use the recursion removal trick: Θ λt. λf. f (t t f) Θ Θ Θ

θ Example 14 FACT = Θ G

θ Example 14 FACT = Θ G = ((λt. λf. f (t t f)) (λt. λf. f (t t f))) G

θ Example 14 FACT = Θ G = ((λt. λf. f (t t f)) (λt. λf. f (t t f))) G (λf. f ((λt. λf. f (t t f)) (λt. λf. f (t t f)) f)) G

θ Example 14 FACT = Θ G = ((λt. λf. f (t t f)) (λt. λf. f (t t f))) G (λf. f ((λt. λf. f (t t f)) (λt. λf. f (t t f)) f)) G G ((λt. λf. f (t t f)) (λt. λf. f (t t f)) G)

θ Example 14 FACT = Θ G = ((λt. λf. f (t t f)) (λt. λf. f (t t f))) G (λf. f ((λt. λf. f (t t f)) (λt. λf. f (t t f)) f)) G G ((λt. λf. f (t t f)) (λt. λf. f (t t f)) G) = G (Θ G)

θ Example 14 FACT = Θ G = ((λt. λf. f (t t f)) (λt. λf. f (t t f))) G (λf. f ((λt. λf. f (t t f)) (λt. λf. f (t t f)) f)) G G ((λt. λf. f (t t f)) (λt. λf. f (t t f)) G) = G (Θ G) = (λf. λn. if n = 0 then 1 else n (f (n 1))) (Θ G) λn. if n = 0 then 1 else n ((Θ G) (n 1)) = λn. if n = 0 then 1 else n (FACT (n 1))

Definitional Translation 15 We know how to encode Booleans, conditionals, natural numbers, and recursion in λ-calculus. Can we define a real programming language by translating everything in it into the λ-calculus?

Definitional Translation 15 We know how to encode Booleans, conditionals, natural numbers, and recursion in λ-calculus. Can we define a real programming language by translating everything in it into the λ-calculus? In definitional translation, we define a denotational semantics where the target is a simpler programming language instead of mathematical objects.

Review: Call-by-Value Here are the syntax and CBV semantics of λ-calculus: e ::= x λx. e e 1 e 2 v ::= λx. e e 1 e 1 e e e 1 e 2 e 1 e 2 v e v e (λx. e) v e{v/x} β There are two kinds of rules: congruence rules that specify evaluation order and computation rules that specify the interesting reductions. 16

Evaluation Contexts 17 Evaluation contexts let us separate out these two kinds of rules.

Evaluation Contexts 17 Evaluation contexts let us separate out these two kinds of rules. An evaluation context E is an expression with a hole in it: a single occurrence of the special symbol [ ] in place of a subexpression. E ::= [ ] E e v E

Evaluation Contexts 17 Evaluation contexts let us separate out these two kinds of rules. An evaluation context E is an expression with a hole in it: a single occurrence of the special symbol [ ] in place of a subexpression. E ::= [ ] E e v E We write E[e] to mean the evaluation context E where the hole has been replaced with the expression e.

Examples 18 E 1 = [ ] (λx. x) E 1 [λy. y y] = (λy. y y) λx. x

Examples 18 E 1 = [ ] (λx. x) E 1 [λy. y y] = (λy. y y) λx. x E 2 = (λz. z z) [ ] E 2 [λx. λy. x] = (λz. z z) (λx. λy. x)

Examples 18 E 1 = [ ] (λx. x) E 1 [λy. y y] = (λy. y y) λx. x E 2 = (λz. z z) [ ] E 2 [λx. λy. x] = (λz. z z) (λx. λy. x) E 3 = ([ ] λx. x x) ((λy. y) (λy. y)) E 3 [λf. λg. f g] = ((λf. λg. f g) λx. x x) ((λy. y) (λy. y))

CBV With Evaluation Contexts 19 With evaluation contexts, we can define the evaluation semantics for the CBV λ-calculus with just two rules: one for evaluation contexts, and one for β-reduction.

CBV With Evaluation Contexts (λx. e) v e{v/x} β 19 With evaluation contexts, we can define the evaluation semantics for the CBV λ-calculus with just two rules: one for evaluation contexts, and one for β-reduction. With this syntax: The small-step rules are: E ::= [ ] E e v E e e E[e] E[e ]

CBN With Evaluation Contexts 20 We can also define the semantics of CBN λ-calculus with evaluation contexts.

CBN With Evaluation Contexts 20 We can also define the semantics of CBN λ-calculus with evaluation contexts. For call-by-name, the syntax for evaluation contexts is different: E ::= [ ] E e

CBN With Evaluation Contexts We can also define the semantics of CBN λ-calculus with evaluation contexts. For call-by-name, the syntax for evaluation contexts is different: E ::= [ ] E e But the small-step rules are the same: e e E[e] E[e ] (λx. e) e e{e /x} β 20

Multi-Argument λ-calculus 21 Let s define a version of the λ-calculus that allows functions to take multiple arguments. e ::= x λx 1,..., x n. e e 0 e 1... e n

Multi-Argument λ-calculus 22 We can define a CBV operational semantics: E ::= [ ] v 0... v i 1 E e i+1... e n e e E[e] E[e ] (λx 1,..., x n. e 0 ) v 1... v n e 0 {v 1 /x 1 }{v 2 /x 2 }... {v n /x n } β The evaluation contexts ensure that we evaluate multi-argument applications e 0 e 1... e n from left to right.

Definitional Translation 23 The multi-argument λ-calculus isn t any more expressive that the pure λ-calculus.

Definitional Translation 23 The multi-argument λ-calculus isn t any more expressive that the pure λ-calculus. We can define a translation T [[ ]] that takes an expression in the multi-argument λ-calculus and returns an equivalent expression in the pure λ-calculus.

Definitional Translation 23 The multi-argument λ-calculus isn t any more expressive that the pure λ-calculus. We can define a translation T [[ ]] that takes an expression in the multi-argument λ-calculus and returns an equivalent expression in the pure λ-calculus. T [[x]] = x T [[λx 1,..., x n. e]] = λx 1.... λx n. T [[e]] T [[e 0 e 1 e 2... e n ]] = (... ((T [[e 0 ]] T [[e 1 ]]) T [[e 2 ]])... T [[e n ]]) This translation curries the multi-argument λ-calculus.