Unit 8

What is Type?

  • Definition:

    • A type is defined as a set of values and the operations allowed on those values.

  • Example of a Basic Type:

    • Integer: Represents any whole number in the range of −2^31 ≤ x < 2^31.

  • Predefined Types:

    • Some types are predefined by the programming language.

  • User-defined Types:

    • Programmers can define other types using definitions.

  • Type Expression:

    • The type of a programming construct is denoted by a type expression.

  • Example:

    • The statement int a[2][3]; demonstrates a type expression.

Type System

  • Definition:

    • The collection of types and their associated rules is collectively known as a type system.

  • Purpose:

    • Establish rules to associate type expressions with different program elements, such as variables, expressions, and functions.

  • Characteristics:

    • Type systems involve rules for:

      • Type equivalence

      • Type compatibility

      • Type inference

  • Compiler Differences:

    • Different compilers for the same language may utilize various type systems.

    • Example: In Pascal, the index set of arrays is included in function type information, while some compilers may allow it to be unspecified.

Type System and Its Types

Strongly Typed

  • Every expression is associated with a clear and unambiguous type.

Statically Typed

  • Every expression is assigned a type during compilation.

  • Example languages: Pascal, Java (employ manifest typing) and Standard ML (employs implicit typing).

Dynamically Typed

  • Types are linked to run-time values instead of expressions.

  • Type errors are only detected during code execution.

  • Example languages: Lisp, Perl, Python, Javascript, Ruby.

Weakly Typed

  • Permits values of one type to be utilized as another, which may introduce undetected errors at compile time and possibly at runtime.

Untyped

  • Allows any operation on any data type with no type checking enforced.

  • Example languages: Assembly, Tcl, BCPL.

Comparison of Type Systems

Feature

Static Typing

Dynamic Typing

Error Detection

Errors found at compile time

Errors found at runtime

Compilation Speed

Generally faster compilation

Slower due to runtime checks

Bug Fixing Cost

Lower cost for bug fixing

Higher cost for bug fixing

Runtime Information

Variable type depends on runtime context

Improves reliability and performance for compiled code

Code Loading

Compiled code loading isn't dynamic

Can load new code dynamically

Type System Strength

Effectiveness is linked to type strength

Dynamic Checking

  • All checking can be implemented dynamically.

  • A sound type system can often eliminate the need for dynamic checking.

  • Compilers may implement statically typed languages while incorporating dynamic checking.

Imperative vs Non-Imperative Programming

  • Imperative Programming:

    • Focuses on how tasks are executed.

    • Programs define a sequence of commands for the computer to perform.

    • Example: Procedural languages categorize as imperative where programs are constructed from subroutines/functions.

  • Non-Imperative Programming:

    • Functional and logic-based languages that describe what the program needs to do rather than how to execute it.

    • Example languages: Prolog and various database query languages.