Exploring Languages
with Interpreters
and Functional Programming

Section 5.2
Type System Concepts

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.

Type System Concepts

Lecture Goals

Types and Subtypes

Type (conceptual):
set of values and set of operations on them

Type S (behavioral) subtype of type T if

Liskov Substitution Principle
Substitute elements of subtype S for elements of T because S operations behave “same” as T operations

Example

Types Furniture (all furniture) and Chair (all chairs)

Satisfy Liskov Substitution Principle

Constant, Variable, Expression

Static and Dynamic (1)

Statically typed language:
types of variable or expression determined from source code at “compile time”

Static and Dynamic (2)

Dynamically typed language:

type of variable or expression cannot be determined at “compile time” – checked at runtime

Most languages mix static and dynamic

Nominal and Structural (1)

Nominal typing:
type of value based on type name assigned when created

Nominal and Structural (2)

Structural typing:
type of value based on structure of value

Polymorphic Operations

Polymorphism:
having “many shapes”
Polymorphic operation:
Associating function names (or operator symbols) with implementations

Kinds of Polymorphism (1)

  1. 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

Kinds of Polymorphism (2)

  1. Parametric polymorphism, same implementation used for different types

    Called generics in Java – Haskell just polymorphism

Polymorphic variables

Polymorphic variable:
variable can “hold” values of different types during program execution.

Key Ideas