[mercury-users] Promise on a typeclass method?

Ian MacLarty maclarty at cs.mu.OZ.AU
Wed Jun 7 11:24:37 AEST 2006


On Tue, Jun 06, 2006 at 10:30:09AM -0500, doug.auclair at logicaltypes.com wrote:
> Dear all,
> 
> Is there a way to enforce that an implementation of a
> typeclassed predicate will always return a (e.g.)
> particular range?
> 
> Specifically, in qcheck2 (see http://www.logicaltypes.com),
> the RNG typeclass is the following:
> 
> :- typeclass random_number_generator(RNG) where [
>         % gives a random float in the range [0, 1)
>         pred rnd(float::out, RNG::in, RNG::out) is det,
>         pred reseed(int::in, RNG::unused, RNG::out) is det
> ].
> 
> How can demand that an implementer of rnd/3 will only return
> floats between 0.0 and 1.0?  In aspect-oriented programming
> (see http://eclipse.org/aspectj for aspects and see 
> http://www.cotilliongroup.com/man/aspects/aspects-man.html
> for aspects in Prolog), one would wrap rnd/3 in a production
> aspect so that every time rnd/3 is called the aspect would
> ensure the value returned is within the range.  How does one
> do this with Mercury constructs?

Can't you just wrap rnd/3 in another predicate that checks that the
output is always between 0 and 1?

Somthing like:

:- pred checking_rnd(float::out, RNG::in, RNG::out) is det
	<= random_number_generator(RNG).

checking_rnd(F, !RNG) :-
	rnd(F, !RNG),
	(
		( F < 0
		; F > 1
		)
	->
		throw an exception
	;
		true
	).

And then call checking_rnd in code that uses the
random_number_generator?

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