11.3.185 read_term/[2,3]   ISO


read_term(-Term, +Options)

read_term(+Stream, -Term, +Options)

Read a term from Stream, optionally returning extra information about the term.



stream_object, must be ground

A valid Prolog input stream, defaults to the current input stream.



The term that is read.


list of term, must be ground, except Vars, Names, and Layout as described below.

A list of zero or more of the following:


Controls what action to take on syntax errors. Val must be one of the values allowed for the syntax_errors Prolog flag. The default is set by that flag. See ref-lps-flg.


Vars is bound to the list of variables in the term input, in left-to-right traversal order.


Names is bound to a list of Name=Var pairs, where each Name is an atom indicating the name of a non-anonymous variable in the term, and Var is the corresponding variable. The elements of the list are in the same order as in Term, i.e. in left-to-right traversal order.


Names is bound to a list of Name=Var pairs, one for each non-anonymous variable appearing only once in the term. The elements of the list are in the same order as in Term, i.e. in left-to-right traversal order.


Boolean must be true or false. If selected, any occurrences of @/2 in the term read in are replaced by the potentially cyclic terms they denote as described below. Otherwise (the default), Term is just unified with the term read in.

The notation used when this option is selected is @(Template,Substitution) where Substitution is a list of Var=Term pairs where the Var occurs in Template or in one of the Terms. This notation stands for the instance of Template obtained by binding each Var to its corresponding Term. The purpose of this notation is to provide a finite printed representation of cyclic terms. This notation is not used by default, and @/2 has no special meaning except in this context.

Terms can be written in this notation using write_term/[2,3] (see mpg-ref-write_term).


Layout is bound to a layout term corresponding to Term (see Glossary).


Boolean must be true or false. If this option is true, read_term/[2,3] will consume the layout-text-item that follows the terminating ‘.’ (this layout-text-item can either be a layout-char or a comment starting with a ‘%’). If the option is false, the layout-text-item will remain in the input stream, so that subsequent character input predicates will see it. The default of the consume_layout option is false.


The characters read are subject to character-conversion.


Stream errors (see ref-iou-sfh-est), plus:


A syntax error was found.


An illegal option was specified.


| ?- read_term(T, [variable_names(L)]).
|: append([U|X],Y,[U|Z]) :- append(X,Y,Z).
L = ['U'=_A,'X'=_B,'Y'=_C,'Z'=_D],
T = (append([_A|_B],_C,[_A|_D]):-append(_B,_C,_D))
| ?- read_term(T, [layout(L), variable_names(Va), singletons(S)]).
|: [
     X = Y

L = [35,[36,36],[36,[37,37,37],38]],
S = ['Y'=_A],
T = [foo(_B),_B=_A],
Va = ['X'=_B,'Y'=_A]

| ?- read_term(T, [consume_layout(false)]), get_code(C).
|: 1.

C = 10,
T = 1

| ?- read_term(T, [consume_layout(true)]), get_code(C).
|: 1.
|: a

C = 97,
T = 1

See Also

read/[1,2], char_conversion/2, ref-iou-tin-trm, ref-lps-flg.

Send feedback on this subject.