我正在尝试使用quickcheck生成给定函数的随机参数(假设其所有类型都具有Arbitrary实例和Show实例)以及对这些参数的函数的评估。我只需要打印参数的值并在之后评估答案。所以我期待一个具有以下类型的函数
randomEvaluate :: Testable a => a -> IO ( [String] -- arguments
, String ) -- Answer after evaluating
-- IO is just needed to get a new random number generator. If I pass a generator then I think probably I will not need IO here.
我仍然不确定这里的类型,但我认为Testable a
会这样做。
我仍然无法真正得到我需要的东西。我很困惑quickcheck数据类型Rose
,Result
等等。
更新
假设我有一个功能
add :: Int -> Int -> Int
add a b = a+b
然后我假设像
这样的行为> randomEvaluate add
(["1","3"],"4")
其中1和3是为Int
生成的随机值,4是f 1 3
。
答案 0 :(得分:9)
除了模块Test.QuickCheck.Arbitrary
和Test.QuickCheck.Gen
之外,我认为您不能使用大部分QuickCheck代码。
以下是一些简单的代码,仅提供一个参数所需的函数:
import Test.QuickCheck.Arbitrary
import Test.QuickCheck.Gen
import System.Random
randomEvaluate :: (Arbitrary a, Show a, Show b) => (a -> b) -> IO (String, String)
randomEvaluate f = do
stdGen <- newStdGen
let x = unGen arbitrary stdGen 1000
let y = f x
return (show x, show y)
在这里你可以看到它的实际效果:
*Main> randomEvaluate (\(a,b) -> a + b)
("(-292,-655)","-947")
*Main> randomEvaluate (\(a,b) -> a + b)
("(586,-905)","-319")
*Main> randomEvaluate (\(a,b) -> a + b)
("(547,-72)","475")
正如您所看到的那样,如果您发现它,可以将它与具有多个参数的函数一起使用。如果这还不够,那么事情会变得有点困难,但是应该可以使用某种类型的技巧。
这是一种需要“仅”将函数的返回值包装在newtype中的方法。 (对于非Haskell98功能,这可能是可以避免的):
class RandEval a where
randomEvaluate :: StdGen -> a -> ([String], String)
newtype Ret a = Ret a
instance Show a => RandEval (Ret a) where
randomEvaluate _ (Ret x) = ([], show x)
instance (Show a, Arbitrary a, RandEval b) => RandEval (a -> b) where
randomEvaluate stdGen f = (show x : args, ret)
where (stdGen1, stdGen2) = split stdGen
x = unGen arbitrary stdGen1 1000
(args, ret) = randomEvaluate stdGen2 (f x)
doRandomEvaluate :: RandEval a => a -> IO ([String], String)
doRandomEvaluate f = do
stdGen <- newStdGen
return $ randomEvaluate stdGen f
请在此处查看:
*Main> doRandomEvaluate (\a b -> Ret (a && b))
(["False","True"],"False")
*Main> doRandomEvaluate (\a b -> Ret (a + b))
(["944","758"],"1702")
*Main> doRandomEvaluate (\a b c -> Ret (a + b + c))
(["-274","413","865"],"1004")
*Main> doRandomEvaluate (\a b c d -> Ret (a + b + c + d))
(["-61","-503","-704","-877"],"-2145")
如果还必须明确标记返回值,则可行,但使用语言扩展:
{-# LANGUAGE FlexibleInstances, UndecidableInstances, OverlappingInstances #-}
import Test.QuickCheck.Arbitrary
import Test.QuickCheck.Gen
import System.Random
import Control.Arrow
class RandEval a where
randomEvaluate :: StdGen -> a -> ([String], String)
instance (Show a, Arbitrary a, RandEval b) => RandEval (a -> b) where
randomEvaluate stdGen f = first (show x:) $ randomEvaluate stdGen2 (f x)
where (stdGen1, stdGen2) = split stdGen
x = unGen arbitrary stdGen1 1000
instance Show a => RandEval a where
randomEvaluate _ x = ([], show x)
doRandomEvaluate :: RandEval a => a -> IO ([String], String)
doRandomEvaluate f = do
stdGen <- newStdGen
return $ randomEvaluate stdGen f
以下是发布的原始用例:
*Main> doRandomEvaluate ( (+) :: Int -> Int -> Int )
(["-5998437593420471249","339001240294599646"],"-5659436353125871603")
但现在你正处于GHC如何解决重叠实例的奇思妙想。例如。即使有这个很好的(但也是非Haskell98)实例来显示布尔函数:
type BoolFun a = Bool -> a
instance Show a => Show (BoolFun a) where
show f = "True -> " ++ show (f True) ++ ", False -> " ++ show (f False)
aBoolFun :: Bool -> BoolFun Bool
aBoolFun x y = x && y
您在doRandomEvaluate
:
*Main> doRandomEvaluate aBoolFun
(["False","False"],"False")
使用原始解决方案,您可以:
*Main> doRandomEvaluate (Ret . aBoolFun)
(["False"],"True -> False, False -> False")
*Main> doRandomEvaluate (Ret . aBoolFun)
(["True"],"True -> True, False -> False")
但请注意,这是一个滑坡。对上面代码的一个小改动,它在GHC 7.6.1中停止工作(但仍然适用于GHC 7.4.1):
instance (Show a, Arbitrary a, RandEval b) => RandEval (a -> b) where
randomEvaluate stdGen f = (show x:args, ret)
where (stdGen1, stdGen2) = split stdGen
x = unGen arbitrary stdGen1 1000
(args, ret) = randomEvaluate stdGen2 (f x)
SPJ explains为什么这不是一个真正的错误 - 对我来说这是一个明确的迹象,表明这种方法将类型类hackery推得太过分了。
答案 1 :(得分:3)
QuickCheck非常简单:
Prelude> import Test.QuickCheck
提供了一个简单的驱动程序功能:
Prelude Test.QuickCheck> :t quickCheck
quickCheck :: Testable prop => prop -> IO ()
因此,定义具有'Testable'中找到的类型的东西:
Prelude Test.QuickCheck> let prop_commut a b = a + b == b + a
Prelude Test.QuickCheck> :t prop_commut
prop_commut :: (Eq a, Num a) => a -> a -> Bool
运行它:
Prelude Test.QuickCheck> quickCheck prop_commut
+++ OK, passed 100 tests.
更全面的治疗see RWH