[m-users.] Mode selection

Paul Bone paul at bone.id.au
Thu Oct 16 11:57:38 AEDT 2014


This question came up on IRC today.  Consider append/3:

    :- pred append(list(T), list(T), list(T)).
    :- mode append(in, in, out) is det.
    :- mode append(out, out, in) is multi.

And a call to append with each of it's arguments fully instantiated.

    % I've written these out so that I can name them and refer to them
    % below.
    A = [1, 2],
    B = [3],
    C = [1, 2, 3],
    append(A, B, C).

This is a case where "append(in, in, in) is semidet" is an implied mode.
While it makes no (declarative semantics) difference, which mode of append
will be used here?  Does the compiler select the first mode that fits or the
"best" mode?

A follow up question.  What if the implied mode is written down in the
declaration.

    :- pred append(list(T), list(T), list(T)).
    :- mode append(in, in, out) is det.
    :- mode append(in, in, in) is semidet.
    :- mode append(out, out, in) is multi.

I'm pretty sure that in this case the in, in, in, mode will be used; so that
the most efficient execution can be used as the values for each of the
arguments can be used to fail earlier.  However I'm not certain.  Is this
true?

Thanks.


-- 
Paul Bone



More information about the users mailing list