Converting Between Constants and Characters

Quintus Prolog currently supports the following kinds of constants:

The data type "list of character codes" is called chars. You can convert between atoms, numbers, and chars using the following predicates:

name/2 is retained for compatibility with DEC-10 Prolog, C-Prolog, and earlier releases of Quintus Prolog. All the other predicates have names that follow a rule, which you would do well to follow in your own code.

Suppose you have two data types foo and baz, and a predicate that converts from one type to another. If each value of type foo corresponds to exactly one value of type baz, and if each value of type baz corresponds to at most one value of type foo, the predicate should be called

     foo_baz(?The_foo, ?The_baz)

As an example, let foo be the data type "character code" (we call this "char"), and let baz be the data type "atom". Given any char C, there is exactly one atom whose name is [C]. Given any atom, either its name contains one single character C, or it contains some other number of characters, in which case there is no unique character to which it corresponds. Therefore, a predicate that converts between character codes and single-character atoms will have the name

     char_atom(?Char, ?Atom)

Remember that this pattern means that we can always solve for the second argument given a value for the first, and that we may be able to solve for the first argument given a value for the second.

If a foo can be converted to a unique baz, but a baz might correspond to more than one foo, the predicate is to be called

     foo_to_baz(+The_foo, ?The_baz)

The _to_ tells you that the conversion only works one way around. For example, given an atom or number, there is a unique list of character codes that can be made from it, but a given list of character codes such as "0" could have come from an atom or an integer. Therefore a predicate that converts between arbitrary constants and character codes should be called

     constant_to_chars(+Constant, ?Chars)

In fact this operation is called name/2, because that is what it was called in DEC-10 Prolog.

All the new data type conversion predicates follow these naming rules. Now let us look at them.