[ Dynamic Predicates | Reference Manual | Alphabetic Index ]

asserta(+Clause)

Add specified clause for a dynamic procedure to the database before any existing clauses
Clause
Atom or compound term

Description

Adds the specified clause for a dynamic procedure to the database.] The clause is added BEFORE any existing clauses for the procedure.

The procedure must be declared to be dynamic using the dynamic/1 built-in. If the procedure is undefined an exception is raised. However, the default error handler for this exception simply declares the procedure dynamic and asserts the clause.

The asserted clause is NOT removed from the database on backtracking through the call to asserta/1.

asserta/1 satisfies the logical update semantics. Asserting a clause to a procedure will not, in any way, affect previous calls to it when backtracking.

No clause transformation is performed on Clause. Use expand_clause/2 to explicitly expand the clause before calling this predicate if clause expansion is rquired.

Modes and Determinism

Modules

This predicate is sensitive to its module context (tool predicate, see @/2).

Exceptions

(4) instantiation fault
Clause is a free variable.
(4) instantiation fault
The head of Clause is a free variable.
(5) type error
Clause is not a valid Prolog clause.
(63) procedure not dynamic
The procedure is not dynamic.
(70) accessing an undefined dynamic procedure
The procedure is undefined. However, the default error handler for this exception simply declares the procedure dynamic and asserts the clause, if the error was caused by an assert.

Examples

   Success:
    ?- asserta(city(london)), asserta(city(munich)), asserta(city(paris)).
    yes.
    ?- listing.
    city(paris).
    city(munich).
    city(london).
    yes.

    ?- asserta((likes(X,Y) :- man(X), woman(Y))).
    yes.
    ?- listing.
    likes(_186, _187) :-
        man(_186),
        woman(_187).
    yes.


   Error:
    asserta(X).                        - gives error 4.
    asserta("the man").                - gives error 5.
    asserta(1).                        - gives error 5.
    asserta((my_static(X):-write(X))). - gives error 63.
                                        if my_static/1 is not
                                        dynamic.
   Logical semantics :

   If the following clauses are in the database :
p :- asserta(p), fail.
p :- fail.

q :- fail.
q :- asserta(q), fail.

   The queries p.  and q.  will both fail.

See Also

dynamic / 1, assert / 1, assertz / 1, retract / 1, compile_term / 1