Subject: Re: [SWIPL] Q

From: "Richard A. O'Keefe" <[email-masked]>
Date: Fri, 17 Aug 2001 12:23:06 +1200 (NZST)

"Lyosha" <[email-masked]> wrote:
	If [] is not a list then I would expect is_list([]) to fail.
 	However it succeeds, which
	of course we can treat as an exception. :)
	
What exception?  [] *is* a list.
It is also an atom.  There is nothing new about this.
The empty list in Lisp has also been an atom for the last 40 years or more.

In Prolog, *EVERY* term is a variable, or a number, or it has a function
symbol and zero or more arguments.  (Some Prologs add strings, and some
add "foreign objects".)  Lists are not an exception to this.  An empty
is not a variable, is not a number, and doesn't have any arguments, so
it _has_ to be an atom because there isn't anything else for it to be.

This is book one lesson 2.

	I have another question about lists though.
	
	?- A is [2].
	A = 2
	
	?- A is [4]*[5].
	A = 20
	
	I expected to get an exception when using lists in 'is'.

Had you read the manual, you would not have expected this.

	It's not to be complained about I guess but I am curious - why
	is it happening?  Anyone know?

Anyone who reads the manual or a good Prolog book knows this.  It is
so that you can do arithmetic with characters.  Recall that strings in
Prolog are lists of character codes.  If I want to check whether a
character is a digit, and if so, return its binary equivalent, I could
write

    digit_value(C, D) :-		% ASCII version
	48 =< C, C =< 57,
	D is C - 48.
or
    digit_value(C, D) :-		% EBCDIC version
	16'F0 =< C, C =< 16'F9,
	D is C - 16'F0.

But it is much much clearer to write

    digit_value(C, D) :-		% portable version
	"0" =< C, C =< "9",
	D is C - "0".

I invented the 0' notation in DEC-10 Prolog, so that this can also be
written as

    digit_value(C, D) :-
	0'0 =< C, C =< 0'9,
	D is C - 0'0.

but quite a few Prologs never picked it up.

The rule therefore is that a Prolog system is supposed to accept
	[N]
as an arithmetic expression, where N is an integer.  Prologs are
not required to accept other lists, so
	X is [1+1]
might (Quintus) or might not (SWI) be accepted.