1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Names and Bindings.
1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values.
Chapter 7:: Data Types Programming Language Pragmatics
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
1 Chapter 6: Data types A data type defines a collection of data objects and a set of predefined operations on those objects Definition: A descriptor is.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 355 – Programming Languages
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
College of Computer Science and Engineering
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
1 Data Types In Text: Chapter 5. 2 Chapter 5: Data Types Outline What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.
ISBN 0-321— Chapter 6 Structured Data Types Arrays Associated Arrays Records Unions.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
CS 363 Comparative Programming Languages Data Types.
1 CS Programming Languages Class 08 September 19, 2000.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Data Types Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer Types.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
Names, Scope, and Bindings Programming Languages and Paradigms.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Concepts of Programming Languages
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Type Checking, and Scopes
Data Types In Text: Chapter 6.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Chapter 6 Data Types.
Presentation transcript:

1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values

2 Why data types? Data abstraction –Programming style – incorrect design decisions show up at translation time –Modifiability – enhance readability Type checking (semantic analysis) can be done at compile time - the process a translator goes through to determine whether the type information is consistent Compiler uses type information to allocate space for variables Translation efficiency: Type conversion (coercion) can be done at compile time

3 Overview cont… Declarations –Explicit type information Type declarations –Give new names to types in type declararion Type checking –Type Inference Rules for determining the types of constructs from available type information –Type equivalence determines if two types are the same Type system –Type construction methods + type inference rules + type equivalence algorithm

4 Simple Types Predefined types Enumerated types Pascal type fruit = (apple, orange, banana); C/C++ enum fruit { apple, orange, banana };

5 Simple Types Subrange types Pascal type byte = ; minors = 0..19; teens = ; ADA subtype teens is INTEGER range ;

6 Data Aggregates and Type Constructors Aggregate (compound) objects and types are constructed from simple types Recursive – can also construct aggregate objects and types from aggregate types Predefined – records, arrays, strings……

7 Constructors - Cartesian Product U X V = { (u, v) | u is in U and v is in V} Projection functions –p 1 : U X V -> U and P 2 : U X V -> V Pascal family of languages - record type polygon = record edgeCt: integer; edgeLen: real end; var a : polygon; Cartesian product type INTEGER X REAL Projections a.edgeCt a.edgeLen

8 Constructors – Mapping (arrays) The array constructor defines mappings as data aggregates Mapping from an array index to the value stored in that position in the array The domain is the values of the index The range is the values stored in the array

9 Constructors Mapping (arrays) C/C++ typedef int little_people_num[3]; little_people_num gollum_city = {0, 0, 0} gollum_city[3] = 5 typedef int matrix[10][20];

10 Arrays Design Issues: 1. What types are legal for subscripts? 2. Are subscripting expressions in element references range checked? 3. When are subscript ranges bound? 4. When does allocation take place? 5. What is the maximum number of subscripts? 6. Can array objects be initialized? 7. Are any kind of slices allowed?

11 Arrays Indexing is a mapping from indices to elements map(array_name, index_value_list)  an element

12 Arrays Subscript Types: –FORTRAN, C - integer only –Pascal - any ordinal type (integer, boolean, char, enum) –Ada - integer or enum (includes boolean and char) –Java - integer types only

13 Arrays Categories of arrays (based on subscript binding and binding to storage) 1. Static - range of subscripts and storage bindings are static e.g. FORTRAN 77, some arrays in Ada –Advantage: execution efficiency (no allocation or deallocation)

14 Arrays 2. Fixed stack dynamic - range of subscripts is statically bound, but storage is bound at elaboration time (at function call time) –e.g. Most Java locals, and C locals that are not static –Advantage: space efficiency

15 Arrays 3. Stack-dynamic - range and storage are dynamic (decided at run time), but fixed after initial creation on for the variable’s lifetime –e.g. Ada declare blocks declare STUFF : array (1..N) of FLOAT; begin... end; –Advantage: flexibility - size need not be known until the array is about to be used

16 Arrays 4. Heap-dynamic – stored on heap and sizes decided at run time. e.g. (FORTRAN 90) INTEGER, ALLOCATABLE, ARRAY (:,:) :: MAT (Declares MAT to be a dynamic 2-dim array) ALLOCATE (MAT (10,NUMBER_OF_COLS)) (Allocates MAT to have 10 rows and NUMBER_OF_COLS columns) DEALLOCATE MAT (Deallocates MAT ’s storage)

