If I type in SWI Prolog a "nth0" query, the result is:
N = 0,
X = [a|_G282] ;
N = 1,
X = [_G281, a|_G285] ;
N = 2,
X = [_G281, _G284, a|_G288] ;
however, the SWI manual says:
type_error(integer, Index) if Index is not an integer or unbound.
so, if my understanding of this text is correct (?), it seems that an error should be triggered instead of the previous results.
In addition, I wonder which one of the behaviours is the correct one taken into account the ISO standard.
(I known stack-overflow doesn't allows questions for references, so, I will not ask for a link to standard, but I hope do not break the rules if I ask: is ISO standard publicly available? if not, there are any equivalent RFC? ).
Best How To :
nth0/3 is not part of ISO Prolog, so there is no explicit reference. However, the way how and when errors are reported is defined in the standard. In particular, type errors are never reported due to an argument being not sufficiently instantiated. The concrete formulation in the SWI manual is a bit unfortunate. It should rather read for
nth0(N, Xs, E):
N is neither a variable nor an integer
Would now there be a restriction on
N being instantiated (it's not in this case, but let's assume it), then there would be an error condition:
N is a variable
nth0/3 used to be part of the DECsystem 10 library
listut (also written
ListUt) since 1983. Originally, the definition was only intended to be used with
N being an integer. However, errors, as we have them now, did not yet exist and the system simply (and incorrectly) failed with an uninstantiated variable.
It was later adopted (and corrected) by Quintus Prolog in about 1984.
Now, we have an elaborate error classification that is able to capture the fine semantic differences between the various error situations.
More how predicates are defined in the standard.