Lambda Calculus.

Similar documents
Untyped Lambda Calculus

Untyped Lambda Calculus

Lambda Calculus. Concepts in Programming Languages Recitation 6:

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Chapter 5: The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

Type Systems Winter Semester 2006

The Untyped Lambda Calculus

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

Formal Systems and their Applications

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CIS 500 Software Foundations Fall September 25

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

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

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Functional Programming

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

Introduction to the l-calculus

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

Functional Languages. Hwansoo Han

Recursive Definitions, Fixed Points and the Combinator

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

The Untyped Lambda Calculus

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

CMSC 330: Organization of Programming Languages

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

Part I. Historical Origins

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Lambda Calculus. Lecture 4 CS /26/10

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

CMSC330. Objects, Functional Programming, and lambda calculus

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

Programming Language Pragmatics

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

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

Lecture 5: The Untyped λ-calculus

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 1. ÚVOD DO PŘEDMĚTU, LAMBDA CALCULUS

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Pure Lambda Calculus. Lecture 17

Variables and Bindings

Elixir, functional programming and the Lambda calculus.

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

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

CMSC 330: Organization of Programming Languages

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

Denotational Semantics. Domain Theory

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Introduction to the Lambda Calculus

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

VU Semantik von Programmiersprachen

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

Chapter 11 :: Functional Languages

Graphical Untyped Lambda Calculus Interactive Interpreter

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

Functional Programming. Big Picture. Design of Programming Languages

CSE 505: Concepts of Programming Languages

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

The Untyped Lambda Calculus

Lambda Calculus. Lambda Calculus

Fundamentals and lambda calculus

Introduction to Haskell

Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Functional Programming

CITS3211 FUNCTIONAL PROGRAMMING

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

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

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

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Lambda the Ultimate. Corky Cartwright Vivek Sarkar Department of Computer Science Rice University

Case study: leical analysis Leical analysis converts u sequences of characters u into u sequences of tokens u tokens are also called words or leemes F

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Functional Programming

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

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

Costly software bugs that could have been averted with type checking

An Introduction to the Lambda Calculus

Lambda Calculus and Type Inference

dynamically typed dynamically scoped

4/19/2018. Chapter 11 :: Functional Languages

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Lambda Calculus. Variables and Functions. cs3723 1

To figure this out we need a more precise understanding of how ML works

A notation for describing computations that focuses. Allows convenient denition and manipulation of

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

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

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Urmas Tamm Tartu 2013

Introduction to the Lambda Calculus. Chris Lomont

CS 242. Fundamentals. Reading: See last slide

Transcription:

Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto, Stephen A. Edwards) Benjamin Pierce Types and Programming Languages http://www.cs.cornell.edu/courses/cs3110/2008fa/recitations/rec26.html

Computation Models Turing Machines Wang Machines Counter Programs Lambda Calculus

Historical Contet Like Alan Turing, another mathematician, Alonzo Church, was very interested, during the 1930s, in the question What is a computable function? He developed a formal system known as the pure lambda calculus, in order to describe programs in a simple and precise way Today the Lambda Calculus serves as a mathematical foundation for the study of functional programming languages, and especially for the study of denotational semantics. Reference: http://en.wikipedia.org/wiki/lambda_calculus 3

Alonzo Church 1903 1995 Professor at Princeton (1929 1967) and UCLA (1967 1990) Invented the Lambda Calculus Notable students: Alan Turing Stephen Cole Kleene Martin Davis Barkley Roser Dana Scott Leon Hankin Michael Rabin John Kemeny

Simple Eample factorial int fact(int ) { if (==0) return 1; else return n * fact(n-1); } C No state mutations fact : int : int = if =0 then 1 else n * fact n-1 ML No state mutations Epression only

Another Eample Compose comp f: int int g: int int: int int = fun g (f ) comp fun + 1 fun + 1 comp fun + 1 fun + 1 2 comp fun 2 * fun y 4 * y comp fun 2 * fun y 4 * y 3

What is calculus A complete computational model An assembly language for functional programming Powerful Concise Counterintuitive Can eplain many interesting PL features

Basics Repetitive epressions can be compactly represented using functional abstraction Eample: (5 * 4* 3 * 2 * 1) + (7 * 6 * 5 * 4 * 3 * 2 *1) = factorial(5) + factorial(7) factorial(n) = if n = 0 then 1 else n * factorial(n-1) factorial= n. if n = 0 then 1 else n * factorial(n-1) factorial= n. if n = 0 then 1 else n * apply (factorial (n-1))

Untyped Lambda Calculus t ::=. t t t terms variable abstraction application Terms can be represented as abstract synta trees Syntactic Conventions Applications associates to left e 1 e 2 e 3 (e 1 e 2 ) e 3 The body of abstraction etends as far as possible. y. y. ( y. ( y) ) apply apply e1 e2 e3

Untyped Lambda Calculus t ::=. t t t terms variable abstraction application Terms can be represented as abstract synta trees Syntactic Conventions Applications associates to left e 1 e 2 e 3 (e 1 e 2 ) e 3 The body of abstraction etends as far as possible. y. y. ( y. ( y) ) y apply apply y

