endojs / Jessie

Tiny subset of JavaScript for ocap-safe universal mobile code
Apache License 2.0
281 stars 16 forks source link

value inequality (lists, sets, maps) #23

Closed dckc closed 5 years ago

dckc commented 5 years ago

If I were to implement Jessie using something that has a notion of value-equality for maps, lists, and sets, like python, Monte, or scala, this would be particularly inconvenient:

> const xx = new Map([[1, 2], [3, 4]])
> const yy = new Map([[1, 2], [3, 4]])
> xx
Map { 1 => 2, 3 => 4 }
> yy
Map { 1 => 2, 3 => 4 }
> xx == yy
false

Is there a feasible answer besides too bad :-P ?

michaelfig commented 5 years ago

Not sure if this is relevant, but Jessie only has ===, not ==, so it is expected only to have value equality for strings and numbers.

erights commented 5 years ago

A brief tutorial on JavaScript equality

JavaScript has no value equality. === on objects is identity equality. == is neither identity nor value equality, nor anything sensible. It is a pile of insane coercions followed by a ===.

Since WeakMap and WeakSet accept only objects as keys, the equality they use is equivalent to === on these object keys.

Ignoring insane == which we omit from Jessie, JavaScript has three abstract equality operations that differ on their handling of NaN and -0 but are otherwise equivalent. All three of these are unfortunately present in Jessie as well, since NaN and -0 are present, as are the constructs that apply each of these three. The fourth case below is more of an abstraction leak and is fortunately absent from Jessie.

I wrote all this from memory. Someone should check it against the spec. In particular, I'm not sure I'm remembering indexOf and switch case matching accurately.

dckc commented 5 years ago

I didn't mean anything by == vs ===.

It's pretty clear that the bottom line is: Too bad. JavaScript is what it is. Yes, if you're implementing Jessie in something where all maps from 1 to 2 and 3 to 4 are indistinguishable, you'll have to add some creation-marker thingy on the side to distinguish them in Jessie.