CSE 341: Programming Languages

Similar documents
CSE341: Programming Languages Interlude: Course Motivation. Zach Tatlock Winter 2018

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

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

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

CSE 341: Programming Languages

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

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

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

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

CSE 341: Programming Languages

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

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

CSE 341 Section 5. Winter 2018

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

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

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

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

CSE 505: Concepts of Programming Languages

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

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

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

Programming Languages

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

Introduction to OCaml

Functional Programming

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

CSE341: Programming Languages Lecture 22 OOP vs. Functional Decomposition; Adding Operators & Variants; Double-Dispatch. Dan Grossman Autumn 2018

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

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

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

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

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

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

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

Part I: Written Problems

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

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

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

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

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

CSE 341 : Programming Languages

Programming Languages

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE : Fall 2017

CS 11 Haskell track: lecture 1

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

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE 130 : Fall 2013

CSE341 Spring 2017, Final Examination June 8, 2017

CSCI-GA Scripting Languages

Programming Languages

Programming Languages

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

A Practical Optional Type System for Clojure. Ambrose Bonnaire-Sergeant

CSE341 Spring 2017, Final Examination June 8, 2017

Lecture 1: Overview

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

The Environment Model. Nate Foster Spring 2018

A Programming Language. A different language g is a different vision of life. CSE 130 : Fall Two variables. L1: x++; y--; (y=0)?

Functions. Nate Foster Spring 2018

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE 130 : Fall 2014

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Spring 2015

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

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Fall 2015

LECTURE 1. Overview and History

CS152: Programming Languages. Lecture 5 Pseudo-Denotational Semantics. Dan Grossman Spring 2011

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

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

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

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

Programming Languages

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

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

Lecture 3: Recursion; Structural Induction

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

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

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert

CSE341, Spring 2013, Final Examination June 13, 2013

Recap: Functions as first-class values

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

A Programming Language. A different language g is a different vision of life. CSE 130 : Spring Two variables. L1: x++; y--; (y=0)?

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

Compilers: The goal. Safe. e : ASM

CSci 4223 Lecture 18 April 3, 2013 Topics: OOP vs. FP

CSC209. Software Tools and Systems Programming.

! Broaden your language horizons. ! Study how languages are implemented. ! Study how languages are described / specified

CS558 Programming Languages

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

CS153: Compilers Lecture 15: Local Optimization

We made it! Java: Assembly language: OS: Machine code: Computer system:

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

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

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

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

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

Scala, Your Next Programming Language

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CSE 341, Autumn 2015, Ruby Introduction Summary

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods. Dan Grossman Autumn 2018

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

Transcription:

CSE 341: Programming Languages Dan Grossman Spring 2008 Lecture 6 Nested pattern-matching; course motivation Dan Grossman CSE341 Spring 2008, Lecture 6 1

Patterns What we know: case-expresssions do pattern-matching to choose branch val-bindings and fun-arguments also do pattern-matching All functions take one argument Can match datatypes (including lists, options) and records (including tuples) The full story is more general patterns are much richer than we have let on. Dan Grossman CSE341 Spring 2008, Lecture 6 2

Deep patterns The full definition of pattern-matching is recursive, processing the matched-on value and the pattern together. A pattern can be: A variable (matches everything, introduces a binding) _ (matches everything, no binding) A constructor C (matches value C, if C carries no data) A constructor and a pattern (e.g., C p) (matches a value if the value is a C and p matches the value it carries) A pair of patterns ((p1,p2)) (matches a pair if p1 matches the first component and p2 matches the second component) A record pattern...... Dan Grossman CSE341 Spring 2008, Lecture 6 3

Can you handle the truth? It s really: case e of p1 => e1... pn => en val p = e fun f p1 = e1 f p2 = e2... f pn = en Inexhaustive matches may raise exceptions and are bad style. Example: could write pattern Add pr or Add (e1,e2) Again: The definition of pattern-matching is recursive over the value-being-matched and the pattern. _ and binding a variable are just base cases. Dan Grossman CSE341 Spring 2008, Lecture 6 4

