% File sum.pl

% The following predicate sum(L,N) holds when L is a list of numbers
% and N is their sum

sum([],0).
sum([X|L],N) :- sum(L,M), N is M + X.

% Sample session. Note that, dues to the extra-logical predicate "is",
% the sum is not invertible, i.e. ?- sum(L, 6) gives error.
% This is because in the atom
%    N is Exp
% the variables in Exp must be instantiated at the moment the atom
% is evaluated

/*
?- consult('sum.pl').
sum.pl compiled, 0.00 sec, 644 bytes.

Yes
?- sum([1,2,3],N).

N = 6 ;

No
?- sum(L,6).
[WARNING: Arguments are not sufficiently instantiated]
 ^ Exception: (  9) 6 is 0+_G230 ? abort

Execution Aborted

?
*/
