Reputation: 7476
This may be simple question, but here goes ..... let say I have something along the lines :
.... step1, step2, step3, ...
i want every time step3 fail backtracking to skip step2 and go to step1 instead, but on the way forward to follow the normal ..step1, step2, step3 .. sequence.
.... step1, (step2, step3), ...
doesn't seem to do it, or I'm doing something wrong.
It needs more testing, but this seems to work so far :
.... step1, (step2 -> (step3; true)), ...
may be this is swi-prolog idiosyncrasy, to require true.
Upvotes: 2
Views: 230
Reputation: 58244
The expression step1, (step2, step3)
is no different in behavior than step1, step2, step3
. The parentheses only force a precedence grouping, which has no effect in this case (it's like writing 1 + (2 + 3)
instead of 1 + 2 + 3
).
A possible solution is:
step1, (step2 -> step3)
It means that you would only ever get the first solution out of step2
. So I think it's the same as:
step1, once(step2), step3
A "step" in the above two examples could be a conjunction of sub-steps, with suitable parentheses, if desired.
Upvotes: 2
Reputation: 40768
In the literature, this search strategy is also called backjumping.
One way to implement this in Prolog is using exceptions.
See the ISO predicates catch/3
and throw/1
:
In particular, in your case, you could arrange it so that failure of step3
leads to a "ball" being thrown via throw/1
, which can be caught via catch/3
at the intended target position.
Upvotes: 3