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

Similar documents
Programming Languages (CSCI 4430/6430) History, Syntax, Semantics, Essentials, Paradigms

CSCI.4430/6969 Programming Languages Lecture Notes

Programming Languages (CSCI 4430/6430) Part 1: Functional Programming: Summary

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

Fundamentals and lambda calculus

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CMSC 330: Organization of Programming Languages

CMSC330. Objects, Functional Programming, and lambda calculus

The Untyped Lambda Calculus

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

Lambda Calculus. Lecture 4 CS /26/10

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

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

Introduction to the λ-calculus

Lecture 5: The Untyped λ-calculus

The Untyped Lambda Calculus

10.6 Theoretical Foundations

Introductory Example

Lambda Calculus and Type Inference

Chapter 5: The Untyped Lambda Calculus

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

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

Introduction to the Lambda Calculus

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

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

Recursive Definitions, Fixed Points and the Combinator

Lambda Calculus and Extensions as Foundation of Functional Programming

1 Scope, Bound and Free Occurrences, Closed Terms

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

Type Systems Winter Semester 2006

VU Semantik von Programmiersprachen

dynamically typed dynamically scoped

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

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

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

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

CMSC 330: Organization of Programming Languages

Pure Lambda Calculus. Lecture 17

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

CITS3211 FUNCTIONAL PROGRAMMING

Lambda Calculus. Lambda Calculus

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

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

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.

> module Lambda where > import Data.List -- I need some standard list functions

Untyped Lambda Calculus

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Untyped Lambda Calculus

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CIS 500 Software Foundations Fall September 25

3.1 λ-calculus: Syntax

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus-2. Church Rosser Property

Part I. Historical Origins

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

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

Formal Systems and their Applications

Implementing functional languages with abstract machines

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Functional Programming. Functional Programming

Introduction to the Lambda Calculus. Chris Lomont

Concepts of programming languages

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

1.3. Conditional expressions To express case distinctions like

International School on Rewriting (ISR 2015) Rewriting Techniques for Correctness of Program Transformations

The Untyped Lambda Calculus

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

Functional Programming

Chapter 5: The Untyped Lambda Calculus

Advanced Seminar Softwareengineering - Denotational semantics

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

Writing code that I'm not smart enough to write. A funny thing happened at Lambda Jam

A Representation of Terms of the Lambda-Calculus in Standard ML

Functional Programming Languages (FPL)

CS522 - Programming Language Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics

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

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

CSE 505: Concepts of Programming Languages

Programming Languages

Typed Lambda Calculus and Exception Handling

Mutable References. Chapter 1

Lecture Notes on Data Representation

Elixir, functional programming and the Lambda calculus.

Functional Programming

CMSC330 Spring 2017 Midterm 2

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Transcription:

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer Polytechnic Institute February 11, 2010 C. Varela 1

Mathematical Functions Take the mathematical function: f(x) = x 2 f is a function that maps integers to integers: Function f: Z Z Domain Range We apply the function f to numbers in its domain to obtain a number in its range, e.g.: f(-2) = 4 C. Varela 2

Function Composition Given the mathematical functions: f(x) = x 2, g(x) = x+1 f g is the composition of f and g: f g (x) = f(g(x)) f g (x) = f(g(x)) = f(x+1) = (x+1) 2 = x 2 + 2x + 1 g f (x) = g(f(x)) = g(x 2 ) = x 2 + 1 Function composition is therefore not commutative. Function composition can be regarded as a (higher-order) function with the following type: : (Z Z) x (Z Z) (Z Z) C. Varela 3

Lambda Calculus (Church and Kleene 1930 s) A unified language to manipulate and reason about functions. Given f(x) = x 2 λx. x 2 represents the same f function, except it is anonymous. To represent the function evaluation f(2) = 4, we use the following λ-calculus syntax: (λx. x 2 2) 2 2 4 C. Varela 4

Lambda Calculus Syntax and Semantics The syntax of a λ-calculus expression is as follows: e ::= v variable λv.e functional abstraction (e e) function application The semantics of a λ-calculus expression is as follows: (λx.e M) E{M/x} where we choose a fresh x, alpha-renaming the lambda abstraction if necessary to avoid capturing free variables in M. C. Varela 5

