You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
instance Arbitrary (Ratio a) is broken in general case and sometimes throws exceptions #356
Instance Arbitrary (Ratio a) is broken in general case and sometimes throws exceptions. For example for Ratio Natural type will cause underflow exceptions during the tests.
I'm not sure it could be fixed by some constraints or not, maybe if not - it's not the best idea to have generic instance itself, and better to split it into multiple, depends on a parameter. Maybe we could have something like this (not sure, didn't tested it):
uFractional:: (Fractionala) =>Gena
uFractional =
sized $\n ->do
denom <- chooseInt (1, max1 n)
numer <- chooseInt (0, n * denom)
pure$fromIntegral numer /fromIntegral denom
In the case of Ratio a, maybe it can be fixed by not using chooseInt, but instead using the Arbitrary instance for a. E.g. instead of denom <- chooseInt (1, max 1 n), something like denom <- resize (max 1 n) (arbitrary :: Gen a) `suchThat` (>= 1).
What about implementing arbitrarySizedPositiveFractional and require Arbitrary a on Arbitrary (Ratio a) and doing something like:
instance (Integrala, Arbitrarya) =>Arbitrary (Ratioa) where
arbitrary =do
r <- arbitrarySizedPositiveFractional
s <-signum<$> arbitrary
pure$ s %1* r
shrink = shrinkRealFrac
The Ratio a is bad abstraction. It's kind of works with Natural but not really well (though this is Num issue), it kind of works with Int but not really (especially with small integers like Int8 or Int16). It's not great.
I don't actually see the problem for Natural or Int. For Int8 and friends you run into some under and overflow issues but for practical purposes that shouldn't be an issue for Int.
Another, less strict version of instance a ~ Integer => Arbitrary (Ratio a) where ... is
classArbitraryRatioawherearbitraryRatio::Gen (Ratioa)
instanceArbitraryRatioa=>Arbitrary (Ratioa) where arbitrary = arbitraryRatio
instanceArbitraryRatioIntegerinstanceArbitraryRatioNaturalinstanceArbitraryRatioInt8instanceArbitraryRatioWord8...instanceArbitraryRatioIntinstanceArbitraryRatioWord
Some of these implementation maybe same, but I don't think it's possible to figure out "good" general instance, a clearly better one than (%) <$> arbitrary <*> arbitraryNotZero (IMO that instance is very good for fixed width integral types)
I still don't see any argument against the signum instance I proposed?
I don't feel confident it covers all the cases of Ratio Int8, which an instance should (as that type is very small).
Also for something like Integer, generating one just to get its sign is not "cheap". (And that is common problem in QuickCheck, e.g. Arbitrary Word64 instance is very slow: when testing some bitfiddling code, the generation of Word64s is often taking more time than evaluating the property).
Good point! It doesn't cover -1/128 - that's something to think carefully about. Although, we don't generate extremes in QuickCheck anyway. Another thing to think about.
You could special case the instance on the expensive cases (I.e. Integer and Natural) with some OverlappingInstances.
Activity
nick8325 commentedon May 16, 2023
Hmm, that's pretty annoying!
In the case of
Ratio a, maybe it can be fixed by not usingchooseInt, but instead using theArbitraryinstance fora. E.g. instead ofdenom <- chooseInt (1, max 1 n), something likedenom <- resize (max 1 n) (arbitrary :: Gen a) `suchThat` (>= 1).MaximilianAlgehed commentedon Mar 28, 2024
What about implementing
arbitrarySizedPositiveFractionaland requireArbitrary aonArbitrary (Ratio a)and doing something like:phadej commentedon Mar 28, 2024
The
instance should be fine too.
The
Ratio ais bad abstraction. It's kind of works withNaturalbut not really well (though this isNumissue), it kind of works withIntbut not really (especially with small integers likeInt8orInt16). It's not great.MaximilianAlgehed commentedon Mar 28, 2024
I don't actually see the problem for
NaturalorInt. ForInt8and friends you run into some under and overflow issues but for practical purposes that shouldn't be an issue forInt.phadej commentedon Mar 28, 2024
https://gitlab.haskell.org/ghc/ghc/-/issues/21646
It's quite easy to overflow, even for 64bit
Int; denominators can grow surprisingly fast.OTOH, maybe
QuickCheckis a tool to figure out where a logic usingRatio Intstarts to not resemble proper rationals anymore.phadej commentedon Mar 28, 2024
Another, less strict version of
instance a ~ Integer => Arbitrary (Ratio a) where ...isSome of these implementation maybe same, but I don't think it's possible to figure out "good" general instance, a clearly better one than
(%) <$> arbitrary <*> arbitraryNotZero(IMO that instance is very good for fixed width integral types)MaximilianAlgehed commentedon Mar 28, 2024
Yes. For sure.
MaximilianAlgehed commentedon Mar 28, 2024
I still don't see any argument against the
signuminstance I proposed?phadej commentedon Mar 28, 2024
I don't feel confident it covers all the cases of
Ratio Int8, which an instance should (as that type is very small).Also for something like
Integer, generating one just to get its sign is not "cheap". (And that is common problem inQuickCheck, e.g.Arbitrary Word64instance is very slow: when testing some bitfiddling code, the generation ofWord64s is often taking more time than evaluating the property).MaximilianAlgehed commentedon Mar 28, 2024
Good point! It doesn't cover
-1/128- that's something to think carefully about. Although, we don't generate extremes in QuickCheck anyway. Another thing to think about.You could special case the instance on the expensive cases (I.e. Integer and Natural) with some OverlappingInstances.