当模块名称与文件名称不匹配时,通过模块名称在GHCi中加载模块 [英] Load a module in GHCi by module name when module name doesn't match file name
问题描述
假设我有一个名为 MyModule.hs
的源文件,其中的模块声明是 module My.Module where ...
Suppose I am given a source file called MyModule.hs
and inside it the module declaration is module My.Module where ...
(note: not module MyModule where ...
).
我不允许更改此源文件或更改文件所在的目录结构。
I am not permitted to alter this source file or change the directory structure where the file resides.
阅读一些关于在GHCi中导入模块的文档,看起来好像有办法导入 (例如 import
或:load
),但不是任何方式指定将在本地目录的所有文件中搜索的模块名称。
From reading some docs about importing modules in GHCi, it looks like there are ways to import by file name (e.g. either import
or :load
), but not any ways to specify a module name that will be searched for in all files of the local directory.
有没有办法导入 My.Module
在GHCi中,而不需要通过指定文件名(仅限于模块名)并且不安装它(例如,不用 cabal
>构建它,只需快速将其放入GHCi通过模块名称)?
Is there a way to import My.Module
in GHCi without doing it by specifying the file's name (only the module name) and without installing it (e.g. not building it with cabal
, just quickly tossing it into GHCi by module name)?
推荐答案
在文档
对于这些目录中的每一个,它都会尝试将basename.extension附加到目录,并检查该文件是否存在。 basename的值是模块名称,点用目录分隔符替换('/'或'\',取决于系统),扩展名是源扩展名(hs,lhs)...
For each of these directories, it tries appending basename.extension to the directory, and checks whether the file exists. The value of basename is the module name with dots replaced by the directory separator ('/' or '\', depending on the system), and extension is a source extension (hs, lhs)...
关键部分是
The key part being
basename的值是模块名称由点替换为目录分隔符('/'或'\',具体取决于系统)
The value of basename is the module name with dots replaced by the directory separator ('/' or '\', depending on the system)
所以你的模块 My.Module
的名称将被搜索为 My / Module.hs
。你需要有一个目录结构,如
So your module name of My.Module
will be searched for as My/Module.hs
. You would need to have a directory structure like
project/
My/
Module.hs
project.cabal
并从文件夹项目
您可以运行
And from the folder project
you could run
$ cabal repl
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.
> import My.Module
如果您的文件名为 MyModule.hs
,你的模块名称是 MyModule
,但这只是上述规则的特例。
You can do this if your file is named MyModule.hs
and your module name is MyModule
, but it's just a special case of the rule above.
这有很好的理由,即它强制实施一个结构来简化项目结构和GHC的搜索算法。如果这条规则不存在,什么会阻止我进行操作
There are good reasons for this, namely that it enforces a structure to simplify your project structure and GHC's search algorithm. If this rule wasn't in place, what would stop me from having
project/
MyModule1.hs
MyModule2.hs
其中 .hs
文件有模块声明 My.Module
?如果我运行 import My.Module
,那么在GHCi中加载哪一个是正确的?通过指定文件名和路径,您立即知道模块 XYZWQRST
位于路径 X / Y / Z / W / Q / R /S/T.hs
,不需要搜索。它减少了模块名称规格较宽松时可能出现的不确定性。
where both .hs
files had the module declaration My.Module
? Which one would be correct to load in GHCi if I ran import My.Module
? By specifying what the filename and path is, you immediately know that the module X.Y.Z.W.Q.R.S.T
is at the path X/Y/Z/W/Q/R/S/T.hs
, no searching required. It reduces a lot of the ambiguity that could occur with looser module name specifications.
这篇关于当模块名称与文件名称不匹配时,通过模块名称在GHCi中加载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!