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

Similar documents
Functional Languages. Hwansoo Han

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

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

Chapter 11 :: Functional Languages

1.3. Conditional expressions To express case distinctions like

Programming Languages

A Brief Introduction to Scheme (II)

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

Lambda Calculus.

Variables. Substitution

Pure Lambda Calculus. Lecture 17

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

Lecture 13 CIS 341: COMPILERS

CMSC 330: Organization of Programming Languages. Lambda Calculus

Comp 311: Sample Midterm Examination

Functional Programming. Pure Functional Languages

Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Lambda Calculus. Lambda Calculus

Functional Programming. Pure Functional Programming

Functional Programming

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

Concepts of programming languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

Introduction to the Lambda Calculus

Introduction to OCaml

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

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Functional Programming. Pure Functional Languages

CMSC 330: Organization of Programming Languages

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

Lecture Notes on Data Representation

6.001 Notes: Section 8.1

From the λ-calculus to Functional Programming Drew McDermott Posted

Chapter 1. Fundamentals of Higher Order Programming

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

Programming Systems in Artificial Intelligence Functional Programming

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

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

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

CSE 341: Programming Languages

Lecture 5: The Untyped λ-calculus

Fundamentals and lambda calculus

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Evaluating Scheme Expressions

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

Type Systems Winter Semester 2006

1 Scope, Bound and Free Occurrences, Closed Terms

CSE 505: Concepts of Programming Languages

CS115 - Module 9 - filter, map, and friends

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

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

Lexicografie computationala Feb., 2012

Functional Programming Languages (FPL)

Programming Language Pragmatics

Chapter 5: The Untyped Lambda Calculus

Recap: Functions as first-class values

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

The Environment Model. Nate Foster Spring 2018

Principles of Programming Languages

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Functional Programming

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

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

CMSC 330: Organization of Programming Languages

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

CS61A Midterm 2 Review (v1.1)

Shell CSCE 314 TAMU. Haskell Functions

CSCI.4430/6969 Programming Languages Lecture Notes

LECTURE 16. Functional Programming

Lambda Calculus. Variables and Functions. cs3723 1

6.001 Notes: Section 4.1

6.001 Notes: Section 6.1

Introduction to the λ-calculus

An Introduction to Functions

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

Functional Programming. Big Picture. Design of Programming Languages

Introduction to Scheme

An introduction to Scheme

Module 5: Lists. Readings: HtDP, Sections 9, 10.

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

Compiler Design Spring 2018

Untyped Lambda Calculus

CSCC24 Functional Programming Scheme Part 2

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

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

Principles of Programming Languages

Transcription:

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

Function filter2: variant of filter1 function from last lecture ;; filter2 : test lon -> lon ;; to construct a list of those numbers n ;; in alon such that (test n) is true (define (filter2 test alon) (cond [(empty? alon) empty] [else (cond [(test (first alon)) (cons (first alon) (filter2 test (rest alon)))] [else (filter2 test (rest alon) t)])])) Function filter2 takes function test as a data input COMP 211, Spring 2010 2

