跳棋和EqProp

时间:2018-04-05 02:37:04

标签: haskell quickcheck

我正在浏览Haskell Book,有关应用程序的章节开始使用checkers库进行测试。

此库介绍了EqProp类型类,但我不了解它与Eq的区别。无论如何,要求EqProp使用Eq的大多数示例都会使用let objectsFromXHR = JSON.parse(`[ { "id": 1001, "first_name": "Rick", "last_name": "Grimes", "img": "https://ia.media-imdb.com/images/M/MV5BMjI2NDYyNjg4NF5BMl5BanBnXkFtZTcwMjI5OTMwNA@@._V1_.jpg", "career": "Sheriff", "actor": "Andrew Lincoln", "still_alive": true, "tags": ["rick","grimes","sheriff","andrew","lincoln","alive"] }, { "id": 1002, "first_name": "Daryl", "last_name": "Dixon", "img": "https://ia.media-imdb.com/images/M/MV5BMTQ5ODE4NTgzMl5BMl5BanBnXkFtZTcwODI0MjAwMw@@._V1_.jpg", "career": "unknown", "actor": "Norman Reedus", "still_alive": true, "tags": ["daryl","dixon","unknown","norman","reedus","alive"] }, { "id": 1003, "first_name": "Glenn", "last_name": "Rhee", "img": "https://ia.media-imdb.com/images/M/MV5BMTcxODE2MDgxOF5BMl5BanBnXkFtZTgwNjk2MDM1NzE@._V1_.jpg", "career": "Nerd", "actor": "Steven Yeun", "still_alive": false, "tags": ["glenn","rhee","nerd","steven","yeun","dead"] } ]`); const target = 'Glenn' console.log(objectsFromXHR.filter(each => each.first_name === target));

为什么人们使用CheChekers而不是QuickCheck?

2 个答案:

答案 0 :(得分:3)

  

这个库引入了EqProp类型类,我不明白它与Eq的区别。

来自(==)

Eq为您提供了Bool;来自(=-=)的{​​{1}}为您提供了快速检查EqProp。如果您有Property的实例,则还可以将其设为Eq实例as witnessed by eqEqProp的有趣实例是那些不是 EqProp实例的实例,例如函数(参见Li-yao Xia's answer)。

  

为什么人们使用检查器而不是Quickcheck?

Checkers是一套基于QuickCheck构建的额外工具。正如其readmeHackage description所暗示的那样,其目标之一就是可以轻松地将类法表达为Quickcheck属性。例如,请注意我将在下面引用的the applicative "test batch"的定义与我们经常编写Eq定律的方式非常相似,并且很好地自我记录。

(可以跳过顶级签名中的簿记约束。另请注意,Applicative参数是以最简单的方式 - 可能 - 工作方式,仅用于类型特化,目的是通过例如m (a,b,c)来测试undefined :: [(Int,Int,Int)]的列表实例。)

Applicative

答案 1 :(得分:2)

关键是函数和无限值没有可判定的相等(Eq),但可以测试EqProp )通过观察(随机)有限片段。