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 (CIS120e)

Programming Languages and Techniques (CIS120)

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

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)

Programming Languages and Techniques (CIS120)

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

Principles of Functional Programming

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Lists. Michael P. Fourman. February 2, 2010

Programming Languages and Techniques (CIS120)

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

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

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

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

CSE341, Fall 2011, Lecture 12 Summary

Object Oriented Design

Lecture 19: Signatures, Structures, and Type Abstraction

CS3110 Spring 2016 Lecture 6: Modules

Programming Languages and Techniques (CIS120)

Class diagrams and architectural design

Programming Languages and Techniques (CIS120)

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. Rust Basics

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Programming Languages and Techniques (CIS120)

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

Sample Exam; Solutions

Modules and Representation Invariants

Some Advanced ML Features

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Programming Languages and Techniques (CIS120)

CS 180 Problem Solving and Object Oriented Programming Fall 2011

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Today s lecture. CS 314 fall 01 C++ 1, page 1

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

Programming Languages and Techniques (CIS120)

Java is an objet-oriented programming language providing features that support

15 210: Parallel and Sequential Data Structures and Algorithms

Recap: Functions as first-class values

Functions. Nate Foster Spring 2018

UML class diagrams. Nigel Goddard. School of Informatics University of Edinburgh

CS111: PROGRAMMING LANGUAGE II

Chapter 6 Introduction to Defining Classes

Programming Languages and Techniques (CIS120)

Chapter 1 Getting Started

Programming Languages and Techniques (CIS120e)

CMSC201 Computer Science I for Majors

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

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

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

Abstraction Functions and Representation Invariants

Two approaches to writing interfaces

PIC 10A Objects/Classes

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Programming Languages and Techniques (CIS120)

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

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Sixth lecture; classes, objects, reference operator.

61A Lecture 3. Friday, September 5

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

301AA - Advanced Programming

CS 104 (Spring 2014) Final Exam 05/09/2014

Programming Languages and Techniques (CIS120)

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

6.005 Elements of Software Construction Fall 2008

17 Multiple Inheritance and ADT Extensions

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

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

INF121: Functional Algorithmic and Programming

Overloading, Type Classes, and Algebraic Datatypes

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

Metaprogramming assignment 3

Verification and Parallelism in Intro CS. Dan Licata Wesleyan University

Lecture 7: Data Abstractions

DC69 C# &.NET JUNE C# is a simple, modern, object oriented language derived from C++ and Java.

Harvard School of Engineering and Applied Sciences Computer Science 152

Programming Languages and Techniques (CIS120)

Object oriented programming with C++

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Programming Languages and Techniques (CIS120e)

Object-Oriented Modeling. Sequence Diagram. Slides accompanying Version 1.0

Transcription:

Programming Languages and Techniques (CIS120) Lecture 9 Jan 30, 2013 Abstract Types: Sets

Announcements Homework 3 is available on the web Due MONDAY, February 4 th at 11:59:59pm PracRce with BSTs, generic funcrons, abstract datatypes If you added CIS 120 recently, make sure that you can see your scores online. If you get feedback about your scores, you are in our database. If not, please send mail to tas120@lists.seas.upenn.edu

Abstract CollecRons

Design Process 1. Understand the problem 2. Formalize the interface 3. Write test cases 4. Implement the required behavior How to formalize the interface? Determine data representaron + relevant operarons So far, we have used built- in structures for representaron (int, list, tree) What if the built- in structures don t fit exactly?

A design problem As a high- school student, Stephanie had the job of reading books and finding which words, out of a list of the 1000- most common SAT vocabulary words, appeared in a paracular book. She enjoyed being paid to read, but she would have enjoyed being paid to program more. How could she have automated this task? 1. What are the important concepts or abstracaons for this problem? The collecron of words that appear in a book The collecron of 1000- most common SAT words The collecron of words from the first collecron that are contained in the second collecron

A set is an abstract type Use a set for this collecron In math, we typically write sets like this: {1,2,3} {true,false} with operarons like: S T or S T for union and intersecron we write x S to mean that x is a member of the set S A set is a lot like a list, except: Order doesn't maeer Duplicates don't maeer It isn't built into OCaml Sets show up frequently in applicarons 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,

