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

Don't assume NRoot[Real] is perfect #1173

Merged
merged 10 commits into from
May 21, 2022
Merged

Conversation

kschwarz1116
Copy link
Contributor

Fixes #1106.

Open to other suggestions here!

Comment on lines 122 to 123
implicit val realArb: Arbitrary[Real] = Arbitrary(realFromLongs)
forAll { (x0: Real, k: Sized[Int, _1, _10]) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure if this is any nicer, but it is a bit more explicit :)

Suggested change
implicit val realArb: Arbitrary[Real] = Arbitrary(realFromLongs)
forAll { (x0: Real, k: Sized[Int, _1, _10]) =>
forAll(realFromLongs, arbitrary[Sized[Int, _1, _10]]) { (x0, k) =>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this will fix the compile errors I reckon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this worked in 3 but not in 2 😮 -- implicit resolution must have changed in 3.
I split these tests into a separate suite so that it's more clear.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we explicitly pass the generator, then we can keep them in the same file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to go that route if that's standard here - I felt kind of bad writing arbitrary[Sized[Int, _1, _10]] 3 times though 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure what Sized is, but you may be able to use Gen.chooseNum or something to achieve the same result. I think it's worth it to keep the tests together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling to find a solution that uses realFromLongs in the derivation of Arbitrary[NonZero[Real]] for this case that is compatible between scala 2 and 3 but keeps everything in one suite. It seems like where I'd previously have used an implicit def in scala 2, I should be using a given in scala 3. I suppose two suites in one file would be doable here too, but that's not far off from what's already in the PR.

I pulled in that test here because it seemed like it was susceptible to the same problem in the issue. What do you think the path forward should be?

Copy link
Member

@armanbilge armanbilge May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what the Scala 2/3 issue is here, since implicit def should also work on Scala 3. Are you saying that Arbitrary[NonZero[Real]] is not using the implicit val realArb: Arbitrary[Real] at the top of that file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so I read the compiler output again and it's not a compat issue, I'm just not sure how to provide implicit evidence properly. Sorry about the confusion.

Here's the current NonZero instance:

 implicit def nonZeroSpireImplicit[A: Signed: AdditiveGroup: Arbitrary]: Arbitrary[NonZero[A]] =
    Arbitrary(arbitrary[A].filter(_.signum != 0).map(NonZero(_)))

I want the instance of Arbitrary[A] used here to be realFromLongs, so I updated it to:

implicit def nonZeroSpireImplicit[A: Signed: AdditiveGroup](implicit arbA: Arbitrary[A]): Arbitrary[NonZero[A]] =
    Arbitrary(arbA.arbitrary.filter(_.signum != 0).map(NonZero(_)))

I was hoping to pass in realFromLongs explicitly, but in doing so I can't find evidence for Signed[Real].

Copy link
Member

@armanbilge armanbilge May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, gotcha, no worries :) IIRC before you changed it the signature was like this:

implicit def nonZeroSpireImplicit[A: Signed: AdditiveGroup: Arbitrary]: Arbitrary[NonZero[A]]

You can then explicitly pass in realFromLongs with something like this:

nonZeroSpireImplicit[Real](implicitly, implicitly, Arbitrary(realFromLongs))

Comment on lines 115 to +117
property("x.pow(3) = x * x * x") {
forAll { (x: Real) =>
x.pow(2) == x * x
x.pow(3) == x * x * x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops, thank you! :)

Comment on lines -214 to -219
// sample1("sample1 id")(x => x)
// sample1("sample1 negate")(x => -x)
// sample1("sample1 +")(x => x + x)
// sample1("sample1 *")(x => x * x)
// sample1("sample1 sqrt")(_.sqrt())
// sample1("sample1 pow(2)")(_.pow(2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what this was. It's been commented for nearly a decade now it seems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your guess is as good as mine. It seemed like a unused test that I couldn't really decipher.

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.

Looks great, thanks for iterating on this!!

@armanbilge armanbilge merged commit e361e57 into typelevel:main May 21, 2022
@kschwarz1116 kschwarz1116 deleted the fix-nroot branch May 25, 2022 03:35
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.

RealScalaCheckSuite.x.pow(k).nroot(k) = x failed
2 participants