这套定义来自 Lamport 的 TLA+ 体系。Lamport, Specifying Systems, §2.1, p.15:“we define a behavior to be a sequence of states, where a state is an assignment of values to variables.” 同书 p.18:“A behavior is an infinite sequence of states”。系统的含义就是它允许的全部行为组成的集合。
定义 · 系统允许的行为
行为 s₀, s₁, s₂, … 是状态机 (Init, Next) 允许的行为,当且仅当:
s₀ ∈ Init;
对每个 i,要么 (sᵢ, sᵢ₊₁) ∈ Next,要么 sᵢ₊₁ = sᵢ。
第 2 条中的 sᵢ₊₁ = sᵢ 叫“原地踏步”(stuttering step)。Specifying Systems, §2.2, p.17:“we can represent a terminating execution of any system by an infinite behavior that ends with a sequence of nothing but stuttering steps.” 严格说,原地踏步是“变量不变”的步;本课只有两个变量,所以等同于整个状态不变。程序停下以后,行为用原地踏步把最后一个状态无限重复下去,所以行为总是无穷长。第 3 课会细讲原地踏步。
条件属于动作,不属于步。任意两个状态都能组成一个步:本例 16 个类型正确的状态两两配对,共 256 个步。动作是对步判断真假的条件,满足动作 A 的步叫“A 步”。Next 从 256 个步里只选中 14 个,可达状态图只用到其中 10 个。Specifying Systems, p.16:“A pair of successive states … is called a step.”“A step that satisfies the action HCnxt is called an HCnxt step.”
性质(property)是行为的一个集合。Lamport 的说法是:时序公式对一个行为为真或为假(“A temporal formula is true or false of a behavior”,Specifying Systems §8.1, p.88)。一个对行为判断真假的公式,等价于“它为真的那些行为”组成的集合;本课用集合的说法,因为包含关系更直观。Alpern 与 Schneider 在 “Defining Liveness”(1985) 中沿用了这种看法,第 4 课会用到。系统 S 满足性质 P,当且仅当 beh(S) ⊆ P。
反例(counterexample)是 beh(S) 中不属于 P 的一条行为。TLC 只打印反例的有限前缀,因为前缀已经足以说明违反发生在哪里。
TLC 检查不变式时,默认用广度优先搜索(breadth-first search,BFS)计算上面的 R₀, R₁, …。Specifying Systems, §14.3.1, p.241:“TLC keeps two data structures: a directed graph G whose nodes are states, and a queue (a sequence) U of states. … The graph G is the part of the state reachability graph that TLC has found so far”。同页还规定 G 中每个状态都有指向自己的边,这正是原地踏步。TLC 启动时也会打印 “Running breadth-first search Model-Checking”。算法如下:
把每个初始状态放进“已见集合”和队列,并检查它是否满足 I。
如果队列为空,停止,报告“没有发现错误”。
从队首取出一个状态 s。
对每个动作,计算 s 的后继 t。
如果 t 已在已见集合中,丢弃 t,处理下一个后继。
如果 t 不满足 I,停止,沿父指针还原从初始状态到 t 的路径,作为反例输出。
把 t 放进已见集合和队尾,记下 t 的父状态是 s;处理完全部后继后回到第 2 步。
BFS 先处理完第 n 层,才开始第 n+1 层。所以第一个被发现的坏状态,离初始状态的步数最少。由父指针还原的反例因此是最短的。这个保证有两个前提。第一,只用一个工作线程:TLC 维护者 Markus Kuppe 指出,多线程时 TLC “only provides an approximation of the shortest counterexample”(TLA+ 邮件组)。本课用 -workers 1。第二,只针对不变式这类安全性质。
Error: Invariant AtMostOne is violated.
Error: The behavior up to this point is:
State 1: <Initial predicate>
/\ s1 = "idle"
/\ s2 = "idle"
State 2: <Checkout1 line 11, col 14 to line 11, col 67 of module MiniCheckout>
/\ s1 = "open"
/\ s2 = "idle"
State 3: <Checkout2 line 12, col 14 to line 12, col 67 of module MiniCheckout>
/\ s1 = "open"
/\ s2 = "open"
State 4: <Callback1 line 15, col 14 to line 16, col 67 of module MiniCheckout>
/\ s1 = "done"
/\ s2 = "open"
State 5: <Callback2 line 17, col 14 to line 18, col 67 of module MiniCheckout>
/\ s1 = "done"
/\ s2 = "done"
10 states generated, 9 distinct states found, 1 states left on queue.
动手 2 · 看 BFS 一步一步做什么(10 分钟)
选“原样”,反复点“单步”,对照上面的 7 步算法看每一行日志。
停下时,核对输出的三个数字和反例是否与上面 TLC 的输出一致。
选“修复:履约时再检查”,点“跑完”,看规则是否成立。
选“替代修复:锁持有到履约”,点“跑完”,数一数可达状态有几个。
“修复”指履约时在用户锁内再检查一次:另一种订阅已生效,就退款(refunded)。“替代修复”假设下单锁一直持有到履约结束:另一个订单处于 open 或 done 时,下单都被拒绝。
可达状态 10 个,边 10 条。TLC 输出 11 states generated, 10 distinct states found, 0 states left on queue。(done, done) 不在 R 里,所以 AtMostOne 是修复版的不变式。16 个类型正确的状态中有 6 个不可达。
Discussion