“:=”和“==”在水星

发布于 2024-11-28 00:19:18 字数 479 浏览 1 评论 0原文

我最近在 Mercury 中看到了这个代码示例:

append(X,Y,Z) :-
  X == [],
  Z := Y.
append(X,Y,Z) :-
  X => [H | T],
  append(T,Y,NT),
  Z <= [H | NT].

作为一名 Prolog 程序员,我想知道:普通的统一 = 之间有什么区别 以及这里使用的 :==>

Mercury 参考< /a>,这些运算符获得不同的优先级,但他们没有解释其中的差异。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没结果 2024-12-05 00:19:18

首先让我们使用缩进重新编写代码:

append(X, Y, Z) :-
    X == [],
    Z := Y.
append(X, Y, Z) :-
    X => [H | T],
    append(T, Y, NT),
    Z <= [H | NT].

您似乎必须将所有代码缩进四个空格,这在注释中似乎不起作用,我上面的注释应该被忽略(我无法删除它们)。

上面的代码不是真正的 Mercury 代码,而是伪代码。它作为真正的 Mercury 代码没有意义,因为 <==> 运算符用于类型类 (IIRC),而不是统一。另外,我以前没有见过 := 运算符,我不确定它的作用。

在这种伪代码风格中(我相信),作者试图表明 := 是统一的赋值类型,其中 X 是分配了值Y。类似地,=> 显示 X解构<= 显示构造< /em> 的 Z。另外 == 显示 X 和空列表之间的相等测试。所有这些操作都是统一的类型。编译器知道谓词的每种模式应该使用哪种类型的统一。对于此代码,有意义的模式是 append(in, in, out)

Mercury 在这方面与 Prolog 不同,它知道要使用哪种类型的统一,因此可以生成更高效的代码并确保该程序模式正确

还有一件事,此伪代码的真实 Mercury 代码将是:

:- pred append(list(T)::in, list(T)::in, list(T)::out) is det.

append(X, Y, Z) :-
    X = [],
    Z = Y.
append(X, Y, Z) :-
    X = [H | T],
    append(T, Y, NT),
    Z = [H | NT].

请注意,每个统一都是 = 并且已添加谓词和模式声明。

First let's re-write the code using indentation:

append(X, Y, Z) :-
    X == [],
    Z := Y.
append(X, Y, Z) :-
    X => [H | T],
    append(T, Y, NT),
    Z <= [H | NT].

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 where X is assigned the value of Y. Similarly => is showing a deconstruction of X and <= is showing a construction of Z. Also == shows an equality test between X 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 is append(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:

:- pred append(list(T)::in, list(T)::in, list(T)::out) is det.

append(X, Y, Z) :-
    X = [],
    Z = Y.
append(X, Y, Z) :-
    X = [H | T],
    append(T, Y, NT),
    Z = [H | NT].

Note that every unification is a = and a predicate and mode declaration has been added.

感情洁癖 2024-12-05 00:19:18

在具体的 Mercury 语法中,运算符 := 用于字段更新。

In concrete Mercury syntax the operator := is used for field updates.

送君千里 2024-12-05 00:19:18

也许我们无法使用像 ':=' '<=' '=>' 这样的运算符最近的 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 =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文