λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Similar documents
Chapter 5: The Untyped Lambda Calculus

Type Systems Winter Semester 2006

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

CIS 500 Software Foundations Fall September 25

Formal Systems and their Applications

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Lambda Calculus. Lecture 4 CS /26/10

CIS 500 Software Foundations Midterm I

The Untyped Lambda Calculus

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Chapter 5: The Untyped Lambda Calculus

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

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

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.

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Untyped Lambda Calculus

Introduction to the Lambda Calculus. Chris Lomont

Pure Lambda Calculus. Lecture 17

Untyped Lambda Calculus

Concepts of programming languages

Lecture 5: The Untyped λ-calculus

λ calculus is inconsistent

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

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

1 Scope, Bound and Free Occurrences, Closed Terms

The Untyped Lambda Calculus

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

Introduction to the Lambda Calculus

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Lambda Calculus. Variables and Functions. cs3723 1

CSE 505: Concepts of Programming Languages

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

VU Semantik von Programmiersprachen

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Fundamentals and lambda calculus

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

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

Introduction to the λ-calculus

The Untyped Lambda Calculus

CSE-321 Programming Languages 2010 Midterm

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

The Untyped Lambda Calculus

Lexicografie computationala Feb., 2012

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

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

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

Part I. Historical Origins

From the λ-calculus to Functional Programming Drew McDermott Posted

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Recursive Definitions, Fixed Points and the Combinator

Elixir, functional programming and the Lambda calculus.

Functional Languages and Higher-Order Functions

More Lambda Calculus and Intro to Type Systems

dynamically typed dynamically scoped

Costly software bugs that could have been averted with type checking

Lambda Calculus.

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lambda Calculus and Extensions as Foundation of Functional Programming

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

CMSC 330: Organization of Programming Languages

Programming Languages

Functional Programming

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

CMSC 330: Organization of Programming Languages

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Chapter 2 The Language PCF

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

Graphical Untyped Lambda Calculus Interactive Interpreter

Lambda Calculus and Type Inference

1.3. Conditional expressions To express case distinctions like

Lecture Notes on Data Representation

CSE-321 Programming Languages 2012 Midterm

An Introduction to the Lambda Calculus

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

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

Functional Programming

Lambda Calculus-2. Church Rosser Property

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

CIS 500 Software Foundations Fall October 2

lambda calculus History 11/28/13 Jianguo Lu A formal system designed to inves:gate func:on defini:on, func:on applica:on, and recursion.

More Lambda Calculus and Intro to Type Systems

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Transcription:

Course 2D1453, 2006-07 Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Some material from B. Pierce: TAPL + some from G. Klein, NICTA Alonzo Church, 1903-1995 Church-Turing thesis First undecidability results Invented λ-calculus in 30 s λ calculus λ-calculus Intended as foundation for mathematics Discovered to be inconsistent, so interest faded (see later) Foundational importance in programming languages Lisp, McCarthy 1959 Programming languages and denotational semantics Landin, Scott, Strachey 60 s and 70 s Now an indispensable tool in logic, proof theory, semantics, type systems Untyped λ-calculus - Basic Idea Turing complete model of computation Notation for abstract functions λx. x + 5: Name of function that takes one argument and adds 5 to it I.e. a function f: x x + 5 But: Basic λ-calculus talks only about functions Not numbers or other data types They are not needed! Function application λ-abstraction sufficient for introducing functions To use functions need to be able to Refer to them: Use variables x, y, z For instance in λx. x the identity function Apply them to an argument: Application f x, same as f(x) (λx. x + 5) 4 To compute with functions need to be able to evaluate them (λx. x + 5) 4 evaluates to 4 + 5 But language is untyped (λx. x + 5) (λy. y) is also ok Terms, Variables, Syntax Assume a set of variables x, y, z Term syntax in BNF: t ::= x λx. t t t Conventions: List bound variables λx y. t = λx. (λy. t) Application binds to left: x y z = (x y) z Abstraction binds to right: λx. x y = λx. (x y) Example: λx y z. x z (y z) β reduction The fundamental principle of computation in λ-calculus is replacement of formal by actual parameters Example: (λx y. f (y x)) 5 (λx.x) Substitution: t[s/x] is t with s substituted for variable x Must avoid variable capture β reduction: - (λx. t) s β t[s/x] t β t s t β s t s β s s t β s t t β t λ x. t β λ x. t 1

