代码之家  ›  专栏  ›  技术社区  ›  Damian Nadales

显示集成收缩限制的示例

  •  1
  • Damian Nadales  · 技术社区  · 6 年前

    我只是看着 a video 整体收缩 用于基于属性的测试。这种方法似乎比其他方法有一些优势 然而,有人在报告中指出 this reddit thread 综合收缩法不适用于一元发生器:

    以你的方式进行收缩并不适合发电机的一元风格。下面是一个例子,考虑生成一个任意列表(现在忽略终止):

    do x <- arbitrary
       xs <- arbitrary
       return (x:xs)
    

    hedgehog 似乎能够找到列表中失败属性的最小反例,我想知道是否有一个例子可以显示上面引用中指出的缺点。

    2 回复  |  直到 6 年前
        1
  •  2
  •   typesanitizer    6 年前

    在微积分术语中,问题是你没有遵循负梯度(最陡下降),而是先沿一个轴最小化,然后沿另一个轴最小化。基于这个类比,很容易想出至少一个捏造的例子——考虑函数。

    -- f x y = ((x^2 - 1)^2 - 0.2*x) * ((y^2 - 1/2)^2 - 0.1*y)
    f x y = (x^4 - 2.2*x^2 + 1) * (y^4 - 1.1*y^2 + 1/4)
    

    See plot on WolframAlpha

    我们正在测试它的性能 f x y > 0 ,假设一个最小的例子有一个离原点最近的点 (0, 0) . 取决于你第一次开始收缩的地方,你完全有可能最终接近 (±1, 0) x 首先,然后不允许 y 改变很多。然而,在理想的情况下,你可能会想去一个离你很近的地方 (0, ±1/2) 满足最小值准则。

        2
  •  0
  •   Damian Nadales    6 年前

    {-# LANGUAGE OverloadedStrings #-}
    
    module Main where
    
    import Hedgehog
    import qualified Hedgehog.Gen as Gen
    import qualified Hedgehog.Range as Range
    
    notLargeOrBothNotEmpty :: Property
    notLargeOrBothNotEmpty = property $ do
      xs <- forAll randomIntLists
      ys <- forAll randomIntLists
      assert $ length xs < 4 && (xs /= [] && ys /=[])
      where
        randomIntLists = Gen.frequency
          [ (1, Gen.list (Range.constant 0 1) randomInt)
          , (10, Gen.list (Range.constant 1 100) randomInt)
          ]
        randomInt = Gen.integral (Range.constat 1 10)
    
    main :: IO Bool
    main = checkParallel $ 
      Group "Test.Example" [("Produce a minimal counter-example", notLargeOrBothNotEmpty)]
    
    

    ( [ 1 , 1 , 1 , 1 ], []) . 然而 ([], []) hedgehog ).

    4 <= length xs || (xs == [] && ys == [])
    

    如果最初发现反例,在哪里 ys /= [] 4 <= length xs xs 首先,然后将继续收缩 ys 保持 xs 常量,如我在原始问题中引用的帖子所述。