CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

The lambda calculus David Walker CS 441. the (untyped) lambda calculus a language of functions e ::= x | \x.e | e1 e2 v ::= \x.e there are several different.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
Foundations of Programming Languages: Introduction to Lambda Calculus
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
CS5205: Foundation in Programming Languages Lecture 1 : Overview
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
Prof. Fateman CS 164 Lecture 23
1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.
Introduction to the λ-Calculus and Functional Programming Languages Arne Kutzner Hanyang University 2015.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.
Lesson2 Lambda Calculus Basics 1/10/02 Chapter 5.1, 5.2.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
CMSC 330: Organization of Programming Languages Operational Semantics.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lecture 6: Lambda Calculus
Lambda Calculus CSE 340 – Principles of Programming Languages
ML: a quasi-functional language with strong typing
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
More on Lambda Calculus
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Typed Arithmetic Expressions
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Announcements Exam 2 on Friday, November 2nd Topics
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

CSE 230 The -Calculus

Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs Simple, Powerful, Extensible “Whatever the next 700 languages turn out to be, they will surely be variants of lambda calculus.” (Landin ’66)

Syntax Three kinds of expressions (terms): e ::= x Variables | x.e Functions ( -abstraction) | e 1 e 2 Application

Application associates to the left x y z means (x y) z Abstraction extends as far right as possible: x. x y. x y z means x.(x ( y. ((x y) z))) Syntax

Examples of Lambda Expressions Identity function  I = def x. x A function that always returns the identity fun  y. ( x. x) A function that applies arg to identity function:  f. f ( x. x)

Scope of an Identifier (Variable) “part of program where variable is accessible”

Free and Bound Variables  x. E Abstraction binds variable x in E x is the newly introduced variable E is the scope of x x is bound in x. E

y is free in E if it occurs not bound in E Free(x) = {x} Free( E 1 E 2 ) = Free(E 1 )  Free(E 2 ) Free( x. E) = Free(E) - { x } e.g: Free( x. x ( y. x y z) ) = { z } Free and Bound Variables

Renaming Bound Variables  -renaming -terms after renaming bound variables Considered identical to original Example: x. x == y. y == z. z Rename bound variables so names unique x. x ( y.y) x instead of x. x ( x.x) x Easy to see the scope of bindings

Substitution [E’/x] E : Substitution of E’ for x in E 1.Uniquely rename bound vars in E and E’ 2.Do textual substitution of E’ for x in E Example: [y ( x. x)/x] y. ( x. x) y x 1.After renaming: [y ( v. v)/x] z. ( u. u) z x 2.After substitution: z. ( u. u) z (y ( v. v))

Semantics (“Evaluation”) The evaluation of ( x. e) e’ 1.binds x to e’ 2.evaluates e with the new binding 3.yields the result of this evaluation

Semantics: Beta-Reduction ( x. e) e’  [e’/x]e

Semantics (“Evaluation”) The evaluation of ( x. e) e’ 1.binds x to e’ 2.evaluates e with the new binding 3.yields the result of this evaluation Example: ( f. f (f e)) g  g (g e)

Another View of Reduction APP x. x x x ee’ e Terms can grow substantially by reduction 

Examples of Evaluation Identity function ( x. x) E  [E / x] x = E

Examples of Evaluation … yet again ( f. f ( x. x)) ( x. x)  [ x. x / f] f ( x. x) = [( x. x) / f] f ( y. y) = ( x. x) ( y. y)  [ y. y /x] x = y. y

Examples of Evaluation ( x. x x)( y. y y)  [ y. y y / x] x x = ( y. y y)( y. y y) = ( x. x x)( y. y y)  … … A non-terminating evaluation !

Review A calculus of functions: e := x | x. e | e 1 e 2 Eval strategies = “Where to reduce” ? Normal, Call-by-name, Call-by-value Church-Rosser Theorem Regardless of strategy, upto one “normal form”

