Haskell:基于属性的高阶函数测试 [英] Haskell: Property Based Testing for Higher Order Function

查看:100
本文介绍了Haskell:基于属性的高阶函数测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数foo必须满足的两个属性:

I have two properties that a function foo must satisfy:

prop_1 :: [Int] -> Bool
prop_1 xs = foo xs id == xs 

prop_2 :: [Int] -> (Int -> Int) -> (Int -> Int) -> Bool
prop_2 xs f g = foo (foo xs f) g == foo xs (g . f)

我正在尝试使用quickCheck检查以上属性是否满足以下功能:

I am trying to check whether the above properties satisfy the following function using quickCheck:

foo :: [a] -> (a -> b) -> [b]
foo xs f = []

当我尝试使用prop_2运行quickCheck时,出现以下错误:

When I tried running quickCheck with prop_2 I get the following error:

quickCheck(prop_2)

<interactive>:18:1: error:
     No instance for (Show (Int -> Int))
        arising from a use of 'quickCheck'
        (maybe you haven't applied a function to enough arguments?)
     In the expression: quickCheck (prop_2)
      In an equation for 'it': it = quickCheck (prop_2)

我不确定为什么会收到此错误以及如何解决该错误.任何见解都会受到赞赏.

I am not sure why I am getting this error and how I can resolve it. Any insights are appreciated.

推荐答案

作为

As the documentation on QuickCheck says:

但是,在测试这种属性之前,必须确保可以打印功能值(以防发现反例).也就是说,函数类型必须是类Show的实例.要安排此操作,您必须将模块ShowFunctions 导入每个包含此类高阶属性的模块中.如果找到反例,则函数值将显示为"<function>"

However, before we can test such a property, we must see to it that function values can be printed (in case a counter-example is found). That is, function types must be instances of class Show. To arrange this, you must import module ShowFunctions into every module containing higher-order properties of this kind. If a counter-example is found, function values will be displayed as "<function>"

因此,您可以通过导入以下模块来解决此问题:

So you can fix this by importing a module like:

import Text.Show.Functions

prop_1 :: [Int] -> Bool
prop_1 xs = foo xs id == xs 

prop_2 :: [Int] -> (Int -> Int) -> (Int -> Int) -> Bool
prop_2 xs f g = foo (foo xs f) g == foo xs (g . f)

这篇关于Haskell:基于属性的高阶函数测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