3
votes

I'm unsure what my issue is here. I have a trio of modules A.hs, B.hs, and C.hs. All are located at C:\..path...\folder and modules B and C both import from A.

That is, both modules B and C contain the line import A

I can :l C:\..path..\folder\A.hs in gchi and play with its contents; however, ghci gives the following error when I try to :l C:\..path..\folder\B.hs or :l C:\..path..\folder\C.hs

    Could not find module `A'
    Use -v to see a list of the files searched for.
Failed, modules loaded: none.

Which I find odd because I had no trouble compiling B.hs to B.exe and running the executable. How can I compile and run a module that I can't load into ghci? Or, why would an import succeed at compile time but fail in loading; especially when that very module being imported is itself load-able?

1
I had a similar problem once. If you load the modules on startup (ghci A.hs B.hs) does it give an error? - Wolfe Macfarlane
Hmm, so I tried it and it works with some caveats. If I start up ghci in the order you suggested then ghci makes the claim that both modules - Tshimanga
Hmm, it works with some caveats: If I start up ghci A.hs B.hs then ghci makes the claim that both modules have been loaded, but then when I actually start trying to interact with the contents only functions and types from A.hs are accessible. I get a not-in-scope error for anything in B.hs. If I exchange the arguments (start up instead as ghci B.hs A.hs) I can play with the contents of both. This peculiarity is interesting in it's own right. Still, the question of understanding why the original approach fails still stands; but, this work-around is appreciated. Thank you @WolfeMacfarlane - Tshimanga
@Tshimanga After running ghci A.hs B.hs, you should be able to switch which module is "visible" using :m, as in :m *A or :m *B. - Daniel Wagner

1 Answers

5
votes

By default, ghci searches only in the current directory for imported modules. To start with, the current directory is the one used to launch ghci; but it can be changed from within ghci with the :cd command. Thus, you could

> :cd C:\...path...\folder
> :l B.hs

and this should find both B.hs and A.hs in what is now the current directory. Alternately (and especially if you have modules in multiple directories) you can launch ghci with the -i command line option to add directories to its module search path. For example, in your command prompt you might

% ghci -iC:\...path...\folder
> :l B.hs

which will instruct ghci to include C:\...path...\folder in its search path, and therefore find B.hs and A.hs there even if it is not the current directory.