Ownership Types for Object Encapsulation Authors:Chandrasekhar Boyapati Barbara Liskov Liuba Shrira Presented by: Charles Lin Course: CMSC 631.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Comp 212: Intermediate Programming Lecture 18 – Java Generics By: Anupam Chanda.
C++ Programming Languages
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Java Generics.
External Uniqueness Presented by Nir Atias Dave Clarke Tobias Wrigstad Encapsulation Seminar 2006.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Alias Annotations for Program Understanding Jonathan Aldrich Valentin Kostadinov Craig Chambers University of Washington.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Ownership Types for Object Encapsulation Barbara Liskov Chandrasekhar Boyapati Liuba Shrira Laboratory for Computer Science Massachusetts Institute of.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University.
Laboratory for Computer Science Massachusetts Institute of Technology Ownership Types for Safe Region-Based Memory Management in Real-Time Java Chandrasekhar.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
An Approach to Safe Object Sharing Ciaran Bryce & Chrislain Razafimahefa University of Geneva, Switzerland.
Lecture 9 Concepts of Programming Languages
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
CS (CCN 27241) Software Engineering for Scientific Computing Lecture 5: C++ Key Concepts.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
C++ Memory Overview 4 major memory segments Key differences from Java
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Mutability: C++ const usage SWE 332 Fall 2011 Paul Ammann.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Difficult Specifications in Javari Immutability Inference Jaime Quinonez Program Analysis Group April 20, 2007.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Lazy Modular Upgrades in Persistent Object Stores Chandrasekhar Boyapati, Barbara Liskov, Liuba Shrira, Chuang-Hue Moh, Steven Richman MIT CSAIL October.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Memory Management.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Review What is an object? What is a class?
Motivation and Overview
Java Programming: Guided Learning with Early Objects
A First C++ Class – a Circle
Lecture 9 Concepts of Programming Languages
Objects Managing a Resource
Object-Oriented Programming
Java Programming Language
Templates CMSC 202, Version 4/02.
Object Encapsulation CSC 422 Dr. Spiegel.
SPL – PS3 C++ Classes.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Ownership Types for Object Encapsulation Authors:Chandrasekhar Boyapati Barbara Liskov Liuba Shrira Presented by: Charles Lin Course: CMSC 631

A brief digression: const Consider the following C++ code: Produces following errors: class Foo { int x ; int go( const Bar & bar ) const { x = bar.mutate() ; } foo.cpp: In method `void Foo::foo(const Bar &) const': foo.cpp:20: passing `const Bar' as `this' argument of `int Bar::mutate()' discards qualifiers foo.cpp:20: assignment of member `Foo::x' in read-only structure

The burden of annotation Given const errors, what might a programmer do? const tells compiler that an object is not mutated Programmers may ignore this: bad! Even if programmers don’t need to do much annotation, they must know what to do

Modular reasoning Want to reason about programs to perform updates Want to reason on individual modules (e.g., a Java file) Want to avoid interference from objects in other modules (e.g., by object encapsulation)

Encapsulation Goal of encapsulation is to hide implementation private hides implementation…or does it? class Foo { private Bar b ; public Bar getBar() { return b ; // leaks out private data member }

Object Encapsulation Want to enforce encapsulation, i.e., can’t directly access encapsulated objects Consider Set class, implemented with ArrayList. arrList ~ ~ seto

“depends on”: Criteria for Encapsulation Objects often contain subobjects Object x depends on subobject s if mutations of s affect invariants of x Thus, s should encapsulate x

Example: Set Set should encapsulate ArrayList If set elements are immutable, then Set doesn’t have to encapsulate elements. set arrList abcd

Problem with Encapsulation Iterators need access to internal representation of object set arrListiter Solution: only allow violations within same module (e.g., Java inner classes)

Solution: Ownership Types Allows programmer to declare owners of objects If v is owner of s, then outside objects can not access s, except possibly through v Want to do this check statically

Rules for Ownership Types Every object has a (single) owner The owner can be another object or world The owner of an object does not change over time The ownership relation forms a tree rooted at world

Diagram of Ownership Tree Note: ownership is not transitive. o1 owns o2, but o1 does not own o3 world o1 o2 o3 o4 o5 o6 o7

What Can Objects Access? Itself and objects it owns Its ancestors in the ownership tree, and objects they own Anything owned by world (recall, no transitivity of ownership)

How to Annotate class TStack { TNode head ; } // Nodes in stack class TNode { TNode next ; T value ; } // Data class T {...}

What does annotation mean? Syntax looks like templates, but contains ownership types instead First parameter is “real” owner. Remaining parameters pass ownership types to subobjects Rule: Object o 1 <= o i, for 1 < i <= n where x <= y means x is descendant of y in ownership tree

Example: TStack TStack TNode T Client world

Inner Classes: Handling Iterators Allow inner classes to break encapsulation Inner classes have type annotations just like outer classes Inner classes do not “inherit” type annotations from outer class

Methods can have ownership types Allows wrapper objects to have “freer” ownership types than objects they wrap. See example with elements() method class TStack { TStackEnum elements () where (enumOwner <= TOwner) {…}..}

Encapsulation Theorem x can access an object owned by o only if (x <= o) or x is an inner class of object o Proof is due to restrictions placed on ownership type parameter list

Type Rules The proof is left to the interested reader

Applications Upgrades in persistent object stores TF(x) only accesses objects owned by x Controlling aliasing Data races Extension: ownership types combined with region types

Key people in Ownership Types Chandrasekhar Boyapati Dave Clarke

Thoughts Annotation Burden? (System uses type inferencing, but still requires 1 in 30 lines must be annotated) Copy constructors? Can one object access subobjects of the same type? What if we don’t care about encapsulation everywhere? Seems simple…why are type rules complex?

Conclusion Ownership types aren’t magic. As with any type system, you must apply them so they are useful. In particular, you must determine the “depends on” relation to apply ownership correctly.