[mercury-users] field updates

david wallin david at wallin.cx
Tue Oct 24 06:41:15 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
>

Thanks everyone,

I was obviously still in Java-land because I tried

Structure ^ fieldname := NewValue


However, I managed to get stuck again. I have defined a type with a 
field of type array, something like:


:- type game_state ---> game_state(
				bank    :: int,
				players :: array(player_state),
				rounds  :: int
			).

:- pred player_bet_on_number(int, int, game_state, game_state).
:- mode player_bet_on_number(in, in, in, out) is det.


player_bet_on_number(Player, Number, GameI, GameO) :-
	GameI ^ players = P,
	array__size(P) = Size,
	(
	  Size > Player ->
	  array__lookup(P, Player, PlayerState),
	  PlayerState2 = PlayerState ^ number := Number,
	  array__set(P, Player, PlayerState2, P2),
	  GameO = GameI ^ players := P2
	;
	  GameI = GameO
	).

This is what the compiler has to say about this :

game.m:112: In clause for `player_bet_on_number(in, in, in, out)':
game.m:112:   in argument 1 of call to predicate `array:set/4':
game.m:112:   mode error: variable `P' has instantiatedness `ground',
game.m:112:   expected instantiatedness was `(array:uniq_array)'.


I guess I could switch from array__set/4 to array__slow_set/4 and the 
problems would go away, right ?

But is there another (better) way to do this ? (I fear this means 
delving into modes and instantiations, though)


--david.


--------------------------------------------------------------------------
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