-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use spurious Fractional[MiniInt]
to test Invariant[Fractional]
#4216
Use spurious Fractional[MiniInt]
to test Invariant[Fractional]
#4216
Conversation
Currently we use Float when doing laws testing for Invariant[Fractional]. This in turn requires us to generate an Eq[Fractional[Float]], which can only be done using DeprecatedEqInstances.catsLawsEqForFn1. In order to stop using this deprecated instances, in this change we now use MiniInt to do our laws testing for Invariant[Fractional]. Note that this approach is spurious since MiniInt is not a fractional number type. But we can use it to test the lawfulness of the Invariant[Fractional] instance, and there is no "MiniFloat" type that is both small enough to have an ExhaustiveCheck instance and fractional. See typelevel#4033 for a broader discussion of this.
checkAll("Invariant[Numeric]", InvariantTests[Numeric].invariant[MiniInt, Boolean, Boolean]) | ||
checkAll("Invariant[Integral]", InvariantTests[Integral].invariant[MiniInt, Boolean, Boolean]) | ||
checkAll("Invariant[Fractional]", InvariantTests[Fractional].invariant[MiniInt, Boolean, Boolean]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved these down here so that they are grouped with the rest of the Invariant
laws tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for trying this alternative approach. This is a lot simpler than MiniFloat
and I guess it's good enough. Just a couple minor tweaks.
implicit private val arbFractionalFloat: Arbitrary[Fractional[Float]] = Arbitrary( | ||
Gen.const(implicitly[Fractional[Float]]) | ||
) | ||
implicit private val arbFractionalFloat: Arbitrary[Fractional[MiniInt]] = Arbitrary(Gen.const(fractionalForMiniInt)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a spurious instance, probably better to pass it explicitly than have it floating around in implicit scope. Also would be good to add a comment to that effect.
implicit private val arbFractionalFloat: Arbitrary[Fractional[MiniInt]] = Arbitrary(Gen.const(fractionalForMiniInt)) | |
private val arbFractionalMiniInt: Arbitrary[Fractional[MiniInt]] = Arbitrary(Gen.const(fractionalForMiniInt)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, maybe define it only at its use site? e.g.
{
implicit private val arbFractionalMiniInt = ???
checkAll("Invariant[Fractional]", InvariantTests[Fractional].invariant[MiniInt, Boolean, Boolean])
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 thank you for chasing this down!
Fractional[MiniInt]
to test Invariant[Fractional]
Thanks @armanbilge for working with me on this one! |
Currently we use
Float
when doing laws testing forInvariant[Fractional]
inAlgebraInvariantSuite
. This in turn requires us to define anEq[Fractional[Float]]
, which can only be done usingDeprecatedEqInstances.catsLawsEqForFn1
(here). In order to stop using this deprecated instances, in this change we now useMiniInt
to do our laws testing forInvariant[Fractional]
. BecauseMiniInt
has anExhaustiveCheck
instance, we no longer have to use the deprecatedEq[A => B]
instance.Note that this approach is spurious since
MiniInt
is not a fractional number type. But we can use it to test the lawfulness of theInvariant[Fractional]
instance, and there is no "MiniFloat
" type that is both small enough to have anExhaustiveCheck
instance and fractional. See #4033 for a broader discussion of this.