Module term [Preloaded]

Terms can be written to text streams or read from text streams depending on the current operator definitions. It is possible to switch off using the current operator definitions. Further during writing we might want to put atoms and variables back into quotes when necessary. It is possible to switch on quoting of atoms and variables. Finally terms of the form ‘$VAR’(<number>) are usually recognized and written out as <var>. It is also possible to switch off this variable numbering:

Table 11: Predefined Write Predicates

Predicate numbervars quoted ignore_ops
write true false false
writeq false true false
write_canonical false true true

The spacing is determined by the context type option. The context type ‘?’ minimizes the spacing. The other context types use spacing for the current compound and they also determine which meta-declaration should be looked up in case of a closure. Here are some examples whereby we assume a meta_predicate declaration solve(0):

Table 11: Context Dependent Spacing

Context Example
? solve((_A,_B)):-solve(_A),solve(_B)
0 or -1
solve((_A, _B)) :- solve(_A), solve(_B)

If the format option is newline then the spacing is enhanced by new lines and further spaces so that the output matches the Prolog coding guidelines as published in [9]. Further the priority option determines whether parentheses are set around operator expressions. Finally the operand option determines whether parentheses are set around single standing operators and some pathological left associative cases already mentioned in the ISO standard.

When double quotes or back quotes are set to ‘variable’ and quote is true, then variable names are automatically set into the corresponding quotes when necessary. If neither double quotes nor back quotes are set to ‘variable’, then the predicates write_term/[2,3] throw an error if a variable name would need quotes.

The following term input/output predicates are provided:

write(E): [ISO 8.14.2]
write(T, E): [ISO 8.14.2]
The unary predicate writes the term E to the standard output whereby numbering variables. The binary predicate takes an additional text stream sink as argument.
writeq(E): [ISO 8.14.2]
writeq(T, E): [ISO 8.14.2]
The predicate writes the term E to the standard output whereby quoting atoms and variables if necessary. The binary predicate takes an additional text stream sink as argument.
write_canonical(E): [ISO 8.14.2]
write_canonical(T, E): [ISO 8.14.2]
The predicate writes the term E to the standard output whereby quoting atoms and variables if necessary and ignoring operator declarations. The binary predicate takes an additional text stream sink as argument.
write_term(E, O): [ISO 8.14.2]
write_term(T, E, O): [ISO 8.14.2]
The predicate writes the term E to the standard output taking into account the write options O. The ternary predicate takes an additional text stream sink as argument. The following write options are available:
quoted(B): [ISO] If B then functors are quoted if necessary.
numbervars(B): [ISO] If B then compounds ‘$VAR’(I) are written as variables.
ignore_ops(B): [ISO] If B then functors are not interpreted as operators.
variable_names(N): N are the variable names.
context(C): C is the meta-argument specifier.
format(F): F is the write format.
priority(P): P is the write priority.
operand(O): C is the operand type of the term.
double_quotes(U): U is the parsing of double quotes.
back_quotes(U): U is the parsing of back quotes.
single_quotes(U): U is the parsing of single quotes.
annotation(A): A is the annotation mode.
source(S): S is the source the term belongs to.
part(N): P is the part that should be written.
Legal format values are ‘false’, ‘newline’, ‘navigation’ and ‘true’. The default format value is ‘false’. Legal context type values are ‘?’ or an integer. The default context type value is ‘?’. Legal operand type values are ‘none’, ‘left’ and ‘leftassoc’. The default operand type value is ‘none’.

Legal annotation values are ‘false’, ‘makedot’, ‘filler’ and ‘true’. The default annotation value is ‘false’. Legal double quotes and back quotes values are ‘error’, ‘chars’, ‘codes’, ‘atom’ and ‘variable’. The default quote values are taken from the corresponding Prolog flags. Legal part values are ‘false’, ‘comment’, ‘statement’ and ‘true’. The de-fault part value is ‘true’.
read(E): [ISO 8.14.1]
read(T, E): [ISO 8.14.1]
The unary predicate reads a sentence from the standard input and parses it into a Prolog term. The sentence consists of the tokens up to the first terminating period (“.”). When the sentence only contains filler without a terminating period (“.”) then the predicate succeeds when E unifies with the end_of_file atom. Otherwise the predicate succeeds when E unifies with the parsed term. The binary predicate takes an additional text stream source as argument.
read_term(E, O): [ISO 8.14.1]
read_term(T, E, O): [ISO 8.14.1]
The predicate reads the term E from the standard output taking into account the read options O. The ternary predicate takes an additional text stream source as argument. The following read options are available:
variables(L): [ISO] L are the variables.
variable_names(N): [ISO] N are the variable names.
singletons(S): [ISO] S are the singleton names.
double_quotes(U): U is the parsing of double quotes.
back_quotes(U): U is the parsing of back quotes.
single_quotes(U): U is the parsing of single quotes.
annotation(A): A is the annotation mode.
source(S): S is the source the term belongs to.
line_no(N): N is line number where the term starts.
Legal double quotes and back quotes values are ‘error’, ‘chars’, ‘codes’, ‘atom’ and ‘variable’. The default values are taken from the corresponding Prolog flags. Legal annotation values are ‘false’, ‘makedot’, ‘filler’ and ‘true’. The default annotation is ‘false’.
The following Prolog flags for term input/output are provided:
char_conversion: [ISO 7.11.2.1]
Legal values are ‘on’ [ISO] and ‘off'’ [ISO]. The flag indicates whether unquoted tokens are character converted. The default value is ‘off’. Currently the value cannot be changed.
double_quotes: [ISO 7.11.2.5]
Legal values are ‘error’, ‘chars’ [ISO], ‘codes’ [ISO], ‘atom’ [ISO] and ‘variable’. The flag indicates how double quoted strings are parsed. The default value is ‘codes’.
back_quotes:
Legal values are ‘error’, ‘chars’, ‘codes’, ‘atom’ and ‘variable’. The flag indicates how back quoted strings are parsed. The default value is ‘error’.
single_quotes:
Legal values are ‘error’, ‘chars’, ‘codes’, ‘atom’ and ‘variable’. The flag indicates how single quoted strings are parsed. The default value is ‘atom’.

Comments