CS 314 Principles of Programming Languages. Lecture 21

Similar documents
CS 314 Principles of Programming Languages. Lecture 16

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

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

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

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

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

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

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

Lambda Calculus. Lecture 4 CS /26/10

Chapter 5: The Untyped Lambda Calculus

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

CS 314 Principles of Programming Languages. Lecture 13

CS 314 Principles of Programming Languages

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

CS 314 Principles of Programming Languages

Lecture 5: The Untyped λ-calculus

Concepts of programming languages

CIS 500 Software Foundations Fall September 25

The Untyped Lambda Calculus

Type Systems Winter Semester 2006

CIS 500 Software Foundations Fall October 2

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

Lambda Calculus. Variables and Functions. cs3723 1

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

CMSC 330: Organization of Programming Languages

CS 314 Principles of Programming Languages

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

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

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

CIS 500 Software Foundations Midterm I

CS 314 Principles of Programming Languages

More Lambda Calculus and Intro to Type Systems

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.

Functional Programming

Introduction to the Lambda Calculus

Fundamentals and lambda calculus

CS 415 Midterm Exam Spring SOLUTION

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

Recursive Definitions, Fixed Points and the Combinator

CS 242. Fundamentals. Reading: See last slide

Pure Lambda Calculus. Lecture 17

Organization of Programming Languages CS3200/5200N. Lecture 11

CS 320 Homework One Due 2/5 11:59pm

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

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

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

COSE212: Programming Languages. Lecture 0 Course Overview

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

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

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

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

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

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

Shell CSCE 314 TAMU. Haskell Functions

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

Introduction to the λ-calculus

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

Introductory Example

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

CMSC 330: Organization of Programming Languages

CSE 341: Programming Languages

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

CS522 - Programming Language Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics

The Untyped Lambda Calculus

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

Chapter 5: The Untyped Lambda Calculus

Y-Combinator. Contents. 1 Introduction. Avi Hayoun, Hodai Goldman and Lior Zur-Lotan. November 24, Terms recap. 1.2 Y-Combinator notes

CS 415 Midterm Exam Fall 2003

1 Scope, Bound and Free Occurrences, Closed Terms

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

Programming Languages. Dr. Philip Cannata 1

CS3: Introduction to Symbolic Programming. Lecture 5:

More Lambda Calculus and Intro to Type Systems

The Untyped Lambda Calculus

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

The lambda calculus (An introduction)

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Lecture Notes on Data Representation

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Chapter 11 :: Functional Languages

Programming Languages

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Programming Languages

Functional Programming. Pure Functional Languages

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CMSC 330: Organization of Programming Languages. Lambda Calculus

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Transcription:

CS 314 Principles of Programming Languages Lecture 21 Zheng Zhang Department of Computer Science Rutgers University Wednesday 23 rd November, 2016 Zheng Zhang 1 CS@Rutgers University

Class Information Project 2 and Homework 7 released. HW 7 due 11/30 Wednesday, 11:59pm EST. Project 2 due 12/4 Sunday, 11:59pm EST. Midterm grade calculation policy change. Originally midterm is 25% and final comprises 35% of the overall grade. Now we will take the better score of your midterm and final exam in percentage and use it to calculate 60% of the overall grade as long as you take the final exam. Midterm grades will be released earlier next week. Zheng Zhang 2 CS@Rutgers University

Recursion in lambda calculus Does this make sense? f... f... In lambda calculus, such an equation does not define a term. How to find a λ- term that does satisfy the recursive definition? Zheng Zhang 3 CS@Rutgers University

Example Example: add λmn. (cond m (add (succ m) (pred n)) (iszero? n)) Just to make things easier to read, we will write instead: add λmn. if (iszero? n) then m else (add (succ m) (pred n)) This is not a valid definition of a λ term. What about this one? add λf.(λmn. if (iszero? n) then m else (f (succ m) (pred n))) Claim: The fixed point of the above function is what we are looking for. Zheng Zhang 4 CS@Rutgers University

Function fixed points The fixed points of a function g is the set of values fix g = {x x = g(x)}. Examples: function g fix g λx.6 {6} λx.(6 - x) {3} λx.((x x) + (x-4)) {-2, 2} λx.x entire domain of f λx.(x+1) { } Zheng Zhang 5 CS@Rutgers University

Function fixed points Is there a λ term Y that computes a fixed point of a function F = λf.(... f... ), i.e., YF = F(YF)? YES. Y is called the fixed point combinator. Y λf.((λx.f(x x)) (λx.f(x x))) YF = ((λf.((λx.f(x x)) (λx.f(x x)))) F) = (λx.f(x x)) (λx.f(x x)) = F( (λx.f(x x)) (λx.f(x x))) = F(YF) Zheng Zhang 6 CS@Rutgers University

