Module term [Preloaded]

Terms can be written to text streams or read from text streams depending on the current oper-ator definitions. It is possible to switch off operator usage by the write option ignore_ops/1. Further during writing variables and atoms are put into quotes when necessary. It is possible to switch on quoting by the write option quoted/1.

Finally terms of the form ‘$VAR’(<number>) are usually recognized and written out as <var>. It is possible to switch on the variable numbering by the write option numbervars/1. The Prolog system also supports '$STR'(<atom>) terms to represent strings and can read or write them as quoted strings.

Table 11: Predefined Write Predicates

Predicate numbervars quoted ignore_ops
write Yes No No
writeq Yes Yes No
write_canonical No Yes Yes

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 needed around an operator expressions depending on the level of the operator.

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.
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 context type values are ‘?’ or an integer. The default context type value is ‘?’. Le-gal format values are ‘false’, ‘newline’, ‘navigation’ and ‘true’. The default format value is ‘false’. Legal values for the operator priority are integer values in the range 0 to 1200. The default operator priority is 1200.

Legal annotation values are ‘false’, ‘makedot’, ‘filler’ and ‘true’. The default annotation value is ‘false’. Legal single quotes, double quotes and back quotes values are ‘error’, ‘chars’ [ISO], ‘codes’ [ISO], ‘variable', 'atom' [ISO] and ‘string’. 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.
priority(P): P is the read priority.
terminator(T): T is the terminator.
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 terminator values are 'period' [ISO], 'end_of_file' and 'none'. The default value is 'period'. Legal single quotes, double quotes and back quotes values are ‘error’, ‘chars’ [ISO], ‘codes’ [ISO], ‘variable’, 'atom' [ISO] and ‘string'. 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], ‘variable’, ‘atom’ [ISO] and ‘string’. The flag indicates how double quoted strings are parsed. The default value is ‘codes’.
back_quotes:
Legal values are ‘error’, ‘chars’, ‘codes’, ‘variable’, ‘atom’ and ‘string’. The flag indicates how back quoted strings are parsed. The default value is ‘error’.
single_quotes:
Legal values are ‘error’, ‘chars’, ‘codes’, ‘variable’, ‘atom’ and ‘string’. The flag indicates how single quoted strings are parsed. The default value is ‘atom’.

Comments