The λ-calculus. Lecturer: John Wickerson. Phil Wadler

Σχετικά έγγραφα
About these lecture notes. Simply Typed λ-calculus. Types

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

The Simply Typed Lambda Calculus

C.S. 430 Assignment 6, Sample Solutions

From the finite to the transfinite: Λµ-terms and streams

Foundations of Computer Science ENGR 3520 Fall 2013 Thursday, Nov 21, 2013

A Lambda Model Characterizing Computational Behaviours of Terms

derivation of the Laplacian from rectangular to spherical coordinates

2 Composition. Invertible Mappings

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

λρ-calculus 1. each λ-variable is a λρ-term, called an atom or atomic term; 2. if M and N are λρ-term then (MN) is a λρ-term called an application;

x E[x] x xµº λx. E[x] λx. x 2 3x +2

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Lecture 2. Soundness and completeness of propositional logic

Finitary proof systems for Kozen s µ

THE CALL-BY-NEED LAMBDA CALCULUS, REVISITED

Fractional Colorings and Zykov Products of graphs

Models for Probabilistic Programs with an Adversary

Introduction to Type Theory February 2008 Alpha Lernet Summer School Piriapolis, Uruguay. Herman Geuvers Nijmegen & Eindhoven, NL

Chapter 3: Ordinal Numbers

Nowhere-zero flows Let be a digraph, Abelian group. A Γ-circulation in is a mapping : such that, where, and : tail in X, head in

Every set of first-order formulas is equivalent to an independent set

Proving with Computer Assistance Lecture 2. Herman Geuvers

Section 9.2 Polar Equations and Graphs

Αλγόριθμοι και πολυπλοκότητα NP-Completeness (2)

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

Numerical Analysis FMN011

Sequent Calculi for the Modal µ-calculus over S5. Luca Alberucci, University of Berne. Logic Colloquium Berne, July 4th 2008

k A = [k, k]( )[a 1, a 2 ] = [ka 1,ka 2 ] 4For the division of two intervals of confidence in R +

Finite Field Problems: Solutions

Example Sheet 3 Solutions

Formal Semantics. 1 Type Logic

Type Theory and Coq. Herman Geuvers. Principal Types and Type Checking

TMA4115 Matematikk 3

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

CS-XXX: Graduate Programming Languages. Lecture 27 Higher-Order Polymorphism. Matthew Fluet 2012

Homework 3 Solutions

ORDINAL ARITHMETIC JULIAN J. SCHLÖDER

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

Homomorphism in Intuitionistic Fuzzy Automata

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

PARTIAL NOTES for 6.1 Trigonometric Identities

Lambda calculus. Part II Lambda Calculi with Types

Fourier Series. MATH 211, Calculus II. J. Robert Buchanan. Spring Department of Mathematics

Matrices and Determinants

The ε-pseudospectrum of a Matrix

5. Choice under Uncertainty

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

A Note on Intuitionistic Fuzzy. Equivalence Relation

CRASH COURSE IN PRECALCULUS

EE512: Error Control Coding

Second Order Partial Differential Equations

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

Other Test Constructions: Likelihood Ratio & Bayes Tests

The Pohozaev identity for the fractional Laplacian

Section 8.3 Trigonometric Equations

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

Theorem 8 Let φ be the most powerful size α test of H

Statistical Inference I Locally most powerful tests

Κεφάλαιο 10 Λάμβδα λογισμός

Solutions to Exercise Sheet 5

Bounding Nonsplitting Enumeration Degrees

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

T λx. (λy. x) F λx. (λy. y) if λpca. pca

MINIMAL CLOSED SETS AND MAXIMAL CLOSED SETS

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

Chapter 6: Systems of Linear Differential. be continuous functions on the interval

If we restrict the domain of y = sin x to [ π, π ], the restrict function. y = sin x, π 2 x π 2

Εγχειρίδια Μαθηµατικών και Χταποδάκι στα Κάρβουνα

Exercises 10. Find a fundamental matrix of the given system of equations. Also find the fundamental matrix Φ(t) satisfying Φ(0) = I. 1.

Homework 8 Model Solution Section

