haskell - Haskell 中未导入记录的类型类实例

标签 haskell module typeclass

我正在尝试使用 time-recurrence 中找到的 UTCTime 实例 CalendarTimeConvertible图书馆。它在文档中列出,但是当我将库导入 ghci 并评估 i: CalendarTimeConvertible 时,该实例不会出现。

class CalendarTimeConvertible t where
  toCalendarTime :: t -> CalendarTime
  fromCalendarTime :: CalendarTime -> Maybe t
    -- Defined in ‘time-recurrence-0.9.3:Data.Time.CalendarTime.CalendarTime’
instance CalendarTimeConvertible CalendarTime
  -- Defined in ‘time-recurrence-0.9.3:Data.Time.CalendarTime.CalendarTime’

它也不会出现在 i: UTCTime 的输出中。

data UTCTime = UTCTime {utctDay :: Day, utctDayTime :: DiffTime}
    -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Eq UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Ord UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Read UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Format.Parse’
instance Show UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.LocalTime.Internal.ZonedTime’
instance ParseTime UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Format.Parse.Instances’
instance FormatTime UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Format.Format.Instances’

我想不通。我缺少导入步骤吗?我搜索了显式导入类型类实例的方法,但一无所获。

编辑: 这是我的 session :

λ> import Data.Time.Clock
λ> :i UTCTime
data UTCTime
  = UTCTime {utctDay :: time-1.9.1:Data.Time.Calendar.Days.Day,
             utctDayTime :: DiffTime}
    -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Eq UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Ord UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Read UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Format.Parse’
instance Show UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.LocalTime.Internal.ZonedTime’
λ> import Data.Time.CalendarTime
λ> :i CalendarTimeConvertible
class CalendarTimeConvertible t where
  toCalendarTime :: t -> CalendarTime
  fromCalendarTime :: CalendarTime -> Maybe t
    -- Defined in ‘time-recurrence-0.9.3:Data.Time.CalendarTime.CalendarTime’
instance CalendarTimeConvertible CalendarTime
  -- Defined in ‘time-recurrence-0.9.3:Data.Time.CalendarTime.CalendarTime’
λ> :i UTCTime
data UTCTime
  = UTCTime {utctDay :: time-1.9.1:Data.Time.Calendar.Days.Day,
             utctDayTime :: DiffTime}
    -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Eq UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Ord UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Clock.Internal.UTCTime’
instance Read UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.Format.Parse’
instance Show UTCTime
  -- Defined in ‘time-1.9.1:Data.Time.LocalTime.Internal.ZonedTime’
λ> 

最佳答案

通常这意味着您在范围内有相同类型的两个不同版本。例如。你有UTCTime来自time-1.9.1 ,但是time-recurrence定义 UTCTime 的实例来自time-1.5.0.1 .

要检查理论,请尝试运行 ghc-pkg list time 。如果我是对的,它将列出已安装的两个不同版本。

另外,请注意 time-recurrence不支持time-1.9.1 。它对time有以下约束: :

time >= 1.4 && < 1.6

它证实了我的理论。

您可以使用 ghci -hide-package time-1.5.0.1 修复它

关于haskell - Haskell 中未导入记录的类型类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50745505/

相关文章:

python - 为什么在 Dockerfile 中使用 RUN 安装自定义 python 包不起作用?

php - 如何在 Zend Framework 中启用我的模块?

haskell - 如何轻松使 Data.List.Vector 成为 Arbitrary 的成员?

xml - 从简单的 XML 中获取数据

haskell - 无论实例如何,都在哪里使用 Haskell 类别组合?

haskell - 递归类型族

java - 使用模块初始化 Jersey 客户端的正确方法是什么?

haskell - 为什么 `succ i` 在 `i::Num a => a` 处有效(而不是 `Enum a` )?

haskell - 如何为具有两个参数的类型实例 `Functor`?

haskell - 如何为两个参数具有相同类型的对编写一个 monad 实例?