[mercury-users] Event handling in mercury

Ralph Becket rbeck at microsoft.com
Tue Jul 17 02:41:37 AEST 2001


> From: Fergus Henderson [mailto:fjh at cs.mu.OZ.AU]
> Sent: 16 July 2001 16:45
> 
> That's a good idea.  It's one that has been on my to-do list for quite
a while,
> but for various reasons I haven't found time for it.
> [...]
> I think it would be enough to just do it like this:
> 
> 	:- module io_store.   % compare module store
> 	:- interface.
> 
> 	:- import_module store. % type store__mutvar(T,S).
> 	:- import_module io.    % type io__state.
> 
> 	:- type io_store.
> 
> 	:- pred io_store__new_mutvar(T,mutvar(T, io_store), io__state,
io__state).
> 	:- mode io_store__new_mutvar(in, out, di, uo) is det.
> 
> 	:- pred io_store__get_mutvar(mutvar(T, io_store), T, io__state,
io__state).
> 	:- mode io_store__get_mutvar(in, out, di, uo) is det.
> 
> 	:- pred io_store__set_mutvar(mutvar(T, io_store), T, io__state,
io__state).
> 	:- mode io_store__set_mutvar(in, in, di, uo) is det.
> 
> There should also be a procedure analagous to
store__new_cyclic_mutvar.

I don't think that's the right interface.  The problem is that
once you've created a new mutvar you have to pass it around
*in addition* to the io__state in order to get at its contents,
which doesn't seem to be in the spirit of global variables.

I'd rather be able to access globals by name (string or int, say)
which I could define by convention in my application.  The interface
would look something like this:

:- type global_name == string.

:- pred io__init_globals(io__state::di, io__state::uo) is det.
:- pred io__get_global(global_name::in, univ::out, io__state::di,
io__state::uo) is det.
:- pred io__set_global(global_name::in, univ::in,  io__state::di,
io__state::uo) is det.

[obvious implementations...]

You'd then set up easy-access preds on a per application basis, e.g.

:- pred my_int(int::out, io__state::di, io__state::uo) is det.

my_int(X) -->
	io__get_global("my_int", U),
	{ det_univ_to_type(U, X) }.

:- pred 'my_int :='(io__state::di, int::in, io__state::uo) is det.

'my_int :='(IO0, X, IO) :-
	io__set_global("my_int", univ(X), IO0, IO).

[I think that's the right argument order for DCG field access
preds.]

I doubt the cost of casting to/from univ would be too great, whereas
having to pass around an extra argument containing my set of mutvars
would probably be inconvenient.

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



More information about the users mailing list