1% File: /opt/PrologMUD/pack/logicmoo_base/prolog/logicmoo/util/hook_database.pl
    2:- module(hook_database,
    3          [ ain/1,
    4            ain0/1,
    5            aina/1,
    6            ainz/1,
    7            
    8            if_flag_true/2,
    9            current_module_from/2,
   10            attributes_equal/3,
   11            
   12          ereq/1,
   13          dbreq/1,
   14            
   15            is_visible_module/1,
   16            hb_to_clause/3,
   17            paina/1,pain/1,painz/1,
   18            modulize_head/2,
   19            remove_term_attr_type/2,
   20            ainz_clause/1,ainz_clause/2,
   21            simple_var/1,
   22            
   23            find_module/2,
   24            module_of/3,
   25            callable_module/2,
   26            expand_to_hb/3,
   27            assert_if_new/1,
   28            asserta_if_new/1,
   29            asserta_new/1,
   30            assertz_if_new/1,
   31            assertz_new/1,
   32            assert_setting/1,
   33            assert_setting_if_missing/1,
   34            call_provider/1,
   35            call_provider/2,
   36            clause_true/1,
   37            modulize_head_fb/4,
   38            clause_asserted/1,clause_asserted/2,clause_asserted/3,
   39            clause_asserted_i/1,clause_asserted_i/2,clause_asserted_i/3,
   40            clause_i/1,clause_i/2,clause_i/3,
   41            assert_i/1,asserta_i/1,assertz_i/1,
   42            retract_i/1,retractall_i/1,
   43            
   44            clause_safe/2,
   45            
   46            erase_safe/2,
   47            eraseall/2,
   48            find_and_call/1,
   49            somehow_callable/1,
   50            find_and_call/3,
   51            std_provider/3,
   52            mpred_mop/3,
   53            mpred_op_prolog/2,
   54            mpred_split_op_data/3,
   55            retract_eq/1,
   56            safe_univ/2,
   57            safe_univ0/2,
   58            my_module_sensitive_code/1
   59          ]).   60
   61:- meta_predicate clause_asserted_i(:).   62:- set_module(class(library)).   63
   64:- meta_predicate
   65        ain(:),
   66        ain0(:),
   67        pain(:),
   68        paina(:),
   69        painz(:),
   70        aina(:),
   71        ainz(:),
   72        ainz_clause(:),
   73        ainz_clause(:, ?),
   74        expand_to_hb(?, ?, ?),
   75        assert_if_new(:),
   76        asserta_if_new(:),
   77        asserta_new(:),
   78        assertz_if_new(:),
   79        call_provider(*),
   80        clause_asserted(:),
   81        clause_asserted(:, ?),
   82        clause_asserted(:, ?, -),
   83        clause_safe(?, ?),
   84        eraseall(+, +),
   85        find_and_call(*),
   86        find_and_call(+, +, ?),
   87        module_of(+,+,?),
   88        callable_module(:,-),
   89        find_module(+, ?),
   90        mpred_mop(+, 1, ?),
   91        mpred_op_prolog(?, :),
   92        mpred_op_prolog0(1,?),
   93        my_module_sensitive_code(?).   94
   95:- module_transparent
   96         find_module/2,
   97         module_of/3,
   98         callable_module/2,
   99            
  100        
  101        modulize_head/2,
  102        modulize_head_fb/4,
  103        my_module_sensitive_code/1,
  104        assertz_new/1,
  105        call_provider/2,
  106        
  107        is_visible_module/1,
  108        clause_asserted/3,
  109        erase_safe/2,
  110        current_module_from/2,
  111        find_and_call/1,
  112        baseKB:first_std_provider/3,
  113        std_provider/3,
  114        mpred_split_op_data/3,
  115        retract_eq/1,
  116        safe_univ/2,
  117        clause_asserted/1,clause_asserted/2,clause_asserted/3,
  118        safe_univ0/2.  119
  120
  121:- module_transparent
  122         ain/1,
  123            ain0/1,
  124            aina/1,
  125            ainz/1,
  126
  127            if_flag_true/2,
  128            current_module_from/2,
  129            attributes_equal/3,            
  130
  131            
  132            is_visible_module/1,
  133            hb_to_clause/3,
  134            paina/1,pain/1,painz/1,
  135            modulize_head/2,
  136            remove_term_attr_type/2,
  137            ainz_clause/1,ainz_clause/2,
  138            simple_var/1,
  139            
  140            expand_to_hb/3,
  141            assert_if_new/1,
  142            asserta_if_new/1,
  143            asserta_new/1,
  144            assertz_if_new/1,
  145            assertz_new/1,
  146            assert_setting/1,
  147            assert_setting_if_missing/1,
  148            call_provider/1,
  149            call_provider/2,
  150            clause_true/1,
  151            modulize_head_fb/4,
  152            clause_asserted/1,clause_asserted/2,clause_asserted/3,
  153            clause_asserted_i/1,clause_asserted_i/2,clause_asserted_i/3,
  154            clause_i/1,clause_i/2,clause_i/3,
  155            assert_i/1,asserta_i/1,assertz_i/1,
  156            retract_i/1,retractall_i/1,
  157
  158
  159            clause_safe/2,
  160            
  161            erase_safe/2,
  162            eraseall/2,
  163            find_and_call/1,
  164            somehow_callable/1,
  165            find_and_call/3,
  166            std_provider/3,
  167            mpred_mop/3,
  168            mpred_op_prolog/2,
  169            mpred_split_op_data/3,
  170            retract_eq/1,
  171            safe_univ/2,
  172            safe_univ0/2,
  173            my_module_sensitive_code/1.  174
  175:- reexport(library(clause_attvars)).  176:- reexport(library(lockable_vars)).  177
  178:-multifile(baseKB:first_std_provider/3).  179:-multifile(baseKB:next_std_provider/3).  180:-dynamic(baseKB:first_std_provider/3).  181:-dynamic(baseKB:next_std_provider/3).  182
  183baseKB:first_std_provider(_,_,mpred_op_prolog).
  184
  185:- meta_predicate clause_safe(:, ?).  186:- module_transparent clause_safe/2.  187:- export(clause_safe/2).  188
  189
  190:- meta_predicate my_module_sensitive_code(?).  191
  192%= 	 	 
 my_module_sensitive_code(?E) is semidet