Untyped Lambda Calculus++ t ::=. t t t number op t 1 t 2 t k true false terms variable abstraction application number built-in operator op t 1, t 2,, t k true false

Lambda Calculus in Python (. ) y (lambda : ) (y)

Lambda Calculus in ML (. ) y (fun ) (y)

Functions on int. succ apply succ (. succ ) 0 apply 0 succ apply 1 Define a function which computes 2?

Simon Peyton Jones, The Implementation of Functional Programming Languages, Prentice-Hall, 1987. Lambda Epressions Function application are written in prefi form Add 4 to 5 (+ 4 5) Evaluation: Select a rede and apply function (+(* 5 6) (* 8 3)) (+ 30 (* 8 3)) Different evaluation orders (+( 30 24) 54 (+(* 5 6) (* 8 3)) (+(* 5 6 ) 24) (+ 30 24) 54

Function Application and Currying Function application written as jutaposition f Every function has eactly one argument Multiple arguments can be simulated by Curring (+ ) is a function which adds to its argument What is +4? What is + (+ 4 5) = (+4) 5 9

Lambda Abstraction The only other thing in the lambda calculus is lambda abstraction a notation for defining unnamed functions (. + 1) The function of that adds 1 to

Boolean Epressions (if true y) (if false y) y Boolean negation? Boolean conjunction Boolean disjunction

Beta Reduction Substitute formal by actual arguments (. + 1) 4 ( + 4 1) can occur multiple times Or not at all 5 (. + ) 4 ( + 4 4) 8 (. + 1 6) 4 ( + 1 6) 7

Beta-Reduction(Cont) Fuzzy but mechanical Etra parenthesis and tree-notations can help (. y. + y) 3 4 = ((.( y. ((+ ) y))) 3) 4 ( y. ((+ 3) y)) 4 ((+ 3) 4) 7

Functions can be arguments Arbitrary functions as arguments/return ( f. f 3) (. + 1) (. + 1) 3 (. + 1) 3 (+ 3 1) 4

Free and Bound Variables. (+ y) 2 is bound y is free Determined in the enclosed contet Like global variables Both and y are free in (+ y)

Ill-Formed Beta Reductions Sometimes beta-reductions cannot be applied Like runtime error/undefined semantics in imperative programs (1 5) ( 5 y. y) (+ true 2) (test 5 1 2) Later will refine lambda calculus to avoid such terms by the compiler

Beta Reduction Formally

Substitution Replace a term by a term + (( + 2) * y)[ 3, y 7] =? + (( + 2) * y)[ z + 2] =? + (( + 2) * y)[t z + 2] =? + (( + 2) * y)[ y] More tricky in programming languages Why?

Free vs. Bound Variables An occurrence of is bound in t if it occurs in. t otherwise it is free is a binder Eamples Id=. y. (y z) z.. y. (y z) (. ) FV: t 2 Var is the set free variables of t FV() = {} FV(. t) = FV(t) {} FV (t 1 t 2 ) = FV(t 1 ) FV(t 2 )

Beta-Reduction (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) [ s] = s [ s] y = y if y [ s] (. t 1 ) =. t 1 [ s] ( y. t 1 ) = y. [ s] t 1 if y and y FV(s) [ s] (t 1 t 2 ) = ([ s] t 1 ) ([ s] t 2 ) [ s] y y t 1 [ s] t 1

Beta-Reduction (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) [ s] = s [ s] y = y [ s] ( y. t 1 ) = y. [ s] t 1 [ s] (t 1 t 2 ) = ([ s] t 1 ) ([ s] t 2 ) [ s] apply if y if y and y FV(s) apply t 1 t 2 [ s] t1 [ s] t 2

Eample Beta-Reduction (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede (. ) y y apply y [ y] y

Eample Beta-Reduction (e 2) (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede apply (. (. ) ) (u r) apply apply apply u r apply apply apply u r apply u r u r

Eample Beta-Reduction (e 2) (cont) (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede (. (. ) ) (u r) u r (. ) apply apply apply u r apply apply u r u r

Alpha- Renaming Alpha conversion: Renaming of a bound variable and its bound occurrences. y.y. z.z Simplifies terms 32

Eample Beta-Reduction (e 2) with renaming (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede (. (. ) ) (u r)

Eample Beta-Reduction (e 3) (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede ( ( w. w)) (y z) w. y z w w apply apply y apply z y apply w apply z w apply y apply z w w w

Simple Eercise Adding scopes to epressions Proposed synta let = t 1 in t 2 Informal meaning: all the occurrences of in t2 are replaced by t1 Eample: let a =. ( w. w) in a a = How can we simulate let?

Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y

Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( ))

Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y

Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y

Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) def f(): while True: pass y def g(): return 2 print g(f())

Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (. ) ((. ) ( z. (. ) z)) id (id ( z. id z)) z z

Order of Evaluation Full-beta-reduction All possible orders Applicative order call by value (Eager) Left to right Fully evaluate arguments before function Normal order The leftmost, outermost rede is always reduced first Call by name Evaluate arguments as needed Call by need Evaluate arguments as needed and store for subsequent usages Implemented in Haskel

Different Evaluation Orders (. y. ( z. z) y) (( u. u) ( w. w)) y u w u w z y z

Call By Value (. y. ( z. z) y) (( u. u) ( w. w)) y y u w y w w z y u w z z y z y z z

Call By Name (Lazy) (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y u w z z y z

Normal Order (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y y y u w z z y z

Call-by-value Operational Semantics t ::=. t t t terms variable abstraction application v ::=. t other values values abstraction values (. t 1 ) v 2 [ v 2 ] t 1 (E-AppAbs) t 1 t 1 t 1 t 2 t 1 t 2 (E-APPL1) t 2 t 2 v 1 t 2 v 1 t 2 (E-APPL2)

Call By Value (. y. ( z. z) y) (( u. u) ( w. w)) y y u w y w w z y u w z z y z y z z

Call By Value OS (. y. ( z. z) y) (( u. u) ( w. w)) E-AppAbs w u w v 2 w t 1 u w

Call By Value OS (2) v 1 t 2 (. y. ( z. z) y) (( u. u) ( w. w)) u w u w w w w y u w E-Appl2 y w u w z y z y z z

Call By Value OS (3) (. y. ( z. z) y) (( u. u) ( w. w)) v 2 t 1 w EAppAbs y y w z y z z y z

Programming in Calculus Functions with multiple arguments Simulating values Tuples Booleans Numerics Recursion

Programming in the Multiple arguments f= (, y). s -> Currying f=. y. s f v w = ((. y.s) v) w ( y.[ v]s) w [ v] [y w] s (f v) w = ((. y. *+y*y) 3) 4 ( y.[ 3] *+y*y) 4 = ( y.3*3+y*y) 4 [y 4] 3*3+y*y = 3*3+4*4

Adding Values Can be eplicitly added t ::= terms variable. t abstraction t t application true false 1 2 Can be simulated

Simulating Booleans tru = t. f. t fls = t. f. f

Simulating Tests tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) then else l t then else value value m f n t t n f m then n n m else l m t

Simulating Tests(2) tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) then else m then else n n else n n t then t f t m f t

Simulating Tests(3) tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) then else n else t then n t then else f f t t

Simulating Tests(4) tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) then else t f then else f then else t

Simulating Tests(5) tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) then else f then then else

Simulating Tests tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) test fls then else = ( l. m. n. l m n) ( t. f. f)

