Haskell:在WinGHCi中卸载模块

时间:2012-04-24 09:59:32

标签: haskell ghc ghci winghci

我加载了两个模块(NecessaryModule1.hs和NecessaryModule2.hs在Haskell : loading ALL files in current directory path中相互链接)。现在我要卸载NecessaryModule2.hs。我在System.Plugins.Load中找到了一个“卸载”功能,但它在WinGHCi中不起作用。我得到的错误信息是:

>unload NecessaryModule2

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

<interactive>:1:8:
    Not in scope: data constructor `NecessaryModule2'

我试过

import System.Plugins.Load

但这不起作用。有没有办法以上述方式卸载模块?

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

[对Riccardo的回应]

嗨Riccardo,我尝试了你的建议,但我无法让它在WinGHCi中工作。我有一个文件NecessaryModule1.hs如下:

module NecessaryModule1 where

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

我通过':cd'命令转到文件的位置,然后执行:

> :module +NecessaryModule1

<no location info>:
    Could not find module `NecessaryModule1':
      it is not a module in the current program, or in any known package.

这是对的吗?谢谢[编辑:见下面的更正]

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

[更正上述]

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

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

--NecessaryModule1.hs
module NecessaryModule1 where

addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
然后我们做:

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

1 个答案:

答案 0 :(得分:13)

已安装的模块

您必须使用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: Not in scope: `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

f x = sort x

你可以通过双击它(在带有winghci的窗口上),在控制台中输入ghci test.hs,或者加载ghci并输入:load test.hs来加载它(小心相对/绝对路径)。

另一个有用的ghci命令是:reload,它将重新编译之前加载的模块。更改源文件时使用它,并且想要快速更新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将为您提供所有可用命令的完整列表。