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

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

Type Systems Winter Semester 2006

References. ( pointers )

Types and Programming Languages. Lecture 6. Normalization, references, and exceptions

Mutable References. Chapter 1

Part VI. Imperative Functional Programming

Programming Languages Lecture 15: Recursive Types & Subtyping

Part III. Chapter 15: Subtyping

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

References and Exceptions. CS 565 Lecture 14 4/1/08

We defined congruence rules that determine the order of evaluation, using the following evaluation

Programming Languages and Techniques (CIS120)

Subsumption. Principle of safe substitution

Lecture Notes on Memory Layout

Programming Languages Lecture 14: Sum, Product, Recursive Types

Simply-Typed Lambda Calculus

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

CIS 500 Software Foundations Fall October 2

Reasoning About Imperative Programs. COS 441 Slides 10

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

Programming Languages

CIS 500 Software Foundations Fall September 25

CIS 500 Software Foundations Midterm I

Checks and Balances - Constraint Solving without Surprises in Object-Constraint Programming Languages: Full Formal Development

Type Systems Winter Semester 2006

CMSC 330: Organization of Programming Languages. Operational Semantics

Chapter 11 :: Functional Languages

CS558 Programming Languages

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Lecture 1: Overview

Rely-Guarantee References for Refinement Types over Aliased Mutable Data

Programming Languages Fall 2014

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Lectures Basics in Procedural Programming: Machinery

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

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Qualifying Exam in Programming Languages and Compilers

Hoare logic. WHILE p, a language with pointers. Introduction. Syntax of WHILE p. Lecture 5: Introduction to separation logic

Hoare logic. Lecture 5: Introduction to separation logic. Jean Pichon-Pharabod University of Cambridge. CST Part II 2017/18

Recursive Types and Subtyping

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

The PCAT Programming Language Reference Manual

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

Lecture Notes on Data Representation

Weeks 6&7: Procedures and Parameter Passing

Modules, Structs, Hashes, and Operational Semantics

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

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

References and Mutable Data Structures

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Variables and numeric types

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

CSE 307: Principles of Programming Languages

CS558 Programming Languages

Part III Chapter 15: Subtyping

Programming Languages and Techniques (CIS120)

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Recursive Types and Subtyping

12. Pointers Address-of operator (&)

Static Program Analysis Part 1 the TIP language

3.7 Denotational Semantics

More Lambda Calculus and Intro to Type Systems

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

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

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

CHAPTER ONE OVERVIEW. 1.1 Continuation-passing style

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

Operational Semantics. One-Slide Summary. Lecture Outline

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS558 Programming Languages

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell)

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Concepts of programming languages

Type Systems, Type Inference, and Polymorphism

Second-Order Type Systems

Programming Languages: Application and Interpretation

G Programming Languages - Fall 2012

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

G Programming Languages - Fall 2012

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Hierarchical Pointer Analysis for Distributed Programs

CS422 - Programming Language Design

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

Type Checking and Type Inference

CS558 Programming Languages

Introduction to the Lambda Calculus

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

+ Abstract Data Types

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Chapter 8 :: Composite Types

Chapter 5 Names, Binding, Type Checking and Scopes

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

CMSC 331 Final Exam Section 0201 December 18, 2000

Transcription:

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

References

Mutability So far, what we discussed does not include computational effects (also known as side effects). In particular, whenever we defined function, we never changed variables or data. Rather, we always computed new data. For instance, the operations to insert an item into the data structure didn't effect the old copy of the data structure. Instead, we always built a new data structure with the item appropriately inserted. For the most part, programming in a functional style (i.e., without side effects) is a "good thing" because it's easier to reason locally about the behavior of the program.

Mutability In most programming languages, variables are mutable i.e., a variable provides both a name that refers to a previously calculated value, and the possibility of overwriting this value with another (which will be referred to by the same name) In some languages (e.g., OCaml), these features are separate: variables are only for naming the binding between a variable and its value is immutable introduce a new class of mutable values (called reference cells or references) at any given moment, a reference holds a value (and can be dereferenced to obtain this value) a new value may be assigned to a reference

