[Haskell-cafe] carry "state" around ....
ketil at malde.org
Tue Jul 29 02:23:16 EDT 2008
"Galchin, Vasili" <vigalchin at gmail.com> writes:
> ok guys .. what is this "phantom type" concept? Is it a type theory thing or
> just Haskell type concept?
Here's another example. Say you want to use bytestrings with
different encodings. You obviously don't want to concatenate a string
representing Latin characters with a string in Cyrillic.
One way to do this, is to define phantom types for the encodings, and
a bytestring type that takes additional type parameter
data Bytestring enc = MkBS ...
Operations like concat work on same-typed bytestrings:
concat :: Bytestring e -> Bytestring e -> Bytestring e
The parameter (enc) isn't used on the right hand side, so all
Bytestrings will have the same representation, but Bytestring KOI8 and
Bytestring ISO8859_1 will have different types, so although the
runtime won't know the difference, trying to 'concat' them will give
you a type error at compile time.
If I haven't seen further, it is by standing in the footprints of giants
More information about the Haskell-Cafe