“:=”和“==”在水星
我最近在 Mercury 中看到了这个代码示例:
append(X,Y,Z) :-
X == [],
Z := Y.
append(X,Y,Z) :-
X => [H | T],
append(T,Y,NT),
Z <= [H | NT].
作为一名 Prolog 程序员,我想知道:普通的统一 =
之间有什么区别 以及这里使用的 :=
或 =>
?
I recently came across this code example in Mercury:
append(X,Y,Z) :-
X == [],
Z := Y.
append(X,Y,Z) :-
X => [H | T],
append(T,Y,NT),
Z <= [H | NT].
Being a Prolog programmer, I wonder: what's the difference between a normal unification =
and the :=
or =>
which are use here?
In the Mercury reference, these operators get different priorities, but they don't explain the difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先让我们使用缩进重新编写代码:
您似乎必须将所有代码缩进四个空格,这在注释中似乎不起作用,我上面的注释应该被忽略(我无法删除它们)。
上面的代码不是真正的 Mercury 代码,而是伪代码。它作为真正的 Mercury 代码没有意义,因为
<=
和=>
运算符用于类型类 (IIRC),而不是统一。另外,我以前没有见过:=
运算符,我不确定它的作用。在这种伪代码风格中(我相信),作者试图表明
:=
是统一的赋值类型,其中X
是分配了值Y
。类似地,=>
显示X
的解构,<=
显示构造< /em> 的Z
。另外==
显示X
和空列表之间的相等测试。所有这些操作都是统一的类型。编译器知道谓词的每种模式应该使用哪种类型的统一。对于此代码,有意义的模式是append(in, in, out)
Mercury 在这方面与 Prolog 不同,它知道要使用哪种类型的统一,因此可以生成更高效的代码并确保该程序模式正确。
还有一件事,此伪代码的真实 Mercury 代码将是:
请注意,每个统一都是
=
并且已添加谓词和模式声明。First let's re-write the code using indentation:
You seem to have to indent all code by four spaces, which doesn't seem to work in comments, my comments above should be ignored (I'm not able to delete them).
The code above isn't real Mercury code, it is pseudo code. It doesn't make sense as real Mercury code because the
<=
and=>
operators are used for typeclasses (IIRC), not unification. Additionally, I haven't seen the:=
operator before, I'm not sure what is does.In this style of pseudo code (I believe) that the author is trying to show that
:=
is an assignment type of unification whereX
is assigned the value ofY
. Similarly=>
is showing a deconstruction ofX
and<=
is showing a construction ofZ
. Also==
shows an equality test betweenX
and the empty list. All of these operations are types of unification. The compiler knows which type of unification should be used for each mode of the predicate. For this code the mode that makes sense isappend(in, in, out)
Mercury is different from Prolog in this respect, it knows which type of unification to use and therefore can generate more efficient code and ensure that the program is mode-correct.
One more thing, the real Mercury code for this pseudo code would be:
Note that every unification is a
=
and a predicate and mode declaration has been added.在具体的 Mercury 语法中,运算符
:=
用于字段更新。In concrete Mercury syntax the operator
:=
is used for field updates.也许我们无法使用像 ':=' '<=' '=>' 这样的运算符最近的 Mercury 版本中出现了 '==',但根据 Nancy Mazur 论文中的描述,实际上这些运算符是专门化的统一。
'=>'代表解构,例如 X => f(Y1, Y2, ..., Yn),其中 X 为输入,所有 Yn 为输出。这是半德特。 '<=' 则相反,并且是 det。 '=='用于两边都磨的情况,是semidet。 ':=' 就像任何其他语言中的常规赋值运算符一样,并且它是 det。在较旧的论文中,我什至看到他们使用“==”而不是“=>”执行解构。 (我觉得我的英语很糟糕=x=)
Maybe we are not able to use such operators like ':=' '<=' '=>' '==' in recent Mercury release, but actually these operators are specialized unification, according to the description in Nancy Mazur's thesis.
'=>' stands for deconstruction e.g. X => f(Y1, Y2, ..., Yn) where X is input and all Yn is output. It's semidet. '<=' is on the contrary, and is det. '==' is used in the situation where both sides are ground, and it is semidet. ':=' is just like regular assigning operator in any other language, and it's det. In older papers I even see that they use '==' instead of '=>' to perform a deconstruction. (I think my English is awful = x =)