Type Systems
Fall 2021


Classification of Types

Simple types: Predefined types used to construct all other types
Discrete Types: Enumerated and Subrange Types
Subsets/subranges

Composite, or constructed type 

Records:


How are records stored in memory and what are offsets?
Initialization 

Type Checking

  • Issue for statically typed languages
  • When are two types the same? -- Type Equivalence

    subtype S is T1 range 2..5

    Type Checking  

    Type Compatibility 

    Coercion

    What do you think this means?
  • The programmer should be aware:
  • Casting


    Sequence data types:

         type WkE = (Sat, Sun);
    type WkE = (Sat, Sun);
    Costs = array[WkE] of real;

    ARRAY(Integer range <>, Integer range <>) OF Integer;

    Elaboration
    TYPE  Matrix_4x4_type  IS 
    ARRAY(1..4, 10..13) OF Integer;

  • If the arrays are allocated during compile time then the range is statically bound.
  • Automatic Arrays (variable length arrays) declared in  C functions:  
  • void cFunction (int size ) {
         int marbles [size][size/2];
    }
  • Range bound dynamic, storage dynamic
  • Prior to C99 this code would not compile.
  • Prior to C99: When multidimensional arrays are used, it is necessary to specify the bounds of but the first dimension.

  • When and where does array allocation take place?

  • If  the arrays are allocated from the heap then both the storage allocation and the subscript ranges can be dynamically bound.
  • Java: reference model
  • The key feature of arrays is O (1) access time to any array element.
  • Formula for computing the effective address of a one dimensional array:
  • ba + size * (k - LowerBound of A)

  • As you can see the cost of computing A[k] is independent of k.
  • Multidimensional arrays:  Where does the second element of the array go? 
  • A: array [LowerBound .. UpperBound ] of array [1..4] of char
  • Row-major -- Rows are contiguous in memory A[2,4] is followed by A[2,5]
  • Column-major major columns are contiguous, A[2,4] is followed by A[3,4]
  • Fortran uses Column major most languages uses row major --
  • What is the implications for porting code? (Cache?, program correctness?)

  • double[ ] [ ] identity = {
    { 1.0, 0.0, 0.0 },
    { 0.0, 1.0, 0.0 },
    { 0.0, 0.0, 1.0 }
    };

    Haskell, Prolog

    • Do not have arrays.
    • lists are the way to sequence data.
    • How are lists different from arrays?

    Pointers:

    main(){
      char* ptChr =0;
      short* ptShort =0;
      long* ptLong =0;
      int i;
     
      printf("               Index Char Short  Long\n");
      for ( i =0; i < 6; i++) {
        printf("Offset of Pointers %d    %d    %2d    %2d\n",
           i, ptChr+i, ptShort+i, ptLong+i);
      }
    }
    ~>
                    Index Char Short Long
    Offset of Pointers  0    0     0    0
    Offset of Pointers  1    1     2    4
    Offset of Pointers  2    2     4    8
    Offset of Pointers  3    3     6   12
    Offset of Pointers  4    4     8   16
    Offset of Pointers  5    5    10   20

    Pointers and Arrays in C