Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import a Haskell module in GHCi?

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.

like image 559
user1153980 Avatar asked Jul 30 '15 19:07

user1153980


People also ask

How do I load a Haskell file into GHCi?

If you modify your Haskell source file while GHCi is still running, use the command ":r" or ":reload" to make GHCi reload it.

How do I import a Haskell module?

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.

What does GHCi mean in Haskell?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.

How do I get out of GHCi Haskell?

Quits GHCi. You can also quit by typing control-D at the prompt. Attempts to reload the current target set (see :load ) if any of the modules in the set, or any dependent module, has changed.


2 Answers

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.

like image 127
Zeta Avatar answered Sep 30 '22 08:09

Zeta


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.

like image 26
xdavidliu Avatar answered Sep 30 '22 10:09

xdavidliu