[Haskell-beginners] Data declarations
aditya siram
aditya.siram at gmail.com
Fri Jun 3 05:58:10 CEST 2011
> data KeyValuePair a b = KeyValuePair String String
'a' and 'b' are called phantom types and they are used to tag a
datatype. Rather than explain what that means in some abstract way
I'll just show you how I have used them:
I had the following simple program that retrieved values from and
wrote to a dictionary data structure (like Oxford's or Webster's).
Here are the types in Haskell:
type Name = String
type Definition = String
type Dictionary = [(Name,Definition)]
The actual contents of the dictionary were stored in a SQLite database
so I needed to read them into the "Dictionary" type whenever I started
up the program.
And I had a number of functions that queried the "Dictionary" that
required on a populated dictionary and I wanted to check *at compile
time* that they would only be called on a populated dictionary. So I
added the following two datatypes:
data Populated = Populated
data New = New
I then changed my "Dictionary" to take an extra argument that is never
used just like your "KeyValuePair":
type Dictionary a = [(Name,Definition)]
Now I could write a function that populated the dictionary:
populateFromDatabase :: Dictionary New -> IO (Dictionary Populated)
and query functions:
getDefinition :: Name -> Dictionary Populated -> Definition
Since "populateFromDatabase" is the only function that can create a
"Dictionary Populated" the phantom type statically guarantees that
"getDefinition" and any other function that expects a full dictionary
can never be called before "populateFromDatabase".
The Haskell Wiki has a similar example [1] and Richard Jones has a
nice article on they are used in Ocaml [2] .
hth,
-deech
[1] http://www.haskell.org/haskellwiki/Phantom_type
[2] http://camltastic.blogspot.com/2008/05/phantom-types.html
More information about the Beginners
mailing list