13

我想试试Writerghci 中的 monad。正如这里所建议的,我尝试仅使用堆栈来管理 GHC 和包,并避免全局安装。

在安装堆栈后,从全新的 Ubuntu 15.04 安装:

stack setup
mkdir lyah && cd lyah
stack new
stack install mtl
stack ghci
ghci> import Control.Monad.Writer
Could not find module ‘Control.Monad.Writer’
It is a member of the hidden package ‘mtl-2.1.3.1’.

我知道堆栈前的 ghc-pkg 用于显示/隐藏包,但我不确定如何在此处继续“取消隐藏”mtl 包。

4

4 回答 4

16

编辑创建的 .cabal 文件stack new并添加mtl到该build-depends部分。文件的该部分应如下所示:

build-depends:       base >= 4.7 && < 5
                   , mtl

然后,做一个stack buildbefore stack ghci

顺便说一句,不要stack install用于安装库 - 它只是复制二进制文件的快捷方式。例如stack install hlint,将首先构建包,然后将生成的二进制文件复制到~/.local/bin/。相反,请始终将包添加到 .cabal 文件中,如上所示,并使用stack build它们来安装它们。

于 2015-07-23T18:04:02.020 回答
2

如果您正在使用stack,请尝试将您的包定义添加到dependencies字段中,package.yaml以便将其公开到您的项目中。

dependencies:
- mtl

然后,stack run

示例package.yaml

于 2021-07-13T03:09:42.967 回答
2

只是为了补充@Jazmit 的答案。请注意,您的.cabal文件将包含两个build-depends部分。一下,library: 一下executable my-project-exec。在这种情况下,您需要将模块放在该executable部分下。

例子:

我的项目.cabal

library: 
  build-depends:
  ...

executable my-project-exe:
  build-depends:
    base >= 4.7 && < 5
    , mtl

有关库和可执行文件的更多信息,请查看文档: https ://cabal.readthedocs.io/en/latest/developing-packages.html#editing-the-cabal-file

于 2021-04-13T16:19:29.000 回答
1

由于您在 GHCi 中工作,您只需更改传递给底层 GHC 的命令行。例如,我最近这样做:

Prelude> import qualified GI.Gtk as Gtk

<no location info>: error:
    Could not load module ‘GI.Gtk’
    It is a member of the hidden package ‘gi-gtk-3.0.31’.
    Perhaps you need to add ‘gi-gtk’ to the build-depends in your .cabal file.
    It is a member of the hidden package ‘gi-gtk-3.0.27’.
    Perhaps you need to add ‘gi-gtk’ to the build-depends in your .cabal file.
Prelude> :set -package gi-gtk-3.0.27
package flags have changed, resetting and loading new packages...
Prelude> import qualified GI.Gtk as Gtk
Prelude Gtk> 
于 2019-07-11T07:56:55.283 回答