Programming in Booleans tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) test fls then else = ( l. m. n. l m n) ( t. f. f) and = b. b. test b b fls or =? not =?

Programming in Numerals c 0 = s. z. z c 1 = s. z. s z c 2 = s. z. s (s z) c 3 = s. z. s (s (s z)) succ = n. s. z. s (n s z) plus = m. n. s. z. m s (n s z) times = m. n. m (plus n) c

Combinators A combinator is a function in the Lambda Calculus having no free variables Eamples. is a combinator. y. ( y) is a combinator. y. ( z) is not a combinator Combinators can serve nicely as modular building blocks for more comple epressions The Church numerals and simulated Booleans are eamples of useful combinators

Recursion Where is recursion in the lambda calculus? Cannot define FAC = (test (= n 0) 1 (* n (FAC (- n 1)))) Even let does not help

Fiedpoints We say X is a fiedpoint of a function F if F(X)= X Eamples F =. F =.2 F =.(+ 1 ) FAC is a fiedpoint of the non-recursive equation H= f. n. (test (= n 0) 1 (* n (f (- n 1)))) Compute FAC from H using beta reductions

The Y-combinator FAC is a fiedpoint of the non-recursive equation H= f. n. (test (= n 0) 1 (* n (f (- n 1)))) A function Y which computes FAC from H FAC = Y H FAC = H FAC Y H = H (Y H) Y is the function which that takes a function f and applies f (f ( (f )))

The Y-combinator FAC is a fiedpoint of the non-recursive equation H= f. n. (test (= n 0) 1 (* n (f (- n 1)))) A function Y which computes FAC from H FAC = Y H FAC = H FAC Y H = H (Y H) FAC 1 = Y H 1 = H (Y H) 1 = f. n. (test (= n 0) 1 (* n (f (- n 1)))) (Y H) 1 n. (test (= n 0) 1 (* n (Y H)(- n 1)))) 1 * (test (= 1 0) 1 (* n (Y H)(- n 1)))) * 1 (Y H) 0 = * 1 H (Y H) 0 = * 1 f. n. (test (= n 0) 1 (* n (f (- n 1)))) (Y H) 0 * * 1 n. (test (= n 0) 1 (* n ((Y H) (- n 1)))) 0 * * 1 (test (= 0 0) 1 (* n ((Y H) (- n 1)))) * * 1 1 1

The Y-combinator omega= (. ) (. ) Recursion can be simulated Y = (. ( y. (y y)) ( y. (y y))) Y f * f (Y f)

Summary: Lambda Calculus Powerful The ultimate assembly language Useful to illustrate ideas But can be counterintuitive Usually etended with useful syntactic sugars Other calculi eist pi-calculus object calculus mobile ambients