Chapter 5: The Untyped Lambda Calculus

Similar documents
Chapter 5: The Untyped Lambda Calculus

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

CIS 500 Software Foundations Fall September 25

Type Systems Winter Semester 2006

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

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

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.

Lambda Calculus. Lecture 4 CS /26/10

Concepts of programming languages

The Untyped Lambda Calculus

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

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

Untyped Lambda Calculus

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

Untyped Lambda Calculus

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

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Costly software bugs that could have been averted with type checking

Functional Languages and Higher-Order Functions

CIS 500 Software Foundations Midterm I

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

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

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

Fundamentals and lambda calculus

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

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

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

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

The Untyped Lambda Calculus

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

Pure Lambda Calculus. Lecture 17

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

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

Introduction to the Lambda Calculus

Lambda Calculus.

CSE 505: Concepts of Programming Languages

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

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

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

The Untyped Lambda Calculus

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

Lambda Calculus. Lambda Calculus

VU Semantik von Programmiersprachen

1 Scope, Bound and Free Occurrences, Closed Terms

Lecture 5: The Untyped λ-calculus

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

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

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

Introductory Example

Formal Systems and their Applications

CMSC 330: Organization of Programming Languages

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 alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

An Introduction to the Lambda Calculus

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

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

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

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

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

CSE-321 Programming Languages 2010 Midterm

Lambda Calculus and Extensions as Foundation of Functional Programming

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

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

The Untyped Lambda Calculus

Lambda Calculus-2. Church Rosser Property

301AA - Advanced Programming

More Lambda Calculus and Intro to Type Systems

CIS 500 Software Foundations Fall October 2

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

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

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

Lambda Calculus. Concepts in Programming Languages Recitation 6:

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.

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Lambda Calculus. Variables and Functions. cs3723 1

Functional Programming

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

dynamically typed dynamically scoped

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

CMSC 330: Organization of Programming Languages

Programming Languages

More Lambda Calculus and Intro to Type Systems

Introduction to the λ-calculus

Elixir, functional programming and the Lambda calculus.

CSE-321 Programming Languages 2012 Midterm

Lambda Calculus and Type Inference

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CITS3211 FUNCTIONAL PROGRAMMING

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

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Transcription:

Chapter 5: The Untyped Lambda Calculus What is lambda calculus for? Basics: syntax and operational semantics Programming in the Lambda Calculus Formalities (formal definitions)

What is Lambda calculus for? A core calculus (used by Landin) for capturing the language s essential mechanisms, with a collection of convenient derived forms whose behavior is understood by translating them into the core A formal system invented in the 1920s by Alonzo Church (1936, 1941), in which all computation is reduced to the basic operations of function definition and application.

Basics

Syntax The lambda-calculus (or λ-calculus) embodies this kind of function definition and application in the purest possible form.

Abstract Syntax Trees (s t) u (or simply written as s t u)

Abstract Syntax Trees λx. (λy. ((x y) x)) (or simply written as λx. λy. x y x )

Scope An occurrence of the variable x is said to be bound when it occurs in the body t of an abstraction λx.t. λx is a binder whose scope is t. A binder can be renamed: e.g., λx.x = λy.y. An occurrence of x is free if it appears in a position where it is not bound by an enclosing abstraction on x. Exercises: Find free variable occurrences from the following terms: x y, λx.x, λy. x y, (λx.x) x.

Operational Semantics Beta-reduction: the only computation the term obtained by replacing all free occurrences of x in t 12 by t 2 A term of the form (λx.t12) t2 is called a redex. Examples: (λx.x) y y (λx. x (λx.x)) (u r) u r (λx.x)

Evaluation Strategies Full beta-reduction Any redex may be reduced at any time. Example: Let id = λx.x. We can apply beta reduction to any of the following underlined redexes: Note: lambda calculus is confluent under full beta-reduction. Ref. Church-Rosser property.

Evaluation Strategies The normal order strategy The leftmost, outmost redex is always reduced first.

Evaluation Strategies The call-by-name strategy A more restrictive normal order strategy, allowing no reduction inside abstraction.

Evaluation Strategies The call-by-value strategy only outermost redexes are reduced and where a redex is reduced only when its right-hand side has already been reduced to a value, a term that cannot be reduced any more.

Programming in the Lambda Calculus Multiple Arguments Church Booleans Pairs Church Numerals Recursion

Multiple Arguments f (x, y) = s currying (f x) y = s f = λx. (λy. s)

Church Booleans Boolean values can be encoded as: tru = λt. λf. t fls = λt. λf. f Boolean conditional and operators can be encoded as: test = λl. λm. λn. l m n and = λb. λc. b c fls

Church Booleans An Example

Church Numerals Encoding Church numerals: Defining functions on Church numerals: succ = λn. λs. λz. s (n s z); plus = λm. λn. λs. λz. m s (n s z); times = λm. λn. m (plus n) c0;

Church Numerals Can you define minus? Suppose we have pred, can you define minus? λm. λn. n pred m Can you define pred? λn. λs. λz. n λg. λh. h g s λu. z (λu. u) λu. z -- a wrapped zero (λu. u) the last application to be skipped λg. λh. h g s -- apply h if it is the last application, otherwise apply g Try n = 0, 1, 2 to see the effect

Pairs Encoding An Example

Recursion Terms with no normal form are said to diverge. omega = (λx. x x) (λx. x x); Fixed-point combinator fix = λf. (λx. f (λy. x x y)) (λx. f (λy. x x y)); Note: fix f = f (fix f)

Recursion Basic Idea: A recursive definition: h = <body containing h> g = λf. <body containing f> h = fix g

Recursion Example: fac = λn. if eq n c0 then c1 else times n (fac (pred n) g = λf. λn. if eq n c0 then c1 else times n (f (pred n) fac = fix g Exercise: Check that fac c3 c6.

Y Combinator Why fix is used instead of Y?

Answer Assuming call-by-value (x x) is not a value while (λy. x x y) is Y will diverge for any f 24

Formalities (Formal Definitions) Syntax (free variables) Substitution Operational Semantics

Syntax Definition [Terms]: Let V be a countable set of variable names. The set of terms is the smallest set T such that 1. x T for every x V; 2. if t 1 T and x V, then λx.t 1 T; 3. If t1 T and t 2 T, then t 1 t 2 T. Free Variables FV(x) = {x} FV(λx.t 1 ) = FV(t 1 ) \ {x} FV(t 1 t 2 ) = FV(t 1 ) FV(t 2 )

Substitution Example: [x y z] (λy. x y) = [x y z] (λw. x w) = λw. y z w

Operational Semantics

Summary What is lambda calculus for? A core calculus for capturing language essential mechanisms Simple but powerful Syntax Function definition + function application Binder, scope, free variables Operational semantics Substitution Evaluation strategies: normal order, call-by-name, call-by-value

Homework Understand Chapter 5. Do exercise 5.3.6 in Chapter 5.