Functional Programming. Introduction To Cool

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

Functional Programming. Introduction To Cool

Functional Programming. Introduction To Cool

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

History of Programming Languages. Functional Programming (1/2)

Cunning Plan. Gone In Sixty Seconds. History of Programming Languages. Functional Programming. Functional Programming. Review and Administrivia

History of Programming Languages. Functional Programming

Winter Compiler Construction Who. Mailing list and forum

CS558 Programming Languages

CSCI-GA Scripting Languages

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

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

Compiler construction 2009

The Substitution Model. Nate Foster Spring 2018

Types, Type Inference and Unification

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

So what does studying PL buy me?

Simply-Typed Lambda Calculus

SNU Programming Language Theory

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Passing Out Review Forms

Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values.

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

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

Some instance messages and methods

The Substitution Model

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

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

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CS558 Programming Languages

Lecture #23: Conversion and Type Inference

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

CS 11 Ocaml track: lecture 2

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

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

G Programming Languages - Fall 2012

More Static Semantics. Static. One-Slide Summary. Lecture Outline. Typing Rules. Dispatch Rules SELF_TYPE

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Lecture: Functional Programming

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm Solution

Programming Languages and Techniques (CIS120)

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

Type Checking and Type Inference

Introduction to OCaml

Some Advanced ML Features

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Programming Languages and Compilers (CS 421)

Object typing and subtypes

Programming Languages

CMSC330. Objects, Functional Programming, and lambda calculus

Functional Programming. Big Picture. Design of Programming Languages

ML Type Inference and Unification. Arlen Cox

More Lambda Calculus and Intro to Type Systems

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

NOTE: Answer ANY FOUR of the following 6 sections:

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

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

CMSC 330: Organization of Programming Languages

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

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

Software II: Principles of Programming Languages. Why Expressions?

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

CS 11 Haskell track: lecture 1

Programming Languages

CMSC 330, Fall 2013, Practice Problems 3

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Introduction to OCaml

Type Checking and Type Equality

Chapter 11 :: Functional Languages

CSE 341: Programming Languages

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Programming Languages

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

Case by Case. Chapter 3

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

Programming Languages

An introduction introduction to functional functional programming programming using usin Haskell

CS422 - Programming Language Design

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Harvard School of Engineering and Applied Sciences Computer Science 152

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Operational Semantics. One-Slide Summary. Lecture Outline

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

Programming Languages and Techniques (CIS120)

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

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

What about Object-Oriented Languages?

Programming Languages and Techniques (CIS120)

Transcription:

Functional Programming Introduction To Cool #1

Cunning Plan ML Functional Programming Fold Sorting Cool Overview Syntax Objects Methods Types #2

This is my final day... as your... companion... through Ocaml and Cool. After this we start the interpreter project. #3

One-Slide Summary Functions and type inference are polymorphic and operate on more than one type (e.g., List.length works on int lists and string lists). fold is a powerful higher-order function (like a swiss-army knife or duct tape). Cool is a Java-like language with classes, methods, private fields, and inheritance. #4

Pattern Matching (Error!) Simplifies Code (eliminates ifs, accessors) type btree = (* binary tree of strings *) Node of btree * string * btree Leaf of string let rec height tree = match tree with Leaf _ -> 1 Node(x,_,y) -> 1 + max (height x) (height y) let rec mem tree elt = match tree with bug? Leaf str Node(_,str,_) -> str = elt Node(x,_,y) -> mem x elt mem y elt #5

Pattern Matching (Error!) Simplifies Code (eliminates ifs, accessors) type btree = (* binary tree of strings *) Node of btree * string * btree Leaf of string let rec bad tree elt = match tree with Leaf str Node(_,str,_) -> str = elt Node(x,_,y) -> bad x elt bad y elt let rec mem tree elt = match tree with Leaf str Node(_,str,_) when str = elt -> true Node(x,_,y) -> mem x elt mem y elt #6

