Converting between Constants and Text

Three predicates convert between constants and lists of ASCII character codes: atom_chars/2, number_chars/2, and name/2.

There is a general convention that a predicate that converts objects of type foo to objects of type baz should have one of these forms:

     foo_to_baz(+Foo, -Baz) (1)
     
     foo_baz(?Foo, ?Baz) (2)
     

Use (1) if the conversion works only one way, or (2) if for any Foo there is exactly one related Baz and for any Baz at most one Foo.

The type name used for lists of ASCII character codes is chars thus, the predicate that relates an atom to its name is atom_chars(?Atom, ?Chars), and the predicate that relates a number to its textual representation is number_chars(?Number, ?Chars).

atom_chars(Atom, Chars) is a relation between an atom Atom and a list Chars consisting of the ASCII character codes comprising the printed representation of Atom. Initially, either Atom must be instantiated to an atom, or Chars must be instantiated to a proper list of character codes.

number_chars(Number, Chars) is a relation between a number Number and a list Chars consisting of the ASCII character codes comprising the printed representation of Number. Initially, either Number must be instantiated to a number, or Chars must be instantiated to a proper list of character codes.

name/2 converts from any sort of constant to a chars representation. Given a chars value, name/2 will convert it to a number if it can, otherwise to an atom. This means that there are atoms that can be constructed by atom_chars/2 but not by name/2. name/2 is retained for backwards compatibility with DEC-10 Prolog and C-Prolog. New programs should use atom_chars/2 or number_chars/2 as appropriate.