1% File: /opt/PrologMUD/pack/logicmoo_base/prolog/logicmoo/util/hook_database.pl
    2:- module(hook_database,[]).    3:- define_into_module(          
    4[ ain/1,
    5  ain0/1,
    6  aina/1,
    7  ainz/1,
    8  ainz_clause/1,
    9  ainz_clause/2,
   10  assert_i/1,
   11  assert_if_new/1,
   12  assert_setting/1,
   13  assert_setting_if_missing/1,
   14  asserta_i/1,
   15  asserta_if_new/1,
   16  asserta_new/1,
   17  assertz_i/1,
   18  assertz_if_new/1,
   19  assertz_new/1,
   20  attributes_equal/3,
   21  baseKB:first_std_provider/3,
   22  call_provider/1,
   23  call_provider/2,
   24  callable_module/2,
   25  clause_asserted/1,
   26  clause_asserted/2,
   27  clause_asserted/3,
   28  clause_asserted_i/1,
   29  clause_asserted_i/2,
   30  clause_asserted_i/3,
   31  clause_b/1,
   32  clause_i/1,
   33  clause_i/2,
   34  clause_i/3,
   35  clause_safe/2,
   36  clause_true/1,
   37  current_module_from/2,
   38  dbreq/1,
   39  erase_safe/2,
   40  eraseall/2,
   41  ereq/1,
   42  expand_to_hb/3,
   43  find_and_call/1,
   44  find_and_call/3,
   45  find_module/2,
   46  found_call/2,
   47  hb_to_clause/3,
   48  if_flag_true/2,
   49  is_visible_module/1,
   50  module_of/3,
   51  modulize_head/2,
   52  modulize_head_fb/4,
   53  mpred_mop/3,
   54  mpred_op_prolog/2,
   55  mpred_split_op_data/3,
   56  my_module_sensitive_code/1,
   57  pain/1,
   58  paina/1,
   59  painz/1,
   60  remove_term_attr_type/2,
   61  retract_eq/1,
   62  retract_i/1,
   63  retractall_i/1,
   64  safe_univ/2,
   65  safe_univ0/2,
   66  simple_var/1,
   67  somehow_callable/1,
   68  std_provider/3,
   69  suggest_m/1]).

Utility LOGICMOO HOOK DATABASE

This module allows for the following functions: when prolog data changes in database this allows for attribute variables to be put in the data base. Logicmoo_attvar_serializer uses this to add and remove attribute variable in database. It also allows PFC to have more elaborate database interaction.

and Utility LOGICMOO_HOOK_HYBRID Allows one to intercept asserts and retracks to the prolog database. Implement clauses that are not really in the prolog database act as if they are.

author
- Douglas R. Miles
license
- LGPL */
   82:- set_module(class(library)).   83/*
   84% TODO % :- meta_predicate clause_asserted_i(*).
   85% TODO % :- meta_predicate
   86        ain(*),
   87        ain0(*),
   88        pain(*),
   89        paina(*),
   90        painz(*),
   91        aina(*),
   92        ainz(*),
   93        ainz_clause(*),
   94        ainz_clause(*, ?),
   95        expand_to_hb(?, ?, ?),
   96        assert_if_new(*),
   97        asserta_if_new(:),
   98        asserta_new(*),
   99        assertz_if_new(*),
  100        call_provider(*),
  101        clause_asserted(*),
  102        clause_asserted(*, ?),
  103        clause_asserted(*, ?, -),
  104        clause_safe(?, ?),
  105        eraseall(+, +),
  106        find_and_call(*),
  107        find_and_call(+, +, ?),
  108        module_of(+,+,?),
  109        callable_module(*,-),
  110        find_module(+, ?),
  111        mpred_mop(+, *, ?),
  112        mpred_op_prolog(?, *),
  113        mpred_op_prolog0(*, *),
  114        my_module_sensitive_code(?).
  115*/
  116:- module_transparent
  117  ain/1,
  118  ain0/1,
  119  aina/1,
  120  ainz/1,
  121  ainz_clause/1,
  122  ainz_clause/2,
  123  assert_i/1,
  124  assert_if_new/1,
  125  assert_setting/1,
  126  assert_setting_if_missing/1,
  127  asserta_i/1,
  128  asserta_if_new/1,
  129  asserta_new/1,
  130  assertz_i/1,
  131  assertz_if_new/1,
  132  assertz_new/1,
  133  attributes_equal/3,
  134  baseKB:first_std_provider/3,
  135  call_provider/1,
  136  call_provider/2,
  137  callable_module/2,
  138  clause_asserted/1,
  139  clause_asserted/2,
  140  clause_asserted/3,
  141  clause_asserted_i/1,
  142  clause_asserted_i/2,
  143  clause_asserted_i/3,
  144  clause_b/1,
  145  clause_i/1,
  146  clause_i/2,
  147  clause_i/3,
  148  clause_safe/2,
  149  clause_true/1,
  150  current_module_from/2,
  151  dbreq/1,
  152  erase_safe/2,
  153  eraseall/2,
  154  ereq/1,
  155  expand_to_hb/3,
  156  find_and_call/1,
  157  find_and_call/3,
  158  find_module/2,
  159  hb_to_clause/3,
  160  if_flag_true/2,
  161  is_visible_module/1,
  162  module_of/3,
  163  modulize_head/2,
  164  modulize_head_fb/4,
  165  mpred_mop/3,
  166  mpred_op_prolog/2,
  167  mpred_split_op_data/3,
  168  my_module_sensitive_code/1,
  169  pain/1,
  170  paina/1,
  171  painz/1,
  172  remove_term_attr_type/2,
  173  retract_eq/1,
  174  retract_i/1,
  175  retractall_i/1,
  176  safe_univ/2,
  177  safe_univ0/2,
  178  simple_var/1,
  179  somehow_callable/1,
  180  std_provider/3,
  181  suggest_m/1.  182
  183% % % OFF :- system:reexport(library(logicmoo/clause_attvars)).
  184% % % OFF :- system:reexport(library(logicmoo/lockable_vars)).
  185
  186:-multifile(baseKB:first_std_provider/3).  187:-dynamic(baseKB:first_std_provider/3).  188:-multifile(baseKB:next_std_provider/2).  189:-dynamic(baseKB:next_std_provider/2).  190
  191baseKB:first_std_provider(_,_,mpred_op_prolog).
  192
  193% TODO % :- meta_predicate clause_safe(*, ?).
  194:- module_transparent clause_safe/2.  195:- export(clause_safe/2).  196
  197
  198% TODO % :- meta_predicate my_module_sensitive_code(?).
  199
  200%= 	 	 
 my_module_sensitive_code(?E) is semidet
