Hi Mads,

I was wondering if anybody had experimented with using Template Haskell
(TH) and ordinary SQL to make type-safe database access?

I know HaskellDB, for example, does something quite similar. There's a preprocessor that generates a Haskell file with a Haskell representation of the types of the database's tables. You could of course replace this with a TH function. There are two very nice papers about the design of HaskellDB:

http://research.microsoft.com/users/daan/download/papers/dsec.ps

http://haskelldb.sourceforge.net/haskelldb.pdf

I think there may a bit of problem with the approach you suggest: as the type returned by the query is computed by the SQL server (if I understand you correctly), it's very hard to do anything with the result of the query - the Haskell compiler has no idea what type the result has, so you can't do anything with it. I think it makes much more sense to bookkeep type information on the Haskell side.

Hope this helps,

  Wouter