Side Track: Evaluation Order Redex: Term of the shape (λx. t) t As defined, β-reduction is highly nondeterministic Not determined which redex to reduce next Example: β λy x. ((λy. y) x (λz. z z) β (λx. x) ((λy x. x) (λz. z z)) β (λx. x) (λx. (λy. y) x) Evaluation Order, II Full β-reduction: is β (λx. x) ((λy x. x) (λz. z z)) (λy x. x) (λz. z z) (λx. x) Normal order reduction: Reduce leftmost, outermost redex first (λy x. (λy. y) x) (λz. z z) λx. (λy. y) x λx. x Evaluation Order, III Call-by-name reduction: Leftmost, outermost, but no reduction under λ (λy x. (λy. y) x) (λz. z z) λx. (λy. y) x Call-by-need: Variant that uses sharing to avoid reevaluation (Haskell) Call-by-value Outermost, arguments must be value (= λ-abstraction) (λx. x) ((λx. (λy. y) x)) (λx. (λy. y) x) Programming in λ-calculus Can encode lots of stuff: Turing machines, functional programs Logic and set theory Booleans: Define tt == λx y. x ff == λx y. y if == λx y z. x y z and == λx y. x y ff Example: if tt t s, and tt t Exercise 1: Define boolean or and not functions Define: pair == λf s b. b f s fst == λp. p tt snd == λp. P ff Example: Try fst(pair t s) Pairing Church Numerals Church numerals: 0 == λ s z. z 1 == λ s z. s z 2 == λ s z. s (s z) 3 == λ s z. s (s (s z))... That is, n is the function that applies its first argument n times to its second argument! iszero == λn. n (λ x. ff) tt succ == λn s z. s (n s z) plus == λm n s z. m s (n s z) times == λm n.m (plus n) 0 2

Church Numerals - Exercises Exercise: Define exponentiation, i.e. a term for raising one Church numeral to the power of another. Predecessor is a little tricky zz == pair 0 0 ss == λp. pair (snd p) (succ (snd p)) prd == λn. fst (n ss zz) Exercise 2: Use prd to define a subtraction function Church Numerals, More Exercises Exercise 4: Define Church lists. A list [x,y,z] can be thought of as a function taking two arguments c (for cons) and n (for nil), and returns c x (c y (c z n)), similar to the Church numerals. Write a function nil representing the empty list, and a function cons that takes arguments h and (a function representing) a list tl, and returns the representation of the list with head h and tail tl. Write a function isnil that takes a list and return a Church boolean, and a function head. Finally, use an encoding similar to that of prd to write a tail function. Exercise 3: Define a function equal that test two numbers for equality and returns a Church boolean. Normal Forms and Divergence Normal form for : Term t for which no s exists such that t s There are terms in λ-calculus without normal forms: omega == (λ x. x x) (λ x. x x) omega omega is said to be divergent, non-terminating Fixed Points Define: fix f == (λx. f (λy. x x y)) (λx. f (λy. x x y)) We see that: fix f (λx. f (λy. x x y)) (λx. f (λy. x x y)) f (λy. (λx. f (λy. x x y)) (λx. f (λy. x x y)) y) = f (λx. f (λy. x x y)) (λx. f (λy. x x y)) == f(fix f) = is actually η-conversion, see later fix can be used to define recursive functions Define first g = λf. body of function to be defined f Then fix g is the result Recursion Define factbody == λf. λn. if (equal n 0) 1 (times n (f (prd n))) factorial == fix factbody Exercise 5: Compute factorial n for some n Exercise 6: Write a function that sums all members of a list of Church numerals Free and Bound Variables Now turn to some formalities about λ-terms FV(t): The set of free variables of term t FV(x) = {x} FV(t s) = FV(t) FV(s) FV(λx. t) = FV(t) {x} Example. Bound variable: In λx.t, x is a bound variable Closed term t: FV(t) = 3

Tricky business Attempt #1: (λy. t)[s/x] = λy.(t[s/x]) But then: (λx. x)[y/x] = λx. y Substitution, I The bound variable x is turned into free variable y! Substitution, II Attempt #2: (λy. t)[s/x] = λy. t, if x = y (λ y. t)[s/x] = λ y.(t[s/x]), if x y Better, but now: (λx. y)[x/y] = λx. x Capture of bound variable! Substitution, III Attempt #3: (λy. t)[s/x] = λy. t, if x = y (λy. t)[s/x] = λy.(t[s/x]), if x y and y FV(s) Even better, but now (λx. y)[x/y] is undefined Alpha-conversion Solution: Work with terms up to renaming of bound variables Alpha-conversion: Terms that are identical up to choice of bound variables are interchangeable in all contexts t 1 = α t 2 : t 1 and t 2 are identical up to alpha-conversion Convention: If t 1 = α t 2 then t 1 = t 2 Example: λx y. x y z Really working with terms modulo = α All operations must respect = α Substitution, IV Final attempt: (λy. t)[s/x] = λy.(t[s/x]), if x y and y FV(s) Clause for case x = y not needed due to = α Now: (λx. t)[s/x] = (λy. t[y/x])[s/x], where y FV(t) {x} FV(s) = λy. t[y/x][s/x] Confluence Confluence, or Church-Rosser (CR) property: if s * s 1 and s * s 2 then there is t such that s 1 * t and s 2 * t s * * s 1 s 2 * * t Full β reduction in λ-calculus is confluent Order of reduction does not matter for result Normal forms in λ-calculus are unique Example: Use example slide 7 4

Conversion and Reduction Primary concept is reduction β conversion s = β t: s and t have common reduct under β * Exists s such that s β* s and t β* s t reducible if s exists such that t s If and only if t contains a redex (λx.t 1 ) t 2 Exercise 7: Show this formally. Normal Forms If t not reducible then t is in normal form t has normal form (under ): there is some s in normal form such that t * s Proposition: If is CR then normal forms, if they exist, are unique. Proof: Diagram chasing Then s is reduct of t under etc. η Conversion Principle of extensionality: Terms are determined by their functional behaviour So: If two terms are equal for all arguments, then they should be regarded as the same So if x FV(t) then t = λx. t x η reduction: x FV(t) λx. t x η t Congruence closure rules s η s s t η s t η Conversion, II Example: (λ x. f x) (λ y. g y) η* f g βη : Both β and η reductions allowed Both η and βη are CR Equality in Isabelle is modulo α, β, η t η t s t η s t t η t λ x. t η λ x. t Some History λ calculus was originally intended as foundation for mathematics Frege (predicate logic, ~1879): Allows arbitrary quantification over predicates Russell (1901): Paradox D = {X X X} Russell and Whitehead (Principia Mathematica, 1910-13): Types and type orders, fix the problem Church (1930): λ-calculus as logic Inconsistency of λ-calculus Logical sentences coded as λ-terms x P == P x {x P x} == λx. P x Define D == λ x. not(x x) Then (Russell s paradox) D D = β not(d D) 5

Exercise 8 Prove the following lemma concerning the relation β: Lemma: If t β s then t = t 1 [t 2 /x] for some x, t 1, t 2 such that x occurs exactly once in t 1, and such that - t 2 has the form (λy.t 2,1 ) t 2,2 (for some y, t 2,1, t 2,2 ) - s = t 1 [t 2,1 [t 2,2 /y]/x] Use this lemma to conclude that there are t, t, s, s such that t β t, s β s, but t s β t s does not hold 6