11.3.10 asserta/[1,2] [ISO]


These predicates add a dynamic clause, Clause, to the Prolog database. They optionally return a database reference in Ref:


asserta(+Clause, -Ref)

Clause will precede all existing clauses in the database.


callable, must be nonvar

A valid dynamic Prolog clause.


A database reference, which uniquely identifies the newly asserted Clause.


Clause must be of the form:

     or     Head :- Body
     or     M:Clause

where Head is of type callable and Body is a valid clause body. If specified, M must be an atom.

asserta(Head) means assert the unit-clause Head. The exact same effect can be achieved by asserta((Head :- true)).

If Body is uninstantiated it is taken to mean call(Body). For example, (A) is equivalent to (B):

     | ?- asserta((p(X) :- X)).            (A)
     | ?- asserta((p(X) :- call(X))).      (B)

Ref should be uninstantiated; a range exception is signalled if Ref does not unify with its return value. This exception is signalled after the assert has been completed.

The procedure for Clause must be dynamic or undefined. If it is undefined, it is set to be dynamic.

When an assert takes place, the new clause is immediately seen by any subsequent call to the procedure. However, if there is a currently active call of the procedure at the time the clause is asserted, the new clause is not encountered on backtracking by that call. See ref-mdb-bas for further explanation of what happens when currently running code is modified.

Any uninstantiated variables in the Clause will be replaced by new private variables, along with copies of any subgoals blocked on these variables (see ref-sem-sec).


if Head (in Clause) or M is uninstantiated.
if Head is not of type callable, or if M is not an atom, or if Body is not a valid clause body.
if the procedure corresponding to Head is not dynamic.

See Also


Send feedback on this subject.