nick8325 / quickcheck

Automatic testing of Haskell programs.
Other
723 stars 121 forks source link

Consider adding an unsafe orphan Eq instance for functions #232

Closed chessai closed 5 years ago

chessai commented 5 years ago

I think the following instance would be useful, using the random package:

instance (Random a, Eq b) => Eq (a -> b) where
  f == g = all (\x -> f x == g x) (take 100 (randoms (mkStdGen 7))
  -- this always has the same seed. you could get the seed from
  -- the system clock using unsafePerformIO or something

relevant issue brought up on a library i maintain: https://github.com/andrewthad/quickcheck-classes/issues/10 the gist of the issue is that checkers has EqProp, but quickcheck-classes attempts to introduce as little mental overhead as possible, instead opting to be a nice wrapper around quickcheck that makes testing of typeclass lawfulness easy. Having this instance and exposing it explicitly as an orphan instance would be useful.

phadej commented 5 years ago

There are also other Eq (a -> b) instances, e.g. http://hackage.haskell.org/package/universe-reverse-instances-1.0/docs/src/Data-Universe-Instances-Eq.html. I'd suggest to stay out of this and define a combinator (returning Property).

nick8325 commented 5 years ago

I'm going to say no here, sorry. I'm really not keen on adding an incorrect Eq instance, especially when orphan instances can easily leak out.

This instance is a bit dangerous even when used for testing. You can use it to write properties of the form f == g ==> ..., which will appear to execute correctly, but in fact will fail with spurious counterexamples. It's better if comparison of functions returns a Property - then this kind of property will fail to typecheck.

chessai commented 5 years ago

Okay, no problem. Thanks