[mercury-users] Questions on the counter class

Zoltan Somogyi zs at cs.mu.OZ.AU
Fri Apr 20 13:40:20 AEST 2001


On 19-Apr-2001, Terrence Brannon <princepawn at earthlink.net> wrote:
> 1 - When the docs say that counter__allocate returns something,
> this isn't true. This is a predicate. It doesn't return
> anything. What it does do is bind two out parameters.

Binding output arguments is often called "returning" them.

> 2 - Looking at the implementation for counter__allocate, it appears that
> int::out is bound to N, not N+1, thus I don't see how the docs could
> be right on this point when they say "(a) the next integer to be allocated
> from that
> counter," --- N is the _current_ integer not the next one.
> ...
> 3 - I would like to see a sample usage of this counter. What confuses
> me is how to extract the current value of the counter. How would I do
> the following:

You are making a distinction between the "current" value of the counter and
its "next" value, but no such disntinction actually exists. The counter is
an abstract data type; its user cannot see inside it. The user cannot see
what the state of the counter is, except by asking for the next counter value,
which changes the state.

The example you asked for follows.

rand_print ->>
	{ rand_num(R) },
	{ counter__init(0, Counter) },
	rand_print_2(R, Counter).

rand_print_2(R, Counter0) -->
	{ counter__allocate(Value, Counter0, Counter1) },
	( { R = Value } ->
		[]	% terminate the loop
	; { R > Value } -> 
		io__write_nl(Value),
		rand_print_2(R, Counter1)
	;
		{ error("the counter is larger than the random number")}
	).

However, in this case using explicit arithmetic would be better. The counter
module is intended for other uses.

Zoltan Somogyi <zs at cs.mu.OZ.AU> http://www.cs.mu.oz.au/~zs/
Department of Computer Science and Software Engineering, Univ. of Melbourne
--------------------------------------------------------------------------
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