Programming with the -calculus -calculus vs. “real languages” ? Local variables? Bools, If-then-else ? Records? Integers ? Recursion ? Functions: well, those we have …

Local Variables (Let Bindings) let x = e 1 in e 2 is just ( x. e 2 ) e 1

Programming with the -calculus -calculus vs. “real languages” ? Local variables (YES!) Bools, If-then-else ? Records? Integers ? Recursion ? Functions: well, those we have …

Encoding Booleans in -calculus What can we do with a boolean? Make a binary choice How can you view this as a “function” ? Bool is a fun that takes two choices, returns one

Encoding Booleans in -calculus Bool = fun, that takes two choices, returns one true = def x. y. x false = def x. y. y if E 1 then E 2 else E 3 = def E 1 E 2 E 3 Example: “if true then u else v” is ( x. y. x) u v  ( y. u) v  u

Boolean Operations: Not, Or Boolean operations: not Function takes b: returns function takes x,y: returns “opposite” of b’s return not = def b.( x. y. b y x) Boolean operations: or Function takes b 1, b 2 : returns function takes x,y: returns (if b 1 then x else (if b 2 then x else y)) or = def b 1. b 2.( x. y. b 1 x (b 2 x y))

Programming with the -calculus -calculus vs. “real languages” ? Local variables (YES!) Bools, If-then-else (YES!) Records? Integers ? Recursion ? Functions: well, those we have …

Encoding Pairs (and so, Records) What can we do with a pair ? Select one of its elements Pair = function takes a bool, returns the left or the right element mkpair e 1 e 2 = def b. b e 1 e = “function-waiting-for-bool” fst p = def p true snd p = def p false

Encoding Pairs (and so, Records) mkpair e 1 e 2 = def b. b e 1 e fst p = def p true snd p = def p false Example fst (mkpair x y)  (mkpair x y) true  true x y  x

Programming with the -calculus -calculus vs. “real languages” ? Local variables (YES!) Bools, If-then-else (YES!) Records (YES!) Integers ? Recursion ? Functions: well, those we have …

Encoding Natural Numbers What can we do with a natural number ? Iterate a number of times over some function n = function that takes fun f, starting value s, returns: f applied to s “n” times 0 = def f. s. s 1 = def f. s. f s 2 = def f. s. f (f s)  Called Church numerals (Unary Representation) (n f s) = apply f to s “n” times, i.e. f n (s)

Operating on Natural Numbers Testing equality with 0 iszero n = def n ( b. false) true iszero = def n.( b.false) true Successor function succ n = def f. s. f (n f s) succ = def n. f. s. f (n f s) Addition add n 1 n 2 = def n 1 succ n 2 add = def n 1. n 2. n 1 succ n 2 Multiplication mult n 1 n 2 = def n 1 (add n 2 ) 0 mult = def n 1. n 2. n 1 (add n 2 ) 0

Example: Computing with Naturals What is the result of add 0 ? ( n 1. n 2. n 1 succ n 2 ) 0  n 2. 0 succ n 2 = n 2. ( f. s. s) succ n 2  n 2. n 2 = x. x

mult 2 2  2 (add 2) 0  (add 2) ((add 2) 0)  2 succ (add 2 0)  2 succ (2 succ 0)  succ (succ (succ (succ 0)))  succ (succ (succ ( f. s. f (0 f s))))  succ (succ (succ ( f. s. f s)))  succ (succ ( g. y. g (( f. s. f s) g y)))  succ (succ ( g. y. g (g y)))  * g. y. g (g (g (g y))) = 4 Example: Computing with Naturals

Programming with the -calculus -calculus vs. “real languages” ? Local variables (YES!) Bools, If-then-else (YES!) Records (YES!) Integers (YES!) Recursion ? Functions: well, those we have …

Encoding Recursion Write a function find: IN : predicate P, number n OUT: smallest num >= n s.t. P(n)=True