If we restrict the domain of y = sin x to [ π 2, π 2

Αλγόριθμοι και πολυπλοκότητα NP-Completeness

Lecture 15 - Root System Axiomatics

Abstract Storage Devices

Section 7.6 Double and Half Angle Formulas

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

Introduction to Type Theory February 2008 Alpha Lernet Summer School Piriapolis, Uruguay. Herman Geuvers Nijmegen & Eindhoven, NL

Uniform Convergence of Fourier Series Michael Taylor

Math221: HW# 1 solutions

2. THEORY OF EQUATIONS. PREVIOUS EAMCET Bits.

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

forms This gives Remark 1. How to remember the above formulas: Substituting these into the equation we obtain with

Problem Set 3: Solutions

ST5224: Advanced Statistical Theory II

Lecture 21: Properties and robustness of LSE

Commutative Monoids in Intuitionistic Fuzzy Sets

Instruction Execution Times

2. Let H 1 and H 2 be Hilbert spaces and let T : H 1 H 2 be a bounded linear operator. Prove that [T (H 1 )] = N (T ). (6p)

Parametrized Surfaces

Congruence Classes of Invertible Matrices of Order 3 over F 2

Chap. 6 Pushdown Automata

Arithmetical applications of lagrangian interpolation. Tanguy Rivoal. Institut Fourier CNRS and Université de Grenoble 1

Spherical Coordinates

1. Introduction and Preliminaries.

Solution Series 9. i=1 x i and i=1 x i.

12. Radon-Nikodym Theorem

Physical DB Design. B-Trees Index files can become quite large for large main files Indices on index files are possible.

SOLUTIONS TO MATH38181 EXTREME VALUES AND FINANCIAL RISK EXAM

Transcript:

The λ-calculus Lecturer: John Wickerson Phil Wadler

A tiny bit of Java expr ::= expr + expr expr < expr x n block ::= cmd { cmd... cmd } cmd ::= expr; if(cmd) block else block; if(cmd) block; try{cmd} finally{cmd... cmd}; while(cmd) block;... 2

A tiny bit of Haskell expr ::= expr + expr expr < expr x n let x = expr in expr if expr then expr else expr expr expr \x. expr expr : expr [] true false (expr, expr) 3

The whole λ-calculus M ::= λx. M M M x 4

The λ-calculus is... The simplest programming language in the world A training ground for studying other programming languages 5

Outline 1. Syntax (free variables, α-equivalence, substitution) 2. Semantics (-reduction, confluence, reduction strategies) 3. Usage (encoding arithmetic, recursion) 6

Examples M ::= λx.m M M λx. x x λx. λy. λz. M = λxyz. M λx. y (λx. x)(λy. y) M1 M2 M3 = (M1 M2) M3 λx. λy. λz. x (λx. x)(λy. λz. x (y z))(λx. y x x) 7

KEY CONCEPT λx. x y binder free variable bound variable 8

Free variables Let FV(M) denote the set of free variables in the λ-term M. For instance: FV(λx. y) = {y}. If FV(M) = then we say M is "closed". 9

α-equivalence For example: (λx. x) =α (λy. y) int i; for(i=0; i<5; i++) x+=i; (λx. x (λy. y) y) =α? (λy. y (λx. x) x) (λx. x (λy. y) y) =α? (λy. y (λx. x) y) (λx. x (λy. y) y) =α? (λw. w (λw. w) y) (λz. z (λy. y) y) =α? (λz. z (λw. w) y) (λz. z (λz. z) y) =α? (λz. z (λz. z) y) 10

α-equivalence M ::= λx.m M M x M =α M' N =α N' x =α x M N =α M' N' z FV(M) FV(N) M[z/x] =α N[z/y] (λx. M) =α (λy. N) 11

Substitution Replace e with 2.718 in try { f.writefloat(e ^ 2); } catch (IOException e) { e.printstacktrace(); } 12

Substitution let x= 4*y in let y=w+5 in x*y 13

Substitution let y=w+5 4*y in *y 14

Substitution let x= 4*y in let z=w+5 in x*z 15

Substitution let z=w+5 4*y in *z 16

y[m/x] = Substitution M if y=x {y if y x M ::= λx.m M M x (λy. N)[M/x] = { λy. N λz. N[z/y][M/x] if y=x if y x where z FV(M) (FV(N) - {y}) {x} (N1 N2)[M/y] = (N1[M/y])(N2[M/y]) 17

KEY CONCEPTS α-equivalence capture-avoiding substitution 18

BONUS DeBruijn indices let x=4*y in let z=w+x in x*z let =4*y in let =w+ in * 19

BONUS DeBruijn indices λx. x λ. 0 λx. y λ. y (λx. x)(λy. y) (λ. 0)(λ. 0) λx. λy. λz. x λ. λ. λ. 2 (λx. x)(λy. λz. x (y z))(λx. y x x) (λ. 0)(λ. λ. x (1 0))(λ. y 0 0) 20

Outline 1. Syntax (free variables, α-equivalence, substitution) 2. Semantics (-reduction, confluence, reduction strategies) 3. Usage (encoding arithmetic, recursion) 21

Java semantics (C, σ) (C', σ') (if(c)s, σ) (if(c')s, σ') (if(true)s, σ) (S, σ) (if(false)s, σ) (skip, σ) 22

λ-calculus semantics "redex" (λx. M) N M[N/x] M M' M M' N N' λx. M λx. M' M N M' N M N M N' M =α M' M' N' N' =α N M N 23

-reduction examples (λx. x x) ((λx. y) z) 24

-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) 25

-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) y ((λx. y) z) 26

-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) y ((λx. y) z) y y 27

-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) ((λx. y) z) y y ((λx. y) z) y y 28

