[mercury-users] field updates

Tyson Dowd trd at cs.mu.OZ.AU
Mon Oct 23 12:46:45 AEDT 2000


On 23-Oct-2000, david wallin <david at wallin.cx> wrote:
> Hi all,
> 
> I've, without much luck, tried to figure out how field updates work. 
> Could someone direct me to some working examples (i.e. source code) 
> on how to do this ?
> 
> 
> I am currently running the latest stable release (2000-10-08).
> 


	NewStructure = OldStructure ^ fieldname := NewFieldValue


This is from the Mercury ICFP programming competition (a ray tracer),
I've simplified it a little.

:- type render_params
        ---> render_params(
                amb :: color,           % the ambient light
                lights :: array,        % array(light)
                scene :: scene,         % the scene to render
                depth :: int,
                fov :: real,            % the field of view
                wid :: int,             % the width, in pixels
                ht :: int,              % the height, in pixels
                file :: string
        ).

So some code to use and set these fields would be:

        Depth = RenderParams ^ depth,
        ( Depth > 0 ->
                NewRenderParams = RenderParams ^ depth := Depth - 1,

		< some code to fire a new ray using NewRenderParams >
        ;
		% Return a black point
                ReflectedI = point(0.0, 0.0, 0.0)
        ),

This code checks the depth field of RenderParams.  If it is
greater than zero, we make a new copy of RenderParams (called
NewRenderParams) with Depth - 1 as the depth value.
(This code is used for doing reflections with a depth limit).


If you want a full working example, try this:

:- module hello.
:- interface.
:- import_module io.

:- pred main(io__state::di, io__state::uo) is det.

:- implementation.

:- type message ---> message(
                        field1 :: string,
                        field2 :: string
                ).

main -->
        { Message0 = message("", "") },
        { Message1 = Message0 ^ field1 := "Hello" },
        { Message2 = Message1 ^ field2 := "world" },
        io__write_string(Message2 ^ field1),
        io__write_string(", "),
        io__write_string(Message2 ^ field2),
        io__write_string("\n").





-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't everyone's cup of fur.
     trd at cs.mu.oz.au        # 
http://www.cs.mu.oz.au/~trd #
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list