My Module Sensitive Code.
  198my_module_sensitive_code(_E):- source_context_module(CM),writeln(source_context_module=CM).
  199
  200
  201% clause_safe(M:H,B):-!,predicate_property(M:H,number_of_clauses(_)),system:clause(H,B).
  202% clause_safe(H,B):-predicate_property(_:H,number_of_clauses(_)),system:clause(H,B).
  203
  204%= 	 	 
 clause_safe(?H, ?B) is semidet
Clause Safely Paying Attention To Corner Cases.
  210clause_safe(H,B):-predicate_property(H,number_of_clauses(C)),C>0,system:clause(H,B).
  211
  212:- meta_predicate(if_flag_true(:,:)).  213if_flag_true(TF,Goal):-
  214  (current_prolog_flag(TF,F) -> 
  215    (F\=false -> find_and_call(Goal); true);
  216   (find_and_call(TF)->find_and_call(Goal);true)).
  217
  218/*
  219if_flag_true(TF,Goal):-
  220  (somehow_callable(TF)-> 
  221    (find_and_call(TF)->find_and_call(Goal);true);
  222  (current_prolog_flag(TF,F) -> 
  223    (F\=false -> find_and_call(Goal); true);
  224   trace_or_throw(if_flag_true(TF,Goal)))).
  225*/
  226
  227:- export(mpred_op_prolog/2).  228:- module_transparent(mpred_op_prolog/2).  229% mpred_op_prolog(P):-mpred_split_op_data(P,OP,Term),mpred_op_prolog(OP,Term).
  230
  231
  232%= 	 	 
 mpred_split_op_data(?OP, ?O, ?P) is semidet
Managed Predicate Split Oper. Data.
  238mpred_split_op_data(M:OP,M:O,P):-sanity(compound(OP)),OP=..[O,P],!.
  239mpred_split_op_data(M:OP,M:call,OP):-!.
  240mpred_split_op_data(OP,O,P):-sanity(compound(OP)),OP=..[O,P],!.
  241mpred_split_op_data(OP,call,OP).
  242
  243
  244
  245% mpred_mop(OP,CALL):- sanity(not_ftVar(OP)),fail.
  246:- export(mpred_mop/3).  247:- meta_predicate mpred_mop(+,1,?).  248
  249%= 	 	 
 mpred_mop(+M, :PRED1Op, ?Term) is semidet
