In order to find all possible instantiations of a variable in a query, a LOOP must be defined in Prolog: when one constant has been matched, Prolog must be forced to go back and define another. There are various techniques for doing this; one, which simplifies some of the specific features of Prolog, is shown in the following file.
% recipe1.pl
% D. Gibbon, 29.10.97
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Helpful predicates.
%% Backtrack (go back and take the next choice):
backtrack :- fail.
%% Carry on after backtracking:
perform(X) :- call(X),!,fail.
perform(X).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ingredient(vinaigrette,vinegar).
ingredient(vinaigrette,oil).
ingredient(vinaigrette,salt).
ingredient(vinaigrette,pepper).
ingredient(vinaigrette,mustard).
mix(vinaigrette,salt,mixture0,mixture1,by_shaking).
mix(vinaigrette,pepper,mixture1,mixture2,by_shaking).
mix(vinaigrette,mustard,mixture2,mixture3,by_stirring).
mix(vinaigrette,vinegar,mixture3,mixture4,by_stirring).
mix(vinaigrette,oil,mixture4,mixture5,by_stirring_dropwise).
examples :-
nl,write('Match: '),ingredient(vinaigrette,mustard),write('yes'),tab(1),
backtrack.
examples :-
nl,write('Ingredients: '),ingredient(vinaigrette,What),write(What),tab(1),
backtrack.
examples :-
nl,write('Recipes: '), ingredient(Food,mustard),write(Food),tab(1),
backtrack.
examples :-
nl,write('Mix_styles: '), mix(Food,Ingredient1,Ingredient2,Mixture,Mix_style),write(Mix_style),tab(1),
backtrack.
:- perform(examples), nl, nl, halt.
The output of the programme is:
Match: yes
Ingredients: vinegar oil salt pepper mustard
Recipes: vinaigrette
Mix_styles: by_shaking by_shaking by_stirring by_stirring by_stirring_dropwise