Suppose we have a programming language ℤ which has the following syntax:
ℤ := 0 | 1 | (+ ℤ ℤ) | (* ℤ ℤ) | (- ℤ ℤ) | (max ℤ ℤ)
For convenience, we can define new binding forms in our language as follows:
(not x) = (- 1 x)
(abs x) = (- (max 0 (+ x x)) x)
(min x y) = (- 0 (max (- 0 x) (- 0 y)))
(nil x) = (not (min 1 (abs x)))
This language is powerful enough to express branching and comparison operators:
(if x y z) = (+ (* x y) (* (not x) z))
(eq x y) = (nil (- x y))
(ne x y) = (not (eq x y))
(le x y) = (nil (max 0 (- x y)))
(gt x y) = (not (le x y))
(ge x y) = (le y x)
(lt x y) = (not (ge x y))
Now, the question is whether we can define integer division is this language:
(div x y) = ?
(rem x y) = (- x (* y (div x y)))
I don't think that it's possible to define (div x y)
because ℤ doesn't have loops. However, I don't know how to prove it. Note that if it's possible then the result of (div x 0)
doesn't matter. Hence, either define (div x y)
or prove that it's impossible to do so.
A combination of recursion and branching will give you loops.
In more functional terms, we're doing repeated subtraction. If x >= y, add one to the quotient, subtract y from x, and recur. Otherwise, return 0.
It's impossible.
Call a function
f : Z -> Z
eventually polynomial if there exists a polynomialp
with integer coefficients and a thresholdt
such that, for everyx > t
, we havef(x) = p(x)
. Letd(x) = [x/2]
be floor division by two.d
is not eventually polynomial, because the difference sequence ofd
has infinitely many zeros (f(2y) = y = f(2y+1)
for ally
), whereas the difference sequence of every non constant polynomial has finitely many. It suffices to show that all implementable functions are eventually polynomial.The proof proceeds by structural induction.
0
and1
are polynomial. It's straightforward to show that sums, products, and differences of eventually polynomial functions are eventually polynomial: use the max of the two thresholds and the fact that the set of polynomials is closed under these operations. All that remains is closure undermax
.Let
f
be eventually polynomial via a polynomialp
, andg
be eventually polynomial via a polynomialq
. Ifp = q
, then clearlyx |-> max(f(x), g(x))
is eventually polynomial via the same polynomial. Otherwise, observe thatp - q
has finitely many real roots. Setting the threshold to an upper bound on the roots, we observe that the max function is eventually polynomial viap
orq
since the other case of the max never triggers here.