COMP6463: λ-calculus

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

Simply Typed λ-calculus

Introduction to lambda calculus Part 2

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

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

Traditional and Non Traditional lambda calculi

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

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

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

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

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

Consequence Relations and Natural Deduction

Normalization by Evaluation

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

Models of computation

Simply Typed λ-calculus

Hoare Logic: Reasoning About Imperative Programs

COMP4630: λ-calculus

Introduction to Turing Machines

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

Introduction to lambda calculus Part 6

Advanced Lambda Calculus Lecture 5

Lazy Strong Normalization

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

Beyond First-Order Logic

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

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

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

The Lambda-Calculus Reduction System

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

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

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

Lambda Calculus! Gunnar Gotshalks! LC-1

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

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

Categories, Proofs and Programs

Diagrams for Meaning Preservation

07 Equational Logic and Algebraic Reasoning

Equivalent Forms of the Axiom of Infinity

Examples: P: it is not the case that P. P Q: P or Q P Q: P implies Q (if P then Q) Typical formula:

Axiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11 CSE

Advanced Lambda Calculus. Henk Barendregt & Giulio Manzonetto ICIS Faculty of Science Radboud University Nijmegen, The Netherlands

A call-by-name lambda-calculus machine

Lecture Notes on Compositional Reasoning

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

Consequence Relations and Natural Deduction

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

Programming Language Concepts: Lecture 18

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

Models of Computation,

Static Program Analysis

Denoting computation

Programming Language Concepts: Lecture 16

Gödel s Incompleteness Theorem. Overview. Computability and Logic

Local computation of β-reduction A concrete presentation of Game Semantics

Proving Completeness for Nested Sequent Calculi 1

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 15 Propositional Calculus (PC)

Type Systems Winter Semester 2006

A Common Notation System for the Lambda-Calculus and Combinatory Logic

First Order Logic: Syntax and Semantics

CSCI 490 problem set 6

Logic. Propositional Logic: Syntax

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Safety Analysis versus Type Inference

3. The λ-calculus and Implication

The Calculus of Inductive Constructions

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

Nameless Representation of Terms

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

Notes on multivariable calculus

Origin in Mathematical Logic

Realisability methods of proof and semantics with application to expansion

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

Logic and Probability Lecture 3: Beyond Boolean Logic

Predicate Logic. Xinyu Feng 09/26/2011. University of Science and Technology of China (USTC)

Propositional Logic: Part II - Syntax & Proofs 0-0

Non-Idempotent Typing Operators, beyond the λ-calculus

Polynomial Space. The classes PS and NPS Relationship to Other Classes Equivalence PS = NPS A PS-Complete Problem

Extending the Lambda Calculus: An Eager Functional Language

How to Think of Intersection Types as Cartesian Products

Simply Typed Lambda-Calculi (II)

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

Predicate Logic. x. x + 0 = x. Predicate logic over integer expressions: a language of logical assertions, for example. Why discuss predicate logic?

Seminaar Abstrakte Wiskunde Seminar in Abstract Mathematics Lecture notes in progress (27 March 2010)

15-424/ Recitation 1 First-Order Logic, Syntax and Semantics, and Differential Equations Notes by: Brandon Bohrer

Further discussion of Turing machines

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

Introduction to Logic in Computer Science: Autumn 2006

Propositional Logic: Syntax

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

Recursion Theorem. Ziv Scully Ziv Scully Recursion Theorem / 28

The Greek Alphabet. (a) The Untyped λ-calculus

Decidability: Church-Turing Thesis

First-Order Logic. 1 Syntax. Domain of Discourse. FO Vocabulary. Terms

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

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

An analogy from Calculus: limits

17.1 Correctness of First-Order Tableaux

Transcription:

COMP6463: λ-calculus 1. Basics Michael Norrish Michael.Norrish@nicta.com.au Canberra Research Lab., NICTA Semester 2, 2015

Outline Introduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies Conclusion

