The logic does not generalize to things outside of the training set. It cannot reason about code very well, but it can write you functions with memorized docs.
I am fairly certain variations of this are in the training set. The tokens following that about "in reality Zs not having Ys" are due to X, Y, and Z being incongruous in the rest of the data.
It is not not performing a logical calculation, it is predicting the next token.
Explanations of simple logical chains are also in the training data.
Think of it instead as really good (and flexible) language templates. It can fill in the template for different things.
> It is not not performing a logical calculation, it is predicting the next token.
Those two things are not in any way mutually exclusive. Understanding the logic is an effective way to accurately predict the next token.
> I am fairly certain variations of this are in the training set.
Yes, which is probably how ChatGPT learned that logical principle. It has now learned to correctly apply that logical principle to novel situations. I suspect that this is very similar to how human beings learn logic as well.