Managed Predicate Mop.
  255mpred_mop(M,C:call,CALL):-!,find_and_call(C,M,CALL).
  256mpred_mop(M,C:Op,Term):-!,append_term(Op,Term,CALL),find_and_call(C,M,CALL).
  257mpred_mop(M,Op,Term):-append_term(Op,Term,CALL),find_and_call(M,M,CALL).
  258mpred_mop(M,call,CALL):-!,find_and_call(M,M,CALL).
  259mpred_mop(M,Op,Term):-append_term(Op,Term,CALL),find_and_call(M,M,CALL).
  260
  261
  262:-meta_predicate(cp2(0)).  263cp2(G):-loop_check_early(G,G).
  264
  265:-meta_predicate(found_call(+,*)).  266found_call(C,G):- on_x_debug(loop_check_early(C:call(G),cp2(C:G))).
 find_and_call(+OUT1, +C, ?G) is semidet
Find And Call.
  272:-meta_predicate(find_and_call(+,+,?)).  273find_and_call(_,_,C:G):-current_predicate(_,C:G),!,found_call(C,G).
  274find_and_call(_,C,  G):-current_predicate(_,C:G),!,found_call(C,G).
  275find_and_call(C,_,  G):-current_predicate(_,C:G),!,found_call(C,G).
  276find_and_call(_,_,  G):-current_predicate(_,C:G),!,found_call(C,G).
  277find_and_call(C,M,  G):-dtrace,C:on_x_debug(M:G).
  278
  279
  280current_module_ordered_here(M):- context_module(M).
  281current_module_ordered_here(M):- prolog_load_context(module,M).
  282current_module_ordered_here(M):- '$current_typein_module'(M).
  283
  284current_module_ordered_all(O,M):- no_repeats(O,current_module_ordered_here(O)),import_module(O,M).
  285current_module_ordered_all(user).
  286current_module_ordered_all(baseKB).
  287current_module_ordered_all(X):-current_module(X).
  288
  289
  290current_module_ordered(X):- no_repeats(X,current_module_ordered_all(X)).
  291
  292%= 	 	 
 find_and_call(:TermG) is semidet
Find And Call.
  298find_and_call(C:G):-current_predicate(_,C:G),!,found_call(C,G).
  299find_and_call(G):-current_predicate(_,G),!,on_x_debug(loop_check_early(G,cp2(G))).
  300find_and_call(_:G):-current_predicate(_,R:G),!,found_call(R,G).
  301find_and_call(G):-current_predicate(_,R:G),!,found_call(R,G).
  302
  303module_of(O,G,M):-predicate_property(O:G,imported_from(M)),!.
  304module_of(M,G,M):-predicate_property(M:G,defined), \+ predicate_property(M:G,imported_from(_)).
  305
  306find_module(G,R):- strip_module(G,M,P),module_of(M,P,R),!.
  307find_module(G,M):- current_module_ordered(C),module_of(C,G,M),!.
  308
  309callable_module(G,R):- strip_module(G,R,P),predicate_property(R:P,defined),!.
  310callable_module(G,R):- strip_module(G,_,P),current_module_ordered(R),predicate_property(R:P,defined),!.
  311callable_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
  317somehow_callable(G):-current_predicate(_,G),!.
  318somehow_callable(_:G):-!,current_predicate(_,_:G),!.
  319somehow_callable(G):-current_predicate(_,_:G),!.
  320
  321
  322%= 	 	 
 ain0(?N) is semidet
Assert If New Primary Helper.
  328ain0(N):-quietly(clause_asserted(N))->true;mpred_op_prolog(assert,N).
  329
  330:- export(mpred_op_prolog/2).  331:- module_transparent(mpred_op_prolog/2).  332:- meta_predicate mpred_op_prolog(?,:).  333
  334%= 	 	 
 mpred_op_prolog(?UPARAM1, ?N) is semidet
Managed Predicate Oper. Prolog.
  340mpred_op_prolog(ain0,N):- !,(quietly(clause_asserted(N))->true;mpred_op_prolog0(assert,N)).
  341mpred_op_prolog(paina,N):-!,(quietly(clause_asserted(N))->true;mpred_op_prolog0(system:asserta,N)).
  342mpred_op_prolog(painz,N):-!,(quietly(clause_asserted(N))->true;mpred_op_prolog0(system:assertz,N)).
  343mpred_op_prolog(pain,N):- !,(quietly(clause_asserted(N))->true;mpred_op_prolog0(assert,N)).
  344mpred_op_prolog(aina,N):- !,(clause_asserted(N)->true;mpred_op_prolog0(system:asserta,N)).
  345mpred_op_prolog(ainz,N):- !,(clause_asserted(N)->true;mpred_op_prolog0(system:assertz,N)).
  346mpred_op_prolog(ain,N):-  !,(clause_asserted(N)->true;mpred_op_prolog0(assert,N)).
  347% mpred_op_prolog(OP,M:Term):- unnumbervars(Term,Unumbered),Term \=@= Unumbered,!,dtrace,mpred_mop(M,OP,Unumbered).
  348mpred_op_prolog(OP,M:Term):-  dtrace,!,mpred_mop(M, OP,Term).
  349mpred_op_prolog(OP,M:Term):- 
  350  copy_term(Term, Copy, Gs),
  351  (Gs==[] -> mpred_mop(M,OP,Term);
  352    show_call(why,(
  353      expand_to_hb(Copy,H,B),conjoin(maplist(call,Gs),B,NB),dtrace,mpred_mop(M,OP,(H:-NB))))).
  354  
  355
  356%= 	 	 
 mpred_op_prolog0(:PRED1OP, ?MTerm) is semidet
