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

implement Cats typeclass instances for Ask and Local #465

Merged
merged 3 commits into from
Jun 26, 2023

Conversation

bpholt
Copy link
Member

@bpholt bpholt commented Dec 28, 2022

This lets users map/flatMap (for Ask) or imap (for Local) the environment of a given instance and make the mapped value available in a new implicit scope.

This came up for me when trying to provide a Local[F, natchez.Span[F]], where F[_] is Kleisli[IO, Span[IO], *]. Local.baseLocalForKleisli gives us Local[Kleisli[IO, Span[IO], *], Span[IO]], which is not quite the right shape for an app with an implicit ev: Local[F, Span[F]] (which becomes ev: Local[Kleisli[IO, Span[IO], *], Span[Kleisli[IO, Span[IO], *]]]). In other words, baseLocalForKleisli gives us a local environment of Span[IO] when we need Span[Kleisli[IO, Span[IO], *]].

Given Invariant[Local[F, *]], we can do

Local[Kleisli[IO, Span[IO], *], Span[IO]].imap(Kleisli.liftK)(Kleisli.applyK(NoopSpan()))

to get the Local[Kleisli[IO, Span[IO], *], Span[Kleisli[IO, Span[IO], *]]] needed for the app.

this lets users map (or imap, for Local) the environment of a given
instance and make the mapped value available in a new implicit scope
@rossabaker
Copy link
Member

Interesting. I was thinking about your Eq[Ask], and it hit me: is there anywhere in the Cats ecosystem where we have an instance for an instance? I can see where this is useful, but it's novel enough to have me thinking why it hasn't done.

@bpholt
Copy link
Member Author

bpholt commented Feb 9, 2023

I have a vague memory of suggesting similar sorts of instances in the past, but I'm having trouble finding those PRs. Maybe it was in chat, not sure. I don't remember there being specific reasons not to do it, mostly just "huh that's different". But it's been a while; maybe I'm misremembering.

I considered just adding the map/flatMap/imap methods to Ask and Local directly, but after using cats-tagless for a while, it's been my experience that it's occasionally frustrating to have types with the methods for Cats instances, that don't actually implement the cats instances. Implementing the functionality using the typeclasses felt like it would be a little more flexible, and all else being equal, that's a good thing.

Implementing this way also lets us take advantage of the laws, which would be harder to test otherwise. Now that I think about it, that actually might be the stronger argument for doing it this way. 🙂

@armanbilge
Copy link
Member

where we have an instance for an instance

Do the Invariant[Eq] instances and stuff count? They're not higher-kinded, but they are still instances right?

https://github.com/typelevel/cats/blob/024dd3d13ac9725ea663a4f914a89aa41bc3baea/core/src/main/scala/cats/Invariant.scala#L171-L255

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.

👍 I really like this idea!

@armanbilge armanbilge merged commit 90ff651 into typelevel:main Jun 26, 2023
11 checks passed
@bpholt bpholt deleted the local-imap branch August 15, 2023 22:00
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.

None yet

3 participants