The Y combinator Example: F λf.(λmn. if (iszero? n) then m else (f (succ m) (pred n))) ((YF) 3 2) = (((λf.((λx.f(x x)) (λx.f(x x)))) F) 3 2) = ( (F((λx.F(x x)) (λx.f(x x)))) 3 2) = ((λmn.if (iszero? n) then m else (((λx.f(x x)) (λx.f(x x))) (succ m) (pred n))) 3 2) = if (iszero? 2) then 3 else (((λx.f(x x)) (λx.f(x x))) (succ 3) (pred 2)) = ( ((λx.f(x x)) (λx.f(x x))) 4 1) = ((F((λx.F(x x)) (λx.f(x x)))) 4 1) = if (iszero? 1) then 4 else (((λx.f(x x)) (λx.f(x x))) (succ 4) (pred 1)) = ( ((λx.f(x x)) (λx.f(x x))) 5 0) = ((F( (λx.f(x x)) (λx.f(x x)))) 5 0) = if (iszero? 0) then 5 else (((λx.f(x x)) (λx.f(x x))) (succ 5) (pred 0)) = 5 Zheng Zhang 7 CS@Rutgers University

The Y combinator example (cont.) Note: Informally, the Y combinator allows us to get as many copies of the recursive procedure body as we need. The computation unrolls recursive procedure calls one at a time. This notion of recursion is purely syntactic. Zheng Zhang 8 CS@Rutgers University

Lambda calculus final remarks We can express all computable functions in our λ calculus. However, nobody programs in lambda calculus. For that we have more convenient functional languages. All computable functions can be express by the following two combinators, referred to as S and K: K λxy.x S λxyz.xz(yz) Combinatory logic is as powerful as Turing Machines. Zheng Zhang 9 CS@Rutgers University

Free and bound variables Function (lambda (x) e) binds variable x in body e. You can think of this as a declaration of variable x with scope e. free occurrence ( (lambda (y) (y z)) y ) binding occurrence bound occurrence Zheng Zhang 10 CS@Rutgers University

Lexical (static) scoping The occurrence of a variable matches the lexically closest binding occurrence. An occurrence of a variable without a matching binding occurrence is called free. A variable can occur free and bound in an expression. Conceptually, we only substitute free occurrences of the formal arguments in the function body when computing a function application! Substituting bound occurrences of a formal argument is called capturing. Zheng Zhang 11 CS@Rutgers University

Environments and Closures Environments: Defer substitution by recording the bindings for the variables we would substitute in a data structure called an environment. If we need the value that a variable denotes, we just look it up in the environment. An environment is a finite map from variables to values ρ Env = Variables Values Closures This is what you get if you define a function value in our mzscheme interpreter: > (lambda (x) (+ x a)) #<procedure> > (define test (lambda (x) (+ x a))) > > (test 1) reference to undefined identifier: a Zheng Zhang 12 CS@Rutgers University

Closures (cont.) Pair the environment with a function (lambda abstraction). The environment must contain values for all free variables of the function. The function can only be evaluated in its attached environment, making capturing (i.e., replacing the wrong parameter) impossible. Such a pairing is called a closure. A closure is a pair consisting of an environment and a lambda abstraction cl Closure = { λ, ρ FreeVar(λ) DOM(ρ)} Closures can be used to implement lexical scoping. They represent lexically scoped function values. Zheng Zhang 13 CS@Rutgers University

How To Apply a Closure? How to apply a closure value to actual argument values? 1. Let c v be the closure value (lambda(x) e), ρ. 2. Apply c v to a value a v as follows : Evaluate the body e of the function in the environment ρ of the closure extended by the mapping of the formal parameter x to the actual value a v (ρ[x a v ]). ((lambda(x) ((lambda(z) ((lambda(x)(z x)) 3)) (lambda(y)(+ x y)))) 1) closure interpreter { } ((lambda(z) {x 1} ((lambda(x)(z x)) 3)) (lambda(y)(+ x y))) ((lambda(x) {x 1, (z x)) 3) z (lambda(y)(+ x y)),{x 1} } (z x) {x 3, z (lambda(y)(+ x y)),{x 1} } (+ x y) {x 1, y 3} 4 Zheng Zhang 14 CS@Rutgers University

Next Lecture Things to do: Homework problem set 7 and project 2 posted. Next time: Parallel and concurrent programming Zheng Zhang 15 CS@Rutgers University