Encoding Recursion find satisfies the equation: find p n = if p n then n else find p (succ n) Define: F = f. p. n.(p n) n (f p (succ n)) A fixpoint of F is an x s.t. x = F x find is a fixpoint of F ! – as find p n = F find p n – so find = F find Q: Given -term F, how to write its fixpoint ?

The Y-Combinator Fixpoint Combinator Y = def F. ( y.F(y y)) ( x. F(x x)) Earns its name as … Y F  ( y.F (y y)) ( x. F (x x))   F (( x.F (x x))( z. F (z z)))  F (Y F) So, for any -calculus function F get Y F is fixpoint! Y F = F (Y F)

Whoa! Define: F = f. p. n.(p n) n (f p (succ n)) and: find = Y F Whats going on ? find p n =  Y F p n =  F (Y F) p n =  F find p n =  (p n) n (find p (succ n))

Y-Combinator in Practice fac n = if n<1 then 1 else n * fac (n-1) is just fac = \n->if n<1 then 1 else n * fac (n-1) is just fac = Y (\f n->if n<1 then 1 else n*f(n-1)) All Recursion Factored Into Y

Many other fixpoint combinators Including Klop’s Combinator: Y k = def (L L L L L L L L L L L L L L L L L L L L L L L L L L) where: L = def abcdefghIjklmnopqstuvwxyzr. r (t h i s i s a f i x p o i n t c o m b i n a t o r)

Expressiveness of -calculus Encodings are fun Programming in pure -calculus is not! We know -calculus encodes them So add 0,1,2,…,true,false,if-then-else to PL Next, types…

Appendix Evaluation Strategies

Q: Which  -redex should be picked ? Good news, Church-Rosser Theorem: independent of strategy, there is at most one normal form Bad news, some strategies may fail to find a normal form: – ( x. y) (( y.y y) ( y.y y)) ! ( x. y) (( y.y y) ( y.y y)) ! … – ( x. y) (( y.y y) ( y.y y)) ! y We consider three strategies: – normal – call-by-name – call-by-value

Normal-Order Reduction outermost redex: a redex not contained inside another redex ( e. f. e) (( a. b. a) x y) (( c. d. c) u v) Outermost: Normal order: leftmost outermost redex first Theorem: If e has a normal form e’ then normal order reduction will reduce e to e’.

Weak vs Strong Reduction In most PL, functions considered “fully evaluated” Weak Reduction: – No reduction done under lambdas – i.e. inside a function body Strong Reduction: – Reduction is done under lambdas – Partial evaluation of function, other optimizations Normal order reduces under lambda x.(( y.y y) ( y.y y)) ! x.(( y.y y) ( y.y y)) ! … Not always desired …

Call-by-Name Reduction Don’t reduce under Don’t evaluate the argument to a function call – Directly substitute; evaluate when reducing body Demand-driven – an expression is not evaluated unless needed in body Normalizing – it converges whenever normal order converges – but does not always evaluate to a normal form

Call-by-Name Example ( y. ( x. x) y) (( u. u) ( v. v))   ( y. y) (( u. u) ( v. v))   ( u. u) ( v. v)   v. v

Call-by-Value Reduction Don’t reduce under lambda Do evaluate argument to function call Most languages are call-by-value Not normalizing – ( x. y) (( y.y y) ( y.y y)) – diverges, but normal order (or CBN) converges

Call-by-Value Example: ( y. ( x. x) y) (( u. u) ( v. v))   ( y. ( x. x) y) ( v. v)   ( y. y) ( v. v)   v. v Evaluate arg

CBV vs. CBN Call-by-value: – Easy to implement – May diverge Call-by-name: – More difficult to implement must pass unevaluated expressions Args multiply-evaluated inside function body – Simpler theory than call-by-value Terminates more often (always if normal form exists) e.g. if arg is non-terminating, but not used … Various other reduction strategies…