CMSC 330: Organization of Programming Languages. Objects and Functional Programming

Similar documents
CMSC330. Objects, Functional Programming, and lambda calculus

OCaml Language Choices CMSC 330: Organization of Programming Languages

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

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

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

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

CMSC 330: Organization of Programming Languages

Anonymous Functions CMSC 330: Organization of Programming Languages

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

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC 330: Organization of Programming Languages. OCaml Data Types

CMSC 330, Fall 2013, Practice Problems 3

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

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

CMSC 330: Organization of Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CMSC330 Fall 2013 Practice Problems 6 Solutions

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

CMSC 430 Introduction to Compilers. Fall Everything (else) you always wanted to know about OCaml (but were afraid to ask)

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC330 Spring 2014 Midterm 2 Solutions

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

Computer Science II (20082) Week 1: Review and Inheritance

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

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

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

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

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

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

JAVA MOCK TEST JAVA MOCK TEST II

CMSC 330: Organization of Programming Languages. Objects and Abstract Data Types

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

CSCI-GA Scripting Languages

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

CS S-24 Final Review I 1

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Functional Programming. Introduction To Cool

A LISP Interpreter in ML

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

CMSC330 Spring 2017 Midterm #1 9:30am/11:00am/12:30pm/5:00pm

Class (1A) Young Won Lim 9/8/14

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CIS 120 Midterm II November 8, 2013 SOLUTIONS

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

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

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Topic 7: Algebraic Data Types

Programming Languages and Techniques (CIS120)

CMSC330 Fall 2014 Midterm 2 Solutions

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

CMSC330 Spring 2017 Midterm #1 Solution

CMSC 330: Organization of Programming Languages. Rust Basics

Mini-ML. CS 502 Lecture 2 8/28/08

CMSC 330: Organization of Programming Languages

An introduction introduction to functional functional programming programming using usin Haskell

CMSC 132: Object-Oriented Programming II

An introduction to C++ template programming

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

Class (1A) Young Won Lim 11/20/14

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Type Checking and Type Inference

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

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

CSCC24 Functional Programming Scheme Part 2

CMSC330 Fall 2015 Midterm #1 12:30pm/2:00pm/5:00pm

Introduction to OCaml

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

Computer Science II (20073) Week 1: Review and Inheritance

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

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

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Name: Pennkey: CIS 120 Midterm II November 18, 2011

Introduction to Programming Using Java (98-388)

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

CS558 Programming Languages

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

List Functions, and Higher-Order Functions

Programming Languages and Techniques (CIS120)

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

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

L3 Programming September 19, OCaml Cheatsheet

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

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Begin at the beginning

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

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

F28PL1 Programming Languages. Lecture 14: Standard ML 4

CMSC 631. Functional Programming with OCaml

Functional Programming. Introduction To Cool

Programming Languages and Techniques (CIS120)

Introduction to Functional Programming in Haskell 1 / 56

Transcription:

CMSC 330: Organization of Programming Languages Objects and Functional Programming

OOP vs. FP Object-oriented programming (OOP) Computation as interactions between objects Objects encapsulate state, which is usually mutable Ø Accessed / modified via object s public methods Functional programming (FP) Computation as evaluation of functions Ø Mutable data used to improve efficiency Higher-order functions implemented as closures Ø Closure = function + environment 2

Relating Objects to Closures An object... Is a collection of fields (data)...and methods (code) When a method is invoked Ø Method has implicit this parameter that can be used to access fields of object A closure... Is a pointer to an environment (data)...and a function body (code) When a closure is invoked Ø Function has implicit environment that can be used to access variables 3

