0

我收到一个我不太理解的冲突家庭实例错误:

data DNum n = DNumAdd n | DNumSub n
instance Num n => V n where
  type D n = DNum n
  -- ...

data DList a = DListUpdate Int a | DListCons a
instance V [a] where
  type D [a] = DList a
  -- ...

结果是

    Conflicting family instance declarations:
      D n = DNum n -- Defined at /Users/gmt/tmi/src/Tmi.hs:16:8
      D [a] = DList a -- Defined at /Users/gmt/tmi/src/Tmi.hs:22:8

n并不比[a]因为Num n实例头中的约束更通用。这些对我来说似乎并不矛盾。Num如果我实施for以后可能会出现冲突的问题[a]吗?如果是这样,任何两个实例都不会存在这种潜在的冲突吗?

功能依赖会以类型族不允许的方式允许这样做吗?

完整代码如下:

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

module Tmi
( tmiMain ) where

import Util

class V v where
  type D v
  (.+) :: v -> D v -> v

data DNum n = DNumAdd n | DNumSub n
instance Num n => V n where
  type D n = DNum n
  x .+ (DNumAdd dx) = x + dx
  x .+ (DNumSub dx) = x - dx

data DList a = DListUpdate Int a | DListCons a
instance V [a] where
  type D [a] = DList a
  xs .+ (DListUpdate i x) = replaceAt i x
    where replaceAt i x xs = take i xs ++ [x] ++ drop (i+1) xs
  xs .+ (DListCons x) = x : xs

tmiMain = do
  msp $ 3 .+ (DNumAdd 12)
  msp $ [1, 2, 3] .+ (DListUpdate 1 20)
  msp $ [1, 2, 3] .+ (DListCons 10)
4

1 回答 1

0

事实证明,Num n在检查冲突时不使用这里。

https://stackoverflow.com/a/17594004/5265393

于 2020-04-21T18:00:54.837 回答