2. Formalize the interface: myset.ml file type 'a set =! (* Need a way to create sets *)! let empty : 'a set =! let add (x:'a) (s:'a set) : 'a set =! let union (s1:'a set)(s2:'a set) : 'a set =! let remove (x:'a) (s:'a set) : 'a set =! let list_to_set (l:'a list) : 'a set =! (* and a way to work with them *)! let is_empty (x:'a set) : bool =! let member (x:'a) (s:'a set) : bool =! let equal (s1: a set) (s2: a set) : bool =! let elements (s:'a set) : 'a list =!

2a. Look at the interface: myset.mli file Keyword val names values type 'a set =! that must be defined and val empty : 'a set! their types. val add : 'a -> 'a set -> 'a set! val union : 'a set -> 'a set -> 'a set! val remove : 'a -> 'a set -> 'a set! val list_to_set : 'a list -> 'a set! val is_empty : 'a set -> bool! val member : 'a -> 'a set -> bool! val equal : 'a set -> 'a set -> bool! val elements : 'a set -> 'a list! OCaml puts interfaces (as above) in a.mli file The corresponding implementaaon goes in the.ml file

Aside: FuncRon Types In OCaml, the type of funcrons from input t to output u is wrieen: t -> u FuncRons with mulrple arguments use mulrple arrows Here are some examples we have already seen: size : tree -> int! hamming_distance : helix -> helix -> int! acids_of_helix : helix -> acids list! length : a list -> int! zip : a list -> b list -> ( a* b) list! lookup : tree -> int -> bool! insert : a tree -> a -> a tree!

3. Write tests (in another file) let s1 = Myset.add 3 Myset.empty! let s2 = Myset.add 4 Myset.empty! let s3 = Myset.union s1 s2! let test () : bool = (Myset.member 3 s1) = true! ;; run_test Myset.member 3 s1 test! let test () : bool = (Myset.member 4 s3) = true! ;; run_test Myset.member 4 s3 test! To use the values defined in the set module use the dot syntax: Myset.<member> AlternaRvely, use ;; open Myset at the top of a file to bring all of the names defined in the interface into scope. Note: Module names are always capitalized in OCaml

4. Implement the behavior There are many ways to implement sets. lists, trees, arrays, etc. How do we choose which implementa1on? Many such implementarons 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?

Abstract types BIG IDEA: Hide the concrete representaaon of a type behind an abstract interface. Example: concrete set representaron the implementaaon is a list or a tree abstract interface defines the operarons in terms of a set type The interface restricts how other parts of the program can interact with the data. Benefits: Safety: The other parts of the program can t break any invariants Modularity: It is possible to change the implementaron without changing the rest of the program

Defining Abstract Types Different programming languages* have different ways of lepng you define abstract types. At a minimum, this means providing: A way to specify (write down) an interface A means of hiding implementaron details (encapsulaaon) In OCaml: Interfaces are specified using a signature or ML interface file (.mli) EncapsulaRon is achieved because the interface can omit definirons. Clients can t menron values not named in the interface. *In Java, interfaces can also be wrieen down explicitly and encapsularon is achieved by public/ private modifiers on object fields. (We ll cover this in detail later.)

Example module interface:.mli file Type declararon has no type 'a set! definiron its representaron is abstract. val empty : 'a set! val add : 'a -> 'a set -> 'a set! val union : 'a set -> 'a set -> 'a set! val remove : 'a -> 'a set -> 'a set! val list_to_set : 'a list -> 'a set! val is_empty : 'a set -> bool! val member : 'a -> 'a set -> bool! val equal : 'a set -> 'a set -> bool! val elements : 'a set -> 'a list! Create a.mli file that omits informaron on purpose The definiron of the set type is hidden Auxiliary funcrons used in the implementaron are hidden

Naming the interface (a signature) module type Set = sig! type 'a set! val empty : 'a set! val is_empty : 'a set -> bool! val member : 'a -> 'a set -> bool! val add : 'a -> 'a set -> 'a set! val union : 'a set -> 'a set -> 'a set! val remove : 'a -> 'a set -> 'a set! val list_to_set : 'a list -> 'a set! val equal : a set -> a set -> bool! val elements : 'a set -> 'a list! end! Name the interface so that it can be reused by mulrple implementarons.

Module ImplementaRon: trees module BSet : Set =! struct! type a tree =! Constrains the module to use the named interface. Empty Node of a tree * a * a tree! type 'a set = a tree (* definition hidden by.mli *)! let empty : 'a set = Empty! let is_empty (s:'a set) : bool =!! end! The implementaron has to include all of the interface values It can contain more funcrons and type definirons (e.g. auxiliary funcrons) but those cannot be used outside the module The types of the provided implementarons must match the interface

Module ImplementaRon: lists module LSet : Set =! struct! type 'a set = a list! Constrains the module to use the given interface. let empty : 'a set = []! let is_empty (s:'a set) : bool =!! end! To use the values defined in this module later on in the file use the dot syntax: LSet.<member> In another file, use ;; open Hwset.LSet at the top of the file to bring all of the names defined in the interface into scope.