
-------- Original Message -------- Subject: Re: Any Prolog language lawyer out there? Date: 20 Oct 2004 08:17:19 GMT From: Jan Wielemaker jan@ct.xs4all.nl Reply-To: jan at nospam.swi.psy.uva.nl Organization: SWI, University of Amsterdam Newsgroups: comp.lang.prolog References: 41750912.8040102@cs.unipr.it 1098196164.417498@seven.kulnet.kuleuven.ac.be slrncnaat8.jkn.jan@ct.xs4all.nl 87acuijutl.fsf@gondolin.bb.bawue.de
Jens,
Thanks for clarifying. I do not have the official ISO text. Either this isn't correct in 'Prolog the Standard', or I missed this fragment (I do not have the book at hand right now).
In article 87acuijutl.fsf@gondolin.bb.bawue.de, Jens Kilian wrote:
Jan Wielemaker jan@ct.xs4all.nl writes:
| ?- functor(- 4,X,Y). X = -4, Y = 0 ?
I think this is a mistake. Reading "Prolog, the standard", I find (p. 230) that an integer is an integer token, which is defined as a sequence of digits (besides binary, octal, etc). If this is really true, than negative numbers cannot be represented and only get their property when passed to one of the arithmetic predicates.
My copy of a draft version of the standard (ISO/IEC JTC1 SC22 WG17 N110) says
6.3.1.2 Negative numbers term = atom, integer; Abstract: -n - n Priority: 0 term = atom, float number; Abstract: -r - r Priority: 0 The prefix operator - with a numeric constant as operand denotes the corresponding negative constant.
This explicitly codifies the interpretation of '- 4' seen above.
I do not like it ...
The tokenizer *cannot* consume the '-' in front of a number, because it may need to be interpreted as an operator. Not even the presence or absence of whitespace helps:
X is Y-4. X is Y - 4.
These must both be interpreted as is(X, -(Y, 4)).
This is of course a bit undesirable as the tokeniser returns atoms, ints and floats as Prolog objects. Using 2-complement 32 bit ints, it cannot represent the token 2147483648. With your example Y-4 I started to wonder how I did this long time ago. Turns out the parser tells the tokeniser that the next token must be an operator (infix or postfix). If the tokeniser sees the next argument must be an operator it returns the atom '-', else the negative integer.
Except for allowing for white-space this may actually be right. At any location where a term of the priority of the prefix operator - is allowes, it is also allowed to use an integer (priority 0), so the only reason to interpret the - in "-4" as an operator is if we need an infix operator at that place. It cannot even be interpreted as postfix, as the parser will fail afterwards on the "4"
Writing terms -(number) is also interesting. The only valid representation is using (). Tested SICStus, Ciao, hProlog, gprolog, YAP and SWI-Prolog. SICStus, Ciao and SWI get this right.
I'm tempted to accept "- 4" as the negative number -4 and print a style warning. Not in a hurry though :-)
Does the official standard document document say anything about handling + in front of numbers?
Yes, this sucks. As does most of Prolog's syntax ;-)
If it was the only thing that sucked, it would be almost perfect :-)
Cheers --- Jan