Prolog doesn't give multiple solutions to my goal.
- From: Giovanni Gherdovich <gherdovich@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 23 Aug 2007 15:08:27 -0000
Hello.
I wrote this minimal example code to explain my problem.
The predicate `compute` take a list as input; this list
is a set of instructions like 'put_a', 'put_b' and 'put_x_or_y'.
When it reads 'put_a', it appends an `a` to the
instructions list, and so on; after it, the read instruction
is deleted.
When it reads `put_x_or_y` it can appends an `x`
or an `y` in a nondeterministic fashion.
This is the code:
% =================== %
compute(List):-
finished(List),
nl, write('Outcome of the computation: '), write(List).
compute([put_a|Tail]):-
append(Tail,[a],UpdatedList),
compute(UpdatedList).
compute([put_b|Tail]):-
append(Tail,[b],UpdatedList),
compute(UpdatedList).
compute([put_x_or_y|Tail]):-
append(Tail,[x],UpdatedList),
compute(UpdatedList).
compute([put_x_or_y|Tail]):-
append(Tail,[y],UpdatedList),
compute(UpdatedList).
finished(List):-
not(member(put_a,List)),
not(member(put_b,List)),
not(member(put_x_or_y,List)).
% =================== %
When I wrote that I expected for a query like
?- compute([put_a,put_x_or_y,put_x_or_y,put_b]).
all the possible result, i.e.
[a, x, x, b]
[a, x, y, b]
[a, y, x, b]
[a, y, y, b]
afterall Prolog has multiple choices for the
head to match with [put_x_or_y|Tail], and after
the goal succeds it should go back on his steps
to take the ways previusly ignored...
But instead of it I got only [a, x, x, b].
Why backtracking doesn't do my game?
Thanks in advance,
Giovanni
.
- Follow-Ups:
- Re: Prolog doesn't give multiple solutions to my goal.
- From: student
- Re: Prolog doesn't give multiple solutions to my goal.
- From: Bart Demoen
- Re: Prolog doesn't give multiple solutions to my goal.
- Prev by Date: Re: memory management in swi prolog
- Next by Date: how to rename variables?
- Previous by thread: memory management in swi prolog
- Next by thread: Re: Prolog doesn't give multiple solutions to my goal.
- Index(es):