Swi Prolog- Syntax Error, Operator Priority Clash

Swi Prolog- Syntax Error, Operator Priority Clash

I'm trying to create a function that checks if a square is intersecting a rectangle. I have multiple test cases, just showing first one, but even for the first test case I keep getting an "operator priority clash" error. This is resulting in every case being returned as false, even though I double checked my math and it should return true for this first case. What am I doing wrong?

intersect(
      square(point2d(X1,Y1),LENGTH),
      rectangle(point2d(X2,Y2),point2d(X3,Y3))) :-
   X1 =< X2 =< (X1 + LENGTH),
   (Y1-LENGTH) =< Y2 =< Y1.
1

1 Answer

Arithmetic operators behaviour is not specific of SWI-Prolog. As you may expect, they are binary built in predicates that evaluate their arguments as arithmetic expressions.

You have to explicit the range checking: for instance

intersect(
  square(point2d(X1,Y1),LENGTH),
  rectangle(point2d(X2,Y2),point2d(X3,Y3))
) :-
  X1 =< X2, X2 =< (X1 + LENGTH), (Y1-LENGTH) =< Y2, Y2 =< Y1.
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.