Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Programming Languages and Techniques (CIS120)

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

Programming Languages and Techniques (CIS120e)

CS 11 Ocaml track: lecture 4. Today: modules

Modular Programming. Prof. Clarkson Fall Today s music: "Giorgio By Moroder" by Daft Punk

Programming Languages and Techniques (CIS120)

Principles of Functional Programming

Balanced Trees. Nate Foster Spring 2018

Lists. Michael P. Fourman. February 2, 2010

Balanced Trees. Prof. Clarkson Fall Today s music: Get the Balance Right by Depeche Mode

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Modules and Representation Invariants

About this exam review

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

PIC 10B Lecture 1 Winter 2014 Homework Assignment #2

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

Some Advanced ML Features

CMSC 330: Organization of Programming Languages

Programming Languages and Techniques (CIS120)

Lecture 11. Lecture 11: External Sorting

Procedural Abstraction

Programming Languages and Techniques (CIS120)

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

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

QUIZ. What is wrong with this code that uses default arguments?

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

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

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Abstraction Functions and Representation Invariants

Programming Languages and Techniques (CIS120)

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

Programming Languages and Techniques (CIS120)

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

1 Splay trees. 2 Implementation of splay. CS134b Homework #1 January 8, 2001 Due January 15, 2001

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages

CSE 341 : Programming Languages Midterm, Spring 2015

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

Lecture 21: Red-Black Trees

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CS558 Programming Languages

Programming Languages and Techniques (CIS120)

Lecture 19: Signatures, Structures, and Type Abstraction

Class Note #02. [Overall Information] [During the Lecture]

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

Part I: Short Answer (12 questions, 65 points total)

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Admin. ! Assignment 3. ! due Monday at 11:59pm! one small error in 5b (fast division) that s been fixed. ! Midterm next Thursday in-class (10/1)

Overview. CS301 Session 3. Problem set 1. Thinking recursively

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

CSE341, Fall 2011, Lecture 12 Summary

Lecture 7. Log into Linux New documents posted to course webpage

Friday Four Square! 4:15PM, Outside Gates

Programming Languages and Techniques (CIS120)

Sixth lecture; classes, objects, reference operator.

Query Processing with Indexes. Announcements (February 24) Review. CPS 216 Advanced Database Systems

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

Object Oriented Design

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE 130, Fall 2006: Final Examination

Midterms Save the Dates!

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Balanced Trees. Nate Foster Spring 2019

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

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

Programming Languages and Techniques (CIS120)

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

We've tried to include the common errors and grading standard for every question.

PIC 10A Objects/Classes

CS3110 Spring 2016 Lecture 6: Modules

22c:111 Programming Language Concepts. Fall Types I

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University


CSC324 Functional Programming Typing, Exceptions in ML

CS5112: Algorithms and Data Structures for Applications

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Ch. 10: Name Control

CSE341 Spring 2017, Midterm Examination April 28, 2017

CS2110: Software Development Methods. Maps and Sets in Java

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

Programming Languages and Techniques (CIS120)

Sample Exam; Solutions

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

KOMAR UNIVERSITY OF SCIENCE AND TECHNOLOGY (KUST)

CIS 500 Software Foundations Fall December 6

Administrivia. Existential Types. CIS 500 Software Foundations Fall December 6. Administrivia. Motivation. Motivation

Transcription:

Programming Languages and Techniques (CIS120) Lecture 10 February 2, 2018 Abstract types: Sets Chapter 10

Announcements Homework 3 due Tuesday at 11:59:59pm Midterm 1 Friday, February 9, in class Covers material through Chapter 10 Review materials (old exams) on course website Some old exams cover material in Ch. 11 (options), can ignore that Review session Wednesday Feb 7, 6 7:30 PM DRLB A1 Make-up exam registration on course website

Abstract Collections

Are you familiar with the idea of a set from mathematics? 1. yes 2. no In math, we typically write sets like this: Ø {1,2,3} {true,false} with operations: S T for union and S T for intersection; we write x S for the predicate x is a member of the set S CIS120

A set is an abstraction A set is a collection of data we have operations for forming sets of elements we can ask whether elements are in a set A set is a lot like a list, except: Order doesn't matter Duplicates don't matter It isn't built into OCaml Sets are useful in programming An element s presence or absence in the set is all that matters Examples: set of students in a class, set of coordinates in a graph, set of answers to a survey, set of data samples from an experiment,