How can we pass different test functions as data for filter2? 1. By introducing a top-level definition (define (Positive? n) (> n 0)) (check-expect (filter2 Positive? '(-1 2 0 3)) '(2 3)) 2. By using a local expression to avoid cluttering top-level definitions --- how? COMP 211, Spring 2010 3

How can we pass different test functions as data for filter2? 2. By using a local expression to avoid cluttering top-level definitions (check-expect (filter2 (local ((define (Positive? n) (> n 0))) Positive?) '(-1 2 0 3)) '(2 3)) at the expense of cluttered local expressions. COMP 211, Spring 2010 4

Motivation for λ-notation It is sometimes convenient to build new functions in the middle of a computation without having to name them Often, these functions are used only once There may even be an unbounded number of such functions created at run-time Examples: arguments to functions like map, filter, fold, and many more "higher-order" functions COMP 211, Spring 2010 5

λ-notation: Basic Idea λ-notation was invented by mathematicians. For example, given f (x) = x 2 + 1 what is f? f is the function that maps x to x 2 + 1 which we might write as x x 2 + 1. The latter avoids naming the function. The notation λ x. x 2 + 1 evolved instead of x x 2 + 1 In Scheme, we write (lambda (x) (+ (* x x) 1)) instead of λ x. x 2 + 1 COMP 211, Spring 2010 6

How can we pass different test functions as data for filter2? 3) By using lambda expressions (check-expect (filter2 (lambda (n) (> n 0)) '(-1 2 0 3)) '(2 3)) note that lambda expressions are anonymous! COMP 211, Spring 2010 7

Lambda Expression A lambda expression consists of The keyword lambda A list of variable names denoting arguments An expression denoting a function of the arguments (lambda (var 1 var 2 var n ) exp) exp is an arbitrary expression var i is a variable (argument) that is only available for use within exp A lambda-expression is just a value with a type that happens to be a function COMP 211, Spring 2010

Function definitions revisited A function definition basically defines a function value for a given name, just like any other variable definition defines a value for a name (define (f x) (+ (* x x) 1)) is just short hand for(define f (lambda (x) (+ (* x x) 1))) Question: when is the divide-by-zero error encountered in the following top-level definitions? (define x (/ 1 0)) (define f (lambda (n) (/ n 0))) COMP 211, Spring 2010 9

Why λ? The name was used by its inventor Alonzo Church, logician, 1903-1995. Princeton, NJ Introduced lambda in 1930 s to formalize math Church is Corky s academic great-grandfather Alonzo Church -> Hartley Rogers -> David Luckham -> Corky Cartwright COMP 211, Spring 2010 10

Scope for a Lambda Abstraction Argument scope: (lambda (x 1... x n ) body) introduces the variables x 1... x n which have body as their scope (except for holes) Example: (lambda (x) (+ (* x x) 1))) Scope for variable introduced by define. At the top-level, (define f rhs)introduces the variable f which is visible everywhere (except in holes introduced by local definitions in f). Inside (local [(define f 1 rhs 1 )... (define f n rhs n )) body) the variables f 1... f n have body as their scope. Recursion comes from define not from lambda! COMP 211, Spring 2010 11

Example Now we can write the following program (define l '(1 2 3 4 5)) (define a (local ((define (square x) (* x x))) (map square l))) concisely as (define l '(1 2 3 4 5)) (define a (map (lambda (x) (* x x)) l)) COMP 211, Spring 2010 12

lambda vs. local Recall that: (lambda (x1... xn) exp) is equivalent to (local ((define (f x1... xn) exp)) f) Is lambda as general as local? No! How do I introduce a recursive function definition using lambda alone? It can be done but it involves deep, subtle, and messy use of λ-notation (topic in Comp 311). Direct formulations of recursion rely on the name of the defined function, which lambda lacks. COMP 211, Spring 2010 13

Evaluation of λ-expressions How do we evaluate a λ-expression (lambda (x 1... x n ) body)? It's a value --- no further reduction can be performed! What about λ-applications? β-reduction rule ((lambda (x 1 x n ) body) V 1 V n )=> body[x 1 :=V 1 x n :=V n ] where V 1,...,V n are values and body[x 1 :=V 1... x n :=V n ] means body with x 1 replaced by V 1,..., x n replaced by V n. Examples: ((lambda (x) (* x 5)) 4) => (* 4 5) => 20 ((lambda (x) (x x)) (lambda (x) (x x))) => ((lambda (x) (x x)) (lambda (x) (x x))) =>...??? COMP 211, Spring 2010 14

More Examples ((lambda (x y z) (+ x y z)) 1 2 3) => (+ 1 2 3) (((lambda (x) (lambda (y) (+ x y))) (* 2 3)) 4) => (((lambda (x) (lambda (y) (+ x y))) 6) 4) => ((lambda (y) (+ 6 y)) 4) => (+ 6 4) => 10 COMP 211, Spring 2010 15

Fine Points of Substitution Only the free occurrences of a variable are replaced. A variable occurrence v in an expression E is free iff it does not refer to a variable bound in E. A non-free (bound) variable occurrence v in expression E must be embedded in a local scope (defined by a lambda or a local) within E. Examples: Neither occurrence of x is free in (lambda (x) x) Neither occurrence of x is free in (local [(define x 12)] x) x is free in (+ y x) x is free in (lambda (y) (+ y x)) Only the first occurrence of x is free in ((+ x (local [(define x 12)] (* x 13)) COMP 211, Spring 2010 16

Example: Lambda expression as a return value (define (gen-add-by n) (lambda (x) (+ x n)) ) What do the following expressions evaluate to? (gen-add-by 1) ((gen-add-by 1) 1) ((gen-add-by 2) ((gen-add-by 1) 1)) COMP 211, Spring 2010

Nesting λ (lambda (x) (lambda (y) (+ (* x y) (* 4 5))) => (lambda (x) (lambda (y) (+ (* x y) (* 4 5))) ((lambda (x) (lambda (y) (+ x 1)) 5) => (lambda (y) (+ 5 1)) ((lambda (x) (lambda (x) (+ x 1)) 5) => (lambda (x) (+ x 1)) ;; NOTE: the red y is a free variable ((lambda (x) (lambda (y) (y x))) (lambda (z) (+ y z))) => (lambda (y) (y (lambda (z) (+ y z))))) which is WRONG (but this case can t arise in legal Scheme evaluations) COMP 211, Spring 2010 18

Safe Substitution To salvage the correctness of β-reduction in the general case, we must stipulate that the rule uses safe substitution, where safe substitution renames local variables in the code body that is being modified by the substitution to avoid capturing free variables in the argument expression that is being substituted. ((lambda (x) (lambda (y) (y x))) (lambda (z) (+ y z))) => ((lambda (x) (lambda (f) (f x))) (lambda (z) (+ y z))) => (lambda (f) (f (lambda (z) (+ y z)))) We will not hold you responsible on exams for understanding either safe substitution or the subtleties of β-reduction when the argument expressions contain free variables. COMP 211, Spring 2010 19

When Should I Use a Lambda? It makes sense to use a lambda instead of define when the function is not recursive; the function is needed only once; and the function is either being passed to another function, or being returned as the final result (contract returns -> ) Note: It is hard to read code when lambda is used at the head of an application ((lambda (x) (* x x)) (+ 13 14)) We can rewrite this as: (local ((define x (+ 13 14))) (* x x)) COMP 211, Spring 2010 20

Lambda Is Becoming Pervasive in PL Python By popular demand, a few features commonly found in functional programming languages and Lisp have been added to Python [...] Guido van Rossum, 4.7.4 Lambda Forms, Python Tutorial COMP 211, Spring 2010 21

Examples of lambda in Python identityfunc = lambda x: x print identityfunc(2) # prints 2 sumfunc = lambda a, b: a + b print sumfunc(2, 3) # prints 5 squarefunc = lambda a: a * a cubefunc = lambda a: a * a * a rootfunc = lambda a: a ** 0.5 compose = lambda f, g: lambda x: f(g(x)) print compose(squarefunc, rootfunc)(4.0) # prints 4.0 print compose(rootfunc, cubefunc)(4.0) # prints 8.0 COMP 211, Spring 2010

Examples of lambda in F# (anonymous functions) (fun n -> n * 2 ) // anonymous function (a lambda expr) List.map (fun n -> n * 2) [1;2;3] // returns [2;4;6] Lambda expressions are especially useful when you want to perform operations on a list or other collection and want to avoid the extra work of defining a function. Many F# library functions take function values as arguments, and it can be especially convenient to use a lambda expression in those cases. Source: http://msdn.microsoft.com/en-us/library/dd233201(vs.100).aspx COMP 211, Spring 2010

Use of Functions as Data in Google s Map-Reduce Framework Application of functional programming concepts to datacenter-scale distributed systems Filter = computation specified by map function Aggregator = computation specified by reduce (fold) function Reference: MapReduce: Simplified Data Processing on Large Clusters, J. Dean & S. Ghemawat, OSDI 2004. http://labs.google.com/papers/mapreduce. html Source: Figure 10.6 from textbook Source for figure: Figure 10.6 from Principles of Parallel Programming by C.Lin & L. Snyder