Currying The lambda calculus can only represent functions of one variable. It turns out that one-variable functions are sufficient to represent multiple-variable functions, using a strategy called currying. E.g., given the mathematical function: of type h(x,y) = x+y h: Z x Z Z We can represent h as h of type: h : Z Z Z Such that h(x,y) = h (x)(y) = x+y For example, h (2) = g, where g(y) = 2+y We say that h is the curried version of h. C. Varela 6

Function Composition in Lambda Calculus S: λx.x 2 (Square) I: λx.x+1 (Increment) C: λf.λg.λx.(f (g x)) (Function Composition) ((C S) I) Recall semantics rule: (λx.e M) E{M/x} ((λf.λg.λx.(f (g x)) λx.x 2 ) λx.x+1) (λg.λx.(λx.x 2 (g x)) λx.x+1) λx.(λx.x 2 (λx.x+1 x)) λx.(λx.x 2 x+1) λx.x+1 2 C. Varela 7

Free and Bound Variables The lambda functional abstraction is the only syntactic construct that binds variables. That is, in an expression of the form: λv.e we say that occurrences of variable v in expression e are bound. All other variable occurrences are said to be free. E.g., (λx.λy.(x y) (y w)) Bound Variables Free Variables C. Varela 8

α-renaming Alpha renaming is used to prevent capturing free occurrences of variables when reducing a lambda calculus expression, e.g., (λx.λy.(x y) (y w)) λy.((y w) y) This reduction erroneously captures the free occurrence of y. A correct reduction first renames y to z, (or any other fresh variable) e.g., (λx.λy.(x y) (y w)) (λx.λz.(x z) (y w)) λz.((y w) z) where y remains free. C. Varela 9

Order of Evaluation in the Lambda Calculus Does the order of evaluation change the final result? Consider: Recall semantics rule: λx.(λx.x 2 (λx.x+1 x)) (λx.e M) E{M/x} There are two possible evaluation orders: and: λx.(λx.x 2 (λx.x+1 x)) λx.(λx.x 2 x+1) λx.x+1 2 λx.(λx.x 2 (λx.x+1 x)) λx.(λx.x+1 x) 2 λx.x+1 2 Applicative Order Normal Order Is the final result always the same? C. Varela 10

Church-Rosser Theorem If a lambda calculus expression can be evaluated in two different ways and both ways terminate, both ways will yield the same result. e e 1 e 2 e Also called the diamond or confluence property. Furthermore, if there is a way for an expression evaluation to terminate, using normal order will cause termination. C. Varela 11

Order of Evaluation and Termination Consider: (λx.y (λx.(x x) λx.(x x))) There are two possible evaluation orders: Recall semantics rule: (λx.e M) E{M/x} and: (λx.y (λx.(x x) λx.(x x))) (λx.y (λx.(x x) λx.(x x))) (λx.y (λx.(x x) λx.(x x))) y Applicative Order Normal Order In this example, normal order terminates whereas applicative order does not. C. Varela 12

Combinators A lambda calculus expression with no free variables is called a combinator. For example: I: λx.x (Identity) App: λf.λx.(f x) (Application) C: λf.λg.λx.(f (g x)) (Composition) L: (λx.(x x) λx.(x x)) (Loop) Cur: λf.λx.λy.((f x) y) (Currying) Seq: λx.λy.(λz.y x) (Sequencing--normal order) ASeq: λx.λy.(y x) (Sequencing--applicative order) where y denotes a thunk, i.e., a lambda abstraction wrapping the second expression to evaluate. The meaning of a combinator is always the same independently of its context. C. Varela 13

Combinators in Functional Programming Languages Most functional programming languages have a syntactic form for lambda abstractions. For example the identity combinator: can be written in Oz as follows: λx.x fun {$ X} X end and it can be written in Scheme as follows: (lambda(x) x) C. Varela 14

Currying Combinator in Oz The currying combinator can be written in Oz as follows: fun {$ F} fun {$ X} fun {$ Y} {F X Y} end end end It takes a function of two arguments, F, and returns its curried version, e.g., {{{Curry Plus} 2} 3} 5 C. Varela 15

Exercises 20. Lambda Calculus Handout Exercise 1. 21. Lambda Calculus Handout Exercise 2. 22. Lambda Calculus Handout Exercise 5. 23. Lambda Calculus Handout Exercise 6. C. Varela 16