[m-rev.] for review: allow mutable init by impure functions

Julien Fischer juliensf at csse.unimelb.edu.au
Wed Nov 2 23:04:21 AEDT 2011


On Wed, 26 Oct 2011, Peter Wang wrote:

> Branches: main, 11.07
>
> Allow mutable variables to be initialised by impure functions.
>
> Also fix bug #223.  Make thread.semaphore.init/1 and thread.mvar.init/1
> impure, as they should be.  They were introduced to be used as mutable
> initialisers, which led to the oversight of making them pure.
>
> compiler/make_hlds_passes.m:
> compiler/prog_mutable.m:
> 	Modify the generated mutable initialisation predicates such that the
> 	initial value may be the return value of a impure function call.
>
> compiler/purity.m:
> 	Ignore warnings about unnecessary impure annotations on goals in
> 	generated mutable predicates.  These would now appear when
> 	a mutable is initialised by a call to a pure function, or
> 	by a constant.
>
> doc/reference_manual.texi:
> NEWS:
> 	Document the language change.
>
> library/thread.mvar.m:
> library/thread.semaphore.m:
> 	Make thread.semaphore.init/1 and thread.mvar.init/1 impure.
>
> tests/hard_coded/Mmakefile:
> tests/hard_coded/mutable_init_impure.exp:
> tests/hard_coded/mutable_init_impure.m:
> 	Add test case.
>
> diff --git a/NEWS b/NEWS
> index 4714e1f..c68031e 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -13,6 +13,8 @@ Changes to the Mercury language:
>   (or XXXXXXXX) is a Unicode character code in hexadecimal, is replaced with
>   the corresponding Unicode character.
>
> +* Mutables may now be initialised by impure functions.

I suggest:

    * Expressions used to initialise mutables may now contain impure
      or semipure function calls.

...

> diff --git a/doc/reference_manual.texi b/doc/reference_manual.texi
> index 04938ab..c917eba 100644
> --- a/doc/reference_manual.texi
> +++ b/doc/reference_manual.texi
> @@ -4795,7 +4795,7 @@ are equivalent to, the builtin insts @samp{free}, @samp{unique},
>
> The initial value of a mutable, @samp{initial_value}, may be any Mercury
> expression with type @samp{vartype} and inst @samp{varinst} subject to
> -the above restrictions.
> +the above restrictions. It may be impure.

    ... or semipure.

(I think it's worth explicitly mentioning the latter.)

...

> diff --git a/tests/hard_coded/mutable_init_impure.exp b/tests/hard_coded/mutable_init_impure.exp
> new file mode 100644
> index 0000000..a9d787c
> --- /dev/null
> +++ b/tests/hard_coded/mutable_init_impure.exp
> @@ -0,0 +1 @@
> +Success.
> diff --git a/tests/hard_coded/mutable_init_impure.m b/tests/hard_coded/mutable_init_impure.m
> new file mode 100644
> index 0000000..8ed82b8
> --- /dev/null
> +++ b/tests/hard_coded/mutable_init_impure.m
> @@ -0,0 +1,35 @@
> +% Test initialisation of mutables by impure functions.
> +
> +:- module mutable_init_impure.
> +:- interface.
> +
> +:- import_module io.
> +
> +:- pred main(io::di, io::uo) is det.
> +
> +%-----------------------------------------------------------------------------%
> +
> +:- implementation.
> +
> +:- import_module thread.
> +:- import_module thread.semaphore.
> +
> +:- mutable(sem1, semaphore, init_sem, ground, [untrailed, attach_to_io_state]).
> +:- mutable(sem2, semaphore, init_sem, ground, [untrailed, constant]).
> +
> +:- impure func init_sem = semaphore.
> +
> +init_sem = Sem :-
> +    impure Sem = semaphore.init(1).
> +
> +main(!IO) :-
> +    get_sem1(Sem1, !IO),
> +    semaphore.wait(Sem1, !IO),
> +
> +    get_sem2(Sem2),
> +    semaphore.wait(Sem2, !IO),
> +
> +    io.write_string("Success.\n", !IO).
> +

You should add a test for the semipure case as well.

That looks fine otherwise.

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



More information about the reviews mailing list