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 a lawful parallel Applicative instance with derivatives #2

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

Avaq
Copy link
Owner

@Avaq Avaq commented Jul 10, 2023

The parallel Apply instance currently included isn't lawful, as demonstrated by the tests added in this PR.

The problem is that when:

  • The consumption of one of the ap'd Brackets fails; and
  • the disposal of that, or the other, Bracket also fails; then

the sequential Apply favours the E (Left "error" value) returned from the disposal, but the parallel Apply favours the E returned from the consumption. This seems like it might be easy to fix, but it turns out that there doesn't seem to be a way to reliably detect whether it was a Bracket's consumption that failed, or whether it was its disposal. Without this knowledge, the parallel Apply implementation can't choose the correct E to return.

@Avaq Avaq self-assigned this Jul 10, 2023
@Avaq Avaq force-pushed the avaq/everything branch 3 times, most recently from 320af5c to 15ad809 Compare July 11, 2023 09:05
@Avaq Avaq force-pushed the avaq/lawful-parallel branch 2 times, most recently from 0ba652b to 6f43f09 Compare July 11, 2023 12:21
@Avaq Avaq marked this pull request as ready for review July 11, 2023 12:21
if (O.isSome(a)) {
return consume(f(a.value))().then(ret => {
resolveFa(ret);
return promiseFa.then(retFa => pipe(retFa, E.apSecond(ret)));
Copy link
Owner Author

Choose a reason for hiding this comment

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

This is where the magic happens that ensures that the correct return value is used, right now. But it also prevents fab's disposal from running until fa's disposal has completed. Ideally, we'd allow both disposals to run in parallel, just as both acquisitions did.

@Avaq
Copy link
Owner Author

Avaq commented Jul 11, 2023

So the version I've currently pushed is lawful, but sequential in its disposal (parallel only in acquisition).

In practice however, I find that most services have a much faster disposal Task than acquisition Task. So I think I prefer this lawful, predictable, abstract-able, but sequentially disposing version of ApplyPar over the unlawful, surprising, abstraction-breaking, but parallel disposing version of ApplyPar.

@Avaq Avaq force-pushed the avaq/lawful-parallel branch 3 times, most recently from 798cb85 to cf42652 Compare July 11, 2023 13:21
@Avaq
Copy link
Owner Author

Avaq commented Jul 11, 2023

Okay, yeah. I'll include this changes in #1

@Avaq Avaq merged commit d1becbc into avaq/everything Jul 11, 2023
1 check passed
@Avaq Avaq deleted the avaq/lawful-parallel branch July 11, 2023 13:57
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

1 participant