Closed leflings closed 1 year ago
I don't believe Map
makes any guarantee about which key is kept if two equivalent keys are supplied. So I the current behavior is correct, and changing it to keep the prior key would also be correct. I don't see any particular need to change it -- if "BAR" and "bar" are equivalent as keys, it shouldn't really matter which one the map keeps.
Base version: 0.15.1
Reproduced here: https://github.com/leflings/ocaml_base_map_key_issue (
dune exec -- test/map_key_bug.exe -e
)The following might all be intended behaviour, in which case you can disregard this issue, but the behaviour atleast surprised me.
I'm new to OCaml and was recently trying to solve a task where I had to count how many times unique words occur in a string.
I wanted to do this in a case insenstive manner and ended up utilizing
String.Caseless
andMap
for this.Given the following function
My expectation is, that the first occurence of a word, would stick around as the key. But in certain cases, it will get swapped during an invocation of
Map.update
Given the above function, I've written the following tests (with
Alcotest
)The 4th and 5th tests fails with the following output (abbreviated for clarity):
Inspecting https://github.com/janestreet/base/blob/v0.15/src/map.ml#L714-L748 tells me that this behaviour only happens when updating a key that is in a
Node
and not in aLeaf
(as we see in last test case). The offending line seems to be 738, where the node is reconstructed with thekey
given as an argument, rather than they existing keyv
found in the Node.It seems
Map.change
would exhibit the same behaviour.