current_predicate/1 does not work with :- dynamic?

1.8k Views Asked by At

I have some predicates that I define using asserts in Prolog.

I am using current_predicate/1 in order to know whether the assert has been run or not (only one value needs to be asserted).

However, swipl keeps complaining:

Warning: The predicates below are not defined. If these are defined
Warning: at runtime using assert/1, use :- dynamic Name/Arity.
Warning: 
Warning: amountOfStudentsInCourseAsserted/2, which is referenced by

So, I added the :- dynamic amountOfStudentsInCourseAsserted/2, but unfortunately, this adds the predicate to the current_predicate(Predicate).. Therefore I cannot use current_predicate/1 anymore if I am using this dynamic naming.

Is there another predicate like current_predicate/1that isn't true for dynamic names?

2

There are 2 best solutions below

4
On BEST ANSWER

You can use in alternative the predicate_property/2 built-in predicate and the number_of_clauses/1 property.

1
On

The :- dynamic declaration is appropriate, as it will make the symbol known in the database. Then just attempt to match (with appropriate arguments, ignored in the following sample) before asserting:

...
(  amountOfStudentsInCourseAsserted(_,_)
-> true
;  assert(amountOfStudentsInCourseAsserted(X,Y))
),
...