diff --git a/docs/yap.tex b/docs/yap.tex index f475b349f..c93c4dd27 100644 --- a/docs/yap.tex +++ b/docs/yap.tex @@ -5900,6 +5900,19 @@ asserted before being defined. Declares predicate @var{P} or list of predicates [@var{P1},...,@var{Pn}] as a dynamic predicate following either @code{logical} or @code{immediate} semantics. + +@item compile_predicates(:@var{ListOfNameArity}) +@findex compile_predicates/1 +@snindex compile_predicates/1 +@cnindex compile_predicates/1 + +Compile a list of specified dynamic predicates (see @code{dynamic/1} and +@code{assert/1} into normal static predicates. This call tells the +Prolog environment the definition will not change anymore and further +calls to @code{assert/1} or @code{retract/1} on the named predicates +raise a permission error. This predicate is designed to deal with parts +of the program that is generated at runtime but does not change during +the remainder of the program execution. @menu diff --git a/pl/preds.yap b/pl/preds.yap index 2ab2aacbe..6b3f40009 100644 --- a/pl/preds.yap +++ b/pl/preds.yap @@ -1019,3 +1019,35 @@ current_key(A,K) :- ; Pred = Na ). + + +compile_predicates(Ps) :- + '$current_module'(Mod), + '$compile_predicates'(Ps, Mod, compile_predicates(Ps)). + +'$compile_predicates'(V, _, Call) :- + var(V), !, + '$do_error'(instantiation_error,Call). +'$compile_predicates'(M:Ps, _, Call) :- + '$compile_predicates'(Ps, M, Call). +'$compile_predicates'([], _, _). +'$compile_predicates'(P.Ps, M, Call) :- + '$compile_predicate'(P, M, Call). + '$compile_predicates'(Ps, M, Call). + +'$compile_predicate'(P, M, Call) :- + var(P), !, + '$do_error'(instantiation_error,Call). +'$compile_predicate'(M:P, _, Call) :- + '$compile_predicate'(P, M, Call). +'$compile_predicate'(Na/Ar, Mod, _Call) :- + functor(G, Na, Ar), + findall((G.B),clause(Mod:G,B),Cls), + abolish(Mod:Na,Ar), + '$add_all'(Cls, Mod). + +'$add_all'([], _). +'$add_all'((G.B).Cls, Mod) :- + assert_static(Mod:(G:-B)), + '$add_all'(Cls, Mod). +