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

Similar documents
On Understanding Data Abstraction... Revisited

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

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

CMSC 330, Fall 2013, Practice Problems 3

Last time: generic programming

CMSC 330, Fall 2013, Practice Problem 3 Solutions

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

Programming Languages and Techniques (CIS120)

CS3110 Spring 2016 Lecture 6: Modules

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

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

CMSC330. Objects, Functional Programming, and lambda calculus

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

CMSC330 Fall 2013 Practice Problems 6 Solutions

Recap: Functions as first-class values

CMSC 330: Organization of Programming Languages

Coq. LASER 2011 Summerschool Elba Island, Italy. Christine Paulin-Mohring

CIS 120 Midterm I February 16, 2015 SOLUTIONS

OCaml Language Choices CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

Introduction to OCaml

Functions. Nate Foster Spring 2018

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

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Some Advanced ML Features

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. OCaml Higher Order Functions

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

On Understanding Data Abstraction, Revisited

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

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

CMSC330 Spring 2008 Final Exam

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

CSE 341 Section 5. Winter 2018

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

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

CSE-505: Programming Languages. Lecture 18 Existential Types. Zach Tatlock 2016

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

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

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

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CSE20: Discrete Mathematics

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

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

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)

Fall Lecture 3 September 4. Stephen Brookes

CS421 Spring 2014 Midterm 1

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name)

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

CMSC 330: Organization of Programming Languages

COS 320. Compiling Techniques

Sometimes it s good to be lazy

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

CS153: Compilers Lecture 14: Type Checking

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

Principles of Functional Programming

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CIS 120 Midterm I October 2, Name (printed): Username (login id):

Typing Data. Chapter Recursive Types Declaring Recursive Types

CMSC 330: Organization of Programming Languages

A quick introduction to SML

Programming with Universes, Generically

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

CIS 500: Software Foundations

CMSC 341 Lecture 7 Lists

Typed Racket: Racket with Static Types

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

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. Operational Semantics

Imperative programming in F#

CS558 Programming Languages

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

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

Standard ML. Data types. ML Datatypes.1

Monads. Prof. Clarkson Fall Today s music: Vámanos Pal Monte by Eddie Palmieri

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

CSCE 121 ENGR 112 List of Topics for Exam 1

Two approaches to writing interfaces

COM2001 Advanced Programming Techniques. Mike Stannett Department of Computer Science The University of Sheffield

Data Abstraction and Specification of ADTs

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation

Concepts of Programming Languages

Metaprogramming assignment 3

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

Programming Languages and Techniques (CIS120)

Names and Functions. Chapter 2

CSE341 Spring 2017, Final Examination June 8, 2017

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

Tracing Ambiguity in GADT Type Inference

Types, Type Inference and Unification

Variables and Bindings

Programming Languages

CMSC 330: Organization of Programming Languages

Transcription:

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

Abstract Data Types Expose signature operators to create, combine, and observe values Hide representation & implementations Advantages Good engineering (libraries, code reuse, local reasoning) Optimizations (choose implementation w.r.t. your code) Limitations Require static typing Different representations cannot mix 2

Objects Object-oriented programming (OOP) - Computation as interactions between objects - 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 3

Relating ADTs to Objects type set val empty : set Abstract Data Types: define behavior forall sets val isempty : set -> bool val insert : set * int -> set val contains : set * int -> bool Observations on set argument type iset = { isempty : bool; insert : int -> iset; contains : int -> bool; } Objects: - encoded as records - fields define how this object interacts with other objects 4

ADT for ISets using Closures module ISetF : ISET = struct type set = bool * (int -> bool) let empty = (true, fun x -> false) let isempty (e,_) = e let insert((_,f), i) = (false, if f i then f else fun j -> i = j f j) let contains((_,f), i) = f i end;; CMSC 330 - Spring 2017 5

Objects by Encoding We will consider an encoding of objects similar to the Object FP encoding we saw before Objects contain methods But we will define top level methods that take the object as an argument. These will call the object s methods This will let us see how to encode the self/this parameter Not something we looked at before In our examples, objects are not imperative Updates to an object just return a new object with the update made 6

ISet as an object type iset = { isempty : bool; insert : int -> iset; contains : int -> bool; } # let (x:iset) = ;; - # let y = x.insert 1;; - # let z = insert_obj(y,2);; - (* calls y s insert method *) # y.contains 1;; - : bool = true # z.contains 2;; - : bool = false 7

Example: Insert Set calling contains field of the object s let insert_obj(s, n) = if s.contains(n) then s else { isempty = false; contains = fun i -> (i = n s.contains(i)); insert = fun i -> insert_obj (this, i); } recursive call to the defined object Question: How can we encode this? Answer: Use the fixpoint combinator Y fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b 8

Example: Insert Set calling contains field of the object s let rec insert_obj(s, n) = if s.contains(n) then s else (fix (fun this _ -> { isempty = false; contains = fun i -> (i = n s.contains(i)); insert = fun i -> insert_obj (this, i); })) () recursive call to the defined object Question: How can we encode this? Answer: Use the fixpoint combinator Y fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b 9

