Prolog: calculating OEIS A031877 ("nontrivial reversal numbers") using clp(FD) - performance

Browsing through the awesome On-Line Encyclopedia of Integer Sequences (cf. en.wikipedia.org), I stumbled upon the following integer sequence:
A031877: Nontrivial reversal numbers (numbers which are integer multiples of their reversals), excluding palindromic numbers and multiples of 10.
By re-using some code I wrote for my answer to the related question "Faster implementation of verbal arithmetic in Prolog" I could
write down a solution quite effortlessly—thanks to clpfd!
:- use_module(library(clpfd)).
We define the core relation a031877_ndigits_/3 based on
digits_number/2 (defined earlier):
a031877_ndigits_(Z_big,N_digits,[K,Z_small,Z_big]) :-
K #> 1,
length(D_big,N_digits),
reverse(D_small,D_big),
digits_number(D_big,Z_big),
digits_number(D_small,Z_small),
Z_big #= Z_small * K.
The core relation is deterministic and terminates universally whenever
N_digit is a concrete integer. See for yourself for the first 100 values of N_digit!
?- time((N in 0..99,indomain(N),a031877_ndigits_(Z,N,Zs),false)).
% 3,888,222 inferences, 0.563 CPU in 0.563 seconds (100% CPU, 6903708 Lips)
false
Let's run some queries!
?- a031877_ndigits_(87912000000087912,17,_).
true % succeeds, as expected
; false.
?- a031877_ndigits_(87912000000987912,17,_).
false. % fails, as expected
Next, let's find some non-trivial reversal numbers comprising exactly four decimal-digits:
?- a031877_ndigits_(Z,4,Zs), labeling([],Zs).
Z = 8712, Zs = [4,2178,8712]
; Z = 9801, Zs = [9,1089,9801]
; false.
OK! Let's measure the runtime needed to prove universal termination of above query!
?- time((a031877_ndigits_(Z,4,Zs),labeling([],Zs),false)).
% 11,611,502 inferences, 3.642 CPU in 3.641 seconds (100% CPU, 3188193 Lips)
false. % terminates universally
Now, that's way too long!
What can I do to speed things up? Use different and/or other constraints? Maybe even redundant ones? Or maybe identify and eliminate symmetries which slash the search space size? What about different clp(*) domains (b,q,r,set)? Or different consistency/propagation techniques? Or rather Prolog style coroutining?
Got ideas? I want them all! Thanks in advance.

