What Are Phantom Types

Phantom Types come in two forms:

  1. a data type without any values
  2. a generic type that is declared in type's definition that is never used in its data constructors
-- 1. ReadOnly has no values
data ReadOnly

-- 2. `unusedType` is never used in Data's data constructor
data Data unusedType = Data

Why Are PhantomTypes Useful?

See Motivation behind Phantom Types for a quick overview before looking below for some examples.

  • Use a phantom type to restrict what a developer can do with a type
    • ST uses a Region type to prevent local mutation from escaping some scope.
  • Use a phantom type to restrict how a developer can use a function
  • Use phantom types to estimate the state of "the real world" in a monadic computation:
    • Search for an explanation of Indexed Monads
  • Use phantom types to "save" proofs