ghci not loading function from file

蹲街弑〆低调 提交于 2019-12-03 03:29:59

Are you sure that you're loading the right test.hs? Maybe you're in the wrong directory. Or maybe you didn't save test.hs after adding the definition of doubleMe.

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

  1. Remove test.hi and test.o from the directory and then try ghci test. [Sometimes when I run ghc file.hs (and not ghc --make file.hs) it gives undefined reference error, but creates such files that are read by ghci later. Maybe this is a bug.]

  2. Try

    :cd "<path to your file>"
    :l test
    :browse
    

    in ghci. What is the result?

This happened to me, too - and in case anyone else runs into it and stumbles across this page, my issue was that the VM I was running GHCI in was out of disk space - prompting it to try and load an empty file each time.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!