unknown_predicate_handler/3
hook:- multifile unknown_predicate_handler/3.
unknown_predicate_handler(
+Goal, +Module, -NewGoal)
User definable hook to trap calls to unknown predicates
When Prolog comes across a call to an
unknown predicate and the unknown
flag is set to error
, Prolog
makes a call to unknown_predicate_handler/3
in module user
with
the first two arguments bound. Goal is bound to the call to the
undefined predicate and Module is the module in which that
predicate is supposed to be defined. If the call to
unknown_predicate_handler/3
succeeds then Prolog replaces the
call to the undefined predicate with the call to NewGoal. By
default NewGoal is called in module user
. This can be
overridden by making NewGoal have the form Module:SomeGoal.
The following clause gives the
same behaviour as setting unknown(_,fail)
.
unknown_predicate_handler(_, _, fail).
The following clause causes calls to undefined predicates whose names
begin with xyz_
in module m
to be trapped to my_handler/1
in module
n
. Predicates with names not beginning with this character
sequence are not affected.
unknown_predicate_handler(G, m, n:my_handler(G)) :- functor(G,N,_), atom_chars(N,Chars), append("xyz_" _, Chars).
unknown/2
, prolog_flag/3