1/* 
    2% File used as storage place for all predicates which change as
    3% the world is run.
    4%
    5% props(Obj,height(ObjHt))  == k(height,Obj,ObjHt) == rdf(Obj,height,ObjHt) == height(Obj,ObjHt)
    6% padd(Obj,height(ObjHt))  == padd(height,Obj,ObjHt,...) == add(QueryForm)
    7% kretract[all](Obj,height(ObjHt))  == kretract[all](Obj,height,ObjHt) == pretract[all](height,Obj,ObjHt) == del[all](QueryForm)
    8% keraseall(AnyTerm).
    9%
   10%
   11% Dec 13, 2035
   12% Douglas Miles
   13*/
   14% File: /opt/PrologMUD/pack/logicmoo_base/prolog/logicmoo/mpred/mpred_kb_ops.pl
   15%:- if(( ( \+ ((current_prolog_flag(logicmoo_include,Call),Call))) )).
   16:- module(mpred_kb_ops,[]).   17
   18
   19:- include('mpred_header.pi').   20
   21
   22:- user:use_module(library(clpfd),['#='/2]).
 get_arity(:TermTerm, ?F, ?A) is semidet
Get Arity.
   27get_arity(Term,F,A):- atom(Term),F=Term,!,ensure_arity(F,A).
   28get_arity(F/A,F,A):-!,atom(F),ensure_arity(F,A),!,(A>0).
   29get_arity(F // A,F,A2):- must(integer(A)),!, atom(F), is(A2 , A+2), ensure_arity(F,A2),!,(A2>0).
   30get_arity(F // A,F,A2):- use_module(library(clpfd),['#='/2]),!, atom(F), clpfd:call(#=(A2 , A+2)), ensure_arity(F,A2),!,(A2>0).
   31get_arity(M:FA,F,A):-atom(M),!,get_arity(FA,F,A).
   32get_arity(FA,F,A):- get_functor(FA,F,A),must(A>0).
   33
   34% arity_no_bc(F,A):- call_u(arity(F,A)).
   35arity_no_bc(F,A):- clause_b(arity(F,A)).
   36arity_no_bc(F,A):- clause_b(support_hilog(F,A)).
   37arity_no_bc(F,A):- clause_b(functorDeclares(F)),!,A=1.
   38arity_no_bc(completeExtentAsserted,1).
   39arity_no_bc(home,2).
   40arity_no_bc(record,2).
   41arity_no_bc(F,A):- suggest_m(M),clause_b(mpred_prop(M,F,AA,_)),nonvar(AA),A=AA.
   42%arity_no_bc(F,A):- current_predicate(F/A)
   43% arity_no_bc(F,A):- current_predicate(_:F/A),\+(current_predicate(_:F/AA),AA\=A). =
 ensure_arity(?VALUE1, ?VALUE2) is semidet
Ensure Arity.
   49ensure_arity(F,A):- one_must(arity_no_bc(F,A),one_must((current_predicate(F/A),
   50    (A>0),assert_arity(F,A)),(ground(F:A),(A>0),assert_arity(F,A)))),!.
   51
   52
   53%=
 assert_arity(?F, :PRED2A) is semidet
Assert Arity.
   60assert_arity(F,A):- sanity(\+ ((bad_arity(F,A), trace_or_throw_ex(assert_arity(F,A))))), arity_no_bc(F,A),!.
   61assert_arity(F,A):- arity_no_bc(F,AA), A\=AA,dmsg(assert_additional_arity(F,AA->A)),!,ain_fast(arity(F,A)).
   62assert_arity(F,A):- ain_fast(arity(F,A)),!.
   63
   64bad_arity(F,_):- \+ atom(F).
   65bad_arity(_,A):- \+ integer(A).
   66bad_arity('[|]',_).
   67bad_arity(typeProps,0).
   68bad_arity(argIsa,2).
   69bad_arity(isEach,_).
   70bad_arity(_,0).
   71bad_arity(prologDynamic,2).
   72bad_arity(F,A):- \+ good_pred_relation_name(F,A).
   73
   74
   75%=
 good_pred_relation_name(?F, ?A) is semidet
Good Predicate Relation Name.
   81good_pred_relation_name(F,A):- \+ bad_pred_relation_name0(F,A).
   82
   83
   84%=
 bad_pred_relation_name0(?V, ?VALUE2) is semidet
Bad Predicate Relation Name Primary Helper.
   90bad_pred_relation_name0(V,_):- \+ atom(V),!.
   91bad_pred_relation_name0('[]',_).
   92bad_pred_relation_name0('',_).
   93bad_pred_relation_name0('!',_).
   94bad_pred_relation_name0('{}',_).
   95bad_pred_relation_name0(',',_).
   96bad_pred_relation_name0('[|]',_).
   97
   98%=
 bad_pred_relation_name1(?X, ?Y) is semidet
Bad Predicate Relation Name Secondary Helper.
  104bad_pred_relation_name1(X,Y):-bad_pred_relation_name0(X,Y).
  105bad_pred_relation_name1(F,A):-must_det((atom_codes(F,[C|_]),to_upper(C,U))),!, U == C, A>1.
  106bad_pred_relation_name1(F,A):-arity_no_bc(F,AO), A \= AO.
  107
  108% :-after_boot(writeq("Seen Mpred_props at start!\n")),!.
  109
  110%=
 functor_check_univ(?G1, ?F, ?List) is semidet
Functor Check Univ.
  116functor_check_univ(M:G1,F,List):-atom(M),member(M,[dbase,user]),!,functor_check_univ(G1,F,List),!.
  117functor_check_univ(G1,F,List):-must_det(compound(G1)),must_det(G1 \= _:_),must_det(G1 \= _/_),G1=..[F|List],!.
  118
  119
  120%:- endif.
  121% :- ensure_loaded(library('logicmoo/util/logicmoo_util_bugger.pl')).
  122:- use_module(mpred_core).  123%:- use_module(mpred_type_isa).
  124%:- use_module(library(util_varnames)).
  125
  126/*
  127:- module_transparent retract_mu/1,
  128         assert_mu/4,
  129         asserta_mu/2,
  130         assertz_mu/2,
  131         assert_u/1,
  132         asserta_u/1,
  133         assertz_u/1,
  134         attempt_side_effect/1.
  135*/
  136:- module_transparent(attvar_op/2).  137
  138
  139:- meta_predicate 
  140      pred_head(1,*),
  141      attempt_side_effect(+),
  142      call_s(*),
  143      oncely(*),
  144      naf(*),
  145      call_s2(*),
  146      mpred_update_literal(*,*,0,*),
  147      mpred_retry(*),
  148%      mpred_op(?, ?),
  149      mpred_facts_only(*),
  150      map_unless(1,:,*,*),      
  151      is_callable(*),     
  152%      deducedSimply(*),
  153      cnstrn0(:,+),
  154      cnstrn(*),
  155      cnstrn(+,:),
  156      attvar_op(*,*),
  157      % clause_u(+,+,-),
  158      % call_u(+),
  159      assertz_mu(+),      
  160      assertz_mu(+,+),
  161      if_missing1(*),
  162      assert_mu(+),
  163      assert_mu(+,+,+,+),
  164      ain_minfo_2(1,*),
  165      ain_minfo(1,*),                                    
  166%      whenAnd(0,0),
  167      mpred_call_0(*),
  168      mpred_bc_only(*),
  169      mpred_bc_only0(*),
  170      mpred_prove_neg(*),
  171      call_u_req(*),
  172      pfcBC_NoFacts(*).  173
  174 :- meta_predicate mpred_get_support_one(0,*).  175 :- meta_predicate mpred_get_support_precanonical_plus_more(0,*).  176 % :- meta_predicate '__aux_maplist/2_cnstrn0+1'(*,0).
  177 :- meta_predicate repropagate_1(*).  178 :- meta_predicate trigger_supporters_list(0,*).  179 :- meta_predicate repropagate_meta_wrapper_rule(*).  180 :- meta_predicate repropagate_0(*).  181
  182
  183% oncely later will throw an error if there where choice points left over by call
  184:- meta_predicate(oncely(*)).  185:- was_export(oncely/1).
 oncely(:Goal) is semidet
Oncely.
  192oncely(:-(Call)):-!,Call,!.
  193oncely(:-(Call)):-!,call_u(Call).
  194oncely(Call):-once(Call).
  195% ================================================
  196% mpred_op/2
  197% ================================================
  198
  199/*
  200query(t, call_u, G):- call_u(G).
  201query(_, _, Op, G):- dtrace(call_u(call(Op,G))).
  202once(A,B,C,D):-trace_or_throw_ex(once(A,B,C,D)).
  203*/
  204
  205
  206
  207
  208% ================================================
  209% is_callable/call_u/naf
  210% ================================================
  211
  212%:- was_dynamic(naf/1).
  213:- meta_predicate(naf(*)).  214:- was_export(naf/1).
 naf(:Goal) is semidet
Negation-By-Faliure.
  222naf(Goal):- (\+ call_u(Goal)).
  223
  224:- meta_predicate(is_callable(*)).  225:- was_export(is_callable/1).
 is_callable(:GoalC) is semidet
If Is A Callable.
  233is_callable(C):-current_predicate(_,C),!.
  234
  235
  236:- style_check(+singleton).  237
  238% TODO READD
  239%:- foreach(arg(_,isEach(prologMultiValued,prologOrdered,prologNegByFailure,prologPTTP,prologKIF,pfcControlled,ttRelationType,
  240%     prologHybrid,predCanHaveSingletons,prologDynamic,prologBuiltin,functorIsMacro,prologListValued,prologSingleValued),P),.. )
  241
  242
  243% TODO ISSUE https://github.com/TeamSPoon/PrologMUD/issues/7
 check_context_module is semidet
Check Context Module. (throws if it turns out wrong)
  250check_context_module:- !.
  251% check_context_module:- is_release,!.
  252check_context_module:- 
  253  sanity((source_context_module(M1),clause_b(mtHybrid(M1)))),
  254  sanity((defaultAssertMt(M2),clause_b(mtHybrid(M2)))).
 check_real_context_module is semidet
Check Real Context Module (throws if it turns out wrong)
  260check_real_context_module:- is_release,!.
  261check_real_context_module:-!.
  262check_real_context_module:- sanity((context_module(M1),defaultAssertMt(M2),must(M1==M2))).
  263
  264
  265% ======================= mpred_file('pfcsyntax').	% operator declarations.
  266
  267:-
  268 op(1199,fx,('==>')), 
  269 op(1190,xfx,('::::')),
  270 op(1180,xfx,('==>')),
  271 op(1170,xfx,'<==>'),  
  272 op(1160,xfx,('<-')),
  273 op(1150,xfx,'=>'),
  274 op(1140,xfx,'<='),
  275 op(1130,xfx,'<=>'), 
  276 op(600,yfx,'&'), 
  277 op(600,yfx,'v'),
  278 op(350,xfx,'xor'),
  279 op(300,fx,'~'),
  280 op(300,fx,'-').
 mreq(+G) is semidet
Mreq.
  289mreq(G):- if_defined(call_u(G),fail).
  290
  291% ======================= mpred_file('pfccore').	% core of Pfc.
  292
  293%   File   : pfccore.pl
  294%   Author : Tim Finin, finin@prc.unisys.com
  295%   Updated: 10/11/87, ...
  296%            4/2/91 by R. McEntire: added calls to valid_dbref as a
  297%                                   workaround for the Quintus 3.1
  298%                            bug in the recorded database.
  299%   Purpose: core Pfc predicates.
  300
  301/*
  302
  303LogicMOO is mixing Mark Stickel's PTTP (prolog techn theorem prover) to create horn clauses that 
  304 PFC forwards and helps maintain in visible states )  in prolog knowledge baseable.. We use spft/3 to track deductions
  305Research~wise LogicMOO has a main purpose is to prove that grounded negations (of contrapostives) are of first class in importance in helping
  306with Wff checking/TMS 
  307Also alows an inference engine constrain search.. PFC became important since it helps memoize and close off (terminate) transitive closures
  308
  309*/
 is_side_effect_disabled is semidet
If Is A Side Effect Disabled.
  316is_side_effect_disabled:- t_l:no_attempt_side_effects,!.
  317is_side_effect_disabled:- t_l:side_effect_ok,!,fail.
  318is_side_effect_disabled:- t_l:noDBaseMODs(_),!.
 f_to_mfa(+EF, ?R, ?F, ?A) is semidet
Functor Converted To Module-functor-arity.
  326f_to_mfa(EF,R,F,A):-w_get_fa(EF,F,A),
  327              (((current_predicate(F/A),functor(P,F,A),predicate_property(_M:P,imported_from(R)))*->true;
  328              current_predicate(F/A),functor(P,F,A),source_file(R:P,_SF))),
  329              current_predicate(R:F/A).
 w_get_fa(+PI, ?F, ?A) is semidet
W Get Functor-arity.
  336w_get_fa(PI,_F,_A):-is_ftVar(PI),!.
  337w_get_fa(F/A,F,A):- !.
  338w_get_fa(PI,PI,_A):- atomic(PI),!.
  339w_get_fa(PI,F,A):- is_ftCompound(PI),!,functor(PI,F,A).
  340w_get_fa(Mask,F,A):-get_functor(Mask,F,A).
  341
  342
  343
  344:- multifile(baseKB:mpred_hook_rescan_files/0).  345:- dynamic(baseKB:mpred_hook_rescan_files/0).  346:- use_module(library(logicmoo_common)).  347%:- was_dynamic(use_presently/0).
  348% used to annotate a predciate to indicate PFC support
 is_mpred_action(:TermP) is semidet
If Is A Managed Predicate Action.
  355is_mpred_action('$VAR'(_)):-!,fail.
  356is_mpred_action(remove_if_unsupported(_,_)).
  357is_mpred_action(P):-is_static_predicate(P).
 mpred_is_builtin(+P) is semidet
PFC If Is A Builtin.
  363mpred_is_builtin(P):- predicate_property(P,built_in), \+ predicate_property(P,dynamic).
  364mpred_is_builtin(P):- callable(P),functor(P,F,_),clause_b(prologBuiltin(F)).
  365mpred_is_builtin(F):- current_predicate(F/A),A>0,functor(P,F,A),mpred_is_builtin(P).
  366
  367/* UNUSED TODAY
  368
  369:- use_module(library(mavis)).
  370:- use_module(library(type_check)).
  371:- use_module(library(typedef)).
  372*/
  373
  374
  375
  376:- thread_local((t_l:use_side_effect_buffer , t_l:verify_side_effect_buffer)).
 record_se is semidet
Record Se.
  382record_se:- (t_l:use_side_effect_buffer ; t_l:verify_side_effect_buffer).
 add_side_effect(+Op, ?Data) is semidet
Add Side Effect.
  390add_side_effect(_,_):- ( \+  record_se ),!.
  391add_side_effect(Op,Data0):- current_why(Why),serialize_attvars(Data0,Data),assert(t_l:side_effect_buffer(Op,Data,Why)).
Attribute Variable Oper..
  400listing_s(P):-call_s(xlisting(P)).
  401
  402assert_s(H):- assertz_s(H).
  403retractall_s(H):- forall(clause_s(H,_,R),erase(R)).
  404clause_s(H,B):- clause_s(H,B,_).
  405
  406retract_s(H):- lookup_s(H,R),erase(R).
  407
  408lookup_s(H):- lookup_s(H,_). 
  409
  410lookup_s(M:(H:-B),R):- !,clause_s(M:H,B,R).
  411lookup_s((H:-B),R):-  !,clause_s(H,B,R).
  412lookup_s(H,R):- clause_s(H,true,R).
  413
  414lookq_s(X):-lookq_s(X,_Ref).
  415
  416lookq_s(M:(H:-B),R):- !,clauseq_s(M:H,B,R).
  417lookq_s((H:-B),R):- !, clauseq_s(H,B,R).
  418lookq_s(H,R):- clauseq_s(H,true,R).
  419
  420asserta_s(H):- fix_mp(clause(assert,asserta_s),H,M,H0),asserta_i(M:H0).
  421assertz_s(H):- fix_mp(clause(assert,assertz_s),H,M,H0),assertz_i(M:H0).
  422clause_s(H,B,R):- fix_mp(clause(clause,clause_s),H,M,H0),clause_u(M:H0,B,R).
  423clauseq_s(H,B,R):- fix_mp(clause(clause,clauseq_s),H,M,H0),clause_u(M:H0,B,R),clause(M:HC,BC,R),H0=@=HC,BC=@=B.
  424
  425call_s(G0):-
  426  strip_module(G0,_,G),functor(G,F,A),
  427  (memberchk(F/A,[(',')/2])->
  428  mpred_METACALL(call_s,G);
  429  call_s2(G0)).
  430
  431call_s2(G0):-
  432  strip_module(G0,WM,G),
  433  defaultAssertMt(U),  
  434  must(current_predicate(_,U:G)->(CALL=U:G);(current_predicate(_,WM:G0)->CALL=WM:G0; fail)),
  435  call(call,(
  436 '$set_source_module'(S,U),'$module'(M,U),
  437  setup_call_cleanup( % _each
  438    ('$set_source_module'(U),'$set_typein_module'(U)),
  439       call(CALL),
  440     ('$set_source_module'(S),'$set_typein_module'(M))))).
  441
  442
  443:- module_transparent(attvar_op/2).  444
  445% % attvar_op(Op,Data):- deserialize_attvars(Data,Data0), attvar_op(Op,Data0).
  446attvar_op(Op,MData):-
  447 must_det_l((
  448   strip_module(Op,_,OpA), sanity( \+ atom(OpA)),
  449   fix_mp(clause(assert,OpA),MData,M,Data),
  450   add_side_effect(OpA,M:Data),
  451   quietly(current_prolog_flag(assert_attvars,true)->deserialize_attvars(Data,Data0);Data=Data0))),!,
  452   attempt_side_effect_mpa(M,OpA,Data0).
  453
  454
  455:- thread_local(t_l:no_attempt_side_effects/0).
 attempt_side_effect(+PSE) is semidet
Physical Side Effect.
  461attempt_side_effect(PSE):- to_physical_mpa(PSE,M,P,A),!,attempt_side_effect_mpa(M,P,A).
  462
  463to_physical_mpa(PSE,M,P,A):- strip_module(PSE,M,PA),to_physical_pa(PA,P,A).
  464to_physical_pa(PA,P,A):-PA=..[P,A],!. to_physical_pa(PA,call,PA).
  465
  466
  467:- meta_predicate(db_op_call(*,1,?)).  468db_op_call(_What,How,Data):- call(How,Data).
  469
  470% attempt_side_effect_mpa(M,OpA,Data):- record_se,!,add_side_effect(OpA,M:Data).
  471attempt_side_effect_mpa(M,db_op_call(_,retract_u0),Data0):- \+ lookup_u(M:Data0),!,fail.
  472attempt_side_effect_mpa(M,OpA,Data0):- \+ record_se, is_side_effect_disabled,!,mpred_warn('no_attempt_side_effects ~p',attempt_side_effect_mpa(M,OpA,Data0)).
  473% @TODO BROKEN phys ical_side_effect_call(M,assertz_i,Data0):- must((compile_aux_clauses(M:Data0))),!.
  474attempt_side_effect_mpa(M,OpA,Data0):- show_failure(M:call(M:OpA,M:Data0)).
  475
  476
  477/*
  478
  479  b_setval(th_asserts,[]),
  480  call_u(G),
  481  b_getval(th_asserts,List).
  482
  483attempt_side_effect_mpa(C) :- 
  484   b_getval(th_asserts,List),
  485   b_setval(th_asserts,[C|List]),!.
  486
  487
  488
  489*/
 erase_w_attvars(+Data0, ?Ref) is semidet
Erase W Attribute Variables.
  494erase_w_attvars(Data0,Ref):- attempt_side_effect(erase(Ref)),add_side_effect(erase,Data0).
 mpred_nochaining(+Goal) is semidet
PFC No Chaining.
  501mpred_nochaining(Goal):- locally_tl(no_attempt_side_effects,call(Goal)).
 with_chaining(+Goal) is semidet
PFC No Chaining.
  508with_chaining(Goal):- locally(- t_l:no_attempt_side_effects,call(Goal)).
  509
  510% TODO ISSUE https://github.com/TeamSPoon/PrologMUD/issues/7
 match_source_ref1(:TermARG1) is semidet
Match Source Ref Secondary Helper.
  517match_source_ref1(ax):-!.
  518match_source_ref1(mfl4(_VarNameZ,_,_,_)).
 make_uu_remove(:TermU) is semidet
Make Uu Remove.
  524make_uu_remove((_,ax)).
 has_functor(:TermC) is semidet
Has Functor.
  532% -- % has_functor(_):-!,fail.
  533has_functor(F/A):-!,is_ftNameArity(F,A),!.
  534has_functor(C):- (\+ is_ftCompound(C)),!,fail.
  535has_functor(C):- is_ftCompound(C),\+is_list(C).
 mpred_each_literal(+P, ?E) is semidet
PFC Each Literal.
  542mpred_each_literal(P,E):-is_ftNonvar(P),P=(P1,P2),!,(mpred_each_literal(P1,E);mpred_each_literal(P2,E)).
  543mpred_each_literal(P,P). %:-conjuncts_to_list(P,List),member(E,List).
 retract_eq_quitely(+H) is semidet
Retract Using (==/2) (or =@=/2) ) Quitely.
  551retract_eq_quitely(H):- call_u(retract_eq_quitely_f(H)).
 retract_eq_quitely_f(+H) is semidet
Retract Using (==/2) (or =@=/2) ) Quitely False.
  557retract_eq_quitely_f((H:-B)):- !,clause_asserted_i(H,B,Ref),erase(Ref).
  558retract_eq_quitely_f(pfclog(H)):- retract_eq_quitely_f(H),fail.
  559retract_eq_quitely_f((H)):- clause_asserted_i(H,true,Ref),erase(Ref).
 assert_eq_quitely(+H) is semidet
