Download Overview of Type Systems: Understanding Data Types and Their Role in Programming - Prof. E and more Study notes Computer Science in PDF only on Docsity!
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
CS421 Topic 6: Overview of Type Systems
Sameer Sundresh
University of Illinois at Urbana-Champaign
June 4, 2007
Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, Elsa Gunter, and Mark Hills
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Introduction: Why Have Data Types?
Data types play a key role in:
I the design of programs (through data abstraction);
I the analysis of programs (through type checking and type
inference);
I the translation and execution of programs (through
compile-time code generation).
Underlying these uses are type systems that provide one way to
express the meaning of a program.
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Type System Soundness Type Errors
Type
A type t defines a set of possible data values
I E.g., short in C ≡ {i : − 215 ≤ i ≤ 215 − 1 }
I A value in this set is said to be of type t.
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Type System Soundness Type Errors
Type System
The rules of a language that describe how to assign a type to each
expression in a program are the type system.
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Type System Soundness Type Errors
Type Errors
An operation that is applied to an operand of an illegal type is a
type error. When the operation may be applied to an operand of
an illegal type, this is a potential type error.
I 1 +. 2.5 in OCAML
I atoi(); in C
I MyCls cref = (MyCls) X; in Java
This last may or may not be a type error. Such an error must
be detected at run time.
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Strongly Typed Languages Weakly Typed Languages Statically Typed Languages Dynamically Typed Languages
Strongly Typed Languages
A language is strongly typed when no application of an operator
to its arguments can lead to a run-time type error. This depends
strongly on the definitions of “type” and “type system” for the
language. A sound type system is required, and expensive checks
may be needed.
I C++ claimed to be “strongly typed”, but
I Union types allow creating a value at one type and using it at
another
I Type coercions may cause unexpected (undesirable) effects
I No array bounds check (in fact, no runtime checks at all)
I SML, OCAML “strongly typed” but still must do dynamic
array bounds checks, runtime type case analysis, and other
checks
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Strongly Typed Languages Weakly Typed Languages Statically Typed Languages Dynamically Typed Languages
Statically Typed Languages
A static type is a type assigned to an expression at compile-time.
A statically typed language is one where every expression is
assigned a static type.
I Weakly Statically Typed: Assigned types are assumed to be
correct and not checked.
E.g., C, C++, Fortran
I Strongly Statically Typed: Assigned types are checked at
compile-time or run-time
E.g., ML, Haskell, Ada, OCAML, Java.
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Strongly Typed Languages Weakly Typed Languages Statically Typed Languages Dynamically Typed Languages
Dynamically Typed Language
A dynamic type is a type assigned to a storage location at run
time. A dynamically typed language is one where the type of an
expression may not be determined until run time.
I Safe, but may incur significant run-time overheads
I Cost: Cannot generate unique code for some operations at
compile-time
⇒ Generally used only in interpreted languages
Examples: Lisp, Scheme, SmallTalk, Python
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Checking Dynamic Type Checking Static Type Checking
Type Checking
Type checking may be done statically at compile time or
dynamically at run time.
I dynamically typed (aka untyped) languages, like Lisp and
Prolog, do only dynamic type checking
I statically typed languages can do most type checking
statically (at compile time)
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Checking Dynamic Type Checking Static Type Checking
Dynamic Type Checking
I dynamic type checking is performed at run-time before each
operation is applied
I types of variables and operations are left unspecified until
run-time
I variables may be used at different times with different types
I data objects must maintain type information at runtime
I type errors are not detected until the violating section of code
is executed, sometimes years after being written
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Checking Dynamic Type Checking Static Type Checking
Static Types: An Example (OCaml)
1 # let x = 7;;
2 val x : int = 7
3 # let f _ = x + y;;
4 Unbound value y
5 # let y = "three";;
6 val y : string = "three"
7 # let f _ = x + y;;
8 This expression has type string but is here used with type int
9 # f ();;
10 Unbound value f
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Checking Dynamic Type Checking Static Type Checking
Static Type Checking
I performed after parsing, but before code generation –
considered part of the compiler front-end
I type of every variable and type signature of every operator
must be known at compile time
I can eliminate need to store type information with data object
if no dynamic checks are needed – type erasure
I catches many programming errors early – before execution
I cannot check types that depend on dynamically computed
values, like array bounds
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Checking Dynamic Type Checking Static Type Checking
Type Checking Costs
Type checking may incur several possible run-time overheads:
1. Run-time type checks, e.g., MyCls cref = (MyCls) X;
2. Run-time array bounds checks, e.g., int X = A[i];
3. Run-time null pointer checks, e.g., int X = *p;
4. Proper initialization of data values, e.g., zeroing in new T;
5. Garbage Collection (GC): disallow explicit free / delete
Static type checking may eliminate some instances of the first 3
kinds of checks. #2 and #3 are especially hard to eliminate and
every widely-used, strongly typed language relies on GC!
Type System Basics Classes of Type Systems Type Checking Type Declarations & Conversions Applications
Type Declarations Type Inference Type Equivalence and Compatibility Type Casts and Coercions
Type Declarations
A type declaration is the explicit assignment of a type to a variable
or function in the program source
I must be checked in a strongly typed language
I often not necessary for strong typing or even static typing