Search code examples
haskellghcihaskell-mode

How to print the constructors of a type in GHCi?


Using :t I can print the type of an expression. But how do I see the constructors of that type? Also is there a shortcut for this in Haskell mode for emacs?

This seems like a basic thing, but I can't find it. Maybe I'm just searching the wrong terms...

In general, what is the easiest way to see the print the definition of a type without having to go to the documentation.


Solution

  • :info, or just :i, is what you want:

    >>> :i Either
    data Either a b = Left a | Right b  -- Defined in ‘Data.Either’
    ... plus all of Either's instances
    

    From the GHCi documentation:

    :info name ...

    Displays information about the given name(s). For example, if name is a class, then the class methods and their types will be printed; if name is a type constructor, then its definition will be printed; if name is a function, then its type will be printed. If name has been loaded from a source file, then GHCi will also display the location of its definition in the source.

    Haskell-mode in Emacs has haskell-process-do-info (source), for which one Haskell on Emacs Tutorial recommends setting the keybinding C-c C-n C-i.

    (defun haskell-process-do-info (&optional prompt-value) 
      "Print info on the identifier at point.
    If PROMPT-VALUE is non-nil, request identifier via mini-buffer."
    ...
    )