Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

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

CMSC 330: Organization of Programming Languages

Concepts of programming languages

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

Untyped Lambda Calculus

Introduction to the λ-calculus

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

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

CMSC 330: Organization of Programming Languages

Untyped Lambda Calculus

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

Functional Languages. Hwansoo Han

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Graphical Untyped Lambda Calculus Interactive Interpreter

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

1.3. Conditional expressions To express case distinctions like

Introduction to the Lambda Calculus

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

Lambda Calculus. Variables and Functions. cs3723 1

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Chapter 5: The Untyped Lambda Calculus

Programming Languages

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

Lambda Calculus.

Type Systems Winter Semester 2006

Lambda Calculus. Concepts in Programming Languages Recitation 6:

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

Functional Programming

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Functional Programming. Pure Functional Languages

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

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

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

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

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

Formal Systems and their Applications

Organization of Programming Languages CS3200/5200N. Lecture 11

Functional Programming. Big Picture. Design of Programming Languages

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

The Untyped Lambda Calculus

CSE 505: Concepts of Programming Languages

From the λ-calculus to Functional Programming Drew McDermott Posted

Elixir, functional programming and the Lambda calculus.

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

Part I. Historical Origins

CIS 500 Software Foundations Fall September 25

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

Lambda Calculus. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda Calculus. Lecture 4 CS /26/10

Functional Programming. Pure Functional Languages

Chapter 5: The Untyped Lambda Calculus

Functional Programming. Pure Functional Programming

Pure Lambda Calculus. Lecture 17

Functional Programming - 2. Higher Order Functions

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

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

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

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

Programming Languages

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

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

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CS 314 Principles of Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

CS 314 Principles of Programming Languages

Programming Languages Fall 2014

CIS 500 Software Foundations Fall October 2

Functional Programming

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

Software Paradigms (Lesson 4) Functional Programming Paradigm

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming Languages. Dr. Philip Cannata 1

Introduction to the Lambda Calculus. Chris Lomont

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

1 Scope, Bound and Free Occurrences, Closed Terms

dynamically typed dynamically scoped

LECTURE 16. Functional Programming

The Untyped Lambda Calculus

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

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Lecture 5: The Untyped λ-calculus

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

Lambda expressions, functions and binding

CMSC330. Objects, Functional Programming, and lambda calculus

Scheme Quick Reference

Transcription:

Lambda Calculus CS 550 Programming Languages Jeremy Johnson 1

Lambda Calculus The semantics of a pure functional programming language can be mathematically described by a substitution process that mimics our understanding of function application - i.e., substitute all occurrences of the formal parameters of a function in the function body by the arguments in the function call. In order to formally study function definition and application and computability, logicians (Alonzo Church, Stephen Cole Kleene, and others) in the 1930s developed a formal notation called the lambda calculus and rules defining an equivalence relation on lambda expressions that encodes function definition and application. They showed the universality of the lambda calculus, the uniqueness of normal forms, and the undecidablity of the equivalence of lambda expressions. 2

Outline Review substitution model of computation Lambda calculus Free and bound variables alpha and beta reduction Normal forms Universality of lambda calculus 3

