Download Type Systems and Type Inference in Programming Languages and more Slides Computer science in PDF only on Docsity!
slide 1
Type
A type is a collection of computable values
that share some structural property
Examples
int bool
(int int) bool
“Non-examples”
3, true, x.x
f:int int | if x>
then f(x) >
x*(x+1)
slide 2
Uses for Types
Program organization and documentation
- (^) Separate types for separate concepts
- (^) Represent concepts from problem domain
- (^) Indicate intended use of declared identifiers
- (^) Types can be checked, unlike program comments
• Identify and prevent errors
- (^) Compile-time or run-time checking can prevent
meaningless computations such as 3 + true - “Bill”
Support optimization
- (^) Example: short integers require fewer bits
- (^) Access record component by known offset
slide 4
Type Errors
Machine data carries no type information
- (^) 01000000010110000000000000000000 means…
- (^) Floating point value 3.375? 32-bit integer
1,079,508,992? Two 16-bit integers 16472 and 0? Four
ASCII characters @ X NUL NUL?
• A type error is any error that arises because an
operation is attempted on a value of a data type
for which this operation is undefined
- (^) Historical note: in Fortran and Algol, all of the types
were built in. If needed a type “color,” could use
integers, but what does it mean to multiply two colors?
Static vs. Dynamic Typing
Type system imposes constraints on use of values
- (^) Example: only numeric values can be used in addition
- (^) Cannot be expressed syntactically in EBNF
Language can use static typing
- (^) Types of all variables are fixed at compile time
- (^) Example?
… or dynamic typing
- (^) Type of variable can vary at run time depending on
value assigned to this variable
slide 5
When to perform Type Checking?
- 7
- Compile-Time
- (Static Type Binding)
- In theory, you can choose
to type check at compile
time or run-time.
- In practice, languages try
to do it as much statically
as possible.
- Eg. SML, Pascal
- (Dynamic Type
Binding)
dynamic type checking.
- Eg. JavaScript, APL
- When is the variable
bound to the type?
check?
When to perform Type Checking?
Static Type Checking – done at compile time.
- (^) (+) Done only once
- (^) (+) Earlier detection of errors
(–) Less Program Flexibility (Fewer shortcuts and tricks)
Type checking
- (^) Type Checking is the activity of ensuring that the
operands / arguments of an operator / procedure are of
compatible type
This is done by using a set of rules for associating a type
with every expression in the language. (These rules are
known as the type system ).
A type error results when an operator is applied to an
operand of inappropriate/incompatible type.
Type checking
• It involves applying often complex rules for relaxing exact
type matching under certain circumstances, usually called
type compatibility rules.
- Assignment compatibility refers to the compatibility rules
governing assignments.
- (^) Simple example: x = y / 2 + 3.5. Clearly x and y must
be numeric. Can x be an int? Can x be a float? Can y be a
long? What are the (implicit) types of the literals 2 and
Type checking (3)
- (^) Back to previous example: x = y / 2 + 3.5; Suppose the
type of x is float and the type of y is long. Does this
statement type check?
- (^) First determine the type of y / 2: 2 is implicitly an int. Then,
since y is a long, 2 is converted automatically ("promoted") to a
long, and the type of y / 2 is long.
- (^) Now determine the type of the sum: the left operand is a long
and the right operand is a double (implicitly). By the rules of
Java, a long can be promoted to a double, so the result is a
double.
- (^) However, a double cannot be assigned to a float, so a type
error occurs at that point.
Type conversion
- (^) Type conversion can be classified two ways:
- (^) Does the conversion require written code?
- (^) Does the internal representation change, or just the type?
- (^) The 1st classification has two categories:
- (^) automatic or implicit conversion (no code)
- (^) manual or explicit conversion (code must be written)
- (^) The 2nd classification also has two categories:
- (^) The value representation in memory changes
- The value representation in memory doesn't change, just
the type
- All four combinations of these can occur. - 14
Polymorphism
Polymorphism = poly (many) + morph (form)
- (^) Polymorphism is the ability of a data object to that can take
on or assume many different forms.
Polymorphism can be categorised into 2 types
Universal Polymorphism
- (^) Parametric
- (^) Inclusion
- Types of Polymorphism
- (^) Ad-hoc polymorphism
similar function implementations for different types
(method overloading, but not only)
- Subtype (inclusion) polymorphism
instances of different classes related by common super
class
functions that work for different types of data
def plus(x, y):
return x + y
class A {...}
class B extends A {...};
class C extends A {...}
Subtypes
A subtype is a type that has certain constraints
placed on its values or operations
Can be directly specified in some languages
(Ada)
subtype one_to_ten is Integer range 1 .. 10;
slide 19
Subtype Polymorphism
First introduced in the 60s with Simula
- (^) Usually associated with OOP
(in some circles, polymorphism = subtyping)
- (^) Principle of safe substitution (Liskov substitution principle)
Note that this is behavioral subtyping, stronger than simple
functional subtyping.
if S is a subtype of T, then objects of type T may be
replaced with objects of type S without altering any
of the desirable properties of the program .”