Managed Predicate Oper. Prolog Primary Helper.
  362mpred_op_prolog0(OP,MTerm):- call(OP,MTerm).
  363
  364% peekAttributes/2,pushAttributes/2,pushCateElement/2.
  365:- 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)).  366:- 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)).
  367
  368:- meta_predicate paina(:),pain(:),painz(:),ain0(:),ainz_clause(:),ainz_clause(:,?).  369:- meta_predicate clause_asserted(:,?),expand_to_hb(?,?,?),clause_asserted(:),eraseall(+,+).  370
  371% aina(NEW):-ignore((system:retract(NEW),fail)),system:asserta(NEW).
  372% ainz(NEW):-ignore((system:retract(NEW),fail)),system:assertz(NEW).
  373% aina(_Ctx,NEW):-ignore((system:retract(NEW),fail)),system:asserta(NEW).
  374% writeqnl(_Ctx,NEW):- fmt('~q.~n',[NEW]),!.
  375
  376
  377%= 	 	 
 eraseall(+F, +A) is semidet
Eraseall.
  383eraseall(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))).
  384eraseall(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))).
  385
  386
  387%= 	 	 
 mpred_provider(?OP, ?Term, ?PROVIDER) is semidet
Hook To [std_provider/3] For Module Logicmoo_util_database. Managed Predicate Provider.
  394:-thread_local(t_l:std_provider_asserted/3).  395:-thread_local(t_l:current_std_provider/1).  396std_provider(OP,Term,PROVIDER):- t_l:std_provider_asserted(OP,Term,PROVIDER).
  397std_provider(_,_,PROVIDER):- t_l:current_std_provider(PROVIDER).
  398std_provider(OP,Term,PROVIDER):- baseKB:first_std_provider(OP,Term,PROVIDER).
  399
  400
  401:- meta_predicate call_provider(?).  402
  403%= 	 	 
 call_provider(?P) is semidet
Call Provider.
  409call_provider(P):-mpred_split_op_data(P,OP,Term),call_provider(OP,Term).
  410
  411
  412%= 	 	 
 call_provider(?OP, ?Term) is semidet
Call Provider.
  418call_provider(OP,Term):- must(std_provider(OP,Term,PROVIDER)),!,call(PROVIDER,OP,Term).
  419
  420call_provider(OP,Term):- must(std_provider(OP,Term,PROVIDER)),!,
  421   (loop_check_early(call(PROVIDER,OP,Term),fail)*->true;
  422   (loop_check_early(must(baseKB:next_std_provider(PROVIDER,NEXT)),NEXT=mpred_op_prolog),!,PROVIDER\=NEXT,call(NEXT,OP,Term))).
  423
  424
  425
  426:- meta_predicate assert_setting(:).
 assert_setting(?X) is semidet
  428assert_setting(M:P):-functor(P,_,A),dupe_term(P,DP),setarg(A,DP,_),system:retractall(M:DP),system:asserta(M:P).
  429:- meta_predicate assert_setting_if_missing(:).  430assert_setting_if_missing(M:P):-functor(P,_,A),dupe_term(P,DP),setarg(A,DP,_),(system:clause(M:DP,_)->true;system:asserta(M:P)).
  431
  432:- meta_predicate assert_if_new(:).
 assert_if_new(?X) is semidet
Assert If New.
  438assert_if_new(X):-mpred_op_prolog(pain,X).
  439:- meta_predicate asserta_if_new(:).  440
  441%= 	 	 
 asserta_if_new(?X) is semidet
Asserta If New.
  447asserta_if_new(X):-mpred_op_prolog(paina,X).
  448:- meta_predicate assertz_if_new(:).  449
  450%= 	 	 
 assertz_if_new(?X) is semidet
Assertz If New.
  456assertz_if_new(X):-mpred_op_prolog(painz,X).
  457
  458:- meta_predicate asserta_new(:).  459
  460%= 	 	 
 asserta_new(?X) is semidet
