Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

Similar documents
CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Preparing for Class: Watch le Videos! What is an ML program?

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

Programming Languages and Techniques (CIS120)

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

The Substitution Model

The Environment Model

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

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

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

G Programming Languages - Fall 2012

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Objects. Prof. Clarkson Fall ob-ject: to feel distaste for something Webster's Dictionary. Today s music: Kung Fu Fighting by CeeLo Green

Programming Languages Dan Grossman Welcome to the Class!

Mutation. COS 326 David Walker Princeton University

ob-ject: to feel distaste for something Webster's Dictionary

Data Types. Prof. Clarkson Fall Today s music: Pokémon Theme by Jason Paige

CSCI-GA Scripting Languages

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

Hash Tables. Nate Foster Spring 2018

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

Introduction to OCaml

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

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

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

CS 11 Ocaml track: lecture 3

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

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

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS Lecture 14: Hash tables. Prof. Clarkson Spring Today s music: Re-hash by Gorillaz

CSCI 2041: Functions, Mutation, and Arrays

Some Advanced ML Features

CS 231 Data Structures and Algorithms, Fall 2016

Programming Languages and Techniques (CIS120)

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Mutation. COS 326 Andrew W. Appel Princeton University. slides copyright David Walker and Andrew W. Appel

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

CSE 341: Programming Languages

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

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

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

Imperative programming in F#

Lecture 5 Abstract Data Types

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

G Programming Languages - Fall 2012

Control Structures in Java if-else and switch

Monads. Prof. Clarkson Fall Today s music: Vámanos Pal Monte by Eddie Palmieri

Testing. Prof. Clarkson Fall Today s music: Wrecking Ball by Miley Cyrus

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Lecture 02, Fall 2018 Friday September 7

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

Behavioral Equivalence

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

The Environment Model. Nate Foster Spring 2018

CSE 341: Programming Languages

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

THE PRAGMATIC INTRO TO REACT. Clayton Anderson thebhwgroup.com WEB AND MOBILE APP DEVELOPMENT AUSTIN, TX

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

Introduction to Nate Foster Spring 2018

Programming Languages

Introduction to C++ with content from

Ruby: Introduction, Basics

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

Favoring Isolated Mutability The Actor Model of Concurrency. CSCI 5828: Foundations of Software Engineering Lecture 24 04/11/2012

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Syntax Errors; Static Semantics

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

About this exam review

CS 11 Ocaml track: lecture 2

OCaml Language Choices CMSC 330: Organization of Programming Languages

Functions. Nate Foster Spring 2018

C++ for Java Programmers

PIC 10A Objects/Classes

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

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

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

Programming Abstractions

G Programming Languages - Fall 2012

Mutable References. Chapter 1

Lectures Basics in Procedural Programming: Machinery

Data Types. Guest Lecture: Andrew Myers Spring 2018

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Implementing Recursion

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Encapsulation in C++

CS/ENGRD 2110 SPRING 2018

Chapter 11 :: Functional Languages

CITS 3242 Programming Paradigms Part II. Topic 10: Imperative Programming

COMP 202 Java in one week

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

Lecture 15: Even more pointer stuff Virtual function table

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Operator overloading

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

L3 Programming September 19, OCaml Cheatsheet

Transcription:

Mutable Data Types A New Despair Mutability Strikes Back Return of Imperative Programming Prof. Clarkson Fall 2017 Today s music: The Imperial March from the soundtrack to Star Wars, Episode V: The Empire Strikes Back

Review Previously in 3110: Advanced data structures Streams and laziness Balanced binary trees Today: THE DARK SIDE ARRIVES Mutable data types: refs, mutable fields, arrays

Mutable features of OCaml Time to finally admit that OCaml has mutable features It is not a pure language Pure = no side effects Sometimes it really is best to allow values to change: call a function that returns an incremented counter every time efficient hash tables OCaml variables really are immutable But OCaml has mutable references, fields, and arrays...

REFS

References aka ref or ref cell Pointer to a typed location in memory # let x = ref 0;; x val x : int ref = {contents = 0} #!x;; - : int = 0 # x:=1;; x - : unit = () #!x;; - : int = 1 0 1

References The binding of x to the pointer is immutable, as always But the contents of the memory may change

Implementing a counter let counter = ref 0 let next_val = fun () -> counter := (!counter) + 1;!counter next_val() returns 1 then next_val() returns 2 then next_val() returns 3 etc.

Implementing a counter (* better: hides [counter] *) let next_val = let counter = ref 0 in fun () -> incr counter;!counter

Question What's wrong with this implementation? let next_val = fun () -> let counter = ref 0 in incr counter;!counter A. It won't compile, because counter isn't in scope in the final line B. It returns a reference to an integer instead of an integer C. It returns the wrong integer D. Nothing is wrong E. I don't know

Question What's wrong with this implementation? let next_val = fun () -> let counter = ref 0 in incr counter;!counter A. It won't compile, because counter isn't in scope in the final line B. It returns a reference to an integer instead of an integer C. It returns the wrong integer D. Nothing is wrong E. I don't know

Compare these implementations (* works *) let next_val = let counter = ref 0 in fun () -> incr counter;!counter (* broken *) let next_val = fun () -> let counter = ref 0 in incr counter;!counter Q: Why does the first implementation work? A: the anonymous function captures counter in its scope

References Syntax: ref e Evaluation: Evaluate e to a value v Allocate a new location loc in memory to hold v Store v in loc Return loc Note: locations are values; can pass and return from functions Type checking: New type constructor: t ref where t is a type Note: ref is used as keyword in type and as keyword in value ref e : t ref if e : t

