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

Add FAQ section on Functor[Set] #4713

Merged
merged 1 commit into from
Feb 16, 2025
Merged

Conversation

satorg
Copy link
Contributor

@satorg satorg commented Feb 14, 2025

Based on this discussion.

I feel it would be beneficial for everyone to have this section to avoid coming back to this matter over and over again.

@satorg
Copy link
Contributor Author

satorg commented Feb 14, 2025

I'm also considering adding a special check to FunctorLaws that could enforce this rule. Currently FunctorLaws (along with ApplicativeLaws, MonadLaws) do not fail for Set unless a special edge-case type is used for data. But it is not exactly obvious for users that they should check the laws with such types.

For example, it should be fairly easy to add a specialized rule:

// defined it cats-laws
private[cats] case class Weirdo[A](value: A) {
  override def hashCode(): Int = 12345 // it can be just a constant
  override def equals(that: Any): Boolean = that match {
    case _: Weirdo[?] => true
    case _ => super.equals(that)
  }
}

Then add a dedicated rule to FunctorLaws:

def covariantCompositionUniversalEq[A](fa: F[A], f: A => Weirdo[A], g: Weirdo[A]=> A): IsEq[F[A]] =
  fa.map(f).map(g) <-> fa.map(f.andThen(g))

This way it will become apparent that neither Functor[Set] nor similar type that relies on universal equality are lawful functors.

So, wdyt?

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.

I like this and the concept of a law.

@satorg satorg merged commit 2228a80 into typelevel:main Feb 16, 2025
16 checks passed
@satorg satorg deleted the functor-set-faq branch February 16, 2025 10:07
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.

3 participants