1
votes

I'm trying to store a crypto hash as a field in a record type, but the compiler complains:

HashTest.hs:13:1: error:
• No instance for (Data (Digest MD5))
    arising from the second field of ‘MyRecord’ (type ‘Digest MD5’)
  Possible fix:
    use a standalone 'deriving instance' declaration,
      so you can specify the instance context yourself
• When deriving the instance for (Data MyRecord)
   |
13 |                          } deriving (Show, Eq, Data, Generic)
   |                                                ^^^^

Here is the code:

{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE DeriveDataTypeable    #-}

module Playpen.HashTest.HashTest where

import Data.Generics (Data, Typeable)
import GHC.Generics (Generic)

import Crypto.Hash

data MyRecord = MyRecord { idx :: Int
                         , sig :: Digest MD5
                         } deriving (Show, Eq, Generic)

If I write a standalone "deriving instance" clause as follows ...

{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE StandaloneDeriving    #-}

module Playpen.HashTest.HashTest where

import Data.Generics (Data, Typeable)
import GHC.Generics (Generic)

import Crypto.Hash

data MyRecord = MyRecord { idx :: Int
                         , sig :: Digest MD5
                         } deriving (Show, Eq, Generic)

deriving instance Data MyRecord

... then the error becomes

HashTest.hs:16:1: error:
    • No instance for (Data (Digest MD5)) arising from a use of ‘k’
    • In the expression: ((z MyRecord `k` a1) `k` a2)
      In an equation for ‘Data.Data.gfoldl’:
          Data.Data.gfoldl k z (MyRecord a1 a2)
            = ((z MyRecord `k` a1) `k` a2)
      When typechecking the code for ‘Data.Data.gfoldl’
        in a derived instance for ‘Data MyRecord’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘Data MyRecord’
   |
16 | deriving instance Data MyRecord
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

HashTest.hs:16:1: error:
    • No instance for (Data (Digest MD5)) arising from a use of ‘k’
    • In the expression: k (k (z MyRecord))
      In an equation for ‘Data.Data.gunfold’:
          Data.Data.gunfold k z _ = k (k (z MyRecord))
      When typechecking the code for ‘Data.Data.gunfold’
        in a derived instance for ‘Data MyRecord’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘Data MyRecord’
   |
16 | deriving instance Data MyRecord
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Can anyone help me to understand this error and how to fix it?

2
What will go wrong if you do not have Data or Generic instances for this type? - Daniel Wagner
@DanielWagner as it tuns out, nothing. MyRecord is the payload of a Data.Tree. Based on examples of using that type, I had assumed that the payload had to be an instance of Data and Generic. But, having removed those from the declaration of MyRecord, my application still compiles. - Gareth Stockwell

2 Answers

2
votes

One choice would be to skip the Data and Generic instances until they're definitely needed; then at that point learn enough about Data and Generic to implement them by hand.

1
votes

To derive a Data instance for your type, all types it contains should also have that instance.

Make Digest MD5 an instance of Data too.