Download Type Systems and Type Checking: Understanding Strongly and Weakly Typed Languages - Prof. and more Study notes Computer Science in PDF only on Docsity!
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
CS421 Lecture 12: Type Derivations
Mark Hills
University of Illinois at Urbana-Champaign
June 24, 2008
Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, and Elsa Gunter
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Introduction: Why Have Data Types?
Data types play a key role in:
◮ the design of programs (through data abstraction);
◮ the analysis of programs (through type checking and type
inference);
◮ 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.
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
Type
A type t defines a set of possible data values
◮ E.g., short in C ≡ {i : − 215 ≤ i ≤ 215 − 1 }
◮ A value in this set is said to be of type t.
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
Type System
The rules of a language that describe how to assign a type to each
expression in a program are the type system.
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
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.
◮ 1 +. 2.5 in OCAML
◮ atoi(); in C
◮ 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.
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
Strongly Typed Language
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.
◮ C++ claimed to be “strongly typed”, but
◮ Union types allow creating a value at one type and using it at
another
◮ Type coercions may cause unexpected (undesirable) effects
◮ No array bounds check (in fact, no runtime checks at all)
◮ SML, OCAML “strongly typed” but still must do dynamic
array bounds checks, runtime type case analysis, and other
checks
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
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.
◮ Weakly Statically Typed: Assigned types are assumed to be
correct and not checked.
E.g., C, C++, Fortran
◮ Strongly Statically Typed: Assigned types are checked at
compile-time or run-time
E.g., ML, Haskell, Ada, OCAML, Java.
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
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.
◮ Safe, but may incur significant run-time overheads
◮ Cost: Cannot generate unique code for some operations at
compile-time
⇒ Generally used only in interpreted languages
Examples: Lisp, Scheme, SmallTalk
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
Type Checking
Type checking may be done statically at compile time or
dynamically at run time.
◮ dynamically typed languages, like Lisp and Prolog, do only
dynamic type checking
◮ statically typed languages can do most type checking
statically (at compile time)
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
Dynamic Type Checking
◮ dynamic type checking is performed at run-time before each
operation is applied
◮ types of variables and operations are left unspecified until
run-time
◮ variables may be used at different times with different types
◮ data objects must maintain type information at runtime
◮ type errors are not detected until the violating section of code
is executed, sometimes years after being written
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
Dynamic Types: An Example (part 2)
1 CL-USER(6): (setf a (car list2))
2 "three"
3 CL-USER(7): (setf b (car (cdr list2)))
4 "short"
5 CL-USER(8): (concatenate ’string a b)
6 "threeshort"
7 CL-USER(9): (+ a b)
8 Error: ‘"three"’ is not of the expected type ‘NUMBER’
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
Static Type Checking
◮ performed after parsing, but before code generation –
considered part of the compiler front-end
◮ type of every variable and type signature of every operator
must be known at compile time
◮ can eliminate need to store type information with data object
if no dynamic checks are needed – type erasure
◮ catches many programming errors early – before execution
◮ cannot check types that depend on dynamically computed
values, like array bounds
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
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!
Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up
Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion
Type Declarations
A type declaration is the explicit assignment of a type to a variable
or function in the program source
◮ must be checked in a strongly typed language
◮ often not necessary for strong typing or even static typing