Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find function source in Haskell (workflow)

I'm learning Haskell for a while, so I've decided to inspect some popular project to get a feeling how it looks like in reality and perhaps reverse-engineer the process.

I've picked Hakyll because it does something I'm familiar with and is moderately complex. And then I've stucked immediately with the question: how to backtrace imports?

Say, In JavaScript every import is explicit.

let Q = require("Q")         // namespace
let {foo} = require("Q/foo") // value

Haskell defaults to

import Q 

which spoils everything at once and people seem to really abuse it.

Now I look at the tutorial, then at the source and realize I have no clue where one or another function is located and no clue how to discover it except searching.

Is there some trick to discover this kind of information like making syntax error which would reveal source file or whatever? How do you solve such tasks in your workflow?

like image 974
Ivan Kleshnin Avatar asked Jun 22 '16 07:06

Ivan Kleshnin


2 Answers

Some options which do not require compilable code:

  • use hayoo
  • use hoogle at Stackage.org

I recommend Stackage's hoogle since its database is loaded with all of Stackage.

If you have working code:

  • use the :i command in ghci
  • use ghc-mod through your editor or command line

As an example of the last two options, suppose that your have a module Foo.hs which looks like:

module Foo where
import Data.Maybe
...

Using ghci:

$ ghci Foo.hs
GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Foo              ( Foo.hs, interpreted )
Ok, modules loaded: Foo.
*Foo> :i catMaybes
catMaybes :: [Maybe a] -> [a]   -- Defined in ‘Data.Maybe’
*Foo>

Using ghc-mod:

$ ghc-mod info Foo.hs catMaybes
catMaybes :: [Maybe a] -> [a]   -- Defined in ‘Data.Maybe’
$
like image 59
ErikR Avatar answered Nov 15 '22 08:11

ErikR


If you use Intero for Emacs, it supports the standard "go to definition" shortcut of M-..

Other Intero frontends probably use the standard UI of their editor for this functionality as well; I know there's a NeoVim frontend already.

like image 42
Cactus Avatar answered Nov 15 '22 06:11

Cactus