Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the search path for "load" operations in ghci

Tags:

haskell

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.
like image 967
artella Avatar asked Nov 19 '13 17:11

artella


People also ask

How do I load files into ghci?

From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.

How do I load a Haskell file into ghci?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

How do I load a module in Haskell?

The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.


2 Answers

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.

like image 96
Daniel Gratzer Avatar answered Sep 29 '22 15:09

Daniel Gratzer


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

like image 40
Zhe Hu Avatar answered Sep 29 '22 15:09

Zhe Hu