Asserta New.
  466asserta_new(X):-mpred_op_prolog(paina,X).
  467:- meta_predicate asserta_new(:).  468
  469%= 	 	 
 assertz_new(?X) is semidet
Assertz New.
  475assertz_new(X):-mpred_op_prolog(painz,X).
  476
  477
  478%= 	 	 
 pain(?N) is semidet
Pain.
  484pain(N):- call_provider(pain(N)).
  485
  486%= 	 	 
 paina(?N) is semidet
Paina.
  492paina(N):-call_provider(paina(N)).
  493
  494%= 	 	 
 painz(?N) is semidet
Painz.
  500painz(N):-call_provider(painz(N)).
  501
  502
  503:-multifile(ain/1).  504:-multifile(aina/1).  505:-multifile(ainz/1).  506:-module_transparent(ain/1).  507:-module_transparent(aina/1).  508:-module_transparent(ainz/1).  509:-dynamic(ain/1).  510:-dynamic(aina/1).  511:-dynamic(ainz/1).  512
  513%= 	 	 
 ain(?N) is semidet
Assert If New.
  519ain(N):- call_provider(pain(N)).
  520
  521%= 	 	 
 aina(?N) is semidet
Aina.
  527aina(N):-call_provider(paina(N)).
  528
  529%= 	 	 
 ainz(?N) is semidet
Ainz.
  535ainz(N):-call_provider(painz(N)).
  536
  537
  538%= 	 	 
 ainz_clause(?C) is semidet
Ainz Clause.
  544ainz_clause(C):- expand_to_hb(C,H,B),ainz_clause(H,B).
  545
  546%= 	 	 
 ainz_clause(?H, ?B) is semidet
Ainz Clause.
  552ainz_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.
  560expand_to_hb( H,  H,  true) :- var(H),!.
  561% 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)).
  562expand_to_hb(M:(M2:H :- B),HH,BB):-M==M2,!,expand_to_hb((M:H :- B),HH,BB).
  563expand_to_hb((H :- B),H,B):-!.
  564expand_to_hb( M:HB,  M:H,B):- !,expand_to_hb(HB,H,B).
  565expand_to_hb(  ~(HB),   ~(H),B):- !,expand_to_hb(HB,H,B).
  566expand_to_hb( H,  H,  true).
 is_visible_module(+Op) is semidet
Is a stripped Module (Meaning it will be found via inheritance)
  575is_visible_module(A):-var(A),!,fail.
  576is_visible_module(user).
  577is_visible_module(system).
  578is_visible_module(Inherited):-'$current_source_module'(E), default_module(E,Inherited).
  579%is_visible_module(Inherited):-'$current_typein_module'(E), default_module(E,Inherited).
  580is_visible_module(baseKB).
  581
  582
  583simple_var(Var):- var(Var),\+ attvar(Var).
  584
  585to_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.
  591hb_to_clause(H,B,H):- B==true,!.
  592hb_to_clause(M:(H:-B1),B2,(M:H:- (B2,B1))):-!.
  593hb_to_clause((H:-B1),B2,(H:- (B2,B1))):-!.
  594hb_to_clause(H,B,(H:-B)).
  595
  596
  597:-export(clause_asserted/1).  598:-meta_predicate(clause_asserted(:)).  599
  600%= 	 	 
 clause_asserted(?C) is semidet
Clause Asserted.
  606clause_asserted(C):- expand_to_hb(C,H,B),clause_asserted(H,B).
  607
  608:-export(clause_asserted/2).  609:-meta_predicate(clause_asserted(:,?)).  610
  611%= 	 	 
 clause_asserted(?H, ?B) is semidet
Clause Asserted.
  617clause_asserted(H,B):-clause_asserted(H,B,_).
  618
  619:-export(clause_asserted/3).  620:-meta_predicate(clause_asserted(:,?,-)).  621
  622%= 	 	 
 clause_asserted(?M, ?B, -R) is semidet
Clause Asserted.
  628clause_asserted(M:H,B,R):- copy_term(M:H:B,MHB),system:clause(M:H,B,R),variant(M:H:B,MHB).
  629
  630:- meta_predicate(clause_asserted1(:,?,?)).  631clause_asserted1(M:H,B,R):-
  632   functor(H,F,A),functor(HH,F,A),
  633   freeze(BB,(HH:BB=@=H:B)),
  634   clause(M:HH,BB,R).
  635
  636/*
  637
  638clause_asserted_test(M:H,B,R):- freeze(BB,(HH:BB=@=H:B)),clause(M:HH,BB,R).
  639
  640:- asserta(foo(A):-bar(A)).
  641
  642clause_asserted_test(foo(A),bar(A),R).
  643
  644*/
  645
  646
  647:-meta_predicate(modulize_head(?,?)).  648
  649current_module_from(Cm,M):- default_module(Cm,M).
  650current_module_from(Cm,M):- current_module_ordered(M), \+ default_module(Cm,M).
 modulize_head(+HEAD, -ModulePlusHead) is semidet
