是否可以在 Haskell 源代码中导入已在目标文件 (.o) 中编译的模块?

Is it possible, in a Haskell source code, to import a module that has already been compiled in an object file (.o)?

假设我已经编译了一个名为 ModuleA 的模块。 GHC 编译器生成了 .o.hi 文件。

现在我想在另一个源文件中导入 ModuleA 而无需重新编译它或安装新包,即我希望 GHC 检测到 ModuleA 已经编译并存储在.o 文件。

可能吗?怎么样?

到目前为止,我试图设置一些GHC标志以查看.o.hi文件所在的目录,但是编译失败,因为GHC找不到接口ModuleA.

GHC 应该自动为当前目录中的模块执行此操作:

$ echo "module ModuleA where { a :: Int ; a = 1 }" > ModuleA.hs
$ ghc -c ModuleA.hs
$ echo "module Main where { import ModuleA ; main :: IO () ; main = print a }" > ModuleB.hs
$ ghc ModuleB.hs -o ModuleB
[2 of 2] Compiling Main             ( ModuleB.hs, ModuleB.o )
Linking ModuleB ...
$ ./ModuleB
1

(注意上面缺少 "Compiling ModuleA" 行)

如果源文件已经更改,它应该只在本地目录中重新编译导入的模块 上次编译:

$ echo "module ModuleA where { a :: Int ; a = 2 }" >! ModuleA.hs
$ ghc ModuleB.hs -o ModuleB
[1 of 2] Compiling ModuleA          ( ModuleA.hs, ModuleA.o )
Linking ModuleB ...
$ ./ModuleB
2

(注意上面的"Compiling ModuleA"行,缺少"Compiling Main"行)

导入路径上的其他位置也是如此:

$ rm ModuleA.*
$ mkdir foo
$ echo "module ModuleA where { a :: Int ; a = 3 }" > foo/ModuleA.hs
$ ghc -c foo/ModuleA.hs
$ ghc ModuleB.hs -o ModuleB -ifoo
Linking ModuleB ...
$ ./ModuleB
3