Skip to content

Instantly share code, notes, and snippets.

@niloc132
Created September 18, 2011 03:57
Show Gist options
  • Save niloc132/1224711 to your computer and use it in GitHub Desktop.
Save niloc132/1224711 to your computer and use it in GitHub Desktop.
Quick impl of halting problem contradiction
/*
Starting from http://my.opera.com/Vorlath/blog/2009/06/18/halting-problem-composability-and-compositionality
First, my apologies on reopening what is apparently not a recent blog post - the author might
long since have decided to no longer pursue this, but this was pointed out to me today, and
it looked like an interesting discussion to take part on. Whoops.
Consider any language able to turn a string into executable code: Javascript has an eval
function able to take a string, and execute it. This can be used to turn a block of code into
something which can be executed by simply calling a function.
*/
var haltCheckFunc = function(pgrm) {/*...*/};
// where ... in the implementation of a hypothetical check of another string of javascript,
//here noted as pgrm. This is the input to which you refer - it can be asserted that this must
//be valid javascript, and could be checked by wrapping it in function() { }, and evaling that
//result. Note that this wrapping would mean that the only input is whatever is currently
//available in the running js memory.
var contradiction = "if (haltCheckFunc(contradiction)) {while(true);} return;"
// Declare a simple js program which will run itself through the halt check. No input is
//directly required, except for other locally scoped variables
eval(contradiction);// If haltCheckFunc would return true on the string, then this eval will
//not end, thus haltCheckFunc has lied to us
/*
This is trivial to do in JS, but just as doable in any other language where you can implement
an interpreter. If I were to implement (for example) a JS interpreter in C, or Java, or any
other Turing-complete language, I could build this same program, and though the haltCheckFunc
could then be implemented in the underlying language, it would still be unable to predict how
the function would complete.
Input matters, but allowing for a machine powerful enough to implement itself within that
machine means you are too powerful to predict important things, as steps can be taken to
demonstrate that input is a valid program, and yet you cannot specify certain properties about
it. The input only matters in this example such that the haltCheck can parse the input, and
the input can itself somehow be executed.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment