Skip to content

Type-Driven Design

Type-driven design, designing with types, and domain modeling with types use the type system as a software-design tool: modeling states, APIs, workflows, errors, invariants, boundaries, and state transitions.

The core idea

A value moves from a weaker type to a stronger one once something about it has been established:

string → Email
string → UserId
number → PositiveQuantity
DTO → ValidatedOrder
AnonymousUser → AuthenticatedUser
DraftOrder → SubmittedOrder
SubmittedOrder → PaidOrder

The type records what has already happened and what operations are legal now.

An analogy is the documents you collect at an airport. Check-in gives you a boarding pass, security stamps it, and the gate asks only for the stamped pass, not for your booking, your passport and proof that you went through security. Each document is evidence that a step happened, and it’s what makes the next step possible. DraftOrder -> SubmittedOrder -> PaidOrder works the same way: you can edit only a draft and refund only a paid order, and the compiler plays the gate agent. Modeling such transitions is the subject of Workflows And States.

This is the software-design use of typed programming.

The recurring idea is:

untrusted / weakly typed value
          │
          │ runtime computation establishes P(x)
          ▼
   value carrying evidence of P
          │
          │ static type checker preserves
          │ that evidence through the program
          ▼
     domain computation

Scala’s opaque type + smart constructor is a relatively lightweight version of this idea. Refinement and dependent type systems push more of P(x) itself into what the compiler can reason about.

Costs and failure modes

The approach has costs, and it can go wrong: conversion code at every boundary, types that lie because something constructs them without checking, facts that expire after they’re checked, modeling too precisely before the domain is understood, and encodings harder to read than the invariant they protect. These are worked through, with sources, in What Belongs In Types; the guard against most of them is the What Belongs In Types.

Notes in this section

The notes sit in five subfolders, in reading order; each folder has an index. Start with Reading paths to pick a route through the sources.

Foundations

Folder index. What a type system proves, and what to put in one.

  • Type systems: Pierce’s definition, soundness and conservativity, and what type systems buy.
  • Dimensions of type systems: static, dynamic and gradual checking; safety and what “strong” means; nominal and structural equivalence.
  • Type inference: inference as ergonomics, and its limits.
  • Types as propositions: the Curry-Howard correspondence and what it means for ordinary code.
  • What belongs in the type system: types, contracts, runtime checks and tests; good targets; a decision procedure; where types stop matching reality; costs and failure modes.

Parsing and evidence

Folder index. How a value earns a stronger type.

  • Parse, don’t validate: shotgun parsing, partial functions made total, where to parse, and where it gets harder.
  • How strong the evidence is: constructive and encapsulated types, names that are not type safety, hidden constructors, ghosts of departed proofs.
  • Types at the boundary: trust boundaries, DTOs and serialization, the database as a boundary, evolving types, and tolerant readers.

Domain modeling

Folder index. Types that say which values and states can exist.

Architecture

Folder index. From a typed domain to a program.

  • Functional domain modeling: Ghosh’s principles, keeping effects out of the domain, modules as algebras with laws, composing effectful operations, commands and events.
  • Effects, interpreters and wiring: Reader and Kleisli, free monads, ZIO, algebraic effects, and embedded DSLs.
  • Tagless final: the encoding, rules for using it, two critiques, and the expression problem.
  • Declarative design: declarative specifications and engines, four senses of “declarative”, DSLs, and semantic modeling.

Stronger type systems

Folder index. Predicates in types, types that depend on values, and Scala.

  • Refinement types: predicates in types, how the checking works, and limits.
  • Refinement types by language: what Python, TypeScript, Haskell, Rust, Ada and Scala offer, and Scala’s terminology trap.
  • Towards dependent types: the progression from ordinary to dependent types.
  • Scala type system: abstract types and the currency case study, nominal and structural subtyping, union and intersection types, path-dependent types, type tags, multiversal equality, and encoding contracts in Scala.

Across the section

  • Reading paths: a map of every source with where it’s held, and curricula for practical type-driven design, runtime checks and type evidence, and type theory.
  • Provenance: raw input, related private material, and the history of the notes.

Sources in the library

Every library source cited in this section, grouped by kind, with the notes and sections that use it. The Library sources list at the end of each note gives the reverse view, with what each source covers.

Books

Papers, articles and chapters

Slides

Named in the notes, not in the library

Works the notes recommend or quote that have no local copy, in the library or elsewhere on this computer:

  • Books: Edwin Brady, Type-Driven Development with Idris; Vlad Riscutia, Programming with Types; Dan Vanderkam, Effective TypeScript; Jon Gjengset, Rust for Rustaceans; Sandy Maguire, Algebra-Driven Design; Friedman and Christiansen, The Little Typer (all in Reading Paths); Miran Lipovača, Learn You a Haskell for Great Good! (quoted in Domain Modeling With Types).
  • Free online material, linked from Reading Paths but not saved: Scott Wlaschin’s Designing with Types series, Software Foundations, Programming Language Foundations in Agda, and Niki Vazou’s Liquid Haskell course.

Sections inside larger notes:

Applied in projects:

Explore this section

  • Books, papers and courses on type-driven design grouped by goal: a map that lists each source once with what it is for, then reading paths for practical design, runtime evidence and type theory.

Type-Driven Design: Architecture

Organising a typed domain into a program: functional domain modeling with algebras and interpreters, ways to wire them (Reader, tagless final, free monads, ZIO, algebraic effects), and declarative design.

Browse 4 notes
  • Declarative specifications and execution engines, four senses of \"declarative\", the connection to functional programming, DSLs, model-driven engineering and ontologies, and key contributors.

  • Supplying interpreters for domain algebras and deciding where effects happen: Reader and Kleisli, free monads, ZIO and algebraic effects, compared by what their types reveal, plus initial and final embedded DSLs.

  • A domain model built from pure functions over domain types, with side effects kept out of the domain logic and behavior described as algebras with separate interpreters.

  • Tagless final as a typed encoding of algebras and interpreters: its origin in typed EDSLs, the expression problem, Volpe's and Parsons' rules for production code, and two critiques.

Type-Driven Design: Domain Modeling

Modeling a domain so that its types state which values and states can exist: type-first design, newtypes, algebraic data types, illegal states made unrepresentable, values and entities, errors, workflows and state machines.

Browse 5 notes
  • Combining types with AND (product types) and OR (sum types), counting a type's values as a design check, and sum types versus tagged classes and class hierarchies, with Wlaschin, Kurt, Milewski, Parsons, Płachta, Bloch and Ghosh.

  • An overview of modeling a domain with types, following Wlaschin and Kurt: type-first design, how the vocabulary translates across languages, values, entities and aggregates, and errors as values.

  • Designing types so that a value breaking a business rule can't be built: verified emails, \"at least one of\", connascence of value, booleans, a flag plus an optional value, and behaviour as data.

  • Primitive obsession and the newtype pattern, Scala value classes and opaque types, and simple values whose constructors enforce a rule.

  • Modeling a value that moves through states: a workflow as a function type, one type per state, state machines, typestate encodings with phantom types, and long-running workflows.

Type-Driven Design: Foundations

What a type system is and proves, the dimensions along which type systems differ, inference, types as propositions, and how to decide what belongs in the types at all.

Browse 5 notes
  • The axes along which type systems differ: static, dynamic and gradual checking, safety and what \"strong\" means, and nominal versus structural equivalence.

  • Type inference as ergonomics rather than a guarantee: what inference saves, where it stops (System F, type families, refinement qualifiers), and why signatures are still worth writing.

  • What a type system is, starting from Pierce's definition: soundness, conservativity, the four parts of the definition, and what type systems buy, from early error detection to documentation and efficiency.

  • The Curry-Howard reading of types: types as propositions and programs as proofs, what it means for ordinary code, and how far each kind of type system takes it.

  • Choosing between types, contracts, runtime checks and tests: what makes a good type-system target, a decision procedure, where types stop matching reality, and the cost of stronger guarantees.

Type-Driven Design: Parsing and Evidence

Turning untrusted input into types that carry what was checked: parse, don't validate, how strong a parsed type's evidence is, and the trust boundaries where data enters and leaves.

Browse 3 notes
  • How much a parsed type guarantees: smart constructors and newtypes, constructive versus encapsulated types, names that are not type safety, constructors that bypass the check, and evidence without runtime cost.

  • Alexis King's principle: parse input at the boundary into types that keep what was checked, instead of validating and passing on weak types. Shotgun parsing, partial functions made total, and where parsing gets harder.

  • Where the trust boundaries are, how domain types map to the shapes that cross them (DTOs, rows and messages), and what happens when those shapes change while old data stays where it was written.

Type-Driven Design: Stronger Type Systems

Type systems that say more than ordinary ones: refinement types and how they are checked, what each language offers, the step to dependent types, and the Scala type system's features for correctness.

Browse 4 notes
  • Refinement types as base types restricted by predicates: why bother, how SMT-based checking and Liquid inference work, and the limits of the approach.

  • What Python, TypeScript, Haskell, Rust, Ada and Scala offer in place of, or as, refinement types, and Scala's terminology trap: its own \"refinement types\" mean something else.

  • The Scala type features type-driven design relies on: abstract type members, nominal and structural subtyping, union and intersection types, path-dependent types, multiversal equality and contracts.

  • The progression from ordinary types through refinement types to dependent types, the two senses of \"dependent\", and how Haskell, Scala and Pierce's Dependent ML approach it.