博客讨论形式化验证的局限与RH的LEAN证明
来源标题:Limitations of formal verification and the LEAN proof of RH
一篇博客文章通过排序算法的例子来说明形式化验证的局限性。文章先给出"有序序列"的形式化谓词定义,再展示一个空实现——直接返回空序列——也能满足该规范,以此说明形式化规范可能无法捕捉算法的真实意图。文章标题还提到RH的LEAN证明。
以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
Your post cuts off right at "Here are a few things that this formal spec misses that may be important:" — so I'll assume you want the continuation, plus a couple of notes on the code and on carrying the analogy over to Lean/RH.
Nit in the snippet
The predicate Sorted block is missing its closing brace (and the seq<int> generic needs no import but the block needs to be closed):
predicate Sorted(s: seq<int>) {
forall i, j :: 0 <= i < j < |s| ==> s[i] <= s[j]
}
Everything else in your narrative is correct: Sorted([]) is vacuously true, |r| == |a| kills the empty output, multiset(r) == multiset(a) kills [1,1,1,1,1] and [1,2,3,3,3], and forall x :: x in a <==> x in r is the weaker set-equality spec that passes [3,2,1,1,1] → [1,2,3,3,3]. Note that the last failing version in your post is exactly the set-vs-multiset trap.
The "things it misses" list
- Stability. Nothing in the spec says two equal elements keep their relative input order.
[A1, B2, A3]sorted by the int field may legally return[B2, A1, A3]. This is a real production bug class, and it needs an index-carrying spec, not a value spec. - The comparator itself. The spec hard-codes
<=on mathematicalint. Real sorting is by a key, with a user-supplied comparator that must be a total order. If the comparator is intransitive or inconsistent, the same spec is satisfied by garbage — so the spec silently assumes a property of the comparator that it never states. - Model vs machine integers.
seq<int>is unbounded. If the implementation is 32-bit and the comparison overflows, the verified statement and the executed behavior differ. Same forfloatwithNaN, where<=is not a total order. - Termination and resource bounds. Dafny gives you termination for non-ghost methods by default, but you can opt out with
decreases *. And the spec says nothing about complexity: a Bubble Sort and a Radix Sort both satisfy it. If your actual requirement is "O(n log n)", your formal spec captures none of it. - Effects other than the return value. In-place mutation, allocation, aliasing, side effects on the input, exceptions/panics, I/O.
returns (r: seq<int>)is a pure-functional shadow of what the method really does. - Determinism. The spec permits any permutation-preserving order-preserving output — which is exactly why the multi-output freedom is a feature, but also why "the verifier accepted it" tells you nothing about whether this call returns the thing the customer expects.
- Concurrency / parallelism. Nothing about thread safety, memory ordering, or the fact that a parallel sort must still produce the same multiset.
- The meta-point, which is your real thesis. At every iteration the verifier checked code vs. spec. It never checked spec vs. intent. Your spec is the trusted base; and the trusted base is written by the same fallible human who wrote the bug. Formal verification moves trust, it doesn't eliminate it.
Carrying it to RH and Lean
The sorting story is the degenerate/underspecified-spec story. The Lean story is the same story one level up, and it's worth saying explicitly, because the two failure modes differ:
- Lean's kernel checks that the proof term has the type of the stated theorem. It does not check that the stated theorem is the Riemann Hypothesis. The gap is entirely in
Mathlib'sriemannZeta, its definition of the critical strip, of "trivial zero", ofs.re = 1/2, and of the complex exponential/tsummachinery that backs it. If any of those definitions is subtly off, the kernel happily certifies a true statement about the wrong object — the exact analogue ofSorted([]): formally impeccable, semantically empty. - A valid RH proof would still rest on a large trusted base: the Lean kernel, the
Mathlibdefinitions, the elaborator,native_decide(which trusts the compiler), anyaxiomorsorryreachable from the theorem, and the human transcription of the statement. "Machine-checked" bounds the proof step, not the statement step. - The whack-a-mole parallel is exact: for the sorting spec you kept finding weaker wordings that the verifier blessed. For RH, the analogous risk is a statement that is technically true but weaker/degenerate in a way nobody noticed — e.g. a definition of "nontrivial zero" that ends up empty, or a hypothesis that quietly excludes the interesting region. The verifier will not complain; it can't.
The one-line version for the conclusion: formal verification is a proof about the relationship between two artifacts you wrote. The hard part was never the proof — it was writing the two artifacts so that the relationship means what you wanted. Sorting makes that cheap to see; RH is the same problem at a price where the mistake is a generation of wasted effort.
Want me to write this out as the finished continuation in the post's voice (with the Dafny snippets completed and the Lean/RH section as the closing act)?
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。