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

added a new foldRight lazy law, move forallLazy and existLazy laws #2817

Merged
merged 8 commits into from
May 5, 2019

Conversation

kailuowang
Copy link
Contributor

@kailuowang kailuowang commented Apr 28, 2019

I noticed that foldRight's laziness was tested through forallLazy and existLazy laws. Since foldRight is a type class method I think we should have the laziness law on it. In kittens we were caught by the lack of such a law.

Also I noticed that although forallLazy and existLazy are defined in UnorderedFoldableLaws, they are actually only used in FoldableTests. Moreover, implementations in UnorderedFoldable breaks these two laws. So I think they should probably be FoldableLaws.

Update: This actually caught a bug in NonEmptyChain whose Foldable.foldRight implementation is simply delegating to the native (scala std lib) foldRight.

@kailuowang kailuowang requested a review from LukaJCB April 28, 2019 02:07
@kailuowang kailuowang added this to the 2.0.0-RC1 milestone Apr 28, 2019
@kailuowang
Copy link
Contributor Author

BTW, strictly speaking this is breaking change for UnorderedFoldableLaws but it's unlikely to affect anyone and we are breaking BC in cats-laws in our next release.

@joroKr21
Copy link
Member

I guess it makes sense because someone might override forall and exists before testing the laws and then not realize that the foldRight implementation is not lazy.

@joroKr21
Copy link
Member

This mistake is actually incredibly easy to make. Here is a silly example:

    implicit val foldableSeq: Foldable[Seq] = new Foldable[Seq] {
      def foldLeft[A, B](fa: Seq[A], b: B)(f: (B, A) => B) = fa.foldLeft(b)(f)
      def foldRight[A, B](fa: Seq[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]) = fa.foldRight(lb)(f)
      override def forall[A](fa: Seq[A])(p: A => Boolean) = fa.forall(p)
      override def exists[A](fa: Seq[A])(p: A => Boolean) = fa.exists(p)
    }

@kailuowang
Copy link
Contributor Author

@joroKr21 funny that's exactly what I just discovered on NonEmptyChain

@kailuowang kailuowang added bug and removed testing labels Apr 28, 2019
@codecov-io
Copy link

codecov-io commented Apr 28, 2019

Codecov Report

Merging #2817 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2817      +/-   ##
==========================================
+ Coverage   94.15%   94.21%   +0.05%     
==========================================
  Files         368      368              
  Lines        6914     6944      +30     
  Branches      296      301       +5     
==========================================
+ Hits         6510     6542      +32     
+ Misses        404      402       -2
Impacted Files Coverage Δ
core/src/main/scala/cats/data/NonEmptyChain.scala 97.32% <ø> (-0.83%) ⬇️
...c/main/scala/cats/laws/UnorderedFoldableLaws.scala 100% <100%> (+5.26%) ⬆️
.../cats/laws/discipline/UnorderedFoldableTests.scala 100% <100%> (+12.5%) ⬆️
core/src/main/scala/cats/instances/set.scala 100% <100%> (ø) ⬆️
core/src/main/scala/cats/instances/map.scala 95.34% <100%> (+0.22%) ⬆️
...ain/scala/cats/laws/discipline/FoldableTests.scala 100% <100%> (ø) ⬆️
laws/src/main/scala/cats/laws/FoldableLaws.scala 100% <100%> (ø) ⬆️
core/src/main/scala/cats/UnorderedFoldable.scala 100% <100%> (+7.14%) ⬆️
core/src/main/scala/cats/Apply.scala 75% <0%> (ø) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7514aa6...fbc7cf8. Read the comment docs.

@kailuowang
Copy link
Contributor Author

cc @non, @ceedubs and @johnynek who may have opinion over this.

