Docsity

Structuring the Computation - Software Paradigms - Slides | CSCI 169, Study notes of Computer Science

Toggle sidebar
bg1
A. Bellaachia Page: 1
Structuring the Computation
1. Objectives.................................................................................2
2. Strong Typing & Type Checking.............................................3
3. Type Compatibility ..................................................................4
4. Type Conversion ......................................................................5
5. Type and Subtypes ...................................................................6
6. Generic Types...........................................................................7
7. Monomorphic versus Polymorphic..........................................8
8. The Type Structure of Representative languages ....................9
9. Implementation Models............................................................9
10. Implementation of Structured Types..................................11
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Structuring the Computation - Software Paradigms - Slides | CSCI 169 and more Study notes Computer Science in PDF only on Docsity!

Structuring the Computation

    1. Objectives .................................................................................
    1. Strong Typing & Type Checking.............................................
    1. Type Compatibility ..................................................................
    1. Type Conversion ......................................................................
    1. Type and Subtypes ...................................................................
    1. Generic Types...........................................................................
    1. Monomorphic versus Polymorphic ..........................................
    1. The Type Structure of Representative languages ....................
    1. Implementation Models............................................................
    1. Implementation of Structured Types..................................

1. Objectives

  • To control flow among the different constructs in a program.
  • This is key to understanding the semantics of the language
  • Type error can be classified into two categories: ˇ Application errors ˇ Language errors.
  • Language errors: ˇ Here we only address language errors , as opposed to application errors ˇ Language errors are programmer errors in how a language’s syntax and semantics are defined.

ˇ There are two categories o Syntactic o Semantic

  • Type Safety : guarantee no type errors
  • Another Classification:

ˇ Static: o During compilation o It is preferred.

ˇ Statically Typed Languages are Strongly Typed Languages.

3. Type Compatibility

  • A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type.
  • (^) Type compatibility is also called conformance or equivalence.
  • There are two type compatibility methods: ˇ Name compatibility (also called strict compatibility): o Two variables can have compatible types only if they are in either the same declaration or in declarations that use the same type name. o It is highly restrictive. o It is easier to implement. o Ada uses name compatibility.

ˇ Structure type compatibility : o Two variables have compatible types if their types have identical structures. o It is more flexible. o It is difficult to implement: compare the whole structure instead of just names. o Two types are structurally compatible if:

  1. T1 is name compatible with T2; or
  1. T1 and T2 are defined by applying the same type constructor to structurally compatible corresponding type components.

o Examples:

  1. Two records or structure types compatible if they have same structure but different field names?
  2. Two single-dimensioned array types in a Pascal or Ada program are compatible if they have the same element type but have different subscript ranges?
  3. C uses structural compatibility except for structures.

4. Type Conversion

  • It is needed when the types of an expression are not compatible.
  • Some programming languages allow automatic conversions (Also called coercion) : What would the following C program output?

#include <stdio.h>

main(){

char c1 = 'a'; int i = 10;

  • A subtype ST of a basic type T (also called parent type or supertype) can be defined as ˇ A subset of the values of T ˇ Assume that operations of T are inherited by ST
  • A language supporting subtypes must provide: ˇ A way to define subsets of a given type, and ˇ Compatibility rules between a subtype and its supertype ˇ Example: Ada subtype

SUBTYPE Day_Range IS Integer RANGE 0..31; SUBTYPE Year_Range IS Integer RANGE 1901..2099; SUBTYPE natural IS integer range 0..integer'last;

SUBTYPE natural IS integer RANGE 0..integer’last; SUBTYPE positive IS integer RANGE 1..integer’last;

6. Generic Types

  • It allows functions and ADT to have generic types
  • How type-checking is done to guarantee type safety?

ˇ Bind generic types to concrete types at compile- time instantiation.

  • Example:

push: stack(T)xT → stack(T) pop: stack(T) → stack(T)xT length: stack(T) → int

7. Monomorphic versus Polymorphic

  • Monorphic: It is a strict type system.
  • Polymorphic: It is the case where an object can have more than one type
  • Most practical programming languages have some degree of polymorphism.
  • (^) Languages deviates form strict monomorphic if they include one of the following:

ˇ Type equivalence ˇ Coercion ˇ Subtyping ˇ Overloading

  • Polymorphism classification:

polymorphism

Parametric (Templates)

Inclusion (subtypes)

overloading

coercion

universal

Ad hoc

ˇ Descriptor: structures used to store the attributes of the data ˇ Data object: memory locations to store the actual values

  • ••• Built-in Primitive types: ˇ Integers and reals are supported on most computers ˇ They may also provide different sized versions of these types ˇ Representations:

ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ ˇ

Descriptor

integer

sign bit

Descriptor

real

sign bit for mantissa sign bit for exponent

k-bit exponent h-bit mantissa

10. Implementation of Structured Types

  • ••• Cartesian product: ˇ It is a sequential layout of components. ˇ The descriptor contains the following: o The Cartesian product type name and o For each field: ˇ Name of the selector ˇ Type of the field ˇ Reference to the data object ˇ Example: type another_type is struct{ float a; int b; }; type sample_type is struct{ int x; another_type y; }; sample_type z; Sample_type Struct x int Integer value y Floating point value Integer value

another_type struct a float

b int

  • ••• Union and discriminated union ˇ The descriptor of a variable of a union type is the sequence of the descriptors of each component. ˇ Example:

type x_type = array[0..10] of real; type z_type = record case kind: boolean of true: (a:integer); false: (b:x_type); end;

Type Z_type constructor Variant record Selector Kind Type Boolean Case table Boolean value Reference Case table True false Selector b Type X_type Constructor array Selector a Index type Integer Type L. bound 0 reference integer U. bound 10 Range type real Reference

  • ••• Powerset ˇ Use bit-mapping to represent a set ˇ Use AND for Intersection and OR for union ˇ Example:

S = {1, 3, 10, 16}

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 10 0 0 0