Search code examples
haskellghci

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.

Solution

  • 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.