Trivia at type-level
The more expressive the types are, the more safety we can ensure at compile-time. What's more, expressive types serve as documentation that is always up-to-date.
Phantom types
A type that has type variables on the left-hand side that do not appear on the right-hand-side at all is called a phantom type. Such type variables are a cheap, in fact free, technique to guarantee correctness in multiple situations. An example from base is Data.Fixed
, in which the precision of fixed-precision arithmetic is encoded in a phantom type.
An extremely useful class of phantom types is obtained in conjunction with Generalized Algebraic Data Types (GADT). As a little silly example, consider:
-- file: gadts.hs {-# LANGUAGE GADTs #-}data Value a where ...
Get Haskell High Performance Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.