题
我通过现实世界的工作
哈斯克尔中的一个
第4章的习题是实施基于foldr
版本
concat
。我认为这将是一个伟大的候选人与测试
快速检查,因为有一个现有的实现来验证我的
结果。然而,这需要我来定义的一个实例
Arbitrary
类型类,可以产生任意[[Int]]
。到目前为止,我有
一直无法弄清楚如何做到这一点。我的第一次尝试是:
module FoldExcercises_Test
where
import Test.QuickCheck
import Test.QuickCheck.Batch
import FoldExcercises
prop_concat xs =
concat xs == fconcat xs
where types = xs ::[[Int]]
options = TestOptions { no_of_tests = 200
, length_of_tests = 1
, debug_tests = True }
allChecks = [
run (prop_concat)
]
main = do
runTests "simple" options allChecks
这导致没有测试执行。纵观各个位,
作品我猜这是需要一个Arbitrary
实例声明和
加入
instance Arbitrary a => Arbitrary [[a]] where
arbitrary = sized arb'
where arb' n = vector n (arbitrary :: Gen a)
这导致ghci中说我的实例声明是
无效的,添加-XFlexibleInstances可以解决我的问题。
添加{-# OPTIONS_GHC -XFlexibleInstances #-}
指令
结果在类型不匹配和重叠实例警告。
所以需要真实,我的问题是什么,使这项工作?我显然新 哈斯克尔,我不发现,帮助我的任何资源。 任何指针赞赏。
修改
似乎我被快速检查的输出误导当在测试第一方式fconcat
被定义为
fconcat = undefined
实际执行函数正确的确给出了预期的结果。的 DOOP 强>!
解决方案
[[Int]]
已经是一个Arbitrary
实例(因为Int
是Arbitrary
实例,以便为那些本身[a]
的实例的所有a
s Arbitrary
)。所以这是没问题的。
我跑你的代码我自己(与import FoldExcercises
更换fconcat = concat
),它跑了200次测试,因为我本来期望,让我感到迷惑,为什么它不为你做它。但你并不需要添加一个Arbitrary
实例。
不隶属于 StackOverflow