Programming Languages and Techniques (CIS120)

Similar documents
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)

Principles of Functional Programming

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Lists. Michael P. Fourman. February 2, 2010

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

Programming Languages and Techniques (CIS120e)

Balanced Trees. Nate Foster Spring 2018

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

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

Programming Languages and Techniques (CIS120)

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

CSE 341 : Programming Languages Midterm, Spring 2015

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CS 11 Ocaml track: lecture 4. Today: modules

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Abstraction Functions and Representation Invariants

Modules and Representation Invariants

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Standard ML. Data types. ML Datatypes.1

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Programming Languages and Techniques (CIS120)

Lecture 19: Signatures, Structures, and Type Abstraction

Some Advanced ML Features

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

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

CSE 341 Section 5. Winter 2018

Name: CIS 120e Midterm I Review

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

CSC/MAT-220: Lab 6. Due: 11/26/2018

CS3: Introduction to Symbolic Programming. Lecture 5:

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

CIS 500 Software Foundations Midterm I

CSE341 Spring 2016, Midterm Examination April 29, 2016

CIS 120 Final Exam May 7, Name (printed): Pennkey (login id):

Programming Languages and Techniques (CIS120)

Lecture 21: Red-Black Trees

Programming Languages

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

CIS 120 Midterm II November 8, 2013 SOLUTIONS

CMSC 330: Organization of Programming Languages

Programming Languages and Techniques (CIS120)

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

Programming Languages

Programming Languages and Techniques (CIS120)

Practice Midterm Exam #1

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

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE 130 [Winter 2014] Programming Languages

Sample Exam; Solutions

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

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

CS3110 Spring 2016 Lecture 6: Modules

Programming Languages and Techniques (CIS120e)

PREPARING FOR PRELIM 2

List Processing in Ocaml

CSE341, Fall 2011, Midterm Examination October 31, 2011

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Programming Languages and Techniques (CIS120)

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

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Programming Languages and Techniques (CIS120e)

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

CS 321 Programming Languages

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

Name: Pennkey (eg, sweirich): CIS 120 Final Exam May 8, 2012

Metaprogramming assignment 3

CS61C : Machine Structures

Lecture 3: Recursion; Structural Induction

PREPARING FOR PRELIM 1

CSE 130, Fall 2005: Final Examination

CSE341 Spring 2017, Midterm Examination April 28, 2017

Introduction to OCaml

Part I: Written Problems

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

CSE 130, Fall 2006: Final Examination

Exercises on ML. Programming Languages. Chanseok Oh

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

Programming Languages and Techniques (CIS120)

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

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

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

CMSC 330: Organization of Programming Languages

Transcription:

Programming Languages and Techniques (CIS120) Lecture 11 February 5, 2018 Review: Abstract types Finite Maps

Homework 3 due tomorrow at 11:59:59pm Announcements (Homework 4 is due Tuesday, 2/20, and won t go out until next week) Midterm 1 This Friday, February 9, in class Where? See next slide Covers material through Chapter 10 Review materials (old exams) on course website Ignore questions on options from Ch. 11 Review session Wednesday Feb 7, 6 7:30 PM DRLB A1 Make-up exam registration on course website

Midterm 1 This Friday, February 9, in class Announcements Where? 11 AM section: HERE!!!! 12 noon section: Last Names: A M Leidy Labs 10 (Here!) N Z Levin Auditorium (next to Leidy, NOT LEVINE!!!)

Review: Abstract types (e.g. set) An abstract type is defined by its interface and its properties, not its representation. Interface: defines operations on the type 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? concrete representation abstract view Any type (possibly with invariants) that satisfies the interface and properties can be a set. 1 0 Clients of an implementation can only access what is explicitly in the abstract type s interface 3

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 representation is abstract! val empty : 'a set 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

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

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!

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.

Finite Maps Another example of abstract datatype interfaces & concrete implementations

Motivating Scenario Suppose you were writing some course-management software and needed to look up the lab section for a student given the student s PennKey? Students might add/drop the course Students might switch lab sections Students should be in only one lab section How would you do it? What data structure would you use? CIS120 14

Example Each key is associated with a value. No two keys are identical Values can be repeated Key Value stephanie 15 mitch 05 ezaan 10 likat 15 Given the key stephanie" we want to find / lookup the value 15 CIS120

Finite Maps A finite map (a.k.a. dictionary) is a collection of bindings from distinct keys to values. Operations to add & remove bindings, test for key membership, look up the value bound to a particular key Example: a (string, int) map might map a PennKey to the lab section. The map type is generic in two arguments Like sets, finite maps appear in many settings to map: domain names to IP addresses words to their definitions (a dictionary) user names to passwords game character unique identifiers to dialog trees CIS120 16

Tests for Finite Map abstract type ;; open Assert (* Specifying the properties of the MAP abstract type via test cases. *) (* A simple map with one element. *) let m1 : (int,string) map = add 1 "uno" empty (* list entries for this simple map *) ;; run_test "entries m1" (fun () -> entries m1 = [(1,"uno")]) (* access value for key in the map *) ;; run_test "find 1 m1" (fun () -> (get 1 m1) = "uno") (* find for value that does not exist in the map? *) ;; run_failing_test "find 2 m1" (fun () -> (get 2 m1) = "dos" ) let m2 : (int, string) map = add 1 "un" m1 (* find after redefining value, should be new value *) ;; run_test "find 1 m2" (fun () -> (get 1 m2) = "un") (* entries after redefining value, should only show new value *) ;; CIS120 run_test "entries m2" (fun () -> entries m2 = [(1, "un")])

Signature: Finite Map module type MAP = sig type ('k,'v) map val empty : ('k,'v) map val add : 'k -> 'v -> ('k,'v) map -> ('k,'v) map val remove : 'k -> ('k,'v) map -> ('k,'v) map val mem : 'k -> ('k,'v) map -> bool val get : 'k -> ('k,'v) map -> 'v val entries : ('k,'v) map -> ('k * 'v) list val equals : ('k,'v) map -> ('k,'v) map -> bool

Implementation: Ordered Lists module Assoc : MAP = struct (* Represent a finite map as a list of pairs. *) (* Representation invariant: *) (* - no duplicate keys (helps get, remove) *) (* - keys are sorted (helps equals, helps get) *) type ('k,'v) map = ('k * 'v) list let empty : ('k,'v) map = [] let rec mem (key:'k) (m : ('k,'v) map) : bool = begin match m with [] -> false (k,v)::rest -> (key >= k) && (key = k) (mem key rest) ;; run_test "mem test" (fun () -> mem "b" [("a",3); ("b",4)]) CIS120

Implementation: Ordered Lists let rec get (key:'k) (m : ('k,'v) map) : 'v = begin match m with [] -> failwith "key not found" (k,v)::rest -> if key < k then failwith "key not found" else if key = k then v else get key rest let rec remove (key:'k) (m : ('k,'v) map) : ('k,'v) map = begin match m with [] -> [] (k,v)::rest -> if key < k then m else if key = k then rest else (k,v)::remove key rest CIS120

Completing module implementation finitemap.ml

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

.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 module type FOO = sig 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 module Foo : FOO = struct type t = int let z : t = 0 let f (x:t) : int = x + 1 test.ml ;; open Foo ;; print_int (Foo.f Foo.z) Files module Test = struct ;; open Foo ;; print_int (Foo.f Foo.z)