[m-rev.] Add `:- mutable' declaration to the language.

Ralph Becket rafe at cs.mu.OZ.AU
Thu Sep 1 17:05:46 AEST 2005


Julien Fischer, Thursday,  1 September 2005:
> 
> On Thu, 1 Sep 2005, Ralph Becket wrote:
> 
> > Estimated hours taken: 24
> > Branches: main
> >
> > Add `:- mutable' directives to the language, providing modules with private
> > mutable variables.  A directives
> >
> > :- mutable(x, int, 0, ground, [thread_safe]).
> >
> > leads to the compiler generating the following:
> >
> > :- semipure pred get_x(int::out(ground)) is det.
> > :- impure   pred set_x(int::in(ground)) is det.
> > :-          pred initialise_mutable_x(io::di, io::uo) is det.
> > :- initialise initialise_mutable_x/2.
> >
> > initialise_mutable_x(!IO) :-
> > 	promise_pure(
> > 		impure set_x(0)
> > 	).
> >
> > :- pragma foreign_decl("C", "MR_Word mutable_variable_x;").
> >
> > :- pragma foreign_proc("C", get_x(X::out(ground)), [thread_safe],
> > "MR_trail_current_value(&mutable_variable_x); X = x;").
> >
> > :- pragma foreign_proc("C", set_x(X::in(ground)), [thread_safe],
> > "x = X;").
> >
> > Possible attributes for a mutable variable are `thread_safe' and
> > `untrailed'.
> >
> My preferred option here would be that it mutables be untrailed
> by default.

Hmm.  I see what you're saying.  But you're wrong.


> > @@ -1796,6 +1800,162 @@
> > +
> > +% Mutable declaration yntax:
> > +%
> s/yntax/syntax/

Fixed.

> > +% :- mutable(name, type, value, inst, [untrailed, promise_thread_safe]).
> > +% (The list of attributes at the end is optional.)
> > +%
> > +% E.g.:
> > +% :- mutable(counter, int, 0, ground, [promise_thread_safe]).
> > +%
> > +% This is eventually converted into the following:
> > +%
> Delete "eventually".

Done.

> > +% :- semipure pred get_counter(int::out(ground)) is det.
> > +% :- pragma foreign_proc("C",
> > +% 	get_counter(X::out(ground)),
> > +% 	[promise_semipure, will_not_call_mercury, thread_safe],
> > +% 	"X = mutable_counter;").
> > +%
> > +% :- impure pred set_counter(int::in(ground)) is det.
> > +% :- pragma foreign_proc("C",
> > +% 	set_counter(X::in(ground)),
> > +% 	[will_not_call_mercury, thread_safe],
> > +% 	"MR_trail_current_value(&mutable_counter);
> > +% 	 mutable_counter = X;").
> > +%
> > +% :- pragma foreign_decl("C", "MR_Word mutable_counter;").
> > +%
> > +% :- import_module io.
> > +% :- initialise initialise_counter.
> > +% :- impure pred initialise_mutable_counter(io::di, io::uo) is det.
> > +% initialise_mutable_counter(!IO) :-
> > +% 	impure set_counter(0).
> 
> Perhaps we should just support impure initialisation predicates.
> (Is the io module import implicity or do we rely on the user for it.)

Let me check...

> > +% The `thread_safe' attributes are omitted if it is not listed in
> > +% the mutable declaration attributes.  Similarly, MR_trail_current_value()
> > +% does not appear if `promise_thread_safe' appears in the mutable
> > +% declaration attributes.
> > +
> Shouldn't that be "if `untrailed' appears in the mutable declaration..."

Fixed.

> > Index: doc/reference_manual.texi
> > ===================================================================
> > RCS file: /home/mercury1/repository/mercury/doc/reference_manual.texi,v
> > retrieving revision 1.322
> > diff -u -r1.322 reference_manual.texi
> > --- doc/reference_manual.texi	29 Aug 2005 03:22:28 -0000	1.322
> > +++ doc/reference_manual.texi	1 Sep 2005 03:48:07 -0000
> > @@ -558,6 +558,7 @@
> >  :- pragma
> >  :- promise
> >  :- initialise
> > +:- mutable
> >  :- module
> >  :- interface
> >  :- implementation
> > @@ -4237,6 +4238,7 @@
> >  * An example module::
> >  * Sub-modules::
> >  * Optional module initialisation::
> > +* Module-local mutable variables::
> >  @end menu
> >
> >  @node The module system
> > @@ -4582,6 +4584,44 @@
> >  order in which they are specified, although no order may be assumed between
> >  different modules or submodules.
> >
> > + at node Module-local mutable variables
> > + at section Module-local mutable variables
> > +
> > +Certain special cases require a module to have one or more mutable (i.e.
> > +destructively updatable) variables, for example to hold the constraint
> > +store for a solver type.
> > +
> > +A mutable variable can be declared in the implementation section of
> > +a module using the @samp{mutable} directive:
> > +
> 
> You should mention that it is an error for a mutuable variable to
> appear in the interface of a module.

I've added the following comment:

	Note that it is an error for a @samp{mutable} directive to
	appear in the interface section of a module.

> > +C code in the same module can access the mutable variable using the name
> > + at samp{mutable_variable_varname}, which is a global variable with type
> > + at samp{MR_Word}.
> s/with type/of type/

Fixed.

-- Ralph
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list