tags:

views:

83

answers:

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.

A: 

so.p contains:

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
John Boker
+1  A: 

Would p return two answers, each with one of the variables unbound?

Yes. You need to type a ; to see further solutions:

| ?- p(X,Y).

X = a ? ;

Y = b

yes | ?-

This agrees with the theory, where an unbound variable means that any value can be substituted for this variable.

starblue