Confused by "Conflicting family instance declarations"

105 views Asked by At

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)
1

There are 1 answers

0
gregory michael travis On

Turns out, Num n here is not used when checking for conflicts.

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