nar*_*thi 11 haskell quickcheck
我正在使用QuickCheck 1,我有以下数据类型:
data A = ...
instance Arbitrary A where ...
data B = ...
instance Arbitrary B where ...
data C = C A B
Run Code Online (Sandbox Code Playgroud)
现在,我想定义一个Arbitrary实例C,使C值是使用现有的发电机产生A和B.我最终这样做了:
instance Arbitrary C where
arbitrary = elements [(C a b) |
a <- generate 20 (System.Random.mkStdGen 0) arbitrary,
b <- generate 20 (System.Random.mkStdGen 0) arbitrary]
Run Code Online (Sandbox Code Playgroud)
这是明确生成固定数量的值A和B必要的,还是有更好的方法将现有的结合Arbitraries到一个新的?
dav*_*420 20
我这样做:
instance Arbitrary C
where arbitrary = do a <- arbitrary
b <- arbitrary
return (C a b)
Run Code Online (Sandbox Code Playgroud)
虽然sclv liftM2从Control.Monad 使用的想法可能更好:
instance Arbitrary C
where arbitrary = liftM2 C arbitrary arbitrary
Run Code Online (Sandbox Code Playgroud)