Derived and abstract data types. TDT4205 Lecture 15

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

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Introduction to Programming Using Java (98-388)

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

Project Compiler. CS031 TA Help Session November 28, 2011

Programming Languages and Techniques (CIS120)

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

G Programming Languages - Fall 2012

What are the characteristics of Object Oriented programming language?

Programming Languages and Techniques (CIS120)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Chapter 1 Getting Started

CMSC 330: Organization of Programming Languages

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

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

Object typing and subtypes

Inheritance. Quick Review of Last Lecture. November 12, Passing Arguments. Passing Arguments. Variable Assignment Revisited

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

COS 140: Foundations of Computer Science

Semantic Analysis and Type Checking

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

CS11 Intro C++ Spring 2018 Lecture 1

Inheritance, Polymorphism and the Object Memory Model

Programming, numerics and optimization

CS11 Introduction to C++ Fall Lecture 1

Compiler construction 2009

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

Java and C CSE 351 Spring

2. The object-oriented paradigm!

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

First IS-A Relationship: Inheritance

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

More Relationships Between Classes

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

CS18000: Programming I

COMP 110/L Lecture 20. Kyle Dewey

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Operational Semantics. One-Slide Summary. Lecture Outline

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

type conversion polymorphism (intro only) Class class

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

CA Compiler Construction

Types. What is a type?

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

CS11 Intro C++ Spring 2018 Lecture 3

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

1 Shyam sir JAVA Notes

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Intermediate Code Generation

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

The Java Programming Language

What is an algorithm?

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

Winter Compiler Construction Who. Mailing list and forum

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

2. The object-oriented paradigm

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

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Modern Programming Languages. Lecture Java Programming Language. An Introduction

COS 140: Foundations of Computer Science

CS1004: Intro to CS in Java, Spring 2005

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

Inheritance and Polymorphism

CS121/IS223. Object Reference Variables. Dr Olly Gotel

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

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CSE 401/M501 Compilers

Object-Oriented ProgrammingInheritance & Polymorphism

Abstract Data Types (ADT) and C++ Classes

Absolute C++ Walter Savitch

CS558 Programming Languages

LECTURE 3. Compiler Phases

Java Object Oriented Design. CSC207 Fall 2014

Lecture #23: Conversion and Type Inference

Fundamentals of Programming Languages

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Chapter 4 Defining Classes I

Chapter 6 Introduction to Defining Classes

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

Design Patterns: State, Bridge, Visitor

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

PROGRAMMING LANGUAGE 2

CS260 Intro to Java & Android 03.Java Language Basics

6. Pointers, Structs, and Arrays. 1. Juli 2011

Chapter 5 Object-Oriented Programming

CS 2110 AEW SIGN UP FOR 1 CREDIT S/U COURSE!!!

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

Operational Semantics of Cool

Transcription:

1 Derived and abstract data types TDT4205 Lecture 15

2 Where we were We ve looked at static semantics for primitive types and how it relates to type checking We ve hinted at derived types using a multidimensional array declaration as example We ll round of with a quick look at more complicated derived types, and what they do to the type system

3 Type expressions We ve touched upon this int[2][3] consists of a basic type int, and ( array-of-2 (array-of-3) ) Types can be constructed from basic types We ve also mentioned that types can be converted into each other according to a hierarchy (short + char) converts into their least upper bound (int), we can write it that as a function, lub(short,char) short byte double float long int char

4 Arrays can be many things Unbounded Java: public static void main ( String args[] ) Fixed size Ranges C: char mystring[256]; Ada: (Type of mystring contains its size) array[2.. 5] of integer (Type contains offset for indexing) Multidimensional Fortran: REAL, dimension(2,3) :: A No idea how many entries here (Type contains shape, to check that B is (3,2) and C is (2,2) if you write C=matmul(A,B))

5 Records Records are collections of names and types { id1:t1, id2:t2,, idn:tn} C calls them struct Objects, at their simplest, are just records extended with references (pointers) to the functions which manipulate their contents...and some calling syntax that doesn t let you invoke its methods without passing an instance to manipulate...

6 Type constructors When the program can define types, the compiler must be able to construct representations of them, to Put in symbol tables Consult for conversions Etc. Generate a structure representing the type at its declaration Bind names to these structures when variables are determined to be of the matching type

7 Object types The type of a record is the cartesian product of all its component types Looking at objects as glorified records, their types are the same way, just add method signatures Class Point { } float x, y; float getx() { return x; } float gety() { return y; } becomes { x: float, y: float, getx : fun(point float), gety : fun(point float) }

