[mercury-users] determinism suprises

William Lee Irwin III wli at holomorphy.com
Fri Dec 8 17:22:08 AEDT 2000

I was recently surprised to find a couple of "catches" with respect
to determinism.

First, I had a predicate on an algebraic data type with a situation
like this:

:- interface.
:- type algtype ---> left ; right.
:- pred some_pred(algtype).
:- mode some_pred(in) is det.
:- mode some_pred(out) is multi.
:- implementation.

This led to what I found to be a surprising instantiation error.
algtype.m:008: In clause for `some_pred(out)':
algtype.m:008:   mode error: argument 1 did not get sufficiently instantiated.
algtype.m:008:   Final instantiatedness of `HeadVar__1' was `free',
algtype.m:008:   expected final instantiatedness was `ground'.
The workaround I ended up using was:
some_pred(left). some_pred(right).
... which gets hairy for types with more alternatives and explodes
combinatorially with various ways of combining types. One could separate
the modes into distinct predicates, which still leaves me uneasy.

Another "catch" I found was the following:

:- pred color(squareindex, color).
:- mode color(in, out) is det.
:- mode color(in, in) is semidet.
:- mode color(out, in) is multi.
color({File, Rank}, Color) :-
   file_parity(File, FileParity),
   rank_parity(Rank, RankParity),
   add_parity(FileParity, RankParity, Residue),
   parity_to_color(Residue, Color).

Now, file_parity, rank_parity, and parity_to_color have all the
necessary modes and determinisms to allow all of color's modes and
determinisms given the appropriate ordering of conjunctions, but I
still found that the reordering was not done for each mode:

board.m:018: In `color(out, in)':
board.m:018:   error: determinism declaration not satisfied.
board.m:018:   Declared `multi', inferred `nondet'.
board.m:167:   call to `parity_to_color(in, in)' can fail.

So I tried to reorder the conjunction as follows:

color({File, Rank}, Color) :-
    parity_to_color(Residue, Color),
    add_parity(FileParity, RankParity, Residue),
    file_parity(File, FileParity),
    rank_parity(Rank, RankParity).

but this runs into the following error:
board.m:016: In `color(in, out)':
board.m:016:   error: determinism declaration not satisfied.
board.m:016:   Declared `det', inferred `nondet'.
board.m:164:   call to `parity_to_color(out, out)' can succeed more than once.
board.m:165:   call to `add_parity(out, out, in)' can succeed more than once.
board.m:166:   call to `file_parity(in, in)' can fail.
board.m:167:   call to `rank_parity(in, in)' can fail.

The only workarounds I've found for this thus far are to lay out the
tables in their entirety or to split up the predicate, but this is,
frankly, quite verbose. It looks like only one ordering of the
conjunction is being used for all modes, although my expectation was
that the code would be reordered for each mode to satisfy its
determinism. Is this a reasonable expectation?

P.S. I can supply the code in more detail offlist if someone is
	interested in examining it more closely; it's a bit large to post
	in full here.
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