CSE 341: Programming Languages

Similar documents
CSE 341 Section 5. Winter 2018

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

CSE 505: Concepts of Programming Languages

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Introduction to OCaml

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

CSCI-GA Scripting Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

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

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

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

The Untyped Lambda Calculus

CSE 341: Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Functions. Nate Foster Spring 2018

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

CSE 341: Programming Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Programming Languages

G Programming Languages - Fall 2012

Lambda Calculus. Gunnar Gotshalks LC-1

Meta-programming with Names and Necessity p.1

CMSC 330: Organization of Programming Languages

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

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

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

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

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

CSE341: Programming Languages Winter 2018 Unit 5 Summary Zach Tatlock, University of Washington

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

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

CS 242. Fundamentals. Reading: See last slide

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

LECTURE 16. Functional Programming

Formal Systems and their Applications

CS422 - Programming Language Design

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

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

CS1 Recitation. Week 1

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Lecture 5: The Untyped λ-calculus

Lambda Calculus LC-1

CS 11 Haskell track: lecture 1

FUNCTIONAL PROGRAMMING

CSE341, Fall 2011, Midterm Examination October 31, 2011

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

The Untyped Lambda Calculus

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

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

Functional abstraction

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

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

CMSC330. Objects, Functional Programming, and lambda calculus

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

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

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Lambda Calculus see notes on Lambda Calculus

Programming Languages

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

CIS 500 Software Foundations Fall September 25

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CSE341 Spring 2016, Midterm Examination April 29, 2016

Datatype declarations

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

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

CMSC 330: Organization of Programming Languages

Lectures 24 and 25: Scheduling; Introduction to Effects

CSE 505: Concepts of Programming Languages

Lecture 19: Signatures, Structures, and Type Abstraction

Functional Programming

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CSE341, Fall 2011, Lecture 12 Summary

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

CMSC 330: Organization of Programming Languages. Lambda Calculus

CSE 341: Programming Languages

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Functional Programming

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

Comp 311: Sample Midterm Examination

Programming Languages Assignment #7

(Refer Slide Time: 4:00)

Functional programming Primer I

CMSC 330: Organization of Programming Languages

Transcription:

CSE 341: Programming Languages Autumn 2005 Lecture 10 Mutual Recursion, Equivalence, and Syntactic Sugar CSE 341 Autumn 2005, Lecture 10 1

Mutual Recursion You ve already seen how multiple functions can recursively call each other in HW 2. ML uses the keyword and to provide different scope rules. Example: fun even i = if i=0 then true else odd (i-1) and odd i = if i=0 then false else even (i-1) Roughly extends the binding form for functions from fun f1 x1 = e1 to fun f1 x1 = e1 and f2 x2 = e2 and... and fn xn = en. Syntax gotcha: Easy to forget that you write and fi xi = ei, not and fun fi xi = ei. CSE 341 Autumn 2005, Lecture 10 2

Mutual Recursion Idioms 1. Encode a state machine (see product_sign example) Sometimes easier to understand than explicit state values. 2. Process mutually recursive types, as in HW 2 CSE 341 Autumn 2005, Lecture 10 3

Equivalence Equivalence is a fundamental programming concept Code maintenance / backward-compatibility Program verification Program optimization Abstraction and strong interfaces But what does it mean for an expression (or program) e1 to be equivalent to expression e2? CSE 341 Autumn 2005, Lecture 10 4

Equivalence I: where? Context (i.e., where equivalent ) Given where e1 occurs in a program e, replacing e1 with e2 makes a program e equivalent to e At any point in any program, replacing e1 with e2 makes an equivalent program. The latter (contextual equivalence) is much more interesting. For the former, the body of an unused function body is equivalent to everything (that typechecks). CSE 341 Autumn 2005, Lecture 10 5

