Question

I have the following vexing problem. I have implemented the following function:

function bool less(nat x, nat y) {
    if (y<>0) then
       if (x<>0) then
           return less(x-1,y-1);
       else
           return true;
       end;
    else
       return false;
    end;
end;

How can I show that for all x,y the following less(x,y) and less(y,x) are not possible at the same time?

Bye

Was it helpful?

Solution

Well, first of all I would ask you to consider the case of less(-1, -2), so we will have to define the function to be on the bounds of n ≥ 0. Also when the first input is equal to the second it will return true for both orderings, so we will have to assume that x ≠ y.

I would use Proof by Contradiction.

Assume that for some x and some y where x and y are both ≥ 0 and x≠y, that less(x,y) and less(y,x) are both true.

This would imply that while x and y are both nonzero, you subtract one from them n times until one of them is zero, checking x first. The function returns true when the first operand reaches zero, false when the second operand reaches zero while the first is nonzero.

There are two cases for this:

  1. x reaches zero first. In this case n = x, because 0 = x - n(1).
  2. y reaches zero first. In this case n = y, because 0 = y - n(1).

By our assumption, less(x,y) returned true, meaning that the function iterated x times, after which x - x(1) = 0 and y - x(1) > 0 (because y ≠ x, and the function didn't return false before hand).

Similarly, less(y,x) returned true, meaning that the function iterated y times, after which y- y(1) = 0 and x - y(1) > 0 (same reasons as before).

This gives us two useful relations: y - x > 0 and x - y > 0. rearranged: y > x and x > y (the semantic meaning of the function, but we have achieved this from the definition of how the function works and we have reduced it to the pure mathematics which we can work with certain axioms for).

From y > x and x > y, you can rearrange as x > x and y > y (If x is greater than y, then it is greater than all things y is greater than. y is greater than x, therefore x is greater than x).

This is a logical contradiction, and therefore our assumption (that they were both true) is incorrect.

Therefore, by Proof by Contradiction, when x ≠ y, and x,y ≥ 0, the function less cannot return true for both less(x,y) and less(y,x).

(it's been a while since I had to do a proof (though I'm going to have to do some coming up so it is good practice) so I might be a bit rusty. If any one sees an error, please point it out and I will try to fix it)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top