2

If I type in SWI Prolog a "nth0" query, the result is:

?- nth0(N,X,a).
N = 0,
X = [a|_G282] ;
N = 1,
X = [_G281, a|_G285] ;
N = 2,
X = [_G281, _G284, a|_G288] ;
... etc

however, the SWI manual says:

Errors
  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? ).

false
  • 10,264
  • 13
  • 101
  • 209
pasaba por aqui
  • 3,446
  • 16
  • 40
  • See tag [tag:iso-prolog] for the standard. – false May 29 '15 at 12:21
  • (nowadays, in the times of open source, we can not expect prolog becames popular if even the standard is not public. My support to any one who starts an open one, by example as RFC). – pasaba por aqui May 29 '15 at 13:43
  • I seriously recommend you attend an introductory course on standards by your own national standardization institute to understand the difference between an RFC and a standard. – false May 29 '15 at 13:51

2 Answers2

4

If I read this correctly, it says, for

nth0(?Index, ?List, ?Elem)

"... if Index is not an integer or unbound."

In the example, Index is unbound:

?- nth0(N, L, E).
N = 0,
L = [E|_G1103] ;
N = 1,
L = [_G1102, E|_G1106] ;
N = 2,
L = [_G1102, _G1105, E|_G1109] .

So could you be misreading the manual?

Just to make sure: the manual should be understood as "... if Index is not( or( an integer, unbound ) )". What it does when Index is unbound is that it starts enumerating valid lists where Index can be 0 or larger.

  • @pasabaporaqui There is one correct way to interpret it, and I have pointed out which. See the answer by false for a better way to word this requirement. –  May 29 '15 at 13:25
3

The predicate 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
type_error(integer, N).

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
instantiation_error.

The predicate 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.

Community
  • 1
  • 1
false
  • 10,264
  • 13
  • 101
  • 209