-
Notifications
You must be signed in to change notification settings - Fork 57
Description
When upgrading a package of mine to support random-1.3.0
, I needed to add an implementation of isInRange
to a UniformRange
instance in order to make it typecheck. There already existed an Ix
instance for the same type, and the Ix
class offers a very similar-looking inRange
method. As such, I was tempted to define isInRange
in the UniformRange
instance like so:
instance UniformRange T where
...
isInRange = inRange
I later discovered that this is incorrect! This is because UniformRange
's isInRange
requires the ranges to be symmetric, i.e.,
isInRange (lo, hi) x == isInRange (hi, lo) x
But this law does not necessarily hold for Ix
's inRange
. This can be seen in the UniformRange
and Ix
instances for Int
:
λ> isInRange (1, 0 :: Int) 0 -- UniformRange
True
λ> inRange (1, 0 :: Int) 0 -- Ix
False
There might be other differences between isInRange
and inRange
that I am not aware of, but this was one of the more prominent ones that I discovered. I worry that others may fall into the same pitfall. Would you be willing to document these differences in the Haddocks for isInRange
to make them more obvious?