17 Arrays 4. Heap-dynamic (continued) –Truly dynamic: In APL, Perl, and JavaScript, arrays grow and shrink as needed –Fixed dynamic: Java, once you declare the size, it doesn’t change

18 Arrays Number of subscripts –FORTRAN I allowed up to three –FORTRAN 77 allows up to seven –Others - no limit Array Initialization –Usually just a list of values that are put in the array in the order in which the array elements are stored in memory

19 Arrays Array Operations 1. APL - many, see book (p ) 2. Ada –Assignment; RHS can be an aggregate constant or an array name –Catenation; for all single-dimensioned arrays –Relational operators (= and /= only) 3. FORTRAN 90 –Intrinsics (subprograms) for a wide variety of array operations (e.g., matrix multiplication, vector dot product)

20 Arrays Slices –A slice is some substructure of an array; nothing more than a referencing mechanism –Slices are only useful in languages that have array operations

21 Arrays Slice Examples: 1. FORTRAN 90 INTEGER MAT (1:4, 1:4) MAT(1:4, 1) - the first column MAT(2, 1:4) - the second row

22 Example Slices in FORTRAN 90

23 Arrays Implementation of Arrays –Access function maps subscript expressions to an address in the array –Row major (by rows) or column major order (by columns)

24 Locating an Element

25 Compile-Time Descriptors Single-dimensioned array Multi-dimensional array

26 Accessing Formulas – 1D Address(A[i]) = BegAdd + (i-lb)*size + VirtualOrigin +i*size lb: lower bound size: number of bytes of one element Virtual origin allows us to do math once, so don’t have to repeat each time. You must check for valid subscript before you use this formula, as obviously, it doesn’t care what subscript you use.

27 Accessing Formulas Multiple Dimensions ub i : upper bound in i th dimension lb i : lower bound in i th dimension length i = ub i –lb i +1 In row-major order Address(A[i,j]) = begAdd + size((i-lb i )*length j + j-lb j ) = VO + i*mult i + j*mult j

28 Address(A[i,j]) = begAdd + size((i-lb i )*length j + j-lb j ) = VO + i*mult i + j*mult j = i+20j For Example: array of floats A[0..6, 3..7] beginning at location 100 begAdd = 100 size = 4 (if floats take 4 bytes) lb i = 0 ub i = 6 length i = 7 lb j = 3 ub j = 7 length j = 5 VO = *(-3)*5 = 40 mult i = 28 mult j = 20 VO40 lb i 0 ub i 6 mult i 28 lb j 3 ub j 7 mult j 20 repeated for each dimension

29 Accessing Formulas Multiple Dimensions In column-major order Address(A[i,j]) = begAdd + size((i-lb i ) + (j-lb j )*length i ) In 3D in row major: Addr(A[I,j,k]) = begAdd + size*((i-lb i )*length j *length k ) + (j-lb j )length k + k-lb k )

30 Accessing Formulas Slices Suppose we want only the second row of our previous example. We would need array descriptor to look like a normal 1D array (as when you pass the slice, the receiving function can’t be expected to treat it any differently)

31 Address(A[i,j]) = begAdd + size((i-lb i )*length j + j-lb j ) = VO + i*mult i + j*mult j = i+20j For Example: array of floats A[0..6, 3..7] beginning at location 100 If we want only the second row, it is like we have hardcoded the j=2 in the accessing formula, A[0..6,2] The accessing formula is simple Just replace j with 2, adjust the VO, and remove the ub,lb, and length associated with j so i+20j = 80+28i and the table is changed accordingly (and looks just like the descriptor for a regular 1D array) VO80 lb i 0 ub i 6 mult i 28

32 Constructors Union Cartesian products – conjunction of fields Union – disjunction of fields Discriminated or undiscriminated

33 Pascal Variant Record – discriminated union type address_range = 0..maxint; address_type = (absolute, offset); safe_address = record case kind: address_type of absolute: (abs_addr:address_range); offset: (off_addr: integer); end

34 References A reference is the address of an object under the control of the system – which cannot be used as a value or operated on C++ is perhaps the only language where pointers and references exist together. References in C++ are constant pointers that are dereferenced everytime they are used.

