Lecture 6: Lambda Calculus

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 39: Lambda Calculus.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 40: Computing with Glue and Photons.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Lecture 20: λ Calculus λ Calculus & Computability Yan Huang Dept. of Comp. Sci. University of Virginia.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Lecture 8: The Meaning of Life, Searching for Truth and The Stuff Primitives are Made Of (and a smattering of Quantum Physics) David Evans
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
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.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
David Evans CS150: Computer Science University of Virginia Computer Science Class 32: Computability in Theory and Practice.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
(Thunking about Thunks)
Operational Semantics of Scheme
CS5205: Foundations in Programming Languages
Lecture 4: Metacircles Eval Apply David Evans
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
Lecture 2: Schreme David Evans
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 22: Inheritance CS150: Computer Science University of Virginia
Class 27: Universal Turing Machines CS150: Computer Science
Class 30: Models of Computation CS200: Computer Science
More on Lambda Calculus
Lecture 37: A Universal Computer
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.
Lambda Calculus Revisited
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Lesson2 Lambda Calculus Basics
Programming Languages and Compilers (CS 421)
Functional Programming
Class 36: The Meaning of Truth CS200: Computer Science
This Lecture Substitution model
Lecture 21: Crosscutting Aspect-Oriented Programming Background
Lecture 10: The Return of Paco Background just got here last week
Class 37: Making Lists, Numbers and Recursion from Glue Alone
Lecture 27: In Praise of Idleness CS200: Computer Science
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Class 33: Making Recursion M.C. Escher, Ascending and Descending
Lecture 10: Using Object-Oriented Languages
Programming Languages and Compilers (CS 421)
Lecture 19: Proof-Carrying Code Background just got here last week
Class 31: Universal Turing Machines CS200: Computer Science
Lecture 10: Fixed Points ad Infinitum M.C. Escher, Moebius Ants
Class 34: Models of Computation CS200: Computer Science
Class 33: Learning to Count CS200: Computer Science
David Evans Lecture 19: ||ism I don’t think we have found the right programming concepts for parallel computers yet.
Lecture 3: Rules of Evaluation CS200: Computer Science
CSE S. Tanimoto Lambda Calculus
Lecture 14: Mocking Mockingbirds
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
This Lecture Substitution model
Programming Languages
Lecture 15: Crazy Eddie and the Fixed Points Background
Group 4: Song Li, Ying Lu, Hexin Wang, and Michael Walker May 1, 2000
Presentation transcript:

Lecture 6: Lambda Calculus God created the integers – all else is the result of man. Leopold Kronecker God created application – all else is the result of man. Alonzo Church (not really) Background just got here last week finished degree at MIT week before Philosophy of advising students don’t come to grad school to implement someone else’s idea can get paid more to do that in industry learn to be a researcher important part of that is deciding what problems and ideas are worth spending time on grad students should have their own project looking for students who can come up with their own ideas for research will take good students interested in things I’m interested in – systems, programming languages & compilers, security rest of talk – give you a flavor of the kinds of things I am interested in meant to give you ideas (hopefully even inspiration!) but not meant to suggest what you should work on CS655: Programming Languages University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans

Language Complexity and Brain Cell Decay English (OED: 500,000 words) C++ (680 pages) (Number of Morphemes) Language Complexity Scheme (50 pages) ??? (.5 page) 0 5 10 15 20 25 Years Spent in School 6 Feb 2001 CS 655: Lecture 6

Complexity in Scheme Special Forms Primitives Evaluation Complexity if, cond, define, etc. Primitives Numbers (infinitely many) Booleans: #t, #f Functions (+, -, and, or, etc.) Evaluation Complexity Environments (more than ½ of our interpreter) If we have lazy evaluation, (and don’t care about abstraction) we don’t need these. Hard to get rid of? Can we get rid of all this and still have a useful language? 6 Feb 2001 CS 655: Lecture 6

-calculus Alonzo Church, 1940 term = variable | term term | (term) (LISP was developed from -calculus, not the other way round.) term = variable | term term | (term) |  variable . term 6 Feb 2001 CS 655: Lecture 6

Mystery Function (Teaser) p  xy.  pca.pca (x.x xy.x) x) y (p ((x.x xy.y) x) (x. z.z (xy.y) y) m  xy.  pca.pca (x.x xy.x) x) x.x (p y (m ((x.x xy.y) x) y)) f  x.  pca.pca ((x.x xy.x) x) (z.z (xy.y) (x.x)) (m x (f ((x.x xy.y) x))) if x = 0 1 x * f (x – 1) 6 Feb 2001 CS 655: Lecture 6

Evaluation Rules -reduction (renaming) y. M  v. (M [y v]) where v does not occur in M. -reduction (substitution) (x. M)N   M [ x N ] 6 Feb 2001 CS 655: Lecture 6

Identity () x  y if x and y are same name M1 M2  N1 N2 if M1  N1  M2  N2 (M)  (N) if M  N x. M  y. N if x  y  M  N 6 Feb 2001 CS 655: Lecture 6

Defining Substitution ( ) x [x N]  N y [x N]  y where x  y M1 M2 [x N]  M1 [x N] M2 [x N] (x. M) [x N]  x. M 6 Feb 2001 CS 655: Lecture 6

Defining Substitution ( ) (y. M) [x N]  y. (M [x N]) where x  y and y does not appear free in N or x does not appear free in M. (y. M) [x N]  z. (M [y z]) [x N] where x  y, z  x and z  y and z does not appear in M or N, x does occur free in M and y does occur free in N. 6 Feb 2001 CS 655: Lecture 6

