..
   This file is part of Logtalk <https://logtalk.org/>
   SPDX-FileCopyrightText: 1998-2025 Paulo Moura <pmoura@logtalk.org>
   SPDX-License-Identifier: Apache-2.0

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.


.. rst-class:: align-right

**built-in method**

.. index:: pair: assertz/1; Built-in method
.. _methods_assertz_1:

``assertz/1``
=============

Description
-----------

::

   assertz(Head)
   assertz((Head:-Body))

Asserts a clause as the last one for a dynamic predicate.

When the predicate was not previously declared (using a scope directive),
a dynamic predicate declaration is added to the object. In this case, the
predicate scope depends on how this method is called:

``assertz(Clause)``
   The predicate is dynamically declared as :term:`private <private predicate>`.
``::assertz(Clause)``
   The predicate is dynamically declared as :term:`protected <protected predicate>`.
``Object::assertz(Clause)``
   The predicate is dynamically declared as :term:`public <public predicate>`.

Note, however, that dynamically declaring a new predicate requires either a
local assert or the :ref:`dynamic_declarations <flag_dynamic_declarations>`
compiler flag set to ``allow`` when the object was created or compiled.

When the predicate indicator for ``Head`` is declared in a :ref:`directives_uses_2` or
:ref:`directives_use_module_2` directive, the clause is asserted in the referenced
object or module. When the backend Prolog compiler supports a module system, the
predicate argument can also be module qualified.

This method may be used to assert clauses for predicates that are not
declared dynamic for dynamic objects provided that the predicates are
declared in :term:`this`. This allows easy initialization of dynamically
created objects when writing constructors.

Modes and number of proofs
--------------------------

::

   assertz(+clause) - one

Errors
------

| ``Head`` is a variable:
|     ``instantiation_error``
| ``Head`` is neither a variable nor a callable term:
|     ``type_error(callable, Head)``
| ``Body`` cannot be converted to a goal:
|     ``type_error(callable, Body)``
| The predicate indicator of ``Head``, ``Name/Arity``, is that of a private predicate:
|     ``permission_error(modify, private_predicate, Name/Arity)``
| The predicate indicator of ``Head``, ``Name/Arity``, is that of a protected predicate:
|     ``permission_error(modify, protected_predicate, Name/Arity)``
| The predicate indicator of ``Head``, ``Name/Arity``, is that of a static predicate:
|     ``permission_error(modify, static_predicate, Name/Arity)``
| The predicate indicator of ``Head``, ``Name/Arity``, does not match a declared predicate and the target object was created/compiled with support for dynamic declaration of predicates turned off:
|     ``permission_error(create, predicate_declaration, Name/Arity)``

Examples
--------

| To assert a clause as the last one for a local dynamic predicate or a dynamic predicate in :term:`this`:
|     ``assertz(Clause)``
| To assert a clause as the last one for any public or protected dynamic predicate in :term:`self`:
|     ``::assertz(Clause)``
| To assert a clause as the last one for any public dynamic predicate in an explicit object:
|     ``Object::assertz(Clause)``

An example of asserting clauses in :term:`this` and in :term:`self` from a category:

::

   :- category(attributes,
       implements(attributes_protocol)).

       :- private(attr_/1).
       :- dynamic(attr_/1).

       set_in_this(A, X) :-
          assertz(attr_(A, X)).

       set_in_self(A, X) :-
          ::assertz(attr_(A, X)).

       ...

An example of asserting clauses into another object with the predicates
listed using a :ref:`directives_uses_2` directive (similar when using a
:ref:`directives_use_module_2` directive):

::

   :- object(reasoner(_KnowledgeBase_)).

       :- uses(_KnowledgeBase_, [
           foo/1, bar/1
       ]).

       baz(X) :-
          % compiled as _KnowledgeBase_::assertz(foo(X))
          assertz(foo(X)).

      foobar(Name, Argument) :-
         Clause =.. [Name, Argument],
         % runtime resolved to _KnowledgeBase_::assertz(Clause)
         % when Name is either foo or bar
         assertz(Clause).

       ...

.. seealso::

   :ref:`methods_abolish_1`,
   :ref:`methods_asserta_1`,
   :ref:`methods_clause_2`,
   :ref:`methods_retract_1`,
   :ref:`methods_retractall_1`
   :ref:`directives_dynamic_0`,
   :ref:`directives_dynamic_1`,
   :ref:`directives_uses_2`,
   :ref:`directives_use_module_2`
