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)

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

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

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

Programming Languages and Techniques (CIS120)

Balanced Trees. Nate Foster Spring 2018

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

CS 241 Data Organization. August 21, 2018

Programming Languages and Techniques (CIS120)

Algebraic data types. The case against null

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II March 29, 2013 SOLUTIONS

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

Programming Languages and Techniques (CIS120)

Name: CIS 120e Midterm I Review

CSE : Python Programming

Programming Languages and Techniques (CIS120)

PREPARING FOR THE FINAL EXAM

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 15 Testing

Programming Languages and Techniques (CIS120)

Functions. Nate Foster Spring 2018

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

Metaprogramming assignment 3

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Programming Languages and Compilers (CS 421)

The Substitution Model. Nate Foster Spring 2018

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. OCaml Imperative Programming

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

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

CSE 130 [Winter 2014] Programming Languages

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

: Dimension. Lecturer: Barwick. Wednesday 03 February 2016

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

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

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

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

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

PREPARING FOR PRELIM 1

Welcome to... CS113: Introduction to C

Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo();

Programming Languages and Techniques (CIS120)

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

CSCI-GA Scripting Languages

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

Programming Languages and Techniques (CIS120)

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Tips from the experts: How to waste a lot of time on this assignment

Some Advanced ML Features

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

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

OCaml Language Choices CMSC 330: Organization of Programming Languages

Which of the following expressions has the type int list?

Monad Background (3A) Young Won Lim 11/8/17

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages

CS153: Compilers Lecture 15: Local Optimization

Programming Languages and Techniques (CIS120)

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm Solution

Programming Languages and Techniques (CIS120)

Programming with Java

Balanced Trees. Nate Foster Spring 2019

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

h0ps://

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

Monad Background (3A) Young Won Lim 11/18/17

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

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

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

Programming Languages and Techniques (CIS120)

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

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

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

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Project 1 Balanced binary

Admin CS41B MACHINE. Midterm topics. Admin 2/11/16. Midterm next Thursday in-class (2/18) SML. recursion. math. David Kauchak CS 52 Spring 2016

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

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

Primitive Data and Objects

Type Checking and Type Equality

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1


CIS 110 Introduction to Computer Programming. 7 May 2012 Final Exam

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Transcription:

Programming Languages and Techniques () Lecture 12 February 7, 2018 Partiality, Sequencing, Records Chapters 12, 13

Midterm 1 This Friday in class Review session Tonight, 6 7:30 PM DRLB A1 Announcements Make-up exam registration on course website You can bring a cheat sheet, a single-sided, handwritten, 8 ½ x 11 page of notes. Because University shut on Thursday: Wednesday recitations optional topic is review for midterm! Students in cancelled Thursday recitations can attend Wed. recitations. We will arrange additional office hours on Thursday in lieu of recitations if TAs volunteer.) See Piazza!!

Midterm 1 - Where 11 AM section: HERE!!!! 12 noon section: Announcements Last Names: A M HERE!!! N Z Levin Auditorium (next to Leidy, NOT LEVINE!!!)

Dealing with Partiality* *A function is said to be partial if it is not defined for all inputs.

Which of these is a function that calculates the maximum value in a (generic) list: 1. let rec list_max (l:'a list) : a = begin match l with [] -> [] h :: t -> max h (list_max t) end 2. let rec list_max (l:'a list) : a = fold max 0 l 3. let rec list_max (l: a list) : a = begin match l with h :: t -> max h (list_max t) end 4. None of the above Answer: 4