-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) ((λx. y) z) y y ((λx. y) z) y y 29

-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) (λx. x x) y ((λx. y) z) y y ((λx. y) z) y y 30

-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) (λx. x x) y ((λx. y) z) y y ((λx. y) z) y y 31

KEY CONCEPTS -reduction: (λx. M) N M[N/x] 32

Many steps of -reduction Define * as follows: M =α M' M M'' M'' * M' M * M' M * M' 33

Confluence Theorem (Church Rosser). If M * M1 and M * M2 then there exists M' such that M1 * M' and M2 * M'. M * M1* M2 * * M' 34

-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) (λx. x x) y ((λx. y) z) y y ((λx. y) z) y y 35

-normal form "in -normal form" = "contains no redexes" M has a -normal form if M * N for some N in -normal form. Theorem (Uniqueness of -normal forms). If M * N1, M * N2, and N1 and N2 are in -normal form, then N1 =α N2. 36

-normal form Theorem (Uniqueness of -normal forms). If M * N1, M * N2, and N1 and N2 are in -normal form, then N1 =α N2. Proof. By Church Rosser, obtain N such that N1 * N and N2 * N. But N1 and N2 are in -normal form, so N1 =α N =α N2. 37

-equivalence = is the smallest equivalence relation containing. M4 M1 M3 M5 M2 * * M' Simpler version. M 1 = M 2 iff there exists M' such that M 1 * M' and M 2 * M'. 38

KEY CONCEPTS -reduction confluence (Church Rosser) -normal form -equivalence 39

Non-termination Some terms do not have a -normal form. For instance: (λx. x x)(λx. x x) (λx. x x)(λx. x x)... 40

Possible non-termination Some terms might not terminate. For instance: (λx. y)((λx. x x)(λx. x x)) y (λx. y)((λx. x x)(λx. x x)) y... 41

Reduction strategies (λx. M) N Substitute N for x? "call by name" Reduce N? "call by value" Reduce M? not usually done 42

Full -reduction Call by name Call by value M M' λx. M λx. M' M M' M N M' N M N M' M N N M' N M V M' M N V M' N N N' M V / N V N' M N M N' M N V M N' (λx. M) N M[N/x] (λx. M) N N M[N/x] N V / (λx. M) N V M[N/x] 43

Reduction strategies (λx. x x) ((λx. y) z) N V ((λx. y) z) ((λx. y) z) N ((λx. y) z) y y ((λx. y) z) N V (λx. x x) y y y 44

