haskell 如何在cabal repl中加载cabal脚本并导入隐藏包?

x6h2sr28  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(54)

我有一个可以用cabal run执行的外部依赖的脚本。我想在ghci/cabal repl中加载这个脚本,尽可能少的副作用。
这是我的剧本:

#!/usr/bin/env cabal
{- cabal:
build-depends: base
            , split
-}

import Data.List.Split (splitOn)

main :: IO ()
main = do
  let orders = splitOn "x" "axbxc"
  putStrLn $ head orders

字符串
编译一个具有外部依赖关系的haskell脚本,而不需要cabal,

$ cabal run script.hs
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.6.2.0 supports
'ghc' version < 9.4): /home/t/.ghcup/bin/ghc is version 9.4.7
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
 - fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Preprocessing executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
[1 of 1] Compiling Main             ( Main.hs, /tmp/cabal-repl.-4510/dist-newstyle/build/x86_64-linux/ghc-9.4.7/fake-package-0/x/script/build/script/script-tmp/Main.o )
[2 of 2] Linking /tmp/cabal-repl.-4510/dist-newstyle/build/x86_64-linux/ghc-9.4.7/fake-package-0/x/script/build/script/script
a


但是如果我在ghci中导入它,就会出现外部依赖性的问题:

$ cabal repl
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.6.2.0 supports
'ghc' version < 9.4): /home/t/.ghcup/bin/ghc is version 9.4.7
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
 - fake-package-0 (lib) (first run)
Configuring library for fake-package-0..
Preprocessing library for fake-package-0..
Warning: No exposed modules
GHCi, version 9.4.7: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /tmp/cabal-repl.-4707/setcwd.ghci
ghci> :load script.hs
[1 of 2] Compiling Main             ( script.hs, interpreted )

script.hs:7:1: error:
    Could not load module ‘Data.List.Split’
    It is a member of the hidden package ‘split-0.2.4’.
    Perhaps you need to add ‘split’ to the build-depends in your .cabal file.
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
7 | import Data.List.Split (splitOn)
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.


既然我已经在脚本的构建依赖项中添加了“split”,那么有没有什么方法可以将ghci指向该部分呢?我希望尽可能简单,不添加更多文件。我还希望避免在系统级安装库,以避免版本冲突,并保持所有内容的可复制性。

我尝试过的事情

How to load imports automatically in cabal repl?

建议使用*M形式,但似乎对我不起作用:

ghci> :load *script.hs
[1 of 2] Compiling Main             ( script.hs, interpreted )

script.hs:7:1: error:
    Could not load module ‘Data.List.Split’
    It is a member of the hidden package ‘split-0.2.4’.
    Perhaps you need to add ‘split’ to the build-depends in your .cabal file.
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
7 | import Data.List.Split (splitOn)
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.

根据Noughtmare的建议:

cabal repl script.hs不适合我:

$ cabal --version
cabal-install version 3.6.2.0
compiled using version 3.6.2.0 of the Cabal library 
bash-5.1$ cabal repl script.hs 
cabal: 'repl' doesn't take any extra arguments when outside a project:
script.hs


然而,这是可行的:

$ cabal repl --build-depends split
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
 - fake-package-0 (lib) (first run)
Configuring library for fake-package-0..
Preprocessing library for fake-package-0..
Warning: No exposed modules
GHCi, version 9.4.7: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /tmp/cabal-repl.-11713/setcwd.ghci
ghci> :load script.hs
[1 of 2] Compiling Main             ( script.hs, interpreted )
Ok, one module loaded.

70gysomp

70gysomp1#

由于cabal 3.8.1.0,您可以在脚本上调用cabal repl:

$ cabal repl script.hs

字符串
请参阅cabal repl文档的最后一部分:
https://cabal.readthedocs.io/en/stable/cabal-commands.html#cabal-repl
对于旧版本的cabal,您可以首先打开一个repl,手动指定依赖项,然后加载脚本:

$ cabal repl --build-depends split
...
ghci> :load script.hs
[1 of 2] Compiling Main             ( script.hs, interpreted )
Ok, one module loaded.

相关问题