Consider this Prolog predicate:
silly:-
1 = 1.
silly:-
1 = 2.
When querying, the output answer has two states: true and then false. Is there anyway to ask Prolog to terminate as soon as it hits a true statement in a disjunction?
The question is "Is there anyway to ask Prolog to terminate as soon as it hits a true statement in a disjunction"?
The answer is "Use once/1
when querying for the answer".
For example:
?- [user] .
silly:-
1=1 .
silly:-
1=2 .
silly:-
2=2 .
end_of_file .
?- %% query WITHOUT once {results in 2 answers} : %%
silly .
true ;
true.
?- %% query WITH once {results in 1 answer} : %%
once(silly) .
true.
The use of cut !
does not meet the semantic requirements of the question as stated.
Using cut would be appropriate if the question had been more like "Is there anyway to ask tell Prolog to terminate as soon as it hits a true statement** a statement specially indicated* in a disjunction" ?
add a cut
silly:-
1 = 1, !.
silly:-
1 = 2.
or use if/then/else, but then the 'program' take a very different shape, being the alternative branches merged into a single clause. Also note that, as stated in documentation
Unlike !/0, the choice point of the predicate as a whole (due to multiple clauses) is not destroyed.
silly:-
( 1 = 1 -> true ; false ).