I'm getting a conflicting family instance error that I don't quite understand:
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
-- ...
Results in
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
is not more general than [a]
because of the Num n
constraint in the instance header. These don't seem conflicting to me. Is the problem that there could be a conflict later if I implement Num
for [a]
? If so, wouldn't this potential conflict exist for any two instances?
Would functional dependencies permit this in a way that type families do not?
Complete code below:
{-# 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)