11

Salutations. Dijkstra wrote that even a few lines of seemingly simple code could be hopelessly ambiguous. In at least one work, which I can't find now to save my life, he gave a little example program to demonstrate this ambiguity. Can anybody point me to a paper of his where he includes one of these examples?

2 Answers2

11

Read this:

http://en.wikipedia.org/wiki/Halting_problem#Recognizing_partial_solutions

http://www.cs.ucsb.edu/~pconrad/cs40/08S/notes/ has nice coverage; search for "halting problem"

There are several forms of the essential halting contradiction.

def halts( code_block ):
    # Some magical code

def whistler():
    while halts(whistler): 
        sys.whistle( 1 )

Does the "whistler" whistle zero, once or an infinite number of times?

If the halts() function determines that the function whistler appears to halt, the function whistler can't halt.

If the halts() function determines that the function whistler does not appear halt, the function whistler halts.

Therefore, the halts() function can't exist.

S.Lott
  • 45,522
  • 6
  • 93
  • 155
1

Are you sure that the paper was by Dijkstra? Reflections on Trusting Trust by Ken Thompson sounds like it could be what you were thinking of. It demonstrates how absolutely simple, straightforward, and correct programs could wind up doing something absolutely unexpected that isn't visible at all in the source. Even if it isn't what you were thinking of, it is a worthwhile paper to read.

Going a different direction, if you want excellent examples of short programs with surprising behavior, the underhanded C contest is great. For example look at the 2008 winner. The challenge was to write a command line program to blank out part of a picture, in such a way that the picture visually was blanked out perfectly, but the file retains some information about the redacted part of the image. AND in such a way that your code could pass code review. (You could choose the format that the picture is stored in.)

btilly
  • 18,340