43

I find it handy in Python or Common Lisp that you can list a library's contents at runtime. Does Haskell have the same thing, in particular from a GHCI prompt?

2 Answers 2

68

GHCi has a :browse command to list the contents of modules:

Prelude> :browse Data.List
(\\) :: (Eq a) => [a] -> [a] -> [a]
delete :: (Eq a) => a -> [a] -> [a]
deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
elemIndex :: (Eq a) => a -> [a] -> Maybe Int
...
Prelude> :help                    
...
   :browse[!] [[*]<mod>]       display the names defined by module <mod>
                               (!: more details; *: all top-level names)
...
12

Depending on exactly what information you intend to extract... If your version of GHCi supports tab-completion, then you can use that to list all of a namespace's available functions:

Prelude> :m +Data.List
Prelude Data.List> Data.List.<PRESS TAB KEY HERE>
Display all 109 possibilities? (y or n) <PRESS n>
Prelude Data.List> Data.List.un<PRESS TAB KEY HERE>
Data.List.unfoldr  Data.List.unlines  Data.List.unzip3   Data.List.unzip6   
Data.List.union    Data.List.unwords  Data.List.unzip4   Data.List.unzip7   
Data.List.unionBy  Data.List.unzip    Data.List.unzip5   
3
  • In current ghci(s) :m +Data.List its deprecated.
    – EliuX
    Mar 17, 2016 at 4:55
  • 3
    @EliuX what's the undeprecated thing to do?
    – user239558
    Apr 1, 2017 at 22:49
  • @user239558 As far as I know, the old syntax is not deprecated. However, there is some recently added alternative syntax: the familiar import statements allowed in modules are also allowed in ghci. So one could also write import Data.List. I suspect EliuX knew about the addition of this syntax to ghci and assumed that the old syntax was deprecated, but I don't believe that to be correct. Indeed, there is no import statement that has the same effect as ghci's :m * syntax, which is frequently used (even if only implicitly), so it would be surprising indeed for :m to be deprecated. Nov 9, 2017 at 3:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.