Prolog Operator Priority

User input is specified in the format a: b> c> d> e ... and so on. I would like to analyze the input in: (a,> (b,> (c,> (d, e))) would it be possible and is there any suggestion to do this? I tested with this

prepare:- op(750,xfx,user:(:)), % change the default priority of : and > op(700,xfx,user:(>)), display(a: b > c),%this one worked fine and %the display value is :(a,>(b,c)) display(a: b > c > d ). % I cannot have this works, %the error ERROR: %Syntax error: %Operator priority clash is thrown. 

Input cannot be changed. Any suggestion appreciated. Thanks!

+4
source share
1 answer

Perhaps, first of all: both (:)/2 and (>)/2 are already infix operators defined in the standard:

 :- op(600, xfy, :). % ISO/IEC 13211-2 5.2.1, table 1 :- op(700, xfx, >). % ISO/IEC 13211-1 6.3.4.4, table 7 

Changing their priority means that you are changing your overall meaning. This is often not a good idea. Think about it: he, like you, can (change) the priority of statements in Java, C #, C ++, Perl or PHP. They all hesitated to change the priority they inherited from C.

But, strictly speaking, you can do it.

To minimize the negative effect of such a change, try saving the declaration in your own module. And in a system that does not contain modules-local operators, make sure that you return to the original ads safely.

You wrote user:(>) , which affects a special user module. Instead, write your own module.

... or perhaps revise the statement that needs to be changed.

Conditions are read until the next period in one fell swoop. Therefore, if you have a rule, as you have shown, an operator declaration will only take effect when prepare is executed. Therefore, this does not affect the display/1 target argument. You probably loaded prepare , executed and; and rebooted it.

To make an operator’s announcement effective, either execute it directly at the top level (this is a quick hack); or write it as a directive to a file or module.

Then the > -like operator, I will use :> its place should be right-associative, like : and the priority below : You declared xfx , which means no associativity.

 ?- op(500,xfy,:>). true. ?- write_canonical(a:b:>c:>d). :(a,:>(b,:>(c,d))) true. 
+4
source

All Articles