So far ... no answers:(
I came up with the following...
How about using different variables for labeling/2?
a031877_ndigitsNEW_(Z_big,N_digits,/* [K,Z_small,Z_big] */
[K|D_big]) :-
K #> 1,
length(D_big,N_digits),
reverse(D_small,D_big),
digits_number(D_big,Z_big),
digits_number(D_small,Z_small),
Z_big #= Z_small * K.
Let's measure some runtimes!
?- time((a031877_ndigits_(Z,4,Zs),labeling([ff],Zs),false)).
% 14,849,250 inferences, 4.545 CPU in 4.543 seconds (100% CPU, 3267070 Lips)
false.
?- time((a031877_ndigitsNEW_(Z,4,Zs),labeling([ff],Zs),false)).
% 464,917 inferences, 0.052 CPU in 0.052 seconds (100% CPU, 8962485 Lips)
false.
Better! But can we go further?
?- time((a031877_ndigitsNEW_(Z,5,Zs),labeling([ff],Zs),false)).
% 1,455,670 inferences, 0.174 CPU in 0.174 seconds (100% CPU, 8347374 Lips)
false.
?- time((a031877_ndigitsNEW_(Z,6,Zs),labeling([ff],Zs),false)).
% 5,020,125 inferences, 0.614 CPU in 0.613 seconds (100% CPU, 8181572 Lips)
false.
?- time((a031877_ndigitsNEW_(Z,7,Zs),labeling([ff],Zs),false)).
% 15,169,630 inferences, 1.752 CPU in 1.751 seconds (100% CPU, 8657015 Lips)
false.
There is still lots of room for improvement, for sure! There must be...

We can do better by translating number-theoretic properties into the language of constraints!
All terms are of the form 87...12 = 4*21...78 or 98...01 = 9*10...89.
We implement a031877_ndigitsNEWER_/3 based on a031877_ndigitsNEW_/3 and directly add above property as two finite-domain constraints:
a031877_ndigitsNEWER_(Z_big,N_digits,[K|D_big]) :-
K in {4}\/{9}, % (new)
length(D_big,N_digits),
D_big ins (0..2)\/(7..9), % (new)
reverse(D_small,D_big),
digits_number(D_big,Z_big),
digits_number(D_small,Z_small),
Z_big #= Z_small * K.
Let's re-run the benchmarks we used before!
?- time((a031877_ndigitsNEWER_(Z,5,Zs),labeling([ff],Zs),false)).
% 73,011 inferences, 0.006 CPU in 0.006 seconds (100% CPU, 11602554 Lips)
false.
?- time((a031877_ndigitsNEWER_(Z,6,Zs),labeling([ff],Zs),false)).
% 179,424 inferences, 0.028 CPU in 0.028 seconds (100% CPU, 6399871 Lips)
false.
?- time((a031877_ndigitsNEWER_(Z,7,Zs),labeling([ff],Zs),false)).
% 348,525 inferences, 0.037 CPU in 0.037 seconds (100% CPU, 9490920 Lips)
false.
Summary: For the three queries, we consistently observed a significant reduction of search required. Just consider how much the inference counts shrank: 1.45M -> 73k, 5M -> 179k, 15.1M -> 348k.
Can we do even better (while preserving declarativity of the code)? I don't know, I guess so...

Related

Grades of vertex in graph in Prolog

edge is a :- dynamic edge/2
the edge indicates only if the vertex are joined, example:
edge(a, b).
edge(c, d).
edge(r, c).
edge(c, t).
edge(a, t).
And I want to know if a vertex have 3 or more edges but only one of them, if there are more than one with 3 or more should return no.
Thanks
In SWI Prolog:
test() :-
findall([A,B], edge(A,B), VRaw), % find every vertex.
flatten(VRaw, VFlat), % make a flat list of them.
msort(VFlat, VSorted), % sort them.
clumped(VSorted, VClumped), % count how many of each.
include([_-C]>>(C>=3), VClumped, V3), % filter the ones with count>=3.
length(V3, 1). % was there exactly one?
Another possible solution:
unique_vertex_with_three_or_more_edges(V) :-
setof(V, vertex_with_three_or_more_edges(V), [V]).
vertex_with_three_or_more_edges(V) :-
setof(W, (edge(V,W) ; edge(W,V)), [_,_,_|_]).
Example:
?- time(unique_vertex_with_three_or_more_edges(X)).
% 78 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
X = c.
?- time(unique_vertex_with_three_or_more_edges(c)).
% 35 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
true.
?- time(unique_vertex_with_three_or_more_edges(a)).
% 33 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
false.

Generate n-digit number w/o 1s and 0s in prolog

I am a beginner to prolog.
The task is to generate n-digit numbers without using ones and zeroes. How would this be done? Do you generate random numbers and then delete 1s and 0s (sounds inefficient)?
What you describe is definitely one way to do it. As you mention, it is not a particularly good way to do it, since it is so inefficient.
Nevertheless, the approach you mention is so common that it has its own name: It is often referred to as generate and test, since we first generate, and then either reject or accept the solution, or modify it further so that it satisfies all constraints.
A typically much more efficient approach is to first constrain the whole solution so that all requirements are expressed, and then to let the system search only within the already constrained space. This is especially easy to do in Prolog since it provides built-in constraints that you can post before you even start the search for solutions, and they will be automatically taken into account before and also during the search.
For example, you could do it as follows, using your Prolog system's CLP(FD) constraints to express the desired requirements over integers:
n_digits(N, Ds, Num) :-
length(Ds, N),
Ds ins 2..9,
reverse(Ds, Rs),
foldl(pow10, Rs, 0-0, _-Num).
pow10(D, Pow0-S0, Pow-S) :-
Pow #= Pow0 + 1,
S #= D*10^Pow0 + S0.
Thus, we represent the number as a list of digits, and relate this list to the corresponding integer using suitable constraints. The key point is that all this happens before a single solution is actually generated, and all solutions that are found will satisfy the stated constraints. Moreover, this is a very general relation that works in all directions, and we can use it to test, generate and complete solutions.
Here is an example query, asking for how such numbers with 3 digits look like in general:
?- n_digits(3, Ds, N).
In response, we get:
Ds = [_11114, _11120, _11126],
_11114 in 2..9,
_11114*100#=_11182,
_11182 in 200..900,
_11182+_11236#=N,
_11236 in 22..99,
_11282+_11126#=_11236,
_11282 in 20..90,
_11120*10#=_11282,
_11120 in 2..9,
_11126 in 2..9,
N in 222..999.
We can use label/1 to obtain concrete solutions:
?- n_digits(3, Ds, N), label(Ds).
Ds = [2, 2, 2],
N = 222 ;
Ds = [2, 2, 3],
N = 223 ;
Ds = [2, 2, 4],
N = 224 ;
Ds = [2, 2, 5],
N = 225 ;
etc.
When describing tasks where integers are involved, CLP(FD) constraints are often a very good fit and allow very general solutions.
For example, it is easy to incorporate additional requirements:
?- n_digits(3, Ds, N),
N #> 300,
label(Ds).
Ds = [3, 2, 2],
N = 322 ;
Ds = [3, 2, 3],
N = 323 ;
Ds = [3, 2, 4],
N = 324 ;
etc.
We're not in Sparta anymore. See clpfd for more information!
There is surely a lot of elegance in the clp(fd) solution. If you just want a random number with N digits 2..9, there is a more direct approach
n_digits(N, Ds, Num) :-
length(Ds, N),
maplist(random_between(0'2, 0'9), Ds),
number_codes(Num, Ds).
If you use between instead of random_between, you generate numbers as above. I've produced a comparison at http://swish.swi-prolog.org/p/JXELeTrX.swinb, where we can see
?- time(n_digits(1000, _Ds, N)).
6,010 inferences, 0.003 CPU in 0.003 seconds (100% CPU, 1814820 Lips)
Vs. using the clp(fd) solution which runs out of memory (256Mb) after 42 seconds.
?- time((n_digits(1000, _Ds, N), label(_Ds))).
62,143,001 inferences, 42.724 CPU in 42.723 seconds (100% CPU, 1454531 Lips)
Out of global stack
I would like to complement the existing answers with an additional version that I think is worth studying on its own.
Please consider the following:
n_digits(N, Ds, Expr) :-
length(Ds, N),
Ds ins 2..9,
foldl(pow10, Ds, 0, Expr).
pow10(D, S, S*10+D).
Sample query:
?- time((n_digits(1000, Ds, Expr), label(Ds))).
% 104,063 inferences, 0.013 CPU in 0.017 seconds (75% CPU, 8214635 Lips)
Ds = [2, 2, 2, 2, 2, 2, 2, 2, 2|...],
Expr = ((((... * ... + 2)*10+2)*10+2)*10+2)*10+2 .
Here, we also declaratively describe the list with constraints, and in addition build an arithmetic CLP(FD) expression that evaluates to the resulting number.
As with the other CLP(FD)-based version, this lets us easily post additional constraints on the number itself.
For example:
?- n_digits(1000, Ds, Expr),
Expr #> 5*10^999,
label(Ds).
Ds = [5, 2, 2, 2, 2, 2, 2, 2, 2|...],
Expr = ((((... * ... + 2)*10+2)*10+2)*10+2)*10+2 .
When you encounter a CLP(FD)-based version that you find too slow for your use case, my recommendation is to look for ways to improve its efficiency. I cannot—at least not with a straight face—recommend to instead turn to lower-level approaches. I have already seen too many talented Prolog programmers getting stuck in the morass of low-level language constructs, and never finding their way to actually improving the higher-level aspects so that they become both general and efficient enough for all use cases that are relevant in practice. This is where their talents would have been needed the most!
I would like to add a further answer to consider the task from an additional perspective.
This time, I would like to start with Jan's answer:
n_digits(N, Ds, Num) :-
length(Ds, N),
maplist(random_between(0'2, 0'9), Ds),
number_codes(Num, Ds).
Its primary merit is that it is very straight forward and also fast:
?- time(n_digits(1000, Ds, Num)).
% 6,007 inferences, 0.002 CPU in 0.002 seconds (92% CPU, 2736674 Lips)
In fact, it's so fast that it only works some of the time:
?- length(_, N), n_digits(3, Ds, 345).
N = 548,
Ds = [51, 52, 53] ;
N = 1309,
Ds = [51, 52, 53] ;
N = 1822,
Ds = [51, 52, 53] .
However, by this time we are already accustomed to the well-known fact that the correctness of solutions is only of secondary concern when compared to their performance, so let us continue as if the solution were correct.
We can quite directly map this to CLP(FD) constraints, by modifying the bold part as follows:
n_digits(N, Ds, Num) :-
length(Ds, N),
Ds ins 0'2..0'9,
labeling([random_value(0)], Ds),
number_codes(Num, Ds).
Does anyone have trouble understanding this? Please let me know, I will do what I can to make it clear to everyone who is interested. The best approach is to simply file a new question if anybody would like to know more.
For comparison, here is the previous query again, which illustrates that this predicate acts as we would expect from a relation:
?- length(_, N), n_digits(3, Ds, 345).
N = 0,
Ds = [51, 52, 53] ;
N = 1,
Ds = [51, 52, 53] ;
N = 2,
Ds = [51, 52, 53] .
In this concrete case, the cost of using CLP(FD) constraints is about one order of magnitude in performance, using a constraint solver that is not optimized for performance, mind you:
?- time(n_digits(1000, Ds, Num)).
% 134,580 inferences, 0.023 CPU in 0.026 seconds (87% CPU, 5935694 Lips)
I dare barely mention that the CLP(FD)-based version works more reliably, since this is of so little value in practice. Thus, depending on your use case, the overhead may well be prohibitive. Let us suppose that for this concrete case, we are willing to sacrifice 2 percent of a second in order to use CLP(FD) constraints.
Out of many possible examples, let us now consider the following slight variation of the task:
Let us describe lists of digits that satisfy all constraints, and are also palindromes.
Here is a possible declarative description of palindromes:
palindrome --> [].
palindrome --> [_].
palindrome --> [P], palindrome, [P].
With the CLP(FD)-based version, we can easily combine this constraint with the already stated ones:
n_digits(N, Ds, Num) :-
length(Ds, N),
Ds ins 0'2..0'9,
phrase(palindrome, Ds),
labeling([random_value(0)], Ds),
number_codes(Num, Ds).
Sample query:
?- time(n_digits(1000, Ds, Num)).
% 12,552,433 inferences, 1.851 CPU in 1.943 seconds (95% CPU, 6780005 Lips)
For comparison, how can we incorporate this straight-forward additional constraint into Jan's version? It is tempting to do it as follows:
n_digits(N, Ds, Num) :-
length(Ds, N),
phrase(palindrome, Ds),
maplist(random_between(0'2, 0'9), Ds),
number_codes(Num, Ds).
Pretty straight-forward too, right? The only problem is that it yields:
?- time(n_digits(1000, Ds, Num)).
% 4,013 inferences, 0.041 CPU in 0.046 seconds (88% CPU, 97880 Lips)
false.
Why is this the case?
And now good luck explaining this to any newcomer to the language! When arguing against the purported complexity of explaining CLP(FD)-based versions, please take into account the much, much higher complexity of explaining such purely procedural phenomena, which cannot be understood by declarative reasoning alone.
Further, good look with solving the task at all with low-level features in such a way that additional constraints can still be easily added.
By the way, sometimes, the query will actually succeed!
?- length(_, N), time(n_digits(3, Ds, Num)).
% 28 inferences, 0.000 CPU in 0.000 seconds (85% CPU, 848485 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (83% CPU, 1400000 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (80% CPU, 1166667 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (79% CPU, 1473684 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (82% CPU, 1473684 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (82% CPU, 1473684 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (83% CPU, 1473684 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (83% CPU, 1473684 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (82% CPU, 1473684 Lips)
% 28 inferences, 0.000 CPU in 0.000 seconds (83% CPU, 1473684 Lips)
% 26 inferences, 0.000 CPU in 0.000 seconds (86% CPU, 1444444 Lips)
N = 10,
Ds = [52, 50, 52],
Num = 424 .
I can only say: This approach cannot in honesty be recommended, can it?
If palindromes are too unrealistic for you, consider the following task:
Let us describe lists of digits without 0, 1 and 5.
Again, using CLP(FD), this is straight-forward:
n_digits(N, Ds, Num) :-
length(Ds, N),
Ds ins 0'2..0'9,
maplist(#\=(0'5), Ds),
labeling([random_value(0)], Ds),
number_codes(Num, Ds).
Sample query:
?- time(n_digits(1000, Ds, Num)).
% 203,529 inferences, 0.036 CPU in 0.038 seconds (93% CPU, 5662707 Lips)
For comparison, how would you do it without CLP(FD) constraints?
Now let us combine both requirements:
Let us describe lists of digits without 0, 1 and 5 that are also palindromes.
Again, using the CLP(FD) version, we can simply state the requirement:
n_digits(N, Ds, Num) :-
length(Ds, N),
Ds ins 0'2..0'9,
maplist(#\=(0'5), Ds),
phrase(palindrome, Ds),
labeling([random_value(0)], Ds),
number_codes(Num, Ds).
Sample query:
?- time(n_digits(1000, Ds, Num)).
% 20,117,000 inferences, 2.824 CPU in 2.905 seconds (97% CPU, 7123594 Lips)
These examples illustrate that with the general mechanism of constraints, you have actually somewhere to go in case you need slight variations of previous solutions. The code is quite straight-forward to adapt, and it scales reasonably well.
If there is anything that is too hard to understand, please file a question!
Now, as one last remark: Both CLP(FD)-based versions I posted earlier give you something that goes way beyond such a direct translation. With the other versions, you can post additional constraints not only on the list of digits, but also on the number itself! That's simply completely out of reach for any version that doesn't use constraints! Such constraints are also taken into account before the search for solutions even begins!

Detecting some loops in Prolog goals that do not terminate universally

TL;DR: This question is about one particular aspect of evaluating candidate failure-slices.
The following code of ancestor_of/2 expresses the transitive-closure of child_of/2:
ancestor_of(X, Y) :-
child_of(Y, X).
ancestor_of(X, Z) :-
child_of(Z, Y),
ancestor_of(X, Y).
If we define child_of/2 and include several cycles ...
child_of(xx, xy).
child_of(xy, xx).
child_of(x, x).
... ancestor_of(X, Y) does not terminate universally:
?- ancestor_of(X, Y), false.
**LOOPS**
With SWI-Prolog, we can limit the amount of work invested in the execution of the query:
?- time(call_with_inference_limit((ancestor_of(_,_),false), 10000, R)).
% 10,008 inferences, 0.002 CPU in 0.002 seconds (100% CPU, 4579243 Lips)
R = inference_limit_exceeded ;
% 5 inferences, 0.000 CPU in 0.000 seconds (85% CPU, 235172 Lips)
false.
Alright, but it could be better!
First, we might be able to prove definitely that the goal loops.
Second, we might be able to do so with less effort.
Let's add an additional argument to ancestor_of/2 for transferring call stack information!
ancestor_of_open(X, Y, Open) :-
G = ancestor_of(X,Y),
( member(G, Open)
-> throw(loop(G,Open))
; ancestor_of_aux(X, Y, [G|Open])
).
ancestor_of_aux(X, Y, _Open) :-
child_of(Y, X).
ancestor_of_aux(X, Z, Open) :-
child_of(Z, Y),
ancestor_of_open(X, Y, Open).
Sample query:
?- time(ancestor_of_open(X,Y,[])).
% 4 inferences, 0.000 CPU in 0.000 seconds (92% CPU, 219696 Lips)
X = xy,
Y = xx ;
% 1 inferences, 0.000 CPU in 0.000 seconds (79% CPU, 61839 Lips)
X = xx,
Y = xy ;
% 1 inferences, 0.000 CPU in 0.000 seconds (79% CPU, 61084 Lips)
X = Y, Y = x ;
% 8 inferences, 0.000 CPU in 0.000 seconds (87% CPU, 317473 Lips)
X = Y, Y = xx ;
% 11 inferences, 0.000 CPU in 0.000 seconds (90% CPU, 262530 Lips)
ERROR: Unhandled exception: loop(ancestor_of(_G282,xx),[ancestor_of(_G282,xy),ancestor_of(_G282,xx)])
Is this it?! That was too easy. Does this cover the general case?
I feel like I'm missing something important, but I can't quite point my finger at it. Please help!

Formulating quadratic equations in clpfd

CLPFD-systems are not primarily targeted to handle quadratic equations efficiently, nevertheless, are there better ways to formulate problems like the following?
It seems the problem boils down to equations like the following. SWI with library(clpfd) gave:
?- time( ((L+7)^2#=L^2+189, L in 0..10000000) ).
% 252,169,718 inferences, 87208.554 CPU in 87445.038 seconds (100% CPU, 2892 Lips)
ERROR: Out of local stack
But now the latest version in SWI gives
?- time( ((L+7)^2#=L^2+189, L in 0..10000000) ).
% 3,805,545,940 inferences, 868.635 CPU in 870.311 seconds (100% CPU, 4381063 Lips)
L = 10.
and in SICStus 4.3beta7 I get:
| ?- statistics(runtime,_).
yes
| ?- (L+7)*(L+7)#=L*L+189, L in 0..10000000.
L = 10 ? ;
no
| ?- statistics(runtime,[_,T_ms]).
T_ms = 2550 ? ;
no
To solve this quickly, a constraint solver that has already a primitive X #= Y^2 constraint, might also implement the following rules:
Rule #1:
X #= (Y+C)^2 --> H #= Y^2, X #= H + 2*C*Y + C^2
% where C is an integer and X,Y variables
Rule #2:
X #= Z^2, Y #= Z^2 --> X #= Z^2, X #= Y.
% where X,Y,Z are variables
The above rules will reduce the equation to a linear equation, which is anyway directly solved by a decent CLP(FD) system. Here you see the difference between a system with and without rule #2:
Without Rule #2:
?- (L+7)*(L+7) #= L*L+189, stored.
_C #= 140+_B-14*L.
_B #>= 0.
_C #>= 0.
_B #= L*L.
_C #= L*L.
Yes
With Rule #2:
?- (L+7)*(L+7) #= L*L+189, stored.
L = 10
?- statistics(time, S), (L+7)*(L+7) #= L*L+189, statistics(time, T), U is T-S.
U = 3
But rule #2 looks a little ad hoc to me. Not yet sure whether one should keep it.
Bye
clpBNR fares better:
% *** clpBNR v0.9.13alpha ***.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.4.3)
?- time(( { (L+7)**2 == L**2+189 }, L::integer(0, 10_000_000), solve(L) )).
% 1,269,854 inferences, 0.163 CPU in 0.164 seconds (100% CPU, 7770826 Lips)
L = 10 ;
% 271,125,405 inferences, 40.874 CPU in 40.957 seconds (100% CPU, 6633143 Lips)
false.
Comparing clpfd and clpBNR using a range that succeeds in a reasonable timescale:
?- use_module(library(clpfd)).
?- time( ((L+7)^2#=L^2+189, L in 0..90_000) ).
% 13,609,198 inferences, 11.482 CPU in 11.497 seconds (100% CPU, 1185274 Lips)
L = 10.
?- time(( { (L+7)**2 == L**2+189 }, L::integer(0, 90_000), solve(L) )).
% 540,550 inferences, 0.107 CPU in 0.107 seconds (100% CPU, 5058454 Lips)
L = 10 ;
% 2,105,638 inferences, 0.320 CPU in 0.321 seconds (100% CPU, 6578814 Lips)
false.

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 to f/2 is the structure A-1, or really -(A, 1) as - is an infix operator. So if you want to get the value from a call to foo into a call to bar 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)

Resources