My Module Sensitive Code.
  206my_module_sensitive_code(_E):- source_context_module(CM),writeln(source_context_module=CM).
 clause_safe(?H, ?B) is semidet
Clause Safely Paying Attention To Corner Cases.
  214clause_safe(H,B):-predicate_property(H,number_of_clauses(C)),C>0,system:clause(H,B).
  215
  216% TODO % :- meta_predicate(if_flag_true(+,:)).
  217if_flag_true(TF,Goal):-
  218  (current_prolog_flag(TF,F) -> 
  219    (F\=(false) -> find_and_call(Goal); true);
  220   (find_and_call(TF)->find_and_call(Goal);true)).
  221
  222/*
  223if_flag_true(TF,Goal):-
  224  (somehow_callable(TF)-> 
  225    (find_and_call(TF)->find_and_call(Goal);true);
  226  (current_prolog_flag(TF,F) -> 
  227    (F\=false -> find_and_call(Goal); true);
  228   trace_or_throw(if_flag_true(TF,Goal)))).
  229*/
  230
  231:- export(mpred_op_prolog/2).  232:- module_transparent(mpred_op_prolog/2).  233% mpred_op_prolog(P):-mpred_split_op_data(P,OP,Term),mpred_op_prolog(OP,Term).
  234
  235
  236%= 	 	 
 mpred_split_op_data(?OP, ?O, ?P) is semidet
Managed Predicate Split Oper. Data.
  242mpred_split_op_data(M:OP,M:O,P):-sanity(compound(OP)),OP=..[O,P],!.
  243mpred_split_op_data(M:OP,M:call,OP):-!.
  244mpred_split_op_data(OP,O,P):-sanity(compound(OP)),OP=..[O,P],!.
  245mpred_split_op_data(OP,call,OP).
  246
  247
  248
  249% mpred_mop(OP,CALL):- sanity(not_ftVar(OP)),fail.
  250:- export(mpred_mop/3).  251% TODO % :- meta_predicate mpred_mop(+,1,?).
  252
  253%= 	 	 
 mpred_mop(+M, :PRED1Op, ?Term) is semidet
Managed Predicate Mop.
  259mpred_mop(M,C:call,CALL):-!,find_and_call(C,M,CALL).
  260mpred_mop(M,C:Op,Term):-!,append_term(Op,Term,CALL),find_and_call(C,M,CALL).
  261mpred_mop(M,Op,Term):-append_term(Op,Term,CALL),find_and_call(M,M,CALL).
  262mpred_mop(M,call,CALL):-!,find_and_call(M,M,CALL).
  263mpred_mop(M,Op,Term):-append_term(Op,Term,CALL),find_and_call(M,M,CALL).
  264
  265
  266:-meta_predicate(cp2(0)).  267cp2(G):-loop_check_early(G,G).
  268
  269:-meta_predicate(found_call(+,*)).  270found_call(C,G):- functor(G,F,A), import(C:F/A), call(G).
  271%found_call(C,G):- functor(G,F,A), import(C:F/A), on_x_debug(loop_check_early(C:call(G),cp2(C:G))).
 find_and_call(+OUT1, +C, ?G) is semidet
