Opened 4 months ago

Closed 4 months ago

Last modified 3 months ago

#8622 closed bug (invalid)

Importing modules in .ghci file doesn't work

Reported by: m4dc4p Owned by:
Priority: normal Milestone:
Component: GHCi Version: 7.6.3
Keywords: Cc: hvr, simonmar
Operating System: MacOS X Architecture: x86_64 (amd64)
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

When attempting to import a module in the .ghci file, GHCi does not seem to use the search path.

Define a module names "Macros.hs":

module Macros where

hello :: String -> IO String
hello msg = return ("putStrLn " ++ show msg)

Now, in the same directory, create a .ghci file:

import Macros
:def hello hello

Run GHCi from that same directory, and you get an error:

$ ghci
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.

<no location info>:
    Could not find module `Macros'
    It is not a module in the current program, or in any known package.

<interactive>:1:25: Not in scope: `hello'

It seems that GHCi should be able to find "Macros.hs", compile it, and all me to use it in defining macros.

Adding the current directory (relative or absolute) to the search path (using -i) doesn't help.

Why do I want to do this? I want to define GHCi macros in a separate module (that is not related to my program) and import them, rather than defining them directly in the .ghci file.

Change History (8)

comment:1 follow-up: Changed 4 months ago by nomeata

Independent of this possible bug, note that in GHCi you can use symbols without importing them, by using a fully qualified name Macros.hello. This works best if you create a Cabal package with Macros and install it. See http://darcs.nomeata.de/ghc-heap-view/ghci for examples.

comment:2 Changed 4 months ago by m4dc4p

  • Summary changed from Importing modules in .ghci file doesn't not work to Importing modules in .ghci file doesn't work

comment:3 in reply to: ↑ 1 Changed 4 months ago by m4dc4p

Replying to nomeata:

This works best if you create a Cabal package with Macros and install it. See http://darcs.nomeata.de/ghc-heap-view/ghci for examples.

That seems to be the best workaround, but it is a bit of a pain. Thanks for the example!

comment:4 follow-up: Changed 4 months ago by simonpj

  • Cc simonmar added

Actually this is by design. You can only use import and :module for a module that either

  • is already loaded as part of the current program (:show modules tells you which ones are, I think), or
  • is a module of a visible package

If you use :load Macros rather than import Macros, all is well.

However, the user documentation is not good on this point. Notably, it says (2.4.5.1) that "You cannot add a module to the scope if it is not loaded". But that's not true: you can also add (or import) any package module.

Copying Simon M to check my statements here. If correct, I'll update the documentation.

Simion

comment:5 Changed 4 months ago by m4dc4p

  • Resolution set to invalid
  • Status changed from new to closed

Thanks for taking a look - I've marked this bug as invalid, since its by design. Hope that is the right process!

comment:6 in reply to: ↑ 4 Changed 3 months ago by simonmar

Replying to simonpj:

However, the user documentation is not good on this point. Notably, it says (2.4.5.1) that "You cannot add a module to the scope if it is not loaded". But that's not true: you can also add (or import) any package module.

Copying Simon M to check my statements here. If correct, I'll update the documentation.

Yes, looks like it should say "if it is not loaded or a package module" or something. Maybe the negatives should be reversed.

comment:7 Changed 3 months ago by Simon Peyton Jones <simonpj@…>

In 0ec530202369fef2fecc7446027a65b4a345f0a9/ghc:

Improve documentation of :module etc (Trac #8622)

I did quite a bit of restructuring, as well as adding
the note specifically referred to in #8622

comment:8 Changed 3 months ago by m4dc4p

I'm glad my ticket resulted in such an improvement to the documentation - thank you!

Note: See TracTickets for help on using tickets.