简体   繁体   中英

Defining operators in Prolog

Having defined two operators in Prolog:

op(100, xfy, #).
op(100, fy, ϴ).

what is the expression

a # ϴ b # c

equivalent to?

a # ϴ (b # c)

or maybe

a # ((ϴ b) # c)

And why?

You can see what Prolog will do with it using write_canonical/1 :

?- op(100,xfy,#).
true.

?- op(100,fy,@).
true.

?- write_canonical(a # @ b # c).
#(a,@(#(b,c)))
true.

So, it appears that your first speculation is correct. The interpretation of a # @ b # c is a # (@(b # c)) . The key comment in the documentation for op/3 bearing on this is:

The f indicates the position of the functor, while x and y indicate the position of the arguments. y should be interpreted as "on this position a term with precedence lower or equal to the precedence of the functor should occur". For x the precedence of the argument must be strictly lower.

The use of fy results in the precedence grouping of @(b # c) rather than (@b) # c .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM