2014-10-10 10:00:27 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: undefined.yap *
|
|
|
|
* Last rev: 8/2/88 *
|
|
|
|
* mods: *
|
|
|
|
* comments: Predicate Undefined for YAP *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/** @defgroup Undefined_Procedures Handling Undefined Procedures
|
|
|
|
@ingroup YAPControl
|
|
|
|
@{
|
|
|
|
|
|
|
|
A predicate in a module is said to be undefined if there are no clauses
|
|
|
|
defining the predicate, and if the predicate has not been declared to be
|
|
|
|
dynamic. What YAP does when trying to execute undefined predicates can
|
|
|
|
be specified in three different ways:
|
|
|
|
|
|
|
|
|
|
|
|
+ By setting an YAP flag, through the yap_flag/2 or
|
|
|
|
set_prolog_flag/2 built-ins. This solution generalizes the
|
|
|
|
ISO standard by allowing module-specific behavior.
|
|
|
|
+ By using the unknown/2 built-in (this deprecated solution is
|
|
|
|
compatible with previous releases of YAP).
|
|
|
|
+ By defining clauses for the hook predicate
|
|
|
|
`user:unknown_predicate_handler/3`. This solution is compatible
|
|
|
|
with SICStus Prolog.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
/** @pred user:unknown_predicate_handler(+ _Call_, + _M_, - _N_)
|
2014-10-10 10:00:27 +01:00
|
|
|
|
|
|
|
In YAP, the default action on undefined predicates is to output an
|
|
|
|
`error` message. Alternatives are to silently `fail`, or to print a
|
|
|
|
`warning` message and then fail. This follows the ISO Prolog standard
|
|
|
|
where the default action is `error`.
|
|
|
|
|
|
|
|
The user:unknown_predicate_handler/3 hook was originally include in
|
|
|
|
SICStus Prolog. It allows redefining the answer for specifici
|
|
|
|
calls. As an example. after defining `undefined/1` by:
|
|
|
|
|
|
|
|
~~~~~{.prolog}
|
|
|
|
undefined(A) :- format('Undefined predicate: ~w~n',[A]), fail.
|
|
|
|
~~~~~
|
|
|
|
and executing the goal:
|
|
|
|
|
|
|
|
~~~~~{.prolog}
|
|
|
|
:- assert(user:unknown_predicate_handler(U,M,undefined(M:U)) )
|
|
|
|
~~~~~
|
|
|
|
a call to a predicate for which no clauses were defined will result in
|
|
|
|
the output of a message of the form:
|
|
|
|
|
|
|
|
~~~~~{.prolog}
|
2017-06-06 12:47:59 +01:00
|
|
|
Undefined predicate:
|
2014-10-10 10:00:27 +01:00
|
|
|
~~~~~
|
|
|
|
followed by the failure of that call.
|
|
|
|
*/
|
|
|
|
:- multifile user:unknown_predicate_handler/3.
|
|
|
|
|
2017-06-05 13:06:12 +01:00
|
|
|
undefined_query(G0, M0, Cut) :-
|
2019-01-22 19:32:19 +00:00
|
|
|
recorded('$import','$import'(M,M0,G,G0,_,_),_),
|
|
|
|
'$call'(G, Cut, G, M).
|
2017-06-05 13:06:12 +01:00
|
|
|
|
2014-10-10 10:00:27 +01:00
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
/**
|
2017-06-05 13:06:12 +01:00
|
|
|
* @pred '$undefp_search'(+ M0:G0, -MG)
|
2016-01-03 02:06:09 +00:00
|
|
|
*
|
|
|
|
* @param G0 input goal
|
|
|
|
* @param M0 current module
|
|
|
|
* @param G1 new goal
|
|
|
|
*
|
|
|
|
* @return succeeds on finding G1, otherwise fails.
|
|
|
|
*
|
|
|
|
* Tries:
|
|
|
|
* 1 - `user:unknown_predicate_handler`
|
|
|
|
* 2 - `goal_expansion`
|
|
|
|
* 1 - `import` mechanism`
|
|
|
|
*/
|
|
|
|
'$undefp_search'(M0:G0, MG) :-
|
|
|
|
'$pred_exists'(unknown_predicate_handler(_,_,_,_), user),
|
|
|
|
'$yap_strip_module'(M0:G0, EM0, GM0),
|
2018-12-16 02:21:54 +00:00
|
|
|
user:unknown_predicate_handler(GM0,EM0,MG),
|
2018-12-14 14:53:39 +00:00
|
|
|
!.
|
2019-01-09 09:32:09 +00:00
|
|
|
'$undefp_search'(M0:G0, MG) :-
|
2019-03-04 15:49:53 +00:00
|
|
|
'$predicate_definition'(M0:G0, MG), !.
|
2018-01-18 14:47:27 +00:00
|
|
|
|
2019-03-12 10:51:39 +00:00
|
|
|
'$undef_error'(error, Mod:Goal) :-
|
|
|
|
'$do_error'(existence_error(procedure,Mod:Goal), Mod:Goal).
|
|
|
|
'$undef_error'(warning,Mod:Goal) :-
|
|
|
|
'$program_continuation'(PMod,PName,PAr),
|
|
|
|
print_message(warning,error(existence_error(procedure,Mod:Goal), context(Mod:Goal,PMod:PName/PAr))).
|
|
|
|
'$undef_error'(fail,_).
|
|
|
|
|
2019-03-06 10:49:55 +00:00
|
|
|
'$undef_setup'(Action,Debug,Current) :-
|
2019-01-21 21:42:27 +00:00
|
|
|
yap_flag( unknown, Action, fail),
|
2018-12-14 14:53:39 +00:00
|
|
|
yap_flag( debug, Debug, false),
|
2019-03-04 15:49:53 +00:00
|
|
|
'$stop_creeping'(Current).
|
|
|
|
|
2019-02-21 21:03:44 +00:00
|
|
|
'$undef_cleanup'(Action,Debug, _Current) :-
|
2018-12-14 14:53:39 +00:00
|
|
|
yap_flag( unknown, _, Action),
|
2019-02-21 21:03:44 +00:00
|
|
|
yap_flag( debug, _, Debug).
|
2016-07-31 16:39:12 +01:00
|
|
|
|
2019-03-12 10:51:39 +00:00
|
|
|
'$found_undefined_predicate'( M0:G0, M:G ) :-
|
|
|
|
'$pred_exists'(unknown_predicate_handler(_,_,_), user),
|
|
|
|
'$yap_strip_module'(M0:G0, EM0, GM0),
|
|
|
|
user:unknown_predicate_handler(GM0,EM0,M:G),
|
|
|
|
!.
|
|
|
|
'$found_undefined_predicate'( M0:G0, _ ) :-
|
|
|
|
yap_flag( unknown, _, Action),
|
|
|
|
'$undef_error'(Action, M0:G0 ).
|
|
|
|
|
|
|
|
'$search_undef'(M0:G0, M:G) :-
|
|
|
|
% make sure we do not loop on undefined predicates
|
|
|
|
setup_call_cleanup(
|
|
|
|
'$undef_setup'(Action,Debug,Current),
|
|
|
|
'$get_undefined_predicate'( M0:G0, M:G ),
|
|
|
|
'$undef_cleanup'(Action,Debug,Current)
|
|
|
|
),
|
|
|
|
!.
|
|
|
|
'$search_undef'(M0:G0, M:G) :-
|
|
|
|
'$found_undefined_predicate'( M0:G0, M:G ).
|
|
|
|
|
|
|
|
%% undef handler:
|
|
|
|
% we found an import, and call again
|
|
|
|
% we have user code in the unknown_predicate
|
|
|
|
% we fail, output a message, and just generate an exception.
|
2019-04-27 17:55:22 +01:00
|
|
|
'$undefp'([M0|G0],_) :-
|
2019-03-12 10:51:39 +00:00
|
|
|
'$search_undef'(M0:G0, M:G),
|
|
|
|
'$trace'(M:G).
|
|
|
|
|
2019-02-21 21:03:44 +00:00
|
|
|
:- abolish(prolog:'$undefp0'/2).
|
2019-01-31 11:54:17 +00:00
|
|
|
:- '$undefp_handler'('$undefp'(_,_), prolog).
|
2016-07-31 16:39:12 +01:00
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
/** @pred unknown(- _O_,+ _N_)
|
|
|
|
|
|
|
|
The unknown predicate, informs about what the user wants to be done
|
2019-01-09 09:32:09 +00:00
|
|
|
when there are no clauses for a predicate. Using unknown/3 is
|
|
|
|
strongly deprecated. We recommend setting the `unknown` prolog
|
|
|
|
flag for generic behaviour, and calling the hook
|
|
|
|
user:unknown_predicate_handler/3 to fine-tune specific cases
|
|
|
|
undefined goals.
|
2016-01-03 02:06:09 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
unknown(P, NP) :-
|
2019-01-31 11:54:17 +00:00
|
|
|
yap_flag( unknown, P, NP ).
|
2014-10-10 10:00:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
@}
|
|
|
|
*/
|