Next: , Previous: , Up: mpg-bpr   [Contents][Index]


11.3.245 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.

Exceptions

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

Examples

The following clause gives the same behavior 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, ref-lps-flg.


Send feedback on this subject.