国际象棋不变量
Chess invariants

原始链接: http://muratbuffalo.blogspot.com/2026/05/chess-invariants.html

本文通过形式化验证的视角探讨国际象棋,将该游戏视为一种交替执行的并发系统。通过定义游戏的“模型与问题”空间,作者展示了如何推导出数学不变量,以确保系统逻辑的完备性。 作者将这些不变量分为两类: * **状态不变量:** 定义有效游戏状态的谓词,例如“回合奇偶性”(TurnParity,将玩家回合与走棋步数关联)和“前一玩家未被将军”(PreviousPlayerNotInCheck,确保走棋的合法性)。 * **转移不变量:** 控制状态变化的谓词,例如“棋子数量不递增”(PieceCountNonIncreasing)和“恰好两格变化”(ExactlyTwoSquaresChange),这些谓词定义了棋子移动的物理机制。 分析强调,虽然基本棋规易于建模,但诸如王车易位或吃过路兵等复杂动作会破坏简单的平庸不变量,从而需要更细致的约束条件。最终,文章指出,即使是对国际象棋这样复杂的游戏,将其规则形式化也是发现潜在逻辑属性及识别边缘情况(例如关于兵升变的各种历史细微差别)的强大方法。

这篇 Hacker News 帖子探讨了在软件开发中构建国际象棋规则的复杂性。对话首先区分了“硬”规则(如王车易位、吃过路兵和兵的升变)与战术概念(如牵制和闪击)。 参与者澄清道,虽然牵制和闪击常被描述为规则,但它们实际上是基本移动限制下产生的自然结果。如果基础规则实现得当,这些战术会自动显现,而无需专门的逻辑代码。 当一位用户建议将这些固有的复杂性作为延长 CRUD 应用开发周期的理由时,讨论变得幽默起来。他打趣道,如果构建一个拥有 1500 年历史的国际象棋游戏都如此困难,那么像集成 Stripe 这样简单的任务势必会引发“状态不变量冲突”。帖子最后建议,对数学和形式化博弈感兴趣的人可以研究一下“棋盘复杂度”。
相关文章

原文

Chess is a lot trickier than it looks. It has so many rules: castling, en passant, pawn promotion, pinning, the discovered check, and the deadlock case of stalemate.

It is a concurrent system, but with a very specific kind of concurrency: interleaved execution. More specifically, taking turns: white, then black, then white.

You know what we do with concurrent systems here? Here we model them, and we distill their invariants.

Here is some setup definitions first.

In a CS or math paper, if you write "Section 2: Model and Problem" well enough, the rest of the paper writes itself. With this setup you can sort of see what the actions will be.

In fact, forget about the actions. Let's look at some invariants.

Invariants

When deriving invariants we ask: what must always be true? I find it useful to split the safety invariants into two camps: state invariants (which are predicates over a single state) and transition invariants (which are predicates over a step). The transition invariants are not as commonly used as state invariants, but they can be very helpful, especially when you are reasoning about transitions of a system. In the case of a system like chess, I think the transition invariants come in very handy as you may see below.

State invariants

TypeOK says every variable lives in the right space. It is boring, but it has caught more bugs than I would like to admit. OneKingPerColor and BothKingsOnBoard are also sanity checks. 

TurnParity is the first interesting one. It ties two state variables together: WHITE moves on even moves, BLACK on odd. The MakeMove action satisfies this TurnParity.

PreviousPlayerNotInCheck restates the rule that "you must end your turn not in check" as "look back: the player who just moved is not in check". NotBothInCheck is a corollary.

Transition invariants

These are predicates over a <<state, next-state>> pair, written with the bracketed form: [][P]_vars. They express how things change with constraints.  The notation is simple: x is the value of the variable x in this state, and x' denotes the value in the next-state.

MoveCountStrictlyIncreases and TurnAlternates say each step increments the move count with the colors flipping. If a transition ever messes this up, something has gone wrong.

PieceCountNonIncreasing rules out pieces appearing out of thin air. SingleCapturePerMove tightens this: at most one piece disappears per step. ExactlyTwoSquaresChange is the strongest here. It says precisely two squares change per move, the source (now empty) and the destination (now holding the moving piece). 

Haha, yes, this is a model of the basic chess rules only. A useful exercise here is to consider which of these invariants survive when we add castling, pawns, en passant?

ExactlyTwoSquaresChange gets violated when we add castling: four squares change in one move. Similarly, en passant captures a piece not on the destination square, so three squares change.

PieceCountNonIncreasing survives pawn promotion (when a pawn becomes a queen, the count is unchanged).

UPDATE: James Corey writes: As always, enumerating the rules gets you thinking even pre analysis.  Like, apparently it wasn't until 19th century that people made clear that you couldn't promote a pawn to a King, surprising an attempted checkmate by responding Le roi est mort, vive le roi!

联系我们 contact @ memedata.com