Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know what are the full list of function availlable in an import?

Tags:

list

haskell

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)

like image 299
user3313834 Avatar asked Aug 02 '17 14:08

user3313834


People also ask

How do I get a list of all functions in a Python module?

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.

Which library function returns the list of all functions in a module?

dir() is a built-in function that also returns the list of all attributes and functions in a module.

How do you import all functions in Python?

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 .


2 Answers

You can use browse:

Prelude> :browse Data.List

It will list all the methods available

like image 128
A Monad is a Monoid Avatar answered Oct 16 '22 13:10

A Monad is a Monoid


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
like image 30
Jogger Avatar answered Oct 16 '22 15:10

Jogger