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

Fix #92: Sync.map should suspend evaluation #96

Merged
merged 4 commits into from
Dec 14, 2017

Conversation

alexandru
Copy link
Member

@alexandru alexandru commented Dec 12, 2017

PR adds laws in SyncLaws.

This can be coupled with the "map fusion" changes for PR #95 — I've separated it because this PR is about the laws in SyncLaws.

The laws we are adding:

  1. bind suspends evaluation
  2. map suspends evaluation
  3. stack-safe on repeated maps

Note the first 2 laws need to take an fa: F[A] parameter because we want this suspension of effects regardless of what the source F[A] is (Pure or not). If we don't do this, then as mentioned in issue #92 the implementation is not stack safe because the passed closure can contain a recursive function call.

I've also changed IO.ioEffect because interestingly we weren't overriding map or unit, so we couldn't see that IO.map wasn't suspending the evaluation because it wasn't being used in the tests.

Note that I prefer override def everywhere because this makes it easier to spot incompatible changes in cats-core or in our own type classes for methods that have default implementations.


Extra clarification for the added laws ...

Adding a law about stackSafetyOnRepeatedMaps is not enough to guarantee stack safety.

This is because that code by itself is stack safe even if Pure(a).map(f) has strict behavior — this is because there's no recursion in f. Thus this needs to be coupled with a law that says map needs to suspend execution, no matter what the source F[A] is.

This point is a little subtle because it needs a clear sample, like the one described in issue #92.

@codecov-io
Copy link

codecov-io commented Dec 12, 2017

Codecov Report

Merging #96 into master will increase coverage by 0.42%.
The diff coverage is 96.55%.

@@            Coverage Diff             @@
##           master      #96      +/-   ##
==========================================
+ Coverage   88.01%   88.44%   +0.42%     
==========================================
  Files          20       20              
  Lines         434      450      +16     
  Branches       35       40       +5     
==========================================
+ Hits          382      398      +16     
  Misses         52       52

@mpilquist
Copy link
Member

👍 Needs a merge conflict.

@alexandru
Copy link
Member Author

Merge conflict fixed, also added & removed some comments.

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