A set implementation A BST can implement (represent) a set there is a way to represent an empty set (Empty) there is a way to list all elements contained in the set (inorder) there is a way to test membership (lookup) Can define union/intersection (with insert and delete) 1 < > 0 3 concrete representation abstract view BSTs are not the only way to implement sets 1 3 0

Three Example Representations of Sets BST: 1 < > Alternate representation: unsorted linked list. Alternate representation: reverse sorted array with Index of next slot. 0 3 3::0::1::[] 3 1 0 X X concrete representation abstract view concrete representation abstract view concrete representation abstract view 1 0 1 0 1 0 3 3 3

Abstract types (e.g. set) An abstract type is defined by its interface and its properties, not its representation Interface: defines the type and operations There is a type of sets There is an empty set There is a way to add elements to a set to make a bigger set There is a way to list all elements in a set There is a way to test membership Properties: define how the operations interact with each other Elements that were added can be found in the set Adding an element a second time doesn t change the elements of a set Adding in a different order doesn t change the elements of a set Any type that satisfies the interface and properties can be a set? concrete representation abstract view 1 0 3

Implementing sets There are many ways to implement sets lists, trees, arrays, etc. How do we choose which implementation? Deps on the needs of the application How often is member used vs. add? How big can the sets be? Many such implementations are of the flavor a set is a with some invariants A set is a list with no repeated elements. A set is a tree with no repeated elements A set is a binary search tree A set is an array of bits, where 0 = absent, 1 = present How do we preserve the invariants of the implementation? Invariant: a property that remains unchanged when a specified transformation is applied.

Sets in OCaml OCaml directly supports the declaration of abstract types via signatures

The name of the signature module type SET = sig Set Signature The sig keyword indicates an interface declaration type 'a set Type declaration has no right-hand side its val empty : 'a set representation is abstract! val add : 'a -> 'a set -> 'a set val member : 'a -> 'a set -> bool val equals : 'a set -> 'a set -> bool val set_of_list : 'a list -> 'a set The interface members are the (only!) means of manipulating the abstract type. Signature (a.k.a. interface): defines operations on the type

A module implements an interface An implementation of the set interface will look like this: Name of the module Signature that it implements The struct keyword indicates a module implementation module BSTSet : SET = struct (* implementations of type and operations *)

Implement the set Module module BSTSet : SET = struct type 'a tree = Empty Node of 'a tree * 'a * 'a tree type 'a set = 'a tree let empty : 'a set = Empty Module must define (give a concrete representation to) the type declared in the signature The implementation must include everything promised by the interface It can contain more functions and type definitions (e.g. auxiliary or helper functions) but those cannot be used outside the module The types of the provided implementations must match the signature

Abstract vs. Concrete BSTSet s = 1 < > 0 3 module BSTSet : SET = struct type 'a tree = type 'a set = 'a tree let empty : 'a set = Empty let add (x:'a) (s:'a set) :'a set =... (* can treat s as a tree *) concrete representation abstract view module type SET = sig type 'a set val empty : 'a set val add : 'a -> 'a set -> 'a set 1 3 0 (* A client of the BSTSet module *) ;; open BSTSet let s : int set = add 0 (add 3 (add 1 empty))

Another Implementation module ULSet : SET = struct type 'a set = 'a list A different definition for the type set let empty : 'a set = []

