Chris Stryczynski
Chris Stryczynski

Reputation: 33861

How can I import a module that is in GHCi paths?

I seem unable to import a module when executing ghci directly, however I am able to load the module when executing ghci xmonad.hs.

Directory tree:

.
├── ghcid.sh
├── lib
│   ├── FocusWindow.hs
│   ├── MiddleColumn.hs
│   ├── Readme.md
│   └── TAGS
├── Readme.md
├── TAGS
├── xmonad2.hs
├── xmonad.errors
├── xmonad.hs
├── xmonad.state
├── xmonad.state.backup
└── xmonad-x86_64-linux

.ghci

:set -Wall
:set -i:lib

ghci prompt:

Prelude> :show paths
current working directory: 
  /home/chris/.xmonad
module import search paths:
  .
  lib
Display all 2040 possibilities? (y or n)
Prelude> import FocusWindow

<no location info>: error:
    Could not find module ‘FocusWindow’
    It is not a module in the current program, or in any known package.

ghci xmonad.hs prompt

GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/chris/.xmonad/.ghci
[1 of 3] Compiling FocusWindow      ( lib/FocusWindow.hs, interpreted )
[2 of 3] Compiling MiddleColumn     ( lib/MiddleColumn.hs, interpreted )
[3 of 3] Compiling Main             ( xmonad.hs, interpreted )
Ok, modules loaded: MiddleColumn, FocusWindow, Main.
*Main> import FocusWindow
*Main FocusWindow> :show paths
current working directory: 
  /home/chris/.xmonad
module import search paths:
  .
  lib

Upvotes: 0

Views: 539

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152682

You can only import modules that are from some known package or which have been loaded. When you run ghci xmonad.hs, it loads xmonad.hs and any modules needed by it. When you run ghci, it doesn't load anything by default.

You can load your module alone by running :load FocusWindow, or :l FocusWindow for short. That will automatically also "import" that module in a special way (specifically: making all names available, even un-exported ones), and you will also be able to do normal imports of modules used by FocusWindow.

It would probably be a nice feature for ghci's import support to try loading unknown modules before complaining. I suspect a patch adding that feature would be accepted.

Upvotes: 4

Related Questions