artella
artella

Reputation: 5118

Specifying the search path for "load" operations in ghci

In Loading source files it states that the search path for finding source files is specified with the -i option :

ghci -idir1:...:dirn

Does this mean that when one performs :

:load test.hs

then ghci looks in the directories above for test.hs? I saw the response at Problem Specifying Source Directory to GHC but I am still not clear about this.

For example in Windows XP I put test.hs in :

C:\Documents and Settings\winuser\My Documents

and then ran :

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

However upon doing :load test.hs, ghci complained about not being able to find the file.

[EDIT 1]

I want to avoid using :cd because it unloads all loaded modules, which prevents me from loading files from multiple locations

[EDIT 2 : response to 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

Then I can do the following :

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

However I haven't found a way to make modules A and B simultaneously available when the modules are stored in files in different locations

[EDIT 3 : response to 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'

[EDIT 4 : response to 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.

Upvotes: 6

Views: 6416

Answers (2)

Zhe Hu
Zhe Hu

Reputation: 4007

In the context of running ghci with stack.

Step 1:

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

Step 2: (inside ghci)

:show paths

module import search paths: c:\Documents

It seems ghci doesn't like "space" in the path

Step 3: (still inside ghci)

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

Step 4: (still inside ghci)

:show paths

So avoid "space" inside path. Search paths can be set with command line options at the beginning or inside ghci, and be checked with :show paths

Upvotes: 4

daniel gratzer
daniel gratzer

Reputation: 53871

The load path is how GHCi searches for modules. So if you named your module Test.hs and added

 module Test where

Than you can do

 > :load Test

otherwise you can use

 > :cd SomeDirectory
 > :load test.hs

Response to edit:

(Warning, I run eshell so the commands/paths look different)

~         $ 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

And now I have access to both A and B.

Upvotes: 6

Related Questions