Pattern Matching Mistakes What if I forget a case? let rec is_odd x = match x with 0 -> false 2 -> false x when x > 2 -> is_odd (x-2) Warning P: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: 1 #7

Polymorphism Functions and type inference are polymorphic Operate on more than one type let rec length x = match x with [] -> 0 α means any hd :: tl -> 1 + length tl one type val length : α list -> int length [1;2;3] = 3 length [ algol ; smalltalk ; ml ] = 3 length [1 ; algol ] =? #8

Higher-Order Functions Function are first-class values Can be used whenever a value is expected Notably, can be passed around Closure captures the environment let rec map f lst = match lst with f is itself a [] -> [] function! hd :: tl -> f hd :: map f tl val map : (α -> β) -> α list -> β list let offset = 10 in let myfun x = x + offset in val myfun : int -> int map myfun [1;8;22] = [11;18;32] Extremely powerful programming technique General iterators Implement abstraction #9

The Story of Fold We ve seen length and map We can also imagine sum product and or filter reverse mem [1; 5; 8 ] [1; 5; 8 ] [true; true; false ] [true; true; false ] (fun x -> x>4) [1; 5; 8] [1; 5; 8] 5 [1; 5; 8] = 14 = 40 = false = true = [5; 8] = [8; 5; 1] = true Can we build all of these? #10

The House That Fold Built The fold operator comes from Recursion Theory (Kleene, 1952) let rec fold f acc lst = match lst with [] -> acc hd :: tl -> fold f (f acc hd) tl val fold : (α -> β -> α) -> α -> β list -> α Imagine we re summing a list (f = addition): 9 2 7 4 5 acc lst 11 7 4 5 f 18 4 5 27 #11

It s Lego Time Let s build things out of Fold! length lst = fold (fun acc elt -> acc + 1) 0 lst sum lst = fold (fun acc elt -> acc + elt) 0 lst product lst= fold (fun acc elt -> acc * elt) 1 lst and lst = fold (fun acc elt -> acc & elt) true lst How would we do or? How would we do reverse? #12

Tougher Legos Examples: reverse lst = fold (fun acc e -> acc @ [e]) [] lst Note typing: (acc : α list) (e : α) filter keep_it lst = fold (fun acc elt -> if keep_it elt then elt :: acc else acc) [] lst mem wanted lst = fold (fun acc elt -> acc wanted = elt) false lst Note typing: (acc : bool) (e : α) How do we do map? Recall: map (fun x -> x +10) [1;2] = [11;12] Let s write it on the board #13

Map From Fold let map myfun lst = fold (fun acc elt -> (myfun elt) :: acc) [] lst Types: (myfun : α -> β) Types: (lst : α list) Do nothing which is of no use. - Miyamoto Musashi, 1584-1645 Types: (acc : β list) Types: (elt : α) How do we do sort? (sort : (α * α -> bool) -> α list -> α list) #14

Sorting Examples langs = [ fortran ; algol ; c ] courses = [ 216; 333; 415] sort (fun a b -> a < b) langs [ algol ; c ; fortran ] sort (fun a b -> a > b) langs [ fortran ; c ; algol ] Java uses Inner Classes for this. sort (fun a b -> strlen a < strlen b) langs [ c ; algol ; fortran ] sort (fun a b -> match is_odd a, is_odd b with true, false -> true (* odd numbers first *) false, true -> false (* even numbers last *) _, _ -> a < b (* otherwise ascending *)) courses [ 333 ; 415 ; 216 ] #15

Partial Application and Currying let myadd x y = x + y val myadd : int -> int -> int myadd 3 5 = 8 let addtwo = myadd 2 How do we know what this means? We use referential transparency! Basically, just substitute it in. val addtwo : int -> int addtwo 77 = 79 Currying: if you fix some arguments, you get a function of the remaining arguments #16

ML, Python and Ruby all support functional programming closures, anonymous functions, etc. ML has strong static typing and type inference (as in this lecture) Ruby and Python have strong dynamic typing (or duck typing) All three combine OO and Functional although it is rare to use both. #17

