ChatGPT and Reasoning about Programs

4 points by theGnuMe 10 months ago

I was able to get ChatGPT to reason about a simple program. I find that quite interesting. Has anyone else found anything similar or as interesting?

def main(): i=0 while i>0: i = i + 1

The given code defines a function named "main" that contains a while loop that will never execute. In Python, the "while" statement is used to create a loop that repeats the statements in the loop body as long as the given condition is true. In this case, the condition of the while loop is "i > 0", which means that the loop will only execute if the value of "i" is greater than 0. However, the initial value of "i" is 0, so the condition will never be true, and the loop will never execute. Here is an example of how the function could be modified to make the loop execute:

Copy code def main(): i=1 while i>0: i = i + 1 In this revised version of the function, the initial value of "i" is set to 1, so the condition "i > 0" is satisfied, and the loop will execute. The loop body contains a single statement, "i = i + 1", which increases the value of "i" by 1 on each iteration. This means that the loop will continue to execute indefinitely, increasing the value of "i" by 1 on each iteration. In order to make the loop stop executing at some point, you would need to add additional code to the loop body to check for a certain condition and break out of the loop when that condition is met. I hope this helps. Let me know if you have any other questions.

kurtreed 10 months ago

Sometimes it's helpful, sometimes it's not. Unfortunately it always sounds fully confident.

  • PaulHoule 10 months ago

    I think of the dialogues between Achilles and the Tortoise in the Godel, Escher, Bach book. They would have tried adding a classifier that can tell when it is wrong and when it is right, but that is of course undecidable.

PaulHoule 10 months ago

You gave it just the program as a prompt?

  • theGnuMe 10 months ago

    Yes. I did ask it prior to that if it could solve the halting problem which it said it could not because it is a large language model etc... So then I just gave it the code and it came up with what I posted.

    • PaulHoule 10 months ago

      Nobody can solve the halting program in general but it is certainly possible to write a program that could prove that the first program terminates, that the loop never gets entered, the same way a person could make the same logical argument that ChatGPT makes.

      • theGnuMe 10 months ago

        I mean Turing proved it was not decidable on his machine.