Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

Similar documents
Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

Key components of a lang. Deconstructing OCaml. In OCaml. Units of computation. In Java/Python. In OCaml. Units of computation.

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

From last time. News. Example. Example. Typechecking let. Typechecking let. Pick up your first two quizzes after class. Assignment #2 due tomorrow

Parametric types. aka: what s up with those a???

CSE 130 : Fall Programming Languages. Lecture 11: Ranjit Jhala UC San Diego. programming

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

CMSC 330: Organization of Programming Languages

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Introduction to OCaml

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

Introduction to Functional Programming and Haskell. Aden Seaman

Generic polymorphism on steroids

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Recap: Functions as first-class values

Functional Languages. Hwansoo Han

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

Semester Review CSC 301

Types and Type Inference

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

Programming Languages

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

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

CS558 Programming Languages

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

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

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

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

CS558 Programming Languages

CS 11 Haskell track: lecture 1

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

Type Checking and Type Inference

CSCI-GA Scripting Languages

Anonymous Functions CMSC 330: Organization of Programming Languages

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

Programming Languages

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

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

CSC 533: Organization of Programming Languages. Spring 2005

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

Concepts of programming languages

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

Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

CSE 307: Principles of Programming Languages

Programming Languages

301AA - Advanced Programming [AP-2017]

Lecture 12: Data Types (and Some Leftover ML)

Types and Type Inference

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Types, Type Inference and Unification

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CMSC 330, Fall 2013, Practice Problems 3

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

Overview. Elements of Programming Languages. Another example. Consider the humble identity function

Programming Languages

Types. What is a type?

So what does studying PL buy me?

Lecture #23: Conversion and Type Inference

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Is Functional Programming (FP) for me? ACCU Conference 2008 Hubert Matthews

Some Advanced ML Features

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

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

Programming Languages, Summary CSC419; Odelia Schwartz

CS558 Programming Languages

CMSC 330: Organization of Programming Languages

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

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

TYPES, VALUES AND DECLARATIONS

Programming Languages 2nd edition Tucker and Noonan"

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

StackVsHeap SPL/2010 SPL/20

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

Functional Programming. Introduction To Cool

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Objects as Session-Typed Processes

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

CS558 Programming Languages

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

CSE 130, Fall 2005: Final Examination

Object Oriented Paradigm

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

The role of semantic analysis in a compiler

Pierce Ch. 3, 8, 11, 15. Type Systems

CS558 Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

FUNCTIONAL PROGRAMMING

CS383 PROGRAMMING LANGUAGES. Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

Dynamically-typed Languages. David Miller

Transcription:

Summary of polymorphism Subtype Parametric Bounded F-bounded Back to OCaml Polymorphic types allow us to reuse code However, not always obvious from staring at code But... Types never entered w/ program! 1 2 Example 1 Type inference aka: how in the world does Ocaml figure out all the types for me??? let x = 2 + 3;; let y = string_of_int x;; 3 4 Example 2 let x = 2 + 3;; let inc y = x + y;; Example 2 let x = 2 + 3;; let inc y = x + y;; 5 6 1

Example 3 let foo x = let (y,z) = x in z-y;; Example 3 let foo x = let (y,z) = x in z-y;; 7 8 Example 4 Example 4 ML doesn t know what the function does, or even that it terminates. ML only knows its type! let rec cat l = [] -> h::t -> h^(cat t) let rec cat l = [] -> h::t -> h^(cat t) 9 10 Example 5 let rec map f l = [] -> [] h::t ->(f h)::(map f t) Example 5 let rec map f l = [] -> [] h::t ->(f h)::(map f t) 11 12 2

Inferring types with a Introduce unknown type vars Example 6 let compose (f,g) x = f (g x) Figure out equalities that must hold, and solve these equalities Remaining types vars get a forall and thus become the a, b, etc. 13 14 Example 6 let compose (f,g) x = f (g x) Example 7 let rec fold f cur l = [] -> cur h::t -> fold f (f h cur) t 15 16 Example 7 let rec fold f cur l = [] -> cur h::t -> fold f (f h cur) t Deconstructing OCaml What makes up a language 17 3