The Lambda Calculus The λ-calculus is a fundamental computational model. It also has a number of connections to logic. Alonzo Church (1903 1995) The next five lectures (including this one) will concentrate on the untyped calculus. The typed calculus has the most elegant connections to logic, so we will focus on computation. But there is also an interesting equational logic to consider. Introduction 3/30

Course Plan By me: 1. Basics (today) 2. Equational Logic connection to β examples inconsistent assumptions soundness & completeness 3. Soundness via Church-Rosser 4. Standardisation 5. Computation Church Numerals Encoding Other Types Encoding the Recursive Functions And then, five lectures on the typed calculus by Jeremy Dawson. Introduction 4/30

Some Sources H. P. Barendregt, The Lambda Calculus: Its Syntax and Semantics. Elsevier, 1984. ISBN 0 444 87508 5. Chris Hankin, Lambda Calculi: A Guide for Computer Scientists. Oxford University Press, 1994. ISBN 0 19 853840 3. Introduction 5/30

What are Lambda Terms? Lambda terms make up the world s simplest programming language. A lambda term is either a variable v; or the application of term M to term N, written M N; or the abstraction of variable v in term M, written (λv. M). Examples: f x f, a function, applied to an argument x (f x) y a function applied to two arguments f (g x) two function calls (λv. v) the identity function (λu. (λv. u z)) x abstraction and application Lambda Calculus Terms 6/30

Term Shorthands Applications are left-associative. So, you can write M N P, and don t have to write (M N) P (You do have to write M (N P) with the parentheses.) You can chain binders. Instead of (λu. (λv. M)), just write (λu v. M) Thus: instead of S = (λa b c. a c (b c)) S = (λa. (λb. (λc. (a c) (b c)))) Think of abstract syntax trees if necessary. Lambda Calculus Terms 7/30

Abstractions are (Anonymous) Functions In secondary school mathematics, you learn to write things like f(x) = x 2 + 2x + 1 meaning that f is a function that takes a parameter x and returns a value derived from that x. When you learnt to program, you might have learnt to write int f (int x) { return x*x + 2*x + 1; } instead. Lambda Calculus Terms 8/30

Abstractions are (Anonymous) Functions In λ-calculus (with arithmetic added), you might write f = (λx. x 2 + 2x + 1) In other words, (λx. x 2 + 2x + 1) is a function that takes a value x as a parameter, and calculates a return value. This λ-term is a function without a name. We can decide to give it the name f (or g, or nothing) later. Lambda Calculus Terms 9/30

Free and Bound Names Consider: (λv. u v) The variable v is bound. It s the name of a parameter. The variable u is free. It s not the name of an enclosing parameter. The same notions are apparent in other languages: int f (int v) { return u + v; } Lambda Calculus Terms Alpha Equivalence 10/30

Bound Names Can be Renamed These two functions are the same: int f (int v) { return u + v; } int f (int x) { return u + x; } But this one is different: int f (int u) { return u + u; } Clearly, not all bound variable renaming is OK. Lambda Calculus Terms Alpha Equivalence 11/30

Alpha Equivalence Two λ-terms M and N are alpha-equivalent (written M N) if they are the same up to renaming of bound variables Proof rules v v M 1 M 2 N 1 N 2 M 1 N 1 M 2 N 2 v not free in M N (uv) M (λu. M) (λv. N) where (uv) M = swap u and v everywhere they appear in M Lambda Calculus Terms Alpha Equivalence 12/30

Alpha Equivalence Examples Rule for (λv. M) again: v not free in M N (uv) M (λu. M) (λv. N) where (uv) M = swap u and v everywhere they appear in M So, (λv. v u) x (λw. w u) x (λu. u u) x Lambda Calculus Terms Alpha Equivalence 13/30

Alpha Equivalence Examples Rule for (λv. M) again: v not free in M N (uv) M (λu. M) (λv. N) where (uv) M = swap u and v everywhere they appear in M So, (λv. v u) x (λw. w u) x (λu. u u) x (λu. (λv. u v) u) (λv. (λu. v u) v) (λu. (λu. u v) u) Lambda Calculus Terms Alpha Equivalence 13/30

