DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

Similar documents
Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

CPS 506 Comparative Programming Languages. Programming Language

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Concepts of Programming Languages

Data Abstraction. Hwansoo Han

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Types and Classes. I II From predefined (simple) and user-defined (composite) types via Abstract data types

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Fundamentals of Programming Languages

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

Chapter 5 Names, Binding, Type Checking and Scopes

Object-oriented Programming. Object-oriented Programming

Today s lecture. CS 314 fall 01 C++ 1, page 1

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

Lecture Notes on Programming Languages

CSC 533: Organization of Programming Languages. Spring 2005

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Chapter 5. Names, Bindings, and Scopes

Imperative Programming

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

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

CSCI312 Principles of Programming Languages!

Short Notes of CS201

Haskell 98 in short! CPSC 449 Principles of Programming Languages

CS201 - Introduction to Programming Glossary By

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Java Object Oriented Design. CSC207 Fall 2014

Inheritance and Substitution (Budd chapter 8, 10)

September 10,

TYPES, VALUES AND DECLARATIONS

OBJECT ORIENTED PROGRAMMING USING C++

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Object Oriented Paradigm

Chapter 10 :: Data Abstraction and Object Orientation

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Types. What is a type?

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

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

G Programming Languages - Fall 2012

CSE 307: Principles of Programming Languages

22c:111 Programming Language Concepts. Fall Types I

Programming Languages 2nd edition Tucker and Noonan"

Object Oriented Issues in VDM++

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

CSE 307: Principles of Programming Languages

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Chapter 10 Object-Oriented Programming

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Types and Type Inference

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Chapter 9 :: Data Abstraction and Object Orientation

Chapter 5 Names, Bindings, Type Checking, and Scopes

Names, Scopes, and Bindings II. Hwansoo Han

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

Classes, subclasses, subtyping

Chapter 5 Object-Oriented Programming

Inheritance and Interfaces

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

What are the characteristics of Object Oriented programming language?

Lecturer: William W.Y. Hsu. Programming Languages

Subtyping (Dynamic Polymorphism)

Inheritance. OOP: Inheritance 1

VALLIAMMAI ENGINEERING COLLEGE

CS-202 Introduction to Object Oriented Programming

Implementing Subprograms

C++ Important Questions with Answers

G Programming Languages - Fall 2012

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

HANDLING NONLOCAL REFERENCES

Programming Languages, Summary CSC419; Odelia Schwartz

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Review sheet for Final Exam (List of objectives for this course)

Types and Type Inference

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Properties of an identifier (and the object it represents) may be set at

Programming Languages

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Instantiation of Template class

CS558 Programming Languages

Informatica 3 Syntax and Semantics

The role of semantic analysis in a compiler

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

CSC324 Principles of Programming Languages

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Inheritance. Transitivity

Transcription:

DATA TYPES CS 403: Types and Classes Stefan D. Bruda Fall 2017 Algorithms + data structures = programs Abstractions of data entities highly desirable Program semantics embedded in data types Data types enable semantic checking Data types can enhance design Data types can determine memory layout and allocation Issues: Extent to which type information is represented in program How types are constructed How types are checked (done) When types are checked (done) Data type = set of values + operations on those values A data type is an algebra Set of values defined by enumeration, subrange, or mathematical construction Set model with membership concept ( ) Set model of types means language type constructor equivalents for the set operations,,, CS 403: Types and Classes (S. D. Bruda) Fall 2017 1 / 25 DATA TYPES (CONT D) Determining types Explicit typing type determined at declaration, usually invariant throughout execution (Pascal, C, C++, Java) Implicit typing type determined by usage (Prolog, Lisp) Mixed implicit typing by default, but allows explicit declarations (Miranda, Haskell, ML) Simple types Pre-defined (int, bool, etc.) Enumeration enum colour {red, green, blue}; data Colour = Red Green Blue Ordinal (discrete order on elements i.e., not real) Type constructors: cartesian product U V = {(u, v) : u U, v V }; p 1 : U V U; p 2 : U V V Record implementation struct icr {int i; char c; double r;}; Tuple implementation type Icr = (Int, Char, Double) CS 403: Types and Classes (S. D. Bruda) Fall 2017 2 / 25 DATA TYPES (CONT D) Type constructors: union Undiscriminated unions uses names to distinguish components union intorreal {int i; double r}; Direct algebraic definition data Either a b = Left a Right b Type constructors: function types U V Array types A[i] is a mapping from int to the type of the array Issues regarding the type of the index: Restrict to int? (e.g., C) Restrict to subrange? (e.g., Pascal) Allow any ordinal type? (e.g., Perl) Is the index part of the type? Function types typedef int (*fun) (int, int) Int -> Int -> Int Type constructors: recursive types Least fixed point: int int int int int int struct intlist {int key; intlist next}; not legal in C++ (infinite recursion with no base case) Faked in C++: struct intlist {int key; intlist *next}; data IntList = Nil Cons Int IntList CS 403: Types and Classes (S. D. Bruda) Fall 2017 3 / 25

