如何使用'stack ghci'导入而不是在启动时加载模块?

时间:2017-04-16 02:34:35

标签: haskell ghci haskell-stack

我有一个名为prime-tools的haskell包。当我在包目录中使用stack ghci时,我希望它能够打开一个交互式ghci并自动import prime-tools。相反,我发现加载.cabal文件中声明的所有模块

例如,以下是我的.cabal文件中的摘录,显示了声明了哪些模块:

library
  -- Modules exported by the library.
  exposed-modules:     PrimeTools.MathStuff, PrimeTools.Factors, PrimeTools.PQTrials, PrimeTools.Main, PrimeTools.Base, PrimeTools.Lucas

  -- Modules included in this library but not exported.
  other-modules:       PrimeTools.Extras

这就是我在项目文件夹中运行ghci>后收到stack ghci提示时发生的情况:

Ok, modules loaded: PrimeTools.MathStuff, PrimeTools.Factors, PrimeTools.PQTrials, PrimeTools.Main, PrimeTools.Base, PrimeTools.Lucas, PrimeTools.Extras.
ghci> 

加载模块而不是import prime-tools的问题在于我现在可以使用所有模块中定义的所有函数,无论它们是否被导出

这种区别引起的问题的一个例子:包prime-tools中有两个模块,它们具有一个名为pfactor的函数的实现。其中一个是导出的,打算由程序包的最终用户使用,而另一个仅供内部使用而不导出。

在有人发表评论之前,有充分的理由让pfactor有两个实现,但这与我的问题无关。

我的问题:如何使用stack自动启动ghci环境本地版ghc导入文件夹I在?

中运行命令

我想要的行为等同于运行以下命令序列:

stack ghci
ghci> :module -PrimeTools.MathStuff
ghci> :module -PrimeTools.Factors
ghci> :module -PrimeTools.PQTrials
ghci> :module -PrimeTools.Main
ghci> :module -PrimeTools.Base
ghci> :module -PrimeTools.Lucas
ghci> :module -PrimeTools.Extras
ghci> import PrimeTools.MathStuff
ghci> import PrimeTools.Factors
ghci> import PrimeTools.PQTrials
ghci> import PrimeTools.Main
ghci> import PrimeTools.Base
ghci> import PrimeTools.Lucas

这里的关键是我要导入.cabal文件中声明的 exposed-modules 不要加载任何模块。我不介意是否也导入 other-modules 。有没有办法可以使用stack执行此操作,而无需每次都运行这么长的命令序列?

1 个答案:

答案 0 :(得分:3)

合理的解决方法是定义a custom GHCi macro以便按照您希望的方式导入模块。在项目根目录中按以下行创建 jQuery('body').on('change', '.masonry .item :checkbox', function () { var urls = []; urls.length = 6; if (jQuery(urls.length > 6)) { alert("Puoi aggiungere un massimo di 6 immagini"); } else { jQuery('.masonry .item :checkbox:checked').each(function () { urls.push(jQuery(this).next('label').find('img').attr('src')); }); } }); 文件:

.ghci

这样,GHCi中的:{ :def deload (\_ -> return ":module -PrimeTools.MathStuff\n\ \import PrimeTools.MathStuff" ) :} 命令将从范围中删除,然后重新导入:deload - 您可以在列表中添加任意数量的模块。虽然我使用multiline string syntax编写了此内容,但您可以在括号内添加任何PrimeTools.MathStuff表达式,因此您可以根据自己的需要自由拼写或扩展它。