Prev Next Up Home Keys Figs Search New

Adding Quotes to Numbers

Appeared in Volume 7/1, February 1994

Keywords: integers.

paul@cs.keele.ac.uk
Paul Singleton
16th August 1993

Jane Littlehales writes:
My problem is that the ProXL draw_string command for writing text to the graphics window cannot handle numbers unless they are in quotes.

I see this as a run-time matter (of type conversion) rather than as a compile-time matter (of syntax), and I've emailed concrete suggestions.

I think the whole unfortunate business of atoms/integers/floats is something which Prolog implementors should be embarrassed about.

There is surely nothing in first-order logic which says "thou shalt have three disjoint primitive types": it merely simplifies the implementation.

Prolog would be cleaner and simpler if it had just one 'atom' type, with arithmetic defined over those atoms which happened to be numeric, and undefined over the rest. Whether numeric atoms were represented internally in two's-complement notation or whatever should be irrelevant to the Prolog user. I like to program in terms (no pun intended) of "constants" (the union of atom, integer and float), and avoid using or creating any atoms which name/2 would rather represent as numbers.

I am over-familiar with arguments in favour of application-oriented type systems (I am involved in teaching Pascal to undergrads) and I don't like seeing Prolog on that slippery slope. By all means let's build type schemes for Prolog, but keep the core language minimal.

mcovingt@aisun3.ai.uga.edu
Michael Covington
16th August 1993

Let's talk about Prolog types, not about character strings. By "quotes around numbers" I presume that Jane Littlehales means atoms which look like numbers.

For instance, '2.3' is an atom and 2.3 is a number.

Interconversion is fairly simple:

number_atom(N,A) :- 
  number_chars(N,C), atom_chars(A,C).
Thus:

?- number_atom(2.3,What), writeq(What).
'2.3'
You're not inserting quotes, you're changing a type there.

andrewt@cse.unsw.edu.au
Andrew Taylor
17th August 1993

If you restrict legal integer names (no leading zeros, no signs on postive integers and between minint and maxint) there is a one-to-one mapping between integers and their names. So, as Paul suggests, it is feasible to implement atoms whose names are legal integers as two's-complement integers internally without this being visible to the user.

Unfortunately there is no such one-to-one mapping for floats. 7.5 and 0.75e1 are both names for the same float. If internally you used a floating point representation for atoms whose name is a legal float then both these atoms would have the same representation.

fjh@munta.cs.mu.OZ.AU
Fergus James Henderson
18th August 1993

Paul Singleton writes:
Prolog would be cleaner and simpler if it had just one 'atom' type...

In what way would that be different from the current situation?

He continues:
I am over-familiar with arguments in favour of application-oriented type systems...

What do you mean by "application-oriented"?

A type scheme doesn't necessarily complicate the core language, it may just restrict the class of well-formed programs. For example, a type system like Goedel's doesn't affect the semantics of type-correct programs at all - it's exactly the same as for a simple single-sorted (untyped) logic. It just makes certain programs illegal.

mcovingt@aisun3.ai.uga.edu
Michael Covington
18th August 1993

Fergus James Henderson writes:
In what way would that be different from the current situation?

You would not have '2.3' and 2.3 as different objects in the same language.

Right now, if I remember the draft ISO standard right, all of the following are different objects, not unifiable with each other:

2, 2.0, '2', '2.0', '2.00', '2.000', etc.

roland@sics.se
Roland Karlsson
18th August 1993

To treat numbers the same way as atoms complicates the language. The atoms 'a0' and 'a00' are different just as the atoms '1.0' and '1.00'. The numbers 1.0 and 1.00 are the same though and are both represented as the floating point number 1. So, how shall you do with the test:

1.0 == 1.00 or if you like '1.0' == '1.00'

Shall it fail or succeed?

paul@cs.keele.ac.uk
Paul Singleton
18th August 1993

I think you are confusing atoms with their source syntax representations.

The only property of atoms with which Prolog is properly concerned is their equality: two atoms are either identical or distinct.

It is certainly useful to add predicates which define a total ordering of atomic values, and which define arithmetic (integer, floating, rational, whatever) on some values, but these are "library" rather than "core" facilities.

Do you think Prolog should also have a primitive DATE type like SQL?

Either floats should be implemented as a structure of simpler types, e.g.

float(Exponent, Mantissa)

or Prolog should have some general, extensible mechanism for adding (for the sake of efficiency) user-defined opaque types. I accept that special-casing 'integer' and 'float' is justified on performance grounds, as might be the special casing of other types (date, char, map grid reference, long int, ...)

Unification should succeed iff the atoms denoted are identical.

paul@cs.keele.ac.uk
Paul Singleton
18th August 1993

Fergus James Henderson writes:
In what way would that be different from the current situation?

Atoms would just be functors of arity zero. Prolog arbitrarily forbids

3(a,b,c)

There would be no runtime "type" exceptions, just failure.

I didn't say it would be more useful, just cleaner and simpler...

Prev Next Up Home Keys Figs Search New