Key components of a lang Units of computation Types Units of computation Memory model In OCaml In OCaml Expressions that evaluate to values Everything is an expression int, bool, real if-then-else let-in match fn x -> x+1 e1 e2 In Java/Python In Java/Python Store and update commands Message sends 4

In Prolog In Prolog Logical facts Inference rules Mexican(CARNITAS) Food(CARNITAS) Mexican(X) Food(X) Delicious(X) Delicious(CARNITAS) Fact Fact Fact Rule Types Types Used to classify things created by the programmer Classification used to check what can be done with/to those things In OCaml: Static typing Types are assigned statically at compile time Without computing values Rules state when expressions are typecorrect e1:t1 T2 e2: T1 e1 e2 : T2 In OCaml: Static typing How can one reuse code for different types? parametric types: a * b -> b * a implicit forall Type discovered (inferred) automatically from code less burden on the programmer 5

In Python: Dynamic typing Types assigned to values/objects as they are computed, ie: dynamically Before an operation is performed, check that operands are compatible with operation In Python: Dynamic typing More programs are accepted by compiler More flexible, but find errors late [1, abc, 1.8, [ efg, 20]] let x = if b then 1 else abc let y = if b then x + 1 else x ^ efg Dynamic vs. Static, OO vs. Func Dynamic vs. Static, OO vs. Func Statically typed Dynamically typed Statically typed Dynamically typed OO OO Java Python, Smalltalk Functional Functional Ocaml, Haskell Lisp/Scheme Polymorphism Can a language be dynamically typed, but not polymorphic? Every dynamically typed language is polymorphic functions just simply work on any datatype that can be operated on at runtime Memory/Data model aka: what do variables refer to? Only need explicit polymorphism in statically typed languages to assign at compile time a suitably general polymorphic type 6

Data model in functional langs Environment of bindings (phonebook) Never change a binding add new bindings at the end of the phonebook Data model in functional langs Variables are names that refer into the phonebook Most recent entry looked up during evaluation Environment frozen inside function value so that the behavior of the function cannot be changed later on (easier reasoning) Data model in OO langs Variables are cells in memory Can change them by assigning into them Variables point to objects on the heap x = x + 10 Data model in Prolog Variables in Prolog are unknowns to solve for Mexican(CARNITAS) Food(CARNITAS) X Mexican(X) Food(X) Delicious(X) Delicious(Y)? Q: What is delicious? A: CARNITAS! What s the point of all this? Final words on functional programming 7

Advantages of functional progs Functional programming more concise one line of lisp can replace 20 lines of C (quote from http://www.ddj.com/dept/architect/184414500?pgno=3) Recall reverse function in OCaml: let reverse = fold (::) [];; How many lines in C, C++? Don t be fooled Some of the programming assignments made you do certain things using fold in order to force you to think about it, even though using fold was not the easiest way to do it. But there are many cases where map and fold make life A LOT EASIER. Can better reason about progs No side effects. Call a function twice with same params, produces same value As a result, computations can be reordered more easily They can also be parallelized more easily So what? From the authors: Inspired by similar primitives in LISP and other languages http://labs.google.com/papers/mapreduce-osdi04-slides/index-auto-0003.html The point is this: programmers who only know Java/C/C++ would probably not have come up with this idea Many other similar examples in industry This stuff is for real: F# F# = Microsoft s Ocaml-on-steroids http://channel9.msdn.com/pdc2008/tl11/ Why FP is way cool How FP works with Objects (C#) How FP allows you to write parallel code all with an extremely engaging speaker Remember The next time you use google, think of how functional programming has inspired some of the technical ideas behind their engine And of course: Free your mind -Morpheus 8