Mutability Writing values into memory locations is the fundamental mechanism of imperative languages such as Pascal or C. Mutable structures are required to implement many efficient algorithms. They are also very convenient to represent the current state of a state machine.

Basic Examples r = ref 5!r r := 7 (r:=succ(!r);!r) (r:=succ(!r); r:=succ(!r); r:=succ(!r); r:=succ(!r);!r) i.e., ((((r:=succ(!r); r:=succ(!r)); r:=succ(!r)); :=succ(!r));!r)

Reference Basic operations: allocation : ref (operator) dereferencing :! assignment: := Is there any difference between?: 5 + 8; r: =7; (r:=succ(!r);!r)

Aliasing A value of type ref T is a pointer to a cell holding a value of type T. r = If this value is copied by assigning it to another variable, the cell pointed to is not copied. ( r and s are aliases) 5 r = s = So we can change r by assigning to s: (s:=10;!r) 5

Aliasing all around us Reference cells are not the only language feature that introduces the possibility of aliasing. arrays communication channels I/O devices (disks, etc.)

The difficulties of aliasing The possibility of aliasing invalidates all sorts of useful forms of reasoning about programs, both by programmers... e.g., function λλ: RRR NNN. λλ: RRR NNN. (r 2; s 3;! r) always returns 2 unless r and s are aliases....and by compilers: Code motion out of loops, common subexpression elimination, allocation of variables to registers, and detection of uninitialized variables all depend upon the compiler knowing which objects a load or a store operation could reference. High-performance compilers spend significant energy on alias analysis to try to establish when different variables cannot possibly refer to the same storage.

The benefits of aliasing The problems of aliasing have led some language designers simply to disallow it (e.g., Haskell). But there are good reasons why most languages do provide constructs involving aliasing: efficiency (e.g., arrays) action at a distance (e.g., symbol tables) shared resources (e.g., locks) in concurrent systems etc.

Example c = rrr 0 incc = λλ: UUUU. (c ssss! c ;! c) decc = λλ: UUUU. (c pppp! c ;! c) incc uuuu dddd uuuu o = {i = iiii, d = dddd} lll nnnnnnnnnn =o λ.uuuu. lll c = rrr 0 ii let incc = λλ: UUUU. (c ssss! c ;! c) in let decc = λλ: UUUU. c pppp! c ;! c let o = {i = incc, d = dddd} in o

Syntax... plus other familiar types, in examples.

Typing rules

Example

Evaluation What is the value of the expression ref 0? Crucial observation: evaluating ref 0 must do something? Otherwise, r = ref 0 s = ref 0 and r = ref 0 s = r would behave the same. Specifically, evaluating ref 0 should allocate some storage and yield a reference (or pointer) to that storage. So what is a reference?

The store A reference names a location in the store (also known as the heap or just the memory). What is the store? Concretely: An array of 8-bit bytes, indexed by 32-bit integers. More abstractly: an array of values. Even more abstractly: a partial function from locations to values.

Locations Syntax of values:... and since all values are terms...

Syntax of Terms

Aside Does this mean we are going to allow programmers to write explicit locations in their programs?? No: This is just a modeling trick. We are enriching the source language to include some run-time structures, so that we can continue to formalize evaluation as a relation between source terms. Aside: If we formalize evaluation in the big-step style, then we can add locations to the set of values (results of evaluation) without adding them to the set of terms.

Evaluation The result of evaluating a term now depends on the store in which it is evaluated. Moreover, the result of evaluating a term is not just a value we must also keep track of the changes that get made to the store. i.e., the evaluation relation should now map a term and a store to a reduced term and a new store. t μ t μμ We use the metavariable μ to range over stores.

Evaluation An assignment t 1 t 2 first evaluates t 1 and t 2 until they become values...... and then returns unit and updates the store:

