首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写“派生实例”声明?

如何编写“派生实例”声明?
EN

Stack Overflow用户
提问于 2019-04-21 16:41:24
回答 2查看 97关注 0票数 1

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

代码语言:javascript
复制
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)
   |                                                ^^^^

代码如下:

代码语言:javascript
复制
{-# 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)

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

代码语言:javascript
复制
{-# 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

..。那么错误就变成了

代码语言:javascript
复制
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
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

有没有人能帮我理解这个错误以及如何修复它?

EN

回答 2

Stack Overflow用户

发布于 2019-04-22 23:05:55

一种选择是跳过DataGeneric实例,直到确实需要它们;然后了解足够的DataGeneric,以便手动实现它们。

票数 2
EN

Stack Overflow用户

发布于 2019-04-21 17:03:29

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

Digest MD5也成为Data的一个实例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55781223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档