Quiz 1: Which is the Empty Set? A. let empty_obj _ = (fix (fun this _ -> { isempty = false; contains = fun i -> false; insert = fun i -> insert_obj(this, i) })) () B. let empty_obj _ = (fix (fun this _ -> { isempty = true; contains = fun i -> false; insert = fun i -> insert_obj(this, i)})) () C. let empty_obj _ = (fix (fun this _ -> { isempty = true; contains = fun i -> false; insert = fun i -> this})) () 10

Quiz 1: Which is the Empty Set? B. let empty_obj _ = (fix (fun this _ -> { isempty = true; contains = fun i -> false; insert = fun i -> insert_obj(this, i)})) () 11

Quiz 2: Which produces the union? A. let union_obj(s1, s2) = (fix (fun this _ -> { isempty = s1.isempty s2.isempty; contains = fun i -> s1.contains(i) s2.contains(i); insert = fun i -> insert_obj(this, i)})) () B. let union_obj(s1, s2) = (fix (fun this _ -> { isempty = s1.isempty s2.isempty; contains = fun i -> s1.contains(i) && s2.contains(i); insert = fun i -> insert_obj(this, i)})) () C. let union_obj(s1, s2) = (fix (fun this _ -> { isempty = s1.isempty && s2.isempty; contains = fun i -> s1.contains(i) s2.contains(i); insert = fun i -> insert_obj(this, i)})) () 12

The Union Set: Autognosis C. let union_obj(s1, s2) = (fix (fun this _ -> { isempty = s1.isempty && s2.isempty; contains = fun i -> s1.contains(i) s2.contains(i); insert = fun i -> insert_obj(this, i)})) () Autognosis: An object can only access other objects through their public interface. Problem: The representation of s1 s2 is unknown, so cannot be used for optimizations. Solution: Expose more implementation details. 13

Lack of Autognosis in ADTs Autognosis: An object can only access other objects through their public interface. + Each implementation can inspect representation + Used for optimizations (eg., sorted lists) - Different implementations cannot interact - eg., cannot union SeIt and ISetBST 14

Quiz 3: The Even Set A. even_obj _ = (fix (fun this _ -> { isempty = false; contains = fun i -> i mod 2 = 0; insert = fun i -> insert_obj(this, i)})) () B. even_obj _ = (fix (fun this _ -> { isempty = true; contains = fun i -> true; insert = fun i -> insert_obj(this, i)})) () C. even_obj _ = (fix (fun this _ -> { isempty = i mod 2 = 0; contains = fun i -> i mod 2 = 0; insert = fun i -> insert_obj(this, i)})) () 15

The Even Set: Flexibility A. even_obj _ = (fix (fun this _ -> { isempty = false; contains = fun i -> i mod 2 = 0; insert = fun i -> insert_obj(this, i)})) () Flexibility: - Objects accept any value that implements required methods. - They can be easily extended with new (here infinite) representations. 16

Computations are Object Interactions union_obj( even_obj(), empty_obj().insert(3) ).contains(3) Computation via dynamic binding: The function to be called is selected from the object record. Every object has a different contains field. Difficult to reason. 17

Computations are Object Interactions contains = fun i -> i mod 2 = 0 union_obj( even_obj(), empty_obj().insert(3) ).contains(3) contains = fun i -> false contains = fun i -> i == 3 false contains = fun i -> i == 3 false i mod 2 = 0 18

Quiz 4: ADTs vs. Objects Used for data abstraction (i.e., separate behavior from implementation) A. ADTs B. Objects C. None D. Both 19

Quiz 4: ADTs vs. Objects Used for data abstraction (i.e., separate behavior from implementation) ADTs use Type Abstraction: expose a type whose representation is hidden. Objects use Procedural Abstraction: expose procedures available on each object. A. ADTs B. Objects C. None D. Both 20

Quiz 5: ADTs vs. Objects Require Static Type System A. ADTs B. Objects C. None D. Both 21

ADTs require Static Types type set val empty : set val isempty : set -> bool val insert : set * int -> set val contains : set * int -> bool A. ADTs B. Objects C. None D. Both CMSC 330 - Spring 2017 22

Quiz 5: ADTs vs. Objects Require Static Type System ADTs rely on Static Type System to define the Type Abstraction Objects define abstractions via records: can be used in Static & Dependently typed lang. A. ADTs B. Objects C. None D. Both 23

Quiz 6: ADTs vs. Objects Allow mixing different representations. A. ADTs B. Objects C. None D. Both 24

Quiz 6: ADTs vs. Objects Allow mixing different representations. ADTs doesn t permit interaction of different representations. Representation can be inspected, which is used for optimizations. Objects cannot inspect representations (example union). Different representations can be mixed. A. ADTs B. Objects C. None D. Both 25

ADTs vs. Objects ADTs are easier to reason. The implementation is statically known (unlike dynamic binding) Obey theories in math (abstract algebra) and PL (existential typing) Objects are flexible and easy to extend. Only requirement to have appropriate fields 26