Modulize Head.
  656modulize_head(MH,M:H):- strip_module(MH,Cm,H),!,
  657  modulize_head_fb(Cm,H,Cm,M:H).
  658
  659modulize_head_fb(From,H,Fallback,M:H):- 
  660 quietly((findall(M:H,
  661  ((no_repeats(M, ((current_module_from(From,M),current_predicate(_,M:H),\+ predicate_property(M:H,imported_from(_))))))->true;
  662  M=Fallback),List))),
  663 member(M:H,List).
  664
  665
  666
  667:- reexport(library(listing_vars)).
 clause_asserted_i(+Head) is semidet
PFC Clause For User Interface.
  674clause_asserted_i(Head):- clausify_attributes(Head,HeadI),!,clause_asserted(HeadI).
  675clause_asserted_i(Head):- 
  676  \+ \+ ((
  677  % fully_expand_now_wte(assert,Head,HeadC),
  678  copy_term(Head,HC),
  679  copy_term_nat(Head,Head_copy),
  680  % find a unit system:clause identical to Head by finding one which unifies,
  681  clause_i(Head_copy),
  682  % and then checking to see if it is identical
  683  term_attvars(Head:Head_copy:HC,Vars),maplist(del_attr_type(vn),Vars),
  684  =@=(Head,HC),
  685  variant(Head,Head_copy))),!.
  686
  687
  688clause_asserted_i(H,B):- clause_asserted_i(H,B,_).
  689clause_asserted_i(MH,B,R):- ground(MH:B),!,with_quiet_vars_lock((system:clause(MH,B,R),system:clause(MHR,BR,R),ground(MHR:BR))).
  690clause_asserted_i(MH,B,R):- copy_term(MH:B,MHB),clause_i(MH,B,R),variant(MH:B,MHB).
  691
  692
  693
  694
  695put_clause_ref(_Ref,_V):- !.
  696put_clause_ref(Ref,V):- !, nop(dmsg(put_clause_ref(Ref,V))).
  697put_clause_ref(Ref,V):-put_attr(V,cref,Ref).
  698
  699remove_term_attr_type(Term,Mod):- quietly((term_attvars(Term,AVs),maplist(del_attr_type(Mod),AVs))).
  700
  701:- op(700,xfx,'=@=').  702
  703
  704attribute_is_info(name_variable(_Var,  _Name)).
  705attribute_is_info(put_attrs(_Var, vn, _Name)).
  706
  707attributes_equal(R,L,Attribs):-R=@=L,!,Attribs = R.
  708attributes_equal([INFO|L],R,TODO):- attribute_is_info(INFO),INFO,!,delete(R,INFO,RR),attributes_equal(L,RR,TODO).
  709attributes_equal(R,[INFO|L],TODO):- attribute_is_info(INFO),INFO,!,delete(R,INFO,RR),attributes_equal(L,RR,TODO).
  710
  711attributes_equal(L,R,[H|TODO]):- select(H,L,LL), select(HH,R,RR),H==HH,!,
  712    delete(LL,HH,LLL),delete(RR,H,RRR),attributes_equal(LLL,RRR,TODO).
  713attributes_equal(L,R,[H|TODO]):- select(H,L,LL), select(HH,R,RR),H =HH,!,
  714    delete(LL,HH,LLL),delete(RR,H,RRR),attributes_equal(LLL,RRR,TODO).
 clause_i(?H, ?B, ?Ref) is semidet
