FW: [mercury-users] Records

Peter Schachte schachte at cs.mu.OZ.AU
Tue Nov 9 18:24:35 AEDT 1999


On Tue, Nov 09, 1999 at 11:18:19AM +1100, Simon Taylor wrote:
> That wouldn't always work. In some cases multiple field updates must
> be done as an atomic operation for type correctness.
> 
> :- type foo(T)
> 	---> foo(field1::T, field2::T, int, int).
> 
> :- func update_foo(foo(T)) = foo(int).
> 
> update_foo(In) = In ^ field1 := 1
>                     ^ field2 := 2. 

Bummer!

If you unfold what's done here and eliminate the redundant (because
it's unused) construction, it'd be fine.  And you'd really need to do
that unfolding to get reasonable efficiency.  But I understand we
don't want type checking to require unfolding, so that approach can't
handle these sorts of updates.

Ok, then, this ought to work, though it's not as pretty.

    :- func fields1and2(foo(T)) = pair(T).
    :- func 'fields1and2:='(foo(T1), pair(T2)) = foo(T2).

    fields1and2(foo(A,B,_,_)) = A-B.
    'fields1and2:='(foo(_,_,C,D),A-B) = foo(A,B,C,D).

    :- func update_foo(foo(T)) = foo(int).

    update_foo(In) = In ^ fields1and2 := 1-2.	% I hope the overloaded 
						% minus doesn't mess up


-- 
Peter Schachte                     In war, the first casualty is truth.
mailto:schachte at cs.mu.OZ.AU            -- Aeschylus, ca. 475 BC 
http://www.cs.mu.oz.au/~schachte/  
PGP: finger schachte at 128.250.37.3  
--------------------------------------------------------------------------
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