[英]Haskell / GHCi - loading modules from different directories
My haskell application has the following directory structure:我的 haskell 应用程序具有以下目录结构:
src/
utils/Utils.hs
subsystem/Subsystem.hs
The Subsystem
module imports Utils
module. Subsystem
模块导入Utils
模块。 I would like to hand test this code in GHCi.我想在 GHCi 中手动测试这段代码。
The problem is GHCi seems to be only looking for modules available in '.'
问题是 GHCi 似乎只在寻找'.'
(current directory), so I copied Utils.hs
to subsystem folder and was able to hand-test Subsytem.hs
. (当前目录),所以我将Utils.hs
复制到了子系统文件夹并能够手动测试Subsytem.hs
。 Is there a better way to do this?有一个更好的方法吗? For example I would like to start GHCi in the src
directory and let it search for modules in ./utils
and ./subsystem
directories.例如,我想在src
目录中启动 GHCi 并让它在./utils
和./subsystem
目录中搜索模块。 Can I specify a module path to GHCi?我可以指定 GHCi 的模块路径吗?
You can tell GHCi where to search for modules by using the -i
option:您可以使用-i
选项告诉 GHCi 在哪里搜索模块:
ghci Foo.Bar -isrc
This will load src/Foo/Bar.hs
into GHCi.这会将src/Foo/Bar.hs
到 GHCi 中。 This way, you can also specify two different directories like this:这样,您还可以像这样指定两个不同的目录:
ghci Bar.hs -i.:config
It will look for the dependencies in./ and./config/.它将在./ 和./config/ 中查找依赖项。
See the GHC user's guide for more information about the module search path . 有关模块搜索路径的更多信息,请参阅 GHC 用户指南。
By default, when GHC looks for modules, it interprets Foo.Bar
as Foo/Bar.hs
.默认情况下,当 GHC 查找模块时,它将Foo.Bar
解释为Foo/Bar.hs
。 So if you have a single project, you could have a module Utils
as Utils.hs
in the top-level directory, and a module Utils.Fishcakes
as Utils/Fishcakes.hs
.因此,如果您有一个项目,则可以在顶级目录中将模块Utils
作为Utils.hs
,并将模块Utils.Fishcakes
作为Utils/Fishcakes.hs
。 Note that Utils.hs
can exist alongside a directory named Utils
, or both can exist independently.请注意, Utils.hs
可以与名为Utils
的目录一起存在,或者两者都可以独立存在。 A common style tends to be using the top-level module to simply re-export things from modules below it in the hierarchy, but this isn't required.一种常见的风格往往是使用顶级模块来简单地从层次结构中它下面的模块重新导出事物,但这不是必需的。 The GHC User Guide covers the above behavior, as well as describing what other options are supported. GHC 用户指南涵盖了上述行为,并描述了支持哪些其他选项。
As far as I know, in most cases code will either use the above default structure, will use some other structure specified as part of a cabal build, or will expect to be installed as a library.据我所知,在大多数情况下,代码要么使用上述默认结构,要么使用作为 cabal 构建的一部分指定的其他结构,或者期望作为库安装。
You can create a.ghci file with something like this:您可以使用以下内容创建一个 .ghci 文件:
:set -isrc -iutils -isubsystem :set -isrc -iutils -isubsystem
If you project looks like the following...如果您的项目如下所示...
src/
utils/Utils.hs
subsystem/Subsystem.hs
.....
myproject.cabal
Setup.hs
You can create a .ghci
file in the root directory of the project, same directory that src
, myproject.cabal
and Setup.hs
is in. The content of .gchi
should be this..您可以在项目的根目录下创建一个.ghci
文件,与src
、 myproject.cabal
和Setup.hs
所在的目录相同。 .gchi
的内容应该是这个..
:set -isrc/utils -isrc/subsystem
Now you can call ghci from the root directory of your project and it will auto-load any linked modules.现在您可以从项目的根目录调用 ghci,它会自动加载任何链接的模块。
$ ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>:load src/subsystem/Subsystem.hs
... should load Subsystem.hs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.