11.3.237 unknown_predicate_handler/3 [hook]

Synopsis

:- multifile user:unknown_predicate_handler/3.

user:unknown_predicate_handler(+Goal, +Module, -NewGoal)

User definable hook to trap calls to unknown predicates.

Arguments

Goal
callable

The goal to trap.

Module
atom

Any atom that is a current module

NewGoal
callable

The goal to call instead.

Description

When Prolog comes across a call to an unknown predicate, Prolog makes a call to user:unknown_predicate_handler/3 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 user:unknown_predicate_handler/3 succeeds, then Prolog replaces the call to the undefined predicate with the call to Module:NewGoal. Otherwise, the action taken is governed by the unknown Prolog flag. See ref-lps-flg.

Exceptions

Exceptions are treated as failures, except an error message is printed.

Examples

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_concat(xyz_, _, N).

See Also

Undefined Predicates, ref-ere.


Send feedback on this subject.