Equivalence II: how? how equivalent partial : e and e are equivalent if they input and output the same data (any limits on input?) total : partial plus e and e have the same termination behavior efficiency: e and e are totally equivalent and one never takes more than (for example) c times longer than the other (or uses much more space or...) syntactic notions: e and e differ only in whitespace and comments (for example) Key notion: what is observable? (memory, clock, REP-loop, file-system,...) CSE 341 Autumn 2005, Lecture 10 6

Accounting for Effects Consider whether fn x => e1 and fn x => e2 are totally contextually equivalent. Is this enough? For all environments, e1 terminates and evaluates to v under the environments if and only if e2 terminates and evaluates to v under the environment. Functions produce values; may also produce (side-) effects. Consider both! Purely functional languages have fewer/none, but ML is not purely functional. In real languages, contextual equivalence usually requires many things. Nonetheless, equivalence usually means total contextual equivalence for practical purposes (optimization, reasoning about correctness, etc.). CSE 341 Autumn 2005, Lecture 10 7

Syntactic Sugar Syntactic sugar causes cancer of the semicolon. Alan Perlis When all expressions using one construct are totally equivalent to another more primitive construct, we say the former is syntactic sugar. Makes language definition easier Makes language implementation easier Examples: e1 andalso e2 (define as a conditional) if e1 then e2 else e3 (define as a case) fun f x y = e (define with an anonymous function) CSE 341 Autumn 2005, Lecture 10 8

More sugar #1 e is just let val (x,...) = e in x end If we ignore types, then we have even more sugar: let val p = e1 in e2 end is just (fn p => e2) e1. In fact, if we let every program type-check (or just use one big datatype), then a language with just functions and function application is as powerful as ML or Java (in the Turing Tarpit sense). This language is called lambda calculus we ll learn a bit more about it later. CSE 341 Autumn 2005, Lecture 10 9

Equivalences for Functions While sugar defines one construct in terms of another, there are also important notions of meaning-preserving changes involving functions and bound variables. They re so important that a goal of language design is that a language supports them. But the correct definitions are subtle. First example: systematic renaming Is fn x => e1 equivalent to fn y => e2 where e2 is e1 with every x replaced by y? CSE 341 Autumn 2005, Lecture 10 10

Systematic renaming requires care Is fn x => e1 is equivalent to fn y => e2 where e2 is e1 with every x replaced by y? What if e1 is y? What if e1 is fn x => x? Need caveats: fn x => e1 is equivalent to fn y => e2 where e2 is e1 with every free x replaced by y and y is not free in e1, and no free x occurs within the scope of a binding for y (capture; e.g.: fn x => let y = 2 in x + y end; see also next slide.) Note: We can provide a very precise recursive (meta-)definition of free variables in an expression. Next: Is (fn x => e1) e2 equivalent to e3 where e3 is e1 with every x replaced by e2? CSE 341 Autumn 2005, Lecture 10 11

Argument Substitution Is (fn x => e1) e2 equivalent to e3 where e3 is e1 with every x replaced by e2? Every free x (of course). A free variable in e2 must not be bound at an occurrence of x. (Called capture.) Always satisfiable by renaming bound variables. Evaluating e2 must have no effects (printing, exceptions, infinite-loop, etc.) Closely tied to the rule that arguments are evaluated to values before function application. (Not true for all languages) In ML, many expressions have no such effects (x, #foo x,...); much fewer in Java. Efficiency? Could be faster or slower. (Why?) CSE 341 Autumn 2005, Lecture 10 12

Unnecessary Function Wrapping A common source of bad style for beginners Is e1 equivalent to fn x => e1 x? Sure, provided: e1 is effect-free x does not occur free in e1 Example: List.map (fn x => SOME x) lst List.map SOME lst CSE 341 Autumn 2005, Lecture 10 13

Summary We breezed through some core programming-language facts and design goals: Definition of equivalence depends on observable behavior Syntactic sugar makes a big language smaller by defining constructs in terms of equivalence Notion of free and bound variables crucial to understanding function equivalence. Three common forms of function equivalence: Systematic Renaming Argument Substitution Unnecessary Function Wrapping CSE 341 Autumn 2005, Lecture 10 14