align-right
built-in method
pair: asserta/1; Built-in method
asserta(Head)
asserta((Head:-Body))
Asserts a clause as the first one for an object 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:
asserta(Clause)
The predicate is dynamically declared as
private <private predicate>
.::asserta(Clause)
The predicate is dynamically declared as
protected <protected predicate>
.Object::asserta(Clause)
The predicate is dynamically declared as
public <public predicate>
.
Note, however, that dynamically declaring a new predicate requires either a local assert or the 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 directives_uses_2
or 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 this
. This allows easy initialization of dynamically created objects when writing constructors.
asserta(+clause) - one
Head
is a variable:instantiation_error
Head
is a 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 or compiled with support for dynamic declaration of predicates turned off:permission_error(create, predicate_declaration, Name/Arity)
this
:asserta(Clause)
To assert a clause as the first one for any public or protected dynamic predicate in
self
:::asserta(Clause)
To assert a clause as the first one for any public dynamic predicate in an explicit object:
Object::asserta(Clause)
An example of asserting clauses in this
and in self
from a category:
:- category(attributes,
implements(attributes_protocol)).
:- private(attr_/1).
:- dynamic(attr_/1).
set_in_this(A, X) :-
asserta(attr_(A, X)).
set_in_self(A, X) :-
::asserta(attr_(A, X)).
...
An example of asserting clauses into another object with the predicates listed using a directives_uses_2
directive (similar when using a directives_use_module_2
directive):
:- object(reasoner(_KnowledgeBase_)).
:- uses(_KnowledgeBase_, [
foo/1, bar/1
]).
baz(X) :-
% compiled as _KnowledgeBase_::assertz(foo(X))
asserta(foo(X)).
foobar(Name, Argument) :-
Clause =.. [Name, Argument],
% runtime resolved to _KnowledgeBase_::assertz(Clause)
% when Name is either foo or bar
asserta(Clause).
...
methods_abolish_1
, methods_assertz_1
, methods_clause_2
, methods_retract_1
, methods_retractall_1
directives_dynamic_0
, directives_dynamic_1
, directives_uses_2
, directives_use_module_2