问题
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)
回答1:
You can use browse:
Prelude> :browse Data.List
It will list all the methods available
回答2:
There is online documentation available, for example here. It is generally best practice to use qualified imports like import qualified Data.List as L
or import Data.List (permutations, foldl')
to avoid this issue, though.
回答3:
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
来源:https://stackoverflow.com/questions/45463664/how-to-know-what-are-the-full-list-of-function-availlable-in-an-import