I am attempting to solve some mutually recursive constraints with SWI-Prolog. These constraints are relatively simple, but querying any of these predicates leads to infinite recursion:
%If X is an animal, then X is a bird or a mammal, and vice-versa.
animal(X) :-
(mammal(X);bird(X)),
(male(X);female(X)).
male(X) :- animal(X).
female(X) :- animal(X).
bird(X) :- (X='parrot';X='pigeon'),animal(X).
mammal(X) :- (X='cat';X='dog'),animal(X).
Would it be possible to solve these constraints in Prolog without making them non-recursive?
I wrote a similar program with several base cases, but the query mammal(X),bird(X) still leads to infinite recursion instead of returning false:
%If X is an animal, then X is a bird or a mammal, and vice-versa.
animal(X) :-
(mammal(X);bird(X)).
bird('parrot').
bird('pigeon').
bird(X) :- (X='parrot';X='pigeon'),animal(X).
mammal('cat').
mammal('dog').
mammal(X) :- (X='cat';X='dog'),animal(X).
dif(mammal(X), bird(X))doesn't do what you probably think it does. In fact, it will always succeed since the termsmammal(X)andbird(X)are always necessarily different for anyX. As Scott points out in his "answer", you don't have any facts or base cases.dif/2predicates were redundant in this case. I edited the program to correct this problem.animal/1is defined in terms lfmale/1,female/1andmammal/1. Andmale/1,female/1, andmammal/1are defined in terms ofanimal/1.mammaldefined in terms ofanimal, andanimaldefined in terms ofmammal.