p4-spec icon indicating copy to clipboard operation
p4-spec copied to clipboard

Clarify wording with respect to header union equality and inequality comparisons

Open kfcripps opened this issue 7 months ago • 3 comments

The spec states:

Two header unions can be compared for equality (==) or inequality (!=) if they have the same type. The unions are equal if and only if all their corresponding fields are equal (i.e., either all fields are invalid in both unions, or in both unions the same field is valid, and the values of the valid fields are equal as headers).

This part is confusing to me:

(i.e., either all fields are invalid in both unions, or in both unions the same field is valid, and the values of the valid fields are equal as headers).

To me this implies that a header union equality comparison is false if one pair of corresponding unioned headers (e.g. u1.h1 and u2.h1) are both invalid, and another pair (e.g. u1.h2 and u2.h2) are both valid and have equal fields.

Am I misunderstanding the spec here, or would the following be a more accurate description of the intended semantics?

Two header unions can be compared for equality (==) or inequality (!=) if they have the same type. The unions are equal if and only if all of their corresponding unioned headers are equal.

kfcripps avatar Jun 16 '25 18:06 kfcripps

I don't see your implication -- it clearly says the result of an equality comparison is true if a pair of fields are both valid and have equal fields.

Your alternate description is the same semantics as long as two invalid headers of the same type type are always equal, which I believe is the way header equality is specified.

ChrisDodd avatar Jun 16 '25 23:06 ChrisDodd

The part you call confusing could be rewritten this way, but only you can say if it is any more clear:

i.e. either:

(a) all fields are invalid in both unions

or

(b) in both unions the same field is valid, and the values of the valid fields are equal as headers.

In the scenario you describe, where u1.h1 and u2.h1 are both invalid, and u1.h2 and u2.h2 are both valid and have equal fields, then:

Condition (a) is false, because it is not the case that all fields are invalid in both unions.

Condition (b) is true, because in both unions the same field h2 is valid, and the values of those valid fields are equal as headers.

Thus the condition "(a) or (b)" is equal to "false or true", or true.

jafingerhut avatar Jun 16 '25 23:06 jafingerhut

I think we might have started with something like the suggested clarification originally, but that was thought to be unclear, so the current wording was added as a clarification.

The shorter description depends on the fact that two invalid headers of the same type are always equal, and a valid header is always unequal to an invalid header of the same type. Both of those are true (and specified in 8.18 -- operations on headers). That's what the parenthesized ie comment is trying to point out.

ChrisDodd avatar Jun 16 '25 23:06 ChrisDodd