Haskell:创建函数实例

问题描述 投票:-3回答:1

我目前正在从很棒的Haskell from first principles中学习Haskell,并且尝试使用QuickCheck检查每种数据类型的仿函数实例时,我偶然遇到了市长为我的Function数据类型创建Function实例的问题:

Four

这将引发以下错误:

data Four' a b = Four' a a a b deriving (Eq,Show)
instance Function (Four' a b) where
  function =  let
                  injection :: (Function a, Function b) => (Four' a b) -> (a,a,a,b)
                  injection (Four' x1 x2 x3 z1) = (x1,x2,x3,z1)
                  surjection :: (Function a, Function b) => (a,a,a,b) -> (Four' a b)
                  surjection (x1,x2,x3,z1) = (Four' x1 x2 x3 z1)
              in functionMap injection surjection

而且,如果它们都生成函数,则尝试创建Error: • No instance for (Function a) arising from a use of ‘functionMap’ Possible fix: add (Function a) to the context of the type signature for: function :: forall b1. (Four' a b -> b1) -> Four' a b :-> b1 or the instance declaration • In the expression: functionMap injection surjection 实例和Function实例到底有什么区别?

haskell quickcheck
1个回答
2
投票
我有些怀疑,这确实是您想要的。对您要执行的操作的要求为Arbitrary的一些解释将有助于我们判断是否是这种情况。

但是要回答已经存在的技术问题,Arbitrary(和Function,如果您实际需要Function,您也将需要它们)具有默认的通用实现,因此您无需做或理解任何一个。

    派生CoArbitrary
  1. 写入FunctionGeneric的空实例>
  2. Function
而且,如果创建函数实例和任意实例都生成函数,它们之间到底有什么区别?

    [CoArbitrary:“如何生成和缩小{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic) import Test.QuickCheck data Four a b = ... deriving (Eq, Show, Generic) -- Derive generic -- Default implementations instance (Function a, Function b) => Function (Four a b) instance (CoArbitrary a, CoArbitrary b) => CoArbitrary (Four a b) 类型的值”]
  • Arbitrary a + a:“如何生成和收缩类型为CoArbitrary a的值(对于适当约束的Function a)”]
  • 这些不一样。
© www.soinside.com 2019 - 2024. All rights reserved.