Types and Type Systems
Why Data Types?
- Data types play a key role in:
-
in the design of programs -
in the analysis of programs -
in the translation and execution of programs - Data layout (how many words; which are data and which are pointers) dictated by type
Terminology
-
Type: A
defines a set of ppssible data values -
E.g.
in C is -
A value in this set is said to have type
- Type system: rules of a language assigning types to expressions
Types as Specifications
- Types describe properties
- Different type systems describe different properties, e.g.
- Data is read write-versus read-only
- Operation has authority to access data
- Data came from right source
- Operation might or could not raise an exception
- Common type systems focus on types describing same data layout and access methods
Sound Type System
-
If an expression is assigned type
, and it evaluates toa value , then is in the set of values defined by - SML, OCAML, Scheme and Ada have sound type systems
- Most implementations of C and C++ do not
Strongly Typed Language
-
When no appliaction of an operator to arguments can lead to a run-time type error, langauge is
- E.g. 1 + 2.3;;
- Depends on definition of type error
- 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 (underirable) effects
- No array bounds check (in fact, no runtime checks at all)
- SML, OCAML strongly type but still must do dynamic array bounds checks, runtime type case analysis, and other checks
Static vs Dynamic Types
type assigned to an expression at compile time type assigned to a storage location at run time static type assigned to every expression at compile time type of an expression determined ar run time
Type Checking
- When is op(arg1, …, argn) allowed?
-
assures that operations are applied to the right number of arguments of the right types - Right type may mean same type as was specified, or may mean that there is a predefined implicit coercion that will be applied
- Used to resolve overloaded operations
-
Type checking may be done
at compile time or at run time - Dynamically typed (aka untyped) languages (e.g. LISP, Prolog) do only dynamic type checking
- Statically typed languages can do most typed checking statically
Dynamic Type Checking
- Performed at run-time before each operation is applied
- Types of variables and operations left unspecified until run-time
- Same variable may be used at different types
- Data object must contain type information
- Errors aren’t detected until violating application is execurted (maybe years after the code was written)
Static Type Checking
- Performed after parsing, before code generation
- Type of every variable and signature of every operator must be known at compile time
- Can eliminate need to store type information in data object if no dynamic type checking is needed
- Catches many programming arrors at earliest point
- Can’t check types that depend on dynamically computed values
- E.g. array bounds
- Typically places restriction on languages
- Garbage collection
- References instead of pointers
- All variables initialized when created
- Variable only used at one type
- Union types allow for work-arounds, but effectively introduce dynamic type checks
Type Declarations
-
explicit assigment of types to variables (signatures to functios) in the code of a program - Must be checked in a strongly typed language
- Often not necessary for strong typing or even static typing (depends on the type system)
Type Inference
-
A program analysis to assign a type to an expression from the program cobtext of the expression - Fully static type inference first introduced by Robin Miller in ML
- Haskle, OCAML, SML all use type inference
- Records are a problem for type inference
Format of Type Judgments
-
A
has the form -
is typing environment - Supplies the types of variables (and function names when function names are not variables)
-
is a set of the form -
For any
at most on such that -
is a program expression -
is a type to be assigned to -
pronounced “turnstyle”, or “entails” (or satisfies or, informally, shows)
Axioms - Constants (Monomorphic)
- These rules are true with any typing environmnet
-
are meta-variables
Axioms - Variables (Monomorphic Rule)
Notation: Let
Variable axiom:
Simple Rules - Arithmetic (Mono)
Primitive Binary operators
Special case: Relations
Example:
What do we need to show first?
What we need fot the left and right side?
How to finish?
And
Complete Proof (type derivation)
Simple Rules - Booleans
Connectives
Type Variables in Rules
- If-then-else rule:
-
is a type variable (meta-variable) - Can take any type at all
- All instances in a rule application must get same type
- Then branch, else branch and if-then-else must all have same type