views:

182

answers:

3

Any pattern&practise here for documentation in Haskell?
Are there anything as elegant/handy as what Python does below?

>>> help([].count)
Help on built-in function count:

count(...)
    L.count(value) -> integer -- return number of occurrences of value
+6  A: 

Currently, there is no way to view the Haddock documentation within ghci, but there is a ticket for it.

You can however get a small bit of info using the :info command, e.g.

ghci> :i nub
nub :: (Eq a) => [a] -> [a]     -- Defined in Data.List  

so that you at least know where to look for the documentation for a particular function.

ShinNoNoir
Yea, this ticket is exactly what I want, thanks for mention.
aXqd
+6  A: 

You can use Hoogle to search for documentation by function name or its type signature (perhaps, approximate type signature). There's also a command-line offline version of this tool which you can get from hackage.

kohomologie
+12  A: 

Interactive help in GHCi

The standard Haskell REPL is GHCi. While it is not possible to access complete documentation from within GHCi, it is possible to get quite a lot of useful info.

Print types. In 90% of cases this is enough to understand what a function does and how to use it.

ghci> :t zipWith
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

:t is short for :type.

Print information about symbols. This is useful to find what module does a symbol belong. For a data type it allows to see its definition and class instances. For a type class it allows to see its interface and a list of types which are its instances.

ghci> :i Bool 
data Bool = False | True    -- Defined in GHC.Bool
instance Bounded Bool -- Defined in GHC.Enum
instance Enum Bool -- Defined in GHC.Enum
instance Eq Bool -- Defined in GHC.Base
instance Ord Bool -- Defined in GHC.Base
instance Read Bool -- Defined in GHC.Read
instance Show Bool -- Defined in GHC.Show

ghci> :i Eq
class Eq a where
  (==) :: a -> a -> Bool
  (/=) :: a -> a -> Bool
    -- Defined in GHC.Classes
instance (Eq a) => Eq (Maybe a) -- Defined in Data.Maybe
instance (Eq a, Eq b) => Eq (Either a b) -- Defined in Data.Either
(many more instances follow)

ghci> :i zipWith
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
    -- Defined in GHC.List

:i is short for :info.

Print kinds. Use :k on type constructors.

ghci> :k Maybe
Maybe :: * -> *
ghci> :k Int
Int :: *

:k is short for :kind.

Browse module's contents. This allows to see what symbols an imported module offers.

ghci> :browse Data.List
(\\) :: (Eq a) => [a] -> [a] -> [a]
delete :: (Eq a) => a -> [a] -> [a]
deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
...
(many lines follow)

:t, :k and :i work only for symbols in scope (you need to import module with :m + Module.Name first). :browse works for all available modules.

Online documentation

Most Haskell libraries are documented with Haddock. You can open an HTML version of the documentation and read the details.

You can install it locally, if you use --enable-documentation flag in cabal install.

Otherwise, a good point to browse through all the documentation is a package list on Hackage. It allows to see the documentation also for earlier versions of any package. Sometimes it is very useful.

jetxee
I don't know :browse before, which helps a lot, thanks.
aXqd