EXPLICIT POLYMORPHISM ALGEBRAIC DATA TYPE SPECIFICATION Advantages of implicit typing: Smaller code Can use explicit types if desired/needed Construction of a most general type polymorphism Polymorphic data structures require explicit polymorphism Example: To avoid separate definitions for IntStack, CharStack, StringStack, etc., introduce a mechanism to parameterise the data type declaration C++: template <typename T> struct StackNode { T data; StackNode<T> *next; }; template <typename T> struct Stack { StackNode<T> *thestack; }; Haskell: data Stack a = EmptyStack StackNode a (Stack a) Why data abstraction? Easier to think about hide what doesn t matter Protection prevent access to things you shouldn t see Plug compatibility Replacement of pieces often without recompilation, definitely without rewriting libraries Division of labor in software projects An abstract data type specifies the allowed operations and consistency constraints for some type without specifying an actual implementation An abstract data type can be specified as an algebra of operations on entities An algebraic specification of a data type consists of signatures of available operations, and axioms defining behaviour Algebraic types (implicitly) parameterized Operations divided into constructors and inspectors Axioms contain rules for each combination of constructor and inspector Existence of error operations (or restrictions) CS 403: Types and Classes (S. D. Bruda) Fall 2017 4 / 25 CS 403: Types and Classes (S. D. Bruda) Fall 2017 5 / 25 ABSTRACT DATA TYPES AND HASKELL ABSTRACT DATA TYPES AND HASKELL (CONT D) ADT Stack type Stack(Elm) Operators: empty : Stack push : Elm Stack Stack pop : Stack Stack top : Stack Elm isempty : Stack Bool Axioms: pop(push(e, S)) = S top(push(e, S)) = e isempty(empty) = true isempty(push(e, S)) = false Restrictions: pop(empty) top(empty) Haskell implementation -- Stack.hs module Stack (Stack, empty, push, pop, top, isempty) where data Stack a = Empty Push a (Stack a) deriving Show empty :: Stack a push :: a -> Stack a -> Stack a pop :: Stack a -> Stack a top :: Stack a -> a isempty :: Stack a -> Bool empty = Empty push a s = Push a s pop (Push e s) = s pop (Empty) = error "pop (empty)." top (Push e s) = e top (Empty) = error "top (empty)" isempty (Push e s) = False isempty Empty = True -- main.hs module Main where import Stack astack = push 0 (push 1 (push 2 empty)) Main> astack Push 0 (Push 1 (Push 2 Empty)) Main> :type astack astack :: Stack Integer Main> isempty astack False Main> pop astack Push 1 (Push 2 Empty) Main> let astack = push 0 empty in pop (pop astack) Program error: pop (empty) Main> CS 403: Types and Classes (S. D. Bruda) Fall 2017 6 / 25 CS 403: Types and Classes (S. D. Bruda) Fall 2017 7 / 25

SIMPLIFIED STACK IN HASKELL -- Stack.hs Main> astack module Stack where Push 0 (Push 1 (Push 2 Empty)) data Stack a = Empty Main> :r Push a (Stack a) Main> astack deriving Show Push 0 (Push 1 (Push 2 Empty)) Main> :type astack pop :: Stack a -> Stack a astack :: Stack Integer top :: Stack a -> a Main> isempty astack isempty :: Stack a -> Bool False Main> pop astack pop (Push e s) = s Push 1 (Push 2 Empty) pop (Empty) = error "pop (empty)." Main> let astack = Push 0 Empty top (Push e s) = e in pop (pop astack) top (Empty) = error "top (empty)" isempty (Push e s) = False Program error: pop (empty). isempty Empty = True -- main.hs module Main where import Stack CS 403: Types and Classes (S. D. Bruda) Fall 2017 8 / 25 astack = Push 0 (Push 1 (Push 2 Empty)) MORE SAMPLE ADTS: QUEUES type Queue(Elm) Operators: create : Queue enqueue : Elm Queue Queue dequeue : Queue Queue front : Queue Elm isempty : Queue Bool Axioms: front(enqueue(e, Q)) = if (isempty(q)) then e else front(q) dequeue(enqueue(e, Q)) = if (isempty(q)) then Q else enqueue(e, dequeue(q)) isempty(create) = true isempty(enqueue(e, Q)) = false Restrictions: dequeue(empty) front(empty) CS 403: Types and Classes (S. D. Bruda) Fall 2017 9 / 25 MORE SAMPLE ADTS: COMPLEX NUMBERS ISSUES WITH ABSTRACT DATA TYPES type Complex Operators: create : Real Real Complex real : Complex Real imaginary : Complex Real + : Complex Complex Complex : Complex Complex Complex... Axioms: real(create(r, i)) = r imaginary(create(r, i)) = i real(x + y) = real(x) + real(y) imaginary(x + y) = imaginary(x) + imaginary(y) real(x y) = real(x) real(y) imaginary(x) imaginary(y) imaginary(x y) = imaginary(x) real(y) + real(x) imaginary(y)... 1 Problem: Early binding of ADT interface to implementation Can have only one implementation of any given ADT Solution: dynamic binding stack.pop() selects the right function at run time depending on the specific implementation being used caller needs not know what implementation is used 2 Problem: Related ADTs are different ADTs Example: draw(), moveto(x,y) applicable to several graphical object ADTs (Square, Circle, Line, etc.) Cumbersome code for drawing a list of objects of different types case obj.tag is when t_square => Square.draw(obj.s); when t_circle => Circle.draw(obj.s); when t_line => Line.draw(obj.s); end case; Unmaintainable code (what if we later add the Rectangle ADT?) Solution: dynamic binding and subtyping automatically select the right draw function for object depending on its type CS 403: Types and Classes (S. D. Bruda) Fall 2017 10 / 25 CS 403: Types and Classes (S. D. Bruda) Fall 2017 11 / 25

