The Untyped Lambda Calculus

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

Chapter 5: The Untyped Lambda Calculus

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

Type Systems Winter Semester 2006

CIS 500 Software Foundations Fall September 25

The Untyped Lambda Calculus

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

Lambda Calculus. Lecture 4 CS /26/10

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

Concepts of programming languages

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Lecture 5: The Untyped λ-calculus

The Untyped Lambda Calculus

CIS 500 Software Foundations Midterm I

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

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

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

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

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

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

The Untyped Lambda Calculus

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

Untyped Lambda Calculus

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

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

Functional Languages and Higher-Order Functions

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

Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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

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

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

Formal Systems and their Applications

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.

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

CITS3211 FUNCTIONAL PROGRAMMING

CSE 505: Concepts of Programming Languages

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

Chapter 5: The Untyped Lambda Calculus

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

CMSC 330: Organization of Programming Languages

Fundamentals and lambda calculus

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.

Introduction to the Lambda Calculus

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

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

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

Programming Languages

Recursive Definitions, Fixed Points and the Combinator

dynamically typed dynamically scoped

More Lambda Calculus and Intro to Type Systems

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

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

Introductory Example

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

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

Lambda Calculus. Lambda Calculus

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

Introduction to the λ-calculus

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

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

Costly software bugs that could have been averted with type checking

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

CS 314 Principles of Programming Languages. Lecture 21

Lexicografie computationala Feb., 2012

More Lambda Calculus and Intro to Type Systems

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.

Lambda Calculus. Variables and Functions. cs3723 1

VU Semantik von Programmiersprachen

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

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

Elixir, functional programming and the Lambda calculus.

CIS 500 Software Foundations Fall October 2

CSE-321 Programming Languages 2012 Midterm

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

CSE-321 Programming Languages 2010 Midterm

Lambda Calculus and Extensions as Foundation of Functional Programming

λ calculus is inconsistent

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

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

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CMSC 330: Organization of Programming Languages

Harvard School of Engineering and Applied Sciences Computer Science 152

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

1 Scope, Bound and Free Occurrences, Closed Terms

CMSC 330: Organization of Programming Languages

Lambda Calculus.

