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)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 120 Midterm I February 16, 2015 SOLUTIONS

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

CS 11 Ocaml track: lecture 4. Today: modules

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

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

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

Principles of Functional Programming

Lists. Michael P. Fourman. February 2, 2010

FUNCTIONAL PROGRAMMING

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Balanced Trees. Nate Foster Spring 2018

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

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

Programming Languages and Techniques (CIS120)

CMSC 330, Fall 2013, Practice Problems 3

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

Programming Languages and Techniques (CIS120)

CSE 341 Section 5. Winter 2018

CMSC 341 Lecture 10 Binary Search Trees

Programming Languages and Techniques (CIS120)

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

Lecture 19: Signatures, Structures, and Type Abstraction

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

CS3110 Spring 2016 Lecture 6: Modules

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

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

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

Name: CIS 120e Midterm I Review

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

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

CS421 Spring 2014 Midterm 1

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II March 29, 2013 SOLUTIONS

CMSC 330, Fall 2013, Practice Problem 3 Solutions

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

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

Sample Exam; Solutions

CSE 130, Fall 2006: Final Examination

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

Pattern Matching and Abstract Data Types

CMSC 330: Organization of Programming Languages

Some Advanced ML Features

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

Lecture 13 CIS 341: COMPILERS

Functional Programming. Introduction To Cool

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

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

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

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Part I: Written Problems

CMSC 330: Organization of Programming Languages


CSE 341 : Programming Languages Midterm, Spring 2015

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

CS 321 Programming Languages

Case by Case. Chapter 3

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

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

Modules and Representation Invariants

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

Programming Languages and Techniques (CIS120)

About this exam review

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Metaprogramming assignment 3

Intro. Scheme Basics. scm> 5 5. scm>

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

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

List Processing in Ocaml

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

Module 10: Imperative Programming, Modularization, and The Future

CSE 130, Fall 2005: Final Examination

Types, Expressions, and States

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

Functional Programming Mid-term exam Tuesday 3/10/2017

APCS Semester #1 Final Exam Practice Problems

Introduction to OCaml

CSE 130 [Winter 2014] Programming Languages

The Typed Racket Guide

Functions. Nate Foster Spring 2018

To figure this out we need a more precise understanding of how ML works

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

Programming Languages and Techniques (CIS120)

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

CS3: Introduction to Symbolic Programming. Lecture 10: Tic-tac-toe Lambda.

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

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

Lecture 1: Overview

Transcription:

Programming Languages and Techniques (CIS120) Lecture 10 February 5 th, 2016 Abstract types: sets Lecture notes: Chapter 10

What is the value of this expresssion? let f (x:bool) (y:int) : int = if x then 1 else y in f true 1. 1 2. true 3. fun (y:int) -> if true then 1 else y 4. fun (x:bool) -> if x then 1 else y Answer: 3

Announcements Homework 3 is available due Tuesday at midnight Read Chapter 10 of lecture notes Midterm 1 Register for makeup exam on course website

List processing The fold design pattern

Refactoring code, again Is there a pattern in the definition of these two functions? let rec exists (l : bool list) : bool = begin match l with [] -> false h :: t -> h exists t let rec acid_length (l : acid list) : int = begin match l with [] -> 0 h :: t -> 1 + acid_length t base case: Simple answer when the list is empty combine step: Do something with the head of the list and the recursive call Can we factor out that pattern using first- class functions? CIS120

Abstracting with respect to Base let rec helper (base : bool) (l : bool list) : bool = begin match l with [] -> base h :: t -> h helper base t let exists (l : bool list) = helper false l let rec helper (base : int) (l : acid list) : int = begin match l with [] -> base h :: t -> 1 + helper base t let acid_length (l : acid list) = helper 0 l CIS120

Abstracting with respect to Combine let rec helper (combine : bool -> bool -> bool) (base : bool) (l : bool list) : bool = begin match l with [] -> base h :: t -> combine h (helper combine base t) let exists (l : bool list) = helper (fun (h:bool) (acc:bool) -> h acc) false l let rec helper (combine : acid -> int -> int) (base : int) (l : acid list) : int = begin match l with [] -> base h :: t -> combine h (helper combine base t) let acid_length (l : acid list) = helper (fun (h:acid) (acc:int) -> 1 + acc) 0 l CIS120