ISSUES WITH ABSTRACT DATA TYPES (CONT D) 3 Problem: Why reimplement every ADT from scratch ADT are often related, with substantial overlapping in implementation Solution: inheritance Define an ADT as a variant of another ADT and specify only the differences: FilledSquare is like Square, except... Differential programming Dynamic binding + inheritance = object-oriented programming (OOP) OOP can be added to an existing language (C++ added over C, CLOS added over Lisp) Languages can support OOP but have the same structure and appearance of imperative languages (Eiffel, Java) OOP can be integral part of the language yet a subset of the complete language (type classes in Haskell) Pure OOP languages (Smalltalk) CS 403: Types and Classes (S. D. Bruda) Fall 2017 12 / 25 OO CONCEPTS A class is an ADT defining Format of object (instance variables = fields ) Operations on objects (methods = functions) Operations for creating new objects Objects are instances of a class A class may inherit all operations of another class, needs to override only those that it wants to change In the simplest case, a class inherits all of the entities of its parents A class that inherits is a derived class or subclass The class from which another class inherits is the parent class or superclass Subprograms that define operations on objects are called methods or member functions The entire collection of methods of an object is called its message protocol or message interface Messages have two parts - a method name and the destination object (often called receiver) Dynamic dispatch: call specifies operation name (not implementation), system selects appropriate implementation (function) at runtime CS 403: Types and Classes (S. D. Bruda) Fall 2017 13 / 25 OO EXAMPLE OO POLYMORPHISM class GraphicalObject { private int x, y; public void draw() {... } public void moveto(int x, int y) {... } } class line extends GraphicalObject { public void draw() {... } } GraphicalObject obj; Line line;... // initialization, etc. obj.draw(); line.draw(); line.moveto(0, 0); obj = line; line = obj; // dynamic dispatch // dynamic dispatch // inheritance // ok // not ok -- why? OO implements subtype polymorphism Static type of obj: GraphicalObject Dynamic type of obj: the static type of any subtype (actual type) A polymorphic variable references objects of the class as declared as well as objects of any of its descendants When a class hierarchy includes classes that override methods and such methods are called through a polymorphic variable, the binding to the correct method must be dynamic This polymorphism simplifies the addition of new methods More polymorphic mechanisms: A virtual or abstract method does not include a definition (only defines a protocol) A virtual or abstract class is one that includes at least one virtual method A virtual or abstract class cannot be instantiated Extreme virtual classes: interfaces (Java), pure virtual classes (C++) CS 403: Types and Classes (S. D. Bruda) Fall 2017 14 / 25 CS 403: Types and Classes (S. D. Bruda) Fall 2017 15 / 25

