Fundamentals of Programming Languages. Data Types Lecture 07 sl. dr. ing. Ciprian-Bogdan Chirila

Similar documents
TYPES, VALUES AND DECLARATIONS

22c:111 Programming Language Concepts. Fall Types I

CSCI312 Principles of Programming Languages!

CS558 Programming Languages

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

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

Fundamentals of Programming Languages

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

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

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

Types. What is a type?

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

HANDLING NONLOCAL REFERENCES

CS558 Programming Languages

Data Types In Text: Ch C apter 6 1

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Imperative Programming Languages (IPL)

Fundamentals of Programming Languages. PL quality factors Lecture 01 sl. dr. ing. Ciprian-Bogdan Chirila

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

Structuring the Data. Structuring the Data

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Lecture 12: Data Types (and Some Leftover ML)

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

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

Informatica 3 Syntax and Semantics

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

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Basic Types & User Defined Types

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

Types II. Hwansoo Han

Compiler Construction

CSE 307: Principles of Programming Languages

Chapter 5 Names, Bindings, Type Checking, and Scopes

Principles of Programming Languages. Lecture Outline

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

G Programming Languages - Fall 2012

COS 140: Foundations of Computer Science

Chapter 6. Data Types ISBN

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

CS349/SE382 A1 C Programming Tutorial

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Chapter 6. Data Types

Type systems. Static typing

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Chapter 6. Data Types ISBN

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

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

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

BASIC ELEMENTS OF A COMPUTER PROGRAM

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Example: Haskell algebraic data types (1)

Compiler Construction

Programmiersprachen (Programming Languages)

Topic 7: Algebraic Data Types

Programming Languages

CSC2/454 Programming Languages Design and Implementation Records and Arrays

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

NOTE: Answer ANY FOUR of the following 6 sections:

Types and Programming Languages. Lecture 5. Extensions of simple types

LECTURE 18. Control Flow

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

COS 140: Foundations of Computer Science

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Lecture Notes on Programming Languages

CSE 452: Programming Languages. Where are we? Types: Intuitive Perspective. Data Types. High-level Programming Languages.

Programming Languages

Types and Type Inference

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

Programming Languages

Announcements/Follow-ups

Lexical Considerations

Compiler Construction

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

MIDTERM EXAMINATION - CS130 - Spring 2005

Chapter 5. Names, Bindings, and Scopes

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

Imperative Programming

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Programming Languages, Summary CSC419; Odelia Schwartz

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Lexical Considerations

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

Operational Semantics. One-Slide Summary. Lecture Outline

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Final-Term Papers Solved MCQS with Reference

Pascal: operations on basic data structures (selection) (I) 2.3 Accessing and manipulating data

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Motivation for typed languages

index.pdf January 21,

RTL Reference 1. JVM. 2. Lexical Conventions

CS558 Programming Languages

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

CS 314 Principles of Programming Languages. Lecture 13

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

PROGRAMMING IN C++ COURSE CONTENT

CSC 533: Organization of Programming Languages. Spring 2005

Inductive Data Types

Transcription:

Fundamentals of Programming Languages Data Types Lecture 07 sl. dr. ing. Ciprian-Bogdan Chirila

Predefined types Programmer defined types Scalar types Structured data types Cartesian product Finite projection Sequence Recurrence Variable reunions Sets Pointer type Type Compatibility Lecture outline

Data types A set of objects and A set of operations to Create Destroy Modify Predefined types a certain set of objects specified at language definition Unitary construction of objects in advanced PLs structure operations

Predefined Types The base of the typing system of a language Reflects the system functioning at the hardware level Values and operations related to low level data and machine operations

Predefined Types Numerical base types int in Algol 68 integer in Pascal real in Algol 68 and Pascal float in Ada short int, long int, double Mathematical operations +,-,*,/ For integers and reals Polymorphic operators - overloaded

Predefined Types boolean enumeration type with values true false bool in Algol 68 boolean in Pascal, Java, Ada char in Algol 68, Java, Pascal character in Ada ASCII EBCDIC Extended Binary Coded Decimal Interchange Code

Programmer defined types The most powerful feature of a typing system is to create new types Named type tab=array[1..10] of integer; Anonymous var t:array[1..10] of integer;

Scalar types Scalar type objects are simple constants which can not be decomposed further Integer, real, character, boolean are scalar types The programmer can define its own scalar types

Enumeration type The user specifies in a list the type values type days=(sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); Started in Pascal Present in the majority of the PLs

Other scalar types Important from the portability point of view in Ada type eps is digits 10; a floating point number with a minimum number of 10 significant decimals The precision will be preserved independently of the platform

Subdomains In Pascal type working_day=monday..friday; small_caps= a.. z ; index=0..90; In Ada type eps_1 is new eps range -1.0.. 1.0;

Structured data types PLs offer mechanisms for description and manipulation of data structures containing scalars other structures Structuring mechanism features allowing to build structures starting from its components Selection mechanism features allowing access to a structure component

Cartesian product Structured objects Composed out of a fixed number of components Components are of different types The type of the structured objects is the Cartesian product of the sets corresponding to components If the types of the components are represented by sets C1, C2, C3,, Cn Each element of structured type will be: T=C1 x C2 x x Cn

