Quick answer:
You want to check for the fact or assert the fact. The or as used in the sentence means logical or which translates to Prolog as the ;
operator.
Here is an example
add_imports(Module,Import) :-
(
imports(Module,Import), !
;
assertz(imports(Module,Import))
).
This is the first part of the statement that checks if the fact already exist.
imports(Module,Import)
This is the second part of the statement that asserts the fact if the first part of the or
fails.
assertz(imports(Module,Import))
Note the use of logical or (;
) and the cut (!
). The cut is used to avoid leaving behind unnecessary choice points.
The example used is actually from real working code from the post
which in the example is
add_imports(Module,Import) :-
(
imports(Module,Import), !
;
assert_imports(Module,Import)
).
but you have to understand that when using library(persistency) that it will create the predicate assert_imports/2
, thus why the example code for library(persistency) is different.