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

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

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

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

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

CMSC 330: Organization of Programming Languages

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

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

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

Introduction to OCaml

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

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

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

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

Programming Languages

Recap: Functions as first-class values

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

CS558 Programming Languages

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

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

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

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

Programming Languages

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

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

CS558 Programming Languages

Types and Type Inference

Anonymous Functions CMSC 330: Organization of Programming Languages

Programming Languages and Techniques (CIS120)

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

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

CS558 Programming Languages

Introduction to Functional Programming and Haskell. Aden Seaman

TYPES, VALUES AND DECLARATIONS

CSC 533: Organization of Programming Languages. Spring 2005

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

Type Checking and Type Inference

So what does studying PL buy me?

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

Today. Recap from last Week. Factorial. Factorial. How does it execute? How does it execute? More on recursion. Higher-order functions

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

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

Dynamically-typed Languages. David Miller

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

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

Functional Languages. Hwansoo Han

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Semester Review CSC 301

Today s Plan. Programming Languages. Example : Factorial. Recursion. CSE 130 : Spring Lecture 6: 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

Programming Languages 2nd edition Tucker and Noonan"

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

Types and Type Inference

Types, Type Inference and Unification

Programming Languages

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?

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

CSE 307: Principles of Programming Languages

CS457/557 Functional Languages

COMP 524 Spring 2018 Midterm Thursday, March 1

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

Programming Languages, Summary CSC419; Odelia Schwartz

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

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

Factorial. Next. Factorial. How does it execute? Tail recursion. How does it execute? More on recursion. Higher-order functions

Programming Languages and Techniques (CIS120)

CSCI-GA Scripting Languages

Object Oriented Paradigm

Types. What is a type?

Programming Languages and Techniques (CIS120)

Concepts of Programming Languages

Next. Tail Recursion: Factorial. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial.

Programming Languages

Lecture #23: Conversion and Type Inference

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

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

Classes, Objects, and OOP in Java. June 16, 2017

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

Concepts of programming languages

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

Programming Languages

CS558 Programming Languages

Compilers and computer architecture: Semantic analysis

CMSC330 Spring 2014 Midterm 2 Solutions

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

CMSC 330: Organization of Programming Languages

Functional Programming

Overloading, Type Classes, and Algebraic Datatypes

CS 11 Haskell track: lecture 1

301AA - Advanced Programming [AP-2017]

! 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 OCaml

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

StackVsHeap SPL/2010 SPL/20

CMSC330 Fall 2014 Midterm 2 Solutions

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

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

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Transcription:

News Programming Assignment 4 is up, due Monday Feb 16, 5pm Long assignment, start early Midterm Thursday Feb 12 covers only ocaml part of the course, assignments 1 through 3. Recap: Inferring types Introduce unknown type vars Figure out equalities i that must hold, and solve these equalities Example 5 let rec map f l = match l with [] -> [] h::t ->(f h)::(map f t) Example 5 let rec map f l = match l with [] -> [] h::t ->(f h)::(map f t) 3 4

Inferring types with a Introduce unknown type vars Example 6 let compose (f,g) x = f (g x) Figure out equalities i that must hold, and solve these equalities Remaining types vars get a forall and thus become the a, b, etc. 5 6 Example 6 let compose (f,g) x = f (g x) Example 7 let rec fold f cur l = match l with [] -> cur h::t -> fold f (f h cur) t 7 8

Example 7 let rec fold f cur l = match l with [] -> cur h::t -> fold f (f h cur) t Deconstructing OCaml What makes up a language 9 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

In Prolog In Prolog Logical facts Inference rules Mexican(CARNITAS) Fact Food(CARNITAS) Fact Mexican(X) Food(X) Delicious(X) Rule Delicious(CARNITAS) Fact 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 type- correct 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 In Python: Dynamic typing Types assigned to values/objects as they are computed, ie: dynamically Before an operation is performed, check that t 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 Python, OO Java Smalltalk, Diesel Functional Functional Ocaml, Haskell Lisp/Scheme Polymorphism Name a language g that is dynamically y 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

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 g 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. B t th h d 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 d more easily They can also be parallelized more easily So what? Form the authors: Inspired by similar primitives in LISP and other languages http://labs.google.com/papers/mapreduce-osdi04-slides/index-auto-0003.html g p p p 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 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