Alpha Equivalence Examples Rule for (λv. M) again: v not free in M N (uv) M (λu. M) (λv. N) where (uv) M = swap u and v everywhere they appear in M So, (λv. v u) x (λw. w u) x (λu. u u) x (λu. (λv. u v) u) (λv. (λu. v u) v) (λu. (λu. u v) u) (λx. (λy. f y) x) (λy. (λy. f y) y) (λf. (λy. f y) f) Lambda Calculus Terms Alpha Equivalence 13/30

Substitution Substitution is a ternary operation: M[v := N] means replace free occurrences of v in M with N Conditions: (making sure that free variables in N are not captured by binders in M) (alpha-convert M as necessary) Freeness: (λv. M)[v := N] (λv. M) (as v is not free in (λv. M)) Capture-avoiding: (λv. u v)[u := v] (λv. v v) (!!!) Lambda Calculus Terms Substitution 14/30

Substitution Substitution is a ternary operation: M[v := N] means replace free occurrences of v in M with N Conditions: (making sure that free variables in N are not captured by binders in M) (alpha-convert M as necessary) Freeness: (λv. M)[v := N] (λv. M) (as v is not free in (λv. M)) Capture-avoiding: (λv. u v)[u := v] (λv. v v) (!!!) (λv. u v)[u := v] (λw. u w)[u := v] (λw. v w) Lambda Calculus Terms Substitution 14/30

Substitution: Please Take Care! Substitution is tricky! You have seen it before (in handling f.o.l. quantifiers) Use the Barendregt Variable Convention: rename bound variables so they don t overlap with free variables. Lambda Calculus Terms Substitution 15/30

Substitution Drives Computation A General Programming Language Axiom: When you apply a function to an argument, the result is as if you substituted the actual argument for the formal parameter and then performed the specified computation For example, how do we figure out what f(3,5) will return? int f (int x, int y) { return 2*x + y; } Dynamics Beta Reduction 16/30

Beta Reduction: λ-terms Doing Things Substitution of actuals for formals is the essence of beta-reduction: (λv. M) N β M[v := N] Reduction can occur anywhere within a term. Examples: (λv. v) (λx. x v) β (λx. x v) Dynamics Beta Reduction 17/30

Beta Reduction: λ-terms Doing Things Substitution of actuals for formals is the essence of beta-reduction: (λv. M) N β M[v := N] Reduction can occur anywhere within a term. Examples: (λv. v) (λx. x v) β (λx. x v) (λu. (λv. v z) (u z)) β (λu. (u z) z) Dynamics Beta Reduction 17/30

Beta Reduction: λ-terms Doing Things Substitution of actuals for formals is the essence of beta-reduction: (λv. M) N β M[v := N] Reduction can occur anywhere within a term. Examples: (λv. v) (λx. x v) β (λx. x v) (λu. (λv. v z) (u z)) β (λu. (u z) z) (λu. (λv. v u) (u z)) v z β (λw. w v) (v z) z β (v z) v z Dynamics Beta Reduction 17/30

Beta Reduction: λ-terms Doing Things Substitution of actuals for formals is the essence of beta-reduction: (λv. M) N β M[v := N] Reduction can occur anywhere within a term. Examples: (λv. v) (λx. x v) β (λx. x v) (λu. (λv. v z) (u z)) β (λu. (u z) z) (λu. (λv. v u) (u z)) v z β (λw. w v) (v z) z β (v z) v z (λx. x x) (λy. y y) β (λy. y y) (λy. y y) (λx. x x) (λy. y y) Dynamics Beta Reduction 17/30

Beta Reduction Rules and Notation (λv. M) N β M[v := N] M β M M N β M N N β N M N β M N M β M (λv. M) β (λv. M ) Write M β N to mean M can take zero or more β-reduction steps and evolve to N. Write M + β N to mean M can take one or more β-reduction steps and evolve to N. Dynamics Beta Reduction 18/30

