2

我正在尝试为其定义一个Monoid实例,HList Record但我收到了重叠的实例错误消息。我的尝试(代码看起来很复杂,但基本上就是元组实例)

{-# LANGUAGE TypeFamilies, DataKinds, TypeOperators #-}
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
import Data.Monoid
import Data.HList.FakePrelude
import Data.HList.Record
import Data.HList.Labelable

instance (Monoid v1, Monoid v2) => 
         Monoid (Record '[Tagged (t1::Symbol) v1, Tagged (t2::Symbol) v2]) where
    mempty = (Label :: Label (t1::Symbol)) .=. mempty
          .*. (Label :: Label (t2::Symbol)) .=. mempty .*. emptyRecord
    mappend a b = (Label :: Label (t1::Symbol)) 
               .=. (a .!. (Label :: Label (t1::Symbol)))
               `mappend` (b .!. (Label :: Label (t1::Symbol)))
               .*.(Label :: Label (t2::Symbol)) 
               .=. (a .!. (Label :: Label (t2::Symbol))) 
               `mappend` (b .!. (Label :: Label (t2::Symbol)))
               .*. emptyRecord

错误信息:

Database/Harehouse/Utils.hs:185:59:
Overlapping instances for HEq Symbol l0 t1 b
  arising from a use of `.!.'
Matching instances:
  instance [overlap ok] 'False ~ b => HEq k x y b
    -- Defined in `Data.HList.TypeEqO'
  instance [overlap ok] HEq k x x 'True
    -- Defined in `Data.HList.TypeEqO'
(The choice depends on the instantiation of `t1, b, l0'
 To pick the first instance above, use -XIncoherentInstances
 when compiling the other instance declarations)
In the first argument of `mappend', namely
  `(a .!. (Label :: Label (t1 :: Symbol)))'
In the second argument of `(.=.)', namely
  `(a .!. (Label :: Label (t1 :: Symbol)))
   `mappend` (b .!. (Label :: Label (t1 :: Symbol)))'
In the first argument of `(.*.)', namely
  `(Label :: Label (t1 :: Symbol))
   .=.
     (a .!. (Label :: Label (t1 :: Symbol)))
     `mappend` (b .!. (Label :: Label (t1 :: Symbol)))'

这意味着什么 ?我怎么解决这个问题 ?(我正在使用 GHC 7.6.3)

4

1 回答 1

1

您需要ScopedTypeVariables获取 mappend 定义中的 t1 以匹配上面实例头中的那个。

此外,下一个 HList 版本将有一个像你一样工作的 Monoid 实例,除了它不限于长度为 2 的记录。http://code.haskell.org/HList/Data/HList/HList.hs是实现(的实例用RecordGeneralizedNewtypeDeriving

于 2014-07-15T03:10:11.430 回答