[m-dev.] [refman] Mutable variables example

Julien Fischer juliensf at cs.mu.OZ.AU
Mon May 29 11:55:42 AEST 2006


On Fri, 26 May 2006 doug.auclair at logicaltypes.com wrote:

> Hi!  The refman is exact in describing module mutable variables
> (section 9.6) but does not provide an example.  So, for example,
> I had a misunderstanding with the mutable variable name (I used
> an uppercase first character on the first go).

The reference manual is not exactly exact in that respect.  It should
say that the mutable name has to be a valid Mercury name (as defined in
section 2.2 of the reference manual.)

> I request the documentation be enhanced with an example ...
> something like the following:

I think that example is too large for the reference manual - something
of that size would be okay in the samples directory though.

> :- module mutemebaby.
>
> :- interface.
>
> :- import_module io.
>
> :- pred main(io::di, io::uo) is det.
>
> :- implementation.
> :- import_module list, string.
>
> :- mutable(mutant, int, 0, ground, []).
> :- mutable(io_mutant, int, 0, ground, [attach_to_io_state]).
>
> :- pragma promise_pure(main/2).
>
> main -->
> 	impure main2,
> 	main3.
>
> :- impure pred main2(io, io).
> :- mode main2(di, uo) is det.
>
> main2 -->
> 	{ semipure get_mutant(M0) },
> 	format("Hey, mutant is %d\n", [i(M0)]),
> 	{ impure set_mutant(2),
> 	  semipure get_mutant(M1) },
> 	format("Hey, mutant is now %d\n", [i(M1)]).
>
> :- pred main3(io::di, io::uo) is det.
>
> main3 -->
> 	get_io_mutant(M0),
> 	format("Hey, io_mutant is %d\n", [i(M0)]),
> 	set_io_mutant(2),
> 	get_io_mutant(M1),
> 	format("Hey, io_mutant is now %d\n", [i(M1)]).
>
> :- end_module mutemebaby.
>
> Sorry, I don't have a `diff -u` against the refman, but
> I don't have tools to modify Tex or DVI.

You don't need them - the reference manual is written in texi format.

> What are the real-world cases for mutable variables where their
> initial instantiation would be something other than ground?

As with a lot of this stuff the answer is constraint solvers.  The G12
project has examples where we want to store a value with inst `any' inside
a mutable, in that case the initial instantiation would be `any' rather
than `ground'.

Julien.


--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list