Seeing Typeclass definition in ghci for a specific type

后端 未结 1 666
庸人自扰
庸人自扰 2020-12-04 01:50

Is there a way to see Typeclass definition in ghci for a specific type?

For example, Maybe is defined like this:

instance Functor Maybe          


        
相关标签:
1条回答
  • 2020-12-04 02:16

    No, it's not possible – not just for instances but for anything. GHC only registers the compiled version of a package, so the source code generally won't be available to ghci.

    Probably, most often you'll be using stuff from Hackage; in that case it's very simple to find the source code of such instances by hoogling the module, locating the class or data declaration, and clicking on source.

    When you don't have internet access or whatever else reason you can't hoogle online, you first need to find out in what package the module is included. The easiest way to do that:

    $ ghc-pkg find-module Data.Maybe
    /usr/local/haskell/lib/ghc-7.6.2/package.conf.d
       base-4.6.0.1
       haskell2010-1.1.1.0
    ~/.ghc/x86_64-linux-7.6.2/package.conf.d

    Then, as I said, GHC doesn't know where the source code to these packages is located – in fact it might not even be available on your system! But if you've installed the package (or one that depends on it) with cabal install, it will be there, by default in ~/.cabal/packages/hackage.haskell.org/PᴀᴄᴋᴀɢᴇNᴀᴍᴇ (as a compressed archive, but that's not a big hurdle). Within the package project folder, you can simply locate the module via the directory structure, which represents the module hierarchy.

    Other packages, like your example of Data.Maybe (package haskell2010), may have come right with your installation of GHC, e.g. the Haskell platform. In that case, I believe the easiest thing is to search there for the Haddock documentation file. In my case,

    $ find /usr/local/haskell -name 'Data-Maybe.html' | head -n1 | xargs firefox

    That'll open up the equivalent to what hoogle links you to (but on your local HD), where you can also browse the source code in a user-friendly way.

    0 讨论(0)
提交回复
热议问题