8 Wait a minute... { x: float, y: float, getx : fun(point float), gety : fun(point float) } These are not in the argument lists of float getx() or float gety() Implementation generates one static lump of code for taking x-s out of Points The reason it acts differently with different instances is that the instance is passed as a hidden argument: Point p = new Point(3,2); p.getx() Point.getX ( p ) Inside the method body, the hidden argument can go by the name this

9 Objects imply more Class ColoredPoint extends Point { } int color; int getcolor() { return color; } seen as an extended record works out to { x: float, y: float, color: int, getx : fun(point float), gety : fun(point float), getcolor : fun(coloredpoint int) } That alone doesn t tell us what to do here: ColoredPoint cp = new ColoredPoint ( 3.0, 4.0, red ); Point p; p = cp;

10 Things to support Inheritance, subclassing, polymorphism, overloading, abstract classes, interfaces, etc. create a hierarchy of derived types Type-checking of comparisons, method calls, assignments, etc. must take this programmer-defined hierarchy into account Name Type p Point cp ColoredPoint Symbol table { float, float, fun(point float), fun(point float) } Subclass relation (ColoredPoint <: Point) Point U { int, fun(coloredpoint int) }

11 Assignment, revisited We had id : T - E : T id : T - id = E : T Adding subclasses, it becomes id : T - E : S where S <: T id : T - id = E : T

12 Impact on static checking The type of an object reference isn t known at compile time That s what a class hierarchy is for, different types can step in for each other Overridden methods appear with multiple implementations and sligthly different call signatures Resolution requires some policy on how much dynamic information to account for

13 An example with Java Class Animal { String voice; void speak() { System.out.println ( voice ); } } gives type { voice : String, speak : fun(animal void) } Class Cat extends Animal { String voice = meow ; } gives type { voice : String, speak : fun(animal void) }

14 Same field name, inherited method Fields are statically resolved, not overridden Method calls are dynamically dispatched using the run-time type of the instance, and parameter list + return type Class Animal { } String voice; void speak() { println ( voice ); } Class Cat extends Animal { } String voice = meow ; Statically resolved (new Cat()).speak(); // this prints null // Looking at the Cat type redirects the method call to Animal.speak

15 Overridden method Class Animal { String voice; void speak() { println ( voice ); } } Class Cat extends Animal { String voice = meow ; void speak() { println ( voice ); } } Statically resolved (new Cat()).speak(); // this prints meow // At run time, look at instance and detect that speak : fun( void) means // fun ( Cat void ) instead of fun(animal void)

16 Objects as records One thing the compiler must deal with is how to lay out instances in memory The types of fields and methods indicate what instances must look like in a flat memory space Heap Point a x=2 y=3 *getx *gety Text segment Point_getX: return this->x Point_getY: return this->y Point b x=6 y=7 *getx *gety This carries through into the executable program

17 Objects as abstract data types Checking, say, that a.getx() == b.gety() is correctly typed requires a structure that shows how both have methods like that, and that they return comparable numbers This may be discarded after compilation (well, depending on the language) Point a {x,y} getx() gety() ColoredPoint b {x,y} getx() gety() color=128 getcolor() Point <: ColoredPoint

18 Summary of the week (in reverse) Representation of types Objects make the compiler build a class hierarchy Derived types make the compiler construct user-defined type descriptors Basic types can be hard-coded in a predefined hierarchy Static type checking Static type checking goes through all expressions to determine that the syntax attaches them to meaningful types With an attribute grammar, it can be done during parsing (Static semantics are a subset of natural semantics) You just remove the states

19 That s a lot to take in M kay, I m almost finished prattling on about types now All that remains is to take one short peek at how a simple single inheritance scheme and dynamic call dispatch can map into low-level code After we ve looked closer at low-level code

20 Is all this really necessary? Erm I know we ve been skipping along the borderline of the syllabus here. If you know your L and S attributions and can suggest how to represent simple things in symbol tables, it ll be just fine

21 What was it for, then? Most practical languages define a far richer notion of types than just skipping around the syntax tree and seeing that there are ints and bools in the right places Those are what people actually use Starting from a vague guess at how their compilers and run-time systems work, it s easier to make improved guesses over time The ability to second-guess compilers and run-time systems is a coveted skill among software developers

22 Next time Three-address code Which is an abstract cousin of assembly programming