我正在尝试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)
,以下两种方式可以为我工作:
编译文件 BetterPredicate.hs:
ghc --make BetterPredicate.hs -package time-1.4.0.1
使用
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
模块,您必须执行以下操作:
- 导入数据.时间.时钟
- 将谓词的定义更改为使用 UTCTime 而不是时钟时间
- 从 Control.Exception 导入 SomeException,从 Control.Exception 导入
- 定义
handleAny
函数 - 在
saferFileSize
和getFileSize
函数中使用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