[Haskell-cafe] Using Template Haskell to make type-safe database access

Wouter Swierstra wss at Cs.Nott.AC.UK
Thu May 8 05:02:12 EDT 2008

Hi Mads,

> Not only pictures, but also code can say more than a thousands words.
> Therefore, I have been implementing a proof of concept. The code is
> attached in two files.

Nice! I have to admit, it's much nicer than I expected it to be. Just  
out of curiousity, what happens when you write:

selectTupleList :: Connection -> IO [Integer]

instead of

selectTupleList :: Connection -> IO [(Integer, String, String)]

What kind of error message do you get? More specifically, is this  
error caught statically or dynamically.

The only other limitation I can think of, would be in the situation  
where you don't have compile-time access to the database, e.g.  
developing software for a client with a database that can only be  
accessed from their intranet. I have no idea how much of a limitation  
that is.

> ok, there is some noise. But at the end of line three it says "Unknown
> column 'duser_id'". Also with a little more work I could properly
> improve the output.

The message is a bit verbose, but if you ignore the noise, it gives  
pretty good feedback about what's wrong. Good.

>> Perhaps I should explain my own thoughts on the subject a bit better.
>> I got interested in this problem because I think it makes a nice
>> example of dependent types "in the real world" - you really want to
> But won't you end up implementing all the functionality of an SQL
> parser? While possible, it does seem like a huge job. With a TH  
> solution
> you will safe a lot of work.

Yes - but parsing the result of an SQL describe statement is pretty  

> A library that
> will be a lot more complex to learn than what I am proposing (assuming
> the developer already knows SQL).

Hmm. This is a rather sticky point. One might also argue that Haskell  
developers have to learn SQL to use the solution you propose. I'm not  
particularly convinced. Both approaches have their merits I think.

Anyhow - nice work! Have you asked Bjorn Bringert what he thinks? He's  
a really clever and approachable guy - and he knows a lot more about  
interfacing with databases than I do.

Kind regards,


This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

More information about the Haskell-Cafe mailing list