H. Conrad Cunningham
5 September 2018
Copyright (C) 2018, H. Conrad Cunningham
Acknowledgements:
I created these slides in Fall 2018 to accompany Section 5.2. Type System Concepts, of the book Exploring Languages with Interpreters and Functional Programming. This was also Section 5.2 of the draft book Multiparadgm Programming with Python 3.
Browser Advisory: The HTML version of this document may require use of a browser that supports the display of MathML. A good choice as of September 2018 is a recent version of Firefox from Mozilla.
Examine the general concepts of type systems
Learn how to characterize language type systems
Type S
(behavioral) subtype of type
T
if
S
“subset” of value set T
S
“superset” of operation set
T
S
for elements of
T
because S
operations behave “same” as
T
operations
Types Furniture
(all furniture) and Chair
(all chairs)
Chair
subset of Furniture
Chair
has all attributes of Furniture
,
possibly plus some
Chair
has all operations of Furniture
,
possibly plus some
Furniture
operations on Chair
instances
give “same” result
Satisfy Liskov Substitution Principle
Constant has whatever types defined to have in context
Type of “1” bit, integer, float, complex based on context
Variable has whatever types its value has at particular time
Expression has whatever types it yields based on its constituent variables, constants, operations
Some may be declared explicitly
Others inferred implicitly from context
Examples: Java, Scala, Haskell
type of variable or expression cannot be determined at “compile time” – checked at runtime
Most languages mix static and dynamic
Java uses declared types
Java type Object
may
require runtime checks
Values have same type only if have same type name
Type S
is subtype of T
only if
S
explicitly declared subtype
Example: Java nominally typed using class/interface names
But does not guarantee Liskov Substitution Principle – subclass can change behavior
Values have same type if “same” structure – compatible public data attributes and operations
Type S
is subtype of T
only if
S
has all public data values and operations of
T
, which are of compatible types
Example: Haskell
Ad hoc polymorphism, same function name denotes different implementations depending on use
overloading – compiler determines which to invoke, early binding
+
in Java, Haskell type classes
subtyping – runtime usually searches hierarchy of types, late binding
Called just polymorphism in Java – does not exist in Haskell
Parametric polymorphism, same implementation used for different types
Called generics in Java – Haskell just polymorphism
Dynamically typed language – variables hold any value
Statically typed language – variables hold declared type or declared subtype
Polymorphic (generic) function – parameter hold typed allowed parameter declaration
Types
Subtypes satisfying Liskov Substitution Principle
Static and dynamic languages
Nominal and structural types
Polymorphic operations – matching name with implementation
Polymorphic variables – hold multiple types of values