Guest User

An Impossible Program

a guest
Mar 17th, 2024
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.32 KB | Source Code | 0 0
  1. # Please see here an excellent animation for the halting problem and proof
  2. # https://youtu.be/92WHN-pAFCs?t=240
  3.  
  4. # Short version, assume H, describe H', show the contradictoin arising from executing H'(H').
  5. # Sure, we've all seen that before, but what if we actually execute H'(H')?
  6.  
  7. # Below you will find an H/HALT and an X/OPPOSITE that do both possibilities.
  8. # H/HALT is described as a Turing Machine which can determine if a program and an input will halt or run forever.
  9. # Infinite loops and infinite stacks must therefore be detectable by H.
  10. # Please allow some code outside HALT() to fulfill this detection function since I am not a godlike machine.
  11. # Please allow the function HALT to take the place of H the Turing Machine
  12. # X/OPPOSITE is described as a Turing Machine which will do the opposite of what H returns.
  13. # Please allow the function OPPOSITE to take the place of X the Turing Machine
  14. # Some print statements were added to allow one to track the return values of various invocations of HALT and OPPOSITE/H&X.
  15. # As you will see in its output, HALT is correct in determining OPPOSITE's behavior.  
  16. # OPPOSITE can either enter an infinite loop or return a value.  HALT says it will do both and it does both.
  17. # The proof assumes OPPOSITE(OPPOSITE) is called and thus we call it here.
  18. # OPPOSITE(OPPOSITE) will thus execute HALT(OPPOSITE,OPPOSITE).
  19. # If HALT executes OPPOSITE(OPPOSITE) we enter an infinite loop.  HALT can detect infinite loops.
  20.  
  21. # I hereby show if HALT exists, then HALT exists without contradicting its own existence.
  22. # At least according to the usual proof that's so popular.
  23. # Hopefully this will encourage someone to show a better one.
  24. # I dislike this form of the proof.  H is capable of doing just about anything and I feel at a minimum
  25. # it's been installed with logical paradox crumple zones.
  26.  
  27. # Some more formality, let Opp0 be the first copy of OPPOSITE which is called, Opp1 is the second, etc.
  28. # Let H0 be the first copy of HALT called, let H1 be the second, etc.
  29. # When H0 detects an infinite loop, oh, I never specified what it does!
  30. # Certainly it must, at some point, terminate the loop, otherwise it won't be able to return an answer.
  31. # So if H0 terminates the loop immediately and returns 'NOSTOP', Opp0 will return HALTS and of course, halt.
  32. # H0 would be wrong!  I guess all this is for naught and we should pack up and go home.
  33. # While we're packing up, let's let H0 continue running a bit more.  I'm sure that won't change anything.
  34. # If H0 allows it to keep going, H1 will detect the infinite loop in Opp1.
  35. # Well, H0 can safely terminate the loop at this point, if it didn't, H1 will then wait to terminate the loop, etc.
  36. # How to terminate?  Well, H1 did all that work, might as well let it finish and return a value.
  37. # H1 will see that Opp2 loops forever, which it would if we let it, so well, H1 is correct.
  38. # Opp1 will see that H1 returns 'NOSTOP' and thus Opp1 will return HALTS, and thus Opp1 halts.
  39. # H0 will see that Opp1 halts, and H0 is correct, as we have just seen Opp1 halt.
  40. # So every instance of H that produced an answer has produced a correct answer.
  41.  
  42. import sys
  43. # setting to even and odd will determine outcome of top level OPPOSITE (the one called directly)
  44. sys.setrecursionlimit(6)
  45.  
  46. class HaltDetectedInfinity(Exception):
  47.     def somefn():
  48.         return 'a'
  49.  
  50. # This is for tracking things in the program and implementing HALT's infinity detection
  51. GLOBAL_INFINITIY_COUNTER = int(0)
  52.  
  53. def OPPOSITE(INPUT):
  54.     global GLOBAL_INFINITIY_COUNTER
  55.     current = GLOBAL_INFINITIY_COUNTER
  56.     GLOBAL_INFINITIY_COUNTER += 1
  57.     #print("current",current)
  58.     if HALT(INPUT, INPUT) == 'HALTS':
  59.         localCounter = 0
  60.         while True :
  61.             print("HALT SAID I'D HALT BUT OPPOSITE WILL NEVER HALT HAHAHA")
  62.             localCounter += 1
  63.             # HALT may be able to detect infinite loops, but I have to resort to stopping them
  64.             if localCounter > 5 and current > 0:
  65.                 raise HaltDetectedInfinity('A ' + str(GLOBAL_INFINITIY_COUNTER))
  66.             # top level return because I need my computer back
  67.             # This is not a part of HALT's deliberations
  68.             if localCounter > 5 and current == 0:
  69.                 print("returning top level OPPOSITE, I'd like to run other scripts now");
  70.                 return 0
  71.             # we'll ignore this exception for the top level OPPOSITE because halt might not be running,
  72.             # when halt is running this is part of HALT that detects infinite loops
  73.             if current != 0:
  74.                 raise HaltDetectedInfinity('B')
  75.     else :
  76.         #print("HALT SAID I'D RUN FOREVER BUT OPPOSITE WILL ALWAYS HALT HAHAHA!", current)
  77.         print("HALT SAID I'D RUN FOREVER BUT OPPOSITE WILL ALWAYS HALT HAHAHA!")
  78.         return 'HALTS'
  79.  
  80. # returns 'HALTS' if INPUT1(INPUT2) halts
  81. # returns 'NOSTOP' if INPUT1(INPUT2) doesn't halt
  82. def HALT(INPUT1, INPUT2) :
  83.     global GLOBAL_INFINITIY_COUNTER
  84.     current = GLOBAL_INFINITIY_COUNTER
  85.     if INPUT1 == HALT :
  86.         return 'HALTS' # that answer should be obvious
  87.     if INPUT1 != HALT and INPUT1 != OPPOSITE :
  88.         print("left as exercise for reader")
  89.         exit(0)
  90.     try:
  91.         result = INPUT1(INPUT2)
  92.     except HaltDetectedInfinity as ex:
  93.         msg, = ex.args
  94.         #print("machine doesn't stop", GLOBAL_INFINITIY_COUNTER, msg)
  95.         #print("NOSTOP REACHED", GLOBAL_INFINITIY_COUNTER, msg)
  96.         print("'NOSTOP' reached, HALT detected infinite loop", msg)
  97.         return 'NOSTOP'
  98.     except RecursionError as ex2:
  99.         msg, = ex2.args
  100.         # in case you want to count stack overflow as a machine halting, comment out return 'NOSTOP'
  101.         #print("machine halt detected by HALT", current)
  102.         #print("machine halt detected by HALT")
  103.  
  104.         # If a Turing Machine has access to infinite tape, we're detecting an infinite loop
  105.         print("'NOSTOP' reached, HALT detected infinite loop", msg)
  106.         return 'NOSTOP'
  107.     print("'HALTS' reached, program terminated")
  108.     return 'HALTS'
  109.  
  110. try:
  111.     #print("GLOBAL_INFINITIY_COUNTER",GLOBAL_INFINITIY_COUNTER)
  112.     OPPOSITE(OPPOSITE)
  113. except HaltDetectedInfinity as ex:
  114.     print("we'll ignore this exception since halt isn't running")
  115. GLOBAL_INFINITIY_COUNTER = int(0)
  116. print("---------------------------")
  117. #print("one result of two:",HALT(OPPOSITE,OPPOSITE))
  118.  
  119. # thanks for reading
Advertisement
Add Comment
Please, Sign In to add comment