[mercury-users] unsorted_solutions_foldl... ?
Fergus Henderson
fjh at cs.mu.OZ.AU
Wed Oct 16 23:04:21 AEST 2002
On 16-Oct-2002, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> I wish to collect all outputs of a nondeterministic predicate
> (solutions/2), but I need the predicate to accumulate a value during the
> process. The goal is similar to unsorted_aggregate/4, but Generator and
> Accumulator should be mixed together.
> I need:
> :- pred unsorted_solutions_foldl(pred(T, Aku, Aku), list(T), Aku, Aku).
> :- mode unsorted_solutions_foldl(pred(out, di, uo) is nondet, out, di, uo)
> is cc_multi. % or better det, which is not possible by nature.
This signature doesn't make much sense. `pred(out, di, uo) is nondet'
won't work, because if the procedure is really nondeterministic, then
the output will almost never be unique -- instead it will be "mostly unique",
i.e. mode `muo' rather than `uo'. (See the "backtrackable destructive
update" section of the Mercury language reference manual.)
I think you need to step back a bit and explain what higher-level
problem you are trying to solve, and why you think you need a
predicate with mode `pred(out, di, uo) is nondet'.
> I thought about using unsafe__unsafe_perform_io and io__set_globals,
> io__get_globals this way: (I don't bother with type_to_univ conversion in
> this example.)
>
> unsorted_solutions_foldl(MyPred, Outputs, InAku, OutAku) :-
> impure unsafe_perform_io(set_globals(InAku)),
> solutions(
> (pred(Out::out) is nondet:-
> impure unsafe_perform_io(get_globals(IAku)),
> MyPred(Out, IAku, OAku),
> impure unsafe_perform_io(set_globals(OAku))
> ), Outputs),
> impure unsafe_perform_io(get_globals(OutAku)).
>
> This approach fails on unsafe_perform_io: It's not possible to *get* any
> value out of it.
Well, it's fairly easy to add another variant of unsafe_perform_io that
returns an output value. But this solution is not going to be
thread-safe, or re-entrant; you'll have problems if `MyPred' ends up
calling unsorted_solutions_foldl.
A better way to implement this kind of thing would be to use the nb_reference
type defined in extras/references.
But I think there will almost certainly be a better solution if we step
back a bit and examine more carefully the assumptions that lead to this
design.
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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