Substitution Example (define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (define (f a) (sum-of-squares (+ a 1) (* a 2))) [applicative order] (f 5) (sum-of-squares (+ 5 1) (* 5 2)) (+ (square 6) (square 10)) (+ (* 6 6) (* 10 10)) (+ 36 100) 136 [normal order] (f 5) (sum-of-squares (+ 5 1) (* 5 2)) (+ (square (+ 5 1)) (square (* 5 2)) ) (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) (+ (* 6 6) (* 10 10)) (+ 36 100) 4

Order Matters (define (p) (p)) (define (test x y) (if (= x 0) 0 y)) (test 0 (p)) 5

Lambda Calculus Expressions 1. < exp > variable 2. application: < exp > (< exp > < exp >) 3. abstraction: < exp > (lambda variable. < exp >) Use currying to allow more than one parameter 6

Free and Bound Variables The parameter of an abstraction is bound in the lambda body (lambda (x) E) Variables not bound by a lambda are free FV( exp) = exp exp is a symbol FV(body( exp)) / parameters(exp) exp isan abstraction FV( exp1) FV( exp2) exp = ( exp1 exp2) 7

Lambda Calculus Reductions 1. beta conversion: ((lambda x.e) F) is equivalent to E[F/x], where F is substituted for all free occurrences of the variable x in E, provided all free variables in F remain free when substituted for x in E. 2. alpha conversion: (lambda x.e) is equivalent to (lambda y,e[y/x]) provided y does not appear free in E and y is not bound by a lambda when substituted for x in E. 8

Example Beta reduction 1. ((lambda (x) (lambda (y) (+ x y))) z) 2. β (lambda (y) (+ z y)) 3. ((lambda (x) (lambda (y) (+ x y))) y) 4. (lambda (y) (+ y y)) [name capture] 4 is not equivalent to 2 [invalid beta reduction] Alpha conversion 1. (lambda (y) (+ x y)) 2. α (lambda (z) (+ x z)) 3. (lambda (x) (+ x x)) [name capture] 9

Universality of Lambda Calculus 1. Church numerals and arithmetic using lambda calculus 2. boolean logic, predicates, and conditional statements using lambda calculus 3. Data structures (lists, cons, car, cdr) using lambda calculus 4. Recursion using lambda calculus 10

Church Numerals Encode the natural number n as the n-fold composition operator (lambda (f) (lambda (x) (f (f... (f x)...)))) (define zero (lambda (f) (lambda (x) x))) (define one (lambda (f) (lambda (x) (f x)))) (define two (lambda (f) (lambda (x) (f (f x)))))

Successor (define succ (lambda (n) (lambda (f) (lambda (x) (f ((n f) x))))))

Addition and Multiplication (define addc (lambda (m n) ((m succ) ((n succ) zero)))) (define mulc (lambda (m n) ((m (lambda (a) (addc n a))) zero)))

Boolean Operators Encode true and false as functions (define true (lambda (x y) x)) (define false (lambda (x y) y)) Boolean operators (define and (lambda (p q) (p q false))) (define or (lambda (p q) (p true q))) (define not (lambda (p) (p false true)))

Condional Predicate to check for zero (define iszero (lambda (n) ((n (lambda (x) false)) true))) if-then-else (define if-then-else (lambda (p x y) (p x y)))

Lists as Functions (define mycons (lambda (x y) (lambda (f) (f x y)))) (define mycar (lambda (f) (f (lambda (x y) x)))) (mycar (mycons 'a 'b)) => a

Lists as Functions (define mycons (lambda (x y) (lambda (f) (f x y)))) (define mycdr (lambda (f) (f (lambda (x y) y)))) (mycdr (mycons 'a 'b)) => b

Y Combinator (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) 1. (define Y (lambda (g) ((lambda (x) (g (x x))) (lambda (x) (g (x x)))))) 2. (g (Y g)) = (Y g) [fixed point] 3. (define g (lambda (f n) (if (= n 0) 1 (* n (f (- n 1)))))) 4. (g (Y g) n) 18

Lambda Calculus Practice Exc. 0 Given the functions (define (sumf f n) (if (= n 0) 0 (+ (f n) (sumf f (- n 1))))) (define (nth-power n) (lambda (x) (power x n))) evaluate (sumf (nth-power 3) 2) using the substitution model of function evaluation. 19

Lambda Calculus Practice Exc. 1 Implement a scheme function (FV exp), which returns a list of the free variables in the scheme expression exp. The scheme expression will not have let, letrec, or let* statements - the only bindings come from lambda expressions. 20

Lambda Calculus Practice Exc. 2 Show that the and, or, and not functions using the lambda calculus in are correct by showing that the correct result is obtained for all combinations of the inputs. 21

Lambda Calculus Practice Exc. 3 Using beta-reduction show that (succ two) is three, where two and three are the Church encodings the numbers 2 and 3 Use induction to prove that ((cn add1) 0) = the number n, where cn is the Church encoding of n. You may assume that add1 correctly adds 1 to its input. 22

Lambda Calculus Practice Exc. 4 Assume that m and n are the encodings of the numbers 2 and 3 as Church numerals. Using the substitution model of computation to show that (mulc m n) returns the Church encoding of product m * n = 6. You may assume that (addc a b) returns the Church numeral representing the sum a+b, where inputs a and b are Church encodings for numbers a and b. 23

Lambda Calculus Practice Exc. 5 Show how the predecessor function can be computed in the lambda calculus. Implement your function in Racket. 24

Lambda Calculus Practice Exc. 6 Trace through the expansion (use beta reduction) of (g (Y g) 3) using the functions g and Y from the discussion on the Y combinator. Try running this in the lazy evaluator (why won t this work in Racket?). Note you will need to use currying so that g is a function of one variable. 25

Lambda Calculus Practice Exc. 7 Implement a scheme function that performs a beta reduction. You must also have a predicate which checks to see if the beta reduction is valid (i.e. the substitution does not cause problems with name collisions) 26

Solution to Exc. 6 (define Y (lambda (g) ((lambda (x) (g (x x))) (lambda (x) (g (x x)))))) (Y g) => ((lambda (x) (g (x x))) (lambda (x) (g (x x)))) => (g ((lambda (x) (g (x x))) (lambda (x) (g x x))))) = (g (Y g))

Solution to Exc. 6 (g (Y g) 3) => (if (= 3 0) 1 (* 3 ((Y g) (- 3 1))))) => (if (= 3 0) 1 (* 3 ((Y g) 2)))) => (* 3 ((Y g) 2))) => (* 3 ((g (Y g) 2))) ; assume currying => (* 3 (if (= 2 0) 1 (* 2 ((Y g) (- 2 1)))))) => (* 3 (* 2 ((Y g) 1)))) => (* 3 (* 2 ((g (Y g) 1)))) => (* 3 (* 2 ((if (= 1 0) 1 (* 1 ((Y g) (- 1 1)))))))) => (* 3 (* 2 (* 1 ((Y g) 0)))))) => (* 3 (* 2 (* 1 ((g (Y g) 0))))))) => (* 3 (* 2 (* 1 ((if (= 0 0) 1 (* 0 ((Y g) (- 0 1))))))))))) => (* 3 (* 2 (* 1 1)))))) => 6

Solution to Exc. 6 > (driver-loop) > (define Y (lambda (g) ((lambda (x) (g (x x))) (lambda (x) (g (x x)))))) ok > (define g (lambda (f) (lambda (n) (if (= n 0) 1 (* n (f (- n 1))))))) ok > ((g (Y g)) 0) 1 > ((g (Y g)) 1) 1 > ((g (Y g)) 3) 6