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

Utility functions for handling effects #231

Merged
merged 3 commits into from
Aug 9, 2024
Merged

Conversation

arybczak
Copy link
Member

@arybczak arybczak commented Aug 8, 2024

Supersedes #230.

@michaelpj @ocharles does that work for you?

I decided to go with handleWith instead of interpretWith since the name interpretWith is too close to interpret and might be confusing.

Still reads nicely:

myFunction arg `handleWith_` \case
  NotifyA i -> ...
  NotifyB s -> ...

and won't clash with Control.Monad.Catch.

@arybczak
Copy link
Member Author

arybczak commented Aug 8, 2024

Well, actually maybe interpretWith is better? Because then I can also add interposeWith for a flipped interpose. Damn naming 😅

@ocharles
Copy link

ocharles commented Aug 8, 2024

I think your comment is a good suggestion. Then we don't really introduce any new terms. So I'm +1 on fooWith meaning "flipped foo".

@michaelpj
Copy link
Contributor

Yeah, that would have been what I would have gone for.

@arybczak arybczak force-pushed the first-order-utils branch from 203f022 to f03933f Compare August 8, 2024 12:19
@arybczak arybczak force-pushed the first-order-utils branch from f03933f to cb97e86 Compare August 8, 2024 12:46
@arybczak
Copy link
Member Author

arybczak commented Aug 8, 2024

Ok, I added all possible variants, it should be good to go.

@arybczak arybczak changed the title Utitlity functions for handling effects Utility functions for handling effects Aug 8, 2024
-> EffectHandler e handlerEs
-- ^ The effect handler.
-> Eff es b
reinterpretWith runHandlerEs m handler = reinterpret runHandlerEs handler m
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm slightly sceptical of all the variants. With this one I think it's quite likely that runHandlerEs is also quite large, so is it going to make sense to privilege handler as the postfix one? I'm unsure, which makes me wonder if we should omit it for now.

Copy link
Member Author

@arybczak arybczak Aug 9, 2024

Choose a reason for hiding this comment

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

In my experience runHandlerEs is usually a single internal runner like runReader or runState (maybe both?). In the event that it's an additional setup, like

blah = reinterpret run $ \case
  ...
  where
    run m = do
      x <- setup1
      y <- setup2
      runState x $ runReader y m

reinterpretWith should actually help with this:

blah m = do
  x <- setup1
  y <- setup2
  reinterpretWith (runState x . runReader y) m $ \case
    ...

On the other hand, the handler is almost always multiline, so if you can't partially apply reinterpret, you need to bind the handler separately.

To not look very far, the code from #229 would benefit from reinterpretWith.

Am I missing something? 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

No, that's convincing!

-> EffectHandler e es
-- ^ The effect handler.
-> Eff es a
interposeWith m handler = interpose handler m
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

-> EffectHandler e handlerEs
-- ^ The effect handler.
-> Eff es b
imposeWith runHandlerEs m handler = impose runHandlerEs handler m
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as about reinterpretWith

@arybczak arybczak merged commit 14bbcfd into master Aug 9, 2024
7 checks passed
@arybczak arybczak deleted the first-order-utils branch August 9, 2024 13:56
@michaelpj
Copy link
Contributor

Thanks!

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