Skip to content

Instantly share code, notes, and snippets.

@billmei
Created March 18, 2018 18:55
Show Gist options
  • Save billmei/c71f05b036119411861283a897af8f09 to your computer and use it in GitHub Desktop.
Save billmei/c71f05b036119411861283a897af8f09 to your computer and use it in GitHub Desktop.
Halting problem simple proof in Ruby
# Assume we are able to write a function, magical_solver, which can magically
# determine whether or not a function halts
def magical_solver(source_code)
# outputs 'true' if fn.source_code halts
# outputs 'false' if fn.source_code doesn't halt
end
def always_halt
# Always halts
end
def never_halt
# Never halts (goes into infinite loop)
end
def flipper(source_code)
# If fn halts, output a program that doesn't halt
# If fn doesn't halt, output a program that does halt
if magical_solver(source_code)
never_halt.source_code
else
always_halt.source_code
end
end
magical_solver(flipper(always_halt.source_code)) # false
magical_solver(flipper(never_halt.source_code)) # true
# What if we pass flipper into itself?
magical_solver(flipper(flipper.source_code))
# Possibility A: flipper is a good program
# magical_solver will tell us that flipper is a bad program
# Possibility B: flipper is a bad program
# magical_solver will tell us that flipper is a good program
# Both possibility A and possibility B are contradictions, therefore magical_solver cannot exist,
# and we can never determine whether or not a program halts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment