【发布时间】:2009-11-06 22:03:33
【问题描述】:
我正在处理 Real World
Haskell 其中一个
第 4 章的练习是实现一个基于foldr 的版本
concat。我认为这将是一个很好的测试候选者
QuickCheck,因为有一个现有的实现来验证我的
结果。然而,这需要我定义一个实例
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 #-} 指令
导致类型不匹配和重叠实例警告。
所以我的问题是要完成这项工作需要什么?我显然是新人 到 Haskell 并没有找到任何可以帮助我的资源。 任何指针都非常感谢。
编辑
在测试优先方式 fconcat 被定义为时,我似乎被 QuickCheck 的输出误导了
fconcat = undefined
实际上正确地实现该功能确实给出了预期的结果。 DOOP!
【问题讨论】:
标签: quickcheck haskell