Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何实现这样的数据类型的任意实例?_Haskell_Typeclass_Quickcheck - Fatal编程技术网

Haskell 如何实现这样的数据类型的任意实例?

Haskell 如何实现这样的数据类型的任意实例?,haskell,typeclass,quickcheck,Haskell,Typeclass,Quickcheck,我是Haskell的新手,我正在尝试编写一个测试用例来证明半群typeclass的关联定律 数据类型定义如下: newtype Combine a b = Combine {unCombine :: (a -> b)} instance (Semigroup b) => Semigroup (Combine a b) where (Combine f) <> (Combine g) = Combine (\x -> f x <> g x) 半群

我是Haskell的新手,我正在尝试编写一个测试用例来证明半群typeclass的关联定律

数据类型定义如下:

newtype Combine a b = Combine {unCombine :: (a -> b)}
instance (Semigroup b) => Semigroup (Combine a b) where 
  (Combine f) <> (Combine g) = Combine (\x -> f x <> g x)
半群的实现如下:

newtype Combine a b = Combine {unCombine :: (a -> b)}
instance (Semigroup b) => Semigroup (Combine a b) where 
  (Combine f) <> (Combine g) = Combine (\x -> f x <> g x)
所以在我的主要功能中,我可以这样测试它:

type CombineAssoc = Combine String Ordering -> Combine String Ordering -> Combine String Ordering -> Bool
  quickCheck (assocTestFunc :: CombineAssoc)
但是,我很难实现
组合b
数据类型的
任意
实例。
感谢advance提供的帮助。

您可以利用预定义的函数实例

instance (CoArbitrary a, Arbitrary b) => Arbitrary(Combine a b) where
   arbitrary = Combine <$> arbitrary
实例(共轨道a,任意b)=>任意(组合a和b),其中
任意=组合任意

附加备注:
Combine
需要一个
Show
实例,或者必须与
Blind
一起使用。否则,
quickCheck(assocTestFunc::…)
将不会进行类型检查。不仅如此,还需要一个
Eq
实例。是的,你们都是对的,是否可以为该数据类型提供Show实例和Eq实例的完整实现?谢谢@波平多不一般。如何打印函数
Integer->Bool
?您可以在Show=”“的位置添加一个虚拟实例,如
实例Show(组合a b),但它的信息量不大。如果你对
a,b
是什么更具体一些,你可能会获得更好的结果。@PoppinDouble我担心你不能,因为同样的原因:要比较两个函数,你需要对它的所有输入(可能无限多)进行评估。我建议您使用quickcheck仅在几个随机点上进行测试。也就是说,您不需要检查
f==g
,而需要检查像
这样的所有(\x->f x==gx)
。这意味着您不能为此使用nice
assocTestFunc
。很遗憾,但我们无法避免可计算性的限制。目前我看不到更好的办法。