Find And Call.
  277:-meta_predicate(find_and_call(+,+,?)).  278find_and_call(_,_,C:G):-current_predicate(_,C:G),!,found_call(C,G).
  279find_and_call(_,C,  G):-current_predicate(_,C:G),!,found_call(C,G).
  280find_and_call(C,_,  G):-current_predicate(_,C:G),!,found_call(C,G).
  281find_and_call(_,_,  G):-current_predicate(_,C:G),!,found_call(C,G).
  282find_and_call(C,M,  G):-dtrace,C:on_x_debug(M:G).
  283
  284
  285current_module_ordered_here(M):- context_module(M).
  286current_module_ordered_here(M):- prolog_load_context(module,M).
  287current_module_ordered_here(M):- '$current_typein_module'(M).
  288
  289current_module_ordered_all(O,M):- no_repeats(O,current_module_ordered_here(O)),import_module(O,M).
  290current_module_ordered_all(user).
  291current_module_ordered_all(baseKB).
  292current_module_ordered_all(X):-current_module(X).
  293
  294
  295current_module_ordered(X):- no_repeats(X,current_module_ordered_all(X)).
  296
  297%= 	 	 
 find_and_call(:TermG) is semidet
Find And Call.
  304find_and_call(G):-current_predicate(_,G),call(G).%!,on_x_debug(loop_check_early(G,cp2(G))).
  305find_and_call(C:G):-current_predicate(_,C:G),!,found_call(C,G).
  306find_and_call(M:G):-current_predicate(_,R:G),!,M:found_call(R,G).
  307find_and_call(G):-current_predicate(_,R:G),!,found_call(R,G).
  308
  309module_of(O,G,M):-predicate_property(O:G,imported_from(M)),!.
  310module_of(M,G,M):-predicate_property(M:G,defined), \+ predicate_property(M:G,imported_from(_)).
  311
  312find_module(G,R):- strip_module(G,M,P),module_of(M,P,R),!.
  313find_module(G,M):- current_module_ordered(C),module_of(C,G,M),!.
  314
  315callable_module(G,R):- strip_module(G,R,P),predicate_property(R:P,defined),!.
  316callable_module(G,R):- strip_module(G,_,P),current_module_ordered(R),predicate_property(R:P,defined),!.
  317callable_module(G,R):- strip_module(G,M,P),module_of(M,P,R).
 somehow_callable(:TermG) is semidet
Detects if find_and_call/1 will be able to call the term
  323somehow_callable(G):-current_predicate(_,G),!.
  324somehow_callable(_:G):-!,current_predicate(_,_:G),!.
  325somehow_callable(G):-current_predicate(_,_:G),!.
  326
  327
  328%= 	 	 
 ain0(?N) is semidet
Assert If New Primary Helper.
  334ain0(N):-quietly(clause_asserted(N))->true;mpred_op_prolog(assert,N).
  335
  336:- export(mpred_op_prolog/2).  337:- module_transparent(mpred_op_prolog/2).  338% TODO % :- meta_predicate mpred_op_prolog(?,*).
  339
  340%= 	 	 
 mpred_op_prolog(?UPARAM1, ?N) is semidet
Managed Predicate Oper. Prolog.
  346:- module_transparent(fix_m/2).  347fix_m(N0,M:N):- strip_module(N0,M,N).
  348mpred_op_prolog(ain0,N0):- !,fix_m(N0,N),(quietly(clause_asserted(N))->true;mpred_op_prolog0(assert,N)).
  349mpred_op_prolog(paina,N0):- !,fix_m(N0,N),(quietly(clause_asserted(N))->true;mpred_op_prolog0(asserta,N)).
  350mpred_op_prolog(painz,N0):- !,fix_m(N0,N),(quietly(clause_asserted(N))->true;mpred_op_prolog0(assertz,N)).
  351mpred_op_prolog(pain,N0):- !,fix_m(N0,N),(quietly(clause_asserted(N))->true;mpred_op_prolog0(assert,N)).
  352mpred_op_prolog(aina,N0):- !,fix_m(N0,N),(clause_asserted(N)->true;mpred_op_prolog0(asserta,N)).
  353mpred_op_prolog(ainz,N0):- !,fix_m(N0,N),(clause_asserted(N)->true;mpred_op_prolog0(assertz,N)).
  354mpred_op_prolog(ain,N0):-  !,fix_m(N0,N),(clause_asserted(N)->true;mpred_op_prolog0(assert,N)).
  355% mpred_op_prolog(OP,M:Term):- unnumbervars(Term,Unumbered),Term \=@= Unumbered,!,dtrace,mpred_mop(M,OP,Unumbered).
  356mpred_op_prolog(OP,M:Term):-  dtrace,!,mpred_mop(M, OP,Term).
  357mpred_op_prolog(OP,M:Term):- 
  358  copy_term(Term, Copy, Gs),
  359  (Gs==[] -> mpred_mop(M,OP,Term);
  360    show_call(why,(
  361      expand_to_hb(Copy,H,B),conjoin(maplist(call,Gs),B,NB),dtrace,mpred_mop(M,OP,(H:-NB))))).
  362  
  363
  364%= 	 	 
 mpred_op_prolog0(:PRED1OP, ?MTerm) is semidet
