Today we will take a look at the changes that were made in GHC 9 regarding typed Template Haskell (TTH) and how to use the th-compat library to write TTH code that will work with both GHC 8 and GHC 9.
In our previous blog post, we gave an overview of typed Template Haskell in GHC 8. The article was later amended with the changes required so that the examples compile in GHC 9 in a non-backward compatible way. Make sure to read that post before you continue!
The template-haskell package was changed in version 184.108.40.206 and GHC version 9.0 according to the Make Q (TExp a) into a newtype proposal, in which the typed expression quasi-quoter ([|| ... ||]) now returns a different datatype.
Under the new specification, typed quotations now return Quote m => Code m a instead of Q (TExp a), and typed splices expect this new type as well. This is a breaking change for existing code, and codebases targeting GHC 9.0 either need to adapt their TTH code or use an alternative approach for compatibility.
The actual definition is a bit more complicated, as it contains some levity-polymorphism mechanisms. If you go to its definition, you will actually see this: