A bit too advanced...

Jun 29, 2006 23:14

I figured out the "answer" to my TeX question from yesterday---it's undecidable! There is no way I can get my code to perform correctly and still finish up in a finite amount of time. The way to show this is similar to the proof that the halting problem is undecidable.

Quick sketch of the proof: What I want to do, in effect, is determine if there is a space at the end of the code created by the macro (so that if its expansion ends in a space and the next token after the macro is also a space, I can remove one of the spaces; otherwise I want to do nothing). Assume for the sake of later contradiction that I can create an \ifendsinspace{} function that takes an argument and returns \iftrue if the last token generated by the code in the argument is a space, and \iffalse otherwise (\iftrue and \iffalse are exactly what they sound like). Now, create the following macro:
\newcommand{\trouble}[1]{
\ifendsinspace{#1{#1}}% If the input, when run on itself, ends in a space,
no-space-here% write something that doesn't end in a space.
\else% Otherwise,
space here: % write something that ends in a space
\fi% (this just ends the if statement)
}Now, consider the output of "\trouble{\trouble}":
  • If it ends in a space, then \ifendsinspace is true, and thus it doesn't end in a space.
  • If, however, it doesn't end in a space, then \ifendsinspace is false, and it therefore ends in a space.
Either way, we have a contradiction, so we must reject our assumption and conclude that \ifendsinspace cannot be created. *hip thrust* BAM!

This explains why I was having so much trouble writing such a function! It's pretty neat to find an undecidable problem in the real world; all other undecidable problems I've seen were contrived for classes I took at Mudd.

decidability, tex, latex, undecidability, computer science

Previous post Next post
Up