Haskell:在WinGHCi中卸载模块 [英] Haskell : unload module in WinGHCi

查看:190
本文介绍了Haskell:在WinGHCi中卸载模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载了两个模块(NecessaryModule1.hs和NecessaryModule2.hs,链接在 Haskell:加载当前目录路径中的所有文件)。现在我想卸载NecessaryModule2.hs。我在 System.Plugins.Load 但是它在WinGHCi中不起作用。我得到的错误信息是:

 > unload NecessaryModule2 

< interactive>:1: 1:不在范围内:'unload'

< interactive>:1:8:
不在范围内:数据构造函数`必需的模块2'



我试过了

  import System.Plugins.Load 

但没有奏效。有没有办法以上述方式卸载模块?



---------------------- -------------------------------------------------- ---------------



[对Riccardo的回应]



您好Riccardo,我尝试了您的建议,但我无法在WinGHCi中使用它。我有一个文件NecessaryModule1.hs如下:

  module NecessaryModule1其中

addNumber1 :: Int - > Int - > Int
addNumber1 ab = a + b

我通过':cd'命令,然后执行:

 > :module + NecessaryModule1 

<无位置信息>:
找不到模块'NecessaryModule1':
它不是当前程序中的模块,也不是任何已知的程序包。

这是正确的吗?谢谢



-------------------------- -------------------------------------------------- -----------



[更正到上面]



只是为了解释为什么以上是不正确的(正如Riccardo所解释的),需要做的事情如下:

如果我们有一个文件NecessaryModule1.hs,如下所示:

   -  NecessaryModule1.hs 
模块NecessaryModule1其中

addNumber1 :: Int - > Int - > Int
addNumber1 ab = a + b

然后我们做:

 > :load NecessaryModule1 
[1的1]编译NecessaryModule1(必需的模块1.hs,解释)
好​​了,模块加载:必需模块1。
> addNumber1 4 5
9
> :module -NecessaryModule1
> addNumber1 4 5

< interactive>:1:1:不在范围内:`addNumber1'


<您必须使用ghci的命令才能加载数据库,而不是使用ghci命令来加载数据库。 (:module + My.Module )和卸载(:module -My.Module )已安装的模块。您也可以使用:m 而不是:module 来减少写入次数,如下所示:

  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:不在范围内:`sort'

请记住,ghci提示符总是会提醒您当前已导入的模块:您可以查看该模块,以便知道使用:m -Module卸载的内容。 To.Unload



特定文件

如果您要加载的模块没有安装在系统中(例如,您编写了源代码并将其保存在某处),则需要使用其他命令:load filename.hs 。更快的方法是直接将路径作为 ghci 的命令行参数传递给文件,例如 ghci filename.hs 。如果您运行 winghci 并将其与 .hs 扩展名关联,则只需双击该文件即可。



在这两种情况下,您都会得到一个ghci提示符,指定的模块正确加载并在范围内导入(假设您没有得到编译错误)。与以前一样,现在可以使用:m [+/-] My.Module 来加载和卸载模块,但请注意,这与不同。 :load 因为:module 假设你已经:load 进入/退出范围。



例如,如果您有 test.hs

  module MyModule where 
import Data.List

fx = sort x

您可以通过双击它(在带有winghci的windows上)来加载它,方法是输入 ghci test.hs ,或者加载 ghci 并键入:load test.hs (注意相对/绝对

另一个有用的ghci命令是:reload ,它将重新编译你之前加载的模块。

  Prelude>在您更改源文件时使用它,并且您想快速更新在ghci中加载的模块。 :load test.hs 
[1的1]编译MyModule(test.hs,解释)
好​​了,加载的模块:MyModule。
* MyModule> let xs = [1,2,3] in xs == f xs
True
* MyModule> :重新加载$ ​​b $ b好​​了,加载的模块:MyModule。

:help 会给你一个完整的列表所有可用的命令。


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 :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.

这篇关于Haskell:在WinGHCi中卸载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