【问题标题】:How to write "deriving instance" declaration?如何编写“派生实例”声明?
【发布时间】:2019-04-22 23:05:55
【问题描述】:

我正在尝试将加密哈希存储为记录类型中的字段,但编译器抱怨:

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

代码如下:

{-# 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)

如果我编写一个独立的“派生实例”子句如下...

{-# 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

...那么错误就变成了

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
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

谁能帮我理解这个错误以及如何解决它?

【问题讨论】:

  • 如果您没有此类型的DataGeneric 实例会出现什么问题?
  • @DanielWagner 说出来,什么都没有。 MyRecord 是 Data.Tree 的有效负载。基于使用该类型的示例,我假设有效负载必须是 Data 和 Generic 的实例。但是,从 MyRecord 的声明中删除了这些,我的应用程序仍然可以编译。

标签: haskell deriving


【解决方案1】:

一种选择是跳过DataGeneric 实例,直到确实需要它们为止;然后在那时充分了解DataGeneric 以手动实现它们。

【讨论】:

    【解决方案2】:

    要为您的类型派生Data 实例,它包含的所有类型也应该具有该实例。

    使Digest MD5 也成为Data 的实例。

    【讨论】:

    • 您能否详细说明如何在不可见 Digest 构造函数的情况下实现 Data 实例?
    猜你喜欢
    • 2012-10-02
    • 2018-11-06
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    相关资源
    最近更新 更多