DESIGN ISSUES FOR OOPLS MULTIPLE INHERITANCE Single and multiple inheritance Allocation and deallocation of objects Dynamic and static binding Multiple levels of hiding The exclusivity of objects Are subclasses subtypes? Implementation and interface inheritance Multiple inheritance inherits from more than one class Creates problems Conflicting definitions (e.g., two or more superclasses define a print method) Repeated inheritance: same class inherited more than once If A is multiply inherited, should A s instance variables be repeated or not? It depends: not allowed, can choose per field (Eiffel), can choose per class (C++) Usefulness of multiple inheritance is not universally accepted Smalltalk, Beta: single inheritance only Java: single inheritance of classes but multiple inheritance of interfaces C++, Eiffel: multiple inheritance CS 403: Types and Classes (S. D. Bruda) Fall 2017 16 / 25 CS 403: Types and Classes (S. D. Bruda) Fall 2017 17 / 25 ALLOCATION AND DEALLOCATION OF OBJECTS DYNAMIC AND STATIC BINDING Where are objects allocated? Normal allocation for the language (stack + heap) or heap only If they all live on the heap then references to them are uniform Is deallocation explicit or implicit? Implicit: overhead (about 10%) and nondeterminism in running time Explicit: no overhead, deterministic Errors (as much as 40% of debugging time) Whose responsibility to deallocate? Should all binding of messages to methods be dynamic? If dynamic binding is not allowed then one loses the advantages of dynamic binding If all are then the code is inefficient C++ default is static but can use virtual functions to get dynamic binding Java and Eiffel default is dynamic binding (except for final methods in Java) CS 403: Types and Classes (S. D. Bruda) Fall 2017 18 / 25 CS 403: Types and Classes (S. D. Bruda) Fall 2017 19 / 25

EXCLUSSIVITY OF OBJECTS SUBCLASSES VERSUS SUBTYPES Everything is an object Advantage: elegance, purity, least surprise (everything works in the same way) Disadvantage: potential slow operations on simple objects (e.g., float) However all modern languages where everything is an object have implementation tricks that minimize the overhead Notable examples: C++, Haskell Include an imperative-style typing system for primitive types, but make everything else objects Advantage: fast operations on simple objects, relatively small typing system Disadvantage: confusion caused by the existence of two separate type systems Can also have class wrappers for primitive types (such as Integer, Float, Character, etc. in Java) Single mainstream example: Java Does an is-a relationship hold between a parent class object and an object of the subclass? If so, then a variable of the derived type could appear anywhere that a variable of the parent type is requested Characteristics of a subclass that guarantees it is a subtype: Can only add variables and methods, or redefine existing methods in a compatible way If the subclass hides some of the parent s variables and functions or modifies them in an incompatible way, then it does not create a subtype So what is a compatible way? By imposing some restrictions on the use of inheritance, the language can ensure substitutability Type extension: If the subclass is allowed to only extend the parent class, then substitutability is guaranteed (we have subtypes) The subclass does not modify and does not hide any method Overriding Methods: The redefined method(s) must have the same number of parameters must not enforce any more requirements on the input parameters may require stronger requirements on the result CS 403: Types and Classes (S. D. Bruda) Fall 2017 20 / 25 CS 403: Types and Classes (S. D. Bruda) Fall 2017 21 / 25 OVERLOADED METHODS IN JAVA CONTRAVARIANCE AND COVARIANCE Definitions Dessert 1 Feast(Dessert d, Scone s) {... } 2 Feast(Cake c, Dessert d) {... } Cake ChockolateCake Scone ButteredScone 3 Feast(ChockolateCake cc, Scone s) {... } Usage Feast(dessertref, sconeref) uses definition 1 Feast(chockolatecakeref, dessertref) uses definition 2 Feast(chockolatecakeref, butteredsconeref) uses definition 3 Contravariance for input parameters: the input parameters of the subclass method must be supertypes of the corresponding parameters of the overridden method Covariance on the result: the result of the redefined method must be a subtype of the result of the overridden method Very few languages enforce both rules C++, Java, Object Pascal and Modula-3 require exact identity of the two methods Eiffel and Ada require covariance of both the input and result parameters CS 403: Types and Classes (S. D. Bruda) Fall 2017 22 / 25 CS 403: Types and Classes (S. D. Bruda) Fall 2017 23 / 25

SUBCLASS VS. SUBTYPE Subclass = inheritance Code reuse Relationship between implementations Subtype = polymorphic code Organization of related concepts Relationship between types/interfaces Two different concepts but many languages unify them Notable exception: Java Class hierarchy (single inheritance, class B extends class A) Interface hierarchy (multiple subtyping) Abstract (deferred) classes = classes with incomplete implementation Cannot be instantiated Provide partial implementation with holes that are filled in by subclasses Pure virtual functions in C++ Polymorphism may require dynamic type checking Dynamic type checking is costly and delays error detection But, if overriding methods are restricted to having the same type then the type checking can be static (provided there is no change in visibility in subclasses) Example: C++ public inheritance CS 403: Types and Classes (S. D. Bruda) Fall 2017 24 / 25 JAVA INTERFACES class Printable { void print() { /* default implementation */ } } class Person extends Printable { void print() { /* overriding method */ } } interface Printable { void print() } class Person implements Printable { void print() { /* body of method */ }} With Printable as a class: Actual parameter must be a subclass Must inherit the implementation of Printable Specify both interface and implementation With Printable as an interface: Actual parameter must be an instance of a class that implements the Printable interface Specifies only interface Fewer constraints (more reusable) CS 403: Types and Classes (S. D. Bruda) Fall 2017 25 / 25