Relating Objects to Closures class C { int x = 0; void set_x(int y) { x = y; int get_x() { return x; let make () = let x = ref 0 in ( (fun y -> x := y), (fun () ->!x) ) x = 0 x = ref 0 C c = new C(); c.set_x(3); int y = c.get_x(); fun y -> x := y fun () ->!x let (set, get) = make ();; set 3;; let y = get ();; 4

Encoding Objects with Closures We can apply this transformation in general becomes class C { f1... fn; m1... mn; let make () = let f1 =...... and fn =... in ( fun..., (* body of m1 *)... fun..., (* body of mn *) ) make () is like the constructor The closure environment contains the fields Tuple containing closures (could use record instead) 5

Quiz 1: Is Circle Encoded Correctly? class Circle { float r = 0; void set_r (float t) { r = t; float get_r () { return r; float area(){ return 3.14 * r * r; let make () = let r = 0.0 in ((fun y -> let r = y in ()), (fun () -> r), fun ()-> r *. r *. 3.14 ) C c = new Circle(); c.set_r(1.0); float y = c.get_r(); c.area(); A. True B. False let (set_r, get_r, area) = make ();; set_r 1.0;; let y = get_r();; area();; 6

Quiz 1: Is Circle Encoded Correctly? class Circle { float r = 0; void set_r (float t) { r = t; float get_r () { return r; float area(){ return 3.14 * r * r; let make () = let r = ref 0.0 in ((fun y -> let r := y in ()), (fun () ->!r), fun ()->!r *.!r *. 3.14 ) C c = new Circle(); c.set_r(1.0); float y = c.get_r(); c.area(); A. True B. False let (set_r, get_r, area) = make ();; set_r 1.0;; let y = get_r();; Area();; 7

Relating Closures to Objects A closure is like an object with a designated eval() method The type of eval corresponds to the type of the closure s function, T -> U interface Func<T,U> { U eval(t x); class G implements Func<T,U> { U eval(t x) { /* body of fn */ Environment is stored as field(s) of G 8

Relating Closures to Objects let add1 x = x + 1 interface IntIntFun { Integer eval(integer x); class Add1 implements IntIntFun { Integer eval(integer x) { return x + 1; add1 2;; add1 3;; new Add1().eval(2); new Add1().eval(3) 9

Quiz 2: What does this evaluate to? interface IntIntFun { Integer eval(integer x); class Foo implements IntIntFun { Integer eval(integer x) { return x * 2; new Foo(5); A. 5 B. 10 C. 6 D. None of the above 10

Quiz 2: What does this evaluate to? interface IntIntFun { Integer eval(integer x); class Foo implements IntIntFun { Integer eval(integer x) { return x * 2; new Foo(5); A. 5 B. 10 C. 6 D. None of the above (should be called new Foo().eval(5)) 11

Relating Closures to Objects let app_to_1 f = f 1 interface IntIntFunFun { Integer eval(intintfun x); class AppToOne implements IntIntFunFun { Integer eval(intintfun f) { return f.eval(1); app_to_1 add1;; new AppToOne().eval(new Add1()); 12

Quiz 3: What does this evaluate to? interface IntIntFun { Integer eval(integer x); class Foo implements IntIntFun { Integer eval(integer x) { return x * 2; interface IntIntFunFun { Integer eval(intintfun x); class AppToFive implements IntIntFunFun { Integer eval(intintfun f) { return f.eval(5); new AppToFive().eval (new Foo()); A. 5 B. 10 C. 6 D. Error 13

Quiz 3: What does this evaluate to? interface IntIntFun { Integer eval(integer x); class Foo implements IntIntFun { Integer eval(integer x) { return x * 2; interface IntIntFunFun { Integer eval(intintfun x); class AppToFive implements IntIntFunFun { Integer eval(intintfun f) { return f.eval(5); new AppToFive().eval (new Foo()); A. 5 B. 10 C. 6 D. Error 14

Relating Closures to Objects interface Func<T,U> { U eval(t x); class Add1 implements Func<Integer,Integer> { public Integer eval(integer x) { return x + 1; class AppToOne implements Func<Func<Integer,Integer>,Integer> { public Integer eval(func<integer,integer> f) { return f.eval(1); app_to_1 add1;; new AppToOne().eval(new Add1()); 15

Relating Closures to Objects let add a b = a + b;; a = 1 fun b -> a + b let add1 = add 1;; add1 4;; class Add implements Func<Int,Func<Int,Int>> { private static class AddClosure implements Func<Int,Int> { private final Int a; AddClosure(Int a) { this.a = a; Integer eval(int b) { return a + b; Func<Int,Int> eval(int x) { return new AddClosure(x); a = 1 Func<Int,Int> add1 = new Add().eval(1); add1.eval(4); 16

Encoding Closures with Objects We can apply this transformation in general becomes...(fun x -> (* body of fn *))... let h f... =...f y... interface F<T,U> { U eval(t x); class G implements F<T,U> { U eval(t x) { /* body of fn */ class C { Typ1 h(f<typ2,typ3> f,...) {...f.eval(y)... F is the interface of a closure s function G represents the particular function 17

Quiz 4: Are these two versions equivalent? let mult x y = x * y let f = mult 2 in f 3;; A. True B. False interface IntIntFun { Integer eval(integer x); class Mult implements IntIntFun { private int x; Mult(int x) { this.x = x Integer eval(integer y) { return x * y; Mult f = new Mult(2); f.eval(3); 18

Quiz 4: Are these two versions equivalent? let mult x y = x * y let f = mult 2 in f 3;; A. True B. False interface IntIntFun { Integer eval(integer x); class Mult implements IntIntFun { private int x; Mult(int x) { this.x = x Integer eval(integer y) { return x * y; Mult f = new Mult(2); f.eval(3); 19

Recall a Useful Higher-Order Function let rec map f = function [] -> [] (h::t) -> (f h)::(map f t) Map applies an arbitrary function f To each element of a list And returns the results collected in a list Can we encode this in Java? Using object-oriented programming 20

An Integer List Abstraction in Java public class MyList { private class ConsNode { int head; MyList tail; ConsNode(int h, MyList l){ head = h; tail = l; private ConsNode contents; public MyList () { contents = null; public MyList(int h, MyList l){ contents = new ConsNode (h, l); public MyList cons (int h) { return new MyList(h, this); public int hd () { return contents.head; public MyList tl () { return contents.tail; public boolean isnull () { return (contents == null); 21

A Map Method for Lists in Java Problem Write a map method in Java Must pass a function into another function Solution Can be done using an object with a known method Use interface to specify what method must be present public interface IntFunction { int eval(int arg); (can make this polymorphic but will keep it simpler for now) 22

A Map Method for Lists (cont.) Examples Two classes which both implement Function interface class AddOne implements IntFunction { int eval (int arg) { return (arg + 1); class MultTwo implements IntFunction { int eval(int arg) { return (arg * 2); 23

The New List Class class MyList { public MyList map (IntFunction f) { if (this.isnull()) return this; else return (this.tl()).map(f).cons (f.eval (this.hd())); 24

Applying Map To Lists Then to apply the function, we just do MyList l =...; MyList l1 = l.map(new AddOne()); MyList l2 = l.map(new MultTwo()); We make a new object Ø That has a method that performs the function we want This is sometimes called a callback Ø Because map calls back to the object passed into it But it s really just a higher-order function Ø Written more awkwardly 25

We Can Do This for Fold Also! Recall let rec fold f a = function [] -> a (h::t) -> fold f (f a h) t Fold accumulates a value (in a) as it traverses a list f is used to determine how to fold the head of a list into a This can be done in Java using an approach similar to map! 26

A Fold Method for Lists in Java Problem Write a fold method in Java Must pass a function into another function Solution Can be done using an object with a known method Use interface to specify what method must be present public interface IntBinFunction { Integer eval(integer arg1, Integer arg2); 27

A Fold Method for Lists (cont.) Examples A classes which implements IntBinFunction interface class Sum implements IntBinFunction { Integer eval(integer arg1, Integer arg2) { return new Integer(arg1 + arg2); Note: this is not curried How might you make it so? 28

The New List Class class MyList { public MyList map (IntFunction f) { if (this.isnull()) return this; else return (this.tl()).map(f).cons (f.eval (this.hd())); public int fold (IntBinFunction f, int a) { if (this.isnull()) return a; else return (this.tl()).fold(f, f.eval(a, this.hd())); 29

Applying Fold to Lists To apply the fold function, we just do this: MyList l =...; int s = l.fold (new Sum(), 0); The result is that s contains the sum of the elements in l 30

Java 8 eases the syntax Java 8 allows you to make objects that act as functions, more easily Instead of this Write this MyList l =...; MyList l1 = l.map(new AddOne()); MyList l2 = l.map(new MultTwo()); MyList l =...; MyList l1 = l.map((x) -> x + 1); MyList l2 = l.map((y) -> y * 2); 31

Code as Data Closures and objects are related Both of them allow Ø Data to be associated with higher-order code Ø Passing code around the program The key insight in all of these examples Treat code as if it were data Ø Allowing code to be passed around the program Ø And invoked where it is needed (as callback) Approach depends on programming language Higher-order functions (OCaml, Ruby, Lisp) Function pointers (C, C++) Objects with known methods (Java) 32

Code as Data This is a powerful programming technique Solves a number of problems quite elegantly Ø Create new control structures (e.g., Ruby iterators) Ø Add operations to data structures (e.g., visitor pattern) Ø Event-driven programming (e.g., observer pattern) Keeps code separate Ø Clean division between higher & lower-level code Promotes code reuse Ø Lower-level code supports different callbacks 33