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 laws to explain why there is no e.g. Monad[Validated] #4251

Closed
armanbilge opened this issue Jun 21, 2022 · 5 comments · Fixed by #4255
Closed

Use laws to explain why there is no e.g. Monad[Validated] #4251

armanbilge opened this issue Jun 21, 2022 · 5 comments · Fixed by #4255

Comments

@armanbilge
Copy link
Member

H/t @TonioGela, seems we could improve the Parallel docs https://typelevel.org/cats/typeclasses/parallel.html

@TonioGela
Copy link
Member

The Validated doc talks precisely about the example in the title, demonstrating that implementing Monad the ap function will get overridden and the new implementation is a short-circuiting one. What's your idea for the Parallel doc exactly?

@armanbilge
Copy link
Member Author

Thanks for that pointer! I think it would be great if the documentation could specifically reference a law that relates flatMap to ap. It's the law that specifies the correct behavior, the override default implementation is merely a convenience.

However, at this moment I am struggling to find said law, or appreciate why it doesn't need to exist 🤔

trait MonadLaws[F[_]] extends ApplicativeLaws[F] with FlatMapLaws[F] {

@johnynek
Copy link
Contributor

I believe this is the law:

def mproductConsistency[A, B](fa: F[A], fb: A => F[B]): IsEq[F[(A, B)]] =

@TonioGela
Copy link
Member

I believe this is the law:

def mproductConsistency[A, B](fa: F[A], fb: A => F[B]): IsEq[F[(A, B)]] =

Thanks @johnynek! I'll try it and I'll submit an idea asap.

@TonioGela
Copy link
Member

I opened a PR for this. BTW @johnynek I showed that overriding ap in a Validated[Monad] instance would not satisfy this law:

def flatMapConsistentApply[A, B](fa: F[A], fab: F[A => B]): IsEq[F[B]] =
  fab.ap(fa) <-> fab.flatMap(f => fa.map(f))

but since mproduct is defined in FlatMap it had no point demonstrating its faultiness. Am I right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants