These predicates convert between Prolog constants and lists of character codes. The predicates atom_codes/2, number_codes/2 and name/2 behave the same when converting from a constant to a list of character codes. When converting the other way around, atom_codes/2 will generate an atom, number_codes/2 will generate a number or exception and name/2 will return a number if possible and an atom otherwise.
The ISO standard defines atom_chars/2 to describe the `broken-up' atom as a list of one-character atoms instead of a list of codes. Up to version 3.2.x, SWI-Prolog's atom_chars/2 behaved like atom_codes, compatible with Quintus and SICStus Prolog. As of 3.3.x, SWI-Prolog atom_codes/2 and atom_chars/2 are compliant to the ISO standard.
To ease the pain of all variations in the Prolog community, all SWI-Prolog predicates behave as flexible as possible. This implies the `list-side' accepts either a code-list or a char-list and the `atom-side' accepts all atomic types (atom, number and string).
?- atom_chars(hello, X). X = [h, e, l, l, o]
syntax_errorif Number is unbound or CharList does not describe a number. Following the ISO standard, it allows for leading white space (including newlines) and does not allow for trailing white space.96ISO also allows for Prolog comments in leading white space. We--and most other implementations--believe this is incorrect. We also beleive it would have been better not to allow for white space, or to allow for both leading and trailing white space. Prolog syntax-based conversion can be achieved using format/3 and read_from_chars/2.
name(N, "300"), 400 is N + 100succeeds). If CodeList is not a representation of a number, Atomic will be unified with the atom with the name given by the character code list. When Atomic is an atom or number, the unquoted print representation of it as a character code list will be unified with CodeList.
Note that it is not possible to produce the atom '300' using name/2,
name(300, CodeList), name('300', CodeList)
succeeds. For these reasons, new code should consider using the ISO
the ISO predicates provide no neat way to check that a string can be
interpreted as a number. The most sensible way is to use catch/3
to catch the exception from number_codes/2;
however, this is both slow and cumbersome. We consider making, e.g.,
"abc") fail silently in future versions. See also atom_number/2.
syntax_errorexception is raised. Otherwise Term is ``written'' on Atom using write_term/2 with the option
quoted(true). See also format/3, with_output_to/2 and term_string/2.
variable_namesand return the read term in Term and the variable bindings in Bindings. Bindings is a list of Name = Var couples, thus providing access to the actual variable names. See also read_term/2. If Atom has no valid syntax, a
syntax_errorexception is raised. New code should use read_term_from_atom/3.
?- atomic_concat(name, 42, X). X = name42.
atomic_list_concat(List, '', Atom).
?- atomic_list_concat([gnu, gnat], ', ', A). A = 'gnu, gnat'
The SWI-Prolog version of this predicate can also be used to split atoms by instantiating Separator and Atom as shown below. We kept this functionality to simplify porting old SWI-Prolog code where this predicate was called concat_atom/3. When used in mode (-,+,+), Separator must be a non-empty atom. See also split_string/4.
?- atomic_list_concat(L, -, 'gnu-gnat'). L = [gnu, gnat]
?- sub_atom(Atom, 0, _, _, Prefix). Deprecated.
?- sub_atom(abc, 1, 1, A, S). A = 1, S = b
The implementation minimises non-determinism and creation of atoms. This is a flexible predicate that can do search, prefix- and suffix-matching, etc.