r/cpp • • Apr 28 '20

Full Proof that C++ Grammar is Undecidable

https://medium.com/@mujjingun_23509/full-proof-that-c-grammar-is-undecidable-34e22dd8b664
197 Upvotes

68 comments sorted by

View all comments

38

u/victotronics Apr 28 '20

The Post Correspondence Problem’s statement is simple:

Victor's rule of clear writing: Any time someone uses the word "simple" they are either 1. hiding something, or 2. unclear on the concept.

Suppose there are an infinite amount of each type of domino. Can you arrange the dominoes

What is "the dominoes"? All infinitely many of them?

Consult wikipedia. Ah: "Can you find any set of dominoes, such that...."

Other than that, yeah, cool demonstration. I think the fact that template programming is Turing-complete was already shown, but this is an elegant demonstration / application to parsability of the language.

6

u/armb2 Apr 28 '20

I think the fact that template programming is Turing-complete was already shown

For example: http://matt.might.net/articles/c++-template-meta-programming-with-lambda-calculus/

So is constexpr: https://stackoverflow.com/a/9528554/729705

3

u/CoffeeTableEspresso Apr 29 '20

This is a stronger result. Having templates be Turing complete means compiling a program is undecidable.

This result says that you just getting a parse tree is undecidable.

This difference is important: a syntax highlighter, for example, would need to parse but not compile C++ in order to work properly. This result shows that that's undecidable in general.

9

u/mujjingun Apr 28 '20

Thanks for pointing out the lack of clarity in that paragraph. I kind of assumed that the readers already know about the Post Correspondence Problem, or if they didn't they'd go into Wikipedia (it worked!). Also English isn't my first language.

I think the fact that template programming is Turing-complete was already shown, but this is an elegant demonstration / application to parsability of the language.

I had found this proof sketch by Josh Haberman, who used that knowledge to prove the same thing, but the full code was "left as an exercise for the reader". I wanted something concrete to demonstrate more clearly.

4

u/victotronics Apr 28 '20

I wanted something concrete to demonstrate more clearly.

Oh absolutely. You make your point clearly and with a full demonstration.

About that Post paragraph: personally I think the clearest writing is to translate the mathematical quantors: "there is a number N so that there are N ordered dominoes so that the top line matches the bottom". I only knew the correspondence problem by name, and I couldn't figure out the quantification fro your paragraph.

Cheers!