[mercury-users] determinism declaration not satisfied
Peter Ross
peter.ross at miscrit.be
Thu Jul 12 18:51:51 AEST 2001
OK main can have a determinism of det or cc_multi, which means that it will
always return one answer.
How many answers does a nondet piece of code have?
The answer is zero or more.
Therefore if you have some nondet code in a single solution context, you
must restrict yourself to only choosing one of the possible answers from the
nondet code and handle the case where there is no answer. So the question
is how do we do this?
The answer is to use an if-then-else
if border("sussex", L) then
io__write_string(L)
else
io__write_string("Nothing borders sussex")
The condition of the if can be nondet or semidet code. If the condition is
nondet and produces and output binding (ie binds L to some string) then the
if-the-else becomes multi (as it will be produce one or more answers (one
for each of the times the nondet code can be true plus one for the else
case).
Ok we know have managed always produce one solution. Now we need to
restrict ourselves to one solution. This can be done by using the
determinism cc_multi (commited choice multi). In this model the Mercury
compiler will after finding one answer that makes the condition true will
commit itself to that answer for the rest of the computation. ie it will
never backtrack and ask for another one. Since cc_multi will commit to one
answer only, it will only return one answer and we have achieved the goal we
wished to achieve which is to make main return only one answer.
Hence main must become cc_multi.
:- pred main(io__state::di, io__state::uo) is cc_multi.
Hope this helps.
Pete
Terence wrote:
>
> :- module border.
> :- interface.
> :- import_module io.
>
> :- pred main(io__state::di, io__state::uo) is cc_multi.
>
> :- implementation.
> :- import_module std_util.
>
> main -->
> { border("sussex",L) }, io__write_string(L).
>
> :- pred border(string, string).
> :- mode border(in, out) is nondet.
> :- mode border(out, in) is nondet.
>
> border("sussex","kent").
> border("sussex","surrey").
> border("surrey","kent").
> border("hampshire","sussex").
> border("hampshire","surrey").
> border("hampshire","berkshire").
> border("berkshire","surrey").
> border("wiltshire","hampshire").
> border("wiltshire","berkshire").
>
> :- pred adjacent(string, string).
> :- mode adjacent(in, out) is nondet.
>
> adjacent(X,Y) :- border(X,Y).
> adjacent(X,Y) :- border(Y,X).
>
> :- pred affordable(string, string).
> :- mode affordable(in, out) is nondet.
>
> affordable(X,Y) :- adjacent(X,Z), adjacent(Z,Y).
>
> --------------------------------------------------------------------------
> 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
> --------------------------------------------------------------------------
>
--------------------------------------------------------------------------
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