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

overlaps for Data.Vector.Mutable behaves oddly for empty vectors #444

Open
cdsmith opened this issue Jul 19, 2022 · 2 comments
Open

overlaps for Data.Vector.Mutable behaves oddly for empty vectors #444

cdsmith opened this issue Jul 19, 2022 · 2 comments

Comments

@cdsmith
Copy link

cdsmith commented Jul 19, 2022

The basicOverlaps implementation for Data.Vector.Mutable is:

basicOverlaps (MVector i m arr1) (MVector j n arr2)
    = sameMutableArray arr1 arr2
      && (between i j (j+n) || between j i (i+m))
    where
      between x y z = x >= y && x < z

This will return True for two vectors where two vectors share the same mutable array and offset, but one has a zero length and the other has a non-zero length. I cannot find a precise definition of what it means for possibly-empty vectors to overlap, so it's hard to say for sure this is wrong, but it certainly disagrees with my understanding of what overlaps ought to mean.

I suggest this implementation instead:

basicOverlaps (MVector i m arr1) (MVector j n arr2)
    = sameMutableArray arr1 arr2 && i < j + n && j < i + m

That will still report an overlap for an empty vector with an offset in the middle of a containing non-empty vector, though. It's even less clear what the right answer is there. So I guess what we really need is a precise specification first, and then we can write an implementation that matches it.

@cdsmith cdsmith changed the title overlaps for Data.Vector.Mutable appears to be incorrect overlaps for Data.Vector.Mutable behaves oddly for empty vectors Jul 20, 2022
@konsumlamm
Copy link
Contributor

konsumlamm commented Jul 23, 2022

This will return True for two vectors where two vectors share the same mutable array and offset, but one has a zero length and the other has a non-zero length. I cannot find a precise definition of what it means for possibly-empty vectors to overlap, so it's hard to say for sure this is wrong, but it certainly disagrees with my understanding of what overlaps ought to mean.

I don't see any problem with this. Afaik, the only time where overlapping is relevant (apart from wanting to know if modifying one vector may change the other, which clearly isn't the case if one of them is empty) is when deciding wether to use copy or move (since the vectors may not overlap for copy), but if the source or target vector is empty, that doesn't matter.

Intuitively, I'd expect two vectors to always be non-overlapping when at least one of them is empty. The easiest way to do that is probably to simply check if either of the lengths is 0.

@cdsmith
Copy link
Author

cdsmith commented Jul 23, 2022

Makes sense. So if the definition of overlapping is that there exists a mutation to one that will also modify the other, then the existing implementation is wrong. So is my proposed replacement, though it's wrong in fewer cases. I agree, adding an explicit check if either vector is empty would be an easy way to fix it.

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

No branches or pull requests

2 participants