[m-users.] Mode selection

Mark Brown mark at mercurylang.org
Thu Oct 16 23:02:36 AEDT 2014


On Thu, Oct 16, 2014 at 11:57 AM, Paul Bone <paul at bone.id.au> wrote:
>
> 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?

Bearing in mind that if this mode is used it means that the call to
append has been reordered *after* the goal that produced its third
argument, then you can't in general say that it will fail earlier or
that it is the most efficient choice. Maybe the other goal takes
longer to produce its output than append does, or allocates more
memory?

Cheers,
Mark.



More information about the users mailing list