Arithmetic functions, documentation and operator inconsistencies

The documentation of mathematical functions, and the declarations of operators is inconsistent and confusing.

mod is documented with +IntExpr1 mod +IntExpr2, and current_op(_,_,mod) is true.
gcd is documented with +IntExpr1 gcd +IntExpr2, but current_op(_,_,gcd) is false.
div is documented with div(+IntExpr1, +IntExpr2), yet current_op(_,_,div) is true.

It occurred to me that gcd not being an operator might have something to do with the fact that it’s not an ISO function. However

rdiv is not ISO, is documented with +IntExpr1 gcd +IntExpr2, and current_op(_,_,rdiv) is true.

There is no relation between arithmetic functions and Prolog operators. E.g., ISO sin(Angle) is also not an operator. There is current_arithmetic_function/1 to test whether something is evaluable by is/2, =:=/2, etc. Unfortunately that is not a standard predicate :frowning:

rdiv is from the days that SWI-Prolog did not have rational numbers as first class citizens. It should not have been an operator, but deleting it will probably make (too) many people unhappy because they still have legacy code from before the days that SWI-Prolog supports rational numbers properly.

1 Like

I agree. But I would even go a step farther and avoid Prolog operator,
since it still doesn’t unambigiously indicate a non-semantic function.
Sometimes it helps to use the term syntax operator (*) more consistently

in verbal communication and technical documentation than simply using
the ambigious word Prolog operator. This practice is not found in the ISO
core, which uses Prolog operator for both arithmetic function and syntax operator,

depending in which document section the word is used, but this doesn’t
mean it shouldn’t be practiced outside ISO core. There are more examples of
arithmetic functions that are not syntax operators, for example this here:

?- X is xor(1,2).
X = 3.

That SWI-Prolog has also defined it as a syntax operator is not
required as per ISO core since it was only introduced as a
arithmetic function:

xor/2 (as functor only)
https://www.complang.tuwien.ac.at/ulrich/iso-prolog/dtc2

(*) Could be my own private vocabulary. What one finds
in the wild is operator notation. But if I want to refer to a
specimen of the concept I prefer saying the syntax operator.

1 Like

Agree. SWI-Prolog’s section on arithmetic does not talk about operators at all though. It always refers to them as functions (or arithmetic functions).

Still, the docs are misleading to the uninitiated. I am looking here for example.

Since it is using \infixfunction macro, it shows up on the website as:

+IntExpr1 gcd +IntExpr2

but when we type it like that its no work:

?- X is 3 gcd 5.
ERROR: Syntax error: Operator expected

If instead of

\infixfunction{gcd}{+IntExpr1}{+IntExpr2}

it would say

\function{gcd}{2}{+Expr1, +Expr2}

there would be no confusion.

I again apologize for not making PRs for docs lightly, it way easier to mess it up than to make it better.

2 Likes

Indeed. Fixed.

2 Likes