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

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

COMP6463: λ-calculus

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

Programming Languages

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

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

Models of computation

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

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

Introduction to lambda calculus Part 2

Formal Techniques for Software Engineering: Denotational Semantics

Lambda Calculus! Gunnar Gotshalks! LC-1

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

Fundamentals of Computer Science

Introduction to Turing Machines

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

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

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

Typed Arithmetic Expressions

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

Lecture 14 Rosser s Theorem, the length of proofs, Robinson s Arithmetic, and Church s theorem. Michael Beeson

Automata Theory. Definition. Computational Complexity Theory. Computability Theory

Principles of Programming Languages

The λ-calculus and Curry s Paradox Drew McDermott , revised

Propositions as Types

Lecture 3. Lambda calculus. Iztok Savnik, FAMNIT. October, 2015.

Semantics of Higher-Order Functional Programming

Programming Language Concepts: Lecture 16

Origin in Mathematical Logic

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

Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. Radboud University Nijmegen. March 5, 2012

Type Systems Winter Semester 2006

Normalization by Evaluation

Models of Computation,

Functional Programming with Coq. Yuxin Deng East China Normal University

Introduction to lambda calculus Part 6

TYPED LAMBDA CALCULI. Andrzej S. Murawski University of λeicester.

Programming Language Concepts: Lecture 18

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

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

Typed Lambda Calculi. Nikos Tzeveλekos Queen Mary, University of London 1 / 23

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

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 16 : Turing Machines

CA320 - Computability & Complexity

Charles Wells 1. February 25, 1999

Harvard CS 121 and CSCI E-207 Lecture 4: NFAs vs. DFAs, Closure Properties

Beyond First-Order Logic

CSc 520 Principles of Programming Languages. β-reduction Introductory Example. 22: Lambda Calculus Reductions

Type Systems Winter Semester 2006

3.2 Reduction 29. Truth. The constructor just forms the unit element,. Since there is no destructor, there is no reduction rule.

Recursion Theorem. Ziv Scully Ziv Scully Recursion Theorem / 28

Origin in Mathematical Logic

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 14 : Turing Machines

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

An Introduction to the Lambda Calculus

Advanced Lambda Calculus Lecture 5

Rules and derivations in an elementary logic course arxiv: v1 [cs.lo] 7 Jan 2016

Functional Database Query Languages as. Typed Lambda Calculi of Fixed Order. Gerd G. Hillebrand and Paris C. Kanellakis

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

Church s undecidability result

Decidability: Church-Turing Thesis

Computability Theory. CS215, Lecture 6,

Introduction. Foundations of Computing Science. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Domain theory and denotational semantics of functional programming

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

CS 275 Automata and Formal Language Theory

Simple Type Extensions

Lecture 13: Turing Machine

The Lambda-Calculus Reduction System

Simply Typed Lambda-Calculi (II)

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

A Lambda Calculus for Quantum Computation

Introduction to λ-calculus

The Knaster-Tarski Fixed Point Theorem for Complete Partial Orders

Notes on induction proofs and recursive definitions

Extending the Lambda Calculus: An Eager Functional Language

The Turing machine model of computation

Type Inference. For the Simply-Typed Lambda Calculus. Peter Thiemann, Manuel Geffken. Albert-Ludwigs-Universität Freiburg. University of Freiburg

CS 275 Automata and Formal Language Theory

Computability via the Lambda Calculus with Patterns

Limits of Computation

CST Part IB. Computation Theory. Andrew Pitts

THE UNIVERSITY OF CALGARY FACULTY OF SCIENCE FINAL EXAMINATION COMPUTER SCIENCE 521

Jones-Optimal Partial Evaluation by Specialization-Safe Normalization

Theory of Computation CS3102 Spring 2014 A tale of computers, math, problem solving, life, love and tragic death

Lecture 11: Gödel s Second Incompleteness Theorem, and Tarski s Theorem

Traditional and Non Traditional lambda calculi

Foundations of Computation. Ana Bove

On the Correctness and Efficiency of the Krivine Machine

A λ-calculus with Constants and Let-blocks

Programming Languages and Types