Assert Using (==/2) (or =@=/2) ) Quitely.
  566assert_eq_quitely(H):- attvar_op(db_op_call(assert,assert_if_new),H).
 mpred_is_tautology(+Var) is semidet
PFC If Is A Tautology.

:- module_transparent( (mpred_is_tautology)/1).

  574mpred_is_tautology(V):- (is_ftVar(V) -> true;(copy_term_nat(V,VC),numbervars(VC),mpred_is_taut(VC))),!.
 mpred_is_taut(:TermA) is semidet
PFC If Is A Taut.
  582mpred_is_taut(A):-var(A),!.
  583mpred_is_taut(A:-B):-!,mpred_is_taut(B==>A).
  584mpred_is_taut(A<-B):-!,mpred_is_taut(B==>A).
  585mpred_is_taut(A<==>B):-!,(mpred_is_taut(A==>B);mpred_is_taut(B==>A)).
  586mpred_is_taut(A==>B):- A==B,!.
  587mpred_is_taut((B,_)==>A):- mpred_is_assertable(B),mpred_is_taut(A==>B),!.
  588mpred_is_taut((_,B)==>A):- mpred_is_assertable(B),mpred_is_taut(A==>B),!.
  589mpred_is_taut(B==>(A,_)):- mpred_is_assertable(A),mpred_is_taut(A==>B),!.
  590mpred_is_taut(B==>(_,A)):- mpred_is_assertable(A),mpred_is_taut(A==>B),!.
  591
  592
  593% baseKB:decl_database_hook(Op,Hook):- loop_check_nr(pfc_provide_storage_op(Op,Hook)).
 is_retract_first(+VALUE1) is semidet
If Is A Retract First.
  600is_retract_first(one).
  601is_retract_first(a).
 pfc_provide_storage_op(+Op, ?I1) is semidet
Prolog Forward Chaining Provide Storage Oper..
  608pfc_provide_storage_op(Op,(I1,I2)):-!,pfc_provide_storage_op(Op,I1),pfc_provide_storage_op(Op,I2).
  609pfc_provide_storage_op(Op,(nesc(P))):-!,pfc_provide_storage_op(Op,P).
  610%pfc_provide_storage_op(change(assert,_AorZ),Fact):- loop_check_nr(ainPreTermExpansion(Fact)).
  611% pfcRem1 to just get the first
  612pfc_provide_storage_op(change(retract,OneOrA),FactOrRule):- is_retract_first(OneOrA),!,
  613            loop_check_nr(mpred_withdraw(FactOrRule)),
  614  ignore((ground(FactOrRule),mpred_remove(FactOrRule))).
  615% mpred_remove should be forcefull enough
  616pfc_provide_storage_op(change(retract,all),FactOrRule):- loop_check_nr(mpred_remove(FactOrRule)),!.
  617% pfc_provide_storage_op(clause_u,FactOrRule):- is_ftNonvar(FactOrRule),!,loop_check_nr(clause_u(FactOrRule)).
  618
  619
  620% pfcDatabaseGoal(G):-is_ftCompound(G),get_functor(G,F,A),pfcDatabaseTerm(F/A).
 mpred_pbody(+H, ?B, ?R, ?BIn, ?WHY) is semidet
PFC Pbody.

mpred_pbody(H,B,_R,fail,deduced(backchains)):- get_bc_clause(H,_H,B),!. mpred_pbody(H,infoF(INFO),R,B,Why):-!,mpred_pbody_f(H,INFO,R,B,Why). mpred_pbody(H,B,R,BIn,WHY):- is_true(B),!,BIn=B,get_why(H,H,R,WHY). mpred_pbody(H,B,R,B,asserted(R,(H:-B))).

 get_why(+VALUE1, ?CL, ?R, :TermR) is semidet
Get Generation Of Proof.
  639get_why(_,CL,R,asserted(R,CL:-U)):- clause_u(spft(CL, U, ax),true),!.
  640get_why(H,CL,R,deduced(R,WHY)):- (mpred_get_support(H,WH)*->WHY=(H=WH);(mpred_get_support(CL,WH),WHY=(CL=WH))).
  641
  642
  643
  644%% mpred_pbody_f( +H, ?CL, ?R, ?B, ?WHY) is semidet.
  645%
  646% PFC Pbody False.
  647%
  648mpred_pbody_f(H,CL,R,B,WHY):- CL=(B==>HH),sub_term_eq(H,HH),!,get_why(H,CL,R,WHY).
  649mpred_pbody_f(H,CL,R,B,WHY):- CL=(HH<-B),sub_term_eq(H,HH),!,get_why(H,CL,R,WHY).
  650mpred_pbody_f(H,CL,R,B,WHY):- CL=(HH<==>B),sub_term_eq(H,HH),get_why(H,CL,R,WHY).
  651mpred_pbody_f(H,CL,R,B,WHY):- CL=(B<==>HH),sub_term_eq(H,HH),!,get_why(H,CL,R,WHY).
  652mpred_pbody_f(H,CL,R,fail,infoF(CL)):- trace_or_throw_ex(mpred_pbody_f(H,CL,R)).
 sub_term_eq(+H, ?HH) is semidet
