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?


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’
    $
    

    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.


    If you want this kind of Haskell code browsing support in vim , you might try Stephen Diehl's nice tutorial Vim and Haskell in 2016.

    There are a variety of editing environments for Haskell that allow you to query the GHC compiler for information about the code. For example, I use vim-hdevtools, and I can open the file hakyll/src/Hakyll/Main.hs in vim , move the cursor over the Commands.build symbol, enter command :HdevtoolsInfo , and then GHC will tell me the type of the symbol and where it is defined, and give me the option to jump to definition.

    Commands.build ::
      Config.Configuration -> Logger.Logger -> Rules a -> IO ExitCode
            -- Defined at hackyll/src/Hakyll/Commands.hs:48:1
    

    However, I confess that I did spend a lot of time getting vim-hdevtools to work.

    链接地址: http://www.djcxy.com/p/80780.html

    上一篇: Haskell与现实世界中的程序编程

    下一篇: 在Haskell中查找函数源(工作流)