I have the next two facts loaded in my prolog interpreter:
我在我的prolog解释器中加载了下两个事实:
foo(U+V,1).
foo(U*V,2).
Now I try the next queries with that results:
现在我尝试使用该结果的下一个查询:
foo(x*x+x,R). --> R = 1
foo(x+x*x,R). --> R = 1
foo(x*x*x,R). --> R = 2
Now I try with the next query:
现在我尝试下一个查询:
foo(x*x-x,R). --> no
As I understand, this is explained by how the operator precedence build the tree expression:
据我所知,这可以通过运算符优先级如何构建树表达式来解释:
x+x*x --> + so it matches with --> +
/ \ / \
x * U V
/ \
x x
x-x*x --> - DOES NOT matches any fact.
/ \
x *
/ \
x x
Is this explanation correct?
这个解释是否正确?
1 个解决方案
#1
Yes, this is correct.
是的,这是正确的。
The default operator precedence is defined to be natural, i.e. use the normal mathematical precedence. But if you don't like that you can redefine it.
默认的运算符优先级被定义为自然的,即使用正常的数学优先级。但如果你不喜欢它,你可以重新定义它。
Whether changing the precedence is a great idea is another matter, it effectively changes the syntax of Prolog and can lead to parsing problems. Especially if you change the precedence of the operators for the Prolog syntax, with precedence above 1000.
更改优先级是一个好主意是另一回事,它有效地改变了Prolog的语法并且可能导致解析问题。特别是如果您更改Prolog语法的运算符的优先级,优先级高于1000。
#1
Yes, this is correct.
是的,这是正确的。
The default operator precedence is defined to be natural, i.e. use the normal mathematical precedence. But if you don't like that you can redefine it.
默认的运算符优先级被定义为自然的,即使用正常的数学优先级。但如果你不喜欢它,你可以重新定义它。
Whether changing the precedence is a great idea is another matter, it effectively changes the syntax of Prolog and can lead to parsing problems. Especially if you change the precedence of the operators for the Prolog syntax, with precedence above 1000.
更改优先级是一个好主意是另一回事,它有效地改变了Prolog的语法并且可能导致解析问题。特别是如果您更改Prolog语法的运算符的优先级,优先级高于1000。