[m-users.] Announcement: mercury-rmath library

Julien Fischer jfischer at opturion.com
Mon Aug 23 16:53:18 AEST 2021


Hi Mark,

On Mon, 23 Aug 2021, Mark Clements (gmail) wrote:

> The random number functions are marked as being impure. Note that the random 
> seed is managed opaquely from C. I have tried -- unsuccessfully -- to wrap 
> these functions using IO state:
>
> :- pred wrapped_runif(float::in, float::in, float::out, io::di, io::uo).
> wrapped_runif(Lower, Upper, U, !IO) :- U = runif(Lower,Upper).
>
> Do I also need to make a promise?

Yes, either a promise or a promise_pure scope.  You also need to add the
impure annotation to the call to runif/2.  So, something like:

     :- pred wrapped_runif(float::in, float::in, float::out, io::di, io::uo).
     wrapped_runif(Lower, Upper, U, !IO) :-
       promise_pure (
           impure U = runif(Lower,Upper),
           !:IO = !.IO
       ).

(The unification involving the I/O state is there to document why the
purity promise is correct.)

Julien.


More information about the users mailing list