Francesco Nidito. Programmazione Avanzata AA 2007/08

Similar documents
Lecture 12: Data Types (and Some Leftover ML)

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

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

Midterm CSE 131B Spring 2005

G Programming Languages - Fall 2012

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08

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

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

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

Introduction to C++ with content from

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

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Industrial Programming

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Chapter 8 :: Composite Types

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Types. What is a type?

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Type Conversion. and. Statements

Short Notes of CS201

Programming Languages

CS201 - Introduction to Programming Glossary By

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

12 CREATING NEW TYPES

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Programming Languages

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

first class citizens

Topic 9: Type Checking

Topic 9: Type Checking

Inheritance, Polymorphism and the Object Memory Model

Types II. Hwansoo Han

CSE 431S Type Checking. Washington University Spring 2013

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

Lecture Notes on Programming Languages

Structure of Programming Languages Lecture 10

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

September 10,

22c:111 Programming Language Concepts. Fall Types I

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Structuring the Data. Structuring the Data

Introduction to Programming Using Java (98-388)

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

C-LANGUAGE CURRICULAM

CS201 Some Important Definitions

TYPES, VALUES AND DECLARATIONS

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

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

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Chapter 6. Data Types ISBN

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

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

Variation of Pointers

NOTE: Answer ANY FOUR of the following 6 sections:

C++ (Non for C Programmer) (BT307) 40 Hours

PROGRAMMING IN C++ COURSE CONTENT

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

Pointers and References

Lecture 3. The syntax for accessing a struct member is

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

IA010: Principles of Programming Languages

Fundamentals of Programming Languages

Enumerated Types. Mr. Dave Clausen La Cañada High School

QUIZ. What is wrong with this code that uses default arguments?

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

Type Bindings. Static Type Binding

CS321 Languages and Compiler Design I. Fall 2013 Week 8: Types Andrew Tolmach Portland State University

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

COMPSCI 210 Part II Data Structure

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

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

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

Polymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1

Basic Types & User Defined Types

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Chapter 6. Data Types ISBN

CSCI-GA Scripting Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Final CSE 131B Spring 2004

CS349/SE382 A1 C Programming Tutorial

EL2310 Scientific Programming

COMP 524 Spring 2018 Midterm Thursday, March 1

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

Object-Oriented Principles and Practice / C++

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

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

CSc 520. Principles of Programming Languages 25: Types Introduction

Lecture 3: C Programm

Chapter 6. Data Types

CS 314 Principles of Programming Languages

Transcription:

Francesco Nidito in the Programmazione Avanzata AA 2007/08

Outline 1 2 3 in the in the 4 Reference: Micheal L. Scott, Programming Languages Pragmatics, Chapter 7

What is a type? in the

What is a type? Hardware can manage bits in different ways has no type, but provides operations on numbers and pointers (bit sequences) Software creates the abstraction of types in the

What is a type? Hardware can manage bits in different ways has no type, but provides operations on numbers and pointers (bit sequences) Software creates the abstraction of types Type defines the memory layout of data defines a set of operations that can be performed on value belonging to that type in the

What is a type system? in the

What is a type system? A type system consists of a mechanism for defining types and associating them to language structures a set of rules for: type equivalence (Type A = Type B?) in the

What is a type system? A type system consists of a mechanism for defining types and associating them to language structures a set of rules for: type equivalence (Type A = Type B?) type compatibility (Type A Context i?) in the

What is a type system? A type system consists of a mechanism for defining types and associating them to language structures a set of rules for: type equivalence (Type A = Type B?) type compatibility (Type A Context i?) type inference (x Type A?) in the

Type system rules (Example) type equivalence (Type A = Type B?) e.g. Is it safe to cast an integer to a char? integer x := 26; char a := (char)x; type compatibility (Type A Context i?) e.g. Can I add a string and a real? string s := foo ; real x := s + 5.0; type inference (x Type A?) e.g. For which types of x is f defined? let f x = x + x;; in the

What are type systems good for? in the

What are type systems good for? Detecting errors Enforcing abstraction Documentation Efficiency in the

What is type checking? in the

What is type checking? is the process of ensuring that a program obeys the language s type compatibility rules in the

Strong vs. weak typing Strong typing Values of one type cannot be assigned to variables of another type. Enables incredibly extensive static compiler checks. in the Weak typing Values of one type can be assigned to variables of another type using implicit value conversions.

Strong vs. weak typing (Example) Strong typing check returns an error type fruitsalad: integer; type apple: integer; type pear: integer; apple a := 5; pear p := 3 fruitsalad f := a + p; in the

Strong vs. weak typing (Example) Strong typing check returns an error type fruitsalad: integer; type apple: integer; type pear: integer; apple a := 5; pear p := 3 fruitsalad f := a + p; Weak typing check goes on type fruitsalad: integer; type apple: integer; type pear: integer; apple a := 5; pear p := 3 fruitsalad f := a + p; //fruitsalad = 8 in the

Dynamic vs. static typing Dynamic typing Environment infers the type of a variable/expression from its use. It can happen both at runtime and compile-time. Static typing Programmer must indicate the type of a variable/expression writing it in the code. It s checked at compile-time. in the

