[mercury-users] Exceptions and unique modes

Peter Ross peter.ross at miscrit.be
Wed Feb 21 20:22:36 AEDT 2001


On Fri, Feb 16, 2001 at 02:01:22AM +1100, Fergus Henderson wrote:
> On 15-Feb-2001, Peter Ross <peter.ross at miscrit.be> wrote:
> > I am really starting to think that threaded unique objects, should be
> > treated specially in the language, maybe with some sort of special
> > syntax which allowed you to use one name to talk about a threaded unique
> > object and the compiler was smart enough to do a source-to-source
> > transformation which made this all declarative.
> 
> Well, if using one name to talk about a threaded unique object is what
> you want, I think you may perhaps be able to do it using mutvars
> (see library/store.m).
> 
> It would be nice if we were to provide something similar to that
> but which works on the io__state rather than a separate store type.
> We could improve do that by providing procedures io__new_mutvar,
> io__set_mutvar, and io__get_mutvar to parallel the versions in store.m.
> (Currently the only way to keep the store in the io__state is via
> io__set_globals/io__get_globals, which is a bit of a hack, and not
> maximally convenient.)
> 
While I think this is a nice idea and the equivalent of IORef in
Haskell, it faces the problem that we must have an io__state around to
use it.  This is an unacceptable constraint for me.

Imagine the following situation:

:- pred p(list(resource_requests)::in,
        map(key, resource)::di, map(key, resource)::uo) is det.

p([], Map, Map).
p([R | Rs], Map0, Map) :-
    allocate_resource(R, Resource),
    get_key(Resource, Key),
    map__det_insert(Map0, Key, Resource, Map).

Now I am using the following predicate:

q(ResourceRequests, Answers) :-
    p(ResourceRequests, map__init, Resouces),
    process(Resouces, Answers).

Now if for some reason an exception is thrown in this bit of code, I
must have the latest handle on the map so that I can free all the
resources that I have allocated.  Also if you think about the situation
where the map isn't unique, then you need to protect every update to the
map with an exception handler so that you can free the resources.

This can't be done by either making a copy of the variable or using
mdi/muo modes because we want the latest copy of the map not the one at
the start.  I think that the only way to get the behaviour that I want
is to use a store type, so that I always have a handle on the latest
copy of the variable.

Pete
--------------------------------------------------------------------------
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