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

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

Lambda Calculus. Lecture 4 CS /26/10

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

Type Systems Winter Semester 2006

Fundamentals and lambda calculus

CIS 500 Software Foundations Fall September 25

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

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

Pure Lambda Calculus. Lecture 17

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

Chapter 5: The Untyped Lambda Calculus

Concepts of programming languages

CMSC 330: Organization of Programming Languages

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

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

The Untyped Lambda Calculus

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

CMSC 330: Organization of Programming Languages

1 Scope, Bound and Free Occurrences, Closed Terms

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

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

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

CMSC 330: Organization of Programming Languages

The Untyped Lambda Calculus

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

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

CITS3211 FUNCTIONAL PROGRAMMING

CSE 505: Concepts of Programming Languages

CS 6110 S14 Lecture 1 Introduction 24 January 2014

VU Semantik von Programmiersprachen

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

dynamically typed dynamically scoped

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

Introduction to the Lambda Calculus

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Formal Systems and their Applications

Lecture 5: The Untyped λ-calculus

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

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

Introduction to the Lambda Calculus. Chris Lomont

Recursive Definitions, Fixed Points and the Combinator

CS 314 Principles of Programming Languages. Lecture 21

Part I. Historical Origins

Introductory Example

Chapter 5: The Untyped Lambda Calculus

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

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

Introduction to the λ-calculus

Elixir, functional programming and the Lambda calculus.

Programming Languages

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CMSC330. Objects, Functional Programming, and lambda calculus

Lambda Calculus and Extensions as Foundation of Functional Programming

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

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

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

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

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.

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

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

The Untyped Lambda Calculus

Lexicografie computationala Feb., 2012

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

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

Programming Language Concepts: Lecture 19

Lecture Notes on Data Representation

More Lambda Calculus and Intro to Type Systems

Untyped Lambda Calculus

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

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

Functional Languages and Higher-Order Functions

3.1 λ-calculus: Syntax

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.

Functional Programming

If a program is well-typed, then a type can be inferred. For example, consider the program

Untyped Lambda Calculus

The Untyped Lambda Calculus

CSCI.4430/6969 Programming Languages Lecture Notes

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

CMSC 330: Organization of Programming Languages

301AA - Advanced Programming

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

CSE-321 Programming Languages 2010 Final

Advanced Seminar Softwareengineering - Denotational semantics

More Lambda Calculus and Intro to Type Systems

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.

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

From the λ-calculus to Functional Programming Drew McDermott Posted

An Introduction to the Lambda Calculus

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

CIS 500 Software Foundations Fall October 2

Lambda expressions, functions and binding

Lambda Calculus-2. Church Rosser Property

Transcription:

Introduction to Lambda Calculus Lecture 7 CS 565 02/08/09

Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops) We now turn our attention to a simple but interesting language Turing complete (can express loops and recursion) Higher-order (functional objects are values) Interesting variable binding and scoping issues Foundation for many real-world programming languages Lisp, Scheme, ML, Haskell, Dylan,...

Intuition Suppose we want to describe a function that adds three to any input: plus3 x = succ (succ (succ x)) Read plus3 is a function which, when applied to any number x, yields the successor of the successor of the successor of x Note that the function which adds 3 to any number need not be named plus3; the name plus3 is just a convenient shorthand for naming this function (plus3 x) (succ 0) ((λ x. (succ (succ (succ 0)))) (succ 0))

Basics There are two new primitive syntactic forms: λ x. t (t 1 t 2 ) The function which when given a value v, yields t with v substituted for x in t. the function t 1 applied to argument t 2 Key point: functions are anonymous: they don t need to be named (e.g., plus3). For convenience we ll sometimes write: plus3 x λ x. (succ (succ (succ x))) but the naming is a metalanguage operation.

