For my physics class, we're racing various rolling things down a track and trying to figure out what makes the fastest ones fastest.
I want to save time and not race everything against everything. For example, the can of chicken soup beats the can of clams, and the can of clams beats the can of tomatoes. So, the chicken soup must beat the tomatoes.
Here is everything I've written so far:
% Define everything we know
outspeeds(chicken_soup, clams). % This represents "chicken_soup outspeeds clams."
outspeeds(beef, clams).
outspeeds(chicken_soup, beef).
outspeeds(chicken_soup, tomatoes).
outspeeds(clams, tomatoes).
% Now define inter-relationships
% A outspeeds B if A outspeeds some 3rd party "X" and they outspeed B.
outspeeds(A, B) :- outspeeds(A, X), outspeeds(X, B).
The problem is when I try to ask questions about it. Here is what happens if I try to find everything that chicken_soup
outspeeds:
?- outspeeds(chicken_soup, X).
X = clams ;
X = beef ;
X = tomatoes ;
X = tomatoes ;
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 1.0Gb, global: 4Kb, trail: 0Kb
ERROR: Stack depth: 12,197,875, last-call: 0%, Choice points: 5
ERROR: Probable infinite recursion (cycle):
ERROR: [12,197,875] user:outspeeds(tomatoes, _1098)
ERROR: [12,197,874] user:outspeeds(tomatoes, _1118)
Why is it doing this? What additional clause do I need in the last outspeeds
to keep it from a stack overflow?
I'm new to Prolog so if there's anything horribly wrong with the project as whole please tell me.