In Haskell, how can I get a list of available functions for a data structure?

Viewed 338

I know I'm asking the wrong question here, but I'm coming to Haskell from Python, and I'd like to know how to get what would be the equivalent of a list of available methods for a class. For example, if I'm using HXT to parse an XML file, and there are some functions I can use on the resulting data structure, NTree, like the function getChildren, for instance, how would I go about getting a list of them from, say, ghci? In Python I can just import a module and type module. to get a list.

2 Answers

In Python, you can use dir to learn about the methods and fields of an object or function. There is no analogue in Haskell. :info is occasionally useful when used on a type or constructor, but its output is spare compared to Python's dir.

In Python, you can use dir to learn about the functions, classes, and values defined by a module. In Haskell, you can use :browse in ghci to do the same.

In Python, you can use help to get some programmer-written text describing a function or other object. In Haskell, you can browse the Haddocks on Hackage to do the same. There is a tool that ostensibly brings this capability to ghci, assuming you have the appropriate documentation installed locally, but it is not well maintained, and has broken for me several times.

https://www.haskell.org/hoogle/ can help a bit. Give it a module name or a desired signature.

Classes in Haskell are unlike Python's. Python's class instances are collections of partially-applied functions (bound to self). Haskell classes are more like interfaces from Java or even Go: if something conforms to a list of function signatures, it can "belong to the class".

Functions applicable to data defined in a module are usually described in that module. But a data item can also conform to other interfaces aka typeclasses (like Foldable, Traversable, Applicative, etc), and all functions defined for these typeclasses are also applicable.

More than that, you can define a typeclass in your own module and describe something already existing to conform to it by writing an implementation of the required functions. This makes finding "all applicable functions" even more context-dependent.

Related