-2
  1. What is ex that should be pass to CoArbitrary of the following code?

  2. How to use Function in Test.QuickCheck.Function to represent f and g in proposition?

  3. is it correct to write , if not, how?

    where types = [f, g] :: [Function]

  4. Can variant accept Function ? as i know generate function often use >< or variant which stated in the source code of QuickCheck

The error:

<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

[Updated]

but it is not implemented CoArbitrary http://www.google.com.hk/url?sa=t&rct=j&q=QuickCheck+meiser.pdf&source=web&cd=1&ved=0CBwQFjAA&url=http%3A%2F%2Fwww.st.cs.uni-saarland.de%2Fedu%2Fseminare%2F2005%2Fadvanced-fp%2Fslides%2Fmeiser.pdf&ei=hhfHTo_ZDdCciAethMjqDw&usg=AFQjCNFF467CXacWGMkN8jvgqatkcLcVcg

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

The code:

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
4
  • 1
    Can you add more details on why you are writing such a strange quickcheck property. The property doesn't hold. It is not clear why you need a CoArbitrary instance at all, and why your instance is so strange. Seems you are blindly following someone's advice or copy-pasting the doc.
    – nponeccop
    Nov 18, 2011 at 13:08
  • I want to use QuickCheck to generate random function according to properties
    – M-Askman
    Nov 19, 2011 at 2:43
  • So you want to generate a function which composition with itself is commutative?
    – nponeccop
    Nov 19, 2011 at 12:56
  • yes, just use commutative as example, properties can be any kind of function properties
    – M-Askman
    Nov 22, 2011 at 8:58

2 Answers 2

3

QuickCheck looks for counterexamples. So you need to provide a negation of the property you seek:

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. Read the documentation on the type of Fun _ f. f there is a monomorphic function. QuickCheck cannot generate random functions of different types - it can only generate functions of some particular type. But . is polymorphic, so the particular type of f cannot be inferred from the context. So you need to choose some arbitrary types for f and g and specify it in the type signature for your property.

Also, you don't need let for top-level definitions. Let can be only used inside of expressions in the form of let..in and inside do blocks without in.

0
-1

Taking a note from nponeccop, I'd suggest this template:

import Test.QuickCheck

prop_commutative f g x = ...

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

This approach requires you to come up with a counterexample. It's simple: think of two functions that, when composed both ways, do not produce the same result. This approach also makes use of partial application. I've handed two functions to prop_commutative, leaving it with just the x for quickCheck to generate and test.

This might be too simple, though. If you can do this, then the next step is to remove the specific counterexamples and figure out how to make quickCheck generate functions for you.

5
  • The suggestion is wrong - he needs to quickCheck prop_commutative with 3 unbound arguments. In your example QuickCheck cannot generate the random functions because they are fixed. Read the documentation on 'how to make quickCheck generate functions for you' - it explains your mistakes in more details. At least where f should be inside the definition of prop_commutative, not inside the definition of main.
    – nponeccop
    Nov 18, 2011 at 22:05
  • @nponeccop I think you misunderstood my suggestion. I was suggesting he first fill in the blanks here, and get it running, and then transform it, eliminating the fixed functions.
    – Dan Burton
    Nov 18, 2011 at 23:14
  • oh, I completely missed your point indeed. A great suggestion!
    – nponeccop
    Nov 18, 2011 at 23:21
  • i feel haskell's error message is not specific enough to see where is wrong
    – M-Askman
    Nov 19, 2011 at 2:15
  • The error message says that f . g and g . f are both functions of type (b0 -> b0) and comparison operation is not defined for them. Unfortunately, it's not possible to define it so you need to work around of this problem by comparing functions by providing the same random argument(s) to both and compare their results.
    – nponeccop
    Nov 19, 2011 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.