【问题标题】:Specifying the search path for "load" operations in ghci在 ghci 中指定“加载”操作的搜索路径
【发布时间】:2013-11-19 17:28:48
【问题描述】:

Loading source files 中声明查找源文件的搜索路径是使用 -i 选项指定的:

ghci -idir1:...:dirn

这是否意味着当一个人执行时:

:load test.hs

然后 ghci 在上面的目录中查找 test.hs?我看到了回复 Problem Specifying Source Directory to GHC 但我还是不清楚。

例如在 Windows XP 中,我将 test.hs 放入:

C:\Documents and Settings\winuser\My Documents

然后跑:

ghci -iC:\Documents and Settings\winuser\My Documents

但是在执行:load test.hs 时,ghci 抱怨无法找到该文件。

[编辑 1]

我想避免使用:cd,因为它会卸载所有加载的模块,这会阻止我从多个位置加载文件

[编辑 2:对 jozefg 的回应]

--C:\A\A.hs
module A where
myaddA::Int->Int->Int
myaddA x y = x+y

--C:\B\B.hs
module B where
myaddB::Int->Int->Int
myaddB x y = x+y

然后我可以执行以下操作:

Prelude> :cd C:\A
Prelude> :load A
[1 of 1] Compiling A                ( A.hs, interpreted )
Ok, modules loaded: A.
*A> myaddA 2 3
5
*A> :cd C:\B
Warning: changing directory causes all loaded modules to be unloaded,
because the search path has changed.
Prelude> :load B
[1 of 1] Compiling B                ( B.hs, interpreted )
Ok, modules loaded: B.
*B> myaddB 3 4
7

但是,当模块存储在不同位置的文件中时,我还没有找到使模块 A 和 B 同时可用的方法

[编辑 3:对 jozefg 的回应]

>ls
temp  temp2
>more temp/A.hs
module A where
addA = (+)
>more temp2/B.hs
module B where
addB = (+)
>cd temp
>ghci -i../temp2
GHCi, version 7.6.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> import A B

<interactive>:1:10: parse error on input `B'

[编辑 4:对 jozefg 的回应]

>ls
temp  temp2
>more temp/A.hs
module A where
addA = (+)
>more temp2/B.hs
module B where
addB = (+)
>cd temp
>ghci -i../temp2
GHCi, version 7.6.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> import A

<no location info>:
    Could not find module `A'
    It is not a module in the current program, or in any known package.
Prelude> import B

<no location info>:
    Could not find module `B'
    It is not a module in the current program, or in any known package.

【问题讨论】:

  • 那是怎么解析的?你的目录路径中有空格和:'s
  • @jozefg 我正在尝试它只是从我尝试加载的源的一个目录级别,但我无法让它工作。即使路径有效,似乎也存在此问题。

标签: haskell ghci


【解决方案1】:

加载路径是 GHCi 搜索模块的方式。因此,如果您将模块命名为 Test.hs 并添加

 module Test where

你做不到

 > :load Test

否则你可以使用

 > :cd SomeDirectory
 > :load test.hs

编辑回复:

(警告,我运行 eshell 所以命令/路径看起来不同)

~         $ mkdir temp
~         $ mkdir temp/temp temp/temp2
temp      $ find-file temp/A.hs
-- In A.hs
module A where
addA = (+)
--
temp      $ find-file temp2/B.hs
-- In B.hs
module B where
addB = (+)
--
temp      $ cd temp
temp/temp $ ghci -i../temp2
> :load A B
> import B

现在我可以同时访问AB

【讨论】:

  • 谢谢我知道:cd 但我想避免使用它,因为它会卸载模块,这会阻止我从不同位置加载文件。还有其他方法吗?
  • @artella 有什么理由避免使用 Haskell 的模块系统吗?这就是应该处理这些事情的原因
  • 我尝试了您的建议(参见上面的编辑),但无法按我的意愿工作。
  • @artella 你的加载路径是什么?对我来说效果很好
  • 但在我的示例中,我有模块 A 和模块 B,您是否能够同时使用 myaddA(来自模块 A)和 myaddB(来自模块 B)?我发现在任何给定时间我都可以使用其中一个,但不能同时使用两者(因为模块位于不同位置的文件中)?您指定的示例对我来说很好。
【解决方案2】:

在运行ghcistack 的上下文中。

第 1 步:

 stack ghci --ghci-options -i"C:\Documents and Settings\winuser\My Documents"

第 2 步:(在 ghci 内部)

:show paths

模块导入搜索路径:c:\Documents

似乎 ghci 不喜欢路径中的“空格”

第 3 步:(仍在 ghci 中)

:set -iC:\Users\zheh\Desktop\code\Craft3e-0.1.0.10

第 4 步:(仍在 ghci 中)

:show paths

所以避免路径内的“空格”。搜索路径可以在开头或 ghci 内部使用命令行选项设置,并通过:show paths 进行检查

【讨论】:

    猜你喜欢
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 2021-10-31
    • 2022-06-15
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多