In Haskell if I import a module e.g.
import Data.List
How can I know what is the total method that Data.List made available ?
In Prelude I can use completion like said here Is there a way to see the list of functions in a module, in GHCI?::
Prelude> :m +Data.List
Prelude Data.List> Data.List.<PRESS TAB KEY HERE>
But I want to get this in a list that be can manipulate, not in Prelude.
This question is not about builtins how to know in Haskell the builtins functions?, (I mean by builtins available functions we have without doing any import)
We can list down all the functions present in a Python module by simply using the dir() method in the Python shell or in the command prompt shell.
dir() is a built-in function that also returns the list of all attributes and functions in a module.
To import all functions in a script, use * . This imports all the functions defined in script_in_cwd.py . If script_in_cwd.py has import statements, then * will also import those libraries, modules, and functions. For instance, if script_in_cwd.py had import numpy , then the above statement would also import numpy .
You can use browse:
Prelude> :browse Data.List
It will list all the methods available
From your Haskell program you can call ghc-mod. It is a standalone program that is able to do what you want:
eg in the terminal the command ghc-mod browse Data.List
returns
all
and
any
break
concat
concatMap
cycle
...
If you need the types of the functions you can use ghc-mod browse -d Data.List
. It returns:
all :: Foldable t => (a -> Bool) -> t a -> Bool
and :: Foldable t => t Bool -> Bool
any :: Foldable t => (a -> Bool) -> t a -> Bool
break :: (a -> Bool) -> [a] -> ([a], [a])
concat :: Foldable t => t [a] -> [a]
concatMap :: Foldable t => (a -> [b]) -> t a -> [b]
cycle :: [a] -> [a]
delete :: Eq a => a -> [a] -> [a]
...
You can install ghc-mod with cabal. To call ghc-mod from your Haskell program, you can follow the answers to this SO question. The preferred waŷ is to use the shelly library.
Here is a small demo program:
{-# LANGUAGE OverloadedStrings #-}
import Shelly
import qualified Data.Text as T
main :: IO ()
main = shelly $ silently $ do
out <- run "ghc-mod" ["browse", "-d", "Data.List"]
-- lns will containes a list of lines with the function names and their types
let lns = T.lines out
-- Here we print out the number of functions and the first 5 functions
liftIO $ putStrLn $ show $ Prelude.length lns
liftIO $ mapM_ (putStrLn .T.unpack) $ take 5 lns
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With