确保循环结束的机制

时间:2021-11-18 11:33:47

When I was in college we had a guest lecture from David Parnas. In it he mentioned a mechanism that is used to guarantee that a loop (while loop, for loop, etc.) exits safely at some point. He scoffed at the fact that nobody knew what it was....the sad thing is that years later I don't know either. Does anyone know what this mechanism is called?

当我在大学时,我们接受了David Parnas的客座讲座。在其中他提到了一种机制,用于保证循环(while循环,for循环等)在某些时候安全退出。他嘲笑这样一个事实:没有人知道它是什么......可悲的是,多年后我也不知道。有谁知道这个机制叫什么?

4 个解决方案

#1


Judging by his area of expertise (which includes formal proofs of correctness), I think he meant the loop variant, which is a general technique used to prove loop termination.

从他的专业领域(包括正确的正式证明)来判断,我认为他的意思是循环变量,这是一种用于证明循环终止的通用技术。

#2


This question seems illogical.

这个问题似乎不合逻辑。

There is no one gaurantee to ensure a loop exits other than create a post condition that you know will be hit, i.e. increment a counter, don't touch the counter inside the loop and have a maximum for it to hit.

除了创建一个你知道会被击中的后置条件,即没有一个gaurantee确保循环退出,即增加一个计数器,不要触摸循环内的计数器并且有一个最大值可以击中它。

You could also create a timer or another structure to check if the loop is taking too long.

您还可以创建计时器或其他结构来检查循环是否花费太长时间。

What was Parnas implying here? A universal exit to any loop that doesn't interfere with the logic or simply designing proper post conditions?

Parnas在这里暗示什么?任何循环的通用出口,不会干扰逻辑或只是设计适当的后置条件?

#3


There are many ways to guarantee that a loop exits safely:

有很多方法可以保证循环安全退出:

  1. Don't write it to be infinite
  2. 不要把它写成无限

  3. The others are left as an excercise
  4. 其他人留下来作为练习

Seriously, the best way is to keep it simple, and to have a stable and fully tested termination condition (ie. the code that checks whether it should run another iteration or exit).

说真的,最好的方法是保持简单,并获得稳定且经过充分测试的终止条件(即检查是否应该运行另一次迭代或退出的代码)。

What you have described could very well be translated into the halting problem, which is a well known problem with no solution.

您所描述的内容很可能被转化为暂停问题,这是一个众所周知的问题,没有解决方案。

Also, a google search reveals that David Parnas works at the University of Limerick, you could call them up and ask to talk to him. I'm sure he would be pleased you're still trying to learn from what he taught you back then :)

此外,谷歌搜索显示大卫帕纳斯在利默里克大学工作,你可以打电话给他们并要求与他交谈。我相信他会很高兴你还在努力学习他当时教给你的东西:)

#4


Your guest lecturer might have been referring to Cycle Detection, a method of detecting infinite loops.

您的客座讲师可能一直在指循环检测,这是一种检测无限循环的方法。

This blog post describes the detection of infinite loops in a linked list. Interestingly, it uses the same terminology as the Wikipedia article.

这篇博文描述了链表中无限循环的检测。有趣的是,它使用与*文章相同的术语。

#1


Judging by his area of expertise (which includes formal proofs of correctness), I think he meant the loop variant, which is a general technique used to prove loop termination.

从他的专业领域(包括正确的正式证明)来判断,我认为他的意思是循环变量,这是一种用于证明循环终止的通用技术。

#2


This question seems illogical.

这个问题似乎不合逻辑。

There is no one gaurantee to ensure a loop exits other than create a post condition that you know will be hit, i.e. increment a counter, don't touch the counter inside the loop and have a maximum for it to hit.

除了创建一个你知道会被击中的后置条件,即没有一个gaurantee确保循环退出,即增加一个计数器,不要触摸循环内的计数器并且有一个最大值可以击中它。

You could also create a timer or another structure to check if the loop is taking too long.

您还可以创建计时器或其他结构来检查循环是否花费太长时间。

What was Parnas implying here? A universal exit to any loop that doesn't interfere with the logic or simply designing proper post conditions?

Parnas在这里暗示什么?任何循环的通用出口,不会干扰逻辑或只是设计适当的后置条件?

#3


There are many ways to guarantee that a loop exits safely:

有很多方法可以保证循环安全退出:

  1. Don't write it to be infinite
  2. 不要把它写成无限

  3. The others are left as an excercise
  4. 其他人留下来作为练习

Seriously, the best way is to keep it simple, and to have a stable and fully tested termination condition (ie. the code that checks whether it should run another iteration or exit).

说真的,最好的方法是保持简单,并获得稳定且经过充分测试的终止条件(即检查是否应该运行另一次迭代或退出的代码)。

What you have described could very well be translated into the halting problem, which is a well known problem with no solution.

您所描述的内容很可能被转化为暂停问题,这是一个众所周知的问题,没有解决方案。

Also, a google search reveals that David Parnas works at the University of Limerick, you could call them up and ask to talk to him. I'm sure he would be pleased you're still trying to learn from what he taught you back then :)

此外,谷歌搜索显示大卫帕纳斯在利默里克大学工作,你可以打电话给他们并要求与他交谈。我相信他会很高兴你还在努力学习他当时教给你的东西:)

#4


Your guest lecturer might have been referring to Cycle Detection, a method of detecting infinite loops.

您的客座讲师可能一直在指循环检测,这是一种检测无限循环的方法。

This blog post describes the detection of infinite loops in a linked list. Interestingly, it uses the same terminology as the Wikipedia article.

这篇博文描述了链表中无限循环的检测。有趣的是,它使用与*文章相同的术语。