Cool Overview Classroom Object-Oriented Language Design to Be implementable in one semester Give a taste of implementing modern features Abstraction Static Typing Inheritance Memory management And more... But many grungy things are left out #18

A Simple Example class Point { x : Int <- 0; y : Int <- 0; }; Cool programs are sets of class definitions A special Main class with a special method main Like Java class = a collection of fields and methods Instances of a class are objects #19

Cool Objects class Point { x : Int <- 0; y : Int; (* use default value *) }; The expression new Point creates a new object of class Point An object can be thought of as a record with a slot for each attribute (= field) x 0 y 0 #20

Methods class Point { x : Int <- 0; y : Int <- 0; movepoint(newx : Int, newy : Int) : Point { { x <- newx; y <- newy; self; } -- close block expression }; -- close method }; -- close class A class can also define methods for manipulating its attributes Methods refer to the current object using self #21

Aside: Semicolons class Point { x : Int <- 0; y : Int <- 0; movepoint(newx : Int, newy : Int) : Point { { x <- newx; y <- newy; self; } -- close block expression }; -- close method }; -- close class Yes, it's somewhat arbitrary. Still, don't get it wrong. #22

Information Hiding Methods are global Attributes are local to a class They can only be accessed by that class's methods class Point { x : Int <- 0; y : Int <- 0; getx () : Int { x } ; setx (newx : Int) : Int { x <- newx }; }; #23

Methods and Object Layout Each object knows how to access the code of its methods As if the object contains a slot pointing to the code x y getx setx 0 0 * * In reality, implementations save space by sharing these pointers among instances of the same class x y methods 0 0 * getx setx #24

Inheritance We can extend points to color points using subclassing => class hierarchy class ColorPoint extends Point { color : Int <- 0; movepoint(newx:int, newy:int) : Point { { color <- 0; x <- newx; y <- newy; self; } Note references to fields x y They're defined in Point! }; }; x 0 y 0 color 0 movepoint * #25

Kool Types Every class is a type Base (built-in, predefined) classes: Int Bool String Object for integers for booleans: true, false for strings root of class hierarchy All variables must be declared compiler infers types for expressions (like Java) #26

Cool Type Checking x : Point; x <- new ColorPoint;... is well-typed if Point is an ancestor of ColorPoint in the class hierarchy Anywhere a Point is expected, a ColorPoint can be used (Liskov,...) Rephrase:... is well-typed if ColorPoint is a subtype of Point Type safety: a well-typed program cannot result in run-time type errors #27

Method Invocation and Inheritance Methods are invoked by (dynamic) dispatch Understanding dispatch in the presence of inheritance is a subtle aspect of OO p : Point; p <- new ColorPoint; p.movepoint(1,2); p has static type Point p has dynamic type ColorPoint p.movepoint must invoke ColorPoint version #28

Other Expressions Cool is an expression language (like Ocaml) Every expression has a type and a value Conditionals if E then E else E fi Loops while E loop E pool Case/Switch case E of x : Type => E ;... esac Assignment x <- E Primitive I/O out_string(e), in_string(),... Arithmetic, Logic Operations,... Missing: arrays, floats, interfaces, exceptions Plus: you tell me! #29

Cool Memory Management Memory is allocated every time new E executes Memory is deallocated automatically when an object is not reachable anymore Done by a garbage collector (GC) #30

Course Project A complete interpreter Cool Source ==> Executed Program No optimizations Also no GC Split in 4 programming assignments (PAs) There is adequate time to complete assignments But start early and follow directions PA2-4 ==> individual or teams (of max 2) #31

Homework Wednesday: PA 0 due Thursday: Chapters 2.1-2.2 Thursday: Dijkstra Paper Bonus for getting this far: questions about fold are very popular on tests! If I say write me a function that does foozle to a list, you should be able to code it up with fold. #32