In Haskell ghci, I tried
Prelude> :load filename.hs
Ok, modules loaded: Main.
unfortunately I can’t run any of the functions defined in the file. I compiled the file without any errors, but calling function gives an error, “Not in scope: (function name)”. The strange thing is, awhile earlier I had no problem running this…
The problem is that your editor is compiling the code with something like
Since your file doesn’t have a module declaration, GHC assumes the module is called
Main, since you didn’t specify otherwise and, since it’s compiling a full program, doesn’t export any of the definitions other thanmain; that is, it acts as if you have a module declaration like:Whereas GHCi defaults to:
These module declarations specify the name of the module you’re compiling, and which values are exported. With the first declaration, only
mainis exported from the module; with the second case, every top-level value is exported. Values that aren’t exported can’t be accessed from outside the module, which is why you get the “not in scope” errors in GHCi.GHCi’s inconsistent behaviour is presumably to make testing code easier; you don’t have to have a module declaration to load a file and use its definitions. The solution is to put
module Main where(or some other module name) at the top of your file, which explicitly exports everything. Personally, I think this behaviour is confusing, and the behaviour of GHC and GHCi should probably be changed to be consistent.