Clause For Internal Interface.
  721clause_i(HB):- expand_to_hb(HB,H,B)->clause_i(H,B,_).
  722clause_i(H,B):- clause_i(H,B,_).
  723
  724% TODO track which predicate have attributeds vars
  725clause_i(H0,B0,Ref):- \+ current_prolog_flag(assert_attvars,true) ,!, system:clause(H0,B0,Ref).
  726clause_i(H0,B0,Ref):- clause_attv(H0,B0,Ref).
  727
  728assert_i(X):- check_never_assert(X),fail.
  729assert_i(HB):- clausify_attributes(HB,CL),assert(CL).
  730
  731asserta_i(X):- check_never_assert(X),fail.
  732asserta_i(HB):-clausify_attributes(HB,CL),system:asserta(CL).
  733
  734assertz_i(X):- check_never_assert(X),fail.
  735assertz_i(HB):-clausify_attributes(HB,CL),system:assertz(CL).
  736
  737retract_i(HB):- expand_to_hb(HB,H,B), (clause_i(H,B,Ref)*->erase(Ref)).
  738retractall_i(H):-expand_to_hb(H,HH,_),forall(clause_i(HH,_,Ref),erase(Ref)).
  739
  740
  741:- dynamic(ereq/1).  742:- module_transparent(ereq/1).  743%ereq(mtHybrid(H)):- !, baseKB:mtHybrid(H).
  744ereq(C):- find_and_call(C).
  745
  746:- dynamic(dbreq/1).  747:- module_transparent(dbreq/1).  748dbreq(C):- ereq(C).
  749
  750
  751:-meta_predicate(clause_true(?)).  752
  753
  754predicate_property_safe(P,PP):- quietly(predicate_property(P,PP)).
 clause_b(?C) is semidet
Clause User Microtheory.
  762clause_b(M:Goal):- !, with_quiet_vars_lock((M:clause(Goal,B))),M:call(B).
  763clause_b(Goal):- with_quiet_vars_lock((clause(Goal,B),call(B))*->true;clause_b(baseKB:Goal)).
  764
  765% lookup_u/cheaply_u/call_u/clause_b
  766%clause_b(Goal):-  baseKB:call(call,Goal).
  767%clause_b(M:Goal):- !, (clause(M:Goal,true);clause(Goal,true)).
  768
  769
  770%clause_b(Goal):-  baseKB:clause(Goal,B)*->call(B);clause_b0(Goal).
  771%clause_b(Goal):-  baseKB:clause(Goal,true)*->true;clause_b0(Goal).
  772
  773% clause_b0(Goal):- if_defined(to_was_isa(clause_b,Goal,P0),fail),!,Goal\=P0,baseKB:clause(P0,true).
  774
  775%clause_b(M:C):-!,clause(M:C,true).
  776%clause_b(C):- call_u(clause(C,true)).
  777%clause_b(C):-!,clause(_:C,true).
  778%clause_b(Goal):-  Goal=..[C,PART],!,baseKB:t(C,PART).
  779%clause_b(Goal):-  current_predicate(_,baseKB:Goal),!,loop_check(baseKB:Goal).
  780% clause_b(Goal):- clause(baseKB:Goal,Body),(Body==true->true;call_u(Body)).
  781
  782%= 	 	 
 clause_true(?G) is semidet
Clause True.
  788clause_true(G):- !, clause_b(G).
  789
  790clause_true(M:G):-!,with_quiet_vars_lock((system:clause(M:G,true)*->true;(current_module_ordered(M2),system:clause(M2:G,true)))).
  791clause_true(G):- with_quiet_vars_lock((quietly((current_module_ordered(M), \+ \+  system:clause(M:G,_,_))),!, system:clause(M:G,true))).
  792%clause_true(M:G):- predicate_property(M:G,number_of_clauses(_)),!,system:clause(M:G,true).
  793%clause_true(_:G):-!,predicate_property(M:G,number_of_clauses(_)),system:clause(M:G,true).
  794%clause_true(G):-!,predicate_property(M:G,number_of_clauses(_)),system:clause(M:G,true).
  795
  796clause_true_anywhere(G):- strip_module(G,M,S),!,
  797  functor(S,F,A),
  798  functor(P,F,A),
  799  ((M2=M; M2=baseKB ;(current_module_ordered(M2),M2\=M)),
  800    current_predicate(M2:P)),!,
  801    with_quiet_vars_lock(system:clause(M2:S,B,Ref)),
  802     (B==true->! ;
  803    (clause_property(Ref,module(M22));M22=M2),!,call(M22:B)).
  804
  805
  806current_assertion_module(M):- if_defined(defaultAssertMt(M),M=baseKB).
  807suggest_m(M):- prolog_load_context(module,M),!.
  808suggest_m(M):- (if_defined(defaultAssertMt(M),fail);current_module_ordered(M)),is_visible_module(M).
  809
  810
  811:-export(retract_eq/1).  812
  813%= 	 	 
 retract_eq(?HB) is semidet
Retract Using (==/2) (or =@=/2) ).
  819retract_eq(HB):-expand_to_hb(HB,H,B),show_failure(modulize_head(H,MH)),clause_asserted(MH,B,Ref),erase(Ref).
  820
  821
  822:-export(safe_univ/2).  823
  824%= 	 	 
 safe_univ(?Call, ?List) is semidet
