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

时间:2018-06-07 15:52:32

标签: 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’

我无法弄明白。我是否缺少进口步骤?我搜索了显式导入类型类实例的方法,但是空了。

修改 这是我的会议:

λ> 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’
λ> 

2 个答案:

答案 0 :(得分:4)

time-recurrence取决于time < 1.6,但您的会话使用time-1.9.1。这意味着导入的time包与time正在使用的time-recurrence不同,因此具有实例的UTCTime类型不在范围内。

您需要以某种方式隐藏较新的time或修复time-recurrence以构建它。

答案 1 :(得分:4)

通常,这意味着您在范围内有两个相同类型的不同版本。例如。您从UTCTime获得了time-1.9.1,但time-recurrenceUTCTime定义了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

进行修复