Need an example of Record Syntax in mercury -
i new mercury , trying wrap head around record syntax, reference manual place have encountered , leaves me mystified:
term ^ field1(arg1) ^ field2(arg2, arg3) equivalent field2(arg2, arg3, field1(arg1, term)).
could please real-world example?
record syntax syntax sugar, , manual trying explain how transformation record syntax mercury's normal syntax works. fine if you're trying find out how implement record syntax, not helpful if want learn how use it.
i recommend ignoring (arg1, ...) stuff in parenthesis - i'm not sure if it's part of syntax , i've never seen use it.
lets create structure representing points on cartesian plane.
:- type point ---> point( pt_x :: int, pt_y :: int ).
pt_x , pt_y field names allow retrieve values of point's fields. 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 value , assign new variable.
x = point ^ pt_x,
and can update 1 field without having write out whole point again.
newpoint = oldpoint ^ pt_y := newy,
where things little bit more complicated when used state variable notation, there's syntax sugar occurs.
move_up(d, !point) :- newy = !.point ^ pt_y + d, !point ^ pt_y := newy.
note when read value use !.point, state variable "the current value". , when updated have written:
!:point = !.point ^ pt_y := newy.
however syntax sugar allows write:
!point ^ pt_y := newy.
i hope helps. there many more examples throughout mercury source code:
https://github.com/mercury-language/mercury
and ohther mercury projects, note github language tagging broken, many objective-c files detected mercury , many mercury things detected other files:
Comments
Post a Comment