CSE 341: Programming Languages

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

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

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

Introduction to OCaml

Begin at the beginning

Functions. Nate Foster Spring 2018

Programming Languages

Programming Languages Dan Grossman Welcome to the Class!

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

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Data Types. Guest Lecture: Andrew Myers Spring 2018

Preparing for Class: Watch le Videos! What is an ML program?

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CSE 341: Programming Languages

Operational Semantics. One-Slide Summary. Lecture Outline

Programming Languages

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

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

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

Programming Languages

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

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

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

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

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

Data Types. Prof. Clarkson Fall Today s music: Pokémon Theme by Jason Paige

Programming Languages

CSE 341: Programming Languages

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

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

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

CSE 341: Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

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

CSE 341 Section 5. Winter 2018

Operational Semantics of Cool

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

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

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

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

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Programming Languages

A Brief Introduction to Standard ML

CSE 341 : Programming Languages

Types and Programming Languages. Lecture 5. Extensions of simple types

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

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

Warm-up and Memoization

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS 360: Programming Languages Lecture 12: More Haskell

Operational Semantics

CMSC 330: Organization of Programming Languages. Rust Basics

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

378: Machine Organization and Assembly Language

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

Lists. Michael P. Fourman. February 2, 2010

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

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

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

CSE 341: Programming Languages

Part I: Written Problems

6.001 Notes: Section 15.1

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CSE341: Programming Languages Lecture 26 Course Victory Lap. Dan Grossman Spring 2016

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

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Scheme as implemented by Racket

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Dynamic Types , Spring March 21, 2017

CS558 Programming Languages

Lecture 2: SML Basics

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

Programming Languages

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

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

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

CSE 374 Programming Concepts & Tools

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

The Environment Model. Nate Foster Spring 2018

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

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

Product types add could take a single argument of the product type int * int fun add(,y):int = +y; > val add = fn : int * int -> int add(3,4); > val i

Arrays. myints = new int[15];

Intro. Scheme Basics. scm> 5 5. scm>

CSE 332: Data Abstractions. Ruth Anderson Spring 2014 Lecture 1

6.001 Notes: Section 6.1

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Transcription:

CSE 341: Programming Languages Hal Perkins Spring 2011 Lecture 2 Functions, pairs, and lists Hal Perkins CSE341 Spring 2011, Lecture 2 1

What is a programming language? Here are separable concepts for defining and evaluating a language: syntax: how do you write the various parts of the language? semantics: what do programs mean? (One way to answer: what are the evaluation rules?) idioms: how do you typically use the language to express computations? libraries: does the language provide standard facilities such as file-access, hashtables, etc.? How? tools: what is available for manipulating programs in the language? Hal Perkins CSE341 Spring 2011, Lecture 2 2

Our focus This course: focus on semantics and idioms to make you a better programmer Reality: Good programmers know semantics, idioms, libraries, and tools Libraries are crucial, but you can learn them on your own. Hal Perkins CSE341 Spring 2011, Lecture 2 3

Goals for today Add some more absolutely essential ML constructs Discuss lots of first-week gotchas Enough to do most problems on first homework (rest after Friday) And we will learn better constructs soon andalso, orelse also quite useful Note: These slides make much more sense in conjunction with lec2.sml. Recall a program is a sequence of bindings... Hal Perkins CSE341 Spring 2011, Lecture 2 4

Function Definitions... A second kind of binding is for functions (kinda like Java methods without fields, classes, statements,...) Syntax: Typing rules: fun x0 (x1 : t1,..., xn : tn) = e 1. Context for e is (the function s context extended with) x1:t1,..., xn:tn and: 2. x0 : (t1 *... * tn) -> t where: 3. e has type t in this context (This definition is circular because functions can call themselves and the type-checker guessed t.) (It turns out in ML there is always a best guess and the type-checker can always make that guess. For now, it s magic.) Evaluation: A function is a value. Hal Perkins CSE341 Spring 2011, Lecture 2 5

