Skip to content
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

Merged

Conversation

tmccarthy
Copy link
Contributor

Currently we use Float when doing laws testing for Invariant[Fractional] in AlgebraInvariantSuite. This in turn requires us to define an Eq[Fractional[Float]], which can only be done using DeprecatedEqInstances.catsLawsEqForFn1 (here). In order to stop using this deprecated instances, in this change we now use MiniInt to do our laws testing for Invariant[Fractional]. Because MiniInt has an ExhaustiveCheck instance, we no longer have to use the deprecated Eq[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 the Invariant[Fractional] instance, and there is no "MiniFloat" type that is both small enough to have an ExhaustiveCheck instance and fractional. See #4033 for a broader discussion of this.

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.
Comment on lines 333 to 336
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])

Copy link
Contributor Author

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.

Copy link
Member

@armanbilge armanbilge left a 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))
Copy link
Member

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.

Suggested change
implicit private val arbFractionalFloat: Arbitrary[Fractional[MiniInt]] = Arbitrary(Gen.const(fractionalForMiniInt))
private val arbFractionalMiniInt: Arbitrary[Fractional[MiniInt]] = Arbitrary(Gen.const(fractionalForMiniInt))

Copy link
Member

@armanbilge armanbilge Jun 4, 2022

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])
}

Copy link
Member

@armanbilge armanbilge left a 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!

@armanbilge armanbilge changed the title Use spurious Fractional[MiniInt] to test Invariant[Fractional] Use spurious Fractional[MiniInt] to test Invariant[Fractional] Jun 5, 2022
@armanbilge armanbilge added this to the 2.8.0 milestone Jun 5, 2022
@tmccarthy
Copy link
Contributor Author

Thanks @armanbilge for working with me on this one!

@armanbilge armanbilge merged commit 5f0a292 into typelevel:main Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants