Question

I'm new to prolog and I'm trying to reassign operators in prolog by changing their precedence. I'm running into 4 errors for the following:

 :-op(1000,yf,+).  %unary plus%
 :-op(1000,yf,-).   %unary minus%
 :-op(750,yfx,"%").   %modulo%

The first two give me a similar error that goes like this:

warning: directive failed (op(1000,xf,+)) with exception (error(permission_error(create,operator,+),op/3))

I also get an error with the modulo one (a different error), but I suspect it's because I'm not supposed to enclose % in quotes (but how am I supposed to differentiate it from a comment marker?).

I've redefined a bunch of other operators (such as the addition operator :-op(500,yfx,+).) and they give me no problems. Only the 3 listed above give me errors.

Can anyone shed some light on this?

Thanks!

Was it helpful?

Solution

GNU Prolog documentation states that

An atom can have multiple operator definitions (e.g. prefix and infix like +) however an atom cannot have both an infix and a postfix operator definitions.

from here the errors on first two declaration. Then you should change the specifier to fy. The modulo operator will need single quotes around.

OTHER TIPS

You are attempting to define + as a postfix operator. However, + is also defined as an infix operator and the standard does not permit to define an operator both as postfix and infix. If you really want to do this you have to first undefine the infix operator using priority 0.

However, I can only recommend that you do not change standard operators like + or -. It's like you would change the operator precedence in C, C++, C#, Java, Perl, PHP, Javascript and the like: It would make your life as a programmer very, very miserable.

I cannot recommend to use % as operator in Prolog: % starts a comment. If you want to use it as an operator, you would have to write '%' quoted all the time. Prolog has already mod and rem defined as operators. Isn't that enough?

You are probably using GNU Prolog which is quite ISO conforming. Other Prologs permit you to define infix and postfix at the same time. See #237. But those other Prologs do a lot of things differently.

As a general remark: As a beginner, better stay away from changing the operator table. You really need to get used to the standard operators first. And with more experience you will most probably prefer to only add new operators with similar precedence than existing ones.


: ISO/IEC 13211-1:1995 6.3.4.3 Operators, last paragraph:

There shall not be an infix and a postfix operator with the
same name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top