为什么这些家庭实例声明相互冲突? [英] Why are these family instance declarations conflicting?

查看:89
本文介绍了为什么这些家庭实例声明相互冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码出现错误,我不知道冲突在哪里.

I'm getting an error for this code, and I don't understand where the conflict is.

{-# 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中时,出现以下错误:

When I load my code into GHC, I get the following error:

λ> :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.

推荐答案

在诸如

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

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

GHC considers only the right hand side of the instance arrow when matching instances. I.e., constraints aren't considered. So Gene g overlaps with any other instance, and in particular the one for Gene [a] above.

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

Overlapping instances are allowed under certain conditions, but overlapping associated types or type families are not (they will be, in some restricted cases, in upcoming releases of GHC). Therefore, you get the error on the two Sequence declarations.

这篇关于为什么这些家庭实例声明相互冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