Beta Reduction Does It All! Lambda Terms + Beta Reduction = All Computation Any computation can be encoded in what we have just seen. We will look at computational aspects of the λ-calculus in detail in later lectures. In particular, it is easy to encode Numbers Recursion And that s all you need. Dynamics Beta Reduction 19/30

Eta Reduction We can also add another computational rule, η-reduction: v not free in M (λv. M v) η M (Can perform η-reduction anywhere within a term, as with β.) We will later see how η ties into extensionality. Eta-reduction and eta-expansion (!) also play a role in the typed λ-calculus. Dynamics Eta Reduction 20/30

Can Combine Beta and Eta For example: βη βη (λf. f x) (λy. g y) (λf. f x) g g x Dynamics Eta Reduction 21/30

Can Combine Beta and Eta For example: Or: (λf. f x) (λy. g y) βη (λf. f x) g βη g x (λf. f x) (λy. g y) βη (λy. g y) x βη g x (Eta and Beta coincide here) Dynamics Eta Reduction 21/30

Normal Forms A normal form is a term that cannot be further reduced. For example, (λx. g x) is a β-normal form, but not an η-normal form. It is reasonable to think of normal forms as values towards which we want evaluation to proceed. Dynamics Normal Forms 22/30

Normal Form Questions It is natural to ask: 1. Does term M have a normal form? 2. Does M have more than one normal form? And, we can ask these questions of all terms too. Dynamics Normal Forms 23/30

Lambda Term Evaluation is Non-Deterministic Recall: M β M M N β M N N β N M N β M N Remember also: (λx. x x) (λy. y y) β (λy. y y) (λy. y y) Call (λx. x x) (λy. y y), the self-looping term, Ω. Dynamics Evaluation Strategies 24/30

Infinite Loops (Depending on Evaluation Order) Consider what (λx. y) Ω might do. If we keep evaluating the argument (Ω), we never stop: (λx. y) Ω β (λx. y) Ω β (λx. y) Ω β If we apply the top-level function, substituting Ω for x we get: Done! (λx. y) Ω β y This term ((λx. y) Ω) has one normal form (y), but not all evaluation choices reach it. Dynamics Evaluation Strategies 25/30

How Do Other Languages Do This? int g l o b a l = 3; unsigned f (void) { unsigned x = 0; while (1) { x++; } return x; } int g(unsigned i ) { return g l o b a l ; } int main(void) { return g( f ()); } What happens in C? Dynamics Evaluation Strategies 26/30

Evaluation Strategy #1: Applicative Order Evaluate everything from the bottom up. I.e., in (λv. M) N work will start with M, passing to N and performing the top-level β-reduction last A function s arguments (and the function itself) will be evaluated before the argument is passed to the function. Also known as strict evaluation. (Used in Fortran, C, Pascal, Ada, SML, Java... ) Causes (λx. y) Ω to go into an infinite loop. Dynamics Evaluation Strategies 27/30

Evaluation Strategy #2: Normal Order Evaluate top-down, left-to-right. With (λv. M) N, start by performing the β-reduction, producing M[v := N] Find the top-most, left-most β-redex and reduce it. Keep going This strategy is behind the lazy evaluation of languages like Haskell. Normal order evaluation will always terminate with a normal form if a term has one. (Proof in Lecture 4) Dynamics Evaluation Strategies 28/30

Strategy Rules Write M n N for M normal order reduces to N. Then: (λv. M) N n M[v := N] M n M (λv. M) n (λv. M ) M n M M not an abstraction M N n M N N n N M not an abstraction M in β-nf M N n M N Rules for applicative order evaluation will be an assignment question. Dynamics Evaluation Strategies 29/30

Summary Lambda Terms: Variables, applications, abstractions Bound names, free names, alpha equivalence Substitution Dynamic Behaviour: Beta reduction: computation through substitution Eta reduction Normal forms, and strategies for achieving them Conclusion 30/30