Haddock option to include type signature?

Newbie question:

I like the ghci :set -haddock option that allows :doc to display function descriptions that has embedded in {- | ... -} comments. I also like using :type ... to show the type of an expression. Is there an option to cause Haddock to add the type signature to the comment displayed with :doc, or is there something to use other than :doc that would have the effect of displaying both the type and the description? It’s inconvenient to have to use :type and :doc separately. Or is there a way to define an abbreviation in ghci for “run :doc and :type on the same name”?

In my own code, I’ve been adding the type signature to the Haddock comments, but that’s redundant, and could accidentally lead to the documentation being out of sync with the function.

None of this is a big deal, but several minutes of searching turned up no answers, so I thought it wouldn’t hurt to ask.

1 Like

If you run hoogle in your browser, its doc will return the signature

-- cabal install hoogle, update the dc and slap this in your ~/.ghci
:def hoogle \x -> return $ ":!hoogle -q --colour -n 10 \"" ++ x ++ "\""
:def hdoc \x -> return $ ":!hoogle --info \"" ++ x ++ "\""


> :doc reverse
 'reverse' @xs@ returns the elements of @xs@ in reverse order.
  @xs@ must be finite.
> :hdoc reverse
reverse :: [a] -> [a]
base Prelude
reverse xs returns the elements of xs in
reverse order. xs must be finite.
1 Like

Or even simpler, with just a macro:

λ> :def prova4 \x -> return $ ":type " ++ x ++ "\n :doc " ++ x
λ> :prova4 head
head :: [a] -> a
/O(1)/. Extract the first element of a list, which must be non-empty.
-- again slap :def … in your .ghci

But I still prefer how Hoogle output is cleaner; and of course you can search by signature too with Hoogle:

:def hoogle \x -> return $ ":!hoogle -q --colour -n 10 \"" ++ x ++ "\""
λ> :hoogle (a, b) -> (b, a)
Data.Tuple swap :: (a, b) -> (b, a)
Data.Tuple.HT swap :: (a, b) -> (b, a)
Data.Tuple.Lazy swap :: (a, b) -> (b, a)
1 Like

Thanks @f-a !

The last suggestion will be useful for the code I’m working on. I’m playing with complex data structures, and at this stage of development of the code, I sometimes have trouble keeping straight what function does what. I also need to check what Haskell thinks the types are, which is often not what I intended.

The Hoogle definitions will be very cool and useful for library functions. Being able to search Hoogle from the REPL is very nice.

Thanks for actually writing the code for me. I would have no idea how to do that at this point in my learning process.

1 Like