Haskell : unload module in WinGHCi
Asked Answered
E

1

7

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'
Eighth answered 24/4, 2012 at 9:59 Comment(0)
F
17

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 :m instead of :module in order to write less, like this:

Prelude> :m +Data.List
Prelude Data.List> sort [3,1,2]
[1,2,3]
Prelude Data.List> :m -Data.List
Prelude> sort [3,1,2]

<interactive>:1:1: Not in scope: `sort'

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 to ghci, e.g ghci filename.hs. If you run winghci and you associated it to the .hs extension, 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.Module to load and unload modules, but please note that this is different from :load because :module assumes you already :loaded what you're trying to get in/out of scope.

E.g., if you have test.hs

module MyModule where
import Data.List

f x = sort x

you may load it by double-clicking it (on windows with winghci), by typing ghci test.hs in a console, or by loading ghci and 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.

Prelude> :load test.hs
[1 of 1] Compiling MyModule         ( test.hs, interpreted )
Ok, modules loaded: MyModule.
*MyModule> let xs = [1,2,3] in sort xs == f xs
True
*MyModule> :reload
Ok, modules loaded: MyModule.

:help will give you a complete list of all available commands.

Freer answered 24/4, 2012 at 10:2 Comment(3)
Hi Riccardo, I tried your suggestion but had some problems. I have posted above since there is too little space here. ThanksEighth
Hi Riccardo, thanks a lot for your well explained answer. I really appreciate you spending the time to write all that out. Thank you.Eighth
:m -Data.List works, but it doesn't work with the Prelude. Any way to unload the Prelude?Awoke

© 2022 - 2024 — McMap. All rights reserved.