haskell : unload module in WinGHCi

标签 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,我尝试了您的建议,但无法让它在 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'

最佳答案

已安装的模块

您必须使用 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 的 Windows 上),输入 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将为您提供所有可用命令的完整列表。

关于 haskell : unload module in WinGHCi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10295678/

相关文章:

具有优化访问方法的 Haskell 数据结构

haskell - 如何在不使用记录语法的情况下实现 MonadState 类?

windows - 在 Windows 中使用 GHCI 的 Haskell 外部函数接口(interface)

haskell - 无法安装 Cabal-1.20.0.2

haskell - 使用 cabal 安装依赖项失败

haskell - 如何概括这种就地选择排序?

windows - 为什么在 Windows 中运行 GHCi 无法检测到无限循环?

haskell - 如何在 Edward Kmett 的 "Linear"库中使用可变大小向量?

haskell - 堆栈 ghci 未加载本地模块?

haskell - 在 Haskell 中定义函数并在参数中进行计算