Lambda Calculus and Computation

Similar documents
Material from Recitation 1

THE HALTING PROBLEM. Joshua Eckroth Chautauqua Nov

Source of Slides: Introduction to Automata Theory, Languages, and Computation By John E. Hopcroft, Rajeev Motwani and Jeffrey D.

Functional Languages. Hwansoo Han

Computer Sciences Department

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

Denotational semantics

4/19/2018. Chapter 11 :: Functional Languages

Chapter 11 :: Functional Languages

Cardinality of Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Cardinality of Sets Fall / 15

Time Complexity of an Algorithm

CISC 4090 Theory of Computation

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

Lecture 5: The Halting Problem. Michael Beeson

Computability, Cantor s diagonalization, Russell s Paradox, Gödel s s Incompleteness, Turing Halting Problem.

COMPUTABILITY THEORY AND RECURSIVELY ENUMERABLE SETS

p x i 1 i n x, y, z = 2 x 3 y 5 z

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

MATH Iris Loeb.

In this section we will study problems for which we can prove that there is no algorithm solving them.

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Programming Language Pragmatics

What computers just cannot do. COS 116: 2/28/2008 Sanjeev Arora

CS61A Lecture 38. Robert Huang UC Berkeley April 17, 2013

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

CSE 120. Computer Science Principles

Organization of Programming Languages CS3200/5200N. Lecture 11

Theory of Programming Languages COMP360

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

CS 5 Nightly Wrapup College Canceled

Kurt Gödel and Computability Theory

Problems, Languages, Machines, Computability, Complexity

CSCI 270: Introduction to Algorithms and Theory of Computing Fall 2017 Prof: Leonard Adleman Scribe: Joseph Bebel

Introduction to the Lambda Calculus

Typing Control. Chapter Conditionals

Does this program exist? Limits of computability. Does this program exist? A helper function. For example, if the string program is

CMSC 330: Organization of Programming Languages

Languages and Automata

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine

LECTURE 16. Functional Programming

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

COMP 382: Reasoning about algorithms

Lecture T4: Computability

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

6.045J/18.400J: Automata, Computability and Complexity. Practice Quiz 2

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.

CMSC 330: Organization of Programming Languages

Introduction to the Theory of Computation, Sipser, PWS, ISBN X, 1996

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

CMSC 330: Organization of Programming Languages

Polymorphism and Type Inference

Chapter 5: The Untyped Lambda Calculus

Outline. Language Hierarchy

1.7 The Heine-Borel Covering Theorem; open sets, compact sets

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

Functional Programming

CMSC 330: Organization of Programming Languages

Introduction to the λ-calculus

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

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

1. [5 points each] True or False. If the question is currently open, write O or Open.

Functional Programming. Big Picture. Design of Programming Languages

ALGORITHMIC DECIDABILITY OF COMPUTER PROGRAM-FUNCTIONS LANGUAGE PROPERTIES. Nikolay Kosovskiy

Computability Summary

6.004 Computation Structures Spring 2009

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

Functional Programming. Pure Functional Languages

MATHEMATICAL STRUCTURES FOR COMPUTER SCIENCE

Writing code that I'm not smart enough to write. A funny thing happened at Lambda Jam

Universal Turing Machine Chomsky Hierarchy Decidability Reducibility Uncomputable Functions Rice s Theorem Decidability Continued

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Designing Sequential Logic Sequential logic is used when the solution to some design problem involves a sequence of steps:

Introduction to the Lambda Calculus. Chris Lomont

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

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

Recursively Enumerable Languages, Turing Machines, and Decidability

Problem Set 4: Streams and Lazy Evaluation

05. Turing Machines and Spacetime. I. Turing Machines and Classical Computability.

Last class. CS Principles of Programming Languages. Introduction. Outline

CMSC 330: Organization of Programming Languages. Lambda Calculus

Course notes for Data Compression - 2 Kolmogorov complexity Fall 2005

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

Exam 2. cs3102: Theory of Computation. Class 20: Busy Beavers

Rice s Theorem and Enumeration

Software Paradigms (Lesson 4) Functional Programming Paradigm

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

Theory of Languages and Automata

Infinity and Uncountability. Countable Countably infinite. Enumeration

Today, we will dispel the notion that Java is a magical language that allows us to solve any problem we want if we re smart enough.

