TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Proving that C++'s grammar is undecidable

76 点作者 vitaut大约 5 年前

14 条评论

amluto大约 5 年前
The algorithm in the article has an important bug. It’s a basic search: pop an element from a queue, see if it’s a match and, if not, push two new elements. If the queue is empty, declare failure. Think about this for a moment. Each iteration, the queue length increases by one. Of course it’s never empty. So the algorithm will eventually declare success on a positive input and will run forever on negative input. It <i>has</i> to be this way, since PCP is equivalent to the halting problem. There is no algorithm that solves it correctly and always halts. This is the whole point.<p>This means that this program does not prove that parsing C++ is undecidable in the way the author thinks. There is no term that is a type or a value depending on the solution to an undecidable problem. Instead, there is a term that can not be resolved in finite time. This is a necessity property of pretty much any Turing-complete metaprogramming system.<p>(What I think is actually going on that’s a bit unique to C++ is that C++ cannot be parsed unambiguously. The fact that the grammar depends on whether a term is a type or value and that you can’t determine this from the AST without doing things like expanding templates is nasty.)
identity0大约 5 年前
Am I missing something? The author says that a solution to the Post Correspondence Problem with infinite dominoes in finite time is impossible. He then writes a bunch of C++ code that can solve the PCP for any arbitrary list of dominoes. Did he realize that this list must be finite since it’s written into the C++ code? If the list had infinitely many dominoes it would require infinite space to store and infinite time to compile anyways, PCP or not. That’s like saying Java is undecidable because you can write List&lt;List&lt;List&lt;List&lt;...&gt;&gt;&gt;&gt; and have something take infinitely long to compile.
评论 #23014921 未加载
评论 #23014887 未加载
评论 #23014856 未加载
评论 #23014896 未加载
qppo大约 5 年前
Isn&#x27;t it well known that C++ templates are Turing complete? Aren&#x27;t most metaprogramming facilities in other languages as well?
评论 #23014260 未加载
评论 #23012547 未加载
评论 #23014253 未加载
phkahler大约 5 年前
This kind of makes me laugh. Is this due to the relentless changes they&#x27;ve been making to the language the last decade or two? I thought it kinda went off the rails a while ago.
评论 #23015798 未加载
ridiculous_fish大约 5 年前
&gt; typeOrValue is a type int if the solution to the Post Correspondence Problem is “yes”, and a value 0 of type int if the solution is “no”, using SFINAE<p>Isn&#x27;t this where the `typename` keyword comes in? `typename` is required when using a qualified (meaning ::) dependent (it references a template parameter) identifier as a type. My understanding is that uses of types without typename is a common helpful extension but is not strictly conforming.<p>C++ language lawyers, please check my work...
评论 #23015250 未加载
zelly大约 5 年前
By this logic, any language with metaprogramming is undecidable because you can write undecidable programs as a metaprogram. Execution is not the same as parsing.
评论 #23020373 未加载
评论 #23017151 未加载
jonny383大约 5 年前
C++ is legitamately gross to read these days, unless you sit there reading it every day.<p>The syntax has become so ridiciously complicated, I have developed an involuntary gag relfex every time I read any kind of &quot;modern&quot; C++ code (i.e. heavy use of templates &#x2F; generics &#x2F; keywords).
评论 #23015162 未加载
评论 #23014991 未加载
评论 #23014895 未加载
评论 #23015404 未加载
ltbarcly3大约 5 年前
Templates are Turing complete. Once you have that, it&#x27;s pretty simple right?
vkaku大约 5 年前
I can&#x27;t decide if C++ has a grammar at all.<p>Please have a sense of humor.
rowanG077大约 5 年前
This doesn&#x27;t prove C++&#x27;s grammar is undeciable. Template expansion is seperate from parsing. Besides you can do the same thing in any language that can do Turing complete compile time programming.
评论 #23020422 未加载
frank2大约 5 年前
The code in the OP are images, e.g., cannot be cut and pasted.
评论 #23014546 未加载
bialpio大约 5 年前
The fact that the code compiles on godbolt.org makes me doubtful that this particular instance proves that the grammar is undecidable.
评论 #23017166 未加载
winrid大约 5 年前
The color scheme on the syntax highlighting makes me crave cotton candy...
convery大约 5 年前
Can do a &quot;constexpr void inf() { for (int i = 0; i &lt; 2; ++i) i = 0; }&quot; as a shorter proof.
评论 #23014537 未加载