Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ghci not loading function from file

Tags:

haskell

ghci

In test.hs, I have:

doubleMe x = x + x

In ghci, I type:

Prelude> :l test
[1 of 1] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> doubleMe 9

<interactive>:1:0: Not in scope: `doubleMe'
*Main> 

Why? How to fix?

like image 236
Delirium tremens Avatar asked Jun 01 '10 13:06

Delirium tremens


1 Answers

My guess is that you have defined a main function in your source file.

If you have defined a main function, loading the module with :l test won't import any functions but main. In that case you can load it by prepending an asterix to the module name: :l *test. The reason is that the compiled binary will hide non-exported top-level functions. Prepending an asterix forces GHCi to ignore the precompiled module (test) and interprete the source file instead (test.hs).

[jkramer/sgi5k:.../haskell]# cat test.hs 

main = do
    print $ doubleMe 2

doubleMe x = x + x

[jkramer/sgi5k:.../haskell]# ghc --make test
[jkramer/sgi5k:.../haskell]# ghci
[...some messages...]
>> :l test
Ok, modules loaded: Main.
>> :t doubleMe

<interactive>:1:0: Not in scope: `doubleMe'
>> :l *test
[1 of 1] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
>> :t doubleMe
doubleMe :: (Num a) => a -> a

Check these links for further information:

http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/ghci-compiled.html http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/interactive-evaluation.html#ghci-scope

like image 184
jkramer Avatar answered Sep 21 '22 15:09

jkramer