Semantics of = and !=

Deadly 提交于 2019-12-20 01:55:24

问题


In XQuery,

("foo", "bar") = ("foo", "bar")

yields the value true. That seems obvious. But I noticed that

("foo", "bar") != ("foo", "bar")

also yields true, which I found rather surprising. I know that I can negate = with not($x = $y) and I've noticed that = has some kind of set intersection semantics, but can anyone explains the semantics of !=, and/or provide a reference for it?


回答1:


This can be found in the documentation for XQuery under section "3.5.2 General Comparisons".

The following example contains two general comparisons, both of which are true. This example illustrates the fact that the = and != operators are not inverses of each other.

(1, 2) = (2, 3)
(1, 2) != (2, 3)

Reading into the reasoning, it reads to me as if the rules of Atomization are to blame here. If the elements are untypedAtomic, then the parser is free to "guess" at how the comparison should be made which allows for the difference in operations based on the elements themselves rather than on any operator behavior.




回答2:


XQuery = and != are existential operators. They yield true if any element in the left set together with any element in the right set would return true for this operator (so actually same semantics for =, !=, >, ... - all the comparison operators without alphabetical characters).

("foo", "bar") != ("foo", "bar")

"foo" on the left side is != "bar" on the right side, so the whole comparison is true.

You probably want to use deep-equal for the equals-comparison and its negated version for the not-equals-comparison as you already proposed in your question.



来源:https://stackoverflow.com/questions/9196310/semantics-of-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!