Sub Term Using (==/2) (or =@=/2) ).
  659sub_term_eq(H,HH):-H==HH,!.
  660sub_term_eq(H,HH):-each_subterm(HH,ST),ST==H,!.
 sub_term_v(+H, ?HH) is semidet
Sub Term V.
  667sub_term_v(H,HH):-H=@=HH,!.
  668sub_term_v(H,HH):-each_subterm(HH,ST),ST=@=H,!.
 all_different_head_vals(+Clause) is det
Enforces All Different Head Vals.
  674all_different_head_vals(HB):- (\+ compound(HB) ; ground(HB)),!.
  675all_different_head_vals(HB):- 
  676  mpred_rule_hb(HB,H,B),
  677  term_slots(H,Slots),  
  678  (Slots==[]->
  679     all_different_head_vals(B);
  680    (lock_vars(Slots),all_different_head_vals_2(H,Slots),unlock_vars(Slots))),!.
  681  
  682
  683all_different_head_vals_2(_H,[]):-!.
  684all_different_head_vals_2(H,[A,R|EST]):-get_assertion_head_arg(_,H,E1),E1 ==A,dif(A,E2),get_assertion_head_arg(_,H,E2),\+ contains_var(A,E2),all_different_vals(dif_matrix,[A,E2,R|EST]),!.
  685all_different_head_vals_2(_H,[A,B|C]):-all_different_vals(dif_matrix,[A,B|C]),!.
  686all_different_head_vals_2(HB,_):- \+ compound(HB),!.
  687all_different_head_vals_2(H,[A]):-get_assertion_head_arg(_,H,E1),E1 ==A, H=..[_|ARGS], all_different_vals(dif_matrix,ARGS),!.
  688all_different_head_vals_2(H,[A]):-get_assertion_head_arg(_,H,E1),E1 ==A,  get_assertion_head_arg(_,H,E2), A\==E2, \+ contains_var(A,E2), dif(A,E2),!.
  689all_different_head_vals_2(H,[A]):-get_assertion_head_arg(_,H,E1),E1\==A, compound(E1), contains_var(A,E1), all_different_head_vals_2(E1,[A]),!.
  690all_different_head_vals_2(_,_).
 mpred_rule_hb(+Outcome, ?OutcomeO, ?AnteO) is semidet
Calculate PFC Rule Head+body.
  697mpred_rule_hb(Outcome,OutcomeO,Body):- nonvar(OutcomeO),!,mpred_rule_hb(Outcome,OutcomeN,Body),must(OutcomeO=OutcomeN).
  698mpred_rule_hb(Outcome,OutcomeO,BodyO):- nonvar(BodyO),!,mpred_rule_hb(Outcome,OutcomeO,BodyN),must(BodyN=BodyO).
  699mpred_rule_hb(Outcome,OutcomeO,AnteO):- 
  700  quietly((mpred_rule_hb_0(Outcome,OutcomeO,Ante),
  701  mpred_rule_hb_0(Ante,AnteO,_))).
  702% :-mpred_trace_nochilds(mpred_rule_hb/3).
 mpred_rule_hb_0(+Rule, -Head, -Body) is nondet
