Experiments with Template Haskell, SQL, and Records

The problem of aligning Haskell types with a database schema has been solved from two different ends:

  1. persistent has a quasi-quoter that generate both the schema and the Haskell types for you, including encoders/decoders, from one definition. Since the query interface arising from that is not as easy to work with as it could, persistent has come out of fashion.
  2. pGenie takes an existing database schema as first-class citizen and generates the Haskell types from that.

Your idea of parameterizing the record types is also found in opaleye. They also implicitly use HKD by instantiating all rank-0 parameters with type applications of the form f a uniformly in f but varying in a.

For those who, for some reason, wish to never write any SQL at all, a compatibility shim between persistent’s convenient quasi-quoter (generating monomorphic types) and opaleye’s wonderful parametric record types would be handy.

2 Likes