Turing Machine Languages

CSE 105 THEORY OF COMPUTATION

CSE 505: Concepts of Programming Languages

Special Topics: Programming Languages

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

Transcription:

6.037 Structure and Interpretation of Computer Programs Chelsea Voss csvoss@mit.edu Massachusetts Institute of Technology With material from Mike Phillips and Nelson Elhage February 1, 2018

Limits to Computation David Hilbert s Entscheidungsproblem (1928)

Limits to Computation David Hilbert s Entscheidungsproblem (1928): can calculating machines give a yes or no answer to all mathematical questions?

Limits to Computation David Hilbert s Entscheidungsproblem (1928): can calculating machines give a yes or no answer to all mathematical questions? Figure : Alonzo Church (1903-1995), lambda calculus Figure : Alan Turing (1912-1954), Turing machines

Limits to Computation David Hilbert s Entscheidungsproblem (1928): can calculating machines give a yes or no answer to all mathematical questions? Figure : Alonzo Church (1903-1995), lambda calculus Figure : Alan Turing (1912-1954), Turing machines Theorem (Church, Turing, 1936): These models of computation can t solve every problem.

Limits to Computation David Hilbert s Entscheidungsproblem (1928): can calculating machines give a yes or no answer to all mathematical questions? Figure : Alonzo Church (1903-1995), lambda calculus Figure : Alan Turing (1912-1954), Turing machines Theorem (Church, Turing, 1936): These models of computation can t solve every problem. Proof: next!

Equivalence of Computation Methods First part of the proof: Church-Turing thesis.

Equivalence of Computation Methods First part of the proof: Church-Turing thesis. Any intuitive notion for a computer that you can come up with will be no more powerful than a Turing machine or than lambda calculus. That is, most models of computation are equivalent.

Equivalence of Computation Methods First part of the proof: Church-Turing thesis. Any intuitive notion for a computer that you can come up with will be no more powerful than a Turing machine or than lambda calculus. That is, most models of computation are equivalent. Turing-complete means capable of simulating Turing machines.

Equivalence of Computation Methods First part of the proof: Church-Turing thesis. Any intuitive notion for a computer that you can come up with will be no more powerful than a Turing machine or than lambda calculus. That is, most models of computation are equivalent. Turing-complete means capable of simulating Turing machines. Lambda calculus is Turing-complete (proof: later), and Turing machines can simulate lambda calculus.

Equivalence of Computation Methods First part of the proof: Church-Turing thesis. Any intuitive notion for a computer that you can come up with will be no more powerful than a Turing machine or than lambda calculus. That is, most models of computation are equivalent. Turing-complete means capable of simulating Turing machines. Lambda calculus is Turing-complete (proof: later), and Turing machines can simulate lambda calculus. Some others: Turing machines are Turing-complete

Equivalence of Computation Methods First part of the proof: Church-Turing thesis. Any intuitive notion for a computer that you can come up with will be no more powerful than a Turing machine or than lambda calculus. That is, most models of computation are equivalent. Turing-complete means capable of simulating Turing machines. Lambda calculus is Turing-complete (proof: later), and Turing machines can simulate lambda calculus. Some others: Turing machines are Turing-complete Scheme is Turing-complete

Equivalence of Computation Methods First part of the proof: Church-Turing thesis. Any intuitive notion for a computer that you can come up with will be no more powerful than a Turing machine or than lambda calculus. That is, most models of computation are equivalent. Turing-complete means capable of simulating Turing machines. Lambda calculus is Turing-complete (proof: later), and Turing machines can simulate lambda calculus. Some others: Turing machines are Turing-complete Scheme is Turing-complete Minecraft is Turing-complete

Equivalence of Computation Methods First part of the proof: Church-Turing thesis. Any intuitive notion for a computer that you can come up with will be no more powerful than a Turing machine or than lambda calculus. That is, most models of computation are equivalent. Turing-complete means capable of simulating Turing machines. Lambda calculus is Turing-complete (proof: later), and Turing machines can simulate lambda calculus. Some others: Turing machines are Turing-complete Scheme is Turing-complete Minecraft is Turing-complete Conway s Game of Life is Turing-complete

