Reversible numerical calculations in Prolog
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"?
is/2
is very low-level and limited. As you correctly observe, it cannot be used in all directions and is therefore not a true relation.
For reversible arithmetic, use your Prolog system's constraint solvers.
For example, SWI-Prolog's CLP(FD) manual contains the following definition of n_factorial/2
:
:- use_module(library(clpfd)).
n_factorial(0, 1).
n_factorial(N, F) :- N #> 0, N1 #= N - 1, F #= N * F1, n_factorial(N1, F1).
The following example queries show that it can be used in all directions:
?- n_factorial(47, F).
F = 258623241511168180642964355153611979969197632389120000000000 ;
false.
?- n_factorial(N, 1).
N = 0 ;
N = 1 ;
false.
?- n_factorial(N, 3).
false.
Of course, this definition still relies on unification, and you can therefore not plug in arbitrary integer expressions. A term like 2-2
(which is -(2,2)
in prefix notation) does not unfiy with 0
. But you can easily allow this if you rewrite this to:
:- use_module(library(clpfd)).
n_factorial(N, F) :- N #= 0, F #= 1.
n_factorial(N, F) :- N #> 0, N1 #= N - 1, F #= N * F1, n_factorial(N1, F1).
Example query and its result:
?- n_factorial(2-2, -4+5).
true .
Forget about variables and think that A
and B
- is just a name for value which can be placed into that clause (X :- Y).
to make it reachable. Think about X = (2 + (3 * 4))
in the way of data structures which represent mathematical expression. If you will ask prolog to reach goal f(A-1, B)
it will try to find such atom f(A-1,B).
or a rule (f(A-1,B) :- Z), Z.
which will be unified to "success".
is/2
tries to unify first argument with result of interpreting second argument as an expression. Consider eval/2
as variant of is/2
:
eval(0, 1-1). eval(0, 2-2). eval(1,2-1).
eval(Y, X-0):- eval(Y, X).
eval(Y, A+B):- eval(ValA, A), eval(ValB, B), eval(Y, ValA + ValB).
eval(4, 2*2).
eval(0, 0*_). eval(0, _*0).
eval(Y, X*1):- eval(Y, X).
eval(Y, 1*X):- eval(Y, X).
eval(Y, A*B):- eval(ValA, A), eval(ValB, B), eval(Y, ValA * ValB).
The reason why f(X,120)
doesn't work is simple >/2
works only when its arguments is bound (i.e. you can't compare something not yet defined like X
with anything else). To fix that you have to split that rule into:
f(A,B) :- nonvar(A), A > 0, C is A-1, f(C, D), B is A*D.
f(A,B) :- nonvar(B), f_rev(A, B, 1, 1).
% f_rev/4 - only first argument is unbound.
f_rev(A, B, A, B). % solution
f_rev(A, B, N, C):- C < B, NextN is (N+1), NextC is (C*NextN), f_rev(A, B, NextN, NextC).
Update: (fixed f_rev/4
)
You may be interested in finite-domain solver. There was a question about using such things. By using #>/2
and #=/2
you can describe some formula and restrictions and then resolve them. But these predicates uses special abilities of some prolog systems which allows to associate name with some attributes which may help to narrow set of possible values by intersection of restriction. Some other systems (usually the same) allows you to reorder sequence of processing goals ("suspend").
Also member(X,[1,2,3,4,5,6,7]), f(X, 120)
is probably doing the same thing what your "other queries" do.
If you are interested in logical languages in general you may also look at Curry language (there all non-pure functions is "suspended" until not-yed-defined value is unified).
In this answer we use clpfd, just like this previous answer did.
:- use_module(library(clpfd)).
For easy head-to-head comparison (later on), we call the predicate presented here n_fac/2
:
n_fac(N_expr,F_expr) :-
N #= N_expr, % eval arith expr
F #= F_expr, % eval arith expr
n_facAux(N,F).
Like in this previous answer, n_fac/2
admits the use of arithmetic expressions.
n_facAux(0,1). % 0! = 1
n_facAux(1,1). % 1! = 1
n_facAux(2,2). % 2! = 2
n_facAux(N,F) :-
N #> 2,
F #> N, % redundant constraint
% to help `n_fac(N,N)` terminate
n0_n_fac0_fac(3,N,6,F). % general case starts with "3! = 6"
The helper predicate n_facAux/2
delegates any "real" work to n0_n_fac0_fac/4
:
n0_n_fac0_fac(N ,N,F ,F).
n0_n_fac0_fac(N0,N,F0,F) :-
N0 #< N,
N1 #= N0+1, % count "up", not "down"
F1 #= F0*N1, % calc `1*2*...*N`, not `N*(N-1)*...*2*1`
F1 #=< F, % enforce redundant constraint
n0_n_fac0_fac(N1,N,F1,F).
Let's compare n_fac/2
and n_factorial/2
!
?- n_factorial(47,F).
F = 258623241511168180642964355153611979969197632389120000000000
; false.
?- n_fac(47,F).
F = 258623241511168180642964355153611979969197632389120000000000
; false.
?- n_factorial(N,1).
N = 0
; N = 1
; false.
?- n_fac(N,1).
N = 0
; N = 1
; false.
?- member(F,[3,1_000_000]), ( n_factorial(N,F) ; n_fac(N,F) ).
false. % both predicates agree
OK! Identical, so far... Why not do a little brute-force testing?
?- time((F1 #\= F2,n_factorial(N,F1),n_fac(N,F2))). % 57,739,784 inferences, 6.415 CPU in 7.112 seconds (90% CPU, 9001245 Lips) % Execution Aborted ?- time((F1 #\= F2,n_fac(N,F2),n_factorial(N,F1))). % 52,815,182 inferences, 5.942 CPU in 6.631 seconds (90% CPU, 8888423 Lips) % Execution Aborted ?- time((N1 #> 1,N2 #> 1,N1 #\= N2,n_fac(N1,F),n_factorial(N2,F))). % 99,463,654 inferences, 15.767 CPU in 16.575 seconds (95% CPU, 6308401 Lips) % Execution Aborted ?- time((N1 #> 1,N2 #> 1,N1 #\= N2,n_factorial(N2,F),n_fac(N1,F))). % 187,621,733 inferences, 17.192 CPU in 18.232 seconds (94% CPU, 10913552 Lips) % Execution Aborted
No differences for the first few hundred values of N in 2..sup
... Good!
Moving on: How about the following (suggested in a comment to this answer)?
?- n_factorial(N,N), false.
false.
?- n_fac(N,N), false.
false.
Doing fine! Identical termination behaviour... More?
?- N #< 5, n_factorial(N,_), false.
false.
?- N #< 5, n_fac(N,_), false.
false.
?- F in 10..100, n_factorial(_,F), false.
false.
?- F in 10..100, n_fac(_,F), false.
false.
Alright! Still identical termination properties! Let's dig a little deeper! How about the following?
?- F in inf..10, n_factorial(_,F), false. ... % Execution Aborted % does not terminate universally ?- F in inf..10, n_fac(_,F), false. false. % terminates universally
D'oh! The first query does not terminate, the second does. What a speedup! :)
Let's do some empirical runtime measurements!
?- member(Exp,[6,7,8,9]), F #= 10^Exp, time(n_factorial(N,F)) ; true. % 328,700 inferences, 0.043 CPU in 0.043 seconds (100% CPU, 7660054 Lips) % 1,027,296 inferences, 0.153 CPU in 0.153 seconds (100% CPU, 6735634 Lips) % 5,759,864 inferences, 1.967 CPU in 1.967 seconds (100% CPU, 2927658 Lips) % 22,795,694 inferences, 23.911 CPU in 23.908 seconds (100% CPU, 953351 Lips) true. ?- member(Exp,[6,7,8,9]), F #= 10^Exp, time(n_fac(N,F)) ; true. % 1,340 inferences, 0.000 CPU in 0.000 seconds ( 99% CPU, 3793262 Lips) % 1,479 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 6253673 Lips) % 1,618 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 5129994 Lips) % 1,757 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 5044792 Lips) true.
Wow! Some more?
?- member(U,[10,100,1000]), time((N in 1..U,n_factorial(N,_),false)) ; true. % 34,511 inferences, 0.004 CPU in 0.004 seconds (100% CPU, 9591041 Lips) % 3,091,271 inferences, 0.322 CPU in 0.322 seconds (100% CPU, 9589264 Lips) % 305,413,871 inferences, 90.732 CPU in 90.721 seconds (100% CPU, 3366116 Lips) true. ?- member(U,[10,100,1000]), time((N in 1..U,n_fac(N,_),false)) ; true. % 3,729 inferences, 0.001 CPU in 0.001 seconds (100% CPU, 2973653 Lips) % 36,369 inferences, 0.004 CPU in 0.004 seconds (100% CPU, 10309784 Lips) % 362,471 inferences, 0.036 CPU in 0.036 seconds (100% CPU, 9979610 Lips) true.
The bottom line?
- The code presented in this answer is as low-level as you should go: Forget
is/2
! - Redundant constraints can and do pay off.
- The order of arithmetic operations (counting "up" vs "down") can make quite a difference, too.
- If you want to calculate the factorial of some "large"
N
, consider using a different approach. - Use clpfd!
There are some things which you must remember when looking at Prolog:
There is no implicit return value when you call a predicate. If you want to get a value out of a call you need to add extra arguments which can be used to "return" values, the second argument in your
f/2
predicate. While being more verbose it does have the benefit of being easy to return many values.This means that automatically "evaluating" arguments in a call is really quite meaningless as there is no value to return and it is not done. So there are no nested calls, in this respect Prolog is flat. So when you call
f(A-1, D)
the first argument tof/2
is the structureA-1
, or really-(A, 1)
as-
is an infix operator. So if you want to get the value from a call tofoo
into a call tobar
you have to explicitly use a variable to do it like:foo(..., X), bar(X, ...),
So you need a special predicate which forces arithmetic evaluation,
is/2
. It's second argument is a structure representing an arithmetic expression which it interprets, evaluates and unifies the result with its first argument, which can be either a variable or numerical value.While in principle you can run things backwards with most things you can't. Usually it is only simple predicates working on structures for which it is possible, though there are some very useful cases where it is possible.
is/2
doesn't work backwards, it would be exceptional if it did.
This is why you need the extra variables C
and D
and can't replace C is A-1, f(C, D)
by f(A-1,D)
.
(Yes I know you don't make calls in Prolog, but evaluate goals, but we were starting from a functional viewpoint here)
精彩评论