0

Since Name has a Show instance and strings can be used to generate a corresponding Name, I thought about using the following method:

 \ name -> [e| mkName $(lift (show name)) |]

This works fine in ghci. But in compiled code, the actual compiled variables look like p_a4hEk, whereas the strings produced by Show look like p_6989586621680030942.

What is happening here? Is there a way to lift a Name into a Template Haskell ExpQ?


Use case: I am writing a TH API that can build complex and efficient Haskell functions based on human-readable user descriptions. I am attaching to the returned function a documentation that explains the purpose and type of each parameter in human readable terms. It would be nice if the documentation could also contain the Name used for that parameter in the code, so that the user can review the spliced code as needed and recognize which parameter is which.

Theelepel
  • 193
  • 6
  • You shouldn't really rely on the `Show` instances for `Exp` or `Name` or any other TH syntax for anything except debugging. If you need a string representation of the AST for any purpose, your best bet is probably to use the [pretty printer](https://hackage.haskell.org/package/template-haskell-2.12.0.0/docs/Language-Haskell-TH-Ppr.html#v:pprint). But I don't see how this is really applicable to the use case - if you splice in code with TH, you shouldn't see the source (and hence any variable names) at all. – user2407038 Sep 11 '17 at 15:38
  • I am not printing the TH abstract syntax datatypes; I am printing the spliced expressions as shown by ghci or -ddump-splices. The TH code describes a model in DSL, while the spliced code can be read as a mathematical formula. I use the TH code because it is easier to read and edit, but sometimes I need to check the resulting spliced code for sanity checking and communicating with coworkers. It would be useful if I could explicitly link the variable names in the spliced code to their description-in-words in the model. – Theelepel Sep 11 '17 at 16:07

0 Answers0