35 Constructors Pointer and Recursive Types Some languages (Pascal, Ada) require pointers to be typed PL/1 treated pointers as untyped data objects What is the significance of this for a type checker? C pointers are typed but C allows arithmetic operations on them unlike Pascal and Ada

36 Type Equivalence When are two types the same Structural equivalence Declaration equivalence Name equivalence

37 Structural Equivalence Two types are the same if they have the same structure i.e. they are constructed in exactly the same way using the same type constructors from the same simple types May look alike even when we wanted them to be treated as different.

38 Structural Type Equivalence typedef int anarray[10]; typedef struct { anarray x; int y;} struct1; typedef struct { int x[10]; int y; }struct2; typedef int anarray[10]; typedef struct { anarray a; int b; }struct3; typedef int anarray[10]; typedef struct { int b; anarray a; }struct4; (Note we are just using the syntax of C as an example. C does NOT use structural equivalence for structs

39 Structural Equivalence Check representing types as trees –Check equivalence recursively on subtrees Consider… Dynamic arrays Type array1 = array[-1..9] of integer; array2 = array[0..10] of integer; Array (INTEGER range <>) of INTEGER

40 Name Equivalence Two name types are equivalent only if they have the exact same type name Name equivalence in Ada and C ar1 and ar2 are not considered name equivalent typedef int ar1[10]; typedef ar1 ar2; typedef int age; type ar1 is array (INTEGER range1..10) of INTEGER; type ar2 is new ar1; type age is new INTEGER;

41 Name equivalence… v1: ar1; v2: ar1; v3: ar2; v4: array (INTEGER range ) of INTEGER; v5: array (INTEGER range ) of INTEGER; v4 and v4 cannot be name equivalent, as there is no name. v6,v7: array (INTEGER range ) of INTEGER;

42 Declaration Equivalent Lead back to the same original structure declaration via a series of redeclarations type t1 = array [1..10] of integer; t2 = t1; t3 = t2; These are the same type type t4 = array [1..10] of integer; t5 = array [1..10] of integer; These are different types.

43 Type Checking Involves the application of a type equivalence algorithm to expressions and statements to determine if they make sense Any attempt to manipulate a data object with an illegal operation is a type error Program is said to be type safe (or type secure) if guaranteed to have no type errors Static versus dynamic type checking Run time errors

44 Type Checking… Strong typing and type checking –Strong guarantees type safety A language is strongly typed if its type system guarantees statically (as far as possible) that no data-corrupting errors can occur during execution. Statically typed versus dynamically typed –Static (type of every program expression be known at compile time) All variables are declared with an associated type All operations are specified by stating the types of the required operands and the type of the result Java is strongly typed Python is dynamic typed and strong typed

45 Strongly typed /* Python code */ >>> foo = "x" >>> foo = foo + 2 Traceback (most recent call last): File " ", line 1, in ? foo = foo + 2 TypeError: cannot concatenate 'str' and 'int' objects >>> foo is of str type. In the second line, we're attempting to add 2 to a variable of str type. A TypeError is returned, indicating that a str object cannot be concatenated with an int object. This is what characterizes strong typed languages: variables are bound to a particular data type.

46 Weakly Typed /* PHP code */ In this example, foo is initially a string type. In the second line, we add this string variable to 2, an integer. This is permitted in PHP, and is characteristic of all weak typed languages. C is static typed and weak typed as we can cast a variable to be a different type (which should not be allowed if it isn’t of that type)

47 Type Conversion r is float and j is integer r = j i is integer and j is integer i = j

48 Type Conversion… Modula2 i := TRUNC (FLOAT(j) ) Explicit type conversion –type conversion functions Implicit conversion –coercion –can weaken type checking – as lots of coercions are allowed. For example, if (a=b) coerces int to boolean.

49 Type conversion… Casts –A value or object of one type is preceded by a type name (int) 3.14 Often does not cause a conversion to take place. Internal representation is reinterpreted as a new type CARDINAL(-1) This is very dangerous!

50 Type Conversions Def: A mixed-mode expression is one that has operands of different types Def: A coercion is an implicit type conversion The disadvantage of coercions: –They decrease in the type error detection ability of the compiler In most languages, all numeric types are coerced in expressions, using widening conversions In Ada, there are virtually no coercions in expressions