Christopher King
Christopher King

Reputation: 10961

GHCi not seeing my module

A made a module Timeit. I can't import it to GHCi. Module:

module Timeit (timeit, timeCatch) where
import Data.Time.Clock

timeit::IO ()->IO (Float)
timeit io=do
    (time,())<-timeCatch io
    return time

timeCatch::IO (a)->IO (Float,a)
timeCatch io=do
    start  <-getCurrentTime
    result <-io
    end    <-getCurrentTime
    return $! (realToFrac (diffUTCTime end start), result)

test=do
    putStrLn "What is your name?"
    name <- getLine
    putStrLn $ "Your name is "++name++"."

GHCi:

theking@ChrisLaptopUbuntu1304:~/Desktop/Haskell$ cd ~/Desktop/Haskell/; 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.
Prelude> import Timeit

<no location info>:
    Could not find module `Timeit'
    Perhaps you meant Time (needs flag -package haskell98-2.0.0.2)

I am able to import it into my other programs, just not GHCi.

Note: Am I a haskell noob.

Upvotes: 2

Views: 1297

Answers (2)

user3310334
user3310334

Reputation:

To import modules from ghci don't use import, rather say

:m +TimeIt

Upvotes: 0

daniel gratzer
daniel gratzer

Reputation: 53901

In order for a module to be imported by GHCi, you have to make sure a few things are true.

First, are you in the same directory? By default GHCi will only search the current directory for modules.

Second, have you added the module header? Your code should start with

 module Timeit where
 ...

Third, your file must actually be named Timeit.hs (with that capitalization). By default Haskell inserts module Main where, which is a problem if your module isn't main.

Last but not least, GHCi seems to require that you use :l Timeit at least once. I'm not sure why this is, but once loaded you can remove it from scope with :m and then import it to your hearts content.

If you've done these things it should import just fine.

Upvotes: 3

Related Questions