References. ( pointers )

Size: px
Start display at page:

Download "References. ( pointers )"

Transcription

1 References ( pointers )

2 Basic Examples like C pointers r = ref 5!r r := 7 malloc *r *r = 7 (r:=succ(!r);!r) (r:=succ(!r); r:=succ(!r); r:=succ(!r); r:=succ(!r);!r)

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

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

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

6 The di culties of aliasing The possibility of aliasing invalidates all sorts of useful forms of reasoning about programs, both by programmers... The function r:ref Nat. s:ref Nat. (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 di erent variables cannot possibly refer to the same storage.

7 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: I e ciency (e.g., arrays) I action at a distance (e.g., symbol tables) I shared resources (e.g., locks) in concurrent systems I etc.

8 Example c = ref 0 incc = x:unit. (c := succ (!c);!c) ++c decc = x:unit. (c := pred (!c);!c) incc unit decc unit o = {i = incc, d = decc} Q: how about c++?

9 let newcounter = _:Unit. let c = ref 0 in let incc = x:unit. (c := succ (!c);!c) in let decc = x:unit. (c := pred (!c);!c) in let o = {i = incc, d = decc} in o

10 Syntax t ::= terms unit unit constant x variable x:t.t abstraction t t application ref t reference creation!t dereference t:=t assignment... plus other familiar types, in examples.

11 Typing Rules ` t 1 : T 1 ` ref t 1 : Ref T 1 (T-Ref) ` t 1 : Ref T 1 `!t 1 : T 1 (T-Deref) ` t 1 : Ref T 1 ` t 2 : T 1 ` t 1 :=t 2 : Unit (T-Assign)

12 Final example NatArray = Ref (Nat!Nat); newarray = _:Unit. ref ( n:nat.0); : Unit! NatArray lookup = a:natarray. n:nat. (!a) n; : NatArray! Nat! Nat update = a:natarray. m:nat. v:nat. let oldf =!a in a := ( n:nat. if equal m n then v else oldf n); : NatArray! Nat! Nat! Unit

13 Evaluation What is the value of the expression ref 0?

14 Evaluation What is the value of the expression ref 0? Crucial observation: evaluating ref 0 must do something. Otherwise, and r = ref 0 s = ref 0 r = ref 0 s = r would behave the same.

15 Evaluation What is the value of the expression ref 0? Crucial observation: evaluating ref 0 must do something. Otherwise, and r = ref 0 s = ref 0 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.

16 Evaluation What is the value of the expression ref 0? Crucial observation: evaluating ref 0 must do something. Otherwise, and r = ref 0 s = ref 0 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?

17 The Store A reference names a location in the store (also known as the heap or just the memory). What is the store?

18 The Store A reference names a location in the store (also known as the heap or just the memory). What is the store? I Concretely: An array of 8-bit bytes, indexed by 32-bit integers.

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

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

21 Locations Syntax of values: v ::= values unit unit constant x:t.t abstraction value l store location... and since all values are terms...

22 Syntax of Terms t ::= terms unit unit constant x variable x:t.t abstraction t t application ref t reference creation!t dereference t:=t assignment l store location

23 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.

24 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 0 µ 0 We use the metavariable µ to range over stores.

25 Evaluation An assignment t 1 :=t 2 first evaluates t 1 and t 2 until they become values... t 1 µ! t 0 1 µ0 t 1 :=t 2 µ! t 0 1 :=t 2 µ 0 (E-Assign1) t 2 µ! t 0 2 µ0 v 1 :=t 2 µ! v 1 :=t 0 2 µ0 (E-Assign2)... and then returns unit and updates the store: l:=v 2 µ! unit [l 7! v 2 ]µ (E-Assign)

26 A term of the form ref t 1 first evaluates inside t 1 until it becomes a value... t 1 µ! t 0 1 µ0 ref t 1 µ! ref t 0 1 µ0 (E-Ref)... and then chooses (allocates) a fresh location l, augments the store with a binding from l to v 1, and returns l: l /2 dom(µ) ref v 1 µ! l (µ, l 7! v 1 ) (E-RefV) p = malloc(...)

27 A term!t 1 first evaluates in t 1 until it becomes a value... t 1 µ! t 0 1 µ0!t 1 µ!!t 0 1 µ0 (E-Deref)... 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: µ(l) =v!l µ! v µ (E-DerefLoc)

28 Evaluation rules for function abstraction and application are augmented with stores, but don t do anything with them directly. t 1 µ! t 0 1 µ0 t 1 t 2 µ! t 0 1 t 2 µ 0 (E-App1) t 2 µ! t 0 2 µ0 v 1 t 2 µ! v 1 t 0 2 µ0 (E-App2) ( x:t 11.t 12 ) v 2 µ! [x 7! v 2 ]t 12 µ (E-AppAbs)

29 Aside: garbage collection Note that we are not modeling garbage collection the store just grows without bound.

30 Aside: pointer arithmetic We can t do any!

31 Store Typings

32 Typing Locations Q: What is the type of a location?

33 Typing Locations Q: What is the type of a location? A: It depends on the store! E.g., in the store (l 1 7! unit, l 2 7! unit), the term!l 2 has type Unit. But in the store (l 1 7! unit, l 2 7! type Unit!Unit. x:unit.x), the term!l 2 has

34 Typing Locations first try Roughly: ` µ(l) : T 1 ` l : Ref T 1

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

36 Problem However, this rule is not completely satisfactory. For one thing, it can make typing derivations very large! E.g., if (µ = l 1 7! x:nat. 999, l 2 7! x:nat.!l 1 (!l 1 x), l 3 7! x:nat.!l 2 (!l 2 x), l 4 7! x:nat.!l 3 (!l 3 x), l 5 7! x:nat.!l 4 (!l 4 x)), then how big is the typing derivation for!l 5?

37 Problem! But wait... it gets worse. Suppose (µ = l 1 7! x:nat.!l 2 x, l 2 7! x:nat.!l 1 x), Now how big is the typing derivation for!l 2?

38 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.

39 E.g., for µ =(l 1 7! x:nat. 999, l 2 7! x:nat.!l 1 (!l 1 x), l 3 7! x:nat.!l 2 (!l 2 x), l 4 7! x:nat.!l 3 (!l 3 x), l 5 7! x:nat.!l 4 (!l 4 x)), A reasonable store typing would be =(l 1 7! Nat!Nat, l 2 7! Nat!Nat, l 3 7! Nat!Nat, l 4 7! Nat!Nat, l 5 7! Nat!Nat)

40 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 µ. (l) =T 1 ` l : Ref T 1 (T-Loc) I.e., typing is now a four-place relation between between contexts, store typings, terms, and types.

41 Final typing rules (l) =T 1 ` l : Ref T 1 (T-Loc) ` t 1 : T 1 ` ref t 1 : Ref T 1 (T-Ref) ` t 1 : Ref T 11 `!t 1 : T 11 (T-Deref) ` t 1 : Ref T 11 ` t 2 : T 11 ` t 1 :=t 2 : Unit (T-Assign)

42 Q: Where do these store typings come from?

43 Q: Where do these store typings come from? A: 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, l /2 dom(µ) ref v 1 µ! l (µ, l 7! v 1 ) (E-RefV) we can observe the type of v 1 and extend the current store typing appropriately.

44 Safety

45 Preservation First attempt: just add stores and store typings in the appropriate places. Theorem (?): If ` t : T and t µ! t 0 µ 0, then ` t 0 : T.

46 Preservation First attempt: just add stores and store typings in the appropriate places. Theorem (?): If ` t : T and t µ! t 0 µ 0, then ` t 0 : T. Wrong! Why is this wrong?

47 Preservation First attempt: just add stores and store typings in the appropriate places. Theorem (?): If ` t : T and t µ! t 0 µ 0, then ` t 0 : T. Wrong! Why is this wrong? Because and µ here are not constrained to have anything to do with each other! (Exercise: Construct an example that breaks this statement of preservation.)

48 Preservation A store µ is said to be well typed with respect to a typing context and a store typing, written ` µ, if dom(µ) =dom( ) and ` µ(l) : (l) for every l 2 dom(µ).

49 Preservation A store µ is said to be well typed with respect to a typing context and a store typing, written ` µ, if dom(µ) =dom( ) and ` µ(l) : (l) for every l 2 dom(µ). Next attempt: Theorem (?): If ` t : T t µ! t 0 µ 0 ` µ then ` t 0 : T.

50 Preservation A store µ is said to be well typed with respect to a typing context and a store typing, written ` µ, if dom(µ) =dom( ) and ` µ(l) : (l) for every l 2 dom(µ). Next attempt: Theorem (?): If ` t : T t µ! t 0 µ 0 ` µ then ` t 0 : T. Still wrong! What s wrong now?

51 Preservation A store µ is said to be well typed with respect to a typing context and a store typing, written ` µ, if dom(µ) =dom( ) and ` µ(l) : (l) for every l 2 dom(µ). Next attempt: Theorem (?): If ` t : T t µ! t 0 µ 0 ` µ then ` t 0 : T. Still wrong! Creation of a new reference cell... l /2 dom(µ) ref v 1 µ! l (µ, l 7! v 1 ) (E-RefV)... breaks the correspondence between the store typing and the store.

52 Preservation (correct version) Theorem: If ` t : T ` µ t µ! t 0 µ 0 then, for some 0, 0 ` t 0 : T 0 ` µ 0.

53 Preservation (correct version) Theorem: If ` t : T ` µ t µ! t 0 µ 0 then, for some 0, 0 ` t 0 : T 0 ` µ 0. Proof: Easy extension of the preservation proof for!.

54 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 0 and store µ 0 with t µ! t 0 µ 0.

Type Systems Winter Semester 2006

Type Systems Winter Semester 2006 Type Systems Winter Semester 2006 Week 9 December 13 December 13, 2006 - version 1.0 Plan PREVIOUSLY: unit, sequencing, let, pairs, sums TODAY: 1. recursion 2. state 3.??? NEXT: exceptions? NEXT: polymorphic

More information

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

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes 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

More information

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

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes Chapter 13: Reference Why reference Typing Evaluation Store Typings Safety Notes References Computational Effects Also known as side effects. A function or expression is said to have a side effect if,

More information

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

Types and Programming Languages. Lecture 6. Normalization, references, and exceptions Types and Programming Languages Lecture 6. Normalization, references, and exceptions Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016 Coming soon One more language features:

More information

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

References and Exceptions. CS 565 Lecture 14 4/1/08 References and Exceptions CS 565 Lecture 14 4/1/08 References In most languages, variables are mutable: it serves as a name for a location the contents of the location can be overwritten, and still be

More information

Mutable References. Chapter 1

Mutable References. Chapter 1 Chapter 1 Mutable References In the (typed or untyped) λ-calculus, or in pure functional languages, a variable is immutable in that once bound to a value as the result of a substitution, its contents never

More information

CIS 500 Software Foundations Midterm II Answer key November 16, 2005

CIS 500 Software Foundations Midterm II Answer key November 16, 2005 CIS 500 Software Foundations Midterm II Answer key November 16, 2005 Simply typed lambda-calculus The following questions refer to the simply typed lambda-calculus with booleans and error handling. The

More information

References and Mutable Data Structures

References and Mutable Data Structures References and Mutable Data Structures Principles of Programming Languages CSE 307 1 Syntax 2 Semantics 3 Version: 1.4 16:44:20 2012/11/29 Compiled at 09:37 on 2018/11/13 Programming Languages References

More information

Programming Languages Fall 2014

Programming Languages Fall 2014 Programming Languages Fall 2014 Lecture 7: Simple Types and Simply-Typed Lambda Calculus Prof. Liang Huang huang@qc.cs.cuny.edu 1 Types stuck terms? how to fix it? 2 Plan First I For today, we ll go back

More information

Part VI. Imperative Functional Programming

Part VI. Imperative Functional Programming Part VI Imperative Functional Programming Chapter 14 Mutable Storage MinML is said to be a pure language because the execution model consists entirely of evaluating an expression for its value. ML is

More information

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

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE Type Systems Pierce Ch. 3, 8, 11, 15 CSE 6341 1 A Simple Language ::= true false if then else 0 succ pred iszero Simple untyped expressions Natural numbers encoded as succ succ

More information

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages Lecture 15: Recursive Types & Subtyping CSE 230: Winter 2008 Principles of Programming Languages Lecture 15: Recursive Types & Subtyping Ranjit Jhala UC San Diego News? Formalize first-order type systems Simple types (integers and booleans)

More information

Chapter 22: Type Reconstruction (Type Inference)

Chapter 22: Type Reconstruction (Type Inference) Chapter 22: Type Reconstruction (Type Inference) Calculating a Principal Type for a Term Constraint based Typing Unification and Principle Types Extension with let-polymorphism Type Variables and Type

More information

Operational Semantics of Cool

Operational Semantics of Cool Operational Semantics of Cool Key Concepts semantics: the meaning of a program, what does program do? how the code is executed? operational semantics: high level code generation steps of calculating values

More information

Goals of this Lecture

Goals of this Lecture C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing

More information

Lecture Notes on Memory Layout

Lecture Notes on Memory Layout Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,

More information

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

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

More information

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics. One-Slide Summary. Lecture Outline Operational Semantics #1 One-Slide Summary Operational semantics are a precise way of specifying how to evaluate a program. A formal semantics tells you what each expression means. Meaning depends on context:

More information

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages CSE 307: Principles of Programming Languages Variables and Constants R. Sekar 1 / 22 Topics 2 / 22 Variables and Constants Variables are stored in memory, whereas constants need not be. Value of variables

More information

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

Checks and Balances - Constraint Solving without Surprises in Object-Constraint Programming Languages: Full Formal Development Checks and Balances - Constraint Solving without Surprises in Object-Constraint Programming Languages: Full Formal Development Tim Felgentreff, Todd Millstein, Alan Borning and Robert Hirschfeld Viewpoints

More information

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

Hoare logic. WHILE p, a language with pointers. Introduction. Syntax of WHILE p. Lecture 5: Introduction to separation logic Introduction Hoare logic Lecture 5: Introduction to separation logic In the previous lectures, we have considered a language, WHILE, where mutability only concerned program variables. Jean Pichon-Pharabod

More information

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

Hoare logic. Lecture 5: Introduction to separation logic. Jean Pichon-Pharabod University of Cambridge. CST Part II 2017/18 Hoare logic Lecture 5: Introduction to separation logic Jean Pichon-Pharabod University of Cambridge CST Part II 2017/18 Introduction In the previous lectures, we have considered a language, WHILE, where

More information

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008. Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008. Outline. Dynamic Allocation. Variables and Constants. Aliases and Problems. Garbage. Introduction. On Wednesday, we were talking

More information

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices. Memory and Addresses Memory is just a sequence of byte-sized storage devices. 1 The bytes are assigned numeric addresses, starting with zero, just like the indexing of the cells of an array. It is the

More information

CIS 500 Software Foundations Fall October 2

CIS 500 Software Foundations Fall October 2 CIS 500 Software Foundations Fall 2006 October 2 Preliminaries Homework Results of my email survey: There was one badly misdesigned (PhD) problem and a couple of others that were less well thought through

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016 Values and Types We divide the universe of values according to types A type is a set of values and a

More information

Costly software bugs that could have been averted with type checking

Costly software bugs that could have been averted with type checking Type Checking Class Notes from Lectures 6 and 7 Lahav Yeffet and Ori Folger The material in these lectures is based on the textbook Types and Programming Languages by Benjamin Pierce. Here is a list of

More information

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018 CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make

More information

` e : T. Gradual Typing. ` e X. Ronald Garcia University of British Columbia

` e : T. Gradual Typing. ` e X. Ronald Garcia University of British Columbia aaab/hicbvbns8naen34wetxtecvi0xwvbirfe9fd3qs0c9oqplsnu3s3stsbgqh1l/ixymixv0h3vw3btsctpxbwoo9gwbmbslnsjvot7w2vrg5tv3ake/u7r8c2kfhbzvkktawsxgiuweoyllmw5pptruppcactjvb6g7md8zukpbetz2n1bcwifnecggj9e2kdw9capbgiaghpvggn/t21ak5c+bv4hakigo0+vaxfyykeztwhinspddjtt8bqrnhdfr2mkvticmy0j6hmqiq/mn8+ck+m0qio0saijweq78njicuykvgogxoovr2zuj/xi/t0bu/yxgaarqtxaio41gnejyedpmkrppceccsmvsxgyieok1ezrocu/zykmlf1fyn5j5evuu3rrwldijo0tly0rwqowfuqc1eui6e0st6s56sf+vd+li0rlnftax9gfx5a8zmk40=

More information

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

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going? Types for References, Exceptions and Continuations Annoucements How s the midterm going? Meeting 21, CSCI 5535, Spring 2009 2 One-Slide Summary Review of Subtyping If τ is a subtype of σ then any expression

More information

Reasoning About Imperative Programs. COS 441 Slides 10

Reasoning About Imperative Programs. COS 441 Slides 10 Reasoning About Imperative Programs COS 441 Slides 10 The last few weeks Agenda reasoning about functional programming It s very simple and very uniform: substitution of equal expressions for equal expressions

More information

Verification & Validation of Open Source

Verification & Validation of Open Source Verification & Validation of Open Source 2011 WORKSHOP ON SPACECRAFT FLIGHT SOFTWARE Gordon Uchenick Coverity, Inc Open Source is Ubiquitous Most commercial and proprietary software systems have some open

More information

Heap Arrays and Linked Lists. Steven R. Bagley

Heap Arrays and Linked Lists. Steven R. Bagley Heap Arrays and Linked Lists Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index Variables and arrays have a type Create our

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

More information

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged

More information

Subsumption. Principle of safe substitution

Subsumption. Principle of safe substitution Recap on Subtyping Subsumption Some types are better than others, in the sense that a value of one can always safely be used where a value of the other is expected. Which can be formalized as by introducing:

More information

[0569] p 0318 garbage

[0569] p 0318 garbage A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the

More information

Advances in Programming Languages: Regions

Advances in Programming Languages: Regions Advances in Programming Languages: Regions Allan Clark and Stephen Gilmore The University of Edinburgh February 22, 2007 Introduction The design decision that memory will be managed on a per-language basis

More information

SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

More information

CIS 500 Software Foundations Midterm I

CIS 500 Software Foundations Midterm I CIS 500 Software Foundations Midterm I October 11, 2006 Name: Student ID: Email: Status: Section: registered for the course not registered: sitting in to improve a previous grade not registered: just taking

More information

Lecture 13: Subtyping

Lecture 13: Subtyping Lecture 13: Subtyping Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Programming Languages Pratikakis (CSD) Subtyping CS546, 2018-2019 1 / 15 Subtyping Usually found

More information

Qualifying Exam in Programming Languages and Compilers

Qualifying Exam in Programming Languages and Compilers Qualifying Exam in Programming Languages and Compilers University of Wisconsin Fall 1991 Instructions This exam contains nine questions, divided into two parts. All students taking the exam should answer

More information

Hoare triples. Floyd-Hoare Logic, Separation Logic

Hoare triples. Floyd-Hoare Logic, Separation Logic Hoare triples Floyd-Hoare Logic, Separation Logic 1. Floyd-Hoare Logic 1969 Reasoning about control Hoare triples {A} p {B} a Hoare triple partial correctness: if the initial state satisfies assertion

More information

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

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can

More information

Static Program Analysis Part 1 the TIP language

Static Program Analysis Part 1 the TIP language Static Program Analysis Part 1 the TIP language http://cs.au.dk/~amoeller/spa/ Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University Questions about programs Does the program terminate

More information

Low-Level C Programming. Memory map Pointers Arrays Structures

Low-Level C Programming. Memory map Pointers Arrays Structures Low-Level C Programming Memory map Pointers Arrays Structures Memory Map 0x7FFF_FFFF Binaries load at 0x20000 by default Stack start set by binary when started Stack grows downwards You will need one stack

More information

Rely-Guarantee References for Refinement Types over Aliased Mutable Data

Rely-Guarantee References for Refinement Types over Aliased Mutable Data Rely-Guarantee References for Refinement Types over Aliased Mutable Data Colin S. Gordon, Michael D. Ernst, and Dan Grossman University of Washington PLDI 2013 Static Verification Meets Aliasing 23 x:ref

More information

12. Pointers Address-of operator (&)

12. Pointers Address-of operator (&) 12. Pointers In earlier chapters, variables have been explained as locations in the computer's memory which can be accessed by their identifer (their name). This way, the program does not need to care

More information

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture Reminder of the last lecture Aliasing Issues: Call by reference, Pointer programs Claude Marché Cours MPRI 2-36-1 Preuve de Programme 18 janvier 2017 Additional features of the specification language Abstract

More information

Concepts of programming languages

Concepts of programming languages Concepts of programming languages Lecture 5 Wouter Swierstra 1 Announcements Submit your project proposal to me by email on Friday; The presentation schedule in now online Exercise session after the lecture.

More information

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

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness? Tradeoffs CSE 505: Programming Languages Lecture 15 Subtyping Zach Tatlock Autumn 2017 Desirable type system properties (desiderata): soundness - exclude all programs that get stuck completeness - include

More information

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

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Understanding Pointers

Understanding Pointers Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer

More information

a data type is Types

a data type is Types Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental

More information

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

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper) Gradual Typing for Functional Languages Jeremy Siek and Walid Taha (presented by Lindsey Kuper) 1 Introduction 2 What we want Static and dynamic typing: both are useful! (If you re here, I assume you agree.)

More information

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8 Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define

More information

CA31-1K DIS. Pointers. TA: You Lu

CA31-1K DIS. Pointers. TA: You Lu CA31-1K DIS Pointers TA: You Lu Pointers Recall that while we think of variables by their names like: int numbers; Computer likes to think of variables by their memory address: 0012FED4 A pointer is a

More information

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages Formal Semantics of Programming Languages Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson http://www.daimi.au.dk/~bra8130/wiley_book/wiley.html Benefits of formal

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far Lecture Outline Operational Semantics of Cool Lecture 13 COOL operational semantics Motivation Notation The rules Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2 Motivation We must specify

More information

Modules, Structs, Hashes, and Operational Semantics

Modules, Structs, Hashes, and Operational Semantics CS 152: Programming Language Paradigms Modules, Structs, Hashes, and Operational Semantics Prof. Tom Austin San José State University Lab Review (in-class) Modules Review Modules from HW 1 (in-class) How

More information

Compilation and Program Analysis (#11) : Hoare triples and shape analysis

Compilation and Program Analysis (#11) : Hoare triples and shape analysis Compilation and Program Analysis (#11) : Hoare triples and shape analysis Laure Gonnord http://laure.gonnord.org/pro/teaching/capm1.html Laure.Gonnord@ens-lyon.fr Master 1, ENS de Lyon dec 2017 Inspiration

More information

A Context-Sensitive Memory Model for Verification of C/C++ Programs

A Context-Sensitive Memory Model for Verification of C/C++ Programs A Context-Sensitive Memory Model for Verification of C/C++ Programs Arie Gurfinkel and Jorge A. Navas University of Waterloo and SRI International SAS 17, August 30th, 2017 Gurfinkel and Navas (UWaterloo/SRI)

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1 Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #34 Function with pointer Argument (Refer Slide Time: 00:05) So, here is the stuff that we have seen about pointers.

More information

Lecture 2: C Programm

Lecture 2: C Programm 0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes

More information

Limitations of the stack

Limitations of the stack The heap hic 1 Limitations of the stack int *table_of(int num, int len) { int table[len+1]; for (int i=0; i

More information

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8 Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns

More information

Lecture 20 C s Memory Model

Lecture 20 C s Memory Model Lecture 20 C s Memory Model 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons 1 The C0 and C Memory Model When we talk about memory in C0, C1, and C, that memory is

More information

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far. Lecture Outline Operational Semantics of Cool COOL operational semantics Motivation Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Notation The rules CS781(Prasad) L24CG 1 CS781(Prasad)

More information

Memory and Pointers written by Cathy Saxton

Memory and Pointers written by Cathy Saxton Memory and Pointers written by Cathy Saxton Basic Memory Layout When a program is running, there are three main chunks of memory that it is using: A program code area where the program itself is loaded.

More information

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples: 1 Programming in C Pointer Variable A variable that stores a memory address Allows C programs to simulate call-by-reference Allows a programmer to create and manipulate dynamic data structures Must be

More information

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University CS558 Programming Languages Winter 2018 Lecture 4a Andrew Tolmach Portland State University 1994-2018 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Lesson 4 Typed Arithmetic Typed Lambda Calculus Lesson 4 Typed Arithmetic Typed Lambda 1/28/03 Chapters 8, 9, 10 Outline Types for Arithmetic types the typing relation safety = progress + preservation The simply typed lambda calculus Function types

More information

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Chapter 8 & Chapter 9 Main Memory & Virtual Memory Chapter 8 & Chapter 9 Main Memory & Virtual Memory 1. Various ways of organizing memory hardware. 2. Memory-management techniques: 1. Paging 2. Segmentation. Introduction Memory consists of a large array

More information

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial Week 7 General remarks Arrays, lists, pointers and 1 2 3 We conclude elementary data structures by discussing and implementing arrays, lists, and trees. Background information on pointers is provided (for

More information

Lecture 20 Notes C s Memory Model

Lecture 20 Notes C s Memory Model Lecture 20 Notes C s Memory Model 15-122: Principles of Imperative Computation (Fall 2015) Frank Pfenning, Rob Simmons 1 The C0 and C Memory Model When we talk about memory in C0, C1, and C, that memory

More information

Hoare Logic and Model Checking

Hoare Logic and Model Checking Hoare Logic and Model Checking Kasper Svendsen University of Cambridge CST Part II 2016/17 Acknowledgement: slides heavily based on previous versions by Mike Gordon and Alan Mycroft Pointers Pointers and

More information

CIS 500 Software Foundations Fall December 6

CIS 500 Software Foundations Fall December 6 CIS 500 Software Foundations Fall 2006 December 6 Administrivia Administrivia No recitations this week Extra office hours will be posted to the class mailing list Exam: Wednesday, Dec 20, 9 11 Location:

More information

Administrivia. Existential Types. CIS 500 Software Foundations Fall December 6. Administrivia. Motivation. Motivation

Administrivia. Existential Types. CIS 500 Software Foundations Fall December 6. Administrivia. Motivation. Motivation CIS 500 Software Foundations Fall 2006 Administrivia December 6 Administrivia No recitations this week Extra office hours will be posted to the class mailing list Exam: Wednesday, Dec 20, 9 11 Location:

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 13 February 12, 2018 Mutable State & Abstract Stack Machine Chapters 14 &15 Homework 4 Announcements due on February 20. Out this morning Midterm results

More information

Lecture 1: Overview

Lecture 1: Overview 15-150 Lecture 1: Overview Lecture by Stefan Muller May 21, 2018 Welcome to 15-150! Today s lecture was an overview that showed the highlights of everything you re learning this semester, which also meant

More information

Stanford University Computer Science Department CS 295 midterm. May 14, (45 points) (30 points) total

Stanford University Computer Science Department CS 295 midterm. May 14, (45 points) (30 points) total Stanford University Computer Science Department CS 295 midterm May 14, 2008 This is an open-book exam. You have 75 minutes. Write all of your answers directly on the paper. Make your answers as concise

More information

POINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory

More information

Compiler Construction

Compiler Construction Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Recap: Static Data Structures Outline of Lecture 18 Recap:

More information

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a

More information

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types CSE 230: Winter 200 Principles of Programming Languages Lecture 4: Sum, Product, Recursive Types The end is nigh HW 3 No HW 4 (= Final) Project (Meeting + Talk) Ranjit Jhala UC San Diego Recap Goal: Relate

More information

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1 Midterm 2 Solutions 1. [20 points] Consider the language that consist of possibly empty lists of the identifier x enclosed by parentheses and separated by commas. The language includes { () (x) (x,x) (x,x,x)

More information

UNIT-4 (COMPILER DESIGN)

UNIT-4 (COMPILER DESIGN) UNIT-4 (COMPILER DESIGN) An important part of any compiler is the construction and maintenance of a dictionary containing names and their associated values, such type of dictionary is called a symbol table.

More information

CS61, Fall 2012 Section 2 Notes

CS61, Fall 2012 Section 2 Notes CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors

More information

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare. 6.172 Performance Engineering of Software Systems Spring 2009 Lecture 9 MIT OpenCourseWare Dynamic Storage Allocation Stack allocation: LIFO (last-in-first-out) Array and pointer A used unused P before

More information

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns What is an array? Pointers Memory issues The name of the array is actually a memory address. You can prove this by trying to print

More information

Chapter 5. Names, Bindings, and Scopes

Chapter 5. Names, Bindings, and Scopes Chapter 5 Names, Bindings, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants 1-2 Introduction Imperative

More information

CS 61C: Great Ideas in Computer Architecture Introduction to C

CS 61C: Great Ideas in Computer Architecture Introduction to C CS 61C: Great Ideas in Computer Architecture Introduction to C Instructors: Vladimir Stojanovic & Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/ 1 Agenda C vs. Java vs. Python Quick Start Introduction

More information

TRELLYS and Beyond: Type Systems for Advanced Functional Programming

TRELLYS and Beyond: Type Systems for Advanced Functional Programming TRELLYS and Beyond: Type Systems for Advanced Functional Programming Aaron Stump Computer Science The University of Iowa Joint work with Tim Sheard, Vilhelm Sjöberg, and Stephanie Weirich. Supported by

More information

A program execution is memory safe so long as memory access errors never occur:

A program execution is memory safe so long as memory access errors never occur: A program execution is memory safe so long as memory access errors never occur: Buffer overflows, null pointer dereference, use after free, use of uninitialized memory, illegal free Memory safety categories

More information

Programming Languages: Application and Interpretation

Programming Languages: Application and Interpretation Programming Languages: Application and Interpretation Version 6.7 October 26, 2016 This is the documentation for the software accompanying the textbook Programming Languages: Application and Interpretation

More information