0

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
    Does this answer your question? [Why are these family instance declarations conflicting?](https://stackoverflow.com/questions/17593730/why-are-these-family-instance-declarations-conflicting) – Joseph Sible-Reinstate Monica Apr 21 '20 at 18:27
  • Yes -- I found that just before I posted, so I included it in the self-answer. Still looking for a way to do what I'm trying to do, but this clears the error up. – gregory michael travis Apr 21 '20 at 19:13
  • 1
    "If so, wouldn't this potential conflict exist for any two instances?" Yep, and that is exactly the design of the language; you can't disambiguate instances based on the nonexistence of other instances. – Ben Apr 21 '20 at 23:02
  • 1
    Thank you, I think I'm starting to get this and your phrasing is the most succinct I've seen. – gregory michael travis Apr 23 '20 at 00:02

1 Answers1

0

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

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