3 ms·
> I cannot easily think of an example where changing the order of clauses (not "goals") in a predicate definition prevents the program from "runnign backwards".
by tom_mellior 6y ago
> I cannot easily think of an example where changing the order of clauses (not "goals") in a predicate definition prevents the program from "runnign backwards".
It's bad enough to use "directions" for what are properly called "modes", but it's positively counterproductive to say that anything in Prolog runs "backwards". Prolog always runs top-to-bottom, left-to-right, and this is precisely why it's easy to write nonterminating definitions. As a community, we are doing ourselves a disservice by claiming that anything can magically run "backwards".
As for your question, the canonical example for clause ordering being a problem is where the incorrect order prevents generation of answers because a recursive clause precedes a non-recursive one.
Example:
xs([x | Xs]) :- xs(Xs).
xs([]).
This recognizes lists of xs:
?- xs([x, x, x]).
true.
But it cannot generate them:
?- xs(Xs).
ERROR: Out of local stack
Contrast with:
ys([]).
ys([y | Ys]) :- ys(Ys).
?- ys(Ys).
Ys = [] ;
Ys = [y] ;
Ys = [y, y] ;
Ys = [y, y, y] .
- YeGoblynQueenne 6y agoI think everyone knows what we mean about "running code backwards" and I was sticking to the OP's choice of language for simplicity. I don't disagree with your (nevertheless pedantic) observation, however. I agree we should be more precise in our terminology. Anyway my bad, I guess you can think of many cases if you try a little. I apologise. I was not in my programming mind today.