Files
mercury/tests/hard_coded/mutable_init_impure.m
Peter Wang c8d8202224 Allow mutable variables to be initialised by impure functions.
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.
2011-11-03 01:01:36 +00:00

47 lines
1.0 KiB
Mathematica

% 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).
:- mutable(foo, string, init_foo, ground, [untrailed, constant]).
:- semipure func init_foo = string.
init_foo = X :-
promise_semipure X = "Testing...".
main(!IO) :-
get_sem1(Sem1, !IO),
semaphore.wait(Sem1, !IO),
get_sem2(Sem2),
semaphore.wait(Sem2, !IO),
get_foo(Foo),
io.write_string(Foo, !IO),
io.nl(!IO),
io.write_string("Success.\n", !IO).
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sts=4 sw=4 et