0
votes

I am getting this error:

  • No instance for (Eq T1) arising from the first field of TT' (typeMatrix T1') Possible fix: use a standalone 'deriving instance' declaration, so you can specify the instance context yourself
    • When deriving the instance for (Eq TT) | 20 | } deriving (Eq, Ord)

and I don't know why and how I can fix this ( error is the same for Ord)

Here's my code:

import Data.Matrix
data T1 = T1 { x :: Char
             , y :: Int 
             , z :: Int 
             }
instance Show T1 where
    show t1 = [(x t1)] 

data TT = TT { myMap :: Matrix T1
             , flag :: Int
             } deriving (Eq, Ord)

Any idea?

1

1 Answers

4
votes

In your example, a value of type TT contains a value of type T1; thus, to equate two values of type TT, you also need to know how to equate two values of type T1. But you haven't defined an Eq instance for T1! Add a deriving (Eq) clause to T1 and the error will go away. The same applies to Ord.

In general, if you have a type A, which contains a value of type B, in order to derive a typeclass on A, you need to derive that same typeclass on B. As described above, this occurs because in order to implement the methods of that typeclass on A, you need to already know how that typeclass behaves on B e.g. you can't equate A values unless you know how B values are equal (which is your example above). Another example: if you want to show values of type A as a String, you need to be able to convert B to a string.