Managed Predicate Oper. Prolog Primary Helper.
  370mpred_op_prolog0(OP,MTerm):- call(OP,MTerm).
  371
  372% peekAttributes/2,pushAttributes/2,pushCateElement/2.
  373:- module_transparent((aina/1,ain/1,ainz/1,ain0/1,ainz_clause/1,ainz_clause/2,clause_asserted/2,expand_to_hb/3,clause_asserted/1,eraseall/2)).  374:- module_transparent((asserta_new/1,asserta_if_new/1,assertz_new/1,assertz_if_new/1,assert_if_new/1)). % ,assertz_if_new_clause/1,assertz_if_new_clause/2,clause_asserted/2,expand_to_hb/2,clause_asserted/1,eraseall/2)).
  375
  376% TODO % :- meta_predicate paina(*),pain(*),painz(*),ain0(*),ainz_clause(*),ainz_clause(*,?).
  377% TODO % :- meta_predicate clause_asserted(*,?),expand_to_hb(?,?,?),clause_asserted(*),eraseall(+,+).
  378
  379% aina(NEW):-ignore((system:retract(NEW),fail)),system:asserta(NEW).
  380% ainz(NEW):-ignore((system:retract(NEW),fail)),system:assertz(NEW).
  381% aina(_Ctx,NEW):-ignore((system:retract(NEW),fail)),system:asserta(NEW).
  382% writeqnl(_Ctx,NEW):- fmt('~q.~n',[NEW]),!.
  383
  384
  385%= 	 	 
 eraseall(+F, +A) is semidet
Eraseall.
  391eraseall(M:F,A):-!,forall((current_predicate(M:F/A),functor_catch(C,F,A)),forall(system:clause(M:C,B,X),erase_safe(system:clause(M:C,B,X),X))).
  392eraseall(F,A):-forall((current_predicate(M:F/A),functor_catch(C,F,A)),forall(system:clause(M:C,B,X),erase_safe(system:clause(M:C,B,X),X))).
  393
  394
  395%= 	 	 
 mpred_provider(?OP, ?Term, ?PROVIDER) is semidet
Hook To [std_provider/3] For Module Logicmoo_util_database. Managed Predicate Provider.
  402:-thread_local(t_l:std_provider_asserted/3).  403:-thread_local(t_l:current_std_provider/1).  404std_provider(OP,Term,PROVIDER):- t_l:std_provider_asserted(OP,Term,PROVIDER).
  405std_provider(_,_,PROVIDER):- t_l:current_std_provider(PROVIDER).
  406std_provider(OP,Term,PROVIDER):- baseKB:first_std_provider(OP,Term,PROVIDER).
  407
  408
  409% TODO % :- meta_predicate call_provider(?).
  410
  411%= 	 	 
 call_provider(?P) is semidet
Call Provider.
  417call_provider(P):-mpred_split_op_data(P,OP,Term),call_provider(OP,Term).
  418
  419
  420%= 	 	 
 call_provider(?OP, ?Term) is semidet
Call Provider.
  426call_provider(OP,Term):- must(std_provider(OP,Term,PROVIDER)),!,call(PROVIDER,OP,Term).
  427
  428call_provider(OP,Term):- must(std_provider(OP,Term,PROVIDER)),!,
  429   (loop_check_early(call(PROVIDER,OP,Term),fail)*->true;
  430   (loop_check_early(must(baseKB:next_std_provider(PROVIDER,NEXT)),NEXT=mpred_op_prolog),!,PROVIDER\=NEXT,call(NEXT,OP,Term))).
  431
  432
  433
  434% TODO % :- meta_predicate assert_setting(:).
  435%% assert_setting( ?X) is semidet.
  436assert_setting(M:P):-functor(P,_,A),dupe_term(P,DP),setarg(A,DP,_),system:retractall(M:DP),system:asserta(M:P).
  437% TODO % :- meta_predicate assert_setting_if_missing(:).
  438assert_setting_if_missing(M:P):-functor(P,_,A),dupe_term(P,DP),setarg(A,DP,_),(system:clause(M:DP,_)->true;system:asserta(M:P)).
 assert_if_new(?X) is semidet
Assert If New.

TODO % :- meta_predicate assert_if_new(:).

  446assert_if_new(X):-mpred_op_prolog(pain,X).
  447% TODO % :- meta_predicate asserta_if_new(*).
  448
  449%= 	 	 
 asserta_if_new(?X) is semidet
Asserta If New.

TODO % :- meta_predicate assertz_if_new(:).

  456asserta_if_new(X):-mpred_op_prolog(paina,X).
  457
  458%= 	 	 
 assertz_if_new(?X) is semidet
Assertz If New.

TODO % :- meta_predicate asserta_new(:).

  465assertz_if_new(X):-mpred_op_prolog(painz,X).
  466
  467
  468%= 	 	 
 asserta_new(?X) is semidet
Asserta New.

TODO % :- meta_predicate asserta_new(*).

  475asserta_new(X):-mpred_op_prolog(paina,X).
  476
  477%= 	 	 
 assertz_new(?X) is semidet
