elementary data types

Basic differences among programming languages:
  • types of data allowed
  • types of operations available
  • mechanisms for controlling the sequence of operations
Elementary data types: built upon the available hardware features
Structured data types: 
software simulated

  1. Data objects, variables, and constants
  2. 1. 1. Data object:
    a run-time grouping of one or more pieces of data in a virtual computer.
    a location in memory with an assigned name in the actual computer.
    Types of data objects:
    • Programmer defined data objects - variables, arrays, constants, files, etc.
    • System defined data objects - set up for housekeeping during program execution, not directly accessible by the program. E.g. run-time storage stacks.
    Data value: a bit pattern that is recognized by the computer.
    Elementary data object: contains a data value that is manipulated as a unit.
    Data structure: a combination of data objects.
    Attributes: determine how the location may be used. Most important attribute - the data type.
    Attributes and Bindings
    • Type: determines the set of data values that the object may take and the applicable operations.
    • Name: the binding of a name to a data object.
    • Component: the binding of a data object to one or more data objects.
    • Location: the storage location in memory assigned by the system.
    • Value: the assignment of a bit pattern to a name.
    Typename and component are bound at translation, location is bound at loading, value is bound at execution
    1. 2. Data objects in programs
    In programs, data objects are represented as variables and constants
    Variables: Data objects defined and named by the programmer explicitly.
    Constants: a data object with a name that is permanently bound to a value for its lifetime.
    • Literals: constants whose name is the written representation of their value.
    • A programmer-defined constant: the name is chosen by the programmer in a definition of the data object.
    1. 4. PersistenceData objects are created and exist during the execution of the program. Some data objects exist only while the program is running. They are called transient data objects. Other data objects continue to exist after the program terminates, e.g. data files. They are called persistent data objects. In certain applications, e.g. transaction-based systems the data and the programs coexist practically indefinitely, and they need a mechanism to indicate that an object is persistent. Languages that provide such mechanisms are called persistent languages.

  3. Data types
  4. A data type is a class of data objects with a set of operations for creating and manipulating them.
    Examples of elementary data types:
    integer, real, character, Boolean, enumeration, pointer.
    2. 1. Specification of elementary data types
    1. Attributes that distinguish data objects of that type
    2. Data type, name - invariant during the lifetime of the object
      • stored in a descriptor and used during the program execution
      • used only to determine the storage representation, not used explicitly during execution

    3. Values that data object of that type may have
    4. Determined by the type of the object
      Usually an ordered set, i.e. it has a least and a greatest value
    5. Operations that define the possible manipulations of data objects of that type.
    6. Primitive - specified as part of the language definition
      Programmer-defined (as subprograms, or class methods)
      An operation is defined by:
      • Domain - set of possible input arguments
      • Range - set of possible results
      • Action - how the result is produced
      The domain and the range are specified by the operation signature
      • the number, order, and data types of the arguments in the domain,
      • the number, order, and data type of the resulting range
      mathematical notation for the specification:
      op name: arg type x arg type x … x arg type ® result typeThe action is specified in the operation implementation
      Sources of ambiguity in the definition of programming language operations
      • Operations that are undefined for certain inputs.
      • Implicit arguments, e.g. use of global variables
      • Implicit results - the operation may modify its arguments
      • (HW 01 - the value of a changed in x = a + b)
      • Self-modification - usually through change of local data between calls,
        i.e. random number generators change the seed.
    Subtypes : a data type that is part of a larger class.
    Examples: in C, C++ int, short, long and char are variations of integers.
    The operations available to the larger class are available to the subtype.
    This can be implemented using inheritance.
    2. 2. Implementation of a data type
    1. Storage representation
    2. Influenced by the hardware
      Described in terms of:
      Size of the memory blocks required
      Layout of attributes and data values within the block
      Two methods to treat attributes:
      1. determined by the compiler and not stored in descriptors during execution - C
      2. stored in a descriptor as part of the data object at run time - LISP Prolog

    3. Implementation of operations
      • Directly as a hardware operation. E.g. integer addition
      • Subprogram/function, e.g. square root operation
      • In-line code. Instead of using a subprogram, the code is copied into the program at the point where the subprogram would have been invoked.

  5. Declarations
  6. Declarations provide information about the name and type of data objects
    needed during program execution.

    • Explicit – programmer defined
    • Implicit – system defined
    • e.g. in FORTRAN - the first letter in the name of the variable determines the type
      Perl - the variable is declared by assigning a value
      $abc = 'a string' $abc is a string variable
      $abc = 7 $abc is an integer variable
    Operation declarations: prototypes of the functions or subroutines that are programmer-defined.
    Examples:
    declaration: float Sub(int, float)
    signature: Sub: int x float --> float
    Purpose of declaration
    • Choice of storage representation
    • Storage management
    • Declaration determines the lifetime of a variable, and allowes for more efficient memory usage.
    • Specifying polymorphic operations.
    • Depending on the data types operations having same name may have different meaning, e.g. integer addition and float addition
      In most language +, -. *, / are overloaded
      Ada - aloows the programmer to overload subprograms
      ML - full polymorphism
    Declarations provide for static type checking

  7. Type checking and type conversion
  8. Type checking: checking that each operation executed by a program receives
    the proper number of arguments of the proper data types.
    Static type checking is done at compilation.
    Dynamic type checking is done at run-time.
    Dynamic type checking – Perl and Prolog
    Implemented by storing a type tag in each data objectAdvantages: Flexibility
    Disadvantages:
    • Difficult to debug
    • Type information must be kept during execution
    • Software implementation required as most hardware does not provide support
    Concern for static type checking affects language aspects:
    Declarations, data-control structures, provisions for separate compilation of subprogramsStrong typing: all type errors can be statically checked
    Type inference: implicit data types, used if the interpretation is unambiguous. Used in ML
    Type Conversion and CoercionExplicit type conversion : routines to change from one data type to another.
    Pascal: the function round - converts a real type into integer
    C - cast, e.g. (int)X for float X converts the value of X to type integer
    Coercion: implicit type conversion, performed by the system.
    Pascal: + integer and real, integer is converted to real
    Java - permits implicit coercions if the operation is widening
    C++ - and explicit cast must be given.
    Two opposite approaches to type coercions:
    • No coercions, any type mismatch is considered an error : Pascal, Ada
    • Coercions are the rule. Only if no conversion is possible, error is reported.
    Advantages of coercions: free the programmer from some low level concerns,
    as adding real numbers and integers.
    Disadvantages: may hide serious programming errors.

No comments:

Post a Comment