确定循环不变式的最佳方法是什么?

发布于 2024-09-03 02:49:51 字数 56 浏览 2 评论 0原文

当使用形式方面创建一些代码时,是否有确定循环不变式的通用方法,或者根据问题的不同,它会完全不同吗?

When using formal aspects to create some code is there a generic method of determining a loop invariant or will it be completely different depending on the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风铃鹿 2024-09-10 02:49:51

已经有人指出,同一个循环可以有多个不变量,而可计算性对你不利。这并不意味着你不能尝试。

事实上,您正在寻找一个归纳不变量:“不变量”这个词也可以用于表示在每次迭代中都为真的属性,但仅仅知道它在一次迭代中保持不变还不够。推断它在下一个成立。如果I是归纳不变量,则I的任何结果都是不变量,但可能不是归纳不变量。

您可能试图获得归纳不变量来证明循环在某些定义的情况(前提条件)下的特定属性(后置条件)。

有两种启发式方法效果很好:

  • 从你所拥有的(前提条件)开始,然后减弱,直到你得到归纳不变量。为了直观地了解如何弱化,请应用一次或多次前向循环迭代,并查看您所拥有的公式中哪些不再为真。

  • 从你想要的(后置条件)开始并加强,直到你有了归纳不变量。要直观地了解如何强化,请向后应用一个或多个循环迭代,并查看需要添加哪些内容,以便推导出后置条件。

如果你想让电脑帮助你练习,我可以推荐Jessie演绎验证插件-用于 Frama-C 的 C 程序。还有其他的,特别是 Java 和 JML 注释,但我对它们不太熟悉。尝试你想到的不变量比在纸上计算它们要快得多。我应该指出,验证一个属性是否是归纳不变量也是不可判定的,但现代自动证明器在许多简单的例子上做得很好。如果您决定走这条路,请从列表中尽可能多地选择:Alt-ergo、Simplify、Z3。

通过可选的(安装稍微困难的)库 Apron,Jessie 还可以自动推断一些简单的不变量。

It has already been pointed out that one same loop can have several invariants, and that Calculability is against you. It doesn't mean that you cannot try.

You are, in fact, looking for an inductive invariant: the word invariant may also be used for a property that is true at each iteration but for which is it not enough to know that it hold at one iteration to deduce that it holds at the next. If I is an inductive invariant, then any consequence of I is an invariant, but may not be an inductive invariant.

You are probably trying to get an inductive invariant to prove a certain property (post-condition) of the loop in some defined circumstances (pre-conditions).

There are two heuristics that work quite well:

  • start with what you have (pre-conditions), and weaken until you have an inductive invariant. In order to get an intuition how to weaken, apply one or several forward loop iterations and see what ceases to be true in the formula you have.

  • start with what you want (post-conditions) and strengthen until you have an inductive invariant. To get the intuition how to strengthen, apply one or several loop iterations backwards and see what needs to be added so that the post-condition can be deduced.

If you want the computer to help you in your practice, I can recommend the Jessie deductive verification plug-in for C programs of Frama-C. There are others, especially for Java and JML annotations, but I am less familiar with them. Trying out the invariants you think of is much faster than working out if they work on paper. I should point out that verifying that a property is an inductive invariant is also undecidable, but modern automatic provers do great on many simple examples. If you decide to go that route, get as many as you can from the list: Alt-ergo, Simplify, Z3.

With the optional (and slightly difficult to install) library Apron, Jessie can also infer some simple invariants automatically.

江心雾 2024-09-10 02:49:51

生成循环不变量实际上很简单。例如,true 就是一个很好的例子。它满足您想要的所有三个属性:

  1. 它在循环入口之前保持
  2. 它在每次迭代之后保持
  3. 它在循环终止之后保持

但您所追求的可能是最强循环不变量。然而,找到最强的循环不变量有时甚至是一个不可判定的任务。请参阅文章可计算循环不变量的不足

It's actually trivial to generate loop invariants. true is a good one for instance. It fulfills all three properties you want:

  1. It holds before loop entry
  2. It holds after each iteration
  3. It holds after loop termination

But what you're after is probably the strongest loop invariant. Finding the strongest loop invariant however, is sometimes even an undecidable task. See article Inadequacy of Computable Loop Invariants.

柒七 2024-09-10 02:49:51

我认为实现自动化并不容易。来自维基:

由于循环和递归程序的基本相似性,证明具有不变量的循环的部分正确性与通过归纳证明递归程序的正确性非常相似。事实上,循环不变量通常是必须证明等价于给定循环的递归程序的归纳性质。

I don't think it's easy to automate this. From wiki:

Because of the fundamental similarity of loops and recursive programs, proving partial correctness of loops with invariants is very similar to proving correctness of recursive programs via induction. In fact, the loop invariant is often the inductive property one has to prove of a recursive program that is equivalent to a given loop.

花开半夏魅人心 2024-09-10 02:49:51

有许多用于查找循环不变量的启发式方法。关于这一点的一本好书是 Ed Cohen 的《Programming in the 90s》。这是关于如何通过手动操作后置条件来找到一个好的不变量。例如:用变量替换常量、加强不变量、...

There are a number of heuristics for finding loop invariants. One good book about this is "Programming in the 1990s" by Ed Cohen. It's about how to find a good invariant by manipulating the postcondition by hand. Examples are: replace a constant by a variable, strengthen invariant, ...

夏见 2024-09-10 02:49:51

我在博客中写过有关编写循环不变量的文章,请参阅 验证循环第 2 部分。证明循环正确所需的不变量通常包括两部分:

  1. 循环终止时预期状态的概括。
  2. 需要额外的位来确保循环体格式良好(例如,边界内的数组索引)。

(2) 简单明了。要导出 (1),请从表达终止后所需状态的谓词开始。它很可能包含某个数据范围内的“forall”或“exists”。现在更改“forall”或“exists”的边界,以便(a)它们依赖于循环修改的变量(例如循环计数器),以及(b)以便首次进入循环时不变式为真(通常通过将“forall”或“exists”的范围设为空)。

I've written about writing loop invariants in my blog, see Verifying Loops Part 2. The invariants needed to prove a loop correct typically comprise 2 parts:

  1. A generalisation of the state that is intended when the loop terminates.
  2. Extra bits needed to ensure that the loop body is well-formed (e.g. array indices in bounds).

(2) is straightforward. To derive (1), start with a predicate expressing the desired state after termination. Chances are it contains a 'forall' or 'exists' over some range of data. Now change the bounds of the 'forall' or 'exists' so that (a) they depend on variables modified by the loop (e.g. loop counters), and (b) so that the invariant is trivially true when the loop is first entered (usually by making the range of the 'forall' or 'exists' empty).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文