运行时,它是包"time-1.5"中的一个隐藏模块" :load Data.Time.Clock.UTC"



我正在尝试BetterPredicate.hs现实世界的 Haskell 第 9 章,关于 ClockTime 的片段代码在我的ghc 7.6.3下抛出错误:

    Couldn't match expected type `ClockTime'
            with actual type `time-1.4.0.1:Data.Time.Clock.UTC.UTCTime'

根据Hyogeol Lee对本书网络版的评论,"getModificationTime does not returns ClockTime type in ghc 7.6.3, but you can use UTCTime instead.",在前奏提示下,我尝试了:+m Data.Time.Clock.UTC,我得到了以下错误:

<no location info>:
Could not find module `Data.Time.Clock.UTC'
It is a member of the hidden package `time-1.4.0.1'.
it is a hidden module in the package `time-1.5'
it is a hidden module in the package `time-1.4.0.1'

ghc-pkg list time的结果是

ghc-pkg list time
/usr/lib64/ghc-7.6.3/package.conf.d
time-1.4.0.1
/home/abelard/.ghc/x86_64-linux-7.6.3/package.conf.d
time-1.5

但我仍然不知道我该怎么办?

编辑:根据user2407038,我使用ghc-pkg unregister time-1.5删除time-1.5,之后,我得到了类似的错误:

:m +Data.Time.Clock.UTC
Could not find module `Data.Time.Clock.UTC'
It is a member of the hidden package `time-1.4.0.1'.
it is a hidden module in the package `time-1.4.0.1'

Prelude> :m +Data.Time.Clock
<no location info>:
Could not find module `Data.Time.Clock'
It is a member of the hidden package `time-1.4.0.1'.

编辑2:阅读后对GHC包的描述被隐藏,并更改BetterPredicate.hs的代码: System.Time(ClockTime(..))进入Data.Time.Clock(UTCTime),以下两种方式可以为我工作:

  1. 编译文件 BetterPredicate.hs: ghc --make BetterPredicate.hs -package time-1.4.0.1

  2. 使用sudo ghc-pkg expose time-1.4.0.1取消隐藏它,然后在ghci下,我可以成功加载模块Data.Time.Clock

:m +Data.Time.Clock

*Main Data.Time.Clock> :t UTCTime

 UTCTime
 :: time-1.4.0.1:Data.Time.Calendar.Days.Day -> DiffTime -> UTCTime

要修复 RWH 第 9 章中的BetterPredicate.hs模块,您必须执行以下操作:

  1. 导入数据.时间.时钟
  2. 将谓词的定义更改为使用 UTCTime 而不是时钟时间
  3. 从 Control.Exception 导入 SomeException,从 Control.Exception
  4. 导入
  5. 定义handleAny函数
  6. saferFileSizegetFileSize函数中使用handleAny而不是handle

import Data.Time.Clock (UTCTime)
import Control.Exception (bracket, handle, SomeException)
...
type Predicate =  FilePath      -- path to directory entry
               -> Permissions   -- permissions
               -> Maybe Integer -- file size (Nothing if not file)
               -> UTCTime       -- last modified
               -> Bool
...
handleAny :: (SomeException -> IO a) -> IO a -> IO a
handleAny = handle
...
saferFileSize path = handleAny (_ -> ...
...
getFileSize path = handleAny (_ -> ...

固定代码在这里:http://lpaste.net/116709

最新更新