Reduction strategies (λx. y)((λx. x x)(λx. x x)) N V y (λx. y)((λx. x x)(λx. x x)) N V y... 45

KEY CONCEPTS call-by-name reduction (wins if argument is unused) call-by-value reduction (wins if argument is used more than once) 46

BONUS Extensionality Is -equivalence the best notion of "equality" between λ- terms? We don't have (λx. sin x) = sin. But we do have (λx. sin x) M = sin M, for any M. Add η-equivalence: x FV(M) (λx. M x) =η M η-equivalence captures "equality" nicely. 47

BONUS Proving Church Rosser -reduction? * * * * 48

BONUS Proving Church Rosser -reduction?? * * * * 49

BONUS Proving Church Rosser -reduction? * * * * 50

BONUS Proving Church Rosser 51

BONUS Proving Church Rosser -reduction? * * * * 52

BONUS Proving Church Rosser -reduction * * * * 53

BONUS Proving Church Rosser -reduction?? * * * * * * 54

BONUS Proving Church Rosser -reduction? * * * * * * 55

BONUS Proving Church Rosser -reduction * * * * * * 56

BONUS Proving Church Rosser -reduction * * * * * * ("Property A") ("Property B") 57

58

59

BONUS Proving Church Rosser -reduction * * * * * * 60

BONUS Proving Church Rosser -reduction? * * * * 61

Outline 1. Syntax (free variables, α-equivalence, substitution) 2. Semantics (-reduction, confluence, reduction strategies) 3. Usage (encoding arithmetic, recursion) 62

Encoding numbers 0 λs. λz. z 1 λs. λz. s z 2 λs. λz. s (s z) n {n λs. λz. s (... s(z)...) 3 λs. λz. s (s (s z)) 63

Encoding arithmetic plus λm. λn. λs. λz. m s (n s z) mult λm. λn. λs. λz. m (n s) z n {n λs. λz. s (... s(z)...) 64

Encoding arithmetic plus λm. λn. λs. λz. m s (n s z) mult λm. λn. λs. λz. m (n s) z Exercise. Evaluate "plus 2 3" and "mult 2 3". ifz λn. λx1. λx2. n (λz. x2) x1 Exercise. Find pred such that "pred 0 = 0" and "pred n+1 = n". 65

λ-definability A partial function f : IN k IN is λ-definable if there exists a closed λ-term F such that f(x1,...,xk) = y iff F(x1,...,xk) = y, and f(x1,...,xk) iff F(x1,...,xk) has no normal form. Church-Turing thesis: f is λ-definable f is computable via register machine f is "computable" f is computable via Turing machine 66

Example: factorial fac n = if n=0 then 1 else n*fac(n-1) fac = λn. ifz n 1 (mult n (fac (pred n)))) 67

Encoding recursion Let fix (λx. λy. y (x x y))(λx. λy. y (x x y)). Observe: fix M * M (fix M). [Recall: x is a fixpoint of f whenever x=f(x).] This means: fix M is a fixpoint of M. 68

Example: factorial fac n = if n=0 then 1 else n*fac(n-1) fac = λn. ifz n 1 (mult n (fac (pred n)))... = (λf. λn. ifz n 1 (mult n (f (pred n)))) fac fac fix (λf. λn. ifz n 1 (mult n (f (pred n))))) Exercise. Evaluate "fac 2". 69

KEY CONCEPTS we can encode... numbers and arithmetic if-statements recursion 70

BONUS Combinators Lambda calculus: M ::= λx. M M M x Can we restrict even further? Yes, we can even get rid of variables! Let S λx. λy. λz. (x z) (y z) and K λx. λy. x. M ::= M M S K 71

BONUS Is λ-calculus broken? Define silly (λx. (x x))(λx. (x x)). Then silly = silly. 72

BONUS Adding types Untyped: M ::= λx. M M M x Typed: M ::= λx:τ. M M M x where τ ::= τ τ no more fix function... not Turing-complete add fix to language explicitly 73

Outline 1. Syntax (free variables, α-equivalence, substitution) 2. Semantics (-reduction, confluence, reduction strategies) 3. Usage (encoding arithmetic, recursion) 74