SWI Prolog- Syntax Error, Operator priority clash

11,796

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.
Share:
11,796

Related videos on Youtube

John
Author by

John

Updated on June 04, 2022

Comments

  • John
    John almost 2 years

    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.
    
    • false
      false over 6 years
      There is also abs/1 which helps for distances! Like abs(X1,X2) =< Rad
  • John
    John over 6 years
    THANK YOU! I'm very new to prolog, still learning all the syntax, this helped so much! was stuck on this for hours!
  • lurker
    lurker over 6 years
    @John Most languages I know, even C, don't allow 3-way comparisons such as x < y < z as a numeric range check. In C, it would be translated into comparing z with the "boolean" result of comparing x < y, which is generally not the intent. It's typically done in pieces: x < y && y < z in C, or in Prolog: X < Y, Y < Z.