T λx (λy. x) F λx (λ y. y)) if λp (λc (λa. (pc)a)

301AA - Advanced Programming

Organization of Programming Languages CS3200/5200N. Lecture 11

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Lecture Notes on Data Representation

Transcription:

CS738: Advanced Compiler Optimizations The Untyped Lambda Calculus Amey Karkare karkare@cse.iitk.ac.in http://www.cse.iitk.ac.in/~karkare/cs738 Department of CSE, IIT Kanpur

Reference Book Types and Programming Languages by Benjamin C. Pierce

The Abstract Syntax t := x Variable

The Abstract Syntax t := x Variable λx.t Abstraction

The Abstract Syntax t := x Variable λx.t Abstraction t t Application

The Abstract Syntax t := x Variable λx.t Abstraction t t Application Parenthesis, (... ), can be used for grouping and scoping.

Conventions λx.t 1 t 2 t 3 is an abbreviation for λx.(t 1 t 2 t 3 ), i.e., the scope of x is as far to the right as possible until it is

Conventions λx.t 1 t 2 t 3 is an abbreviation for λx.(t 1 t 2 t 3 ), i.e., the scope of x is as far to the right as possible until it is terminated by a ) whose matching ( occurs to the left pf λ, OR

Conventions λx.t 1 t 2 t 3 is an abbreviation for λx.(t 1 t 2 t 3 ), i.e., the scope of x is as far to the right as possible until it is terminated by a ) whose matching ( occurs to the left pf λ, OR terminated by the end of the term.

Conventions λx.t 1 t 2 t 3 is an abbreviation for λx.(t 1 t 2 t 3 ), i.e., the scope of x is as far to the right as possible until it is terminated by a ) whose matching ( occurs to the left pf λ, OR terminated by the end of the term. Applications associate to the left: t 1 t 2 t 3 to be read as (t 1 t 2 )t 3 and not as t 1 (t 2 t 3 )

Conventions λx.t 1 t 2 t 3 is an abbreviation for λx.(t 1 t 2 t 3 ), i.e., the scope of x is as far to the right as possible until it is terminated by a ) whose matching ( occurs to the left pf λ, OR terminated by the end of the term. Applications associate to the left: t 1 t 2 t 3 to be read as (t 1 t 2 )t 3 and not as t 1 (t 2 t 3 ) λxyz.t is an abbreviation for λxλyλz.t which in turn is abbreviation for λx.(λy.(λz.t)).

α-renaming The name of a bound variable has no meaning except for its use to identify the bounding λ.

α-renaming The name of a bound variable has no meaning except for its use to identify the bounding λ. Renaming a λ variable, including all its bound occurrences, does not change the meaning of an expression. For example, λx.x x y is equivalent to λu.u u y

α-renaming The name of a bound variable has no meaning except for its use to identify the bounding λ. Renaming a λ variable, including all its bound occurrences, does not change the meaning of an expression. For example, λx.x x y is equivalent to λu.u u y But it is not same as λx.x x w

α-renaming The name of a bound variable has no meaning except for its use to identify the bounding λ. Renaming a λ variable, including all its bound occurrences, does not change the meaning of an expression. For example, λx.x x y is equivalent to λu.u u y But it is not same as λx.x x w Can not change free variables!

β-reduction (Execution Semantics) if an abstraction λx.t 1 is applied to a term t 2 then the result of the application is

β-reduction (Execution Semantics) if an abstraction λx.t 1 is applied to a term t 2 then the result of the application is the body of the abstraction t 1 with all free occurrences of the formal parameter x replaced with t 2.

β-reduction (Execution Semantics) if an abstraction λx.t 1 is applied to a term t 2 then the result of the application is the body of the abstraction t 1 with all free occurrences of the formal parameter x replaced with t 2. For example, (λfλx.f (f x)) g β λx.g (g x)

Caution During β-reduction, make sure a free variable is not captured inadvertently.

Caution During β-reduction, make sure a free variable is not captured inadvertently. The following reduction is WRONG (λxλy.x)(λx.y) β λy.λx.y

Caution During β-reduction, make sure a free variable is not captured inadvertently. The following reduction is WRONG (λxλy.x)(λx.y) β λy.λx.y Use α-renaming to avoid variable capture (λxλy.x)(λx.y) α β (λuλv.u)(λx.y) λv.λx.y

Exercise Apply β-reduction as far as possible 1. (λx y z. x z (y z)) (λx y. x) (λy.y) 2. (λx. x x)(λx. x x) 3. (λx y z. x z (y z)) (λx y. x) ((λx. x x)(λx. x x))

Church-Rosser Theorem Multiple ways to apply β-reduction

Church-Rosser Theorem Multiple ways to apply β-reduction Some may not terminate

Church-Rosser Theorem Multiple ways to apply β-reduction Some may not terminate However, if two different reduction sequences terminate then they always terminate in the same term

Church-Rosser Theorem Multiple ways to apply β-reduction Some may not terminate However, if two different reduction sequences terminate then they always terminate in the same term Also called the Diamond Property

Church-Rosser Theorem Multiple ways to apply β-reduction Some may not terminate However, if two different reduction sequences terminate then they always terminate in the same term Also called the Diamond Property Leftmost, outermost reduction will find the normal form if it exists

Programming in λ Calculus Where is the other stuff?

Programming in λ Calculus Where is the other stuff? Constants?

Programming in λ Calculus Where is the other stuff? Constants? Numbers

Programming in λ Calculus Where is the other stuff? Constants? Numbers Booleans

Programming in λ Calculus Where is the other stuff? Constants? Numbers Booleans Complex Types?

Programming in λ Calculus Where is the other stuff? Constants? Numbers Booleans Complex Types? Lists

Programming in λ Calculus Where is the other stuff? Constants? Numbers Booleans Complex Types? Lists Arrays

Programming in λ Calculus Where is the other stuff? Constants? Numbers Booleans Complex Types? Lists Arrays Don t we need data?

Programming in λ Calculus Where is the other stuff? Constants? Numbers Booleans Complex Types? Lists Arrays Don t we need data?

Programming in λ Calculus Where is the other stuff? Constants? Numbers Booleans Complex Types? Lists Arrays Don t we need data? Abstractions act as functions as well as data!

Numbers: Church Numerals We need a Zero

Numbers: Church Numerals We need a Zero Absence of item

Numbers: Church Numerals We need a Zero Absence of item And something to count

Numbers: Church Numerals We need a Zero Absence of item And something to count Presence of item

Numbers: Church Numerals We need a Zero Absence of item And something to count Presence of item Intuition: Whiteboard and Marker

Numbers: Church Numerals We need a Zero Absence of item And something to count Presence of item Intuition: Whiteboard and Marker Blank board represents Zero

Numbers: Church Numerals We need a Zero Absence of item And something to count Presence of item Intuition: Whiteboard and Marker Blank board represents Zero Each mark by marker represents a count.

Numbers: Church Numerals We need a Zero Absence of item And something to count Presence of item Intuition: Whiteboard and Marker Blank board represents Zero Each mark by marker represents a count. However, other pairs of objects will work as well

Numbers: Church Numerals We need a Zero Absence of item And something to count Presence of item Intuition: Whiteboard and Marker Blank board represents Zero Each mark by marker represents a count. However, other pairs of objects will work as well Lets translate this intuition into λ-expressions

Numbers Zero = λm w. w

Numbers Zero = λm w. w No mark on the whiteboard

Numbers Zero = λm w. w No mark on the whiteboard One = λm w. m w

Numbers Zero = λm w. w No mark on the whiteboard One = λm w. m w One mark on the whiteboard

Numbers Zero = λm w. w No mark on the whiteboard One = λm w. m w One mark on the whiteboard Two = λm w. m (m w)

Numbers Zero = λm w. w No mark on the whiteboard One = λm w. m w One mark on the whiteboard Two = λm w. m (m w)...

Numbers Zero = λm w. w No mark on the whiteboard One = λm w. m w One mark on the whiteboard Two = λm w. m (m w)... What about operations?

Numbers Zero = λm w. w No mark on the whiteboard One = λm w. m w One mark on the whiteboard Two = λm w. m (m w)... What about operations? add, multiply, subtract, divide,...?

Operations on Numbers succ = λx m w. m (x m w)

Operations on Numbers succ = λx m w. m (x m w) Verify: succ N = N + 1

Operations on Numbers succ = λx m w. m (x m w) Verify: succ N = N + 1 add = λx y m w. x m (y m w)

Operations on Numbers succ = λx m w. m (x m w) Verify: succ N = N + 1 add = λx y m w. x m (y m w) Verify: add M N = M + N

Operations on Numbers succ = λx m w. m (x m w) Verify: succ N = N + 1 add = λx y m w. x m (y m w) Verify: add M N = M + N mult = λx y m w. x (y m) w

Operations on Numbers succ = λx m w. m (x m w) Verify: succ N = N + 1 add = λx y m w. x m (y m w) Verify: add M N = M + N mult = λx y m w. x (y m) w Verify: mult M N = M * N

More Operations pred = λx m w. x (λg h. h (g m))(λu. w)(λu. u)

More Operations pred = λx m w. x (λg h. h (g m))(λu. w)(λu. u) Verify: pred N = N - 1

More Operations pred = λx m w. x (λg h. h (g m))(λu. w)(λu. u) Verify: pred N = N - 1 nminus = λx y. y pred x

More Operations pred = λx m w. x (λg h. h (g m))(λu. w)(λu. u) Verify: pred N = N - 1 nminus = λx y. y pred x Verify: nminus M N = max(0, M - N) natural subtraction

Church Booleans True and False

Church Booleans True and False Intuition: Selection of one out of two (complementary) choices

Church Booleans True and False Intuition: Selection of one out of two (complementary) choices True = λx y. x

Church Booleans True and False Intuition: Selection of one out of two (complementary) choices True = λx y. x False = λx y. y

Church Booleans True and False Intuition: Selection of one out of two (complementary) choices True = λx y. x False = λx y. y Predicate:

Church Booleans True and False Intuition: Selection of one out of two (complementary) choices True = λx y. x False = λx y. y Predicate: iszero = λx. x (λu.false) True

Operations on Booleans Logical operations and or not = λp q. p q p = λp q. p p q = λp t f.p f t

Operations on Booleans Logical operations and or not = λp q. p q p = λp q. p p q = λp t f.p f t The conditional operator if if = λc e t e f. (c e t e f )

Operations on Booleans Logical operations and or not = λp q. p q p = λp q. p p q = λp t f.p f t The conditional operator if if c e t e f reduces to e t if c is True, and to e f if c is False if = λc e t e f. (c e t e f )

More... More such types can be found at https://en.wikipedia.org/wiki/church_encoding

More... More such types can be found at https://en.wikipedia.org/wiki/church_encoding It is fun to come up with your own definitions for constants and operations over different types

More... More such types can be found at https://en.wikipedia.org/wiki/church_encoding It is fun to come up with your own definitions for constants and operations over different types or to develop understanding for existing definitions.

We are missing something!! The machinery described so far does not allow us to define Recursive functions Factorial, Fibonacci,... There is no concept of named functions So no way to refer to a function recursively! Fix-point computation comes to rescue

Fix-point and Y -combinator A fix-point of a function f is a value p such that f p = p

Fix-point and Y -combinator A fix-point of a function f is a value p such that f p = p Assume existence of a magic expression, called Y-combinator, that when applied to a λ-expression, gives its fixed point Y f = f (Y f)

Fix-point and Y -combinator A fix-point of a function f is a value p such that f p = p Assume existence of a magic expression, called Y-combinator, that when applied to a λ-expression, gives its fixed point Y f = f (Y f) Y-combinator gives us a way to apply a function recursively

Recursion Example: Factorial fact = λn. if (iszero n) One (mult n (fact (pred n))) = (λf n. if (iszero n) One (mult n (f (pred n)))) fact

Recursion Example: Factorial fact = λn. if (iszero n) One (mult n (fact (pred n))) = (λf n. if (iszero n) One (mult n (f (pred n)))) fact fact = g fact fact is a fixed point of the function g = (λf n. if (iszero n)one (mult n (f (pred n))))

Recursion Example: Factorial fact = λn. if (iszero n) One (mult n (fact (pred n))) = (λf n. if (iszero n) One (mult n (f (pred n)))) fact fact = g fact fact is a fixed point of the function g = (λf n. if (iszero n)one (mult n (f (pred n)))) Using Y-combinator, fact = Y g

Factorial: Verify fact 2 = (Y g) 2

Factorial: Verify fact 2 = (Y g) 2 = g (Y g) 2 by definition of Y-combinator

Factorial: Verify fact 2 = (Y g) 2 = g (Y g) 2 by definition of Y-combinator = (λfn. if (iszero n) 1 (mult n (f (pred n)))) (Y g) 2

Factorial: Verify fact 2 = (Y g) 2 = g (Y g) 2 by definition of Y-combinator = (λfn. if (iszero n) 1 (mult n (f (pred n)))) (Y g) 2 = (λn. if (iszero n) 1 (mult n ((Y g) (pred n)))) 2

Factorial: Verify fact 2 = (Y g) 2 = g (Y g) 2 by definition of Y-combinator = (λfn. if (iszero n) 1 (mult n (f (pred n)))) (Y g) 2 = (λn. if (iszero n) 1 (mult n ((Y g) (pred n)))) 2 = if (iszero 2) 1 (mult 2 ((Y g)(pred2)))

Factorial: Verify fact 2 = (Y g) 2 = g (Y g) 2 by definition of Y-combinator = (λfn. if (iszero n) 1 (mult n (f (pred n)))) (Y g) 2 = (λn. if (iszero n) 1 (mult n ((Y g) (pred n)))) 2 = if (iszero 2) 1 (mult 2 ((Y g)(pred2))) = (mult 2 ((Y g) 1))

Factorial: Verify fact 2 = (Y g) 2 = g (Y g) 2 by definition of Y-combinator = (λfn. if (iszero n) 1 (mult n (f (pred n)))) (Y g) 2 = (λn. if (iszero n) 1 (mult n ((Y g) (pred n)))) 2 = if (iszero 2) 1 (mult 2 ((Y g)(pred2))) = (mult 2 ((Y g) 1))... = (mult 2 (mult 1 (if (iszero 0) 1 (...))))

Factorial: Verify fact 2 = (Y g) 2 = g (Y g) 2 by definition of Y-combinator = (λfn. if (iszero n) 1 (mult n (f (pred n)))) (Y g) 2 = (λn. if (iszero n) 1 (mult n ((Y g) (pred n)))) 2 = if (iszero 2) 1 (mult 2 ((Y g)(pred2))) = (mult 2 ((Y g) 1))... = (mult 2 (mult 1 (if (iszero 0) 1 (...)))) = (mult 2 (mult 1 1))

Factorial: Verify fact 2 = (Y g) 2 = g (Y g) 2 by definition of Y-combinator = (λfn. if (iszero n) 1 (mult n (f (pred n)))) (Y g) 2 = (λn. if (iszero n) 1 (mult n ((Y g) (pred n)))) 2 = if (iszero 2) 1 (mult 2 ((Y g)(pred2))) = (mult 2 ((Y g) 1))... = (mult 2 (mult 1 (if (iszero 0) 1 (...)))) = (mult 2 (mult 1 1)) = 2

Recursion and Y -combinator Y-combinator allows to unroll the body of loop once similar to one unfolding of recursive call

Recursion and Y -combinator Y-combinator allows to unroll the body of loop once similar to one unfolding of recursive call Sequence of Y-combinator applications allow complete unfolding of recursive calls

Recursion and Y -combinator Y-combinator allows to unroll the body of loop once similar to one unfolding of recursive call Sequence of Y-combinator applications allow complete unfolding of recursive calls

Recursion and Y -combinator Y-combinator allows to unroll the body of loop once similar to one unfolding of recursive call Sequence of Y-combinator applications allow complete unfolding of recursive calls BUT, what about the existence of Y-combinator?

Y -combinators Many candidates exist Y 1 = λf. (λx. f (x x)) (λx. f (x x))

Y -combinators Many candidates exist Y 1 = λf. (λx. f (x x)) (λx. f (x x)) Y = λabcdefghijklmnopqstuvwxwzr.r(thisisafixedpointcombinator)

Y -combinators Many candidates exist Y 1 = λf. (λx. f (x x)) (λx. f (x x)) Y = λabcdefghijklmnopqstuvwxwzr.r(thisisafixedpointcombinator) Y funny = TTTTT TTTTT TTTTT TTTTT TTTTT T

Y -combinators Many candidates exist Y 1 = λf. (λx. f (x x)) (λx. f (x x)) Y = λabcdefghijklmnopqstuvwxwzr.r(thisisafixedpointcombinator) Y funny = TTTTT TTTTT TTTTT TTTTT TTTTT T Verify that (Y f) = f (Y f) for each

Summary A cursory look at λ-calculus

Summary A cursory look at λ-calculus Functions are data, and Data are functions!

Summary A cursory look at λ-calculus Functions are data, and Data are functions! Not covered but important to know: The power of λ calculus is equivalent to that of Turing Machine ( Church Turing Thesis )