haskell - GHCI 无法为 ‘System.Time’ 加载接口(interface)

标签 haskell haskell-platform

我正在学习 Haskell 并尝试从书中构建示例。 当我在 GHCI 中使用“:l BetterPredicate”命令加载代码时,出现以下错误:

Prelude> :l BetterPredicate
[1 of 2] Compiling RecursiveContents ( RecursiveContents.hs, interpreted )

RecursiveContents.hs:12:32: warning: [-Wtabs]
    Tab character found here.
    Please use spaces instead.
[2 of 2] Compiling Main             ( BetterPredicate.hs, interpreted )

BetterPredicate.hs:3:1: error:
    Failed to load interface for ‘System.Time’
    Perhaps you meant
      System.CPUTime (from base-4.9.1.0)
      System.Cmd (from process-1.4.3.0)
      System.Mem (from base-4.9.1.0)
    Use -v to see a list of the files searched for.
Failed, modules loaded: RecursiveContents.

这是我要编译的代码:

import Control.Monad (filterM)
import System.Directory (Permissions (..), getModificationTime, getPermissions)
import System.Time (ClockTime(..))
import System.FilePath (takeExtension)
import Control.Exception (bracket, handle)
import System.IO (IOMode(..), hClose, hFileSize, openFile)

-- Our functions
import RecursiveContents (getRecursiveContents)

type Predicate = FilePath        -- Path to directory entry
              -> Permissions     -- permissions
              -> Maybe Integer   -- file size (Nothing if not file)
              -> ClockTime       -- last modified
              -> Bool
-- TBD
getFileSize :: FilePath -> IO (Maybe Integer)

betterFind :: Predicate -> FilePath -> IO [FilePath]
betterFind p path = getRecursiveContents >>= filterM check
    where check name = do 
              perms <- getPermissions name
              size  <- getFileSize name
              modified <- getModificationTime name 
              return (p name perms size modified)

simpleFileSize :: FilePath -> IO Integer
simpleFileSize path = do
    h <- openFile path ReadMode
    size <- hFileSize h
    hClose
    return size

saferFileSize :: FilePath -> IO (Maybe Integer)
saferFileSize path = handle (\_ -> return Nothing) $ do
    h <- openFile path ReadMode
    size <- hFileSize h
    hClose
    return (Just size)

getFileSize :: FilePath -> IO (Maybe Integer)
getFileSize path = handle (\_ -> return Nothing) $
    bracket (openFile path ReadOnly) hClose $ \h -> do
        size <- hFileSize h
        hClose
        return (Just size)


type InfoP a =  FilePath       -- path to directory entry
             -> Permissions    -- permissions
             -> Maybe Integer  -- file size (Nothing if not file)
             -> ClockTime      -- last modified
             -> a 

pathP :: InfoP FilePath
pathP path _ _ _ = path

sizeP :: InfoP Integer
sizeP _ _ (Just size) _ = size
sizeP _ _ Nothing     _ = -1

equalP :: (Eq a) => InfoP a -> a -> InfoP Bool
--equalP f k = \w x y z -> f w x y z == k
equalP f k w x y z = f w x y z == k

根据文档:http://hackage.haskell.org/package/old-time-1.1.0.3/docs/System-Time.html这个模块是旧时库的一部分,所以我想我需要以某种方式导入它,但如果我不使用 Cabal(没有 *.cabal 文件)或其他东西构建包,我不知道该怎么做,我只想在 GHCI 中使用我的代码。

最佳答案

只需使用cabal install 全局安装一个包

$ cabal install old-time

它会给你一个警告,但如果你只是用它来支持 ghci 也没关系。

另请注意 the documentation警告:

This library is deprecated, please look at Data.Time in the time package instead.

关于haskell - GHCI 无法为 ‘System.Time’ 加载接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58150101/

相关文章:

haskell - 真正无序的折叠包

haskell - 使用 hamlet 渲染静态 HTML

haskell - 不明确的出现 `Just'

haskell - 如何卸载旧版本的 GHC?

Haskell列表理解,从数字列表中删除整数

Haskell:将lambda表达式与绑定(bind)函数一起使用时的变量范围

haskell - 你如何指定在 Cabal 文件中输出可执行文件的位置?

haskell - GHC/Haskell 平台安装的任何地方

haskell - ghc 7.4.1 不生成 stub.o 文件

haskell - 如何获得 ByteString 的 Ptr?