How can I import a Haskell module in GHCi?
Asked Answered
L

3

37

I am trying to teach myself Haskell from the book Learn You A Haskell for Great Good. I got up to the last section of chapter 7 (Modules), where it tells how to create your own module. I did a copy and paste of the Geometry module given in the book at the beginning of the section. The name of the file is Geometry.hs, as the book suggested, and the file is in the bin directory for ghci, which is where I previously was able to successfully do a load using :l for another .hs file.

When I type the following command in GHCi

import Geometry

I get the following error:

Could not find module 'Geometry' It is not a module in the current program or in any known package

I must be doing something that is obviously wrong, but I can't figure out what it is.

Limy answered 30/7, 2015 at 19:38 Comment(7)
Don't you mean chapter 7?Transformism
I've only ever been able to load installed modules and modules from the current working directory of ghci (If you are on windows you might maybe be able to run :!cd to find out the current working directory of ghci).Accommodate
Yes, I changed the question to chapter 7. I have been using an older version of the book in pdf form and it is chapter 6 in that version. I ran the :! cd command and it points to the same directory that I have been using.Limy
As far as I know, GHCi won't recognize local files as modules, unless they're used by a :loaded file. Create an additional file where you import Geometry and load that one.Transformism
@Transformism That sounds like an answer to me (though I guess you can just load Geometry itself without creating a wrapper module).Ramsay
@DanielWagner: I wasn't sure how to write that down. Also, I didn't find anything regarding module paths in the report, and I like to source my answers if possible.Transformism
@Transformism Yes, the Report says nothing about how modules are stored (they could go in a database for all the spec cares!). If you want a canonical source, it will have to be the GHC documentation.Ramsay
T
31

When you use import ModuleName in GHCi, it works (mostly) in the same way import Data.List works: GHC checks your local package database for the module, loads it, and brings its (exported) contents into scope.

However, Geometry isn't a module of a package installed with ghc-pkg. Therefore, GHC doesn't know that a module Geometry exists at all. Neither does it interactive variant GHCi.

But if you :load a program, things change. GHC will take its used modules into account:

-- Foo.hs
module Foo where

foo :: IO ()
foo = putStrLn "Hello from foo!"
-- Main.hs
module Main where
import Foo (foo)

main :: IO ()
main = foo
$ cd /path/to/your/files
$ ghci
GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> import Foo

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

Prelude> :l Main.hs
[1 of 2] Compiling Foo              ( Foo.hs, interpreted )
[2 of 2] Compiling Main             ( Main.hs, interpreted )
Ok, modules loaded: Main, Foo.
*Main> :l Main.hs
*Main> foo
Hello from foo!
*Main> import Foo
*Main Foo> -- module now loaded

As you can see, importing Foo first failed. However, after we've actually loaded the program that uses Foo, we were able to use import Foo in GHCi.

So if you want to use import in GHCi, make sure that GHC can find your module, either by including it in a wrapper or installing it. If you just want to load the module itself, use :load.

Transformism answered 31/7, 2015 at 6:52 Comment(7)
Thanks all. I found the problem. The pdf file had a line of code that continued onto the next line of text. The :l oad command was much more helpful with its error message than the import.Limy
Thank you for the answer,. Is there a way to make ghc globally aware of a custom module (so you don't need to navigate to the directory where it lives)?Pelagias
@Pelagias I missed that comment. Did you find an answer meanwhile?Transformism
@Transformism not really, but I was advised not to do this anyway and use a sandbox instead.Pelagias
Thanks. I lost an hour or two on this one. Modules have to start with a capital letter too. Same goes for the :module command.Outmoded
For me cabal install --lib geometry did the trick. After that I was able to import Geometry in ghciTezel
@Tezel This answer is fairly old; Cabal 1.10.something was the most recent version, and the --lib variant wasn't available at that time. Feel free to provide an answer with a more up-to-date usage :)Transformism
G
3

TLDR: the Learn you a Haskell book fails to mention that you have to :load the Geometry.hs file first. Then :m to go back to Prelude and then import Geometry works.

Guesthouse answered 6/1, 2021 at 5:27 Comment(0)
T
1

It is now also possible to add the lib flag when installing packages, i.e. to run cabal install --lib packagename and then to import the corresponding package directly in GHCi. In the present case, for example cabal install --lib hgeometry would facilitate importing modules from this geometry package.

Tezel answered 9/7, 2021 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.