我收到一个我不太理解的冲突家庭实例错误:
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)