[mercury-users] arrays and unique modes (was: field updates)

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Oct 24 10:03:41 AEDT 2000


On 23-Oct-2000, david wallin <david at wallin.cx> wrote:
> 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 ?

Right.

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

Yes.

If you add

	:- inst uniq_game_state == unique(game_state(
					ground,
					uniq_array(ground),
					ground
			)).

and then change to mode for player_bet_on_number/4 from

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

to

	:- mode player_bet_on_number(in, in,
		di(uniq_game_state), out(uniq_game_state)) is det.

that should solve the problem.

Beware however that there are some limitations in the current support
for unique modes.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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