Prolog: How to check if a predicate exists?

后端 未结 2 1607
别那么骄傲
别那么骄傲 2021-01-04 18:31

How can I check if a predicate exists in a Prolog program? That would be an exists/1, like:

?- exists(some_predicate).
false.

?- assert(some_pr         


        
2条回答
  •  别那么骄傲
    2021-01-04 19:00

    You can use current_predicate/1, current_predicate/2 or predicate_property/2 (for the last you will probably need functor/3):

    ?- current_predicate(a/1).
    false.
    
    ?- functor(A,a,1),predicate_property(A,visible).
    false.
    
    ?- functor(A,a,1),current_predicate(_,A).
    false.
    
    ?- assert(a(42)).
    true.
    
    ?- current_predicate(a/1).
    true.
    
    ?- functor(A,a,1),predicate_property(A,visible).
    A = a(_G136).
    
    ?- functor(A,a,1),current_predicate(_,A).
    A = a(_G122).
    

    current_predicate/2 and predicate_property/2 (with visible) succeeds if the predicate can be autoloaded while currrent_predicate/1 fails

提交回复
热议问题