简体   繁体   English

需要一个水银记录语法的例子

[英]Need an example of Record Syntax in mercury

I am new to mercury and am trying to wrap my head around Record Syntax , but the Reference Manual is the only place I have encountered it and it leaves me mystified: 我是水银的新手,我正试图围绕记录语法 ,但参考手册是我遇到它的唯一地方,它让我感到神秘:

Term ^ field1(Arg1) ^ field2(Arg2, Arg3) is equivalent to field2(Arg2, Arg3, field1(Arg1, Term)). 术语^ field1(Arg1)^ field2(Arg2,Arg3)等同于field2(Arg2,Arg3,field1(Arg1,Term))。

Could someone please help with a real-world example? 有人可以帮助一个真实世界的例子吗?

Record syntax is a syntax sugar, and the manual is trying to explain how the transformation from record syntax into Mercury's normal syntax works. 记录语法是一种语法糖,手册试图解释从记录语法到Mercury的正常语法的转换是如何工作的。 This is fine if you're trying to find out how to implement record syntax, but not very helpful if you want to learn how to use it. 如果您想要了解如何实现记录语法,这很好,但如果您想学习如何使用它,则不是很有用。

I recommend ignoring the (Arg1, ...) stuff in the parenthesis - I'm not sure if it's actually part of the syntax and I've never seen anyone use it. 我建议忽略括号中的(Arg1,...)东西 - 我不确定它是否实际上是语法的一部分,我从未见过有人使用它。

Lets create a structure representing points on a Cartesian plane. 让我们创建一个表示笛卡尔平面上的点的结构。

:- type point
    --->    point(
                pt_x        :: int,
                pt_y        :: int
            ).

pt_x and pt_y are field names they allow us to retrieve the values of a point's fields. pt_x和pt_y是字段名称,它们允许我们检索点的字段的值。 For example: 例如:

format("The point's X coordinate is: %d\n", [i(Point ^ pt_x)], !IO),
format("The point's Y coordinate is: %d\n", [i(Point ^ pt_y)], !IO),

We can retrieve a value and assign it to a new variable. 我们可以检索一个值并将其分配给一个新变量。

X = Point ^ pt_x,

And we can also update one field without having to write out the whole point again. 我们还可以更新一个字段,而无需再次写出整点。

NewPoint = OldPoint ^ pt_y := NewY,

Where things get a little bit more complicated is when this is used with state variable notation, there's an extra syntax sugar that occurs. 事情变得更复杂的是当它与状态变量表示法一起使用时,会出现额外的语法糖。

move_up(D, !Point) :-
    NewY = !.Point ^ pt_y + D,
    !Point ^ pt_y := NewY.

Note that when we read a value we use !.Point, which is the state variable for "the current value". 请注意,当我们读取一个值时,我们使用!.Point,它是“当前值”的状态变量。 And when we updated it we could have written: 当我们更新它时,我们可以写:

!:Point = !.Point ^ pt_y := NewY.

However this extra syntax sugar allows us to write: 然而,这个额外的语法糖允许我们写:

!Point ^ pt_y := NewY.

I hope this helps. 我希望这有帮助。 There are many more examples throughout the Mercury source code: Mercury源代码中还有更多示例:

https://github.com/Mercury-Language/mercury https://github.com/Mercury-Language/mercury

and ohther Mercury projects, note that the github language tagging is broken, many Objective-C files are detected as Mercury and many Mercury things are detected as other files: 和其他Mercury项目一样,注意github语言标记被破坏,许多Objective-C文件被检测为Mercury,并且许多Mercury被检测为其他文件:

https://github.com/search?utf8=%E2%9C%93&q=language%3AMercury&type=Repositories&ref=advsearch&l=Mercury https://github.com/search?utf8=%E2%9C%93&q=language%3AMercury&type=Repositories&ref=advsearch&l=Mercury

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM