[m-users.] Missing mode for list.head?
Julien Fischer
jfischer at opturion.com
Thu Mar 14 20:43:42 AEDT 2019
On Thu, 14 Mar 2019, Volker Wysk wrote:
> The list.head function can fail, because of the way it is implemented, even
> when it is known at compile time that the argument list is not empty:
>
> :- func head(list(T)) = T is semidet.
> head([X | _]) = X.
>
> There is no mode declaration for head in list.m.
In the absence of such a declaration it just takes the default
function mode, i.e. head(in) = out.
> But it could be implemented like this:
>
> :- func head1(list(T)) = T.
> :- mode head1(in) = out is semidet.
> :- mode head1(in(non_empty_list)) = out is det.
> head1([A|_]) = A.
>
> This way you can have a deterministic version of head, when it is known that
> it input list isn't empty.
>
> Am I right?
Yes, that's correct.
The standard library doesn't make much use of inst subtyping or other
more advanced features of the mode system. This is in part due to
limitations in the mode checker and in part, simply for historical
reasons.
Julien.
More information about the users
mailing list