Hayato
Hayato

Reputation: 43

How to return general formulas from query?

So in many cases, like:

summa(X, Y, Z) :- 
    Z #= X + Y.

You can give the most general query to return a generic solution:

?-summa(X, Y, Z).

$VAR(X)+ $VAR(Y)#= $VAR(Z).

I have a formula involving recursion (similar to factorial) and I was hoping for a general version of it; however, when I give Prolog the most general query, it gives me specific answers, like:

X = 1, Y = 1;
X = 2, Y = 5;
X = 3, Y = 19.

Does this mean Prolog cannot make a formula out of recursive things, or is there some kind of prerequisite to getting a formulaic answer?

Note, for the standard factorial rule, it has the same behavior:

factorial(1, Y) :-
    Y #= 1.
    
factorial(X, Y) :-
    X #> 1,
    Xnew #= X - 1, 
    Y #= X * Z,
    factorial(Xnew, Z).

This returns:

?- factorial(X, Y).

X = Y, Y = 1 ;
X = Y, Y = 2 ;
X = 3, Y = 6 ;
X = 4, Y = 24 ;
X = 5, Y = 120 ;
X = 6, Y = 720 ;
X = 7, Y = 5040 .

Upvotes: 1

Views: 138

Answers (1)

TA_intern
TA_intern

Reputation: 2436

You are observing a feature of the library which you are using.

What do you expect to see with something simpler?

?- X #= 1.

Do you expect to see this shown back to you verbatim, or do you expect it solved?

How about something slightly more complicated?

?- X #= 2*Y.

And what if we add:

?- X #= 2*Y, Y = 3.

Note: this is library-specific behaviour. You should probably read the library documentation. I don't think this is generic Prolog question.

Upvotes: 1

Related Questions