[mercury-users] a typeclassful random.m

doug.auclair at logicaltypes.com doug.auclair at logicaltypes.com
Sat Feb 24 16:57:48 AEDT 2007


Dear Julian, you and I wrote:

> On Wed, 21 Feb 2007 doug.auclair at logicaltypes.com wrote: 
> > Put another way: perhaps the protocol can be modified to 
> > provide the user a simpler typeclass specification of only one 
> > argument for the majority (in my case, for all) of uses? 
>I haven't found a way.

How about a typeclass constraint on the typeclass:

:- typeclass rng(T) where [
     pred next(int::out, T::in, T::out) is det,
     pred max(int::out, T::in, T::out) is det
].

 % note the new rng(T) typeclass constraint
:- typeclass prng(T, S) <= ((T -> S), (S -> T), rng(T)) where [ 
     pred seed(S::in, T::out) is det
].

The instance declaration then becomes:

:- instance prng(tausworthe3, tausworthe3_seed) where [
     (seed(tausworthe3_seed(A, B, C), RNG) :- seed_tausworthe3(A, B, C) = RNG)
].

:- instance rng(tausworthe3) where [
     pred(next/3) is rand_tausworthe3,
     (max(N, !RNG) :- N = int.max_int)  % is it?
].

And the preds have the "superclass" constraint of:

:- pred random_int(int::out, RNG::in, RNG::out) is det <= rng(RNG).
[... etc ... ]

So creating an pseudo RNG stays the same:

some_jrnd(Taus, !IO) :-
	some [!DRNG] (trandom1.open(device_seed("/dev/urandom"), !:DRNG, !IO),
	              random_int(A, !DRNG, !IO),
		      random_int(B, !DRNG, !IO),
		      random_int(C, !DRNG, !IO),
		      trandom1.close(!.DRNG, !IO),
                      trandom1.seed(tausworthe3_seed(A, B, C), Taus)).

but using the RNG allows the pred declarations to be simpler (and avoid
having the don't care secondary constraint), e.g.:

:- pred new_random_relay(int, relay, RNG, RNG) <= rng(RNG).
:- mode new_random_relay(in, out, in, out) is det.

-----

This gives the functional dependency between the seed and the RNG
as you've put forth and gives me the RNG constraints without the
confusing don't care arguments.  Does this work for you?

Sincerely,
Doug Auclair

--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list