Some function examples fun f1 () = 34 fun f2 = 34 fun f3 (x,y) = x + y fun f4 pr = let val (x,y) = pr in x + y end Is there any difference to callers between f3 and f4? In most languages, argument lists are syntactically separate, second-class constructs. Can be useful: f3 (if e1 then (3,2) else pr) (We discussed this on Wednesday too.) See lec6.sml for a few examples where nested patterns are quite nice. Dan Grossman CSE341 Spring 2008, Lecture 6 5

Course Motivation I owe you an answer to why 341 has material worth learning. 1. Why learn programming languages that are quite different from Java, C, C++? 2. Why learn the fundamental concepts that appear in all (most?) programming languages? 3. Why focus on functional programming (avoiding mutation, embracing recursion, and writing functions that take/return other functions)? Dan Grossman CSE341 Spring 2008, Lecture 6 6

A couple questions... What s the best car? What are the best kind of shoes? Dan Grossman CSE341 Spring 2008, Lecture 6 7

Aren t all languages the same? Yes: Any input-output behavior you can program in language X you can program in language Y Java, ML, and a language with one loop and three infinitely-large integers are equal This is called the Turing tarpit Yes: Certain fundamentals appear in most languages (variables, abstraction, one-of types, recursive definitions,...) Travel to learn more about where you re from ML, Scheme, Ruby well-suited for letting these fundamentals shine No: Most cars have 4 tires, 2 headlights,... Mechanics learn general principles and what s different Dan Grossman CSE341 Spring 2008, Lecture 6 8

Aren t the semantics my least concern? Admittedly, there are many important considerations: What libraries are available? What can get me a summer internship? What does my boss tell me to do? What is the de facto industry standard? What do I already know? Technology leaders affect the answers to these questions. Sound reasoning about programs, interfaces, and compilers requires knowledge of semantics. And there is a place in universities for learning deep truths and beautiful insights as an end in itself. (Like watching Hamlet.) Dan Grossman CSE341 Spring 2008, Lecture 6 9

Aren t languages somebody else s problem? If you design an extensible software system, you ll end up designing a (small?) programming language! Examples: VBScript, JavaScript, PHP, ASP, QuakeC, Renderman, bash, AppleScript, emacs, Eclipse, AutoCAD,... Dan Grossman CSE341 Spring 2008, Lecture 6 10

Functional programming Okay, so why ML and Scheme where: Mutation is discouraged Datatype-based one-of types Higher-order functions (next week) Because: 1. These features are invaluable for correct, elegant, efficient software (great way to think about computation). 2. Functional languages have a history of being ahead of their time 3. They are well-suited to where computing is going (multicore and data centers) Much of the course is (1), so let s give an infomercial for (2) and (3)... Dan Grossman CSE341 Spring 2008, Lecture 6 11

Ahead of their time Garbage collection (Java didn t exist in 1995, SML & Scheme did) Generics (List<T> in Java, C#), much more like SML than C++ XML for universal data representation (like Scheme / Lisp) Function closures in Python, Ruby, etc. Ruby s iterators lifted from CLU (another useless language )... All features dismissed as, fine for academics, but will never make it in the real world. Maybe datatypes or currying or multimethods will be next... Conquering vs. assimilation Dan Grossman CSE341 Spring 2008, Lecture 6 12

Recent Surge F# C# 3.0 Multicore computing (no mutation = easier to parallelize) MapReduce / Hadoop (first published in 2004) Small companies (Jane Street, Galois, many others) And not so small (Ericsson s Erlang) All consider functional programming a key competitive advantage In part for hiring smarter people Lots of research projects (e.g., Macah compiler at UW) Note: None of these examples use SML or Scheme, but that s okay: think how much you ve learned in the last 10 days. Dan Grossman CSE341 Spring 2008, Lecture 6 13

Summary There is no such thing as a best programming language. (There are good general design principles we will study.) A good language is a relevant, crisp, and clear interface for writing software. Software leaders should know about programming languages. Learning languages has super-linear payoff. But you have to learn the semantics and idioms, not a cute syntactic trick for printing Hello World. Functional languages have been on the leading edge for decades, but ideas get absorbed by the masses slowly. Perhaps things are starting to change? Even if not, it will make you a better Java/C programmer Dan Grossman CSE341 Spring 2008, Lecture 6 14