2

What would result from something like the following:

p(X,Y) :- q(X).
p(X,Y) :- r(Y).
q(a).
r(b).

I don't have a Prolog compiler handy, so I can't test what would happen if you then asked p(X,Y). Would the code even compile? Would p return two answers, each with one of the variables unbound?

In a real world scenario, I don't think p(X,Y) would make much sense (one would probably rather want p(X) to follow from either q(X) or r(X)), but I'm interested in what actually happens here, and peripherally, what should happen in such a degenerate case.

4

2 に答える 2

2

p は、変数の 1 つがバインドされていない 2 つの回答を返しますか?

はい。; と入力する必要があります。さらに解決策を表示するには:

| | ?- p(X,Y)。

X = a ? ;

Y = b

はい | ?-

これは、バインドされていない変数とは、この変数に任意の値を代入できることを意味するという理論と一致しています。

于 2009-01-03T10:10:20.210 に答える
1

so.p には以下が含まれます。

p(X,Y) :- q(X).
p(X,Y) :- r(Y).
q(a).
r(b).

$ gprolog
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?- consult('so.p').
compiling /home/jboker/Desktop/so.p for byte code...
/home/jboker/Desktop/so.p:1: warning: singleton variables [Y] for p/2
/home/jboker/Desktop/so.p:2: warning: singleton variables [X] for p/2
/home/jboker/Desktop/so.p compiled, 5 lines read - 506 bytes written, 8 ms

yes
| ?- p(X,Y).

X = a ?

yes
于 2009-01-03T06:12:37.413 に答える