Reversible numerical calculations in Prolog
Posted
by user8472
on Stack Overflow
See other posts from Stack Overflow
or by user8472
Published on 2010-05-24T11:46:00Z
Indexed on
2010/05/26
1:01 UTC
Read the original article
Hit count: 262
prolog
|reversible-functions
While reading SICP I came across logic programming chapter 4.4. Then I started looking into the Prolog programming language and tried to understand some simple assignments in Prolog. I found that Prolog seems to have troubles with numerical calculations.
Here is the computation of a factorial in standard Prolog:
f(0, 1).
f(A, B) :- A > 0, C is A-1, f(C, D), B is A*D.
The issues I find is that I need to introduce two auxiliary variables (C
and D
), a new syntax (is
) and that the problem is non-reversible (i.e., f(5,X)
works as expected, but f(X,120)
does not).
Naively, I expect that at the very least C is A-1, f(C, D)
above may be replaced by f(A-1,D)
, but even that does not work.
My question is: Why do I need to do this extra "stuff" in numerical calculations but not in other queries?
I do understand (and SICP is quite clear about it) that in general information on "what to do" is insufficient to answer the question of "how to do it". So the declarative knowledge in (at least some) math problems is insufficient to actually solve these problems. But that begs the next question: How does this extra "stuff" in Prolog help me to restrict the formulation to just those problems where "what to do" is sufficient to answer "how to do it"?
© Stack Overflow or respective owner