问题
When working with user-defined operators, Prolog sometimes adds / removes parenthesis to expressions involving those operators automatically. Is there a way to control this?
Many thanks
回答1:
The parens are just added by portray. They're not part of the internal representation of the code. You can override portray to change how things are printed. see portray_text
回答2:
An operator is defined like so:
:- op(Precedence, Associativity, Operator).
This answer to a similar question explains this, but the short answer is that the Associativity
option controls this; xfy
makes an operator left-associative and yfx
makes an operator right associative. The Precedence
tells Prolog how to decide which operator is the subexpression when it sees more than one.
来源:https://stackoverflow.com/questions/15175400/controlling-parenthesis-while-working-with-operators