Prolog中缀运算符定义 [英] Prolog infix operator definition
问题描述
我最近正在学习Prolog,并且发现用于定义中缀运算符的三种类型令人困惑.
I am recently learning about Prolog and I find the three types used for defining infix operators confusing.
在指定运算符类型时,xfx,xfy和yfx有什么区别? 我已经搜索了这个问题,但没有发现任何有用的东西.
What are the differences between xfx, xfy and yfx when specifying the type of an operator? I have googled about the problem and haven't found anything useful.
我尝试在Prolog中键入以下代码:
I tried typing the following codes in Prolog:
:- op(500,yfx,is_alive).
is_alive(A,B) :- display([A,B]).
:- op(500,xfy,is_alive2).
is_alive2(A,B) :- display([A,B]).
:- op(500,xfx,is_alive3).
is_alive3(A,B) :- display([A,B]).
和输出:
| ?- 1 is_alive 2.
'.'(1,'.'(2,[]))
yes
| ?- 1 is_alive2 2.
'.'(1,'.'(2,[]))
yes
| ?- 1 is_alive3 2.
'.'(1,'.'(2,[]))
yes
结果对我来说没有区别.
The results showed no difference to me.
推荐答案
运算符的类型"控制关联性,例如表达式"5-4-3"应解释为(5-4)-3",即左联想性还是"5-(4-3)",即右联想性.
The 'type' of the operator controls associativity, e.g. whether expression "5 - 4 - 3" should be interpreted as "(5 - 4) - 3", which is known as left associativity or as "5 - (4 - 3)", which is right associativity.
如果减号运算符应按通常预期的方式工作,则需要将定义为'yfx',使其与左侧关联.其他类型,例如"xfx"和"xfy"是对此的变体.有关更多信息,请参见 http://www.cse.unsw .edu.au/〜billw/cs9414/notes/prolog/op.html .
If the minus operator should work as usually expected, is needs to be defined as 'yfx' which makes it left-associative. Other types as 'xfx and 'xfy' are variations on this. See more at http://www.cse.unsw.edu.au/~billw/cs9414/notes/prolog/op.html.
这篇关于Prolog中缀运算符定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!