Because of Working towards a more stable Template Haskell, I’m trying to use quotes whenever possible in my template Haskell. Unfortunately, I don’t know what types are expected where in the template haskell
quotes. (e.g. Pat
, Con
, Name
, Expr
) Is there a resource that says for example
in [d| $n :: Int|]
, n :: Q Name
or something? (I’m not actually sure what the expected type of n
is )
I’ve been using AI + autocomplete to muddle through, but I’d like reliable documentation.
Appreciated examples would be something that fills in boxes like the following:
[| newtype $(newtype' :: Q Name) = $(con' :: Q ?) $(type' :: Q ?) deriving anyclass ($(n :: ?))|]
It’s sadly not possible to splice a value of type Name
. See: ghc/#24922
Sometimes you can splice a Name
by turning it into a Exp
, Pat
or Type
.
Splicing is documented here: 6.13. Template Haskell — Glasgow Haskell Compiler 9.15.20260306 User's Guide
So, the only allowed types (currently) are Q Exp
, Q Pat
, Q Type
and Q [Dec]
.
Type errors can be a helpful way if finding out what is expected. If you put something of type Q Exp
where you need a Q Pat
, then the error should tell you.
Sorry for the late response. Thank you, I thought that there were more, as the article led me to believe that quotations could do most of the things that manually creating the AST did. Are there any plans for adding to the supported spices? One day, will I be able to do the type of quotation that I put in my top level post?
It can do most but sadly not all. I would love to extend it to support your use-case. It’s quite an important one! I am vaguely planning to work on this at some point, but my time to work on TH is quite limited sadly (I do it all in my free time).