I loaded two modules (NecessaryModule1.hs and NecessaryModule2.hs as outlinked in Haskell : loading ALL files in current directory path). Now I want to unload NecessaryModule2.hs. I found an ‘unload’ function in System.Plugins.Load however but it did not work in WinGHCi. The error message I got was :
>unload NecessaryModule2
<interactive>:1:1: Not in scope: `unload'
<interactive>:1:8:
Not in scope: data constructor `NecessaryModule2'
I tried
import System.Plugins.Load
but that did not work. Is there a way to unload modules in the manner described above?
—————————————————————————————
[RESPONSE TO Riccardo]
Hi Riccardo, I tried your suggestion but I could not get it to work in WinGHCi. I had a file NecessaryModule1.hs as follows :
module NecessaryModule1 where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
I went to the location of the file via the ‘:cd’ command, and then did :
> :module +NecessaryModule1
<no location info>:
Could not find module `NecessaryModule1':
it is not a module in the current program, or in any known package.
Is this correct? Thanks [EDIT : see below for correction]
—————————————————————————————
[CORRECTION TO ABOVE]
Just to explain why the above is incorrect (as explained by Riccardo), what needs to be done is the following :
If we have a file NecessaryModule1.hs as follows :
--NecessaryModule1.hs
module NecessaryModule1 where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
then we do :
> :load NecessaryModule1
[1 of 1] Compiling NecessaryModule1 ( NecessaryModule1.hs, interpreted )
Ok, modules loaded: NecessaryModule1.
> addNumber1 4 5
9
> :module -NecessaryModule1
> addNumber1 4 5
<interactive>:1:1: Not in scope: `addNumber1'
Installed modules
You have to use ghci’s commands in order to load (
:module +My.Module) and unload (:module -My.Module) installed modules. You can also use:minstead of:modulein order to write less, like this:Remember that the ghci prompt always reminds you the module currently imported: you can have a look at that in order to know what to unload with
:m -Module.To.Unload.Specific files
If the module you’re trying to load isn’t installed in the system (e.g. you wrote the source and simply saved the file somewhere), you need to use a different command,
:load filename.hs. A quicker way is to pass the path to the file directly as a command-line argument toghci, e.gghci filename.hs. If you runwinghciand you associated it to the.hsextension, simply double-click the file.In both cases you will get a ghci prompt with the specified module correctly loaded AND imported in scope (provided you don’t have get compilation errors instead). As before, you can now use
:m [+/-] My.Moduleto load and unload modules, but please note that this is different from:loadbecause:moduleassumes you already:loaded what you’re trying to get in/out of scope.E.g., if you have
test.hsyou may load it by double-clicking it (on windows with winghci), by typing
ghci test.hsin a console, or by loadingghciand typing:load test.hs(beware of relative/absolute paths).Another useful ghci command is
:reload, which will recompile the module you loaded before. Use it when you change the source file and you want to quickly update the module loaded in ghci.:helpwill give you a complete list of all available commands.