Fix #92: Sync.map should suspend evaluation #96
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Note the first 2 laws need to take an
fa: F[A]
parameter because we want this suspension of effects regardless of what the sourceF[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 overridingmap
orunit
, so we couldn't see thatIO.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 incats-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 inf
. Thus this needs to be coupled with a law that saysmap
needs to suspend execution, no matter what the sourceF[A]
is.This point is a little subtle because it needs a clear sample, like the one described in issue #92.