[m-dev.] for review: counter.m

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Mar 10 17:03:20 AEDT 2000


On 10-Mar-2000, Mark Anthony BROWN <dougl at cs.mu.OZ.AU> wrote:
> Zoltan Somogyi writes:
> > +
> > +:- type counter.
> > +
> > +	% counter_init(N, Counter) returns a counter whose first allocation
> > +	% will be the integer N.
> > +:- pred counter__init(int::in, counter::out) is det.
> > +
> > +	% counter__allocate(N, Counter0, Counter) takes a counter, and
> > +	% returns (a) the next integer to be allocated from that counter,
> > +	% and (b) the updated state of the counter.
> > +:- pred counter__allocate(int::out, counter::in, counter::out) is det.
> > +
> 
> Is it worth changing the type to
> 
> 	:- type counter(T).
> 
> and using the type parameter for some extra type safety, similarly
> to what is done in varset.m?  This would allow users to ensure that
> a counter initialized for one purpose is not accidentally used
> for another purpose.  The above counter could be defined as
> 
> 	:- type counter == counter(generic).
> 
> for some suitable type `generic'.

I don't think that would actually buy you that much,
because counter__allocate would still return an int,
and so although the added type safety would prevent
you from passing one type of counter where another
type of counter was expected, it wouldn't prevent
you from using the wrong kind of counter to allocate
a particular value.

It does buy you a little, but I think it is probably
not worth the additional complexity.

To get a greater degree of type safety, you'd need
to change counter__allocate so that the first output
argument was some polymorphic abstract type called say
count(T) instead of int.  But if that is what you want,
then you can already get it: just use term__var_supply(T)
and term__var(T) rather than counter(T) and count(T).

(There is perhaps some argument for moving the term__var_supply
and term__var types out of term.m into a new module,
and renaming them as `name_supply' and `name'.)

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list