Assertz New.
  483assertz_new(X):-mpred_op_prolog(painz,X).
  484
  485
  486%= 	 	 
 pain(?N) is semidet
Pain.
  492pain(N):- call_provider(pain(N)).
  493
  494%= 	 	 
 paina(?N) is semidet
Paina.
  500paina(N):-call_provider(paina(N)).
  501
  502%= 	 	 
 painz(?N) is semidet
Painz.
  508painz(N):-call_provider(painz(N)).
  509
  510
  511:-multifile(ain/1).  512:-multifile(aina/1).  513:-multifile(ainz/1).  514:-module_transparent(ain/1).  515:-module_transparent(aina/1).  516:-module_transparent(ainz/1).  517:-dynamic(ain/1).  518:-dynamic(aina/1).  519:-dynamic(ainz/1).  520
  521%= 	 	 
 ain(?N) is semidet
Assert If New.
  527ain(N):- call_provider(pain(N)).
  528
  529%= 	 	 
 aina(?N) is semidet
Aina.
  535aina(N):-call_provider(paina(N)).
  536
  537%= 	 	 
 ainz(?N) is semidet
Ainz.
  543ainz(N):-call_provider(painz(N)).
  544
  545
  546%= 	 	 
 ainz_clause(?C) is semidet
Ainz Clause.
  552ainz_clause(C):- notrace(expand_to_hb(C,H,B)),ainz_clause(H,B).
  553
  554%= 	 	 
 ainz_clause(?H, ?B) is semidet
Ainz Clause.
  560ainz_clause(H,B):- clause_asserted(H,B)->true;call_provider(system:assertz((H:-B))).
 expand_to_hb(?Clause, ?H, ?B) is semidet
Split a Head+Body from Clause.
  568expand_to_hb( H,  H,  true) :- var(H),!.
  569% expand_to_hb( Var, H, B):- var(Var),!,dmsg(warn(expand_to_hb( Var, H, B))), when(nonvar(Var),expand_to_hb( Var, H, B)).
  570expand_to_hb(M:(M2:H :- B),HH,BB):-M==M2,!,expand_to_hb((M:H :- B),HH,BB).
  571%expand_to_hb((M:H :- B),M:H,B):-!.
  572expand_to_hb((H :- B),H,B):-!.
  573expand_to_hb( M:HB,  M:H,B):- !,expand_to_hb(HB,H,B).
  574expand_to_hb(  ~(HB),   ~(H),B):- !,expand_to_hb(HB,H,B).
  575expand_to_hb( H,  H,  true).
 is_visible_module(+Op) is semidet
Is a stripped Module (Meaning it will be found via inheritance)
  584is_visible_module(A):-var(A),!,fail.
  585is_visible_module(user).
  586is_visible_module(system).
  587is_visible_module(Inherited):-'$current_source_module'(E), default_module(E,Inherited).
  588%is_visible_module(Inherited):-'$current_typein_module'(E), default_module(E,Inherited).
  589is_visible_module( baseKB).
  590
  591
  592simple_var(Var):- var(Var),\+ attvar(Var).
  593
  594to_mod_if_needed(M,B,MB):- B==true-> MB=B ; MB = M:B.
 hb_to_clause(?H, ?B, ?Clause) is semidet
Join a Head+Body To Clause.
  600hb_to_clause(H,B,H):- B==true,!.
  601hb_to_clause(M:(H:-B1),B2,(M:H:- (B2,B1))):-!.
  602hb_to_clause((H:-B1),B2,(H:- (B2,B1))):-!.
  603hb_to_clause(H,B,(H:-B)).
  604
  605
  606:-export(clause_asserted/1).  607:-meta_predicate(clause_asserted(*)).  608
  609%= 	 	 
 clause_asserted(?C) is semidet
Clause Asserted.
  615clause_asserted(C):- notrace(expand_to_hb(C,H,B)),clause_asserted(H,B).
  616
  617:-export(clause_asserted/2).  618:-meta_predicate(clause_asserted(*,?)).  619
  620%= 	 	 
 clause_asserted(?H, ?B) is semidet
Clause Asserted.
  626clause_asserted(H,B):-clause_asserted(H,B,_).
  627
  628:-export(clause_asserted/3).  629:-meta_predicate(clause_asserted(*,?,-)).  630
  631%= 	 	 
 clause_asserted(?M, ?B, -R) is semidet
Clause Asserted.
  637clause_asserted(H,B,R):- notrace((copy_term(v(H,B),HB),clause(H,B,R),variant(v(H,B),HB))).
  638
  639% TODO % :- meta_predicate(clause_asserted1( :,?,?)).
  640clause_asserted1(M:H,B,R):-
  641   functor(H,F,A),functor(HH,F,A),
  642   freeze(BB,(HH:BB=@=H:B)),
  643   clause(M:HH,BB,R).
  644
  645/*
  646
  647clause_asserted_test(M:H,B,R):- freeze(BB,(HH:BB=@=H:B)),clause(M:HH,BB,R).
  648
  649:- asserta(foo(A):-bar(A)).
  650
  651clause_asserted_test(foo(A),bar(A),R).
  652
  653*/
  654
  655
  656:-meta_predicate(modulize_head(?,?)).  657
  658current_module_from(Cm,M):- default_module(Cm,M).
  659current_module_from(Cm,M):- current_module_ordered(M), \+ default_module(Cm,M).
 modulize_head(+HEAD, -ModulePlusHead) is semidet
