0

I have this facts or database in prolog to see if the educations is the same or less than. for example highschool <= highschool is true and highschool <= phd is true too, but masters <= highschool is false.

edu_less(high_school, bachelor).
edu_less(bachelor, masters).
edu_less(masters, phd).

edu_lessOrEqual(X,X).
edu_lessOrEqual(X, Y):- edu_less(X,Y).
edu_lessOrEqual(X, Y):- edu_less(X,Z),
                        edu_lessOrEqual(Z,Y).

but this outputs

edu_lessOrEqual(masters, phd).
true;
true;
false.

when i want only one true to be printed in the output.

true;
false.
false
  • 10,264
  • 13
  • 101
  • 209
David Andvett
  • 133
  • 4
  • 10

1 Answers1

2

Basically, it's because in the third clause of edu_lessOrEqual/2, you recursively call edu_lessOrEqual/2, so you wind up with a case where Z and Y are both instantiated to phd (phd is in fact equal to phd so it fulfills the logic you have spelled out). You can correct it by adding a Z \= Y on the end of the third clause, but this is a situation where I would be tempted to use a conditional statement just to make sure I don't wind up with useless choice points.

Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77