3

我收到此代码的错误,我不明白冲突在哪里。

{-# LANGUAGE TypeFamilies, FlexibleContexts, FlexibleInstances,
    UndecidableInstances #-}

import Codec.Gray (integralToGray, grayToIntegral)
import Data.List (foldl', unfoldr)
import Data.Word (Word8)
import Prelude hiding (read)

class Gene g where
  type Sequence g
  write :: Sequence g -> g -> Sequence g
  read :: Sequence g -> Maybe (g, Sequence g)

instance (Gene a, Sequence a ~ [k], Integral k, Gene k, Sequence k ~ [k]) => Gene [a] where
  type Sequence [a] = Sequence a -- LINE 15
  write xs gs = Nothing -- stub
  read xs = Nothing -- stub


class (Enum g, Bounded g) => Word8Enum g where
  writeEnum :: [Word8] -> g -> [Word8]
  writeEnum xs g = Nothing -- stub

  readEnum :: g -> [Word8] -> Maybe (g, [Word8])
  readEnum _ [] = Nothing
  readEnum model (x:xs) = Nothing -- stub

instance (Word8Enum g) => Gene g where
  type Sequence g = [Word8] -- LINE 29
  write = writeEnum
  read = readEnum undefined

当我将代码加载到 GHC 中时,出现以下错误:

λ> :l amy4
[1 of 1] Compiling Main             ( amy4.hs, interpreted )

amy4.hs:15:8:
    Conflicting family instance declarations:
      type Sequence [a] -- Defined at amy4.hs:15:8
      type Sequence g -- Defined at amy4.hs:29:8
Failed, modules loaded: none.
4

2 回答 2

7

有关在未来 GHC 中允许重叠类型族的信息,请参阅此票证。还值得指出的是,对于封闭类型的族,一些(大多数?全部?)重叠是允许的。例如,以下内容不适用于开放类型族:

type family NextListElt (xs :: [*]) (a :: *) :: *
type instance NextListElt (a ': b ': xs) a = b
type instance NextListElt (b ': c ': xs) a = NextListElt (c ': xs) a

但编译为封闭类型族:

type family NextListElt (xs :: [*]) (a :: *) :: * where
  NextListElt (a ': b ': xs) a = b
  NextListElt (b ': c ': xs) a = NextListElt (c ': xs) a
于 2014-10-26T21:52:24.897 回答
6

在诸如

instance (Word8Enum g) => Gene g where
   ...

GHC 在匹配实例时只考虑实例箭头的右侧。即,不考虑约束。因此Gene g与任何其他实例重叠,尤其是Gene [a]上述实例。

在某些条件下允许重叠实例,但不允许重叠关联类型或类型族(在某些受限情况下,它们将出现在即将发布的 GHC 版本中)。因此,您会在两个Sequence声明中得到错误。

于 2013-07-11T12:56:58.597 回答