Modulize Head.
  665modulize_head(MH,M:H):- strip_module(MH,Cm,H),!,
  666  modulize_head_fb(Cm,H,Cm,M:H).
  667
  668modulize_head_fb(From,H,Fallback,M:H):- 
  669 quietly((findall(M:H,
  670  ((no_repeats(M, ((current_module_from(From,M),current_predicate(_,M:H),\+ predicate_property(M:H,imported_from(_))))))->true;
  671  M=Fallback),List))),
  672 member(M:H,List).
  673
  674
  675
  676% % % OFF :- system:reexport(library(logicmoo/util_varnames)).
 clause_asserted_i(+Head) is semidet
PFC Clause For User Interface.
  683clause_asserted_i(Head):- clausify_attributes(Head,HeadI),!,clause_asserted(HeadI).
  684clause_asserted_i(Head):- 
  685  \+ \+ ((
  686  % fully_expand_now_wte(assert,Head,HeadC),
  687  copy_term(Head,HC),
  688  copy_term_nat(Head,Head_copy),
  689  % find a unit system:clause identical to Head by finding one which unifies,
  690  clause_i(Head_copy),
  691  % and then checking to see if it is identical
  692  term_attvars(v(Head,Head_copy,HC),Vars),maplist(del_attr_type(vn),Vars),
  693  =@=(Head,HC),
  694  variant(Head,Head_copy))),!.
  695
  696
  697clause_asserted_i(H,B):- clause_asserted_i(H,B,_).
  698clause_asserted_i(MH,B,R):- ground(MH:B),!, system:clause(MH,B,R),system:clause(MHR,BR,R),ground(MHR:BR).
  699clause_asserted_i(MH,B,R):- copy_term(MH:B,MHB),clause_i(MH,B,R),variant(MH:B,MHB).
  700
  701
  702
  703
  704put_clause_ref(_Ref,_V):- !.
  705put_clause_ref(Ref,V):- !, nop(dmsg(put_clause_ref(Ref,V))).
  706put_clause_ref(Ref,V):-put_attr(V,cref,Ref).
  707
  708remove_term_attr_type(Term, Mod):- is_list(Mod),!,maplist(remove_term_attr_type(Term),Mod).
  709remove_term_attr_type(Term, Mod):- quietly((term_attvars(Term,AVs),term_attvars(AVs,AVs2),maplist(del_attr_type(Mod),AVs2))).
  710
  711:- op(700,xfx,'=@=').  712
  713
  714attribute_is_info(name_variable(_Var,  _Name)).
  715attribute_is_info(put_attrs(_Var, vn, _Name)).
  716
  717attributes_equal(R,L,Attribs):-R=@=L,!,Attribs = R.
  718attributes_equal([INFO|L],R,TODO):- attribute_is_info(INFO),INFO,!,delete(R,INFO,RR),attributes_equal(L,RR,TODO).
  719attributes_equal(R,[INFO|L],TODO):- attribute_is_info(INFO),INFO,!,delete(R,INFO,RR),attributes_equal(L,RR,TODO).
  720
  721attributes_equal(L,R,[H|TODO]):- select(H,L,LL), select(HH,R,RR),H==HH,!,
  722    delete(LL,HH,LLL),delete(RR,H,RRR),attributes_equal(LLL,RRR,TODO).
  723attributes_equal(L,R,[H|TODO]):- select(H,L,LL), select(HH,R,RR),H =HH,!,
  724    delete(LL,HH,LLL),delete(RR,H,RRR),attributes_equal(LLL,RRR,TODO).
 clause_i(?H, ?B, ?Ref) is semidet