Function Applications (a.k.a. Calls) Syntax: e0 (e1,...,en) (parens optional for one argument) Typing rules (all in the application s context): 1. e0 must have some type (t1 *... * tn) -> t 2. ei must have type ti (for i=1,..., i=n) 3. e0 (e1,...,en) has type t Evaluation rules: 1. e0 evaluates to a function f in the application s environment 2. ei evaluates to value vi in the application s environment 3. result is f s body evaluated in an environment extended to bind xi to vi (for i=1,..., i=n). ( an environment is actually the environment where f was defined) Hal Perkins CSE341 Spring 2011, Lecture 2 6

Some Gotchas The * between argument types (and pair-type components) has nothing to do with the * for multiplication In practice, you almost never have to write argument types But you occasionally need them And it can improve error messages and your understanding Type inference is a very cool thing in ML Types unneeded for other variables or function return-types Context and environment for a function body includes: Previous bindings Function arguments The function itself But not later bindings Hal Perkins CSE341 Spring 2011, Lecture 2 7

Recursion A function can be defined in terms of itself. This makes sense if the calls to itself (recursive calls) solve simpler problems. This is more powerful than loops and often more convenient. Many, many examples to come in 341. Hal Perkins CSE341 Spring 2011, Lecture 2 8

Pairs Our first way to build compound data out of simpler data: Syntax to build a pair: (e1,e2) If e1 has type t1 and e2 has type t2 (in current context), then (e1,e2) has type t1*t2. (It would be nice if it were (t1,t2), but it isn t.) If e1 evaluates to v1 and e2 evaluates to v2 (in current environment), then (e1,e2) evaluates to (v1,v2). (Pairs of values are values.) Syntax to get part of a pair: #1 e or #2 e. Type rules for getting part of a pair: Evaluation rules for getting part of a pair: Hal Perkins CSE341 Spring 2011, Lecture 2 9

Tuples Actually, you can have tuples with any number of parts: (e1,e2,...,en) t1 * t2 *... * tn #n e for any number n Hal Perkins CSE341 Spring 2011, Lecture 2 10

Lists We can have pairs of pairs of pairs... but we still commit to the amount of data when we write down a type. Lists can have any number of elements: [] is the empty list (a value) More generally, [v1,v2,...,vn] is a length n list If e1 evaluates to v and e2 evaluates to a list [v1,v2,...,vn], then e1::e2 evaluates to [v,v1,v2,...,vn] (a value). null e evaluates to true if and only if e evaluates to [] If e evaluates to [v1,v2,...,vn], then hd e evaluates to v1 and tl e evaluates to [v2,...,vn]. If e evaluates to [], a run-time exception is raised (this is different than a type error; more on this later) Hal Perkins CSE341 Spring 2011, Lecture 2 11

List types A given list s elements must all have the same type. If the elements have type t, then the list has type t list. Examples: int list, (int*int) list, (int list) list. What are the type rules for ::, null, hd, and tl? Possible exceptions do not affect the type. Hmmm, that does not explain the type of []? It can have any list type, which is indicated via a list. That is, we can build a list of any type from []. Polymorphic types are 3 weeks ahead of us. Teaser: null, hd, and tl are not keywords! Hal Perkins CSE341 Spring 2011, Lecture 2 12

Recursion again Functions over lists that depend on all list elements will be recursive: What should the answer be for the empty list? What should the answer be for a non-empty list? (Typically in terms of the answer for the tail of the list!) Functions that produce lists of (potentially) any size will be recursive: When do we create a small (e.g., empty) list? How should we build a bigger list out of a smaller one? Hal Perkins CSE341 Spring 2011, Lecture 2 13

Sharing, no mutation, etc. Does tl copy the list or share its result with the tail of its argument? What about our elegant append? It doesn t matter!!! All that worrying you did in Java about aliasing, object identity, copying versus updating, equal vs. same-object is only relevant when you have assignment statements! A great reason not to use them. In ML, if append ([1,2],[3,4,5]) produces [1,2,3,4,5], you cannot tell how much sharing there is, so you don t have to think about it. Implementation tends to get the efficiency of sharing (tl is fast and doesn t make a new list), but without mutation there are no complications. Hal Perkins CSE341 Spring 2011, Lecture 2 14