Cartesian product Named also Articles Structures In Pascal and Ada - record In Algol 68 and C structure To describe the type of each component To select a component means to specify the object and the name of the selected field

Cartesian product example Ada type complex is record re,im:real; end record; ----------------------------- c:complex; ----------------------------- c.re:=1; c.im:=0; ----------------------------- c:=(1,0);

Finite projection Is a function defined on IT set with values on ET set IT index type ET element type var a:array[1..100] of char; It is a projection of {1,2,3,,100} set on the characters set The array components called elements are selected through the indexing mechanism To the array name we add an index value to select a certain element

Finite projection a[k] selects the k index element from array a can be regarded as a application of function a with argument k resulting the value of the element In Algol, Ada Selection can be made on a slice not just a single element a[10..19]=(0,1,2,3,4,5,6,7,8,9);

The key moment of binding the set of indexes Fixed at compile time Writing the code which establishes the index set Can not be modified during program execution It is the case for Fortran, C, Pascal Fixed at run time In the moment of array object creation The size can be unknown at compile time Can depend on program variables It is the case for Algol 60, Basic or Ada In languages with dynamic memory allocation like C pointers are used for dynamic arrays access

The key moment of binding the set of indexes Flexible at run time The index set can be modified The size of the array can be modified It is the case for Snobol4 and Algol 68

Sequence Is a structure formed out of a random number of components of the same type Anytime a component can be added Virtually unlimited In PLs Strings of characters Sequential file

Sequence For strings In PL/I, Ada, Basic, Pascal When declaring a string the maximum length must be known Operations PL dependent Catenation First character selection Last character selection Substring selection etc

Recursion A type T is recursive if one of its components is of type T Typical examples are Lists Trees The objects can have arbitrary shapes and sizes

Recursion in pseudocode type node=record info : info_type; left, right : node; end;

Recursion in practice pointers must be used a recursive object of type T must have a reference of a T object not an object itself C, Pascal, Ada, Algol 68 In Lisp lists and trees does not need pointers type node=record info : info_type; left, right : ^node; end;

Variable reunions Allow specifying structures which can have several alternatives The set of all possible structures represents the reunion of alternative sets

Variable reunions In C union { float radius; float rectangle_sides[2]; float triangle_sides[3]; } shape;

Variable reunions at one time shape variable can have only float radius or two float array or three float array in an article all fields coexist in a union there will be only one of the alternative fields

Variable reunions More evolved unions are in Pascal and Ada The union is a part of an article with variants type figure=(circle, triangle, rectangle); shape=record length, area:real; case shape:figure of circle: (radius:real); rectangle: (rectangle_sides:array[1..2] of real); triangle: (triangle_sides:array[1..3] of real); end

Variable reunions Are dangerous The correct variant must be used All responsibility is left on programmers shoulders (C) No compile time checking possible No runtime checking possible Ada and Pascal cases will be detailed later

Sets T is the base type Variables of set(t) can have as value any subset generated by values of T including void set Operations Reunion Intersection Difference Inclusion tests belonging tests

Sets Pascal Has a set type When no such mechanism is present Can be implemented by the programmer by Boolean arrays Bit arrays Lists Trees

The pointer type A pointer is a reference to an object The usual mean to implement recursive data structures In C the only way of transmitting parameters by address

Problems with pointers Type compatibility violations Pseudonyms False references

Type compatibility violations In PL/I a pointer variable can refer any object At compile time is impossible to know the object type and to do appropriate type checking runtime checking is possible but they are expensive In Pascal, Ada pointers have assigned the object types they may refer In C we have the void* generic pointers

Pseudonyms The very same object is referred by several names Their presence in the code affects its readability var a,b:^t; a:=new(t); b:=a; a and b are pseudonyms

False references When a pointer refers an object no longer alive Its access is an error var a,b:^t; a:=new(t); b:=a; dispose(a); b is a false reference even a is set to nil

In C int *p; void f() { int x; p=&x; } f(); False references

Type compatibility T1 and T2 are compatible types if A value of type T1 can be assigned to a variable of type T2 (and vice versa) A parameter of type T1 corresponds to an actual of type T2 (and vice versa)

Example type var t=array[1..100] of integer; t1=array[1..100] of integer; t2=t1; a,b:array[1..100] of integer; c:t; d:t; e,f:t1; g:t2;

Theoretical type compatibilities Name equivalence Structural equivalence

Name equivalence when 2 variables declared together or using the same name for the type In the example a and b are compatible c and d are compatible e and f are compatible a or b with c or d are not compatible

Structural equivalence two variables have compatible types if they have the same structure as type checking we will replace the name of the type by its definition recursive process ends when all user defined type are replaced Two types are compatible if they have the same description In our example a, b, c, d, e, f, g are all compatible

Comparison Structural equivalence simplicity of the implementation Name equivalence Complex operations in order to determine type compatibility Allows refined abstractions type price=integer; students_no=integer; cost:price; effective:student_no;

Comparison Variables cost and effective structurally equivalent assigning values from cost to effective or viceversa is a semantic error Structural equivalence Algol 68 C - structure and union different types even they have identical structures Name equivalence Ada Pascal - equivalence not specified, implementation dependent