Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Haskell deal with documentation?

How do I get online 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
like image 233
aXqd Avatar asked Sep 13 '10 09:09

aXqd


2 Answers

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.

like image 92
sastanin Avatar answered Sep 27 '22 19:09

sastanin


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.

like image 29
ShinNoNoir Avatar answered Sep 27 '22 18:09

ShinNoNoir