Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell : unload module in WinGHCi

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'
like image 678
artella Avatar asked Apr 24 '12 09:04

artella


People also ask

How do I unload a module in Haskell?

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 :load ed what you're trying to get in/out of scope. you may load it by double-clicking it (on windows with winghci), by typing ghci test.

How do I leave Prelude in Haskell?

You can also quit by typing control-D at the prompt. Attempts to reload the current target set (see :load ) if any of the modules in the set, or any dependent module, has changed. Note that this may entail loading new modules, or dropping modules which are no longer indirectly required by the target.

How do I exit Haskell in terminal?

You can also quit by typing a control-D at the prompt. Attempts to reload the current target set (see :load) if any of the modules in the set, or any dependent module, has changed.

How do I go back to Prelude in Haskell?

Try using the :m command.


1 Answers

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.

like image 60
Riccardo T. Avatar answered Sep 28 '22 17:09

Riccardo T.