Skip to content

Chapter 6 of 8

Type System Concepts

Haskell's type classes provide a mechanism for ad-hoc polymorphism, defining behavior that works across many types. Common examples include \( \text{Eq} \) for equality comparison, \( \text{Ord} \) for total ordering, \( \text{Show} \) for conversion to a human-readable string, and \( \text{Semigroup} \) and \( \text{Monoid} \) for associative operations with and without identity. Type classes are not classes in the object-oriented sense; they are interfaces with no inheritance, and instances provide implementations per type. Functions under composition form a monoid with the identity function as the empty element, and endomorphisms — functions from a type to itself — are central to Endomorphism-based aggregation strategies that compose many small transformations into one efficient pass.

Type inference is the compiler's ability to deduce the types of expressions without explicit annotations. The Hindley-Milner algorithm can find the most general type of an expression using unification, and it underpins Haskell, ML, and OCaml, though it cannot infer higher-rank types. Kinds are the types of types: \( \text{Int} \) has kind \( * \), \( \text{Maybe} \) has kind \( * \to * \), and a higher-kinded type like \( \text{MaybeT} \) has kind \( (* \to *) \to * \to * \). The kind system prevents nonsense like \( \text{Maybe Int Int} \). Parametric polymorphism means a function works uniformly across all types without inspecting them, as in \( \text{id} : a \to a \), while ad-hoc polymorphism allows different implementations per type, as with Haskell's type classes. Type variables, written in lowercase like \( a \) or \( b \), are placeholders that enable this parametric polymorphism. In Haskell, the arrow \( \to \) in a type signature is right-associative, so \( a \to b \to c \) parses as \( a \to (b \to c) \), mirroring currying at the type level.

Beyond ordinary type classes, Haskell supports advanced type-level programming. Type families are type-level functions mapping a container type to its element type, and DataKinds promotes ordinary data constructors to the kind level, enabling type-level naturals and length-indexed vectors. Singleton types bridge the value level and the type level, with exactly one inhabitant per type-level tag, allowing runtime reflection of compile-time information. These features culminate in dependently typed programming, where types can depend on values: a vector of length \( n \) carries \( n \) as both a runtime parameter and a type-level index, enabling proofs-as-programs and refined types. Idris is a dependently typed functional language with Haskell-like syntax, totality checking, and compilation to native code, JavaScript, and C. The \( \text{MonadFail} \) class restricts pattern-match failures in do-notation to monads like Maybe and lists, keeping IO pure.

All chapters
  1. 1Foundations of Functional Programming
  2. 2Functions and Higher-Order Programming
  3. 3Data Structures and Types
  4. 4Recursion, Folds, and Evaluation
  5. 5Functors, Applicatives, and Monads
  6. 6Type System Concepts
  7. 7Lambda Calculus and Category Theory
  8. 8Real-World Functional Programming and Concurrency

Drill it

Reading is not remembering. These come from the Functional Programming deck:

Q

What is functional programming?

Functional programming is a declarative programming paradigm that treats computation as the evaluation of mathematical functions. It emphasizes immutability, pu...

Q

What is a pure function?

A pure function is a function that: Always returns the same output for the same input (deterministic)Has no side effects — it doesn't modify external state, per...

Q

What are side effects in functional programming?

Side effects are any observable changes a function makes beyond returning a value. Examples include: Modifying a global or external variableWriting to a file or...

Q

What is immutability?

Immutability means that once a data structure is created, it cannot be changed. Instead of modifying data, you create new copies with the desired changes. Benef...