Haskell timeit 模块 - SystemTimeIT 未找到

问题描述

这一定很简单;但为什么会出现这个错误

PS C:\Users\guthrie\Desktop> cabal install --lib timeit
Resolving dependencies...
Up to date
PS C:\Users\guthrie\Desktop> ghci
GHCi,version 8.10.2: https://www.haskell.org/ghc/  :? for help
Prelude> :m System.TimeIt

<no location info>: error:
    Could not find module `System.TimeIt'
    Perhaps you meant System.Timeout (from base-4.14.1.0)
Prelude>
Leaving GHCi.

Hackage 显示 System.TimeIt 在 timeit-2.0 中定义,这是 cabal 安装的内容

PS C:\Users\guthrie\Desktop> cabal install timeit
Resolving dependencies...
Build profile: -w ghc-8.10.2 -O1
In order,the following will be built (use -v for more details):
 - timeit-2.0 (lib) (requires download & build)
Downloading  timeit-2.0
Downloaded   timeit-2.0
Starting     timeit-2.0 (lib)
Building     timeit-2.0 (lib)
Installing   timeit-2.0 (lib)
Completed    timeit-2.0 (lib)

再次检查:

PS C:\Users\guthrie\Desktop> cabal install --lib timeit
Resolving dependencies...
Up to date

但试图从阴谋集团信息中检查它::

PS C:\Users\guthrie\Desktop> cabal info timeit
* timeit           (library)
    Synopsis:      Time monadic computations with an IO base.
    Versions available: 0.9.0.0,1.0.0.0,2.0
    Versions installed: [ Not installed ]
    Homepage:      https://github.com/merijn/timeit
    Bug reports:   https://github.com/merijn/timeit/issues
    Description:   A simple wrapper to show the used cpu time of monadic
                   computation with an IO base.
    Category:      System
    License:       BSD3
    Author:        Lennart Augustsson
    Maintainer:    Merijn Verstraaten <merijn@inconsistent.nl>,Lennart Augustsson
    Source repo:   ssh://github.com:merijn/timeit.git
    Dependencies:  base >=3 && <5,transformers >=0.2 && <0.6
    Cached:        Yes
    Modules:
        System.TimeIt

所以它报告:“未安装”。 标准 (Criterion.Main) 也有同样的问题。

我看到了 2/2020 的笔记;如果安装包没有 --lib 选项,

在这种情况下,它确实将库添加到包 db 中,但没有 将其添加认环境。 (*https://github.com/haskell/cabal/issues/6262#issuecomment-589843722)

所以我用这个选项重复安装,没有效果 - 只是报告“最新”。

一个链接提供了一个解决方案(解决方法?)来代替 cabal v1-install,它确实有效。但是,推荐的简单解决方案是什么,以便能够仅使用 ghci 来测试小代码?强制迁移到堆栈,本地沙盒和每个小测试的所有内容的副本?

由于我只是对示例进行非常简单的简短测试,因此我没有为每个示例创建沙箱,也没有 .cabal 文件,现在需要这些吗?!

(cabal 3.2.0.o,ghc 8.10.2,Windows 10)

解决方法

我强烈建议您不要尝试直接使用 ghcghci,而是使用 cabal。如果您想在 REPL 中快速使用 timeit,请使用

cabal v2-repl --build-depends timeit

如果您想做更复杂的事情,请创建一个(可能是临时的)项目并添加 timeit 作为依赖项:

$ mkdir my-temporary-package
$ cd my-temporary-package
$ cabal init

编辑my-temporary-package.cabal进行更改

build-depends:       base >=... && <...

build-depends:       base,timeit

(对于快速探索性项目而言,base 上的约束将弊大于利。如果有必要,您可以稍后再添加它们。)然后

$ cabal v2-repl

将为您提供带有 timeit 的 REPL。