假设,我有类似的类型
data D = A | B String | C String
data TestIt m = TestIt {
x :: m Int
, y :: Int -> m D
}我正在编写SmallCheck测试,所以我需要TestIt上的Serial实例
instance Monad m => Serial m (TestIt m) where
series = TestIt <$> (pure <$> series) <~> xxx如何编写这个xxx?我知道它可能需要类似CoSerial的函数,但我不知道如何编写它。当我看到CoSerial文档时,我看到CoSerial的定义中将包含Int而不是D:
instance (CoSerial m a) => CoSerial m (Int a) where
coseries rs = ???因此,我无法了解CoSerial和如何使用它们来为Int -> m D制作Serial。
此外,我还想为y 字段提供依赖串行。我的意思是,如果x有样例0,那么y的序列应该以0作为参数。有可能吗?
发布于 2019-07-10 05:48:21
例如:
newtype MyFun m = MyFun (Int -> m D)
instance (Monad m, Monad n) => Serial m (MyFun n) where
series = MyFun <$> (cons0 $ const $ pure $ A)
\/ (cons0 $ const $ pure $ B "XXX")
-- it shows how to depend on another field sample
\/ (cons0 $ \p -> pure $ C $ show p)并将y域TestIt改为MyFun m。
然后,Serial实例的TestIt变得琐碎。
B使用的是一些常量字符串C - with参数,它可以从x字段的样本中获得(这显示了随机样本之间的“依赖性”,其中之一是一个函数):
testProperty "diffProcResult" $
\(t::TestIt Identity) ->
let (MyFun f) = y t
yVal = f $ x t
in ...https://stackoverflow.com/questions/56951986
复制相似问题