6

The :browse, :info and :type GHCi commands are very convenient.

Is it possible to get the same information programmaticaly in a Haskell program? That is, to get the exported functions from a module, the types of stuff, etc.

Lii
  • 11,553
  • 8
  • 64
  • 88
  • 1
    Eh, sorry? What? I have been looking around with Hoogle and Google and not found anything. – Lii Feb 18 '12 at 16:48
  • 2
    You can use the GHC API. I'm not aware of a simpler way. – Daniel Fischer Feb 18 '12 at 16:56
  • 1
    I can imagine there _might_ be a TH-based solution..? – Louis Wasserman Feb 18 '12 at 18:17
  • Thanks for you answers. Ill go a head and post another question, for the solution of the original problem: Is there are generated sources for Emacs auto completion for Haskell? – Lii Feb 18 '12 at 18:32
  • Ah, the GHC API of course. Fiddly, but seems possible. Thanks! This would be marked as the solutions was it not a comment. – Lii Mar 02 '12 at 12:47
  • 1
    This might be a good lead: http://stackoverflow.com/questions/12075530/how-can-haskell-quasiquotation-be-used-for-replacing-tokens-on-the-haskell-level – worldsayshi Jan 24 '14 at 21:57

3 Answers3

5

:browse - when a Haskell program is compiled, no (useful) information is kept about which module something came from, so your program wouldn't be able to access that information.

:type - Unless you're using Data.Typeable, types aren't visible at all at runtime. Types in Haskell are mostly for the compiler to check to correctness/safety of code.

:info - See above.

amindfv
  • 8,438
  • 5
  • 36
  • 58
  • It should be possible to read that static info from the .hi interface files. – Lii Feb 18 '12 at 17:10
  • Hm, maybe only :browse would make sense to do on static info in a .hi file. – Lii Feb 18 '12 at 17:17
  • The reason that this answer is not relevant is that I am not interested in a program that manipulates its own meta info, but a program that reads the info of a module that is stored on disk. This information is stored in the `.hi` files, and can be read. – Lii Mar 10 '12 at 09:34
3

for getting functions of a module at compile time - the language-haskell-extract package could be interesting to you. It helps you extracting functions according to a regular expression.

http://hackage.haskell.org/package/language-haskell-extract-0.2.1

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
  • The functions in that package extracts the names of the functions of the calling module. That might be useful sometimes, but it does not solve my problem. – Lii Mar 02 '12 at 12:27
0

Daniel Fischer commented:

You can use the GHC API. I'm not aware of a simpler way.

Seems to be fiddly but to work fine. And I guess this is how :info works in GHCi. Thanks for the suggestion.

Community
  • 1
  • 1
Lii
  • 11,553
  • 8
  • 64
  • 88