Clause For Internal Interface.
  731clause_i(HB):- notrace(expand_to_hb(HB,H,B))->clause_i(H,B,_).
  732clause_i(H,B):- clause_i(H,B,_).
  733
  734% TODO track which predicate have attributeds vars
  735clause_i(H0,B0,Ref):- notrace(( \+ current_prolog_flag(assert_attvars,true))) ,!, system:clause(H0,B0,Ref).
  736clause_i(H0,B0,Ref):- clause_attv(H0,B0,Ref).
  737
  738:- multifile(pfc_lib:check_never_assert/1).  739:- dynamic(pfc_lib:check_never_assert/1).  740:- pfc_lib:export(pfc_lib:check_never_assert/1).  741:- system:import(pfc_lib:check_never_assert/1).  742%:- multifile(system:check_never_assert/1).
  743%:- dynamic(system:check_never_assert/1).
  744
  745notrace_check_never_assert(X):- notrace(check_never_assert(X)),check_never_assert(X).
  746
  747assert_i(X):- notrace_check_never_assert(X),fail.
  748assert_i(HB):- clausify_attributes(HB,CL),assert(CL).
  749
  750asserta_i(X):- notrace_check_never_assert(X),fail.
  751asserta_i(HB):-clausify_attributes(HB,CL),system:asserta(CL).
  752
  753assertz_i(X):- notrace_check_never_assert(X),fail.
  754assertz_i(HB):-clausify_attributes(HB,CL),system:assertz(CL).
  755
  756retract_i(HB):- notrace(expand_to_hb(HB,H,B)), (clause_i(H,B,Ref)*->erase(Ref)).
  757retractall_i(H):-expand_to_hb(H,HH,_),forall(clause_i(HH,_,Ref),erase(Ref)).
  758
  759
  760:- dynamic(ereq/1).  761:- module_transparent(ereq/1).  762%ereq(mtHybrid(H)):- !, baseKB:mtHybrid(H).
  763ereq(C):- find_and_call(C).
  764
  765:- dynamic(dbreq/1).  766:- module_transparent(dbreq/1).  767dbreq(C):- ereq(C).
  768
  769
  770
  771:- module_transparent(predicate_property_safe/2).  772predicate_property_safe(P,PP):- quietly(predicate_property(P,PP)).
 clause_b(?C) is semidet
Clause User Microtheory.
  779pfc_with_quiet_vars_lock(G):- call(G).
  780
  781% TODO % :- meta_predicate(clause_b(*)).
  782clause_b(M:Goal):- !, pfc_with_quiet_vars_lock((M:clause(Goal,B))),M:call(B).
  783clause_b(Goal):- pfc_with_quiet_vars_lock((clause(Goal,B),call(B))*->true;clause_b(baseKB:Goal)).
  784
  785% lookup_u/cheaply_u/call_u/clause_b
  786%clause_b(Goal):-  baseKB:call(call,Goal).
  787%clause_b(M:Goal):- !, (clause(M:Goal,true);clause(Goal,true)).
  788
  789
  790%clause_b(Goal):-  baseKB:clause(Goal,B)*->call(B);clause_b0(Goal).
  791%clause_b(Goal):-  baseKB:clause(Goal,true)*->true;clause_b0(Goal).
  792
  793% clause_b0(Goal):- if_defined(to_was_isa(clause_b,Goal,P0),fail),!,Goal\=P0,baseKB:clause(P0,true).
  794
  795%clause_b(M:C):-!,clause(M:C,true).
  796%clause_b(C):- call_u(clause(C,true)).
  797%clause_b(C):-!,clause(_:C,true).
  798%clause_b(Goal):-  Goal=..[C,PART],!,baseKB:t(C,PART).
  799%clause_b(Goal):-  current_predicate(_,baseKB:Goal),!,loop_check(baseKB:Goal).
  800% clause_b(Goal):- clause(baseKB:Goal,Body),(Body==true->true;call_u(Body)).
  801
  802%= 	 	 
 clause_true(?G) is semidet
Clause True.

TODO % :- meta_predicate(clause_true(*)).

  809:- module_transparent(clause_true/1).  810clause_true(G):- !, clause_b(G).
  811
  812clause_true(M:G):-!,pfc_with_quiet_vars_lock((system:clause(M:G,true)*->true;(current_module_ordered(M2),system:clause(M2:G,true)))).
  813clause_true(G):- pfc_with_quiet_vars_lock((quietly((current_module_ordered(M), \+ \+  system:clause(M:G,_,_))),!, system:clause(M:G,true))).
  814%clause_true(M:G):- predicate_property(M:G,number_of_clauses(_)),!,system:clause(M:G,true).
  815%clause_true(_:G):-!,predicate_property(M:G,number_of_clauses(_)),system:clause(M:G,true).
  816%clause_true(G):-!,predicate_property(M:G,number_of_clauses(_)),system:clause(M:G,true).
  817
  818clause_true_anywhere(G):- strip_module(G,M,S),!,
  819  functor(S,F,A),
  820  functor(P,F,A),
  821  ((M2=M; M2=baseKB ;(current_module_ordered(M2),M2\=M)),
  822    current_predicate(M2:P)),!,
  823    pfc_with_quiet_vars_lock(system:clause(M2:S,B,Ref)),
  824     (B==true->! ;
  825    (clause_property(Ref,module(M22));M22=M2),!,call(M22:B)).
  826
  827:- export(current_assertion_module/1).  828current_assertion_module(M):- if_defined(defaultAssertMt(M),M=baseKB).
  829suggest_m(M):- prolog_load_context(module, M),!.
  830suggest_m(M):- (if_defined(defaultAssertMt(M),fail);current_module_ordered(M)),is_visible_module(M).
  831
  832
  833:-export(retract_eq/1).  834
  835%= 	 	 
 retract_eq(?HB) is semidet
Retract Using (==/2) (or =@=/2) ).
  841retract_eq(HB):-expand_to_hb(HB,H,B),show_failure(modulize_head(H,MH)),clause_asserted(MH,B,Ref),erase(Ref).
  842
  843
  844:-export(safe_univ/2).  845
  846%= 	 	 
 safe_univ(?Call, ?List) is semidet
