4

I'm looking for a way to compile a string of a valid Haskell expression code into a TH Exp.

E.g., if there existed an appropriate function, I'd expect it to behave the following way:

> $(theFunctionImLookingFor "\a -> a + 1") 2
3

I've already looked for the implementation of quoteExp :: String -> Q Exp for the underlying QuasiQuoter of [e|..|], but it seems to be magic and there is none.

Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
  • 2
    It can be done, but only on strings known at compile time. Given that constraint, why not just use a quasiquote? – jamshidh Dec 13 '13 at 01:56
  • Since we're talking about TemplateHaskell, of course it's compile time. I need this for an implementation of a custom QuasiQuoter. One of its jobs is to just store the result of the quoted Haskell expression. – Nikita Volkov Dec 13 '13 at 02:39
  • Got it, makes more sense. I have one more question though.... Could you just wrap the builtin expression quasiquoter, just pass quoteExp through with the extra (unsafe, I am guessing) IO to log the result? Or are there enough other changes to how the builtin quoteExp works to warrent a rewrite? – jamshidh Dec 13 '13 at 02:47
  • As mentioned, I couldn't find this "built in" quoteExp. It seems like it's magic. – Nikita Volkov Dec 13 '13 at 02:56
  • 3
    The function you are looking for is `Language.Haskell.Meta.Parse.parseExp` in `haskell-src-meta`. – user2407038 Dec 13 '13 at 05:49
  • @user2407038 That's it! Thanks! If you post it as an answer, I'll accept it. – Nikita Volkov Dec 13 '13 at 13:46

1 Answers1

4

The function you are looking for is parseExp from the Language.Haskell.Meta.Parse module in the haskell-src-meta package.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
user2407038
  • 14,400
  • 3
  • 29
  • 42