PLC type system. if (x : t) 2 G. G ` x : t. G, x : t 1 ` M : t 2 G ` lx : t 1 (M) : t 1 t 2. if x /2 dom(g)

FIXED POINTS AND EXTENSIONALITY IN TYPED FUNCTIONAL PROGRAMMING LANGUAGES

Simply Typed λ-calculus

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

The Curry-Howard Isomorphism

Transcription:

The Lambda Calculus Stephen A. Edwards Columbia University Fall 2014

Lambda Expressions Function application written in prefix form. Add four and five is (+ 4 5) Evaluation: select a redex and evaluate it: (+ ( 5 6) ( 8 3)) (+ 30 ( 8 3)) (+ 30 24) 54 Often more than one way to proceed: (+ ( 5 6) ( 8 3)) (+ ( 5 6) 24) (+ 30 24) 54 Simon Peyton Jones, The Implementation of Functional Programming Languages, Prentice-Hall, 1987.

Function Application and Currying Function application is written as juxtaposition: f x Every function has exactly one argument. Multiple-argument functions, e.g., +, are represented by currying, named after Haskell Brooks Curry (1900 1982). So, (+ x) is the function that adds x to its argument. Function application associates left-to-right: (+ 3 4) = ((+ 3) 4) 7

Lambda Abstraction The only other thing in the lambda calculus is lambda abstraction: a notation for defining unnamed functions. (λx. + x 1) ( λ x. + x 1 ) That function of x that adds x to 1

The Syntax of the Lambda Calculus expr ::= expr expr λ variable. expr constant variable (expr) Constants are numbers and built-in functions; variables are identifiers. Function application binds more tightly than λ: λx.f g x = ( λx.(f g )x )

Beta-Reduction Evaluation of a lambda abstraction beta-reduction is just substitution: (λx. + x 1) 4 (+ 4 1) 5 The argument may appear more than once (λx. + x x) 4 (+ 4 4) 8 or not at all (λx. 3) 5 3

Beta-Reduction Fussy, but mechanical. Extra parentheses may help. ( ( ( (λx. λy. + x y) 3 4 = λx. λy. ( (+ x) y ))) ) 3 4 ( λy. ( (+ 3) y ) ) 4 ( ) (+ 3) 4 7 Functions may be arguments (λf. f 3) (λx. + x 1) (λx. + x 1) 3 (+ 3 1) 4

Free and Bound Variables (λx. + x y) 4 Here, x is like a function argument but y is like a global variable. Technically, x occurs bound and y occurs free in (λx. + x y) However, both x and y occur free in (+ x y)

Beta-Reduction More Formally (λx. E) F β E where E is obtained from E by replacing every instance of x that appears free in E with F. The definition of free and bound mean variables have scopes. Only the rightmost x appears free in (λx. + ( x 1)) x 3 so (λx. (λx. + ( x 1)) x 3) 9 (λ x. + ( x 1)) 9 3 + ( 9 1) 3 + 8 3 11

Another Example ( λx. λy. + x ( (λx. x 3) y )) ( 5 6 λy. + 5 ( (λx. x 3) y )) 6 + 5 ( (λx. x 3) 6 ) + 5 ( 6 3) + 5 3 8

Alpha-Conversion One way to confuse yourself less is to do α-conversion: renaming a λ argument and its bound variables. Formal parameters are only names: they are correct if they are consistent. (λx. (λx. + ( x 1)) x 3) 9 (λx. (λy. + ( y 1)) x 3) 9 ((λy. + ( y 1)) 9 3) (+ ( 9 1) 3) (+ 8 3) 11 You ve probably done this before in C or Java: int add(int x, int y) { return x + y; } int add(int a, int b) { return a + b; }

Beta-Abstraction and Eta-Conversion Running β-reduction in reverse, leaving the meaning of a lambda expression unchanged, is called beta abstraction: + 4 1 (λx. + x 1) 4 Eta-conversion is another type of conversion that leaves meaning unchanged: (λx. + 1 x) η (+ 1) Formally, if F is a function in which x does not occur free, (λx. F x) η F int f(int y) {... } int g(int x) { return f(x); } g(w); can be replaced with f(w)

Reduction Order The order in which you reduce things can matter. (λx. λy. y) ( (λz. z z) (λz. z z) ) Two things can be reduced: (λz. z z) (λz. z z) (λx. λy. y) ( ) However, (λz. z z) (λz. z z) (λz. z z) (λz. z z) (λx. λy. y) ( ) (λy. y)

Normal Form A lambda expression that cannot be β-reduced is in normal form. Thus, λy. y is the normal form of (λx. λy. y) ( (λz. z z) (λz. z z) ) Not everything has a normal form. E.g., (λz. z z) (λz. z z) can only be reduced to itself, so it never produces an non-reducible expression.

Normal Form Can a lambda expression have more than one normal form? Church-Rosser Theorem I: If E 1 E 2, then there exists an expression E such that E 1 E and E 2 E. Corollary. No expression may have two distinct normal forms. Proof. Assume E 1 and E 2 are distinct normal forms for E: E E 1 and E E 2. So E 1 E 2 and by the Church-Rosser Theorem I, there must exist an F such that E 1 F and E 2 F. However, since E 1 and E 2 are in normal form, E 1 = F = E 2, a contradiction.

Normal-Order Reduction Not all expressions have normal forms, but is there a reliable way to find the normal form if it exists? Church-Rosser Theorem II: If E 1 E 2 and E 2 is in normal form, then there exists a normal order reduction sequence from E 1 to E 2. Normal order reduction: reduce the leftmost outermost redex.

Normal-Order Reduction ( ( λx. ( (λw. λz. + w z) 1 )) ( (λx. x x) (λx. x x) ) ) ( (λy. + y 1) (+ 2 3) ) leftmost outermost leftmost innermost λx λx λx λy 3 λw λz 1 x x x x 1 + 2 + y + w z

Boolean Logic in the Lambda Calculus Church Booleans true = λx. λy. x false = λx. λy. y Each is a function of two arguments: true is select first; false is select second. If-then-else uses its predicate to select then or else: ifelse = λp. λa. λb. p a b E.g., ifelse true 42 58 = true 42 58 (λx. λy. x) 42 58 (λy. 42) 58 42

Boolean Logic in the Lambda Calculus Logic operators can be expressed with if-then-else: and = λp. λq. p q p or = λp. λq. p p q not = λp. λa. λb. p b a and true false = (λp. λq. p q p) true false true false true (λx. λy. x) false true false not true = (λp. λa. λb. p b a) true β λa. λb. true b a β λa. λb. b α λx. λy. y = false

Arithmetic: The Church Numerals 0 = λf. λx. x 1 = λf. λx. f x 2 = λf. λx. f (f x) 3 = λf. λx. f ( f (f x) ) I.e., for n = 0,1,2,..., n f x = f (n) (x). The successor function: succ = λn. λf. λx. f (n f x) succ 2 = ( λn. λf. λx. f (n f x) ) 2 λf. λx. f (2 f x) ( (λf ) ) = λf. λx. f. λx. f (f x) f x λf. λx. f ( f (f x) ) = 3

Adding Church Numerals Finally, we can add: plus = λm.λn.λf.λx. m f ( n f x) plus 3 2 = ( λm.λn.λf.λx. m f ( n f x) ) 3 2 λf.λx. 3 f ( 2 f x) λf.λx. f (f (f (2 f x))) λf.λx. f (f (f (f ( f x)))) = 5 Not surprising since f (m) f (n) = f (m+n)

Multiplying Church Numerals mult = λm.λn.λf.m (n f ) mult 2 3 = ( λm.λn.λf.m (n f ) ) 2 3 λf.2 (3 f ) = λf. 2 (λx. f (f (f x))) α λf. 2 (λy. f (f (f y))) λf. λx. (λy. f (f (f y))) ((λy. f (f (f y))) x) λf. λx. (λy. f (f (f y))) (f (f (f x))) λf. λx. f (f (f (f (f (f x))) )) = 6 The predecessor function is trickier since there aren t negative numbers.

Recursion Where is recursion in the lambda calculus? ( fac = λn. if (= n 0) 1 ( n ( fac ( n 1) ))) This does not work: functions are unnamed in the lambda calculus. But it is possible to express recursion as a function. fac = (λn.... fac...) β (λf. (λn.... f...)) fac = H fac That is, the factorial function, fac, is a fixed point of the (non-recursive) function H: H = λf. λn. if (= n 0) 1 ( n (f ( n 1)))

Recursion Let s invent a Y that computes fac from H, i.e., fac = Y H: fac = H fac Y H = H (Y H) fac 1 = Y H 1 = H (Y H) 1 = (λf. λn. if (= n 0) 1 ( n (f ( n 1)))) (Y H) 1 (λn. if (= n 0) 1 ( n ((Y H) ( n 1)))) 1 if (= 1 0) 1 ( 1 ((Y H) ( 1 1))) 1 (Y H 0) = 1 (H (Y H) 0) = 1 ((λf. λn. if (= n 0) 1 ( n (f ( n 1)))) (Y H) 0) 1 ((λn. if (= n 0) 1 ( n (Y H ( n 1)))) 0) 1 (if (= 0 0) 1 ( 0 (Y H ( 0 1)))) 1 1 1

The Y Combinator Here s the eye-popping part: Y can be a simple lambda expression. Y = = λf. ( λx. f (x x) ) ( λx. f (x x) ) ( Y H = λf. ( λx. f (x x) ) ( λx. f (x x) )) H ( λx. H (x x) ) ( λx. H (x x) ) ( (λx ) ( ) ). H (x x) λx. H (x x) H H ( ( λf. ( λx. f (x x) ) ( λx. f (x x) )) ) H = H (Y H) Y: The function that takes a function f and returns f (f (f (f ( ))))

Alonzo Church 1903 1995 Professor at Princeton (1929 1967) and UCLA (1967 1990) Invented the Lambda Calculus Had a few successful graduate students, including Stephen Kleene (Regular expressions) Michael O. Rabin (Nondeterministic automata) Dana Scott (Formal programming language semantics) Alan Turing (Turing machines) Turing award winners

Turing Machines vs. Lambda Calculus In 1936, Alan Turing invented the Turing machine Alonzo Church invented the lambda calculus In 1937, Turing proved that the two models were equivalent, i.e., that they define the same class of computable functions. Modern processors are just overblown Turing machines. Functional languages are just the lambda calculus with a more palatable syntax.