Equivalence of Computation Methods First part of the proof: Church-Turing thesis. Any intuitive notion for a computer that you can come up with will be no more powerful than a Turing machine or than lambda calculus. That is, most models of computation are equivalent. Turing-complete means capable of simulating Turing machines. Lambda calculus is Turing-complete (proof: later), and Turing machines can simulate lambda calculus. Some others: Turing machines are Turing-complete Scheme is Turing-complete Minecraft is Turing-complete Conway s Game of Life is Turing-complete Wolfram s Rule 110 cellular automaton is Turing-complete

Does not compute? Are there problems which our notion of computing cannot solve?

Does not compute? Are there problems which our notion of computing cannot solve? Reworded: are there functions that cannot be computed?

Does not compute? Are there problems which our notion of computing cannot solve? Reworded: are there functions that cannot be computed? Consider functions which map integers to integers.

Does not compute? Are there problems which our notion of computing cannot solve? Reworded: are there functions that cannot be computed? Consider functions which map integers to integers. Can write out a function f as the infinite list of integers f (0), f (1), f (2)...

Does not compute? Are there problems which our notion of computing cannot solve? Reworded: are there functions that cannot be computed? Consider functions which map integers to integers. Can write out a function f as the infinite list of integers f (0), f (1), f (2)... Any program text can be written as a single number, joining together this list

Does not compute? Suppose, for contradiction, you ve made a program to compute each possible function

Does not compute? Suppose, for contradiction, you ve made a program to compute each possible function Put them in a big table, one function per row, one input per column

Does not compute? Suppose, for contradiction, you ve made a program to compute each possible function Put them in a big table, one function per row, one input per column Diagonalize!

Does not compute? Suppose, for contradiction, you ve made a program to compute each possible function Put them in a big table, one function per row, one input per column Diagonalize! We get a contradiction: here s a function that s not in your list.

Does not compute? Suppose, for contradiction, you ve made a program to compute each possible function Put them in a big table, one function per row, one input per column Diagonalize! We get a contradiction: here s a function that s not in your list. Theorem (Church, Turing): These models of computation can t solve every problem.

How many uncomputable problems? Countably infinite: ℵ 0

How many uncomputable problems? Countably infinite: ℵ 0 The number of integers

How many uncomputable problems? Countably infinite: ℵ 0 The number of integers The number of binary strings

How many uncomputable problems? Countably infinite: ℵ 0 The number of integers The number of binary strings The number of programs

How many uncomputable problems? Countably infinite: ℵ 0 The number of integers The number of binary strings The number of programs Uncountably infinite: 2 ℵ 0

How many uncomputable problems? Countably infinite: ℵ 0 The number of integers The number of binary strings The number of programs Uncountably infinite: 2 ℵ 0 The number of functions mapping from integer to integer

How many uncomputable problems? Countably infinite: ℵ 0 The number of integers The number of binary strings The number of programs Uncountably infinite: 2 ℵ 0 The number of functions mapping from integer to integer The number of sets of binary strings

How many uncomputable problems? Countably infinite: ℵ 0 The number of integers The number of binary strings The number of programs Uncountably infinite: 2 ℵ 0 The number of functions mapping from integer to integer The number of sets of binary strings The number of problem specifications

Does not compute: Halting Problem Okay, but can you give me an example?

Does not compute: Halting Problem Okay, but can you give me an example? We ve seen our programs create infinite lists and infinite loops

Does not compute: Halting Problem Okay, but can you give me an example? We ve seen our programs create infinite lists and infinite loops Can we write a program to check if an expression will return a value?

Does not compute: Halting Problem Okay, but can you give me an example? We ve seen our programs create infinite lists and infinite loops Can we write a program to check if an expression will return a value? (define (halt? p) ;... )

Aside: what does this do? ((lambda (x) (x x)) (lambda (x) (x x)))

Aside: what does this do? ((lambda (x) (x x)) (lambda (x) (x x))) = ((lambda (x) (x x)) (lambda (x) (x x)))

Aside: what does this do? ((lambda (x) (x x)) (lambda (x) (x x))) = ((lambda (x) (x x)) (lambda (x) (x x))) = ((lambda (x) (x x)) (lambda (x) (x x)))

Aside: what does this do? ((lambda (x) (x x)) (lambda (x) (x x))) = ((lambda (x) (x x)) (lambda (x) (x x))) = ((lambda (x) (x x)) (lambda (x) (x x))) =...