Calculate PFC rule Head+Body Primary Helper.
  711mpred_rule_hb_0(Outcome,OutcomeO,true):-is_ftVar(Outcome),!,OutcomeO=Outcome.
  712mpred_rule_hb_0(Outcome,OutcomeO,true):- \+compound(Outcome),!,OutcomeO=Outcome.
  713mpred_rule_hb_0((Outcome1,Outcome2),OutcomeO,AnteO):- mpred_rule_hb(Outcome1,Outcome1O,Ante1),mpred_rule_hb(Outcome2,Outcome2O,Ante2),
  714                   conjoin(Outcome1O,Outcome2O,OutcomeO),
  715                   conjoin(Ante1,Ante2,AnteO).
  716mpred_rule_hb_0((Ante1==>Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  717mpred_rule_hb_0((Ante1=>Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  718mpred_rule_hb_0((Ante1->Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  719mpred_rule_hb_0((Ante1*->Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  720% mpred_rule_hb_0((Outcome/Ante1),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  721mpred_rule_hb_0(rhs([Outcome]),OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  722% mpred_rule_hb_0(rhs([OutcomeH|OutcomeT]),OutcomeO,Ante2):- !, mpred_rule_hb(Outcome,OutcomeO,Ante2).
  723mpred_rule_hb_0({Outcome},OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  724mpred_rule_hb_0((Outcome<-Ante1),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  725mpred_rule_hb_0((Ante1 & Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  726mpred_rule_hb_0((Ante1 , Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  727mpred_rule_hb_0((Outcome<==>Ante1),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  728mpred_rule_hb_0((Ante1<==>Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  729mpred_rule_hb_0(_::::Outcome,OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb_0(Outcome,OutcomeO,Ante2).
  730mpred_rule_hb_0(bt(Outcome,Ante1),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  731mpred_rule_hb_0(pt(Ante1,Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  732mpred_rule_hb_0(pk(Ante1a,Ante1b,Outcome),OutcomeO,(Ante1a,Ante1b,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  733mpred_rule_hb_0(nt(Ante1a,Ante1b,Outcome),OutcomeO,(Ante1a,Ante1b,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  734mpred_rule_hb_0(spft(Outcome,Ante1a,Ante1b),OutcomeO,(Ante1a,Ante1b,Ante2)):- (nonvar(Outcome)-> ! ; true),mpred_rule_hb(Outcome,OutcomeO,Ante2).
  735mpred_rule_hb_0(que(Outcome,_),OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  736% mpred_rule_hb_0(pfc Default(Outcome),OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  737mpred_rule_hb_0((Outcome:-Ante),Outcome,Ante):-(nonvar(Outcome)-> ! ; true).
  738mpred_rule_hb_0(Outcome,Outcome,true).
 ain_minfo(+G) is semidet
Assert If New Metainformation.
  745:- module_transparent(ain_minfo/1).  746ain_minfo(G):-ain_minfo(assertz_if_new,G).
 ain_minfo(:PRED1How, ?H) is semidet
Assert If New Metainformation.
  752:- module_transparent(ain_minfo/2).  753ain_minfo(How,(H:-True)):-is_true(True),must(is_ftNonvar(H)),!,ain_minfo(How,H).
  754ain_minfo(How,(H<-B)):- !,ain_minfo(How,(H:-infoF(H<-B))),!,get_bc_clause(H,Post),ain_minfo(How,Post),ain_minfo_2(How,(B:-infoF(H<-B))).
  755ain_minfo(How,(B==>H)):- !,ain_minfo(How,(H:-infoF(B==>H))),!,ain_minfo_2(How,(B:-infoF(B==>H))).
  756ain_minfo(How,(B<==>H)):- !,ain_minfo(How,(H:-infoF(B<==>H))),!,ain_minfo(How,(B:-infoF(B<==>H))),!.
  757ain_minfo(How,((A,B):-INFOC)):-mpred_is_info(INFOC),(is_ftNonvar(A);is_ftNonvar(B)),!,ain_minfo(How,((A):-INFOC)),ain_minfo(How,((B):-INFOC)),!.
  758ain_minfo(How,((A;B):-INFOC)):-mpred_is_info(INFOC),(is_ftNonvar(A);is_ftNonvar(B)),!,ain_minfo(How,((A):-INFOC)),ain_minfo(How,((B):-INFOC)),!.
  759ain_minfo(How,(-(A):-infoF(C))):-is_ftNonvar(C),is_ftNonvar(A),!,ain_minfo(How,((A):-infoF((C)))). % attvar_op(How,(-(A):-infoF(C))).
  760ain_minfo(How,(~(A):-infoF(C))):-is_ftNonvar(C),is_ftNonvar(A),!,ain_minfo(How,((A):-infoF((C)))). % attvar_op(How,(-(A):-infoF(C))).
  761ain_minfo(How,(A:-INFOC)):- is_ftNonvar(INFOC), get_bc_clause(A,AA,INFOCC),A=AA,INFOC==INFOCC,!,attvar_op(How,(A:-INFOC)),!.
  762ain_minfo(How,bt(_ABOX,H,_)):-!,get_bc_clause(H,Post),attvar_op(How,Post).
  763ain_minfo(How,nt(H,Test,Body)):-!,attvar_op(How,(H:-fail,nt(H,Test,Body))).
  764ain_minfo(How,pt(H,Body)):-!,attvar_op(How,(H:-fail,pt(H,Body))).
  765ain_minfo(How,(A0:-INFOC0)):- mpred_is_info(INFOC0), copy_term_and_varnames((A0:-INFOC0),(A:-INFOC)),!,must((mpred_rewrap_h(A,AA),imploded_copyvars((AA:-INFOC),ALLINFO), attvar_op(How,(ALLINFO)))),!.
  766%ain_minfo(How,G):-mpred_trace_msg(skipped_add_meta_facts(How,G)).
  767ain_minfo(_,_).
  768
  769:- was_export(ain_minfo_2/2).
 ain_minfo_2(:PRED1How, ?G) is semidet
Assert If New Metainformation Extended Helper.
  775:- module_transparent(ain_minfo_2/2).  776ain_minfo_2(How,G):-ain_minfo(How,G).
 mpred_is_info(:TermC) is semidet
PFC If Is A Info.
  783mpred_is_info((CWC,Info)):- (atom(CWC),cwc(CWC));mpred_is_info(Info).
  784mpred_is_info(mpred_bc_only(C)):-is_ftNonvar(C),!.
  785mpred_is_info(infoF(C)):-is_ftNonvar(C),!.
  786mpred_is_info(inherit_above(_,_)).
  787
  788cwc(awc).
  789cwc(zwc).
  790cwc(fail).
  791%cwc(Call):- callable(Call),Call.
  792
  793%:- was_dynamic(not_not/1).
 mpred_rewrap_h(+A, ?A) is semidet
PFC Rewrap Head.
  799mpred_rewrap_h(A,A):-is_ftNonvar(A),\+ is_static_predicate(A).
  800mpred_rewrap_h(A,F):- functor(A,F,_),\+ is_static_predicate(F),!.
  801%mpred_rewrap_h(A,not_not(A)):-!.
 cwc is det
Cwc.
  808cwc:-true.
 fwc is det
Fwc.
  814fwc:-true.
 bwc is semidet
Bwc.
  820bwc:-true.
 wac is semidet
Wac.
  826wac:-true.
 is_fc_body(+P) is semidet
If Is A Forward Chaining Body.
  832is_fc_body(P):- has_body_atom(fwc,P).
 is_bc_body(+P) is semidet
If Is A Backchaining Body.
  838is_bc_body(P):- has_body_atom(bwc,P).
 is_action_body(+P) is semidet
If Is A Action Body.
  844is_action_body(P):- has_body_atom(wac,P).
 has_body_atom(+WAC, ?P) is semidet
Has Body Atom.
  852has_body_atom(WAC,P):- call(
  853   WAC==P -> true ; (is_ftCompound(P),get_assertion_head_arg(1,P,E),has_body_atom(WAC,E))),!.
  854
  855/*
  856has_body_atom(WAC,P,Rest):- call(WAC==P -> Rest = true ; (is_ftCompound(P),functor(P,F,A),is_atom_body_pfa(WAC,P,F,A,Rest))).
  857is_atom_body_pfa(WAC,P,F,2,Rest):-get_assertion_head_arg(1,P,E),E==WAC,get_assertion_head_arg(2,P,Rest),!.
  858is_atom_body_pfa(WAC,P,F,2,Rest):-get_assertion_head_arg(2,P,E),E==WAC,get_assertion_head_arg(1,P,Rest),!.
  859*/
  860
  861:- module_transparent( (get_assertion_head_arg)/3).  862get_assertion_head_arg(N,P,E):-get_assertion_head_unnegated(P,PP),!,arg(N,PP,E).
  863
  864same_functors(Head1,Head2):-must_det(get_unnegated_functor(Head1,F1,A1)),must_det(get_unnegated_functor(Head2,F2,A2)),!,F1=F2,A1=A2.
 mpred_update_literal(+P, ?N, ?Q, ?R) is semidet
PFC Update Literal.
  871mpred_update_literal(P,N,Q,R):-
  872    get_assertion_head_arg(N,P,UPDATE),call(replace_arg(P,N,Q_SLOT,Q)),
  873    must(call_u(Q)),update_value(Q_SLOT,UPDATE,NEW), 
  874    replace_arg(Q,N,NEW,R).
  875
  876
  877% spft(5,5,5).
 update_single_valued_arg(+Module, +P, ?N) is semidet
Update Single Valued Argument.
  883:- module_transparent( (update_single_valued_arg)/3).  884
  885update_single_valued_arg(M,M:Pred,N):-!,update_single_valued_arg(M,Pred,N).
  886update_single_valued_arg(_,M:Pred,N):-!,update_single_valued_arg(M,Pred,N).
  887
  888update_single_valued_arg(world,P,N):- !, update_single_valued_arg(baseKB,P,N).
  889update_single_valued_arg(M,P,N):- break, \+ clause_b(mtHybrid(M)), trace, clause_b(mtHybrid(M2)),!,
  890   update_single_valued_arg(M2,P,N).
  891
  892update_single_valued_arg(M,P,N):- 
  893  get_assertion_head_arg(N,P,UPDATE),
  894  is_relative(UPDATE),!,
  895  dtrace,
  896  break,
  897  replace_arg(P,N,OLD,Q),
  898  must_det_l((clause_u(Q),update_value(OLD,UPDATE,NEW),\+ is_relative(NEW), replace_arg(Q,N,NEW,R))),!,
  899  update_single_valued_arg(M,R,N).
  900
  901
  902update_single_valued_arg(M,P,N):- 
  903 call_u((must_det_l((
  904
  905  call_u(mtHybrid(M)),
  906  mpred_type_args \= M,
  907  mpred_kb_ops \= M,
  908  get_assertion_head_arg(N,P,UPDATE),
  909  replace_arg(P,N,Q_SLOT,Q),
  910  var(Q_SLOT),
  911  same_functors(P,Q),
  912  % current_why(U),
  913  must_det_l((
  914     % rtrace(attvar_op(assert_if_new,M:spft(P,U,ax))),
  915     % (call_u(P)->true;(assertz_mu(P))),
  916     assertz_mu(M,P),
  917     doall((
  918          lookup_u(M:Q,E),
  919          UPDATE \== Q_SLOT,
  920          erase(E),
  921          mpred_unfwc1(M:Q))))))))).
  922
  923% ======================= 
  924% utils
  925% ======================= 
 map_literals(+P, ?G) is semidet
Map Literals.
  931map_literals(P,G):-map_literals(P,G,[]).
 map_literals(+VALUE1, :TermH, ?VALUE3) is semidet
Map Literals.
  938map_literals(_,H,_):-is_ftVar(H),!. % skip over it
  939map_literals(_,[],_) :- !.
  940map_literals(Pred,(H,T),S):-!, apply(Pred,[H|S]), map_literals(Pred,T,S).
  941map_literals(Pred,[H|T],S):-!, apply(Pred,[H|S]), map_literals(Pred,T,S).
  942map_literals(Pred,H,S):- mpred_literal(H),must(apply(Pred,[H|S])),!.
  943map_literals(_Pred,H,_S):- \+ is_ftCompound(H),!. % skip over it
  944map_literals(Pred,H,S):-H=..List,!,map_literals(Pred,List,S),!.
 map_unless(:PRED1Test, ?Pred, ?H, ?S) is semidet
Map Unless.
  952map_unless(Test,Pred,H,S):- call(Test,H),ignore(apply(Pred,[H|S])),!.
  953map_unless(_Test,_,[],_) :- !.
  954map_unless(_Test,_Pred,H,_S):- \+ is_ftCompound(H),!. % skip over it
  955map_unless(Test,Pred,(H,T),S):-!, apply(Pred,[H|S]), map_unless(Test,Pred,T,S).
  956map_unless(Test,Pred,[H|T],S):-!, apply(Pred,[H|S]), map_unless(Test,Pred,T,S).
  957map_unless(Test,Pred,H,S):-H=..List,!,map_unless(Test,Pred,List,S),!.
  958
  959
  960:- meta_predicate(map_first_arg(:,+)).
 map_first_arg(+Pred, ?List) is semidet
PFC Maptree.
  965map_first_arg(CM:Pred,List):-map_first_arg(CM,Pred,List,[]).
  966
  967:- meta_predicate(map_first_arg(+,*,+,+)).
 map_first_arg(+Pred, :TermH, ?S) is semidet
PFC Maptree.
  972map_first_arg(CM,Pred,H,S):-is_ftVar(H),!,CM:apply(Pred,[H|S]).
  973map_first_arg(_,_,[],_) :- !.
  974map_first_arg(CM,Pred,(H,T),S):-!, map_first_arg(CM,Pred,H,S), map_first_arg(CM,Pred,T,S).
  975map_first_arg(CM,Pred,(H;T),S):-!, map_first_arg(CM,Pred,H,S) ; map_first_arg(CM,Pred,T,S).
  976map_first_arg(CM,Pred,[H|T],S):-!, CM:apply(Pred,[H|S]), map_first_arg(CM,Pred,T,S).
  977map_first_arg(CM,Pred,H,S):- CM:apply(Pred,[H|S]). 
  978
  979:- fixup_exports.  980
  981% % :- ensure_loaded(logicmoo(util/rec_lambda)).
  982
  983%example pfcVerifyMissing(mpred_isa(I,D), mpred_isa(I,C), ((mpred_isa(I,C), {D==C});-mpred_isa(I,C))). 
  984%example pfcVerifyMissing(mudColor(I,D), mudColor(I,C), ((mudColor(I,C), {D==C});-mudColor(I,C))). 
 pfcVerifyMissing(+GC, ?GO, ?GO) is semidet
Prolog Forward Chaining Verify Missing.
  991pfcVerifyMissing(GC, GO, ((GO, {D==C});\+ GO) ):-  GC=..[F,A|Args],append(Left,[D],Args),append(Left,[C],NewArgs),GO=..[F,A|NewArgs],!.
  992
  993%example mpred_freeLastArg(mpred_isa(I,C),~(mpred_isa(I,C))):-is_ftNonvar(C),!.
  994%example mpred_freeLastArg(mpred_isa(I,C),(mpred_isa(I,F),C\=F)):-!.
 mpred_freeLastArg(+G, ?GG) is semidet
PFC Free Last Argument.
 1000mpred_freeLastArg(G,GG):- G=..[F,A|Args],append(Left,[_],Args),append(Left,[_],NewArgs),GG=..[F,A|NewArgs],!.
 1001mpred_freeLastArg(_G,false).
 mpred_current_op_support(+VALUE1) is semidet
PFC Current Oper. Support.
 1008mpred_current_op_support((p,p)):-!.
 pfcVersion(+VALUE1) is semidet
Prolog Forward Chaining Version.
 1015pfcVersion(6.6).
 1016
 1017
 1018% % :- '$set_source_module'(mpred_kb_ops).
 correctify_support(+S, ?S) is semidet
Correctify Support.
 1024correctify_support(U,(U,ax)):-var(U),!.
 1025correctify_support((U,U),(U,ax)):-!.
 1026correctify_support((S,T),(S,T)):-!.
 1027correctify_support((U,_UU),(U,ax)):-!.
 1028correctify_support([U],S):-correctify_support(U,S).
 1029correctify_support(U,(U,ax)).
 clause_asserted_local(:TermABOX) is semidet
Clause Asserted Local.
 1036clause_asserted_local(MCL):-
 1037  strip_module(MCL,_,CL),
 1038  must(CL=spft(P,Fact,Trigger )),!,
 1039  clause_u(spft(P,Fact,Trigger),true,Ref),
 1040  clause_u(spft(UP,UFact,UTrigger),true,Ref),
 1041  (((UP=@=P,UFact=@=Fact,UTrigger=@=Trigger))).
 is_already_supported(+P, ?S, ?UU) is semidet
If Is A Already Supported.
 1049is_already_supported(P,(S,T),(S,T)):- clause_asserted_local(spft(P,S,T)),!.
 1050is_already_supported(P,_S,UU):- clause_asserted_local(spft(P,US,UT)),must(get_source_uu(UU)),UU=(US,UT).
 1051
 1052% TOO UNSAFE 
 1053% is_already_supported(P,_S):- copy_term_and_varnames(P,PC),sp ftY(PC,_,_),P=@=PC,!.
 1054
 1055
 1056if_missing1(Q):- mpred_literal_nv(Q), call_u( \+ ~ Q), if_missing_mask(Q,R,Test),!, lookup_u(R), Test.
 if_missing_mask(+Q, ?R, ?Test) is semidet
If Missing Mask.
 1064if_missing_mask(M:Q,M:R,M:Test):- nonvar(Q),!,if_missing_mask(Q,R,Test).
 1065if_missing_mask(Q,~Q,\+Q):- \+ is_ftCompound(Q),!.
 1066
 1067%if_missing_mask(ISA, ~ ISA, \+ ISA):- functor(ISA,F,1),(F==tSwim;call_u(functorDeclares(F))),!.
 1068if_missing_mask(HB,RO,TestO):- once(mpred_rule_hb(HB,H,B)),B\==true,HB\==H,!,
 1069     if_missing_mask(H,R,TestO),subst(HB,H,R,RO).
 1070
 1071if_missing_mask(ISA, ISA, \+ ISA):- functor(ISA, _F,1),!.% (F==tSwim;call_u(functorDeclares(F))),!.
 1072
 1073if_missing_mask(Q,R,Test):-
 1074   which_missing_argnum(Q,N),
 1075   if_missing_n_mask(Q,N,R,Test),!.
 1076
 1077if_missing_mask(ISA, ~ ISA, \+ ISA).
 if_missing_n_mask(+Q, ?N, ?R, ?Test) is semidet
If Missing Mask.
 1083if_missing_n_mask(Q,N,R,Test):-
 1084  get_assertion_head_arg(N,Q,Was),
 1085  (nonvar(R)-> (which_missing_argnum(R,RN),get_assertion_head_arg(RN,R,NEW));replace_arg(Q,N,NEW,R)),!,
 1086   Test=dif:dif(Was,NEW).
 1087
 1088/*
 1089Old version
 1090if_missing_mask(Q,N,R,dif:dif(Was,NEW)):- 
 1091 must((is_ftNonvar(Q),acyclic_term(Q),acyclic_term(R),functor(Q,F,A),functor(R,F,A))),
 1092  (singleValuedInArg(F,N) -> 
 1093    (get_assertion_head_arg(N,Q,Was),replace_arg(Q,N,NEW,R));
 1094    ((get_assertion_head_arg(N,Q,Was),is_ftNonvar(Was)) -> replace_arg(Q,N,NEW,R);
 1095        (N=A,get_assertion_head_arg(N,Q,Was),replace_arg(Q,N,NEW,R)))).
 1096*/
 which_missing_argnum(+VALUE1, ?VALUE2) is semidet
Which Missing Argnum.
 1103which_missing_argnum(Q,N):- compound(Q),\+ compound_name_arity(Q,_,0),
 1104 must((acyclic_term(Q),is_ftCompound(Q),get_functor(Q,F,A))),
 1105 F\=t,
 1106  (call_u(singleValuedInArg(F,N)) -> true; which_missing_argnum(Q,F,A,N)).
 1107
 1108which_missing_argnum(_,_,1,_):-!,fail.
 1109which_missing_argnum(Q,_F,A,N):- between(A,1,N),get_assertion_head_arg(N,Q,Was),is_ftNonvar(Was).
 1110
 1111mpred_run_pause:- asserta(t_l:mpred_run_paused).
 1112mpred_run_resume:- retractall(t_l:mpred_run_paused).
 1113
 1114without_running(G):- (t_l:mpred_run_paused->G;locally_tl(mpred_run_pause,G)).
 1115
 1116mpred_remove_file_support(_File):- !.
 1117mpred_remove_file_support(File):- 
 1118  forall((filematch(File,File0),freeze(Match,contains_var(File0,Match))),
 1119      forall(lookup_u(spft( W, Match, ax)),forall(retract_u(spft( W, Match, ax)),mpred_remove(W)))).
 1120
 1121/*
 1122
 1123%% remove_if_unsupported( +Why, ?P) is semidet.
 1124%
 1125% Remove If Unsupported.
 1126%
 1127remove_if_unsupported(Why,P) :- is_ftVar(P),!,trace_or_throw_ex(warn(var_remove_if_unsupported(Why,P))).
 1128remove_if_unsupported(Why,P) :- ((\+ ground(P), P \= (_:-_) , P \= ~(_) ) -> mpred_trace_msg(warn(nonground_remove_if_unsupported(Why,P))) ;true),
 1129   (((mpred_tms_supported(local,P,How),How\=unknown(_)) -> mpred_trace_msg(still_supported(How,Why,local,P)) ; (  mpred_undo(Why,P)))),!.
 1130   % mpred_run.
 1131
 1132*/
 1133
 1134%= mpred_tms_supported(+P,-How) succeeds if P is "supported". What "How" means
 1135%= depends on the TMS mode selected.
 mpred_tms_supported(+P, ?How) is semidet
PFC Truth Maintainence/wff Supported.
 1142mpred_tms_supported(P,How) :-
 1143  lookup_u(tms(Mode)),
 1144  mpred_tms_supported0(Mode,P,How).
 mpred_tms_supported(+Mode, ?P, ?How) is semidet
PFC Truth Maintainence/wff Supported.
 1152mpred_tms_supported(Mode,P,How) :- is_ftVar(Mode),get_tms_mode(P,tms(Mode)),!,mpred_tms_supported0(Mode,P,How).
 1153mpred_tms_supported(Mode,P,How) :- mpred_tms_supported0(Mode,P,How).
 1154mpred_tms_supported(How,_P,unknown(How)).
 1155
 1156:- module_transparent((mpred_wfflist)/2). 1157:- module_transparent((mpred_wff)/3).
 mpred_tms_supported0(+TmsMode, ?P, ?How) is semidet
PFC Truth Maintainence/wff Supported Primary Helper.
 1164mpred_tms_supported0(local,P,How) :-  mpred_get_support(P,How). % ,sanity(mpred_deep_support(How,S)).
 1165mpred_tms_supported0(cycles,P,How) :-  well_founded(P,How).
 1166mpred_tms_supported0(deep,P,How) :- mpred_deep_support(How,P).
 1167
 1168% baseKB:hook_one_minute_timer_tick:- statistics.
 well_founded(+Fact, ?How) is semidet
a fact is well founded if it is supported by the user or by a set of facts and a rules, all of which are well founded.
 1175well_founded(Fact,How) :- mpred_wff(Fact,[],How).
 mpred_wff(?F, ?VALUE2, :TermHow) is semidet
PFC Well-formed Formula.
 1183mpred_wff(F,_,How) :-
 1184  % supported by user (mpred_axiom) or an "absent" fact (assumption).
 1185  ((mpred_axiom(F),How =mpred_axiom(F) ); (mpred_assumption(F),How=mpred_assumption(F))),
 1186  !.
 1187
 1188mpred_wff(F,Descendants,wff(Supporters)) :-
 1189  % first make sure we aren''t in a loop.
 1190  (\+ memberchk(F,Descendants)),
 1191  % find a justification.
 1192  supporters_list(F,Supporters),
 1193  % all of whose members are well founded.
 1194  mpred_wfflist(Supporters,[F|Descendants]),
 1195  !.
 mpred_wfflist(+L1, ?L2) is semidet
simply maps mpred_wff over the list.
 1203mpred_wfflist([],_).
 1204mpred_wfflist([X|Rest],L) :-
 1205  mpred_wff(X,L,_How),
 1206  mpred_wfflist(Rest,L).
 mpred_scan_tms(+P) is semidet
PFC Scan Truth Maintainence/wff.
 1213mpred_scan_tms(P):-mpred_get_support(P,(S,SS)),
 1214  (S==SS-> true;
 1215   once((mpred_deep_support(_How,P)->true;
 1216     (mpred_trace_msg(warn(now_maybe_unsupported(mpred_get_support(P,(S,SS)),fail))))))).
 user_atom(+U) is semidet
User Atom.
 1223user_atom(mfl4(_VarNameZ,_,_,_)):-!.
 1224user_atom(ax).
 1225user_atom(s(_)).
 mpred_deep_support(+How, ?M) is semidet
PFC Deep Support.
 1232mpred_deep_support(_How,unbound):-!,fail.
 1233mpred_deep_support(How,M):-loop_check(mpred_deep_support0(How,M),fail).
 mpred_deep_support0(+U, ?U) is semidet
PFC Deep Support Primary Helper.
 1240mpred_deep_support0(user_atom(U),(U,ax)):-user_atom(U),!.
 1241mpred_deep_support0(How,(A==>_)):-!,mpred_deep_support(How,A).
 1242mpred_deep_support0(pt(HowA,HowB),pt(A,B)):-!,mpred_deep_support(HowA,A),mpred_deep_support(HowB,B).
 1243mpred_deep_support0(HowA->HowB,(A->B)):-!,mpred_deep_support(HowA,A),mpred_deep_support(HowB,B).
 1244mpred_deep_support0(HowA/HowB,(A/B)):-!,mpred_deep_support(HowA,A),mpred_deep_support(HowB,B).
 1245mpred_deep_support0((HowA,HowB),(A,B)):-!,mpred_deep_support(HowA,A),mpred_deep_support(HowB,B).
 1246mpred_deep_support0(How,rhs(P)):-!,maplist(mpred_deep_support,How,P).
 1247mpred_deep_support0(mpred_call_only_facts(\+ P),\+ call_u(P)):-!,mpred_call_only_facts(\+ P).
 1248mpred_deep_support0(mpred_call_only_facts(P),call_u(P)):-!,mpred_call_only_facts(P).
 1249mpred_deep_support0(mpred_call_only_facts(P),{P}):-!,mpred_call_only_facts(P).
 1250mpred_deep_support0(S==>How,P):-mpred_get_support(P,S),mpred_deep_support(How,S),!.
 1251mpred_deep_support0(mpred_call_only_facts(\+(P)),\+(P)):-!, mpred_call_only_facts(\+(P)).
 1252mpred_deep_support0(user_atom(P),P):-user_atom(P),!.
 1253mpred_deep_support0(mpred_call_only_facts((P)),P):-mpred_call_only_facts(P).
 mpred_get_support_precanonical_plus_more(+P, ?Sup) is semidet
PFC Get Support Precanonical Plus More.
 1260mpred_get_support_precanonical_plus_more(P,Sup):- 
 1261  mpred_get_support_one(P,Sup)*->true;
 1262  ((fully_expand(mpred_get_support_precanonical_plus_more,P,PE),!,
 1263    P\=@=PE,mpred_get_support_one(PE,Sup))).
 mpred_get_support_one(+P, ?Sup) is semidet
PFC Get Support One.
 1269mpred_get_support_one(P,Sup):- mpred_get_support(P,Sup)*->true;
 1270  (mpred_get_support_via_clause_db(P,Sup)*->true;
 1271     mpred_get_support_via_sentence(P,Sup)).
 mpred_get_support_via_sentence(+Var, ?VALUE2) is semidet
PFC Get Support Via Sentence.
 1278mpred_get_support_via_sentence(Var,_):-is_ftVar(Var),!,fail.
 1279mpred_get_support_via_sentence((A,B),(FC,TC)):-!, mpred_get_support_precanonical_plus_more(A,(FA,TA)),mpred_get_support_precanonical_plus_more(B,(FB,TB)),conjoin(FA,FB,FC),conjoin(TA,TB,TC).
 1280mpred_get_support_via_sentence(true,g):-!.
 1281mpred_get_support_via_sentence(G,call_u(G)):- call_u(G).
 mpred_get_support_via_clause_db(:TermP, ?OUT) is semidet
PFC Get Support Via Clause Database.
 1289mpred_get_support_via_clause_db(\+ P,OUT):- mpred_get_support_via_clause_db(~(P),OUT).
 1290mpred_get_support_via_clause_db(\+ P,(naf(g),g)):- !, predicate_property(P,number_of_clauses(_)),\+ clause(P,_Body).
 1291mpred_get_support_via_clause_db(P,OUT):- predicate_property(P,number_of_clauses(N)),N>0,
 1292   clause_u(P,Body),(Body==true->Sup=(g);
 1293    (support_ok_via_clause_body(P),mpred_get_support_precanonical_plus_more(Body,Sup))),
 1294   OUT=(Sup,g).
 support_ok_via_clause_body(+H) is semidet
Support Ok Via Clause Body.
 1302support_ok_via_clause_body(_H):-!,fail.
 1303support_ok_via_clause_body(H):- get_functor(H,F,A),support_ok_via_clause_body(H,F,A).
 support_ok_via_clause_body(+VALUE1, ?F, ?VALUE3) is semidet
Support Ok Via Clause Body.
 1310support_ok_via_clause_body(_,(\+),1):-!,fail.
 1311support_ok_via_clause_body(_,F,_):- lookup_u(rtArgsVerbatum(F)),!,fail.
 1312support_ok_via_clause_body(H,F,A):- should_call_for_facts(H,F,A).
 mpred_get_support_precanonical(+F, ?Sup) is semidet
PFC Get Support Precanonical.
 1321mpred_get_support_precanonical(F,Sup):-fully_expand(mpred_get_support_precanonical,F,P),mpred_get_support(P,Sup).
 spft_precanonical(+F, ?SF, ?ST) is semidet
Spft Precanonical.
 1328spft_precanonical(F,SF,ST):- fully_expand(spft_precanonical,F,P),!,mpred_get_support(P,(SF,ST)).
 trigger_supporters_list(+U, :TermARG2) is semidet
Trigger Supports Functor (list Version).
 1335trigger_supporters_list(U,[]) :- match_source_ref1(U),!.
 1336trigger_supporters_list(U,[]) :- atom(U),!.
 1337
 1338trigger_supporters_list(Trigger,[Fact|MoreFacts]) :-
 1339  mpred_get_support_precanonical_plus_more(Trigger,(Fact,AnotherTrigger)),
 1340  must(trigger_supporters_list(AnotherTrigger,MoreFacts)).
 1341
 1342mpred_retry(G):- fail; quietly(G).
 {}(?G) is semidet
an escape construct for bypassing the FOL''s salient body goals.
 1350:- meta_predicate('{}'(*)). 1351:- module_transparent( ({})/1). 1352'{}'(G):- call_u(G).
 1353:- sexport(({})/1).
 neg_in_code(+G) is semidet
Negated In Code.
 1359:- meta_predicate neg_in_code(*). 1360:- export(neg_in_code/1). 1361neg_in_code(G):- no_repeats(loop_check(neg_in_code0(G))).
 1362
 1363:- kb_shared(baseKB:proven_neg/1). 1364
 1365:- meta_predicate neg_in_code0(*). 1366:- export(neg_in_code0/1). 1367/*
 1368neg_in_code0(G):- cwc, loop_check(proven_neg(G)).
 1369neg_in_code0(G):- cwc, var(G),!,loop_check(lookup_u(~ G)).
 1370neg_in_code0(call_u(G)):- !,neg_in_code0(G).
 1371neg_in_code0(~(G)):- nonvar(G),!,  \+ loop_check(~G) ,!.
 1372neg_in_code0(G):-  is_ftNonvar(G), a(prologSingleValued,G),
 1373      must((if_missing_mask(G,R,Test),nonvar(R),nonvar(Test))),call_u(R),!,call_u(Test).
 1374neg_in_code0(G):- cwc, clause(~G,Call)*-> call_u(Call).
 1375*/
 1376neg_in_code0(G):- loop_check(neg_may_naf(G)), \+ loop_check(G),!.
 1377% neg_in_code0(_:G):-!,baseKB:neg_in_code0(G).
 1378
 1379
 1380:- meta_predicate neg_may_naf(*). 1381:- module_transparent(neg_may_naf/1). 1382:- export(neg_may_naf/1).
 neg_may_naf(:GoalP) is semidet
Negated May Negation-by-faliure.
 1388neg_may_naf(P):- mpred_non_neg_literal(P),get_functor(P,F),clause_u(prologNegByFailure(F),true),!.
 1389neg_may_naf(P):- is_ftCompound(P),predicate_property(P,static).
 call_u_req(+G) is semidet
Req.
 1396call_u_req(G):- loop_check(mpred_call_0(G),fail).
 mpred_call_only_facts(:Fact) is nondet
 mpred_call_only_facts(+Why, :Fact) is nondet
PFC Call Only Facts.

is true iff Fact is a fact available for forward chaining.

Note that this has the side effect [maybe] of catching unsupported facts and assigning them support from God. (g,ax)

 1409mpred_call_only_facts(_Why,Clause):- mpred_call_only_facts(Clause).
 1410mpred_call_only_facts(Clause) :-  strip_module(Clause,_,ClauseF), on_x_debug(no_repeats(loop_check(mpred_call_0(ClauseF),fail))). 
 mpred_call_0(+Var) is semidet
PFC call Primary Helper.
 1417mpred_call_0(Var):-is_ftVar(Var),!,mpred_call_with_no_triggers(Var).
 1418mpred_call_0(M):-fixed_negations(M,O),!,mpred_call_0(O).
 1419mpred_call_0(U:X):-U==user,!,mpred_call_0(X).
 1420mpred_call_0(t(A,B)):-(atom(A)->true;(no_repeats(arity_no_bc(A,1)),atom(A))),ABC=..[A,B],mpred_call_0(ABC).
 1421mpred_call_0(isa(B,A)):-(atom(A)->true;(call_u(tCol(A)),atom(A))),ABC=..[A,B],mpred_call_0(ABC).
 1422%mpred_call_0(t(A,B)):-!,(atom(A)->true;(no_repeats(arity_no_bc(A,1)),atom(A))),ABC=..[A,B],mpred_call_0(ABC).
 1423mpred_call_0(t(A,B,C)):-!,(atom(A)->true;(no_repeats(arity_no_bc(A,2)),atom(A))),ABC=..[A,B,C],mpred_call_0(ABC).
 1424mpred_call_0(t(A,B,C,D)):-!,(atom(A)->true;(no_repeats(arity_no_bc(A,3)),atom(A))),ABC=..[A,B,C,D],mpred_call_0(ABC).
 1425mpred_call_0(t(A,B,C,D,E)):-!,(atom(A)->true;(no_repeats(arity_no_bc(A,4)),atom(A))),ABC=..[A,B,C,D,E],mpred_call_0(ABC).
 1426mpred_call_0((C1,C2)):-!,mpred_call_0(C1),mpred_call_0(C2).
 1427mpred_call_0((C1;C2)):-!,(mpred_call_0(C1);mpred_call_0(C2)).
 1428mpred_call_0((C1->C2;C3)):-!,(mpred_call_0(C1)->mpred_call_0(C2);mpred_call_0(C3)).
 1429mpred_call_0((C1*->C2;C3)):-!,(mpred_call_0(C1)*->mpred_call_0(C2);mpred_call_0(C3)).
 1430mpred_call_0((C1->C2)):-!,(mpred_call_0(C1)->mpred_call_0(C2)).
 1431mpred_call_0((C1*->C2)):-!,(mpred_call_0(C1)*->mpred_call_0(C2)).
 1432mpred_call_0(call(X)):- !, mpred_call_0(X).
 1433mpred_call_0(call_u(X)):- !, mpred_call_0(X).
 1434mpred_call_0(\+(X)):- !, \+ mpred_call_0(X).
 1435mpred_call_0(call_u(X)):- !, mpred_call_0(X).
 1436mpred_call_0(clause(H,B,Ref)):-!,clause_u(H,B,Ref).
 1437mpred_call_0(clause(H,B)):-!,clause_u(H,B).
 1438mpred_call_0(clause(HB)):-expand_to_hb(HB,H,B),!,clause_u(H,B).
 1439mpred_call_0(asserta(X)):- !, mpred_aina(X).
 1440mpred_call_0(assertz(X)):- !, mpred_ainz(X).
 1441mpred_call_0(assert(X)):- !, mpred_ain(X).
 1442mpred_call_0(retract(X)):- !, mpred_prolog_retract(X).
 1443mpred_call_0(retractall(X)):- !, mpred_prolog_retractall(X).
 1444
 1445% TODO: test removal
 1446%mpred_call_0(prologHybrid(H)):-get_functor(H,F),!,isa_asserted(F,prologHybrid).
 1447
 1448mpred_call_0((H)):- !, call(H).
 1449
 1450mpred_call_0((H)):- is_static_predicate(H),!,call(H).
 1451mpred_call_0((H)):- is_static_predicate(H),!,show_pred_info(H),dtrace(mpred_call_0((H))).
 1452
 1453%mpred_call_0(HB):-quietly((full_transform_warn_if_changed(mpred_call_0,HB,HHBB))),!,mpred_call_0(HHBB).
 1454mpred_call_0(H):- !, locally_tl(infAssertedOnly(H),call_u(H)).
 1455%mpred_call_0(argIsa(mpred_isa,2,mpred_isa/2)):-  trace_or_throw_ex(mpred_call_0(argIsa(mpred_isa,2,mpred_isa/2))),!,fail.
 1456% TODO: test removal
 1457% mpred_call_0(isa(H,B)):-!,isa_asserted(H,B).
 1458
 1459
 1460
 1461mpred_call_0(M:P):-!,sanity(nonvar(P)),functor(P,F,_),mpred_call_1(M,P,F).
 1462mpred_call_0(G):- strip_module(G,M,P),sanity(nonvar(P)),functor(P,F,_),mpred_call_1(M,P,F).
 mpred_call_1(+VALUE1, ?G, ?VALUE3) is semidet
PFC call Secondary Helper.
 1470mpred_call_1(_,G,_):- is_side_effect_disabled,!,mpred_call_with_no_triggers(G).
 1471
 1472mpred_call_1(M,G,F):- sanity(\+  is_side_effect_disabled),
 1473               (ground(G); \+ current_predicate(_,M:G) ; \+ (predicate_property(M:G,number_of_clauses(CC)),CC>1)), 
 1474    
 1475                ignore((loop_check(call_with_bc_triggers(M:G)),maybeSupport(G,(g,ax)),fail)),
 1476                 \+ current_predicate(F,M:G),\+ current_predicate(_,_:G),
 1477                 doall(show_call(predicate_property(_UM:G,_PP))),
 1478                 debug_logicmoo(logicmoo(_)),
 1479                 fail,
 1480                 %TODO remove this failure
 1481                 must(show_call(kb_shared(M:G))),
 1482                 kb_shared(M:G),!,fail.
 1483mpred_call_1(_,G,_):- mpred_call_with_no_triggers(G).
 1484
 1485
 1486:- thread_local t_l:infBackChainPrevented/1.
 call_with_bc_triggers(+MP) is semidet
Call Using Backchaining Triggers.
 1493call_with_bc_triggers(MP) :- strip_module(MP,_,P), functor(P,F,A), \+ t_l:infBackChainPrevented(F/A), 
 1494  lookup_u(bt(P,Trigger)),
 1495  no_repeats(mpred_get_support(bt(P,Trigger),S)),
 1496  once(no_side_effects(P)),
 1497  locally_tl(infBackChainPrevented(F/A),mpred_eval_lhs(Trigger,S)).
 mpred_call_with_no_triggers(+Clause) is semidet
PFC Call Using No Triggers.
 1504mpred_call_with_no_triggers(Clause) :-  strip_module(Clause,_,F),
 1505  %= this (is_ftVar(F)) is probably not advisable due to extreme inefficiency.
 1506  (is_ftVar(F)    ->  mpred_facts_and_universe(F) ;
 1507     mpred_call_with_no_triggers_bound(F)).
 mpred_call_with_no_triggers_bound(+F) is semidet
PFC Call Using No Triggers Bound.
 1514mpred_call_with_no_triggers_bound(F):- mpred_call_with_no_triggers_uncaugth(F).
 mpred_call_with_no_triggers_uncaugth(+Clause) is semidet
PFC Call Using No Triggers Uncaugth.
 1520mpred_call_with_no_triggers_uncaugth(Clause) :-  strip_module(Clause,_,F),
 1521  show_failure(mpred_call_with_no_triggers_bound,no_side_effects(F)),
 1522  (\+ current_predicate(_,F) -> fail;call_u(F)).
 1523  %= we check for system predicates as well.
 1524  %has_cl(F) -> (clause_u(F,Condition),(Condition==true->true;call_u(Condition)));
 1525  %call_u(F).
 mpred_bc_only(+M) is semidet
PFC Backchaining Only.
 1533%mpred_bc_only(G):- !,defaultAssertMt(W), loop_check(mpred_BC_w_cache(W,G)).
 1534%mpred_bc_only(M:G):- !, loop_check(with_umt(M,mpred_bc_only0(G))).
 1535mpred_bc_only(G):- no_repeats(loop_check(mpred_bc_only0(G))).
 mpred_bc_only(+M) is semidet
PFC Backchaining + FACTS + Inheritance.
 1541mpred_bc_and_with_pfc(G):- no_repeats(loop_check(mpred_bc_and_with_pfc_0(G))).
 1542
 1543mpred_bc_and_with_pfc_0(G):- mpred_call_only_facts(G). % was missing
 1544mpred_bc_and_with_pfc_0(G):- mpred_bc_only0(G).
 1545mpred_bc_and_with_pfc_0(G):- strip_module(G,M,P),inherit_above(M,P).
 1546
 1547
 1548
 1549
 1550% % :- '$set_source_module'(mpred_kb_ops).
 mpred_bc_only0(+G) is semidet
PFC Backchaining Only Primary Helper.
 1556mpred_bc_only0(G):- mpred_unnegate(G,Pos),!, show_call(why,\+ mpred_bc_only(Pos)).
 1557% mpred_bc_only0(G):- pfcBC_NoFacts(G).
 1558mpred_bc_only0(G):- mpred_BC_w_cache(G,G).
 1559
 1560% mpred_bc_only0(G):- mpred_call_only_facts(G).
= pfcBC_NoFacts(F) is true iff F is a fact available for backward chaining ONLY. = Note that this has the side effect of catching unsupported facts and = assigning them support from God. = this Predicate should hide Facts from mpred_bc_only/1 %
 pfcBC_NoFacts(+F) is semidet
Prolog Forward Chaining Backtackable Class No Facts.
 1573pfcBC_NoFacts(F):- pfcBC_NoFacts_TRY(F)*-> true ; (mpred_slow_search,pfcBC_Cache(F)).
 mpred_slow_search is semidet
PFC Slow Search.
 1580mpred_slow_search.
 1581
 1582
 1583/*
 1584%% ruleBackward( +R, ?Condition) is semidet.
 1585%
 1586% Rule Backward.
 1587%
 1588ruleBackward(R,Condition):- call_u(( ruleBackward0(R,Condition),functor(Condition,F,_),
 1589  \+ consequent_arg(_,v(call_u_no_bc,call,call_u),F))).
 1590%ruleBackward0(F,Condition):-clause_u(F,Condition),\+ (is_true(Condition);mpred_is_info(Condition)).
 1591
 1592%% ruleBackward0( +F, ?Condition) is semidet.
 1593%
 1594% Rule Backward Primary Helper.
 1595%
 1596ruleBackward0(F,Condition):- call_u((  '<-'(F,Condition),\+ (is_true(Condition);mpred_is_info(Condition)) )).
 1597
 1598%{X}:-dmsg(legacy({X})),call_u(X).
 1599*/
 pfcBC_NoFacts_TRY(+F) is semidet
Prolog Forward Chaining Backtackable Class No Facts Try.
 1608pfcBC_NoFacts_TRY(F) :- no_repeats(ruleBackward(F,Condition,Support)),
 1609  % neck(F),
 1610  copy_term((Condition,Support),(CCondition,SupportC)),
 1611  no_repeats(F,call_u(Condition)),  
 1612  maybe_support_bt(F,CCondition,SupportC).
 1613
 1614maybe_support_bt(P,_,_):-mpred_ignored(P),!.
 1615maybe_support_bt(F,Condition,Support):-  
 1616  doall((no_repeats(Why,call_u(bt(F,pt(A,Why)))) *-> mpred_add_support_fast(F,(A,Why)))),
 1617  doall((no_repeats(Why,call_u(bt(F,Why))) *-> mpred_add_support_fast(F,(bt(F,Why),Support)))),
 1618  ignore((maybeSupport(F,(Condition,Support)))).
 1619
 1620:- meta_predicate mpred_why_all(*). 1621mpred_why_all(Call):- !,
 1622      call_u(Call),
 1623      doall((
 1624        lookup_u(Call),
 1625        ignore(show_failure(mpred_why(Call))),
 1626        dmsg(result=Call),nl)),
 1627   forall(Call,ignore(show_failure(mpred_why(Call)))).
 1628
 1629mpred_why_all(Call):-
 1630      doall((
 1631        call_u(Call),
 1632        ignore(show_failure(mpred_why(Call))),
 1633        dmsg(result=Call),nl)).
 pfcBC_Cache(+F) is semidet
Prolog Forward Chaining Backtackable Class Cache.
 1641pfcBC_Cache(F) :- mpred_call_only_facts(pfcBC_Cache,F),
 1642   ignore((ground(F),( (\+mpred_call_0(F)), maybeSupport(F,(g,ax))))).
 maybeSupport(+P, ?VALUE2) is semidet
Maybe Support.
 1650maybeSupport(P,_):-mpred_ignored(P),!.
 1651maybeSupport(P,S):- fail, ( \+ ground(P)-> true;
 1652  (predicate_property(P,dynamic)->mpred_post(P,S);true)).
 1653
 1654maybeSupport(P,S):- 
 1655   mpred_add_support_fast(P,S),
 1656   maybeMaybeAdd(P,S).
 1657 
 1658maybeMaybeAdd(P,_):- \+ predicate_property(P,dynamic),!.
 1659maybeMaybeAdd(P,_):- \+ \+ clause_u(P,true),!.
 1660maybeMaybeAdd(P,S):- 
 1661 locally_tl(assert_to(a),
 1662    assert_u_confirmed_was_missing(P)),
 1663   mpred_trace_op(add,P,S),
 1664   mpred_enqueue(P,S).
 mpred_ignored(:TermC) is semidet
PFC Ignored.
 1671mpred_ignored(argIsa(F, A, argIsaFn(F, A))).
 1672mpred_ignored(genls(A,A)).
 1673mpred_ignored(isa(tCol,tCol)).
 1674%mpred_ignored(isa(W,tCol)):-mreq(baseKB:hasInstance_dyn(tCol,W)).
 1675mpred_ignored(isa(W,_)):-is_ftCompound(W),call_u(isa(W,meta_argtypes)).
 1676mpred_ignored(C):-clause_safe(C,true). 
 1677mpred_ignored(isa(_,Atom)):-atom(Atom),atom_concat(ft,_,Atom),!.
 1678mpred_ignored(isa(_,argIsaFn(_, _))).
 has_cl(+H) is semidet
Has Clause.
 1686has_cl(H):-predicate_property(H,number_of_clauses(_)).
 1687
 1688% an action is undoable if there exists a method for undoing it.
 mpred_negation_w_neg(+P, ?NF) is semidet
PFC Negation W Negated.
 1695mpred_negation_w_neg(~(P),P):-is_ftNonvar(P),!.
 1696mpred_negation_w_neg(P,NF):-mpred_nf1_negation(P,NF).
 hook_one_minute_timer_tick is semidet
Hook To [hook_one_minute_timer_tick/0] For Module Mpred_pfc. Hook One Minute Timer Tick.
 1704baseKB:hook_one_minute_timer_tick:-mpred_cleanup.
 mpred_cleanup is semidet
PFC Cleanup.
 1711mpred_cleanup:- forall((no_repeats(F-A,(call_u(mpred_prop(M,F,A,pfcRHS)),A>1))),mpred_cleanup(M,F,A)).
 mpred_cleanup(M, +F, ?A) is semidet
PFC Cleanup.
 1718mpred_cleanup(M,F,A):-functor(P,F,A),predicate_property(P,dynamic)->mpred_cleanup_0(M,P);true.
 mpred_cleanup_0(M, +P) is semidet
PFC cleanup Primary Helper.
 1725mpred_cleanup_0(M,P):- findall(P-B-Ref,M:clause(P,B,Ref),L),
 1726  M:forall(member(P-B-Ref,L),erase_w_attvars(clause(P,B,Ref),Ref)),forall(member(P-B-Ref,L),M:attvar_op(db_op_call(assertz,assertz_if_new),((P:-B)))).
 1727
 1728% :-debug.
 1729%isInstFn(A):-!,trace_or_throw_ex(isInstFn(A)).
 1730
 1731%= mpred_unnegate(N,P) is true if N is a negated term and P is the term
 1732%= with the negation operator stripped.
 1733
 1734/*
 1735%% mpred_unnegate( +P, ?P) is semidet.
 1736%
 1737% PFC Negation.
 1738%
 1739mpred_unnegate((-P),P).
 1740% mpred_unnegate((~P),P).
 1741mpred_unnegate((\+(P)),P).
 1742*/
 1743/*
 1744
 1745%% mpred_negated_literal( +P) is semidet.
 1746%
 1747% PFC Negated Literal.
 1748%
 1749mpred_negated_literal(P):-is_reprop(P),!,fail.
 1750mpred_negated_literal(P):-mpred_negated_literal(P,_).
 1751
 1752%% mpred_negated_literal( +P, ?Q) is semidet.
 1753%
 1754% PFC Negated Literal.
 1755%
 1756mpred_negated_literal(P,Q) :- is_ftNonvar(P),
 1757  mpred_unnegate(P,Q),
 1758  mpred_literal(Q).
 1759
 1760*/
 mpred_is_assertable(+X) is semidet
PFC If Is A Assertable.
 1766mpred_is_assertable(X):- mpred_literal_nv(X),\+ functor(X,{},_).
 mpred_literal_nv(+X) is semidet
PFC Literal Nv.
 1772mpred_literal_nv(X):-is_ftNonvar(X),mpred_literal(X).
 1773
 1774/*
 1775%% mpred_literal( +X) is semidet.
 1776%
 1777% PFC Literal.
 1778%
 1779mpred_literal(X) :- is_reprop(X),!,fail.
 1780mpred_literal(X) :- cyclic_term(X),!,fail.
 1781mpred_literal(X) :- atom(X),!.
 1782mpred_literal(X) :- mpred_negated_literal(X),!.
 1783mpred_literal(X) :- mpred_positive_literal(X),!.
 1784mpred_literal(X) :- is_ftVar(X),!.
 1785
 1786*/
 is_reprop(+X) is semidet
If Is A Reprop.
 1792is_reprop(X):- compound(X),is_reprop_0(X).
 is_reprop_0(+X) is semidet
If Is A reprop Primary Helper.
 1798is_reprop_0(~(X)):-!,is_reprop(X).
 1799is_reprop_0(X):-get_functor(X,repropagate,_).
 mpred_non_neg_literal(+X) is semidet
PFC Not Negated Literal.
 1806mpred_non_neg_literal(X):- is_reprop(X),!,fail.
 1807mpred_non_neg_literal(X):- atom(X),!.
 1808mpred_non_neg_literal(X):- sanity(stack_check),
 1809    mpred_positive_literal(X), X \= ~(_), X \= mpred_prop(_,_,_,_), X \= conflict(_).
 1810
 1811% ======================= mpred_file('pfcsupport').	% support maintenance
 is_relative(:TermV) is semidet
If Is A Relative.
 1818is_relative(V):- (\+is_ftCompound(V)),!,fail.
 1819is_relative(update(_)).
 1820is_relative(replace(_)).
 1821is_relative(rel(_)).
 1822is_relative(+(X)):- \+ is_ftVar(X).
 1823is_relative(-(X)):- \+ is_ftVar(X).
 1824is_relative(*(X)):- \+ is_ftVar(X).
 1825
 1826/*
 1827% TODO not called yet
 1828%= mpred_get_trigger_key(+Trigger,-Key)
 1829%=
 1830%= Arg1 is a trigger.  Key is the best term to index it on.
 1831
 1832mpred_get_trigger_key(pt(Key,_),Key).
 1833mpred_get_trigger_key(pk(Key,_,_),Key).
 1834mpred_get_trigger_key(nt(Key,_,_),Key).
 1835mpred_get_trigger_key(Key,Key).
 1836*/
 1837
 1838/*
 1839
 1840the FOL i get from SUMO, CycL, UMBEL and many *non* RDF ontologies out there.. i convert to Datalog..  evidently my conversion process is unique as it preserves semantics most by the book conversions gave up on. 
 1841
 1842
 1843% TODO not called yet
 1844%=^L
 1845%= Get a key from the trigger that will be used as the first argument of
 1846%= the trigger baseable clause that stores the trigger.
 1847%=
 1848mpred_trigger_key(X,X) :- is_ftVar(X), !.
 1849mpred_trigger_key(chart(word(W),_L),W) :- !.
 1850mpred_trigger_key(chart(stem([Char1|_Rest]),_L),Char1) :- !.
 1851mpred_trigger_key(chart(Concept,_L),Concept) :- !.
 1852mpred_trigger_key(X,X).
 1853*/
 1854% ======================= mpred_file('pfcdb').	% predicates to manipulate database.
 1855
 1856%   File   : pfcdb.pl
 1857%   Author : Tim Finin, finin@prc.unisys.com
 1858%   Author :  Dave Matuszek, dave@prc.unisys.com
 1859%   Author :  Dan Corpron
 1860%   Updated: 10/11/87, ...
 1861%   Purpose: predicates to manipulate a pfc database (e.g. save,
 1862%	restore, reset, etc).
 clause_or_call(+H, ?B) is semidet
Clause Or Call.
 1873clause_or_call(M:H,B):-is_ftVar(M),!,no_repeats(M:F/A,(f_to_mfa(H,M,F,A))),M:clause_or_call(H,B).
 1874clause_or_call(isa(I,C),true):-!,call_u(isa_asserted(I,C)).
 1875clause_or_call(genls(I,C),true):-!,on_x_log_throw(call_u(genls(I,C))).
 1876clause_or_call(H,B):- clause(src_edit(_Before,H),B).
 1877clause_or_call(H,B):- predicate_property(H,number_of_clauses(C)),predicate_property(H,number_of_rules(R)),((R*2<C) -> (clause_u(H,B)*->!;fail) ; clause_u(H,B)).
 1878clause_or_call(H,true):- call_u(should_call_for_facts(H)),no_repeats(on_x_log_throw(H)).
 1879
 1880
 1881% as opposed to simply using clause(H,true).
 should_call_for_facts(+H) is semidet
Should Call For Facts.
 1887should_call_for_facts(H):- get_functor(H,F,A),call_u(should_call_for_facts(H,F,A)).
 should_call_for_facts(+VALUE1, ?F, ?VALUE3) is semidet
Should Call For Facts.
 1893should_call_for_facts(_,F,_):- a(prologSideEffects,F),!,fail.
 1894should_call_for_facts(H,_,_):- modulize_head(H,HH), \+ predicate_property(HH,number_of_clauses(_)),!.
 1895should_call_for_facts(_,F,A):- clause_b(mpred_prop(_M,F,A,pfcRHS)),!,fail.
 1896should_call_for_facts(_,F,A):- clause_b(mpred_prop(_M,F,A,pfcMustFC)),!,fail.
 1897should_call_for_facts(_,F,_):- a(prologDynamic,F),!.
 1898should_call_for_facts(_,F,_):- \+ a(pfcControlled,F),!.
 no_side_effects(+P) is semidet
No Side Effects.
 1906no_side_effects(P):-  (\+ is_side_effect_disabled->true;(get_functor(P,F,_),a(prologSideEffects,F))).
 1907
 1908
 1909:- was_dynamic(functorIsMacro/1).
 compute_resolve(+NewerP, ?OlderQ, ?SU, ?SU, ?OlderQ) is semidet
Compute Resolve.
 1916compute_resolve(NewerP,OlderQ,SU,SU,(mpred_blast(OlderQ),mpred_ain(NewerP,S),mpred_withdraw(conflict(NewerP)))):-
 1917  must(correctify_support(SU,S)),
 1918  wdmsg(compute_resolve(newer(NewerP-S)>older(OlderQ-S))).
 1919compute_resolve(NewerP,OlderQ,S1,[U],Resolve):-compute_resolve(OlderQ,NewerP,[U2],S1,Resolve),match_source_ref1(U),match_source_ref1(U2),!.
 1920compute_resolve(NewerP,OlderQ,SU,S2,(mpred_blast(OlderQ),mpred_ain(NewerP,S1),mpred_withdraw(conflict(NewerP)))):-
 1921  must(correctify_support(SU,S1)),
 1922  wdmsg(compute_resolve((NewerP-S1)>(OlderQ-S2))).
 compute_resolve(+NewerP, ?OlderQ, ?Resolve) is semidet
Compute Resolve.
 1930compute_resolve(NewerP,OlderQ,Resolve):-
 1931   supporters_list(NewerP,S1),
 1932   supporters_list(OlderQ,S2),
 1933   compute_resolve(NewerP,OlderQ,S1,S2,Resolve).
 is_resolved(+C) is semidet
If Is A Resolved.
 1941is_resolved(C):- Why= is_resolved, mpred_call_only_facts(Why,C),\+mpred_call_only_facts(Why,~(C)).
 1942is_resolved(C):- Why= is_resolved, mpred_call_only_facts(Why,~(C)),\+mpred_call_only_facts(Why,C).
 1943
 1944:- must(nop(_)).
 mpred_prove_neg(+G) is semidet
PFC Prove Negated.
 1951mpred_prove_neg(G):-  (dtrace), \+ mpred_bc_only(G), \+ mpred_fact(G).
 pred_head(:PRED1Type, ?P) is semidet
Predicate Head.
 1958pred_head(Type,P):- no_repeats(P,(call(Type,P),\+ nonfact_metawrapper(P),is_ftCompound(P))).
 pred_head_all(+P) is semidet
Predicate Head All.
 1965pred_head_all(P):- pred_head(pred_all,P).
 nonfact_metawrapper(:TermP) is semidet
Nonfact Metawrapper.
 1972nonfact_metawrapper(~(_)).
 1973nonfact_metawrapper(pt(_,_)).
 1974nonfact_metawrapper(bt(_,_,_)).
 1975nonfact_metawrapper(nt(_,_)).
 1976nonfact_metawrapper(spft(_,_,_)).
 1977nonfact_metawrapper(added(_)).
 1978% we use the arity 1 forms is why 
 1979nonfact_metawrapper(term_expansion(_,_)).
 1980nonfact_metawrapper(P):- \+ current_predicate(_,P).
 1981nonfact_metawrapper(M:P):-atom(M),!,nonfact_metawrapper(P).
 1982nonfact_metawrapper(P):- get_functor(P,F,_), 
 1983   (a(prologSideEffects,F);a(rtNotForUnboundPredicates,F)).
 1984nonfact_metawrapper(P):-rewritten_metawrapper(P).
 rewritten_metawrapper(+C) is semidet
Rewritten Metawrapper.
 1991rewritten_metawrapper(_):-!,fail.
 1992%rewritten_metawrapper(isa(_,_)).
 1993rewritten_metawrapper(C):-is_ftCompound(C),functor(C,t,_).
 1994
 1995
 1996%% meta_wrapper_rule( :TermARG1) is semidet.
 1997%
 1998% Meta Wrapper Rule.
 1999%
 2000meta_wrapper_rule((_<-_)).
 2001meta_wrapper_rule((_<==>_)).
 2002meta_wrapper_rule((_==>_)).
 2003meta_wrapper_rule((_:-_)).
 pred_all(+P) is semidet
Predicate All.
 2011pred_all(P):-pred_u0(P).
 2012pred_all(P):-pred_t0(P).
 2013pred_all(P):-pred_r0(P).
 pred_u0(+P) is semidet
Predicate For User Code Primary Helper.
 2020pred_u0(P):-pred_u1(P),has_db_clauses(P).
 2021pred_u0(P):-pred_u2(P).
 pred_u1(+VALUE1) is semidet
Predicate For User Code Secondary Helper.
 2027pred_u1(P):-a(pfcControlled,F),arity_no_bc(F,A),functor(P,F,A).
 2028pred_u1(P):-a(prologHybrid,F),arity_no_bc(F,A),functor(P,F,A).
 2029pred_u1(P):-a(prologDynamic,F),arity_no_bc(F,A),functor(P,F,A).
 pred_u2(+P) is semidet
Predicate For User Code Extended Helper.
 2035pred_u2(P):- compound(P),functor(P,F,A),sanity(no_repeats(arity_no_bc(F,A))),!,has_db_clauses(P).
 2036pred_u2(P):- no_repeats(arity_no_bc(F,A)),functor(P,F,A),has_db_clauses(P).
 has_db_clauses(+PI) is semidet
Has Database Clauses.
 2044has_db_clauses(PI):-modulize_head(PI,P),
 2045   predicate_property(P,number_of_clauses(NC)),\+ predicate_property(P,number_of_rules(NC)), \+ \+ clause_u(P,true).
Predicate True Stucture Primary Helper.
 2053pred_t0(P):-mreq('==>'(P)).
 2054pred_t0(P):-mreq(pt(P,_)).
 2055pred_t0(P):-mreq(bt(P,_)).
 2056pred_t0(P):-mreq(nt(P,_,_)).
 2057pred_t0(P):-mreq(spft(P,_,_)).
 2058
 2059%pred_r0(-(P)):- call_u(-(P)).
 2060%pred_r0(~(P)):- mreq(~(P)).
 2061
 2062
 2063%% pred_r0( :TermP) is semidet.
 2064%
 2065% Predicate R Primary Helper.
 2066%
 2067pred_r0(P==>Q):- mreq(P==>Q).
 2068pred_r0(P<==>Q):- mreq(P<==>Q).
 2069pred_r0(P<-Q):- mreq(P<-Q).
 cnstrn(+X) is semidet
Cnstrn.
 2076:- module_transparent(cnstrn/1). 2077cnstrn(X):-term_variables(X,Vs),maplist(cnstrn0(X),Vs),!.
 cnstrn(+V, ?X) is semidet
Cnstrn.
 2083:- module_transparent(cnstrn/2). 2084cnstrn(V,X):-cnstrn0(X,V).
 cnstrn0(+X, ?V) is semidet
Cnstrn Primary Helper.
 2090:- module_transparent(cnstrn0/2). 2091cnstrn0(X,V):-when(is_ftNonvar(V),X).
 rescan_pfc is semidet
Rescan Prolog Forward Chaining.
 2098rescan_pfc:-forall(clause(baseKB:mpred_hook_rescan_files,Body),show_entry(rescan_pfc,Body)).
 mpred_facts_and_universe(+P) is semidet
PFC Facts And Universe.
 2105mpred_facts_and_universe(P):- (is_ftVar(P)->pred_head_all(P);true),call_u(P). % (meta_wrapper_rule(P)->call_u(P) ; call_u(P)).
 repropagate(:TermP) is semidet
Repropagate.
 2112repropagate(_):-  check_context_module,fail.
 2113%repropagate(P):-  check_real_context_module,fail.
 2114
 2115repropagate(P):-  is_ftVar(P),!.
 2116repropagate(==>P):- !,repropagate(P).
 2117repropagate(P):-  meta_wrapper_rule(P),!,call_u(repropagate_meta_wrapper_rule(P)).
 2118repropagate(P):-  \+ predicate_property(P,_),'$find_predicate'(P,PP),PP\=[],!,forall(member(M:F/A,PP),
 2119                                                          must((functor(Q,F,A),repropagate_1(M:Q)))).
 2120repropagate(F/A):- is_ftNameArity(F,A),!,functor(P,F,A),!,repropagate(P).
 2121repropagate(F/A):- atom(F),is_ftVar(A),!,repropagate(F).
 2122
 2123repropagate(P):-  \+ predicate_property(_:P,_),dmsg(undefined_repropagate(P)),dumpST,dtrace,!,fail.
 2124repropagate(P):-  call_u(repropagate_0(P)).
 2125
 2126
 2127predicate_to_goal(P,Goal):-atom(P),get_arity(P,F,A),functor(Goal,F,A).
 2128predicate_to_goal(PF/A,Goal):-atom(PF),get_arity(PF,F,A),functor(Goal,F,A).
 2129predicate_to_goal(G,G):-compound(G),!.
 repropagate_0(+P) is semidet
repropagate Primary Helper.
 2135repropagate_0(P):- loop_check(call_u(repropagate_1(P)),true).
 2136
 2137:- thread_local t_l:is_repropagating/1.
 repropagate_1(+P) is semidet
repropagate Secondary Helper.
 2144repropagate_1(P):- is_ftVar(P),!.
 2145repropagate_1(==>P):- !,repropagate_1(P).
 2146repropagate_1(USER:P):- USER==user,!,repropagate_1(P).
 2147%repropagate_1((P/_)):-!,repropagate_1(P).
 2148
 2149repropagate_1(P):- call_u(repropagate_2(P)).
 2150
 2151:- export(repropagate_2/1). 2152:- module_transparent(repropagate_2/1).
 repropagate_2(+P) is semidet
repropagate Extended Helper.
 2158repropagate_2(P):-
 2159 call_u(doall((no_repeats((mpred_facts_and_universe(P))),
 2160    locally_tl(is_repropagating(P),ignore((once(show_failure(fwd_ok(P))),show_call(mpred_fwc(P)))))))).
 2161
 2162% repropagate_meta_wrapper_rule(P==>_):- !, repropagate(P).
 repropagate_meta_wrapper_rule(+P) is semidet
Repropagate Meta Wrapper Rule.
 2168repropagate_meta_wrapper_rule(P):-repropagate_1(P).
 fwd_ok(:TermP) is semidet
Forward Repropigated Ok.
 2175fwd_ok(_):-!.
 2176fwd_ok(P):-ground(P),!.
 2177fwd_ok(if_missing1(_,_)).
 2178fwd_ok(idForTest(_,_)).
 2179fwd_ok(clif(_)).
 2180fwd_ok(pfclog(_)).
 2181fwd_ok(X):-compound(X),get_assertion_head_arg(_,X,E),compound(E),functor(E,(:-),_),!.
 2182% fwd_ok(P):-must(ground(P)),!.
 mpred_facts_only(+P) is semidet
PFC Facts Only.
 2189mpred_facts_only(P):- (is_ftVar(P)->(pred_head_all(P),\+ meta_wrapper_rule(P));true),no_repeats(P).
 2190
 2191
 2192
 2193:- thread_local(t_l:user_abox/1). 2194
 2195% :- ((prolog_load_context(file,F),  prolog_load_context(source,F))-> throw(prolog_load_context(source,F)) ; true). :- include('mpred_header.pi').
 2196:- style_check(+singleton). 2197
 2198% TODO READD
 2199%:- foreach(arg(_,isEach(prologMultiValued,prologOrdered,prologNegByFailure,prologPTTP,prologKIF,pfcControlled,ttRelationType,
 2200%     prologHybrid,predCanHaveSingletons,prologDynamic,prologBuiltin,functorIsMacro,prologListValued,prologSingleValued),P),)
 get
 2204% =================================================
 2205% ==============  UTILS BEGIN        ==============
 2206% =================================================
 ruleBackward(+R, ?Condition) is semidet
Rule Backward.
 2212ruleBackward(R,Condition,Support):- ruleBackward0(R,Condition,Support),
 2213  functor(Condition,F,_),\+ arg(_,v(call_u,mpred_bc_only,inherit_above),F).
 ruleBackward0(+F, ?Condition) is semidet
Rule Backward Primary Helper.
 2219ruleBackward0(F,Condition,Support):- call_u('<-'(FF,Condition)),copy_term('<-'(FF,Condition),Support),FF=F.
 2220%ruleBackward0(F,Condition,(F :- Condition)):- clause_u(F,Condition),\+ (is_true(Condition);mpred_is_info(Condition)).
 2221
 2222
 2223% ======================= 
 2224% user''s program''s database
 2225% ======================= 
 assert_mu(+X) is semidet
Assert For User Code.
 2233assert_mu(MH):-  fix_mp(clause(assert,assert_u), MH,M,H),get_unnegated_functor(H,F,A),assert_mu(M,H,F,A).
 2234asserta_mu(MH):- fix_mp(clause(assert,asserta_u),MH,M,H),asserta_mu(M,H).
 2235assertz_mu(MH):- fix_mp(clause(assert,assertz_u),MH,M,H),assertz_mu(M,H).
 2236
 2237
 2238% :- kb_shared(baseKB:singleValuedInArg/2).
 2239:- thread_local(t_l:assert_to/1).
 assert_mu(+Module, +Pred, ?Functor, ?Arity) is semidet
Assert For User Code.
 2245assert_mu(M,M2:Pred,F,A):- M == M2,!, assert_mu(M,Pred,F,A).
 2246% maYBE assert_mu(M,(M2:Pred :- B),F,A):- M == M2,!, assert_mu(M,(Pred :- B),F,A).
 2247assert_mu(M,_:Pred,F,A):- dtrace,sanity(\+ is_ftVar(Pred)),!, assert_mu(M,Pred,F,A).
 2248assert_mu(M,(Pred:- (AWC,More)),_,_):- AWC == awc,!,asserta_mu(M,(Pred:- (AWC,More))).
 2249assert_mu(M,(Pred:- (ZWC,More)),_,_):- ZWC == zwc,!,assertz_mu(M,(Pred:- (ZWC,More))).
 2250%assert_mu(M,Pred,F,_):- clause_b(singleValuedInArg(F,SV)),!,must(update_single_valued_arg(M,Pred,SV)),!.
 2251%assert_mu(M,Pred,F,A):- a(prologSingleValued,F),!,must(update_single_valued_arg(M,Pred,A)),!.
 2252assert_mu(M,Pred,F,_):- a(prologOrdered,F),!,assertz_mu(M,Pred).
 2253assert_mu(M,Pred,_,_):- t_l:assert_to(Where),!, (Where = a -> asserta_mu(M,Pred); assertz_mu(M,Pred)).
 2254%assert_mu(M,Pred,_,1):- !, assertz_mu(M,Pred),!.
 2255assert_mu(M,Pred,_,_):- assertz_mu(M,Pred).
 2256
 2257
 2258:-thread_local(t_l:side_effect_ok/0).
 assertz_mu(+M, ?X) is semidet
Assertz Module Unit.

assertz_mu(abox,X):-!,defaultAssertMt(M),!,assertz_mu(M,X). assertz_mu(M,X):- check_never_assert(M:X), clause_asserted_u(M:X),!. assertz_mu(M,X):- correct_module(M,X,T),T\==M,!,assertz_mu(T,X). assertz_mu(_,X):- must(defaultAssertMt(M)),!,must((expire_tabled_list(M:X),show_call(attvar_op(db_op_call(assertz,assertz_i),M:X)))).

 2271assertz_mu(_,X):- check_never_assert(X),fail.
 2272%assertz_mu(M,M2:Pred,F,A):- M == M2,!, assertz_mu(M,Pred,F,A).
 2273%assertz_mu(M,spft(P,mfl4(VarNameZ,KB,F,L),T)):-M\==KB,!,assertz_mu(KB,spft(P,mfl4(VarNameZ,KB,F,L),T)).
 2274assertz_mu(M,X):- strip_module(X,_,P), %sanity(check_never_assert(M:P)), 
 2275    must((expire_tabled_list(M:P),show_failure(attvar_op(db_op_call(assertz,assertz_i),M:P)))).
 2276   %(clause_asserted_u(M:P)-> true; must((expire_tabled_list(M:P),show_failure(attvar_op(db_op_call(assertz,assertz_i),M:P))))).
 asserta_mu(+M, ?X) is semidet
Asserta Module Unit.

asserta_mu(abox,X):-!,defaultAssertMt(M),!,asserta_mu(M,X). asserta_mu(M,X):- correct_module(M,X,T),T\==M,!,asserta_mu(T,X). asserta_mu(_,X):- must(defaultAssertMt(M)),!,must((expire_tabled_list(M:X),show_failure(attvar_op(db_op_call(assertz,assertz_i),M:X)))).

 2286asserta_mu(_,X):- check_never_assert(X),fail.
 2287asserta_mu(M,X):- strip_module(X,_,P),!, %sanity(check_never_assert(M:P)), 
 2288    must((expire_tabled_list(M:P),show_failure(attvar_op(db_op_call(asserta,asserta_i),M:P)))).
 2289   %(clause_asserted_u(M:P)-> true; must((expire_tabled_list(M:P),show_failure(attvar_op(db_op_call(asserta,asserta_i),M:P))))).
 retract_mu(:TermX) is semidet
Retract For User Code.

retract_mu(que(X,Y)):-!,show_failure(why,retract_eq_quitely_f(que(X,Y))),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).

 2297retract_mu(H0):- throw_depricated, strip_module(H0,_,H),defaultAssertMt(M),show_if_debug(attvar_op(db_op_call(retract,retract_i),M:H)),!,must((expire_tabled_list(H))).
 2298retract_mu(X):- check_never_retract(X),fail.
 2299retract_mu(~(X)):-!,show_success(why,retract_eq_quitely_f(~(X))),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).
 2300retract_mu((X)):-!,show_success(why,retract_eq_quitely_f((X))),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).
 2301retract_mu(M:(H:-B)):- atom(M),!, clause_u(H,B,R),erase(R).
 2302retract_mu((H:-B)):-!, clause_u(H,B,R),erase(R).
 2303%retract_mu(~(X)):-must(is_ftNonvar(X)),!,retract_eq_quitely_f(~(X)),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).
 2304%retract_mu(hs(X)):-!,retract_eq_quitely_f(hs(X)),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).
 2305
 2306
 2307
 2308
 2309
 2310:- retractall(t_l:mpred_debug_local). 2311:- thread_local(t_l:in_rescan_mpred_hook/0). 2312
 2313:- module_transparent(mpred_call_0/1). 2314
 2315 :- meta_predicate update_single_valued_arg(+,+,*). 2316 :- meta_predicate assert_mu(*,+,*,*). 2317 :- meta_predicate mpred_facts_and_universe(*). 2318 :- meta_predicate {*}. 2319 :- meta_predicate neg_in_code0(*). 2320 :- meta_predicate repropagate_2(*). 2321 :- meta_predicate mpred_get_support_via_sentence(*,*). 2322
 2323:- dynamic(infoF/1). 2324
 2325
 2326
 2327mpred_kb_ops_file.
 2328
 2329:- fixup_exports.