What Are Phantom Types
Phantom Types come in two forms:
- a data type without any values
- 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
- Use a phantom type to restrict how a developer can use a function
- See "Restricting ArgumentTypes.purs"
- See Pathy, which uses them to track the distinctions of relative/absolute file paths and of file/directory
- 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
- Watch Ghosts of Departed Proofs (not the best video quality, but very interesting ideas)