How do I figure out which of my imports are from in Haskell?

情到浓时终转凉″ 提交于 2020-01-02 03:35:19

问题


I have a bunch of imports in my package and need to sort out which ones are coming from a specific package (MissingH). I'm not sure how to do this other than by searching for each on Hoogle. Is there a way to do this programmatically or at the command line by just scanning my package's files?

Here's my list of imports (from all files of my package):

import           Control.Arrow
import           Control.Exception      (assert)
import           Control.Monad          (unless)
import           Control.Monad.Except
import           Control.Monad.Zip
import           Control.Applicative
import           Data.Monoid
import           Data.List
import           Data.List.Split        (splitOn)
import qualified Data.Map               as M
import           Data.Maybe
import           Text.Printf            (printf)
import           Data.Char              (toUpper)
import           Data.String.Utils      (replace)

import Data.Char (chr, ord)
import Data.List (sort)

import Control.Applicative
import Data.Monoid
import Data.Char
import Data.List
import Data.List.Split          (chunksOf)
import Data.String.Utils        (replace)
import Text.Printf              (printf)

回答1:


You can ask GHC where it thinks a module comes from (if you have a package already installed providing that module).

% ghc-pkg find-module Data.Maybe
/usr/local/lib/ghc-8.2.2/package.conf.d
    base-4.10.1.0
/home/dmwit/.ghc/x86_64-linux-8.2.2/package.conf.d
    (no packages)

You can probably cook up a few quick scripts to automate calling this and cover 99.9% of the code people actually write. You might also like to abuse graphmod -- use it to create a module graph, then ignore all the structure of the graph and just iterate over the list of module names it discovers for you and call ghc-pkg on each.

...but it's probably going to be much quicker to just delete MissingH from the dependencies in your cabal file (you are using a build tool like stack or cabal, right??) and see which imports GHC complains about.




回答2:


If the packages are in Stackage, you can check the module list for a snapshot to get the Map ModuleName [PackageName] correspondence. Yes, a module name may appear in multiple packages per snapshot. Here's an example listing:

https://www.stackage.org/lts-12.12/docs



来源:https://stackoverflow.com/questions/52743115/how-do-i-figure-out-which-of-my-imports-are-from-in-haskell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!