English 中文(简体)
• 如何调整功能构成与QuickCheck的对应性
原标题:How to falsify commutativity of function composition with QuickCheck
  1. 下面的法典应该通过什么?

  2. • 如何使用测试功能。

  3. 如果不是的话,如何写?

    • 类型=[f、g] [职能]

  4. 备选案文能否接受功能? (一) 通常产生功能;和 检查

错误:

<interactive>:1:12:
    No instance for (Eq (b0 -> b0))
      arising from a use of `prop_commutative 
    Possible fix: add an instance declaration for (Eq (b0 -> b0))
    In the first argument of `quickCheck , namely `prop_commutative 
    In the expression: quickCheck prop_commutative
    In an equation for `it : it = quickCheck prop_commutative

[更新]

图2

Another writing mimic the example in Function, parse error at = in ghci let prop_commutative (Fun _ f) (Fun _ g) = (f.g) == (g.f) can run

守则:

import Test.QuickCheck.Function
import Test.QuickCheck.Gen
import Test.QuickCheck

let prop_commutative (Fun _ f) (Fun _ g) = (f.g) == (g.f)

main = quickCheck prop_commutative
问题回答

QuickCheck负责counterexamples。 因此,你需要否定你所寻求的财产:

prop1 f g x = not $ (f . g) x == (g . f) x

This particular property don t specify function type - any function of a -> a could work. So you need to specify types for f and g, or for whole function prop1.

  1. You cannot compare f . g and g . f for equality because they are both functions and you cannot have a sensible definition of Eq for functions with infinite domains in Haskell. You need to randomly generate the argument too and compare the f . g and g . f functions by comparing their results by passing a random argument to both.

  2. 阅读。 Fun_ f。 a mono吗?> 快速反应 检查不能产生不同类型的随机功能,只能产生某些特定类型的功能。 但.poly吗?>,因此不能根据具体情况推断出特定类型的 f。 因此,你需要选择<代码>f和g的某些任意类型,并在贵财产的签名类型上加以具体说明。

另外,您不需要<代码><>顶级定义>。 只能使用<代码>let.in和do内的表述。 页: 1

我从Nponeccop处收到一份说明,建议采用这一样板:

import Test.QuickCheck

prop_commutative f g x = ...

main = quickCheck $ prop_commutative f g
  where f x = ...
        g x = ...

这种做法要求你采取反措施。 简单明了:设想两个职能,如果两个方式组成,不会产生同样的结果。 这种做法还利用了部分适用。 Ive将两项职能交给prop_commutative,使之仅限x 检查生成和测试。

这可能过于简单。 如果你能够这样做,那么下一步是消除具体的反措施,说明如何迅速做到。 检查为你带来职能。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?