Abstract vs. Concrete ULSet s = 0::3::1::[] module ULSet : SET = struct type 'a set = 'a list let empty : 'a set = [] let add (x:'a) (s:'a set) :'a set = x::s (* can treat s as a list *) concrete representation abstract view module type SET = sig type 'a set val empty : 'a set val add : 'a -> 'a set -> 'a set 1 3 0 (* A client of the ULSet module *) ;; open ULSet let s : int set = add 0 (add 3 (add 1 empty)) Client code doesn t change!

Testing (and using) sets To use the values defined in a set module use open to bring all of the names defined in the interface into scope. ;; open ULSet let s1 = add 3 empty let s2 = add 4 empty let s3 = add 4 s1 let test () : bool = (member 3 s1) ;; run_test "ULSet.member 3 s1" test let test () : bool = (member 4 s3) ;; run_test "ULSet.member 4 s3" test

Testing (and using) sets Alternatively, use the dot syntax: ULSet.<member> No need to use open (avoids shadowing existing names) Note: Module names must be capitalized in OCaml let s1 = ULSet.add 3 ULSet.empty let s2 = ULSet.add 4 ULSet.empty let s3 = ULSet.add 4 s1 let test () : bool = (ULSet.member 3 s1) ;; run_test "ULSet.member 3 s1" test let test () : bool = (ULSet.member 4 s3) ;; run_test "ULSet.member 4 s3" test

Abstract types BIG IDEA: Hide the concrete representation of a type behind an abstract interface to preserve invariants The interface restricts how other parts of the program can interact with the data Type checking ensures that the only way to create a set is with the operations in the interface If all operations preserve invariants, then all sets in the program must satisfy invariants Example: all BST-implemented sets must satisfy the BST invariant, therefore the lookup function can assume that its input satisfies the invariant Benefits: Safety: The other parts of the program can t cause bugs in the set implementation Modularity: It is possible to change the implementation without changing the rest of the program

module type SET = sig type 'a set val empty : 'a set val add : 'a -> 'a set -> 'a set Does this code type check? module BSTSet : SET = struct type 'a tree = Empty Node of 'a tree * 'a * 'a tree type 'a set = 'a tree let empty : 'a set = Empty ;; open BSTSet let s1 : int set = add 1 empty 1. yes 2. no Answer: yes CIS120

Does this code type check? 1. yes 2. no ;; open BSTSet let s1 = add 1 empty let i1 = begin match s1 with Node (_,k,_) -> k Empty -> failwith impossible Answer: no, add constructs a set, not a tree module type SET = sig type 'a set val empty : 'a set val add : 'a -> 'a set -> 'a set module BSTSet : SET = struct type 'a tree = Empty Node of 'a tree * 'a * 'a tree type 'a set = 'a tree let empty : 'a set = Empty CIS120

module type SET = sig type 'a set val empty : 'a set val add : 'a -> 'a set -> 'a set Does this code type check? module BSTSet : SET = struct type 'a tree = Empty Node of 'a tree * 'a * 'a tree type 'a set = 'a tree let empty : 'a set = Empty let size (t : 'a tree) : int = ;; open BSTSet let s1 = add 1 empty let i1 = size s1 1. yes 2. no Answer: no, cannot access helper functions outside the module CIS120

module type SET = sig type 'a set val empty : 'a set val add : 'a -> 'a set -> 'a set Does this code type check? module BSTSet : SET = struct type 'a tree = Empty Node of 'a tree * 'a * 'a tree type 'a set = 'a tree let empty : 'a set = Empty ;; open BSTSet let s1 : int set = Empty 1. yes 2. no CIS120 Answer: no, the Empty data constructor is not available outside the module

If a client module works correctly and starts with: ;; open ULSet will it continue to work if we change that line to: assuming that ULSet and BSTSet both implement SET and satisfy all of the set properties? 1. yes 2. no ;; open BSTSet Answer: yes (though performance may be different) CIS120

module type SET = sig type 'a set val empty : 'a set val add : 'a -> 'a set -> 'a set val member : 'a -> 'a set -> bool module BSTSet : SET = struct type 'a tree = Empty Node of 'a tree * 'a * 'a tree type 'a set = 'a tree let empty : 'a set = Empty Is is possible for a client to call member with a tree that is not a BST? 1. yes 2. no CIS120 No: the BSTSet operations preserve the BST invariants. there is no way to construct a non-bst tree using the interface.

Summary: Abstract Types Different programming languages have different ways of letting you define abstract types At a minimum, this means providing: A way to specify (write down) an interface A means of hiding implementation details (encapsulation) In OCaml: Interfaces are specified using a signature or interface Encapsulation is achieved because the interface can omit information type definitions names and types of auxiliary functions Clients cannot mention values or types not named in the interface

Bonus Material: OCaml Details module and interface files

.ml and.mli files You ve already been using signatures and modules in OCaml. A series of type and val declarations stored in a file foo.mli is considered as defining a signature FOO A series of top-level definitions stored in a file foo.ml is considered as defining a module Foo

foo.mli type t val z : t val f : t -> int foo.ml type t = int let z : t = 0 let f (x:t) : int = x + 1 test.ml ;; open Foo ;; print_int (Foo.f Foo.z) module type FOO = sig type t val z : t val f : t -> int module Foo : FOO = struct type t = int let z : t = 0 let f (x:t) : int = x + 1 module Test = struct ;; open Foo ;; print_int (Foo.f Foo.z) Files