在ghci中指定“加载”操作的搜索路径

时间:2013-11-19 17:28:49

标签: haskell ghci

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.

2 个答案:

答案 0 :(得分:5)

加载路径是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

答案 1 :(得分:3)

在使用ghci运行stack的情况下。

第1步:

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

第2步:(在ghci内部)

:show paths

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

似乎ghci并不喜欢&#34; space&#34;在路径

第3步:(仍在ghci内)

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

第4步:(仍然在ghci内)

:show paths

所以避免&#34;空间&#34;内部路径。可以使用开头或ghci内部的命令行选项设置搜索路径,并使用:show paths

进行检查
相关问题