[英]How to configure .ghci file to import all loaded modules
let say I have a project which is just a bunch of Haskell modules with exercises.假设我有一个项目,它只是一堆带有练习的 Haskell 模块。 I'd like to provide a
.ghci
which automatically imports all modules into ghci
scope.我想提供一个
.ghci
,它会自动将所有模块导入ghci
scope。 The problem is, I can not run import
nor :m +Module.Name
within .ghci
file.问题是,我无法在
.ghci
文件中运行import
或:m +Module.Name
。
Clearly cabal repl
is reading the .ghci
file because options like the prompt are readed.显然
cabal repl
正在读取.ghci
文件,因为读取了提示等选项。 Also it loads modules correctly, but it doesn't bring them into scope (only one gets imported).它也正确加载模块,但不会将它们带入 scope (只有一个被导入)。 If I try to add
import OtherModule
to .ghci
file, then I get the error如果我尝试将
import OtherModule
添加到.ghci
文件,则会收到错误消息
module is member of hidden package fail-ghci
模块是隐藏的 package fail-ghci 的成员
Perhaps you need to add 'fail-ghci' to the build-depends in your.cabal file.也许您需要将“fail-ghci”添加到 your.cabal 文件中的 build-depends 中。
But I can't add fail-ghci to the cabal file, because the library can't depend on itself!!但是我不能将fail-ghci添加到cabal文件中,因为库不能依赖于自己!!
To reproduce.重现。 Create a simple cabal project.
创建一个简单的 cabal 项目。 For example:
例如:
src
|- Module1.hs # include dummy function func1 :: Int
|- Module2.hs # include dummy function func2 :: Int
fail-ghci.cabal
.ghci
The content of fail-ghci.cabal
is fail-ghci.cabal
的内容是
name: fail-ghci
version: 0.1.0.0
license: BSD3
license-file: LICENSE
build-type: Simple
library
exposed-modules:
Module1
, Module2
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010
If you set .ghci
as如果您将
.ghci
设置为
:set prompt "> " -- Set this option to ensure .ghci is readed by cabal repl
It will work fine and will bring Module1.hs
into scope, so func1
is available.它将正常工作并将
Module1.hs
带入 scope,因此func1
可用。 But Module2.hs
wont be in the scope, If I want to use it I'd need to execute import Module2
or equivalent.但是
Module2.hs
不会在 scope 中,如果我想使用它,我需要执行import Module2
或等效项。
Now, I'd like this to happen automatically when running cabal repl
because my project has many modules.现在,我希望在运行
cabal repl
时自动发生这种情况,因为我的项目有很多模块。 The obvious (to me) choice is to modify .ghci
as (对我而言)显而易见的选择是将
.ghci
修改为
:set prompt "> " -- Set this option to ensure .ghci is readed by cabal repl
import Module2 -- Equivalent :m +Module2
But the ghci is unable to import the module, despite of that command woring correctly within ghci.但是 ghci 无法导入模块,尽管该命令在 ghci 中正常工作。 What's the correct configuration to do this?
执行此操作的正确配置是什么?
One workaround I like is to add a new module to your project, perhaps called DefaultRepl
or similar, that imports everything you want.我喜欢的一种解决方法是向您的项目添加一个新模块,可能称为
DefaultRepl
或类似名称,它可以导入您想要的所有内容。 If you put this at the top of your exposed-modules list, then it will be loaded up when you run cabal repl
and bring everything that it imports into scope.如果你把它放在暴露模块列表的顶部,那么当你运行
cabal repl
并将它导入的所有内容带入 scope 时,它将被加载。
I suppose the problem is simply that cabal executes the commands in your .ghci
file before it loads the current project.我想问题只是 cabal 在加载当前项目之前执行了
.ghci
文件中的命令。 Indeed when I cabal repl
in a similar project, the startup output is:事实上,当我在一个类似的项目中进行
cabal repl
时,启动 output 是:
Build profile: -w ghc-9.0.2 -O1
In order, the following will be built (use -v for more details):
- fail-ghci-0.1.0.0 (first run)
Preprocessing library for fail-ghci-0.1.0.0..
GHCi, version 9.0.2: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /tmp/scratch/.ghci
[1 of 2] Compiling Module1 ( src/Module1.hs, interpreted )
[2 of 2] Compiling Module2 ( src/Module2.hs, interpreted )
Ok, two modules loaded.
ghci>
My guess would be that this is probably because cabal repl
starts a ghci session (which loads the .ghci
file during startup) and then uses its API to load the project environment.我的猜测是,这可能是因为
cabal repl
启动了一个 ghci session(它在启动期间加载.ghci
文件),然后使用它的 API 来加载项目环境。 (I was able to confirm that a .ghci
file can import modules from packages that are fully installed, rather than from a current project being cabal repl
d) (我能够确认
.ghci
文件可以从完全安装的包中导入模块,而不是从当前的cabal repl
d 项目中导入模块)
I don't know of any way to automatically execute commands after the project has been loaded.我不知道有什么方法可以在项目加载后自动执行命令。 But you can have your
.ghci
file define a shortcut command that will import all the modules you want.但是你可以让你的
.ghci
文件定义一个快捷命令来导入你想要的所有模块。 That way you have at least have a single quick command to execute once you're in the ghci shell, instead of manually importing everything.这样一来,一旦您进入 ghci shell,您至少可以执行一个快速命令,而不是手动导入所有内容。 Something like this:
像这样的东西:
:def loadall (const . pure) ":m + *Module1 *Module2"
-- or
:def loadall (const . pure) "import Module1 Module2"
-- or etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.