Safer type tests in Prolog

The testing for types needs to distinguish itself from the traditional “type testing” built-ins that implicitly also test for a sufficient instantiation. So we effectively test only for sufficiently instantiated terms (si). And if they are not sufficiently instantiated, an appropriate error is issued.

For a type nn, there is thus a type testing predicate nn_si/1 with the only error condition

a) If there is a θ and σ such that nn_si(Xθ) is
true and nn_si(Xσ) is false

instantiation_error.

atom_si(A) :-
   functor(A, _, 0),    % for the instantiation error
   atom(A).

integer_si(I) :-
   functor(I, _, 0),
   integer(I).

atomic_si(AC) :-
   functor(AC,_,0).

list_si(L) :-
   \+ \+ length(L, _),  % for silent failure
   sort(L, _).          % for the instantiation error

This is available as library(si) in Scryer.

In SWI, due to its differing behavior in length/2, use rather:

list_si(L) :-
    '$skip_list'(_, L, T),
    functor(T,_,_),
    T == [].

Leave a Comment