Data Types - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Data Types

Description:

type Disc is (IsInt, IsReal); type IntOrReal (which: Disc) is. record. case which is ... x: IntOrReal := (IsReal, 2.3); CS4303 Hong Lin. 20. Safety ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 23
Provided by: hong88
Category:
Tags: data | isreal | types

less

Transcript and Presenter's Notes

Title: Data Types


1
Chapter 6
  • Data Types
  • Part 1

2
Topics
  • Data types and type information
  • Simple types
  • Type constructors
  • Type equivalence
  • Type checking
  • Type conversion

3
Data Types
  • Algorithms data structures programs
  • The basic abstraction mechanism
  • a data type is a set of values
  • program data can be classified according to their
    types
  • a data type as a set can be specified in many
    ways.
  • e.g., explicitly listed, enumerated, subrange, ..

4
Abstract data types
  • A set of values generally also has a group of
    operations that can be applied to the values.
    (not often mentioned explicitly but are part of
    the definition.)
  • e.g., arithmetic operations for integer and reals
  • A data type is a set of values, together with a
    set of operations on those values having certain
    properties

5
Use of data types
  • data types can help translators to check for the
    validity of operations and actions. (error
    detection)
  • A translator can also use type information to
    allocate space for variables

6
Type definition
  • Type information can be contained in a program
    either implicitly or explicitly.
  • Implicit type information
  • Types of constants and values
  • Types that can be inferred from name conventions
  • Types that can be inferred from context
  • Explicit type information is primarily contained
    in declarations
  • e.g., int x10
  • It is possible to give names to new types in a
    type declaration.
  • e.g., typedef int10 intarray

7
Type system
  • Type Checking- the process a translator goes
    through to determine whether the type information
    in a program is consistent.
  • Type Equivalence- when two types are the same
  • Type System- The collection of type inference
    rules and type equivalence algorithms

8
Various type systems
  • Strongly Typed Language - well defined types that
    can be determined statically.
  • e.g., Ada, Algol 68, Modula-2, Pascal
  • Medium Typed Language well defined types but
    vague the boundary between types.
  • e.g., C
  • Weakly Typed Language - languages without
    complete static type systems.
  • e.g., Lisp, APL, SNOBOL, BLISS

9
Predefined Types
  • Algol like languages all classify types according
    to a basic scheme.
  • Every language comes with a set of predefined
    types from which all other types are constructed
  • e.g., integer, real, boolean, char
  • long real, double (precision)
  • unsigned integers (range)

10
Simple types
  • Predefined types are considered simple types.
  • Enumerated and subrange types are also simple
    types.
  • e.g., In Pascal
  • enum type
  • color (red, blue, green)
  • Subrange type
  • digit 0 .. 9
  • byte 0 .. 255

11
Base type of a subrange
  • The base type of the subrange could be implicit
    or explicit
  • Pascal implicit
  • Ada explicit
  • e.g.,
  • In Ada
  • subtype digit is INTEGER range 0 .. 9
  •   In Modula-2
  • TYPE digit INTEGER 0..9

12
Ordinal types
  • Ordinal Types- ordered types for which successor
    and predecessor operations exist
  • reals are not ordinal
  • subranges are ordinal types

13
Memory space
  • Space allocated for different types
  • boolean 1 byte (low bit)
  • char 1 byte
  • integer 2-4 bytes
  • reals 4-8 bytes
  • enumerated or subrange min of bytes
  • enumerated types unsigned integer
  • subrange Two possibilities
  • the full space of the base type
  • minimum number of bytes needed to store all the
    values

14
Type constructors
  • Since datatypes are sets, set operations can be
    used to construct new types out of existing ones.
  • cartisian product
  • U V (u, v) u is in U and v is in V
  • projection functions
  • p1 U V ? U, where p1((u, v)) u
  • p2 U V ? V, where p2((u, v)) v

15
Record (structure construction)
  • int char double
  • In C
  • struct INtCharReal
  • int i
  • char c
  • double r
  • In Ada
  • type IntCharReal is record
  • i integer
  • c character
  • r float
  • end record

16
Component selector (structure member)
  • Projections are given by component selector
  • IntCharReal x
  • x.i
  • Difference between Cartesian product and record
  • record structure components have names
  • Cartesian product components are referred to by
    position

17
Tuples
  • Tuples are identical to the Cartesian product
  • in ML
  • type IntCharReal int char real
  • Values of this type are written as tuples, e.g.,
  • (2, a, 3.14), (42, z, 1.1)
  • Projections are written as 1, 2, 3, etc
  • 3 (2, a, 3.14) 3.14

18
Memory allocation
  • Sequential allocation according to the space
    needed by each component
  • IntCharReal 13 bytes
  • int 4 bytes
  • char 1 byte
  • double 8 bytes
  • might take 14 or 16 bytes if the space must be
    allocated in even or word chunks

19
Union
  • Discriminated union
  • Use a tag or discriminator to distinguish which
    type the element is
  • variant record in Ada (and Modula-2), e.g.,
  • type Disc is (IsInt, IsReal)
  • type IntOrReal (which Disc) is
  • record
  • case which is
  • when IsInt gt i integer
  • when IsReal gt r float
  • end case
  • end record
  • x IntOrReal (IsReal, 2.3)

20
Safety
  • Safety is guaranteed by assigning both the
    discriminant and a corresponding value at the
    same time
  • dynamic error checking
  • put(x.i) -- but x.which IsReal at this point
  • then a COSTRAINT_ERROR is generated

21
Undiscriminated union
  • Undiscriminated unions lack tag, e.g., in C (and
    C)
  • typedef union
  • int i
  • float r utype
  • component names tell which of the types the raw
    bits stored in the union should be interpreted
    as, e.g.,
  • x.i is an integer
  • x.r is a float
  • Undiscriminated unions are unsafe because
    assumptions must be made about the type of any
    particular value

22
The end of Chapter 6 Part 1
Write a Comment
User Comments (0)
About PowerShow.com