Reduction (Uninteresting Rules) y. M  v. (M [y v]) where v does not occur in M. M  M M  N  PM  PN M  N  MP  NP M  N  x. M  x. N M  N and N  P  M  P 6 Feb 2001 CS 655: Lecture 6

-Reduction (the source of all computation) (x. M)N  M [ x N ] 6 Feb 2001 CS 655: Lecture 6

Recall Apply in Scheme “To apply a procedure to a list of arguments, evaluate the procedure in a new environment that binds the formal parameters of the procedure to the arguments it is applied to.” We’ve replaced environments with substitution. We’ve replaced eval with reduction. 6 Feb 2001 CS 655: Lecture 6

Some Simple Functions I  x.x C  xy.yx Abbreviation for x.(y. yx) CII = (x.(y. yx)) (x.x) (x.x)  (y. y (x.x)) (x.x)  x.x (x.x)  x.x = I 6 Feb 2001 CS 655: Lecture 6

Evaluating Lambda Expressions redex: Term of the form (x. M)N Something that can be -reduced An expression is in normal form if it contains no redexes (redices). To evaluate a lambda expression, keep doing reductions until you get to normal form. 6 Feb 2001 CS 655: Lecture 6

 f. (( x.f (xx)) ( x. f (xx))) Example  f. (( x.f (xx)) ( x. f (xx))) 6 Feb 2001 CS 655: Lecture 6

Alyssa P. Hacker’s Answer ( f. (( x.f (xx)) ( x. f (xx)))) (z.z)  (x.(z.z)(xx)) ( x. (z.z)(xx))  (z.z) ( x.(z.z)(xx)) ( x.(z.z)(xx))  (x.(z.z)(xx)) ( x.(z.z)(xx))  ... 6 Feb 2001 CS 655: Lecture 6

Ben Bitdiddle’s Answer ( f. (( x.f (xx)) ( x. f (xx)))) (z.z)  (x.(z.z)(xx)) ( x. (z.z)(xx))  (x.xx) (x.(z.z)(xx))  (x.xx) (x.xx)  ... 6 Feb 2001 CS 655: Lecture 6

Be Very Afraid! Some -calculus terms can be -reduced forever! The order in which you choose to do the reductions might change the result! 6 Feb 2001 CS 655: Lecture 6

Take on Faith (for now) All ways of choosing reductions that reduce a lambda expression to normal form will produce the same normal form (but some might never produce a normal form). If we always apply the outermost lambda first, we will find the normal form is there is one. This is normal order reduction – corresponds to normal order (lazy) evaluation 6 Feb 2001 CS 655: Lecture 6

T  xy. x F  xy. y if  pca . pca Who needs primitives? 6 Feb 2001 CS 655: Lecture 6

Evaluation if T M N T  xy. x F  xy. y if  pca . pca ((pca . pca) (xy. x)) M N  (ca . (x.(y. x)) ca)) M N    (x.(y. x)) M N  (y. M )) N   M 6 Feb 2001 CS 655: Lecture 6

Who needs primitives? and  xy. if x y F or  xy. if x T y 6 Feb 2001 CS 655: Lecture 6

Coupling [M, N]  z.z M N first  p.p T second  p.p F first [M, N] = p.p T (z.z M N)   (z.z M N) T = (z.z M N) xy. x   (xy. x) M N   M 6 Feb 2001 CS 655: Lecture 6

Tupling n-tuple: [M] = M [M0,..., Mn-1, Mn] = [M0, [M1 ,..., [Mn-1, Mn ]... ] n-tuple direct: [M0,..., Mn-1, Mn] = z.z M0,..., Mn-1, Mn Pi,n = x.x Ui,n Ui,n = x0... xn. xi What is P1,2? 6 Feb 2001 CS 655: Lecture 6

Primitives What about all those pesky numbers? 6 Feb 2001 CS 655: Lecture 6

What are numbers? We need three (?) functions: succ: n  n + 1 pred: n  n – 1 zero?: n  (n = 0) Is that enough to define add? 6 Feb 2001 CS 655: Lecture 6

Adding for Post-Docs add  xy.if (zero? x) y (add (pred x) (succ y) 6 Feb 2001 CS 655: Lecture 6

Counting 0  I 1  [F, I] 2  [F, [F, I]] 3  [F, [F [F, I]] ... n + 1  [F, n] 6 Feb 2001 CS 655: Lecture 6

Arithmetic Zero?  x.x T Zero? 0 = (x.x T) I = T Zero? 1 = (x.x T) [F, I] = F succ  x.[F, x] pred  x.x F pred 1 = (x.x F) [F, I] = [F, I]F = I = 0 pred 0 = (x.x F) I = IF = F 6 Feb 2001 CS 655: Lecture 6

Factorial mult  xy. if (zero? x) 0 (add y (mult (pred x) y)) fact  x. if (zero? x) 1 (mult x (fact (pred x))) Recursive definitions should make you uncomfortable. Need for definitions should also bother you. 6 Feb 2001 CS 655: Lecture 6

Summary All you need is application and abstraction and you can compute anything This is just one way of representing numbers, booleans, etc. – many others are possible Integers, booleans, if, while, +, *, =, <, subtyping, multiple inheritance, etc. are for wimps! Real programmers only use . 6 Feb 2001 CS 655: Lecture 6

Charge Problem Set 2 out today Read Prakash’s Notes Make a quantum scheme interpreter Longer and harder than PS1 New teams assigned Read Prakash’s Notes Be uncomfortable about recursive definitions 6 Feb 2001 CS 655: Lecture 6