how to list the functions exported by a Haskell module from an .hs script?

Viewed 286

I am aware of this thread and the agreed-upon ghci :browse command, but I am looking for something similar to run from a script.hs file:

Say I have a module that I can import into my script.hs. How do I then view the list of functions I have just gained access to?

What I've settled on for now

Adapting this thread that suggests the now-deprecated ghc-mod command-line program, I am

  • calling the terminal command ghc -e ':browse <module, e.g. Data.List>'
  • from my script.hs using Shelly.

My full script:

#!/usr/bin/env runghc

{-# LANGUAGE OverloadedStrings #-}
import Safe (headDef)
import Shelly
import System.Environment (getArgs)
import qualified Data.Text as T

mdl :: IO String
mdl = getArgs >>= return . headDef "Data.List"

runShelly :: String -> IO ()
runShelly mdl = shelly $ silently $ do  
    out <- run "ghc" ["-e", T.pack (":browse " ++ mdl)]
    let lns = T.lines out
    liftIO $ mapM_ (putStrLn .T.unpack) $ lns

main :: IO ()
main = mdl >>= runShelly

This way I can pass the module name on the command line as <script> <module> and get back the functions, one per line. It defaults to Data.List if I pass no arguments.

So that's a solution, but surely there must be handier introspection facilities than this?

0 Answers
Related