Generics...(ish)
MR K P SCHUPKE
k.schupke at imperial.ac.uk
Wed Jan 28 13:59:49 EST 2004
I have read the "Scrap Your Boilerplate: A Practical Design
Pattern for Generic Programming" and have worked out how to
do it. This seems much better than using Template Haskell for
this kind of problem.
I used mkQ and extQ to convert from various types that are allowed
in the record structure, to an algebraic enumeration type that codes the
valid types. This allows using "everything (++)" to convert a record
(tuple type) to a List of type-names in an enumeration. This is a very
neet and compact way of doing it.
This enables me to define a database table in haskell (where a record
represents a table with columns of the given types), for example:
data TestTable = TestTable {
column1 :: ColumnType Int,
column2 :: ColumnType Float,
column3 :: ColumnType String
}
type ColumnType a = String -- use phantom types to statically type check sql...
testTable :: TestTable
testTable = TestTable {
column1 = "id",
column2 = "aFloatValue"
column3 = "aString"
}
Using generics I can now generate a list:
[("id",SqlInt),("aFloatValue",SqlFloat),("aString",SqlString)]
which can be used to check the Haskell version of the table against the version
in the database, or create the table if none exists...
This is for a kind of port of the HaskellDB stuff to ghc, although it is more
of a complete reimplementation using the same idea. My aim is to use a monad
transformer, so that db operations can be interleaved with other IO.
Regards,
Keean Schupke.
More information about the Glasgow-haskell-users
mailing list