References Syntax: e1 := e2 Evaluation: Evaluate e2 to a value v2 Evaluate e1 to a location loc Store v2 in loc Return () Type checking: If e2 : t and e1 : t ref then e1:=e2 : unit

References Syntax:!e note: not negation Evaluation: Evaluate e to loc Return contents of loc Type checking: If e : t ref then!e : t

References Syntax: e1; e2 Evaluation: Evaluate e1 to a value v1 Then throw away that value (note: e1 could have side effects) evaluate e2 to a value v2 return v2 Type checking: If e1 : unit and e2 : t then e1; e2 : t

Question What does w evaluate to? let x = ref 42 let y = ref 42 let z = x let () = x := 43 let w = (!y) + (!z) A. 42 B. 84 C. 85 D. 86 E. None of the above

Question What does w evaluate to? let x = ref 42 let y = ref 42 let z = x let () = x := 43 let w = (!y) + (!z) A. 42 B. 84 C. 85 D. 86 E. None of the above

Aliases References may have aliases: let x = ref 42 let y = ref 42 let z = x let () = x := 43 let w = (!y) + (!z) z and x are aliases: in "let z = x", x evaluates to a location, and z is bound to the same location changing the contents of that location will cause both!x and!z to change

Equality Suppose we have two refs... let r1 = ref 3110 let r2 = ref 3110 Double equals is physical equality r1 == r1 r1!= r2 Single equals is structural equality r1 = r1 r1 = r2 ref 3110 <> ref 2110 You usually want single equals

MUTABLE FIELDS

Mutable fields Fields of a record type can be declared as mutable: # type point = {x:int; y:int; mutable c:string};; type point = {x:int; y:int; mutable c:string; } # let p = {x=0; y=0; c="red"};; val p : point = {x=0; y=0; c="red"} # p.c <- "white";; - : unit = () # p;; val p : point = {x=0; y=0; c="white"} # p.x <- 3;; Error: The record field x is not mutable

Implementing refs Ref cells are essentially syntactic sugar: type 'a ref = { mutable contents: 'a } let ref x = { contents = x } let (! ) r = r.contents let ( := ) r newval = r.contents <- newval That type is declared in Pervasives The functions are compiled down to something equivalent

ARRAYS

Arrays Arrays generalize ref cells from a single mutable value to a sequence of mutable values # let v = [ 0.; 1. ];; val v : float array = [ 0.; 1. ] # v.(0) <- 5.;; - : unit = () # v;; - : float array = [ 5.; 1. ]

Arrays Syntax: [ e1;...; en ] Evaluation: evaluates to an n-element array elements are initialized to v1...vn if e1 evaluates to v1,..., en evaluates to vn Type checking: [ e1;...; en ] : t array if each ei : t

Arrays Syntax: e1.(e2) Evaluation: if e1 evaluates to v1, and e2 evaluates to v2, and 0<=v2<n, where n is the length of array v1, then e1.(e2) evaluates to element at offset v2 of v1. If v2<0 or v2>=n, raises Invalid_argument. Type checking: e1.(e2) : t if e1 : t array and e2 : int

Arrays Syntax: e1.(e2) <- e3 Evaluation: if e1 evaluates to v1, and e2 evaluates to v2, and 0<=v2<n, where n is the length of array v1, and e3 evaluates to v3, then mutate element at offset v2 of v1 to be v3. If v2<0 or v2>=n, raise Invalid_argument. Evaluates to (). Type checking: e1.(e2) <- e3 : unit if e1 : t array and e2 : int and e3 : t See Array module for more operations, including more ways to create arrays

Control structures Traditional loop structures are useful with arrays: while e1 do e2 done for x=e1 to e2 do e3 done for x=e1 downto e2 do e3 done (they work like you expect)

BEWARE

Beware Immutability is a valuable non-feature might seem weird that lack of feature is valuable...

Immutable lists We have never needed to worry about aliasing with lists! let x = [2;4] let y = [5;3;0] let z = x @ y vs. x y z 2 4 5 3 0 2 4 x 2 4 y z 5 3 0 2 4 5 3 0 (no code you write could ever tell, but OCaml implementation uses the first one)

OCaml vs. Java on mutable data OCaml: blissfully unaware of aliasing Impossible to tell where there is aliasing (except when using imperative features) Example: List.tl is constant time; does not copy rest of the list Java: obsession with aliasing and object identity Must be, so that subsequent assignments affect the right parts of the program Often crucial to make copies in just the right places

Java security nightmare (bad code) class ProtectedResource { private Resource theresource =...; private String[] allowedusers =...; public String[] getallowedusers() { return allowedusers; } public String currentuser() {... } public void usetheresource() { for(int i=0; i < allowedusers.length; i++) { if(currentuser().equals(allowedusers[i])) {... // access allowed: use it return; } } throw new IllegalAccessExcpetion(); } }

Have to make copies The problem: p.getallowedusers()[0] = p.currentuser(); p.usetheresource(); The fix: public String[] getallowedusers() { return a copy of allowedusers } Similar errors as recent as Java 1.7beta

Benefits of immutability Programmer doesn t have to think about aliasing; can concentrate on other aspects of code Language implementation is free to use aliasing, which is cheap Often easier to reason about whether code is correct Perfect fit for concurrent programming But there are downsides: I/O is fundamentally about mutation Some data structures (hash tables, arrays, ) hard(er) to implement in pure style Try not to abuse your new-found power!

Upcoming events [Sat-Tue] Fall Break; no OH; Piazza deactivated to give your fellow students on staff a break, too [Thur] lecture canceled; Prelim at 5:30 and 7:30 This is (reluctantly) imperative. THIS IS 3110