One way to achieve this is as follows:
man(X) :-
(nonvar(X), man(X, _)), !
;
man(X, _).
Or, more preferred, would be:
man(X) :-
( var(X)
-> man(X, _)
; once(man(X, _))
).
The cut will ensure only one solution (at most) to an instantiated X
, whereas the non-instantiated case will run its course. Note that, with the cut, you don't need once/1
. The reason once/1
doesn't work as expected without the cut is that backtracking will still come back and take the "or" condition and succeed there as well.