Making the Helper Generic let rec helper (combine : a -> b -> b) (base : b) (l : a list) : b = begin match l with [] -> base h :: t -> combine h (helper combine base t) let exists (l : bool list) = helper (fun (h:bool) (acc:bool) -> h acc) false l let rec helper (combine : a -> b -> b) (base : b) (l : a list) : b = begin match l with [] -> base h :: t -> combine h (helper combine base t) let acid_length (l : acid list) = helper (fun (h:acid) (acc:int) -> 1 + acc) 0 l CIS120

List Fold let rec fold (combine: 'a -> 'b -> 'b) (base:'b) (l : 'a list) : 'b = begin match l with [] -> base x :: t -> combine x (fold combine base t) let exists (l : bool list) : bool = fold (fun (h:bool) (acc:bool) -> h acc) false l let acid_length (l : acid list) : int = fold (fun (h:acid) (acc:int) -> 1 + acc) 0 l fold (a.k.a. Reduce) Like transform, foundational function for programming with lists Captures the pattern of recursion over lists Also part of OCaml standard library (List.fold_right) Similar operations for other recursive datatypes (fold_tree) CIS120

How would you rewrite this function let rec sum (l : int list) : int = begin match l with [] -> 0 h :: t -> h + sum t using fold? What should be the arguments for base and combine? 1. combine is: (fun (h:int) (acc:int) -> acc + 1) base is: 0 2. combine is: (fun (h:int) (acc:int) -> h + acc) base is: 0 3. combine is: (fun (h:int) (acc:int) -> h + acc) base is: 1 1. sum can t be written by with fold. Answer: 2 CIS120

How would you rewrite this function let rec reverse (l : int list) : int list = begin match l with [] -> [] h :: t -> reverse t @ [h] using fold? What should be the arguments for base and combine? 1. combine is: (fun (h:int) (acc:int list) -> h :: acc) base is: 0 2. combine is: (fun (h:int) (acc:int list) -> acc @ [h]) base is: 0 3. combine is: (fun (h:int) (acc:int list) -> acc @ [h]) base is: [] 1. reverse can t be written by with fold. Answer: 3 CIS120

Functions as Data We ve seen a number of ways in which functions can be treated as data in OCaml Present- day programming practice offers many more examples at the small scale : objects bundle functions (a.k.a. methods) with data iterators ( cursors for walking over data structures) event listeners (in GUIs) etc. The idiom is useful at the large scale : Google s MapReduce Framework for transforming (mapping) sets of key- value pairs Then reducing the results per key of the map Easily distributed to 10,000 machines to execute in parallel! CIS120

Abstract Collections

Are you familiar with the idea of a set from mathematics? 1. yes 2. no CIS120 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 x is a member of the set S

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 An element s presence or absence in the set is all that matters Sets show up frequently in applications 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,

Abstract type: set 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) could define union/intersection (insert and delete) Order doesn't matter We create BSTs by adding elements to an empty BST The BST data structure doesn t remember what order we added the elements Duplicates don't matter Our implementation doesn t keep track of how many times an element is added BST invariant ensure that each node is unique BSTs are not the only way to implement sets 1 < > 0 3 concrete representation abstract view 1 0 3

Three Example Representations of Sets BST: 1 < > Alternate representation: unsorted linked list. Alternate representation: reverse sorted array with index to 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 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 Any type (possibly with invariants) that satisfies the interface and properties can be a set.? concrete representation abstract view 1 0 3

Sets in OCaml

The set interface in OCaml (a signature) module type SET = sig type 'a set Type declaration has no body 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 Keyword val names values that must be defined and their types.

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 or remove? How big will the sets need to 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?

A module implements an interface An implementation of the set interface will look like this: Name of the module Signature that it implements module ULSet : SET = struct (* implementations of all the 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 the type declared in the signature The implementation has to 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 interface

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

Testing (and using) sets To use the values defined in the set module use the dot syntax: ULSet.<member> 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

Testing (and using) sets Alternatively, 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

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. Clients must only use what is declared in the SET interface Benefits: Safety: The other parts of the program can t break any invariants Modularity: It is possible to change the implementation without changing the rest of the program

Does this code type check? ;; open BSTSet let s1 : int set = Empty 1. yes 2. no Answer: no, the Empty data constructor is not available outside the module CIS120