def foldRightLazy[A](fa: F[A]): Boolean = {
var i = 0
F.foldRight(fa, Eval.now("empty")) { (_, _) =>
i += 1
Copy link
Contributor

Choose a reason for hiding this comment

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

We use += here but not below. Any reason for the difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no real reason except this one is written by me and the other ones are from before. Just changed the other ones to be more consistent.

@@ -21,24 +21,6 @@ trait UnorderedFoldableLaws[F[_]] {
(F.isEmpty(fa) || F.exists(fa)(p))
} else true // can't test much in this case

def existsLazy[A](fa: F[A]): Boolean = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Foldable extends UnorderedFoldable. To me, the bug here is that we weren’t calling these in UnorderedFoldable tests and that FoldableLaws were not calling through to them as we should do any time we extend a typeclass.

That seems like the better fix to me than moving the law (since we should still want these laws on UnorderedFoldable).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does look that way but actually the default implementations of exist and forall in UnorderedFoldable do not obey these laws. These laws can only be supported in Foldable through foldRight, hence they should be in Foldable

Copy link
Member

Choose a reason for hiding this comment

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

We can keep the laws in UnorderedFoldableTests if we implement the CommutativeMonoid[Eval[Boolean]]s directly in UnorderedFoldable. See rossabaker@4af246d.

Copy link
Member

@rossabaker rossabaker left a comment

Choose a reason for hiding this comment

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

👍 to foldRightLazy.

We don't need to move the laws. See below.

@@ -21,24 +21,6 @@ trait UnorderedFoldableLaws[F[_]] {
(F.isEmpty(fa) || F.exists(fa)(p))
} else true // can't test much in this case

def existsLazy[A](fa: F[A]): Boolean = {
Copy link
Member

Choose a reason for hiding this comment

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

We can keep the laws in UnorderedFoldableTests if we implement the CommutativeMonoid[Eval[Boolean]]s directly in UnorderedFoldable. See rossabaker@4af246d.

Copy link
Contributor

@johnynek johnynek left a comment

Choose a reason for hiding this comment

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

I’d rather keep the laws the forall and exists are lazy on UnorderedFoldable.

That said, the worst case is already linear complexity so maybe that law is too restrictive anyway. Maybe the real mistake is forcing laziness on Foldable.

@johnynek
Copy link
Contributor

johnynek commented May 4, 2019

If we are going to keep a laziness law, my vote is to keep it on UnorderedFoldable, if we aren’t going to do that, I’d next prefer to remove the laziness requirement entirely.

@joroKr21
Copy link
Member

joroKr21 commented May 4, 2019

That said, the worst case is already linear complexity so maybe that law is too restrictive anyway. Maybe the real mistake is forcing laziness on Foldable.

I think one might expect the laziness from the type signature.

@rossabaker
Copy link
Member

The commit I linked above keeps the lazy laws on UnorderedFoldable, passes the tests for Map[K, ?] and Set with the new default implementation, and preserves bincompat in core. Are there any desiderata that it misses?

@johnynek
Copy link
Contributor

johnynek commented May 4, 2019

@RossBaker I think the approach is good but I have a few nits:

  1. We should override exists and forall for our concrete instances.
  2. In the Eval monoids I’d rather not reevaluate the Eval which could be expensive in principle.

@kailuowang
Copy link
Contributor Author

Thanks for all the feedbacks. I will incorporate @RossBaker's code with @johnynek's suggestions

@rossabaker
Copy link
Member

I agree with @johnynek's suggestions, but if we override for Map and Set, that will leave the default implementations untested. I'm unsure whether we've developed an idiom for testing unused defaults.

@kailuowang
Copy link
Contributor Author

@rossabaker https://github.com/typelevel/cats/blob/master/tests/src/test/scala/cats/tests/UnorderedFoldableSuite.scala is already setup to test some other default implemetations. This should be easy to add.

@kailuowang kailuowang requested a review from rossabaker May 5, 2019 01:05
@kailuowang kailuowang merged commit 287c27e into typelevel:master May 5, 2019
@kailuowang kailuowang modified the milestones: 2.0.0-RC1, 2.0.0-M2 Jun 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants