If Clause is a list, the list elements are interpreted as consecutive clauses. Otherwise, Clause will be taken as a single clause. Each clause may be a fact or a rule.
As with source files, the list may also contain directives (:- xxx) but their handling is slightly different: include/1, if/1, elif/1, else/0, endif/0 and module directives are not allowed. Pragmas are interpreted. All others, as well as file queries (?- xxx) are called as goals.
This predicate works almost as if all the clauses in the list were written into a file and this file was then compiled using compile/1. Unlike in earlier releases, clause and goal expansion is performed, according to the default compiler options. General macro-expansion is not performed, since this is normally the responsibility of the parser. If required, it must be done explicitly via expand_macros/2.
The difference between compile_term/1 and assert/1 is that the predicates for which clauses are compiled are not necessarily dynamic with compile_term/1, unless explicitly declared as such. Therefore clauses compiled with compile_term/1 usually replace the existing ones for the same predicate, moreover their source form is not available. Therefore, it can be used instead of assert/1 if the properties of dynamic procedures are not required.
Unlike compiling a file, when an event occurs which is not just a warning, the following clauses are not compiled, the compilation is aborted.
See compile/2 for a description of compiler options and a exceptions.
Success: % several facts for different predicates ?- compile_term([p(a), p(b), q(1), r("abc")]). % a single clause ?- compile_term(p(X) :- q(X)). % two clauses for the same predicate ?- compile_term([p([]), (p([X|Xs]) :- writeln(X), p(Xs))]). % a declaration and two clauses ?- compile_term([(:- export p/1), p(a), p(b)]). Error: compile_term([p|X]). (Error 4). compile_term([a|b]). (Error 5). compile_term([[a]]). (Error 94). compile_term([(p :- write(a)), write(b)]). (Error 94). compile_term("a"). (Error 130). compile_term(["a" :- b]). (Error 130). compile_term([p(X) :- 1]). (Error 131). compile_term([a, b, a]). (Error 134). compile_term(!). (Error 135). compile_term(:- var(a)). (Error 143).