Quiz answer list_max isn t defined for the empty list! let rec list_max (l:'a list) : a = begin match l with [] -> failwith empty list [h] -> h h::t -> max h (list_max t) end

Client of list_max (* string_of_max calls list_max *) let string_of_max (x:int list) : string = string_of_int (list_max x) Oops! string_of_max will fail if given [] Not so easy to debug if string_of_max is written by one person and list_max is written by another. Interface of list_max is not very informative val list_max : int list -> int

Solutions to Partiality: Option 1 Abort the program: failwith an error message Whenever it is called, failwith halts the program and reports the error message it is given. This solution is appropriate whenever you know that a certain case is impossible The compiler isn t smart enough to figure out that the case is impossible Often happens when there is an invariant on a data structure failwith is also useful to stub out unimplemented parts of your program. Languages (e.g. OCaml, Java) support exception handling facilities to let programs recover from such failures. We'll talk about these when we get to Java

Return a default or error value e.g. define list_max [] to be -1 Error codes used often in C programs null used often in Java But Solutions to Partiality: Option 2 What if -1 (or whatever default you choose) really is the maximum value? Can lead to many bugs if the default isn t handled properly by the callers. IMPOSSIBLE to implement generically! No way to generically create a sensible default value for every possible type Sir Tony Hoare, Turing Award winner and inventor of null calls it his billion dollar mistake! Defaults should be avoided if possible

Optional values Solutions to Partiality: Option 3

Option Types Define a generic datatype of optional values: type 'a option = None Some of 'a A partial function returns an option let list_max (l:list) : int option = Contrast this with null, a legal return value of any type caller can accidentally forget to check whether null was used; results in NullPointerExceptions or crashes Modern language designs (e.g. Apple's Swift, Mozilla's Rust) distinguish between the type String (definitely not null) and String? (optional string)

Example: list_max A function that returns the maximum value of a list as an option (None if the list is empty) let list_max (l:'a list) : 'a option = begin match l with [] -> None x::tl -> Some (fold max x tl) end

Revised client of list_max (* string_of_max calls list_max *) let string_of_max (l:int list) : string = begin match (list_max l) with None -> no maximum Some m -> string_of_int m end string_of_max will never fail The type of list_max makes it explicit that a client must check for partiality. val list_max : int list -> int option

What is the type of this function? let head (x: ) : = begin match x with [] -> None h :: t -> Some h end 1. a list -> a 2. a list -> a list 3. a list -> b option 4. a list -> a option 4. None of the above Answer: 4

What is the value of this expression? let head (x: a list) : a option = begin match x with [] -> None h :: t -> Some h end in [ head [1]; head [] ] 1. [ 1 ; 0 ] 2. 1 3. [Some 1; None] 4. [None; None] 5. None of the above Answer: 3

Revising the MAP interface module type MAP = sig end 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 option val entries : ('k,'v) map -> ('k * 'v) list val equals : ('k,'v) map -> ('k,'v) map -> bool get returns an optional 'v. Now its type isn't a lie!

Commands, Sequencing and Unit What is the type of print_string?

Sequencing Commands and Expressions We can sequence commands inside expressions using ; unlike in C, Java, etc., ; doesn t terminate a statement it separates a command from an expression let f (x:int) : int = print_string "f called with "; print_string (string_of_int x); x + x do not use ; here! note the use of ; here The distinction between commands & expressions is artificial. print_string is a function of type: string -> unit Commands are actually just expressions of type: unit

Similar to "void" in Java or C unit: the trivial type For functions that don't take any arguments let f () : int = 3 let y : int = f () val f : unit -> int val y : int Also for functions that don't return anything, such as testing and printing functions a.k.a commands: (* run_test : string -> (unit -> bool) -> unit *) ;; run_test TestName test (* print_string : string -> unit *) ;; print_string Hello, world!

unit: the boring type Actually, () is a value just like any other value (a 0-ary tuple) For functions that don't take any interesting arguments let f () : int = 3 let y : int = f () val f : unit -> int val y : int Also for functions that don't return anything interesting, such as testing and printing functions a.k.a commands: (* run_test : string -> (unit -> bool) -> unit *) ;; run_test TestName test (* print_string : string -> unit *) ;; print_string Hello, world!

unit: the first-class type Can define values of type unit let x : unit = () val x : unit Can pattern match unit (even in function definitions) let z = begin match x with () -> 4 end fun () -> 3 Is the result of an implicit else branch: ;; if z <> 4 then failwith "oops" ;; if z <> 4 then failwith "oops" else ()

Sequencing Commands and Expressions Expressions of type unit are useful because of their side effects they "do" stuff e.g. printing, changing the value of mutable state let f (x:int) : int = print_string "f called with "; print_string (string_of_int x); x + x do not use ; here! note the use of ; here We can think of ; as an infix function of type: unit -> a -> a

What is the type of f in the following program: let f (x:int) = print_int (x + x) 1. unit -> int 2. unit -> unit 3. int -> unit 4. int -> int 5. f is ill typed Answer: 3

What is the type of f in the following program: let f (x:int) = (print_int x); (x + x) 1. unit -> int 2. unit -> unit 3. int -> unit 4. int -> int 5. f is ill typed Answer: 4

Records

Immutable Records Records are like tuples with named fields: (* a type for representing colors *) type rgb = {r:int; g:int; b:int;} (* some example rgb values *) let red : rgb = {r=255; g=0; b=0;} let blue : rgb = {r=0; g=0; b=255;} let green : rgb = {r=0; g=255; b=0;} let black : rgb = {r=0; g=0; b=0;} let white : rgb = {r=255; g=255; b=255;} Curly braces around record. Semicolons after record components. The type rgb is a record with three fields: r, g, and b fields can have any types; they don t all have to be the same Record values are created using this notation: {field1=val1; field2=val2; }

Field Projection The value in a record field can be obtained by using dot notation: record.field (* a type for representing colors *) type rgb = {r:int; g:int; b:int;} (* using 'dot' notation to project out components *) (* calculate the average of two colors *) let average_rgb (c1:rgb) (c2:rgb) : rgb = {r = (c1.r + c2.r) / 2; g = (c1.g + c2.g) / 2; b = (c1.b + c2.b) / 2;}

OCaml Record Syntax OCaml provides convenient syntax for working with records: Pattern matching with record arguments: let f {r ; g} = r + g Variable names as record fields: let mk_rgb (r:int) (g:int) (b:int ) = {r; g; b}