Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Template Haskell to get the body of function?

Currently I'm trying to do a translation from a Haskell subset without having to deal with all the parsing, typechecking etc. issues. Documentation didn't help me to figure out a function to get the function's body (all the definitions) by its name.

Context for this call should look something like

fac 0 = 1
fac x = z * fac (x - 1)

getBody = ...

main = do
    x <- runQ $ getBody [| fac |]
    print x

Does anyone knows

  1. whether there are some good and up to date docs on TH (not the reference on Hackage) or
  2. how to make getBody?
like image 343
polkovnikov.ph Avatar asked Dec 21 '12 02:12

polkovnikov.ph


1 Answers

In general, the way to find the definition of something with TH is using the reify function. However:

  • You can't use reify at run-time via runQ. The information it needs is not available except during compilation.

  • Currently, using reify to get function definitions is not implemented due to lack of interest anyway.

Looks like you'll need to find another route. Have you considered using the haskell-src-exts package for parsing and/or the GHC API or something based on it?

like image 165
C. A. McCann Avatar answered Oct 23 '22 17:10

C. A. McCann