Safely Paying Attention To Corner Cases Univ.
  852safe_univ(SCall,Univ):-string(SCall),!,maybe_notrace(atom_string(Call,SCall)),[Call]=Univ.
  853safe_univ(Call,List):- quietly(safe_univ0(Call,List)),!.
  854
  855
  856%= 	 	 
 safe_univ0(?M, :TermN) is semidet
Safely Paying Attention To Corner Cases Univ Primary Helper.
  862safe_univ0(M:Call,[N:L|List]):- nonvar(M),nonvar(N),!,safe_univ0(Call,[L|List]).
  863safe_univ0(M:Call,[N:L|List]):- nonvar(N),var(M),N=M,!,safe_univ(Call,[L|List]).
  864safe_univ0(Call,[M:L|List]):- nonvar(M),!,safe_univ(Call,[L|List]).
  865safe_univ0(M:Call,[L|List]):- nonvar(M),!,safe_univ(Call,[L|List]).
  866safe_univ0(Call,[L|List]):- not(is_list(Call)),sanity(atom(L);compound(Call)), Call =..[L|List],!,warn_bad_functor(L).
  867safe_univ0([L|List],[L|List]):- var(List),\+ atom(L),!,rtrace,_ =.. [L|List],warn_bad_functor(L).
  868safe_univ0(Call,[L|List]):- sanity(atom(L);compound(Call)),catchv(Call =.. [L|List],E,(dumpST,'format'('~q~n',[E=safe_univ(Call,List)]))),warn_bad_functor(L).
  869
  870
  871/*
  872
  873%% bad_functor( ?L) is semidet.
  874%
  875% Bad Functor.
  876%
  877bad_functor(L) :- arg(_,v('|','.',[],':','/'),L).
  878
  879:- export(warn_bad_functor/1).
  880
  881%=
  882
  883%% warn_bad_functor( ?L) is semidet.
  884%
  885% Warn Bad Functor.
  886%
  887warn_bad_functor(L):-ignore((quietly(bad_functor(L)),!,dtrace,call(ddmsg(bad_functor(L))))).
  888
  889
  890%= 	 	 
  891*/
  892
  893:-export(erase_safe/2).  894
  895%= 	 	 
 erase_safe(?VALUE1, ?REF) is semidet
Erase Safely Paying Attention To Corner Cases.
  901erase_safe(_,REF):-erase(REF).
  902/*
  903erase_safe(((M:A):-B),REF):-!,erase_safe(system:clause(M:A,B),REF).
  904erase_safe(system:clause(U:A,B),REF):-U=user,!, erase_safe(system:clause(A,B),REF).
  905%erase_safe(system:clause(A,U:B),REF):-U=user,!, erase_safe(system:clause(A,B),REF).
  906%erase_safe(system:clause(M:A,B),REF):-!, erase_safe_now(M,system:clause(A,B),REF).
  907erase_safe(system:clause(A,B),REF):-!, erase_safe_now(_,system:clause(A,B),REF).
  908erase_safe(M:(A:-B),REF):-!,erase_safe(system:clause(M:A,B),REF).
  909erase_safe((A:-B),REF):-!,erase_safe(system:clause(A,B),REF).
  910erase_safe(system:clause(A,B,_),REF):-!,erase_safe(system:clause(A,B),REF).
  911erase_safe(system:asserta(A,_),REF):-!,erase_safe(system:clause(A,true),REF).
  912erase_safe(M:A,REF):-M==user,!,erase_safe(A,REF).
  913erase_safe(A,REF):-!,erase_safe(system:clause(A,true),REF).
  914
  915
  916erase_safe_now(_,system:clause(M:A,B),REF):-!,erase_safe_now(M,system:clause(A,B),REF).
  917erase_safe_now(M,system:clause(A,B),REF):-!,
  918   ignore((show_success(erase_safe_now, \+ system:clause(M:A,B, REF)))),
  919   (((var(REF);
  920   show_success(erase_safe_now, \+ nth_clause(A, _Index, REF));   
  921   show_success(erase_safe_now, clause_property(REF,erased));
  922   show_success(erase_safe_now, \+ clause_property(REF,_))))
  923   -> logicmoo_util_catch:ddmsg(warn(var_erase_safe(system:clause(A,B),REF))) ; 
  924       erase(REF)).
  925*/
  926
  927
  928:- ignore((source_location(S,_),prolog_load_context(module,M),module_property(M,class(library)),
  929 forall(source_file(M:H,S),
  930 ignore((functor(H,F,A),
  931  ignore(((\+ atom_concat('$',_,F),(export(F/A) , current_predicate(system:F/A)->true; system:import(M:F/A))))),
  932  ignore(((\+ predicate_property(M:H,transparent), module_transparent(M:F/A), \+ atom_concat('__aux',_,F),debug(modules,'~N:- module_transparent((~q)/~q).~n',[F,A]))))))))).  933
  934:- fixup_exports.