Consider this program as an example. It uses a delayed goal
room(green).
room(blue).
room(red).
room(white).
location(jimmy,red).
location(ricky,blue).
location(cindy,green).
% "Is a certain room unoccupied?"
not_occupied(Room) :-
nonvar(Room),
assertion(room(Room)),
\+ location(_Person,Room).
% If no specific "Room" has been given, the negated goal is
% delayed until the "Room" has been instantiated.
not_occupied(Room) :-
var(Room),
!,
when(
ground(Room),
(\+ location(_Person,Room))
).
If I now ask
?- not_occupied(R).
then Prolog succeeds and outputs a residual goal
?- not_occupied(R).
when(ground(R),\+location(_7676,R)).
In effect, it doesn't really succeed. It succeeds optimistically (because in order to not stop the computation, succeed it must) but the actual logical success is dependent on actual success of the residual goal.
How do I find out programmatically whether a subgoal succeeded with a residual goal? (And what do I do then?) What is the approach?
P.S.
Having a secondary Prolog truth value might be a nice Prolog extension, in that a true+ would indicate "success under condition that the residual goals succeed". This actually seems to be of some necessity:
In SWI-Prolog, take this inherently ambiguous goal:
do :- not_occupied(_).
Calling it does not even print out any residual goal at all:
?- do.
true.
Did the goal succeed? Not really, it's still in logical limbo, but the toplevel doesn't even tell me. On the other hand there is no way to feed more information into the program to resolve residual goals. But defaulting to "success" because the computation ran to its end feels wrong.