Evaluation A term of the form ref t 1 first evaluates inside t 1 until it becomes a value...... and then chooses (allocates) a fresh location l, augments the store with a binding from l to v 1, and returns l :

Evaluation A term!t 1 first evaluates in t 1 until it becomes a value...... and then looks up this value (which must be a location, if the original term was well typed) and returns its contents in the current store

Evaluation Evaluation rules for function abstraction and application are augmented with stores, but don t do anything with them directly.

Aside Garbage Collection Note that we are not modeling garbage collection the store just grows without bound. Pointer Arithmetic We can t do any!

Store Typings

Typing Locations Question: What is the type of a location? Answer: It depends on the store! e.g., in the store (l 1 unit, l 2 unit), the term! l 2 has type Unit. But in the store (l 1 unit, l 2 λx: Unit. x), the term! l 2 has type Unit Unit.

Typing Locations first try Roughly: More precisely: I.e., typing is now a four-place relation (between contexts, stores, terms, and types).

Problem However, this rule is not completely satisfactory. For one thing, it can make typing derivations very large! e.g., if then how big is the typing derivation for! l 5?

Problem But wait... it gets worse. Suppose how big is the typing derivation for! l 2?

Store Typings Observation: The typing rules we have chosen for references guarantee that a given location in the store is always used to hold values of the same type. These intended types can be collected into a store typing a partial function from locations to types.

Store Typings E.g., for A reasonable store typing would be

Store Typings Now, suppose we are given a store typing describing the store μ in which we intend to evaluate some term t. Then we can use to look up the types of locations in t instead of calculating them from the values in μ. i.e., typing is now a four-place relation contexts, store typings, terms, and types.

Final typing rules

Store Typings Question: Where do these store typings come from? Answer: When we first typecheck a program, there will be no explicit locations, so we can use an empty store typing. So, when a new location is created during evaluation, we can observe the type of v 1 and extend the current store typing appropriately.

Safety

Preservation First attempt: just add stores and store typings in the appropriate places. Theorem(?): if Γ Σ t: T and if t μ t μ, then Γ Σ t : T Right?? Wrong! Why is this wrong? Because Σ and μ here are not constrained to have anything to do with each other!

Preservation A store μ is said to be well typed with respect to a typing context Γ and a store typing Σ, written Γ Σ μ, if doo μ = ddd Σ and Γ Σ μ l : Σ l for every l ddd μ. Theorem (?) : if Γ Σ t: T t μ t μμ Γ Σ μ then Γ Σ t : T Still wrong!

Preservation Creation of a new reference cell... l ddd μ ref v 1 μ l (μ,l v 1 ) (E-REFV)... breaks the correspondence between the store typing and the store.

Preservation Theorem: if Γ Σ t: T Γ Σ μ t μ t µ then, for some Σ Σ, Γ Σ t : T Γ Σ μ. A correct version. Proof: Easy extension of the preservation proof for λ.

Progress Theorem: Suppose t is a closed, well-typed term (that is, Σ t: T for some T and Σ). Then either t is a value or else, for any store μ such that Σ μ, there is some term t and store μ with t μ t μ.

Nontermination via references There are well-typed terms in this system that are not strongly normalizing. For example: t1 = λr: Ref Unit Unit. (r λx: Unit.! r x ;! r unit); t2 = ref λx: Unit. x ; Applying t1 to t2 yields a (well-typed) divergent term.

Recursion via references Indeed, we can define arbitrary recursive functions using references. 1. Allocate a ref cell and initialize it with a dummy function of the appropriate type: fact rrr = ref (λn: Nat. 0) 2. Define the body of the function we are interested in, using the contents of the reference cell for making recursive calls: fact bbbb = λn: Nat. if iszero n then 1 else times n ((! fact rrr )(pred n)) 3. Backpatch by storing the real body into the reference cell: fact rrr : = fact bbbb 4. Extract the contents of the reference cell and use it as desired: fact =! fact rrr fact 5

Homework Read chapter 13 Preview chapter 14 Read and chew over the codes of chap 10. HW: 13.5.2