开发者

prolog, test(X, Y, Z) :- Y is X + Z

How to get Y and Z in prolog, when I only k开发者_开发问答now X?

For example:

test(X, Y, Z) :- X is Y + Z.

but error:

?- test(2, Y, Z).
ERROR: is/2: Arguments are not sufficiently instantiated


It's not possible, because you can choose Y to be anything you want and them compute Z or vice versa.

Although if you know that Y and Z are from some limited set (e.g. positive integers less than 5), you can do something like:

valid_number(1).
valid_number(2).
valid_number(3).
valid_number(4).

test(X, Y, Z) :- valid_number(Y), valid_number(Z), X is Y + Z.


You have to pass them as arguments. Prolog arithmetic (is/2) is not a magic wand, its right argument must be fully instantiated (no variables) before it can be evaluated.

If you want the predicate to work in several "directions", with multiple combinations of ground terms and variables, you'll want to use Constraint Logic Programming, but that's a rather advanced area of logic programming. In CLP on finite domains, you can say

:- use_module(library(clpfd)).  % this differs between implementations
test(X,Y,Z) :- X #= Y + Z.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