CSci 555: Functional Programming
Type System Concepts

H. Conrad Cunningham

9 February 2019

Copyright (C) 2018, 2019, H. Conrad Cunningham
Professor of Computer and Information Science
University of Mississippi
211 Weir Hall
P.O. Box 1848
University, MS 38677
(662) 915-5358

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. I adapted the latter for use in Spring 2019 in the Scala-based CSci 555 (Functional Programming) course.

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 February 2019 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

Nominal and Structural (3)

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, Scala type class pattern

    • 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 and Scala — Haskell just polymorphism

Polymorphic Variables

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

Key Ideas