Safely Paying Attention To Corner Cases Univ.
  830safe_univ(SCall,Univ):-string(SCall),!,maybe_notrace(atom_string(Call,SCall)),[Call]=Univ.
  831safe_univ(Call,List):- quietly(safe_univ0(Call,List)),!.
  832
  833
  834%= 	 	 
 safe_univ0(?M, :TermN) is semidet
Safely Paying Attention To Corner Cases Univ Primary Helper.
  840safe_univ0(M:Call,[N:L|List]):- nonvar(M),nonvar(N),!,safe_univ0(Call,[L|List]).
  841safe_univ0(M:Call,[N:L|List]):- nonvar(N),var(M),N=M,!,safe_univ(Call,[L|List]).
  842safe_univ0(Call,[M:L|List]):- nonvar(M),!,safe_univ(Call,[L|List]).
  843safe_univ0(M:Call,[L|List]):- nonvar(M),!,safe_univ(Call,[L|List]).
  844safe_univ0(Call,[L|List]):- not(is_list(Call)),sanity(atom(L);compound(Call)), Call =..[L|List],!,warn_bad_functor(L).
  845safe_univ0([L|List],[L|List]):- var(List),\+ atom(L),!,rtrace,_ =.. [L|List],warn_bad_functor(L).
  846safe_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).
  847
  848/*
  849
  850%% bad_functor( ?L) is semidet.
  851%
  852% Bad Functor.
  853%
  854bad_functor(L) :- arg(_,v('|','.',[],':','/'),L).
  855
  856:- export(warn_bad_functor/1).
  857
  858%=
  859
  860%% warn_bad_functor( ?L) is semidet.
  861%
  862% Warn Bad Functor.
  863%
  864warn_bad_functor(L):-ignore((quietly(bad_functor(L)),!,dtrace,call(ddmsg(bad_functor(L))))).
  865
  866
  867%= 	 	 
  868*/
  869
  870:-export(erase_safe/2).  871
  872%= 	 	 
 erase_safe(?VALUE1, ?REF) is semidet
Erase Safely Paying Attention To Corner Cases.
  878erase_safe(_,REF):-erase(REF).
  879/*
  880erase_safe(((M:A):-B),REF):-!,erase_safe(system:clause(M:A,B),REF).
  881erase_safe(system:clause(U:A,B),REF):-U=user,!, erase_safe(system:clause(A,B),REF).
  882%erase_safe(system:clause(A,U:B),REF):-U=user,!, erase_safe(system:clause(A,B),REF).
  883%erase_safe(system:clause(M:A,B),REF):-!, erase_safe_now(M,system:clause(A,B),REF).
  884erase_safe(system:clause(A,B),REF):-!, erase_safe_now(_,system:clause(A,B),REF).
  885erase_safe(M:(A:-B),REF):-!,erase_safe(system:clause(M:A,B),REF).
  886erase_safe((A:-B),REF):-!,erase_safe(system:clause(A,B),REF).
  887erase_safe(system:clause(A,B,_),REF):-!,erase_safe(system:clause(A,B),REF).
  888erase_safe(system:asserta(A,_),REF):-!,erase_safe(system:clause(A,true),REF).
  889erase_safe(M:A,REF):-M==user,!,erase_safe(A,REF).
  890erase_safe(A,REF):-!,erase_safe(system:clause(A,true),REF).
  891
  892
  893erase_safe_now(_,system:clause(M:A,B),REF):-!,erase_safe_now(M,system:clause(A,B),REF).
  894erase_safe_now(M,system:clause(A,B),REF):-!,
  895   ignore((show_success(erase_safe_now, \+ system:clause(M:A,B, REF)))),
  896   (((var(REF);
  897   show_success(erase_safe_now, \+ nth_clause(A, _Index, REF));   
  898   show_success(erase_safe_now, clause_property(REF,erased));
  899   show_success(erase_safe_now, \+ clause_property(REF,_))))
  900   -> logicmoo_util_catch:ddmsg(warn(var_erase_safe(system:clause(A,B),REF))) ; 
  901       erase(REF)).
  902*/
  903
  904
  905:- ignore((source_location(S,_),prolog_load_context(module,M),module_property(M,class(library)),
  906 forall(source_file(M:H,S),
  907 ignore((functor(H,F,A),
  908  ignore(((\+ atom_concat('$',_,F),(export(F/A) , current_predicate(system:F/A)->true; system:import(M:F/A))))),
  909  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]))))))))).  910
  911:- fixup_exports.