Search code examples
prologiso-prolog

Why does prolog use =< and not <=?


The numerical comparison operators in prolog are pretty much the same as every other language.

operator meaning
X > Y X greater than Y
X < Y X less than Y
X >= Y X greater than or equal to Y
X =< Y ( not <= ) X less than or equal to Y

Question: Why did prolog diverge from most other languages and use =< and not <=?

Is the reason to due with how such symbol were written by hand, the explanation often cited for the :- meaning <- implication from right to left.


updated - edited to correct typos


Solution

  • In imperative programming languages, smaller equal is literally written as <=. But in Prolog, <= looks more like an arrow to the left since Prolog is often used to implement various theorem provers. Thus =< was the more natural choice1 for the arithmetical comparison operator.

    For arithmetical evaluation and comparison, this is just a little nuisance as a misuse will show up as a harmless syntax error. But it becomes more cumbersome in the context of constraints. The implementation of by SICStus has both #<= and #=< as infix operators meaning implication and smaller equal respectively which leads to many unnecessary beginners' errors. Note that such errors are very difficult to detect, since many uses like X #<= 1 are valid for both meanings.

    Because of this confusion, newer implementations like clpfd for SWI and its successor clpz (for both SICStus and Scryer) use rather #<== and #==> for implication and thus do not define #<= at all.

    Note that also other programming languages have to work around this problem. Most notably Haskell uses => for class constraints thus writing the arrow in the other direction since <= is already taken to mean comparison.


    1 At least since DEC 10 Prolog of 1978.

    The earliest use is probably in the benchmark qsort on page 54 of Report 40, D.H.D. Warren, Implementing Prolog—Compiling Predicate Logic Programs. May 1977.

    partition([X,..L],Y,[X,..L1],L2) :- X =< Y, !,
       partition(L,Y,L1,L2).
    

    This ,.. is a | in ISO. The ..-notation is still present in =.. univ.