Does not compute: Halting Problem Contradiction!

Does not compute: Halting Problem Contradiction! (define (troll) (if (halt? troll) ; if halts? says we halt, infinite-loop ((lambda (x) (x x)) (lambda (x) (x x))) ; if halts? says we dont, return a value \#f))

Does not compute: Halting Problem Contradiction! (define (troll) (if (halt? troll) ; if halts? says we halt, infinite-loop ((lambda (x) (x x)) (lambda (x) (x x))) ; if halts? says we dont, return a value \#f)) (halt? troll)

Does not compute: Halting Problem Contradiction! (define (troll) (if (halt? troll) ; if halts? says we halt, infinite-loop ((lambda (x) (x x)) (lambda (x) (x x))) ; if halts? says we dont, return a value \#f)) (halt? troll) Halting Problem is undecidable for Turing Machines and thus all programming languages. (Turing, 1936)

Does not compute: Halting Problem Contradiction! (define (troll) (if (halt? troll) ; if halts? says we halt, infinite-loop ((lambda (x) (x x)) (lambda (x) (x x))) ; if halts? says we dont, return a value \#f)) (halt? troll) Halting Problem is undecidable for Turing Machines and thus all programming languages. (Turing, 1936) Want to learn more computability theory? See 18.400J/6.045J or 18.404J/6.840J (Sipser).

The Source of Power What s the minimal set of Scheme syntax that you need to achieve Turing-completeness?

The Source of Power What s the minimal set of Scheme syntax that you need to achieve Turing-completeness? define set! numbers strings if recursion cons booleans lambda

Cons cells? (define (cons a b) (lambda (c) (c a b)))

Cons cells? (define (cons a b) (lambda (c) (c a b))) (define (car p) (p (lambda (a b) a)))

Cons cells? (define (cons a b) (lambda (c) (c a b))) (define (car p) (p (lambda (a b) a))) (define (cdr p) (p (lambda (a b) b)))

Booleans? (define true (lambda (a) (lambda (b) a)))

Booleans? (define true (lambda (a) (lambda (b) a))) (define false (lambda (a) (lambda (b) b)))

Booleans? (define true (lambda (a) (lambda (b) a))) (define false (lambda (a) (lambda (b) b))) (define if (lambda (test then else) ((test then) else))

Booleans? (define true (lambda (a) (lambda (b) a))) (define false (lambda (a) (lambda (b) b))) (define if (lambda (test then else) ((test then) else)) Also try: and, or, not

Numbers? Number N: A procedure which takes in a successor function s and a zero z, and returns the successor applied to the zero N times. For example, 3 is represented as (s (s (s z))), given s and z This technique: Church numerals

Numbers? (define (church-0 (lambda (s) (lambda (z) z)))

Numbers? (define (church-0 (lambda (s) (lambda (z) z))) (define (church-1 (lambda (s) (lambda (z) (s z))))

Numbers? (define (church-0 (lambda (s) (lambda (z) z))) (define (church-1 (lambda (s) (lambda (z) (s z)))) (define (church-2 (lambda (s) (lambda (z) (s (s z)))))

Numbers? (define (church-inc n) (lambda (s) (lambda (z) (s ((n s) z))))))

Numbers? (define (church-inc n) (lambda (s) (lambda (z) (s ((n s) z)))))) (define (church-add a b) (lambda (s) (lambda (z) ((a s) ((b s) z))))

Numbers? (define (church-inc n) (lambda (s) (lambda (z) (s ((n s) z)))))) (define (church-add a b) (lambda (s) (lambda (z) ((a s) ((b s) z)))) (define (also-church-add a b) ((a church-inc) b))))

Numbers? (define (church-inc n) (lambda (s) (lambda (z) (s ((n s) z)))))) (define (church-add a b) (lambda (s) (lambda (z) ((a s) ((b s) z)))) (define (also-church-add a b) ((a church-inc) b)))) For fun: Write decrement, write multiply.

Let, define? Use lambdas.

Let, define? Use lambdas. (define x 4) (...stuff)

Let, define? Use lambdas. (define x 4) (...stuff) becomes... ((lambda (x) (...stuff) ) 4)

Let, define? A problem arises! (define (fact n) (if (= n 0) 1 (* n (fact (- n 1)))))

Let, define? A problem arises! (define (fact n) (if (= n 0) 1 (* n (fact (- n 1))))) Why? (lambda (fact)...) (...definition of fact...) fails! fact is not yet defined when called in its function body.

Let, define? A problem arises! (define (fact n) (if (= n 0) 1 (* n (fact (- n 1))))) Why? (lambda (fact)...) (...definition of fact...) fails! fact is not yet defined when called in its function body. If we can t name fact how do we use it in the recursive call?

Factorial again Run it with a copy of itself. (define (fact inner-fact n) (if (= n 0) 1 (* n (inner-fact inner-fact (- n 1)))))

Factorial again Run it with a copy of itself. (define (fact inner-fact n) (if (= n 0) 1 (* n (inner-fact inner-fact (- n 1))))) Now, (fact fact 4) works!

Now without define (fact fact 4) becomes:

Now without define (fact fact 4) becomes: ((lambda (fact n) (if (= n 0) 1 (* n (fact fact (- n 1))))) (lambda (fact n) (if (= n 0) 1 (* n (fact fact (- n 1))))) 4)

Messy. Can we do better? Let s define fact-inner as:

Messy. Can we do better? Let s define fact-inner as: (lambda (fact) (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))))

Messy. Can we do better? Let s define fact-inner as: (lambda (fact) (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) Huh - what s (fact-inner fact)?

Messy. Can we do better? Let s define fact-inner as: (lambda (fact) (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) Huh - what s (fact-inner fact)? (fact-inner fact) = fact.

Messy. Can we do better? Let s define fact-inner as: (lambda (fact) (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) Huh - what s (fact-inner fact)? (fact-inner fact) = fact. A fixed point!

Producing Fixed Points Now let s define Y as: (lambda (f) ((lambda (g) (f (g g))) (lambda (g) (f (g g))))) We ll prove that (Y f) = (f (Y f))

Producing Fixed Points Now let s define Y as: (lambda (f) ((lambda (g) (f (g g))) (lambda (g) (f (g g))))) We ll prove that (Y f) = (f (Y f)) that we can use Y to create fixed points.

Producing Fixed Points From the problem before: we want (fact-inner fact).

Producing Fixed Points From the problem before: we want (fact-inner fact). (define Y (lambda (f) ((lambda (g) (f (g g))) (lambda (g) (f (g g))))) ;; For convenience: ;; H := (lambda (g) (f (g g))) ;; Is (fact-inner fact) = (Y fact-inner)? ;; (Y fact-inner) ;; = (H H) ; (with f = fact-inner) ;; = (fact-inner (g g)) ;; = (fact-inner (H H)) ;; = (fact-inner (Y fact-inner)) ;; = (fact-inner fact) ; Success!

Producing Fixed Points Now we can define fact as follows:

Producing Fixed Points Now we can define fact as follows: (Y (lambda (fact-inner) (lambda (n) (if (= n 0) 1 (* n (fact-inner (- n 1)))))))

Producing Fixed Points Now we can define fact as follows: (Y (lambda (fact-inner) (lambda (n) (if (= n 0) 1 (* n (fact-inner (- n 1))))))) Can create fact without using define!

Producing Fixed Points Now we can define fact as follows: (Y (lambda (fact-inner) (lambda (n) (if (= n 0) 1 (* n (fact-inner (- n 1))))))) Can create fact without using define! Can create all of Scheme using just lambda!

Producing Fixed Points Now we can define fact as follows: (Y (lambda (fact-inner) (lambda (n) (if (= n 0) 1 (* n (fact-inner (- n 1))))))) Can create fact without using define! Can create all of Scheme using just lambda! Lambda calculus is Turing-complete!

Producing Fixed Points Now we can define fact as follows: (Y (lambda (fact-inner) (lambda (n) (if (= n 0) 1 (* n (fact-inner (- n 1))))))) Can create fact without using define! Can create all of Scheme using just lambda! Lambda calculus is Turing-complete! Church-Turing thesis!

Fun links https://xkcd.com/505/ http://www.lel.ed.ac.uk/~gpullum/loopsnoop.html https://youtu.be/1x21hqphy6i https://youtu.be/my8asv7ba94 https://youtu.be/xp5-iiekxe8 https://en.wikipedia.org/wiki/rule_110