Search code examples
computer-sciencetheoryproof

Minimum number of statements: P or NP?


It is a common programmer hobby to write programs which accomplish a task in 1 line of source code. But that is a bit trivial: I can take 1 000 000 lines of code, delete all the line breaks, and voila! 1 line!

So to make things interesting, we can count statements. For a C-style language, a simple of way of counting statements is counting semicolons: So nesting a million if-elses is fine.

Say you've got a program P with n statements. It goes through a sequence of states (variable values) s (where s is a vector) and produces output x. We can pose two questions:

  1. Can a program with fewer than n statements produce output x?
  2. Can a program with fewer than n statements go through some subset of s?

Immediately, some things become obvious. Take the following program:

int sum = a+b;
float mean = sum/2.0;
return mean;

We can refactor (or should I say "defactor") this into a one liner:

return (a+b)/2.0;

Can every program be defactored to one line? Take this program:

string x = "";
for (int i = 0; i<a; i++)   // Should these semicolons count?
{
    x = x + ".";
}
return x;

It's a bit more challenging.

The question can be answered exhaustively by trying every possible program with less than n statements, a number which is finite (theoretically you could have constants with infinitely many possible values, but no real language has infinite memory to store them or infinite disk space to accommodate the source code).

However:

A. Is it possible to prove for a program P which produces x (perhaps through s) with n statements that no Q which can do it in m statements can be found (in an efficient manner)?

B. Can the minimum n be found (in an efficient manner)?

C. Is minimum n guaranteed to be 1?

You can assume any language you want, although real languages would be more interesting. Please provide a definition of a "statement" in your language if your language is unusual.

I have assumed imperative languages, but adaptations of the problem to functional languages are welcome.

There is a trivial solution: For any P, run P and record x. Now write a program Q which simply prints x. For programs with input, make a very long if-else with each input mapped to a correct output.

This solution is unsatisfactory, although I'm not entirely sure why. Firstly, for infinite possible inputs it's impossible (but I already covered my ass by saying real programs are finite, so we can say real input is finite). Second, it only technically passes through a subset of s: Namely the empty set. Third, it's really anticlimactic.

Any help with defining away this little clever trick is also appreciated.

PS: For whatever my word is worth, this isn't homework. I simply got curious about the problem and tried to phrase it as clearly as I could. Of course, I would still say that if it was homework, so...


Solution

  • Since the concept of a statement is language specific, there are languages where every program can be written as a single statement or expression. Hell, there are even languages where every program must be written as a single expression.

    That said, assuming a language where this is not the case (and there definitely are such languages), the problem of finding the minimum of statements to solve a given problem will be neither in P nor NP - it's undecidable.

    The question can be answered exhaustively by trying every possible program with less than n statements, a number which is finite

    Since some of those programs won't terminate and it's impossible to know which ones will (halting problem), that doesn't work.

    Also the number of programs with less than n statements is not finite in most languages. For example there are an infinite number of statements of the form return foo + bar + ... + baz; in most languages.

    A. Is it possible to prove for a program P which produces x (perhaps through s) with n statements that no Q which can do it in m statements can be found (in an efficient manner)?

    (I'm assuming you forgot a m < n here, otherwise the question doesn't make sense.)

    No, it can't be proven at all.

    B. Can the minimum n be found (in an efficient manner)?

    No, it can't be found at all.

    C. Is minimum n guaranteed to be 1?

    As I said at the beginning, it depends on the language and for the purpose of the above questions I assumed a language where the answer to this is no.