实用的非图灵完备语言?
几乎所有使用的编程语言都是图灵完备,并且这提供了代表任何可计算算法,它还带有自己的一套问题。 鉴于我编写的所有算法都是为了停止,我希望能够用一种保证它们会停止的语言来表示它们。
正则表达式用于匹配字符串和有限状态机在词法分析时使用>,但我想知道是否有一种更通用、更广泛的语言不是图灵完备的?
编辑:我应该澄清,通过“通用目的”,我不一定希望能够用该语言编写所有停止算法(我不认为这样的语言会存在),但我怀疑在停止证明中存在共同的线索,可以将其概括为产生一种保证所有算法都停止的语言。
还有另一种方法可以解决这个问题 - 消除对理论上无限内存的需求。 一旦限制了机器允许的内存量,机器所处的状态数就是有限且可数的,因此您可以确定算法是否会停止(通过不允许机器进入之前所处的状态) )。
Nearly all programming languages used are Turing Complete, and while this affords the language to represent any computable algorithm, it also comes with its own set of problems. Seeing as all the algorithms I write are intended to halt, I would like to be able to represent them in a language that guarantees they will halt.
Regular expressions used for matching strings and finite state machines are used when lexing, but I'm wondering if there's a more general, broadly language that's not Turing complete?
edit: I should clarify, by 'general purpose' I don't necessarily want to be able to write all halting algorithms in the language (I don't think that such a language would exist) but I suspect that there are common threads in halting proofs that can be generalized to produce a language in which all algorithms are guaranteed to halt.
There's also another way to tackle this problem - eliminate the need for theoretically infinite memory. Once you limit the amount of memory the machine is allowed, the number of states the machine is in is finite and countable, and therefore you can determine if the algorithm will halt (by not allowing the machine to move into a state it's been in before).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不要听反对者的话。 如果您想保证终止或简化代码(例如通过消除运行时错误的可能性),那么在某些情况下人们可能会更喜欢非图灵完整语言,这是有充分理由的。 有时,仅仅忽略某些事情可能还不够。
论文全函数式编程或多或少有说服力地认为,事实上我们几乎总是应该更喜欢这种受限制的语言,因为编译器的保证要强大得多。 能够证明程序停止本身就很重要,但实际上这是更简单的语言所提供的更容易推理的产物。 作为具有不同能力的语言层次结构中的一个组成部分,非通用语言的实用范围相当广泛。
另一个更全面地解决这种分层概念的系统是休谟。 休谟报告给出了完整的描述该系统及其五层语言的逐渐完善和安全性逐渐降低。
最后,不要忘记慈善机构。 它有点抽象,但它也是一种非常有趣的方法,它是一种有用但非通用的编程语言,它非常直接地基于范畴论的概念。
Don't listen to the naysayers. There are very good reasons one might prefer a non-Turing complete language in some contexts, if you want to guarantee termination, or simplify code, for example by removing the possibility of runtime errors. Sometimes, just ignoring things may not be sufficient.
The paper Total Functional Programming argues more or less persuasively that in fact we should almost always prefer such a restricted language because the compiler's guarantees are so much stronger. Being able to prove a program halts can be significant in and of itself, but really this is the product of the much easier reasoning that the simpler languages afford. As one component in a hierarchy of languages of varying capability, the range of utility of non-universal languages is quite broad.
Another system that addresses this layering concept much more fully is Hume. The Hume Report gives a full description of the system and its five layers of progressively more complete, and progressively less safe, languages.
And finally, don't forget Charity. It's a bit abstract, but it is also a very interesting approach to a useful but not universal programming language, which is based very directly on concepts from category theory.
BlooP(B有声循环的缩写)是一种有趣的非图灵完备语言。 它本质上是一种图灵完备的语言,有一个(主要)警告:每个循环必须包含迭代次数的限制。 不允许无限循环。 因此,可以解决 BlooP 程序的停止问题。
BlooP (short for Bounded loop) is an interesting non-Turing-complete language. It's a essentially a Turing-complete language, with one (major) caveat: every loop must contain a bound on the number of iterations. Infinite loops are not allowed. As a result, the Halting Problem can be solved for BlooP programs.
问题不在于图灵机,而在于“算法”。 您无法预测算法是否会停止的原因是:
任何不能执行递归或循环的语言都不会真正成为“通用”。
正则表达式和有限状态机是同一回事! 词法分析和字符串匹配是同一回事! FSM 停止的原因是它们从不循环; 他们只是逐个字符地传递输入并退出。
编辑:
对于许多算法来说,它们是否会停止是显而易见的。
例如:
这个函数显然永远不会停止。
而且,这个函数显然会停止:
所以底线:你可以保证你的算法停止,只需设计它就可以了。
如果您不确定算法是否会一直停止; 那么你可能无法用任何保证“停止”的语言来实现它。
The problem is not with the Turing machine, it's with "algorithm". The reason why you can't predict if an algorithm will halt or not is because of this:
Any language that can't do recursion or loops wouldn't really be "general-purpose".
Regular expressions and finite-state-machines are the same thing! Lexing and string matching are the same thing! The reason FSMs halt is because they never loop; they just pass on the input char-by-char and exit.
EDIT:
For many algorithms, it's obvious whether or not they would halt.
for instance:
This function clearly never halts.
And, this function obviously halts:
So the bottom line: you CAN guarantee that your algorithm halts, just design it so that it does.
If you are not sure whether the algorithm would halt all the time; then you probably cannot implement it in any language that guarantees "halting".
慈善并不是图灵完备的,但它不仅在理论上、说教上都很有趣(类别理论),但更重要的是,它可以解决实际问题(河内塔)。 它的力量非常强大,甚至可以表达阿克曼函数。
Charity is not Turing complete, still, it is not only theoretically, didactically interesting (category theory), but moreover, it can solve practical problems (Hanoi towers). Its strength is so great that it can express even Ackermann function.
事实证明,图灵完备相当容易。 例如,您只需要 ala BrainF**k 等 8 条指令,更多的是您实际上只需要需要一条指令。
这些语言的核心是循环结构,一旦出现无限循环,就会遇到固有的停止问题。 循环什么时候终止? 即使在支持无限循环的非图灵完备语言中,您可能仍然会停止实践中的问题。
如果您希望所有程序都终止,那么您只需仔细编写代码即可。 某种特定的语言可能更符合您的喜好和风格,但我认为任何语言都不能绝对保证生成的程序会停止。
It turns out that it is fairly easy to be turing complete. For example you only need the 8 instructions ala BrainF**k, and more to the point you really only need one instruction.
The heart of these language is a looping construct, and as soon as you have unbounded loops you have an inherent halting problem. When will the loop terminate? Even in a non-Turing complete language which supported unbounded loops you might still have the halting problem in practice.
If you want all your programs to terminate, then you just need to write your code carefully. A specific language may be more to your liking and style, but I don't think any language can guarantee absolutely that the resulting program will halt.
“消除理论上无限内存的需要。” ——嗯,是的。 任何物理计算机都受到宇宙熵的限制,甚至在此之前,还受到光速(==信息传播的最大速率)的限制。
更简单的是,在物理上可实现的计算机中,只需监视资源消耗并对其进行一些限制即可。 (即当内存或时间消耗> MY_LIMIT时,杀死进程)。
如果您要求的是纯粹的数学/理论解决方案,那么您如何定义“通用”?
"eliminate the need for theoretically infinite memory." -- well, yeah. Any physical computer is limited by the entropy of the universe and, even before that, by the speed of light (== maximum rate at which information can propagate).
Even easier, in a physically-realizable computer, just monitor resource consumption and put some bound on it. (i.e., when memory or time consumption > MY_LIMIT, kill the process).
If what you're asking is a purely mathematical / theoretical solution, how do you define "general purpose"?
恕我直言,做到这一点的正确方法是拥有一种图灵完备的语言,但提供一个用于陈述语义的系统,以便于由证明检查器处理。
然后,假设您正在故意编写一个终止程序,那么您心中就有一个关于它为何停止的很好的论据,并且使用这种新的语言,您应该能够表达该论点并证明它。
顺便说一句,在我的生产编译器中,我有递归,我知道它肯定不会在某些输入上停止。我使用一个令人讨厌的黑客来阻止这个:一个具有“合理”限制的计数器。 仅供参考,实际代码涉及单态多态代码,并且在使用多态递归时会发生无限扩展。 Haskell 捕捉到了这一点,我的 Felix 编译器却没有捕捉到(这是编译器中的一个错误,我碰巧不知道如何修复)。
根据我的一般论点..我肯定想知道哪种注释对于既定目的有好处:我碰巧控制了语言和编译器,因此只要我确切知道要做什么,我就可以轻松添加此类支持添加:)我已经看到为此目的在循环中添加了“不变”和“变体”子句,尽管我不认为该语言扩展到使用该信息来证明终止(而是检查了不变式和变体如果我没记错的话,运行时间)。
也许这值得另一个问题..
The right way to do this, IMHO, is to have a language which is Turing complete, but to provide a system for stating semantics amenable to processing by a proof checker.
Then, assuming you are writing a terminating program deliberately, you have in you mind a good argument as to why it halts, and with this new kind of language you should be able to express that argument, and have it proven.
As an aside in my production compiler I have recursions which I know, for sure, will NOT halt on certain inputs .. I use a nasty hack to stop this: a counter with a "sensible" limit. FYI the actual code is involve in monomorphising polymorphic code, and the infinite expansion occurs when using polymorphic recursion. Haskell catches this, my compiler for Felix doesn't (that's a bug in the compiler I happen not to know how to fix).
Following from my general argument .. I'd sure like to know what kinds of annotations would be good for the stated purpose: I happen to have control of a language and compiler so I could easily add such support if only I knew exactly what to add :) I have seen the addition of an "invariant" and "variant" clause to loops for this purpose, although I don't think the language extended to using that information for proof of termination (rather it checked the invariant and variant at run time if I remember correctly).
Maybe that deserves another question ..
任何非图灵完备的语言作为通用语言都不会很有用。 您也许能够找到一些标榜自己是通用语言而不是图灵完备的语言,但我从未见过。
Any non-Turing-complete language wouldn't be very useful as a general purpose language. You might be able to find something that bills itself as a general purpose language without being Turing-complete but I've never seen one.