Dynamic vs. static typing Dynamic typing Environment infers the type of a variable/expression from its use. It can happen both at runtime and compile-time. Static typing Programmer must indicate the type of a variable/expression writing it in the code. It s checked at compile-time. in the Obviously, in real world they can be mixed!

Dynamic vs. static typing (Example) Dynamic typing: s := foo ; //s is string n := sqrt(42); //n is real Static Typing: string s := foo ; //s is string real n := sqrt(42); //n is real in the

Game of types Non-Typed Static Typed Dynamic Strong in the Weak

in programming languages boolean int, long, float, double (signed/unsigned) char (1 byte, 2 bytes) Enumerations Subrange (n 1..n 2 ) Pointers Composite types struct union array in the

What is type cast? in the

What is type cast? Type cast operation builds from an expression with type Type A a new value of type Type B Consider the following definitions: int add(int i, int j); int add2(int i, double j); in the

What is type cast? Type cast operation builds from an expression with type Type A a new value of type Type B Consider the following definitions: int add(int i, int j); int add2(int i, double j); Ad the following calls: add(2, 3); //Exact add(2, (int)3.0); //Explicit cast add2(2, 3); //Implicit cast in the

Memory layout In 32 bits architectures types require from 1 to 8 bytes Composite types (e.g. structures) are represented chaining constituent types together in the

Memory layout In 32 bits architectures types require from 1 to 8 bytes Composite types (e.g. structures) are represented chaining constituent types together For performance reasons compilers employ padding to align fields to 4 (or 8) bytes addresses in the

Memory layout (Example) struct element { char name[2]; int atomicnumber; float atomicweight; char metallic; }; name free free atomicnumber atomicweight metallic free free free in the

Problems with memory layout C requires that fields of a struct must be placed in the same order of the declaration (essential with pointers!) Not all languages behaves like this: for instance ML doesn t specify any order If the compiler can reorganize fields, holes are minimized: for instance packing name and metallic saves 4 bytes in the

Union Union types allow sharing the same memory area among different types The size of the value is the maximum of the size of the constituents in the union u { struct element e; int number; }; name free free atomicnumber atomicweight metallic free free free number free free free free free free free free free free free free

Enumerate User defined types to increase expressivity Values of an enumerate are ordered and can be used as indexes of arrays or collections in the enum weekday {sun, mon, tue, wed, thu, fri, sat };

Array Array are positional collections of homogeneous data From an abstract point of view an array is a mapping from an index type to an element type Array s indexes in the int char[26]; // C/C++ var frequency : array[ a.. z ] of integer; //Pascal

Pointers Not a real type, it s a label A pointer variable is a variable whose value is a reference to some object in the

Pointers Not a real type, it s a label A pointer variable is a variable whose value is a reference to some object A pointer is not an address of memory. It is an high level reference in the

Pointers Not a real type, it s a label A pointer variable is a variable whose value is a reference to some object A pointer is not an address of memory. It is an high level reference One pointer can refer to an already existing object in the

Pointers Not a real type, it s a label A pointer variable is a variable whose value is a reference to some object A pointer is not an address of memory. It is an high level reference One pointer can refer to an already existing object A pointer can be created allocating memory for it A pointer that was created must be destroyed in the

Problems with pointers: memory leak A created pointer must be destroyed to clean memory A pointer variable when out of scope is lost in the

Problems with pointers: memory leak A created pointer must be destroyed to clean memory A pointer variable when out of scope is lost...but the pointed object is still in memory The pointed object cannot be accessed but uses memory in the { } foo pf = new foo(); pf foo pf foo

Problems with pointers: dangling reference Suppose two pointers pointing to the same object When one of the two pointers is destroyed the object is removed from memory in the

Problems with pointers: dangling reference Suppose two pointers pointing to the same object When one of the two pointers is destroyed the object is removed from memory...but the second pointer is a live pointer that no longer points to a valid object The access to the cleaned object can rise errors in the foo pf1 := new foo(); foo pf2 := pf1; pf 1 pf 2 foo delete(pf1); pf 1 pf 2 foo

Abstract data types According to the abstract view of types, a type is an interface An ADT is a set of values and operations allowed on it Programming languages have mechanisms to define ADT in the

Abstract data types (Example) struct node { int val; struct node *next; }; struct node* next(struct node* l) { return l->next; } struct node* initnode(struct node* l, int v) { l->val = v; l->next = NULL; return l; } void append(struct node* l, int v) { struct node p = l; while (p->next) p = p->next; p->next = initnode((struct node)malloc(sizeof(struct node)),v); } in the

Abstract data types limits C doesn t provide any mechanism to hide the structure of data types A program can access next field without using the next function in the

Abstract data types limits C doesn t provide any mechanism to hide the structure of data types A program can access next field without using the next function To hide data and to preserve abstraction we must use a Class in the

Class type in the

Class type Class is a type constructor like struct or array A class combines Data (like struts) Methods (operations on the data) A class has two special operations to provide Initialization Finalization in the

Class type (Example) class Node { int val; Node m next; Node(int v) { val := v; } Node next() { return m next; } void append(int v) { Node n := this; while (n.m next!= null) n := n.m next; n.m next := new Node(v); } } in the