What is your recent (>= GHC 9.4) experience using Template Haskell?

Alternatively, if you don’t mind generating code that is not optimized for human readability, you could pre-desugar the do-notation and records like this:

spliceStmts [] k = k
spliceStmts (x:xs) k = [| $x >> $(spliceStmts xs k) |]

spliceRevApp f [] = f
spliceRevApp f (x:xs) = [| $(spliceApp f xs) $x |]

...

[d| 
$(varE 'sqlSelectProcessRow) cols = 
   first ((fromString "Failed to parse " ++ $(lift (nameBase name)) ++ ": ") <>)
   flip evalStateT cols $
     $(spliceStmts statements (spliceRevApp (varE name) (reverse fieldExps)))
|]

Of course there are still some problems that need to be ironed out, like that spliceStmts only supports statements that do not bind variables.

2 Likes

This would only help with one of the CPPs but th-abstraction has a compatibility shim for the BndrVis stuff.

1 Like

My main gripe with TH quotes is that your second example does not work. Splices fit in places in the syntax where you can fit either a type, an expression or a list of declarations [EDIT: or a pattern]. A name is none of those, nor is the left-hand side of a declaration (like here), or an instance head (the XX in instance ... => XX where). Names occur in many places: if you want to generate a bunch of foreign declarations, or a bunch of function declarations, you’re out of luck even if you’re doing absolutely nothing interesting with the syntax, and the generated Haskell code should work to back in the GHC 7 era.

4 Likes

I’ve created an issue for this on the GHC issue tracker here: #24922: Name should be spliceable in TH quotes · Issues · Glasgow Haskell Compiler / GHC · GitLab

1 Like

I’ve managed to eliminate most of these instances of CPP using quotes. See the MR here:

It also simplifies the code quite a bit I think (the diff size is: +44 -140).

There’s a bit of an open problem with quotes for Stmts so I wasn’t able to port some of the code over:

3 Likes

JFUI: starting from the resent GHC release you can use typed quotes in untyped splice and vice versa.

Source: Allow untyped brackets in typed splices and vice versa. (7397c784) · Commits · Glasgow Haskell Compiler / GHC · GitLab

2 Likes