Sat Aug 6 12:37:15 CEST 2011

Phantom types

Recently while styding haskell I ran into the trick of using phantom
types to "tag" information at compile time.  I.e. a data type

  data Str t = Str String

is internally just a string, but it is possible to use the type
parameter to specify operations like:

  data Blessed
  data Raw

  bless :: Str Raw -> Str Blessed

The function `bless' could then maintain some kind of invariant on
String.  The presence of that invariant can be indicated at compile
time by the type Str Blessed.  Hiding the constructor `Str' will make
it impossible to create a `Str Blessed' data type.  A limited
constructor could be exported to create non-blessed strings:

  str :: String -> Str Raw