Abstractions Consider the abstraction: g λ f. (f (f (succ 0))) The argument f is used in a function position (in a call). We call g a higher-order function because it takes another function as an input. Now, (g plus3) = (λ f. (f (f (succ 0))) (λ x. (succ (succ (succ x)))) = ((λ x. (succ (succ (succ x))) ((λ x. (succ (succ (succ x)))) (succ 0))) = ((λ x. (succ (succ (succ x)))) (succ (succ (succ (succ 0))))) = (succ (succ (succ (succ (succ (succ (succ 0)))))))

Abstractions Consider double λ f. λ y. (f (f y)) The term yielded by applying double is another function (λ y. (f (f y)) Thus, double is also a higher-order function because it returns a function when applied to an argument.

Example (double plus3 0) = ((λ f. λ y. (f (f y)) (λ x. (succ (succ (succ x)))) 0) = ((λ y. ((λ x. (succ (succ (succ x)))) ((λ x. (succ (succ (succ x)))) y)) 0) = ((λ x. (succ (succ (succ x)))) (succ (succ (succ 0)))) = (succ (succ (succ (succ (succ (succ 0))))))

Key Issues How do we perform substitution: how do we bind free variables, the variables that are non-local in the function Think about the occurrences of f in λ y. (f (f y)) How do we perform application: There may be several different application subterms within a larger term. How do we decide the order to perform applications?

Pure Lambda Calculus The only value is a function Variables denote functions Functions always take functions as arguments Functions always return functions as results Minimalist Can express essentially all modern programming constructs Can apply syntactic reasoning techniques (e.g. operational semantics) to understand behavior.

Scope The λ abstraction λ x. t binds variable x. The scope of the binding is t. Occurrences of x that are not within the scope of an abstraction binding x are said to be free: λ x. λ y. (x y z) λ x. ((λ y. z y) y) Occurrences of x that are within the scope of an abstraction binding x are said to be bound by the abstraction.

Free Variables Intuitively, the free variables of an expression are non-local variables. Define FV(M) formally thus: FV(x) = {x} FV(M 1 M 2 ) = FV(M 1 ) U FV(M 2 ) FV(λ x. M) = FV(M) {x} Free variables become bound after substitution. But, if proper care is not taken, this may lead to unexpected results: (λx.λy. y x) y = λy. y y) We say that term M is α-congruent to N if N results from M by a series of changes to bound variables: λx. (x y) α-congruent to λz. (z y) not α-congruent to λy. (y y) λx.x (λx.x) α-congruent to λx.x (λx.x) and α-congruent to λx.x (λx.x )

Substitution λx.m α-congruent to λy.m[y/x] if y is not free or bound in M. Define this more generally: Let x be a variable, and M and N expressions. Then [M/x]N is the expression N : N is a variable: N = x then N = M N x then N = N N is an application (Y Z): N = ([M/x]Y) ([M/x]Z)

Substitution (cont) N is an abstraction λy.y: y = x then N = N y x then: x does not occur free in Y or if y does not occur free in M: N = λy.[m/x]y x does occur free in Y and y does occur free in M: N = λz.[m/x]([z/y]y) for fresh z

Example (λp.(λq.(λp.p( p q))(λr. (+ p r)))(+ p 4)) 2 [(+ p 4]/q]((λp.p(p q))(λr. (+ p r))) ([(+ p 4)/q](λp.p(p q)))([(+ p 4)/q](λr. (+ p r)) (by case 2) ([(+ p 4)/q](λp.p(p q)))(λr.(+ p r)) (by case 3.2.1 since q does not occur free in (+ p r) (λa.[(+ p 4)/q]([a/p](p(p q))))(λr. (+ p r)) (by case 3.3.2) (λa.a (a (+ p 4)))(λr. (+ p r)) (p. (a.a (a (+ p 4)))(r. (+ p r))) 2

Operational Semantics Values: λ x. t Computation rule: ((λ x. t) v) t[v/x] Congruence rules t 1 t 1 (t 1 t 2 ) (t 1 t 2 ) t 2 t 2 (v t 2 ) (v t 2 ) The first computation rule is referred to as the β-substitution or β-conversion rule. ((λ x. t 1 ) t 2 ) is called a β-redex. The last congruence rule is referred as the η-conversion rule. (λ x. (t x)) where x not in FV(t) is an η-redex x not free in t λ x. (t x) t η-conversion related to notion of function extensionality. Why?

Multiple arguments The λ calculus has no built-in support to handle multiple arguments. However, we can interpret λ terms that when applied yield another λ term as effectively providing the same effect: Example: double λ f. λ x. (f (f x)) We can think of double as a two-argument function. Representing a multi-argument function in terms of single-argument higher-order functions is known as currying.

Programming Examples: Booleans true λ t. λ f. t false λ t. λ f. f (true v w) ((λ t.λ f. t) v) w) ((λ f. v) w) v (false v w) ((λ t.λ f. f) v) w) ((λ f. f) w) w

Booleans (cont) not λ b. b false true The function that returns true if b is false, and false if b is true. and λ b. λ c. b c false The function that given two Boolean values (v and w) returns w if v is true and false if v is false. Thus, (and v w) yields true only if both v and w are true.

Pairs!!!!!!!!!!!!!

Numbers (Church Numerals)!!!!!!

Numbers!!!!!!!

Example!!!!!!!!!!!!!!!!