1/*  
    2
    3% File used as storage place for all predicates which change as
    4% the world is run.
    5%
    6% props(Obj,height(ObjHt))  == k(height,Obj,ObjHt) == rdf(Obj,height,ObjHt) == height(Obj,ObjHt)
    7% padd(Obj,height(ObjHt))  == padd(height,Obj,ObjHt,...) == add(QueryForm)
    8% kretract[all](Obj,height(ObjHt))  == kretract[all](Obj,height,ObjHt) == pretract[all](height,Obj,ObjHt) == del[all](QueryForm)
    9% keraseall(AnyTerm).
   10%
   11%
   12% Dec 13, 2035
   13% Douglas Miles
   14*/
   15% File: /opt/PrologMUD/pack/logicmoo_base/prolog/logicmoo/mpred/mpred_kb_ops.pl
   16%:- if(( ( \+ ((current_prolog_flag(logicmoo_include,Call),Call))) )).
   17hide_this_mpred_kb_ops :- fail, nop( module(mpred_kb_ops,[])).
   18
   19
   20:- include('mpred_header.pi').   21
   22
   23:- user:use_module(library(clpfd),['#='/2]).
 get_arity(:TermTerm, ?F, ?A) is semidet
Get Arity.
   28get_arity(Term,F,A):- atom(Term),F=Term,!,ensure_arity(F,A).
   29get_arity(F/A,F,A):-!,atom(F),ensure_arity(F,A),!,(A>0).
   30get_arity(F // A,F,A2):- must(integer(A)),!, atom(F), is(A2 , A+2), ensure_arity(F,A2),!,(A2>0).
   31get_arity(F // A,F,A2):- use_module(library(clpfd),['#='/2]),!, atom(F), clpfd:call(#=(A2 , A+2)), ensure_arity(F,A2),!,(A2>0).
   32get_arity(M:FA,F,A):-atom(M),!,get_arity(FA,F,A).
   33get_arity(FA,F,A):- get_functor(FA,F,A),must(A>0).
   34
   35% arity_no_bc(F,A):- call_u(arity(F,A)).
   36arity_no_bc(F,A):- clause_b(arity(F,A)).
   37arity_no_bc(F,A):- clause_b(support_hilog(F,A)).
   38arity_no_bc(F,A):- clause_b(functorDeclares(F)),!,A=1.
   39arity_no_bc(completeExtentAsserted,1).
   40arity_no_bc(home,2).
   41arity_no_bc(record,2).
   42arity_no_bc(F,A):- suggest_m(M),clause_b(mpred_prop(M,F,AA,_)),nonvar(AA),A=AA.
   43%arity_no_bc(F,A):- current_predicate(F/A)
   44% arity_no_bc(F,A):- current_predicate(_:F/A),\+(current_predicate(_:F/AA),AA\=A). =
 ensure_arity(?VALUE1, ?VALUE2) is semidet
Ensure Arity.
   50ensure_arity(F,A):- 
   51 one_must(
   52   arity_no_bc(F,A),
   53   one_must(
   54    (current_predicate(F/A),(A>0),assert_arity(F,A)),
   55    (ground(F:A),(A>0),assert_arity(F,A)))),
   56  !.
   57
   58
   59%=
 assert_arity(?F, :PRED2A) is semidet
Assert Arity.
   66assert_arity(F,A):- sanity(\+ ((bad_arity(F,A), trace_or_throw_ex(assert_arity(F,A))))), arity_no_bc(F,A),!.
   67assert_arity(F,A):- arity_no_bc(F,AA), A\=AA,dmsg_pretty(assert_additional_arity(F,AA->A)),!,ain_fast(arity(F,A)).
   68assert_arity(F,A):- ain_fast(arity(F,A)),!.
   69
   70bad_arity(F,_):- \+ atom(F).
   71bad_arity(_,A):- \+ integer(A).
   72bad_arity('[|]',_).
   73bad_arity(typeProps,0).
   74bad_arity(argIsa,2).
   75bad_arity(isEach,_).
   76bad_arity(_,0).
   77bad_arity(prologDynamic,2).
   78bad_arity(F,A):- \+ good_pred_relation_name(F,A).
   79
   80
   81%=
 good_pred_relation_name(?F, ?A) is semidet
Good Predicate Relation Name.
   87good_pred_relation_name(F,A):- \+ bad_pred_relation_name0(F,A).
   88
   89
   90%=
 bad_pred_relation_name0(?V, ?VALUE2) is semidet
Bad Predicate Relation Name Primary Helper.
   96bad_pred_relation_name0(V,_):- \+ atom(V),!.
   97bad_pred_relation_name0('[]',_).
   98bad_pred_relation_name0('',_).
   99bad_pred_relation_name0('!',_).
  100bad_pred_relation_name0('{}',_).
  101bad_pred_relation_name0(',',_).
  102bad_pred_relation_name0('[|]',_).
  103
  104%=
 bad_pred_relation_name1(?X, ?Y) is semidet
Bad Predicate Relation Name Secondary Helper.
  110bad_pred_relation_name1(X,Y):-bad_pred_relation_name0(X,Y).
  111bad_pred_relation_name1(F,A):-must_det((atom_codes(F,[C|_]),to_upper(C,U))),!, U == C, A>1.
  112bad_pred_relation_name1(F,A):-arity_no_bc(F,AO), A \= AO.
  113
  114% :-after_boot(writeq("Seen Mpred_props at start!\n")),!.
  115
  116%=
 functor_check_univ(?G1, ?F, ?List) is semidet
Functor Check Univ.
  122functor_check_univ(M:G1,F,List):-atom(M),member(M,[dbase,user]),!,functor_check_univ(G1,F,List),!.
  123functor_check_univ(G1,F,List):-must_det(compound(G1)),must_det(G1 \= _:_),must_det(G1 \= _/_),G1=..[F|List],!.
  124
  125
  126%:- endif.
  127% :- ensure_loaded(library('logicmoo/util/logicmoo_util_bugger.pl')).
  128%:- ensure_loaded(pfc_lib).
  129%:- use_module(mpred_type_isa).
  130%:- use_module(library(util_varnames)).
  131
  132/*
  133:- module_transparent retract_mu/1,
  134         assert_mu/4,
  135         asserta_mu/2,
  136         assertz_mu/2,
  137         assert_u/1,
  138         asserta_u/1,
  139         assertz_u/1,
  140         attempt_side_effect/1.
  141*/
  142:- module_transparent(attvar_op/2).  143
  144
  145:- meta_predicate 
  146      pred_head(1,*),
  147      attempt_side_effect(+),
  148      call_s(*),
  149      oncely(*),
  150      naf(*),
  151      call_s2(*),
  152      mpred_update_literal(*,*,0,*),
  153      mpred_retry(*),
  154%      mpred_op(?, ?),
  155      mpred_facts_only(*),
  156      map_unless(1,:,*,*),      
  157      is_callable(*),     
  158%      deducedSimply(*),
  159      cnstrn0(:,+),
  160      cnstrn(*),
  161      cnstrn(+,:),
  162      attvar_op(*,*),
  163      % clause_u(+,+,-),
  164      % call_u(+),
  165      assertz_mu(+),      
  166      assertz_mu(+,+),
  167      if_missing1(*),
  168      assert_mu(+),
  169      assert_mu(+,+,+,+),
  170      ain_minfo_2(1,*),
  171      ain_minfo(1,*),                                    
  172%      whenAnd(0,0),
  173      mpred_call_0(*),
  174      mpred_bc_only(*),
  175      mpred_bc_only0(*),
  176      mpred_prove_neg(*),
  177      call_u_req(*),
  178      pfcBC_NoFacts(*).  179
  180 :- meta_predicate mpred_get_support_one(0,*).  181 :- meta_predicate mpred_get_support_precanonical_plus_more(0,*).  182 % :- meta_predicate '__aux_maplist/2_cnstrn0+1'(*,0).
  183 :- meta_predicate repropagate_1(*).  184 :- meta_predicate trigger_supporters_list(0,*).  185 :- meta_predicate repropagate_meta_wrapper_rule(*).  186 :- meta_predicate repropagate_0(*).  187
  188
  189% oncely later will throw an error if there where choice points left over by call
  190:- meta_predicate(oncely(*)).  191:- was_export(oncely/1).
 oncely(:Goal) is semidet
Oncely.
  198oncely(:-(Call)):-!,Call,!.
  199oncely(:-(Call)):-!,call_u(Call).
  200oncely(Call):-once(Call).
  201% ================================================
  202% mpred_op/2
  203% ================================================
  204
  205/*
  206query(t, call_u, G):- call_u(G).
  207query(_, _, Op, G):- dtrace(call_u(call(Op,G))).
  208once(A,B,C,D):-trace_or_throw_ex(once(A,B,C,D)).
  209*/
  210
  211
  212
  213
  214% ================================================
  215% is_callable/call_u/naf
  216% ================================================
  217
  218%:- was_dynamic(naf/1).
  219:- meta_predicate(naf(*)).  220:- was_export(naf/1).
 naf(:Goal) is semidet
Negation-By-Faliure.
  228naf(Goal):- (\+ call_u(Goal)).
  229
  230:- meta_predicate(is_callable(*)).  231:- was_export(is_callable/1).
 is_callable(:GoalC) is semidet
If Is A Callable.
  239is_callable(C):-current_predicate(_,C),!.
  240
  241
  242:- style_check(+singleton).  243
  244% TODO READD
  245%:- foreach(arg(_,isEach(prologMultiValued,prologOrdered,prologNegByFailure,prologPTTP,prologKIF,pfcControlled,ttRelationType,
  246%     prologHybrid,predCanHaveSingletons,prologDynamic,prologBuiltin,functorIsMacro,prologListValued,prologSingleValued),P),.. )
  247
  248
  249% TODO ISSUE https://github.com/TeamSPoon/PrologMUD/issues/7
 check_context_module is semidet
Check Context Module. (throws if it turns out wrong)
  256check_context_module:- !.
  257% check_context_module:- is_release,!.
  258check_context_module:- 
  259  sanity((source_context_module(M1),clause_b(mtHybrid(M1)))),
  260  sanity((defaultAssertMt(M2),clause_b(mtHybrid(M2)))).
 check_real_context_module is semidet
Check Real Context Module (throws if it turns out wrong)
  266check_real_context_module:- is_release,!.
  267check_real_context_module:-!.
  268check_real_context_module:- sanity((context_module(M1),defaultAssertMt(M2),must(M1==M2))).
  269
  270
  271% ======================= mpred_file('pfcsyntax').	% operator declarations.
  272
  273:-
  274 op(1199,fx,('==>')), 
  275 op(1190,xfx,('::::')),
  276 op(1180,xfx,('==>')),
  277 op(1170,xfx,'<==>'),  
  278 op(1160,xfx,('<-')),
  279 op(1150,xfx,'=>'),
  280 op(1140,xfx,'<='),
  281 op(1130,xfx,'<=>'), 
  282 op(600,yfx,'&'), 
  283 op(600,yfx,'v'),
  284 op(350,xfx,'xor'),
  285 op(300,fx,'~'),
  286 op(300,fx,'-').
 mreq(+G) is semidet
Mreq.
  295mreq(G):- if_defined(call_u(G),fail).
  296
  297% ======================= mpred_file('pfccore').	% core of Pfc.
  298
  299%   File   : pfccore.pl
  300%   Author : Tim Finin, finin@prc.unisys.com
  301%   Updated: 10/11/87, ...
  302%            4/2/91 by R. McEntire: added calls to valid_dbref as a
  303%                                   workaround for the Quintus 3.1
  304%                            bug in the recorded database.
  305%   Purpose: core Pfc predicates.
  306
  307/*
  308
  309LogicMOO is mixing Mark Stickel's PTTP (prolog techn theorem prover) to create horn clauses that 
  310 PFC forwards and helps maintain in visible states )  in prolog knowledge baseable.. We use spft/3 to track deductions
  311Research~wise LogicMOO has a main purpose is to prove that grounded negations (of contrapostives) are of first class in importance in helping
  312with Wff checking/TMS 
  313Also alows an inference engine constrain search.. PFC became important since it helps memoize and close off (terminate) transitive closures
  314
  315*/
 is_side_effect_disabled is semidet
If Is A Side Effect Disabled.
  322is_side_effect_disabled:- t_l:no_attempt_side_effects,!.
  323is_side_effect_disabled:- t_l:side_effect_ok,!,fail.
  324is_side_effect_disabled:- t_l:noDBaseMODs(_),!.
 f_to_mfa(+EF, ?R, ?F, ?A) is semidet
Functor Converted To Module-functor-arity.
  332f_to_mfa(EF,R,F,A):-w_get_fa(EF,F,A),
  333              (((current_predicate(F/A),functor(P,F,A),predicate_property(_M:P,imported_from(R)))*->true;
  334              current_predicate(F/A),functor(P,F,A),source_file(R:P,_SF))),
  335              current_predicate(R:F/A).
 w_get_fa(+PI, ?F, ?A) is semidet
W Get Functor-arity.
  342w_get_fa(PI,_F,_A):-is_ftVar(PI),!.
  343w_get_fa(F/A,F,A):- !.
  344w_get_fa(PI,PI,_A):- atomic(PI),!.
  345w_get_fa(PI,F,A):- is_ftCompound(PI),!,functor(PI,F,A).
  346w_get_fa(Mask,F,A):-get_functor(Mask,F,A).
  347
  348
  349
  350:- multifile(baseKB:mpred_hook_rescan_files/0).  351:- dynamic(baseKB:mpred_hook_rescan_files/0).  352:- use_module(library(logicmoo_common)).  353%:- was_dynamic(use_presently/0).
  354% used to annotate a predciate to indicate PFC support
 is_mpred_action(:TermP) is semidet
If Is A Managed Predicate Action.
  361is_mpred_action('$VAR'(_)):-!,fail.
  362is_mpred_action(remove_if_unsupported(_,_)).
  363is_mpred_action(P):-is_static_predicate(P).
 mpred_is_builtin(+P) is semidet
PFC If Is A Builtin.
  369mpred_is_builtin(P):- predicate_property(P,built_in), \+ predicate_property(P,dynamic).
  370mpred_is_builtin(P):- callable(P),functor(P,F,_),clause_b(prologBuiltin(F)).
  371mpred_is_builtin(F):- current_predicate(F/A),A>0,functor(P,F,A),mpred_is_builtin(P).
  372
  373/* UNUSED TODAY
  374
  375:- use_module(library(mavis)).
  376:- use_module(library(type_check)).
  377:- use_module(library(typedef)).
  378*/
  379
  380
  381
  382:- thread_local((t_l:use_side_effect_buffer , t_l:verify_side_effect_buffer)).
 record_se is semidet
Record Se.
  388record_se:- (t_l:use_side_effect_buffer ; t_l:verify_side_effect_buffer).
 add_side_effect(+Op, ?Data) is semidet
Add Side Effect.
  396add_side_effect(_,_):- ( \+  record_se ),!.
  397add_side_effect(Op,Data0):- current_why(Why),serialize_attvars(Data0,Data),assert(t_l:side_effect_buffer(Op,Data,Why)).
Attribute Variable Oper..
  406listing_s(P):-call_s(xlisting(P)).
  407
  408assert_s(H):- assertz_s(H).
  409retractall_s(H):- forall(clause_s(H,_,R),erase(R)).
  410clause_s(H,B):- clause_s(H,B,_).
  411
  412retract_s(H):- lookup_s(H,R),erase(R).
  413
  414lookup_s(H):- lookup_s(H,_). 
  415
  416lookup_s(M:(H:-B),R):- !,clause_s(M:H,B,R).
  417lookup_s((H:-B),R):-  !,clause_s(H,B,R).
  418lookup_s(H,R):- clause_s(H,true,R).
  419
  420lookq_s(X):-lookq_s(X,_Ref).
  421
  422lookq_s(M:(H:-B),R):- !,clauseq_s(M:H,B,R).
  423lookq_s((H:-B),R):- !, clauseq_s(H,B,R).
  424lookq_s(H,R):- clauseq_s(H,true,R).
  425
  426asserta_s(H):- fix_mp(clause(assert,asserta_s),H,M,H0),asserta_i(M:H0).
  427assertz_s(H):- fix_mp(clause(assert,assertz_s),H,M,H0),assertz_i(M:H0).
  428clause_s(H,B,R):- fix_mp(clause(clause,clause_s),H,M,H0),clause_u(M:H0,B,R).
  429clauseq_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.
  430
  431call_s(G0):-
  432  strip_module(G0,_,G),functor(G,F,A),
  433  (memberchk(F/A,[(',')/2])->
  434  mpred_METACALL(call_s,G);
  435  call_s2(G0)).
  436
  437call_s2(G0):-
  438  strip_module(G0,WM,G),
  439  defaultAssertMt(U),  
  440  must(current_predicate(_,U:G)->(CALL=U:G);(current_predicate(_,WM:G0)->CALL=WM:G0; fail)),
  441  call(call,(
  442 '$set_source_module'(S,U),'$module'(M,U),
  443  setup_call_cleanup( % _each
  444    ('$set_source_module'(U),'$set_typein_module'(U)),
  445       call(CALL),
  446     ('$set_source_module'(S),'$set_typein_module'(M))))).
  447
  448
  449:- module_transparent(attvar_op/2).  450
  451% % attvar_op(Op,Data):- deserialize_attvars(Data,Data0), attvar_op(Op,Data0).
  452attvar_op(Op,MData):-
  453 must_det_l((
  454   strip_module(Op,_,OpA), sanity( \+ atom(OpA)),
  455   fix_mp(clause(assert,OpA),MData,M,Data),
  456   add_side_effect(OpA,M:Data),
  457   quietly(current_prolog_flag(assert_attvars,true)->deserialize_attvars(Data,Data0);Data=Data0))),!,
  458   attempt_side_effect_mpa(M,OpA,Data0).
  459
  460
  461:- thread_local(t_l:no_attempt_side_effects/0).
 attempt_side_effect(+PSE) is semidet
Physical Side Effect.
  467attempt_side_effect(PSE):- to_physical_mpa(PSE,M,P,A),!,attempt_side_effect_mpa(M,P,A).
  468
  469to_physical_mpa(PSE,M,P,A):- strip_module(PSE,M,PA),to_physical_pa(PA,P,A).
  470to_physical_pa(PA,P,A):-PA=..[P,A],!. to_physical_pa(PA,call,PA).
  471
  472
  473:- meta_predicate(db_op_call(*,1,?)).  474db_op_call(_What,How,Data):- call(How,Data).
  475
  476% attempt_side_effect_mpa(M,OpA,Data):- record_se,!,add_side_effect(OpA,M:Data).
  477attempt_side_effect_mpa(M,db_op_call(_,retract_u0),Data0):- \+ lookup_u(M:Data0),!,fail.
  478attempt_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)).
  479% @TODO BROKEN phys ical_side_effect_call(M,assertz_i,Data0):- must((compile_aux_clauses(M:Data0))),!.
  480attempt_side_effect_mpa(M,OpA,Data0):- show_failure(M:call(M:OpA,M:Data0)).
  481
  482
  483/*
  484
  485  b_setval(th_asserts,[]),
  486  call_u(G),
  487  b_getval(th_asserts,List).
  488
  489attempt_side_effect_mpa(C) :- 
  490   b_getval(th_asserts,List),
  491   b_setval(th_asserts,[C|List]),!.
  492
  493
  494
  495*/
 erase_w_attvars(+Data0, ?Ref) is semidet
Erase W Attribute Variables.
  500erase_w_attvars(Data0,Ref):- attempt_side_effect(erase(Ref)),add_side_effect(erase,Data0).
 mpred_nochaining(+Goal) is semidet
PFC No Chaining.
  507mpred_nochaining(Goal):- locally_tl(no_attempt_side_effects,call(Goal)).
 with_chaining(+Goal) is semidet
PFC No Chaining.
  514with_chaining(Goal):- locally(- t_l:no_attempt_side_effects,call(Goal)).
  515
  516% TODO ISSUE https://github.com/TeamSPoon/PrologMUD/issues/7
 match_source_ref1(:TermARG1) is semidet
Match Source Ref Secondary Helper.
  523match_source_ref1(ax):-!.
  524match_source_ref1(mfl4(_VarNameZ,_,_,_)).
 make_uu_remove(:TermU) is semidet
Make Uu Remove.
  530make_uu_remove((_,ax)).
 has_functor(:TermC) is semidet
Has Functor.
  538% -- % has_functor(_):-!,fail.
  539has_functor(F/A):-!,is_ftNameArity(F,A),!.
  540has_functor(C):- (\+ is_ftCompound(C)),!,fail.
  541has_functor(C):- is_ftCompound(C),\+is_list(C).
 mpred_each_literal(+P, ?E) is semidet
PFC Each Literal.
  548mpred_each_literal(P,E):-is_ftNonvar(P),P=(P1,P2),!,(mpred_each_literal(P1,E);mpred_each_literal(P2,E)).
  549mpred_each_literal(P,P). %:-conjuncts_to_list(P,List),member(E,List).
 retract_eq_quitely(+H) is semidet
Retract Using (==/2) (or =@=/2) ) Quitely.
  557retract_eq_quitely(H):- call_u(retract_eq_quitely_f(H)).
 retract_eq_quitely_f(+H) is semidet
Retract Using (==/2) (or =@=/2) ) Quitely False.
  563retract_eq_quitely_f((H:-B)):- !,clause_asserted_i(H,B,Ref),erase(Ref).
  564retract_eq_quitely_f(pfclog(H)):- retract_eq_quitely_f(H),fail.
  565retract_eq_quitely_f((H)):- clause_asserted_i(H,true,Ref),erase(Ref).
 assert_eq_quitely(+H) is semidet
Assert Using (==/2) (or =@=/2) ) Quitely.
  572assert_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).

  580mpred_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.
  588mpred_is_taut(A):-var(A),!.
  589mpred_is_taut(A:-B):-!,mpred_is_taut(B==>A).
  590mpred_is_taut(A<-B):-!,mpred_is_taut(B==>A).
  591mpred_is_taut(A<==>B):-!,(mpred_is_taut(A==>B);mpred_is_taut(B==>A)).
  592mpred_is_taut(A==>B):- A==B,!.
  593mpred_is_taut((B,_)==>A):- mpred_is_assertable(B),mpred_is_taut(A==>B),!.
  594mpred_is_taut((_,B)==>A):- mpred_is_assertable(B),mpred_is_taut(A==>B),!.
  595mpred_is_taut(B==>(A,_)):- mpred_is_assertable(A),mpred_is_taut(A==>B),!.
  596mpred_is_taut(B==>(_,A)):- mpred_is_assertable(A),mpred_is_taut(A==>B),!.
  597
  598
  599% 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.
  606is_retract_first(one).
  607is_retract_first(a).
 pfc_provide_storage_op(+Op, ?I1) is semidet
Prolog Forward Chaining Provide Storage Oper..
  614pfc_provide_storage_op(Op,(I1,I2)):-!,pfc_provide_storage_op(Op,I1),pfc_provide_storage_op(Op,I2).
  615pfc_provide_storage_op(Op,(nesc(P))):-!,pfc_provide_storage_op(Op,P).
  616%pfc_provide_storage_op(change(assert,_AorZ),Fact):- loop_check_nr(ainPreTermExpansion(Fact)).
  617% pfcRem1 to just get the first
  618pfc_provide_storage_op(change(retract,OneOrA),FactOrRule):- is_retract_first(OneOrA),!,
  619            loop_check_nr(mpred_withdraw(FactOrRule)),
  620  ignore((ground(FactOrRule),mpred_remove(FactOrRule))).
  621% mpred_remove should be forcefull enough
  622pfc_provide_storage_op(change(retract,all),FactOrRule):- loop_check_nr(mpred_remove(FactOrRule)),!.
  623% pfc_provide_storage_op(clause_u,FactOrRule):- is_ftNonvar(FactOrRule),!,loop_check_nr(clause_u(FactOrRule)).
  624
  625
  626% 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.
  645get_why(_,CL,R,asserted(R,CL:-U)):- clause_u(spft(CL, U, ax),true),!.
  646get_why(H,CL,R,deduced(R,WHY)):- (mpred_get_support(H,WH)*->WHY=(H=WH);(mpred_get_support(CL,WH),WHY=(CL=WH))).
  647
  648
  649
  650%% mpred_pbody_f( +H, ?CL, ?R, ?B, ?WHY) is semidet.
  651%
  652% PFC Pbody False.
  653%
  654mpred_pbody_f(H,CL,R,B,WHY):- CL=(B==>HH),sub_term_eq(H,HH),!,get_why(H,CL,R,WHY).
  655mpred_pbody_f(H,CL,R,B,WHY):- CL=(HH<-B),sub_term_eq(H,HH),!,get_why(H,CL,R,WHY).
  656mpred_pbody_f(H,CL,R,B,WHY):- CL=(HH<==>B),sub_term_eq(H,HH),get_why(H,CL,R,WHY).
  657mpred_pbody_f(H,CL,R,B,WHY):- CL=(B<==>HH),sub_term_eq(H,HH),!,get_why(H,CL,R,WHY).
  658mpred_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) ).
  665sub_term_eq(H,HH):-H==HH,!.
  666sub_term_eq(H,HH):-each_subterm(HH,ST),ST==H,!.
 sub_term_v(+H, ?HH) is semidet
Sub Term V.
  673sub_term_v(H,HH):-H=@=HH,!.
  674sub_term_v(H,HH):-each_subterm(HH,ST),ST=@=H,!.
 all_different_head_vals(+Clause) is det
Enforces All Different Head Vals.
  680all_different_head_vals(HB):- (\+ compound(HB) ; ground(HB)),!.
  681all_different_head_vals(HB):- 
  682  mpred_rule_hb(HB,H,B),
  683  term_slots(H,Slots),  
  684  (Slots==[]->
  685     all_different_head_vals(B);
  686    (lock_vars(Slots),all_different_head_vals_2(H,Slots),unlock_vars(Slots))),!.
  687  
  688
  689all_different_head_vals_2(_H,[]):-!.
  690all_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]),!.
  691all_different_head_vals_2(_H,[A,B|C]):-all_different_vals(dif_matrix,[A,B|C]),!.
  692all_different_head_vals_2(HB,_):- \+ compound(HB),!.
  693all_different_head_vals_2(H,[A]):-get_assertion_head_arg(_,H,E1),E1 ==A, H=..[_|ARGS], all_different_vals(dif_matrix,ARGS),!.
  694all_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),!.
  695all_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]),!.
  696all_different_head_vals_2(_,_).
 mpred_rule_hb(+Outcome, ?OutcomeO, ?AnteO) is semidet
Calculate PFC Rule Head+body.
  703mpred_rule_hb(Outcome,OutcomeO,Body):- nonvar(OutcomeO),!,mpred_rule_hb(Outcome,OutcomeN,Body),must(OutcomeO=OutcomeN).
  704mpred_rule_hb(Outcome,OutcomeO,BodyO):- nonvar(BodyO),!,mpred_rule_hb(Outcome,OutcomeO,BodyN),must(BodyN=BodyO).
  705mpred_rule_hb(Outcome,OutcomeO,AnteO):- 
  706  quietly((mpred_rule_hb_0(Outcome,OutcomeO,Ante),
  707  mpred_rule_hb_0(Ante,AnteO,_))).
  708% :-mpred_trace_nochilds(mpred_rule_hb/3).
 mpred_rule_hb_0(+Rule, -Head, -Body) is nondet
Calculate PFC rule Head+Body Primary Helper.
  717mpred_rule_hb_0(Outcome,OutcomeO,true):-is_ftVar(Outcome),!,OutcomeO=Outcome.
  718mpred_rule_hb_0(Outcome,OutcomeO,true):- \+compound(Outcome),!,OutcomeO=Outcome.
  719mpred_rule_hb_0((Outcome1,Outcome2),OutcomeO,AnteO):- mpred_rule_hb(Outcome1,Outcome1O,Ante1),mpred_rule_hb(Outcome2,Outcome2O,Ante2),
  720                   conjoin(Outcome1O,Outcome2O,OutcomeO),
  721                   conjoin(Ante1,Ante2,AnteO).
  722mpred_rule_hb_0((Ante1==>Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  723mpred_rule_hb_0((Ante1=>Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  724mpred_rule_hb_0((Ante1->Outcome),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).
  726% mpred_rule_hb_0((Outcome/Ante1),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  727mpred_rule_hb_0(rhs([Outcome]),OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  728% mpred_rule_hb_0(rhs([OutcomeH|OutcomeT]),OutcomeO,Ante2):- !, mpred_rule_hb(Outcome,OutcomeO,Ante2).
  729mpred_rule_hb_0({Outcome},OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  730mpred_rule_hb_0((Outcome<-Ante1),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  731mpred_rule_hb_0((Ante1 & Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  732mpred_rule_hb_0((Ante1 , Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  733mpred_rule_hb_0((Outcome<==>Ante1),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  734mpred_rule_hb_0((Ante1<==>Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  735mpred_rule_hb_0(_::::Outcome,OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb_0(Outcome,OutcomeO,Ante2).
  736mpred_rule_hb_0(bt(Outcome,Ante1),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  737mpred_rule_hb_0(pt(Ante1,Outcome),OutcomeO,(Ante1,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  738mpred_rule_hb_0(pk(Ante1a,Ante1b,Outcome),OutcomeO,(Ante1a,Ante1b,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  739mpred_rule_hb_0(nt(Ante1a,Ante1b,Outcome),OutcomeO,(Ante1a,Ante1b,Ante2)):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  740mpred_rule_hb_0(spft(Outcome,Ante1a,Ante1b),OutcomeO,(Ante1a,Ante1b,Ante2)):- (nonvar(Outcome)-> ! ; true),mpred_rule_hb(Outcome,OutcomeO,Ante2).
  741mpred_rule_hb_0(que(Outcome,_),OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  742% mpred_rule_hb_0(pfc Default(Outcome),OutcomeO,Ante2):- (nonvar(Outcome)-> ! ; true), mpred_rule_hb(Outcome,OutcomeO,Ante2).
  743mpred_rule_hb_0((Outcome:-Ante),Outcome,Ante):-(nonvar(Outcome)-> ! ; true).
  744mpred_rule_hb_0(Outcome,Outcome,true).
 ain_minfo(+G) is semidet
Assert If New Metainformation.
  751:- module_transparent(ain_minfo/1).  752ain_minfo(G):-ain_minfo(assertz_if_new,G).
 ain_minfo(:PRED1How, ?H) is semidet
Assert If New Metainformation.
  758:- module_transparent(ain_minfo/2).  759ain_minfo(How,(H:-True)):-is_true(True),must(is_ftNonvar(H)),!,ain_minfo(How,H).
  760ain_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))).
  761ain_minfo(How,(B==>H)):- !,ain_minfo(How,(H:-infoF(B==>H))),!,ain_minfo_2(How,(B:-infoF(B==>H))).
  762ain_minfo(How,(B<==>H)):- !,ain_minfo(How,(H:-infoF(B<==>H))),!,ain_minfo(How,(B:-infoF(B<==>H))),!.
  763ain_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)),!.
  764ain_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)),!.
  765ain_minfo(How,(-(A):-infoF(C))):-is_ftNonvar(C),is_ftNonvar(A),!,ain_minfo(How,((A):-infoF((C)))). % attvar_op(How,(-(A):-infoF(C))).
  766ain_minfo(How,(~(A):-infoF(C))):-is_ftNonvar(C),is_ftNonvar(A),!,ain_minfo(How,((A):-infoF((C)))). % attvar_op(How,(-(A):-infoF(C))).
  767ain_minfo(How,(A:-INFOC)):- is_ftNonvar(INFOC), get_bc_clause(A,AA,INFOCC),A=AA,INFOC==INFOCC,!,attvar_op(How,(A:-INFOC)),!.
  768ain_minfo(How,bt(_ABOX,H,_)):-!,get_bc_clause(H,Post),attvar_op(How,Post).
  769ain_minfo(How,nt(H,Test,Body)):-!,attvar_op(How,(H:-fail,nt(H,Test,Body))).
  770ain_minfo(How,pt(H,Body)):-!,attvar_op(How,(H:-fail,pt(H,Body))).
  771ain_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)))),!.
  772%ain_minfo(How,G):-mpred_trace_msg(skipped_add_meta_facts(How,G)).
  773ain_minfo(_,_).
  774
  775:- was_export(ain_minfo_2/2).
 ain_minfo_2(:PRED1How, ?G) is semidet
Assert If New Metainformation Extended Helper.
  781:- module_transparent(ain_minfo_2/2).  782ain_minfo_2(How,G):-ain_minfo(How,G).
 mpred_is_info(:TermC) is semidet
PFC If Is A Info.
  789mpred_is_info((CWC,Info)):- (atom(CWC),is_a_info(CWC));mpred_is_info(Info).
  790mpred_is_info(mpred_bc_only(C)):-is_ftNonvar(C),!.
  791mpred_is_info(infoF(C)):-is_ftNonvar(C),!.
  792mpred_is_info(inherit_above(_,_)).
  793
  794
  795is_a_info(fail).
  796is_a_info(CWC):- is_pfc_chained(CWC).
  797
  798is_pfc_chained(cwc).
  799is_pfc_chained(awc).
  800is_pfc_chained(zwc).
  801is_pfc_chained(fwc).
  802is_pfc_chained(bwc).
  803is_pfc_chained(wac).
  804
  805
  806
  807:- module_transparent(is_ain_clause/2).  808is_ain_clause( _, Var):- var(Var),!, fail.
  809is_ain_clause( M,(:- Body)):- !, is_ain_body(M,Body),!.
  810is_ain_clause( M,(P:- Body)):- !,(is_ain_head(M,P);is_ain_body(M,Body)),!.
  811is_ain_clause( M,(P)):- !, is_ain_head(M, P).
  812
  813:- module_transparent(is_ain_head/2).  814is_ain_head(_, P):- var(P),!.
  815is_ain_head(_,(_,_)):- !.
  816is_ain_head(_,(_;_)):- !.
  817is_ain_head(_,not(_)):- !.
  818is_ain_head(_,\+(_)):- !.
  819is_ain_head(M, P):- is_ain_body(M, P),!.
  820is_ain_head(_,==>(_)):- !.
  821is_ain_head(_,==>(_,_)):- !.
  822is_ain_head(_,<==>(_,_)):- !.
  823is_ain_head(_,<==(_)):- !.
  824is_ain_head(_,<==(_,_)):- !.
  825is_ain_head(_,'::::'(_,_)):- !.
  826is_ain_head(baseKB,_).
  827is_ain_head(_,=>(_)):- !.
  828is_ain_head(_,=>(_,_)):- !.
  829is_ain_head(_,_):- get_how_virtualize_file(Lang),!,Lang=heads.
  830
  831:- module_transparent(is_ain_body/2).  832is_ain_body(_, P):- var(P),!,fail.
  833is_ain_body(M, (P,_)):- !, nonvar(P), is_ain_body(M, P).
  834is_ain_body(_, CWC):- atom(CWC),  is_pfc_chained(CWC).
  835is_ain_body(M, P):- functor(P,F,A), \+ \+ mpred_prop(M,F,A,_), !,
  836  \+ (mpred_prop(M,F,A,Prop), is_pfc_prolog_only_prop(Prop)).
  837is_ain_body(M, MP):- strip_module(MP,M2,P), M2\==M, !,is_ain_body(M2,P).
  838
  839is_pfc_prolog_only_prop(prologOnly).
  840is_pfc_prolog_only_prop(prologBuiltin).
  841
  842
  843%cwc(Call):- callable(Call),Call.
  844
  845%:- was_dynamic(not_not/1).
 mpred_rewrap_h(+A, ?A) is semidet
PFC Rewrap Head.
  851mpred_rewrap_h(A,A):- is_ftNonvar(A),\+ is_static_predicate(A).
  852mpred_rewrap_h(A,F):- functor(A,F,_),\+ is_static_predicate(F),!.
  853%mpred_rewrap_h(A,not_not(A)):-!.
 cwc is det
Cwc.
  860cwc:-true.
 fwc is det
Fwc.
  866fwc:-true.
 bwc is semidet
Bwc.
  872bwc:-true.
 wac is semidet
Wac.
  878wac:-true.
  879
  880awc:-true.
  881zwc:-true.
 is_fc_body(+P) is semidet
If Is A Forward Chaining Body.
  888is_fc_body(P):- has_body_atom(fwc,P).
 is_bc_body(+P) is semidet
If Is A Backchaining Body.
  894is_bc_body(P):- has_body_atom(bwc,P).
 is_action_body(+P) is semidet
If Is A Action Body.
  900is_action_body(P):- has_body_atom(wac,P).
 has_body_atom(+WAC, ?P) is semidet
Has Body Atom.
  908has_body_atom(WAC,P):- call(
  909   WAC==P -> true ; (is_ftCompound(P),get_assertion_head_arg(1,P,E),has_body_atom(WAC,E))),!.
  910
  911/*
  912has_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))).
  913is_atom_body_pfa(WAC,P,F,2,Rest):-get_assertion_head_arg(1,P,E),E==WAC,get_assertion_head_arg(2,P,Rest),!.
  914is_atom_body_pfa(WAC,P,F,2,Rest):-get_assertion_head_arg(2,P,E),E==WAC,get_assertion_head_arg(1,P,Rest),!.
  915*/
  916
  917:- module_transparent( (get_assertion_head_arg)/3).  918get_assertion_head_arg(N,P,E):-get_assertion_head_unnegated(P,PP),!,arg(N,PP,E).
  919
  920same_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.
  927mpred_update_literal(P,N,Q,R):-
  928    get_assertion_head_arg(N,P,UPDATE),call(replace_arg(P,N,Q_SLOT,Q)),
  929    must(call_u(Q)),update_value(Q_SLOT,UPDATE,NEW), 
  930    replace_arg(Q,N,NEW,R).
  931
  932
  933% spft(5,5,5).
 update_single_valued_arg(+Module, +P, ?N) is semidet
Update Single Valued Argument.
  939:- module_transparent( (update_single_valued_arg)/3).  940
  941update_single_valued_arg(M,M:Pred,N):-!,update_single_valued_arg(M,Pred,N).
  942update_single_valued_arg(_,M:Pred,N):-!,update_single_valued_arg(M,Pred,N).
  943
  944update_single_valued_arg(world,P,N):- !, update_single_valued_arg(baseKB,P,N).
  945update_single_valued_arg(M,P,N):- break, \+ clause_b(mtHybrid(M)), trace, clause_b(mtHybrid(M2)),!,
  946   update_single_valued_arg(M2,P,N).
  947
  948update_single_valued_arg(M,P,N):- 
  949  get_assertion_head_arg(N,P,UPDATE),
  950  is_relative(UPDATE),!,
  951  dtrace,
  952  break,
  953  replace_arg(P,N,OLD,Q),
  954  must_det_l((clause_u(Q),update_value(OLD,UPDATE,NEW),\+ is_relative(NEW), replace_arg(Q,N,NEW,R))),!,
  955  update_single_valued_arg(M,R,N).
  956
  957
  958update_single_valued_arg(M,P,N):- 
  959 call_u((must_det_l((
  960
  961  call_u(mtHybrid(M)),
  962  mpred_type_args \= M,
  963  mpred_kb_ops \= M,
  964  get_assertion_head_arg(N,P,UPDATE),
  965  replace_arg(P,N,Q_SLOT,Q),
  966  var(Q_SLOT),
  967  same_functors(P,Q),
  968  % current_why(U),
  969  must_det_l((
  970     % rtrace(attvar_op(assert_if_new,M:spft(P,U,ax))),
  971     % (call_u(P)->true;(assertz_mu(P))),
  972     assertz_mu(M,P),
  973     doall((
  974          lookup_u(M:Q,E),
  975          UPDATE \== Q_SLOT,
  976          erase(E),
  977          mpred_unfwc1(M:Q))))))))).
  978
  979% ======================= 
  980% utils
  981% ======================= 
 map_literals(+P, ?G) is semidet
Map Literals.
  987map_literals(P,G):-map_literals(P,G,[]).
 map_literals(+VALUE1, :TermH, ?VALUE3) is semidet
Map Literals.
  994map_literals(_,H,_):-is_ftVar(H),!. % skip over it
  995map_literals(_,[],_) :- !.
  996map_literals(Pred,(H,T),S):-!, apply(Pred,[H|S]), map_literals(Pred,T,S).
  997map_literals(Pred,[H|T],S):-!, apply(Pred,[H|S]), map_literals(Pred,T,S).
  998map_literals(Pred,H,S):- mpred_literal(H),must(apply(Pred,[H|S])),!.
  999map_literals(_Pred,H,_S):- \+ is_ftCompound(H),!. % skip over it
 1000map_literals(Pred,H,S):-H=..List,!,map_literals(Pred,List,S),!.
 map_unless(:PRED1Test, ?Pred, ?H, ?S) is semidet
Map Unless.
 1008map_unless(Test,Pred,H,S):- call(Test,H),ignore(apply(Pred,[H|S])),!.
 1009map_unless(_Test,_,[],_) :- !.
 1010map_unless(_Test,_Pred,H,_S):- \+ is_ftCompound(H),!. % skip over it
 1011map_unless(Test,Pred,(H,T),S):-!, apply(Pred,[H|S]), map_unless(Test,Pred,T,S).
 1012map_unless(Test,Pred,[H|T],S):-!, apply(Pred,[H|S]), map_unless(Test,Pred,T,S).
 1013map_unless(Test,Pred,H,S):-H=..List,!,map_unless(Test,Pred,List,S),!.
 1014
 1015
 1016:- meta_predicate(map_first_arg(:,+)).
 map_first_arg(+Pred, ?List) is semidet
PFC Maptree.
 1021map_first_arg(CM:Pred,List):-map_first_arg(CM,Pred,List,[]).
 1022
 1023:- meta_predicate(map_first_arg(+,*,+,+)).
 map_first_arg(+Pred, :TermH, ?S) is semidet
PFC Maptree.
 1028map_first_arg(CM,Pred,H,S):-is_ftVar(H),!,CM:apply(Pred,[H|S]).
 1029map_first_arg(_,_,[],_) :- !.
 1030map_first_arg(CM,Pred,(H,T),S):-!, map_first_arg(CM,Pred,H,S), map_first_arg(CM,Pred,T,S).
 1031map_first_arg(CM,Pred,(H;T),S):-!, map_first_arg(CM,Pred,H,S) ; map_first_arg(CM,Pred,T,S).
 1032map_first_arg(CM,Pred,[H|T],S):-!, CM:apply(Pred,[H|S]), map_first_arg(CM,Pred,T,S).
 1033map_first_arg(CM,Pred,H,S):- CM:apply(Pred,[H|S]). 
 1034
 1035:- fixup_exports. 1036
 1037% % :- ensure_loaded(logicmoo(util/rec_lambda)).
 1038
 1039%example pfcVerifyMissing(mpred_isa(I,D), mpred_isa(I,C), ((mpred_isa(I,C), {D==C});-mpred_isa(I,C))). 
 1040%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.
 1047pfcVerifyMissing(GC, GO, ((GO, {D==C});\+ GO) ):-  GC=..[F,A|Args],append(Left,[D],Args),append(Left,[C],NewArgs),GO=..[F,A|NewArgs],!.
 1048
 1049%example mpred_freeLastArg(mpred_isa(I,C),~(mpred_isa(I,C))):-is_ftNonvar(C),!.
 1050%example mpred_freeLastArg(mpred_isa(I,C),(mpred_isa(I,F),C\=F)):-!.
 mpred_freeLastArg(+G, ?GG) is semidet
PFC Free Last Argument.
 1056mpred_freeLastArg(G,GG):- G=..[F,A|Args],append(Left,[_],Args),append(Left,[_],NewArgs),GG=..[F,A|NewArgs],!.
 1057mpred_freeLastArg(_G,false).
 mpred_current_op_support(+VALUE1) is semidet
PFC Current Oper. Support.
 1064mpred_current_op_support((p,p)):-!.
 pfcVersion(+VALUE1) is semidet
Prolog Forward Chaining Version.
 1071pfcVersion(6.6).
 1072
 1073
 1074% % :- '$set_source_module'(mpred_kb_ops).
 correctify_support(+S, ?S) is semidet
Correctify Support.
 1080correctify_support(U,(U,ax)):-var(U),!.
 1081correctify_support((U,U),(U,ax)):-!.
 1082correctify_support((S,T),(S,T)):-!.
 1083correctify_support((U,_UU),(U,ax)):-!.
 1084correctify_support([U],S):-correctify_support(U,S).
 1085correctify_support(U,(U,ax)).
 clause_asserted_local(:TermABOX) is semidet
Clause Asserted Local.
 1092clause_asserted_local(MCL):-
 1093  strip_module(MCL,_,CL),
 1094  must(CL=spft(P,Fact,Trigger )),!,
 1095  clause_u(spft(P,Fact,Trigger),true,Ref),
 1096  clause_u(spft(UP,UFact,UTrigger),true,Ref),
 1097  (((UP=@=P,UFact=@=Fact,UTrigger=@=Trigger))).
 is_already_supported(+P, ?S, ?UU) is semidet
If Is A Already Supported.
 1105is_already_supported(P,(S,T),(S,T)):- clause_asserted_local(spft(P,S,T)),!.
 1106is_already_supported(P,_S,UU):- clause_asserted_local(spft(P,US,UT)),must(get_source_uu(UU)),UU=(US,UT).
 1107
 1108% TOO UNSAFE 
 1109% is_already_supported(P,_S):- copy_term_and_varnames(P,PC),sp ftY(PC,_,_),P=@=PC,!.
 1110
 1111
 1112if_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.
 1120if_missing_mask(M:Q,M:R,M:Test):- nonvar(Q),!,if_missing_mask(Q,R,Test).
 1121if_missing_mask(Q,~Q,\+Q):- \+ is_ftCompound(Q),!.
 1122
 1123%if_missing_mask(ISA, ~ ISA, \+ ISA):- functor(ISA,F,1),(F==tSwim;call_u(functorDeclares(F))),!.
 1124if_missing_mask(HB,RO,TestO):- once(mpred_rule_hb(HB,H,B)),B\==true,HB\==H,!,
 1125     if_missing_mask(H,R,TestO),subst(HB,H,R,RO).
 1126
 1127if_missing_mask(ISA, ISA, \+ ISA):- functor(ISA, _F,1),!.% (F==tSwim;call_u(functorDeclares(F))),!.
 1128
 1129if_missing_mask(Q,R,Test):-
 1130   which_missing_argnum(Q,N),
 1131   if_missing_n_mask(Q,N,R,Test),!.
 1132
 1133if_missing_mask(ISA, ~ ISA, \+ ISA).
 if_missing_n_mask(+Q, ?N, ?R, ?Test) is semidet
If Missing Mask.
 1139if_missing_n_mask(Q,N,R,Test):-
 1140  get_assertion_head_arg(N,Q,Was),
 1141  (nonvar(R)-> (which_missing_argnum(R,RN),get_assertion_head_arg(RN,R,NEW));replace_arg(Q,N,NEW,R)),!,
 1142   Test=dif:dif(Was,NEW).
 1143
 1144/*
 1145Old version
 1146if_missing_mask(Q,N,R,dif:dif(Was,NEW)):- 
 1147 must((is_ftNonvar(Q),acyclic_term(Q),acyclic_term(R),functor(Q,F,A),functor(R,F,A))),
 1148  (singleValuedInArg(F,N) -> 
 1149    (get_assertion_head_arg(N,Q,Was),replace_arg(Q,N,NEW,R));
 1150    ((get_assertion_head_arg(N,Q,Was),is_ftNonvar(Was)) -> replace_arg(Q,N,NEW,R);
 1151        (N=A,get_assertion_head_arg(N,Q,Was),replace_arg(Q,N,NEW,R)))).
 1152*/
 which_missing_argnum(+VALUE1, ?VALUE2) is semidet
Which Missing Argnum.
 1159which_missing_argnum(Q,N):- compound(Q),\+ compound_name_arity(Q,_,0),
 1160 must((acyclic_term(Q),is_ftCompound(Q),get_functor(Q,F,A))),
 1161 F\=t,
 1162  (call_u(singleValuedInArg(F,N)) -> true; which_missing_argnum(Q,F,A,N)).
 1163
 1164which_missing_argnum(_,_,1,_):-!,fail.
 1165which_missing_argnum(Q,_F,A,N):- between(A,1,N),get_assertion_head_arg(N,Q,Was),is_ftNonvar(Was).
 1166
 1167mpred_run_pause:- asserta(t_l:mpred_run_paused).
 1168mpred_run_resume:- retractall(t_l:mpred_run_paused).
 1169
 1170without_running(G):- (t_l:mpred_run_paused->G;locally_tl(mpred_run_pause,G)).
 1171
 1172mpred_remove_file_support(_File):- !.
 1173mpred_remove_file_support(File):- 
 1174  forall((filematch(File,File0),freeze(Match,contains_var(File0,Match))),
 1175      forall(lookup_u(spft( W, Match, ax)),forall(retract_u(spft( W, Match, ax)),mpred_remove(W)))).
 1176
 1177/*
 1178
 1179%% remove_if_unsupported( +Why, ?P) is semidet.
 1180%
 1181% Remove If Unsupported.
 1182%
 1183remove_if_unsupported(Why,P) :- is_ftVar(P),!,trace_or_throw_ex(warn(var_remove_if_unsupported(Why,P))).
 1184remove_if_unsupported(Why,P) :- ((\+ ground(P), P \= (_:-_) , P \= ~(_) ) -> mpred_trace_msg(warn(nonground_remove_if_unsupported(Why,P))) ;true),
 1185   (((mpred_tms_supported(local,P,How),How\=unknown(_)) -> mpred_trace_msg(still_supported(How,Why,local,P)) ; (  mpred_undo(Why,P)))),!.
 1186   % mpred_run.
 1187
 1188*/
 1189
 1190%= mpred_tms_supported(+P,-How) succeeds if P is "supported". What "How" means
 1191%= depends on the TMS mode selected.
 mpred_tms_supported(+P, ?How) is semidet
PFC Truth Maintainence/wff Supported.
 1198mpred_tms_supported(P,How) :-
 1199  lookup_u(tms(Mode)),
 1200  mpred_tms_supported0(Mode,P,How).
 mpred_tms_supported(+Mode, ?P, ?How) is semidet
PFC Truth Maintainence/wff Supported.
 1208mpred_tms_supported(Mode,P,How) :- is_ftVar(Mode),get_tms_mode(P,tms(Mode)),!,mpred_tms_supported0(Mode,P,How).
 1209mpred_tms_supported(Mode,P,How) :- mpred_tms_supported0(Mode,P,How).
 1210mpred_tms_supported(How,_P,unknown(How)).
 1211
 1212:- module_transparent((mpred_wfflist)/2). 1213:- module_transparent((mpred_wff)/3).
 mpred_tms_supported0(+TmsMode, ?P, ?How) is semidet
PFC Truth Maintainence/wff Supported Primary Helper.
 1220mpred_tms_supported0(local,P,How) :-  mpred_get_support(P,How). % ,sanity(mpred_deep_support(How,S)).
 1221mpred_tms_supported0(cycles,P,How) :-  well_founded(P,How).
 1222mpred_tms_supported0(deep,P,How) :- mpred_deep_support(How,P).
 1223
 1224% 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.
 1231well_founded(Fact,How) :- mpred_wff(Fact,[],How).
 mpred_wff(?F, ?VALUE2, :TermHow) is semidet
PFC Well-formed Formula.
 1239mpred_wff(F,_,How) :-
 1240  % supported by user (mpred_axiom) or an "absent" fact (assumption).
 1241  ((mpred_axiom(F),How =mpred_axiom(F) ); (mpred_assumption(F),How=mpred_assumption(F))),
 1242  !.
 1243
 1244mpred_wff(F,Descendants,wff(Supporters)) :-
 1245  % first make sure we aren''t in a loop.
 1246  (\+ memberchk(F,Descendants)),
 1247  % find a justification.
 1248  supporters_list(F,Supporters),
 1249  % all of whose members are well founded.
 1250  mpred_wfflist(Supporters,[F|Descendants]),
 1251  !.
 mpred_wfflist(+L1, ?L2) is semidet
simply maps mpred_wff over the list.
 1259mpred_wfflist([],_).
 1260mpred_wfflist([X|Rest],L) :-
 1261  mpred_wff(X,L,_How),
 1262  mpred_wfflist(Rest,L).
 mpred_scan_tms(+P) is semidet
PFC Scan Truth Maintainence/wff.
 1269mpred_scan_tms(P):-mpred_get_support(P,(S,SS)),
 1270  (S==SS-> true;
 1271   once((mpred_deep_support(_How,P)->true;
 1272     (mpred_trace_msg(warn(now_maybe_unsupported(mpred_get_support(P,(S,SS)),fail))))))).
 user_atom(+U) is semidet
User Atom.
 1279user_atom(mfl4(_VarNameZ,_,_,_)):-!.
 1280user_atom(ax).
 1281user_atom(s(_)).
 mpred_deep_support(+How, ?M) is semidet
PFC Deep Support.
 1288mpred_deep_support(_How,unbound):-!,fail.
 1289mpred_deep_support(How,M):-loop_check(mpred_deep_support0(How,M),fail).
 mpred_deep_support0(+U, ?U) is semidet
PFC Deep Support Primary Helper.
 1296mpred_deep_support0(user_atom(U),(U,ax)):-user_atom(U),!.
 1297mpred_deep_support0(How,(A==>_)):-!,mpred_deep_support(How,A).
 1298mpred_deep_support0(pt(HowA,HowB),pt(A,B)):-!,mpred_deep_support(HowA,A),mpred_deep_support(HowB,B).
 1299mpred_deep_support0(HowA->HowB,(A->B)):-!,mpred_deep_support(HowA,A),mpred_deep_support(HowB,B).
 1300mpred_deep_support0(HowA/HowB,(A/B)):-!,mpred_deep_support(HowA,A),mpred_deep_support(HowB,B).
 1301mpred_deep_support0((HowA,HowB),(A,B)):-!,mpred_deep_support(HowA,A),mpred_deep_support(HowB,B).
 1302mpred_deep_support0(How,rhs(P)):-!,maplist(mpred_deep_support,How,P).
 1303mpred_deep_support0(mpred_call_only_facts(\+ P),\+ call_u(P)):-!,mpred_call_only_facts(\+ P).
 1304mpred_deep_support0(mpred_call_only_facts(P),call_u(P)):-!,mpred_call_only_facts(P).
 1305mpred_deep_support0(mpred_call_only_facts(P),{P}):-!,mpred_call_only_facts(P).
 1306mpred_deep_support0(S==>How,P):-mpred_get_support(P,S),mpred_deep_support(How,S),!.
 1307mpred_deep_support0(mpred_call_only_facts(\+(P)),\+(P)):-!, mpred_call_only_facts(\+(P)).
 1308mpred_deep_support0(user_atom(P),P):-user_atom(P),!.
 1309mpred_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.
 1316mpred_get_support_precanonical_plus_more(P,Sup):- 
 1317  mpred_get_support_one(P,Sup)*->true;
 1318  ((fully_expand(mpred_get_support_precanonical_plus_more,P,PE),!,
 1319    P\=@=PE,mpred_get_support_one(PE,Sup))).
 mpred_get_support_one(+P, ?Sup) is semidet
PFC Get Support One.
 1325mpred_get_support_one(P,Sup):- mpred_get_support(P,Sup)*->true;
 1326  (mpred_get_support_via_clause_db(P,Sup)*->true;
 1327     mpred_get_support_via_sentence(P,Sup)).
 mpred_get_support_via_sentence(+Var, ?VALUE2) is semidet
PFC Get Support Via Sentence.
 1334mpred_get_support_via_sentence(Var,_):-is_ftVar(Var),!,fail.
 1335mpred_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).
 1336mpred_get_support_via_sentence(true,g):-!.
 1337mpred_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.
 1345mpred_get_support_via_clause_db(\+ P,OUT):- mpred_get_support_via_clause_db(~(P),OUT).
 1346mpred_get_support_via_clause_db(\+ P,(naf(g),g)):- !, predicate_property(P,number_of_clauses(_)),\+ clause(P,_Body).
 1347mpred_get_support_via_clause_db(P,OUT):- predicate_property(P,number_of_clauses(N)),N>0,
 1348   clause_u(P,Body),(Body==true->Sup=(g);
 1349    (support_ok_via_clause_body(P),mpred_get_support_precanonical_plus_more(Body,Sup))),
 1350   OUT=(Sup,g).
 support_ok_via_clause_body(+H) is semidet
Support Ok Via Clause Body.
 1358support_ok_via_clause_body(_H):-!,fail.
 1359support_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.
 1366support_ok_via_clause_body(_,(\+),1):-!,fail.
 1367support_ok_via_clause_body(_,F,_):- lookup_u(rtArgsVerbatum(F)),!,fail.
 1368support_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.
 1377mpred_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.
 1384spft_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).
 1391trigger_supporters_list(U,[]) :- match_source_ref1(U),!.
 1392trigger_supporters_list(U,[]) :- atom(U),!.
 1393
 1394trigger_supporters_list(Trigger,[Fact|MoreFacts]) :-
 1395  mpred_get_support_precanonical_plus_more(Trigger,(Fact,AnotherTrigger)),
 1396  must(trigger_supporters_list(AnotherTrigger,MoreFacts)).
 1397
 1398mpred_retry(G):- fail; quietly(G).
 {?G} is semidet
an escape construct for bypassing the FOL''s salient body goals.
 1406:- meta_predicate('{}'(*)). 1407:- module_transparent( ({})/1). 1408'{}'(G):- call_u(G).
 1409:- sexport(({})/1).
 neg_in_code(+G) is semidet
Negated In Code.
 1415:- meta_predicate neg_in_code(*). 1416:- export(neg_in_code/1). 1417neg_in_code(G):- no_repeats(loop_check(neg_in_code0(G))).
 1418
 1419:- kb_shared(baseKB:proven_neg/1). 1420
 1421:- meta_predicate neg_in_code0(*). 1422:- export(neg_in_code0/1). 1423/*
 1424neg_in_code0(G):- cwc, loop_check(proven_neg(G)).
 1425neg_in_code0(G):- cwc, var(G),!,loop_check(lookup_u(~ G)).
 1426neg_in_code0(call_u(G)):- !,neg_in_code0(G).
 1427neg_in_code0(~(G)):- nonvar(G),!,  \+ loop_check(~G) ,!.
 1428neg_in_code0(G):-  is_ftNonvar(G), a(prologSingleValued,G),
 1429      must((if_missing_mask(G,R,Test),nonvar(R),nonvar(Test))),call_u(R),!,call_u(Test).
 1430neg_in_code0(G):- cwc, clause(~G,Call)*-> call_u(Call).
 1431*/
 1432neg_in_code0(G):- loop_check(neg_may_naf(G)), \+ loop_check(G),!.
 1433% neg_in_code0(_:G):-!,baseKB:neg_in_code0(G).
 1434
 1435
 1436:- meta_predicate neg_may_naf(*). 1437:- module_transparent(neg_may_naf/1). 1438:- export(neg_may_naf/1).
 neg_may_naf(:GoalP) is semidet
Negated May Negation-by-faliure.
 1444neg_may_naf(P):- mpred_non_neg_literal(P),get_functor(P,F),clause_u(prologNegByFailure(F),true),!.
 1445neg_may_naf(P):- is_ftCompound(P),is_never_pfc(P).
 call_u_req(+G) is semidet
Req.
 1452call_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)

 1465mpred_call_only_facts(_Why,Clause):- mpred_call_only_facts(Clause).
 1466mpred_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.
 1473mpred_call_0(Var):-is_ftVar(Var),!,mpred_call_with_no_triggers(Var).
 1474mpred_call_0(M):-fixed_negations(M,O),!,mpred_call_0(O).
 1475mpred_call_0(U:X):-U==user,!,mpred_call_0(X).
 1476mpred_call_0(t(A,B)):-(atom(A)->true;(no_repeats(arity_no_bc(A,1)),atom(A))),ABC=..[A,B],mpred_call_0(ABC).
 1477mpred_call_0(isa(B,A)):-(atom(A)->true;(call_u(tCol(A)),atom(A))),ABC=..[A,B],mpred_call_0(ABC).
 1478%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).
 1479mpred_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).
 1480mpred_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).
 1481mpred_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).
 1482mpred_call_0((C1,C2)):-!,mpred_call_0(C1),mpred_call_0(C2).
 1483mpred_call_0((C1;C2)):-!,(mpred_call_0(C1);mpred_call_0(C2)).
 1484mpred_call_0((C1->C2;C3)):-!,(mpred_call_0(C1)->mpred_call_0(C2);mpred_call_0(C3)).
 1485mpred_call_0((C1*->C2;C3)):-!,(mpred_call_0(C1)*->mpred_call_0(C2);mpred_call_0(C3)).
 1486mpred_call_0((C1->C2)):-!,(mpred_call_0(C1)->mpred_call_0(C2)).
 1487mpred_call_0((C1*->C2)):-!,(mpred_call_0(C1)*->mpred_call_0(C2)).
 1488mpred_call_0(call(X)):- !, mpred_call_0(X).
 1489mpred_call_0(call_u(X)):- !, mpred_call_0(X).
 1490mpred_call_0(\+(X)):- !, \+ mpred_call_0(X).
 1491mpred_call_0(call_u(X)):- !, mpred_call_0(X).
 1492mpred_call_0(clause(H,B,Ref)):-!,clause_u(H,B,Ref).
 1493mpred_call_0(clause(H,B)):-!,clause_u(H,B).
 1494mpred_call_0(clause(HB)):-expand_to_hb(HB,H,B),!,clause_u(H,B).
 1495mpred_call_0(asserta(X)):- !, mpred_aina(X).
 1496mpred_call_0(assertz(X)):- !, mpred_ainz(X).
 1497mpred_call_0(assert(X)):- !, mpred_ain(X).
 1498mpred_call_0(retract(X)):- !, mpred_prolog_retract(X).
 1499mpred_call_0(retractall(X)):- !, mpred_prolog_retractall(X).
 1500
 1501% TODO: test removal
 1502%mpred_call_0(prologHybrid(H)):-get_functor(H,F),!,isa_asserted(F,prologHybrid).
 1503
 1504mpred_call_0((H)):- !, call(H).
 1505
 1506mpred_call_0((H)):- is_static_predicate(H),!,call(H).
 1507mpred_call_0((H)):- is_static_predicate(H),!,show_pred_info(H),dtrace(mpred_call_0((H))).
 1508
 1509%mpred_call_0(HB):-quietly((full_transform_warn_if_changed(mpred_call_0,HB,HHBB))),!,mpred_call_0(HHBB).
 1510mpred_call_0(H):- !, locally_tl(infAssertedOnly(H),call_u(H)).
 1511%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.
 1512% TODO: test removal
 1513% mpred_call_0(isa(H,B)):-!,isa_asserted(H,B).
 1514
 1515
 1516
 1517mpred_call_0(M:P):-!,sanity(nonvar(P)),functor(P,F,_),mpred_call_1(M,P,F).
 1518mpred_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.
 1526mpred_call_1(_,G,_):- is_side_effect_disabled,!,mpred_call_with_no_triggers(G).
 1527
 1528mpred_call_1(M,G,F):- sanity(\+  is_side_effect_disabled),
 1529               (ground(G); \+ current_predicate(_,M:G) ; \+ (predicate_property(M:G,number_of_clauses(CC)),CC>1)), 
 1530    
 1531                ignore((loop_check(call_with_bc_triggers(M:G)),maybeSupport(G,(g,ax)),fail)),
 1532                 \+ current_predicate(F,M:G),\+ current_predicate(_,_:G),
 1533                 doall(show_call(predicate_property(_UM:G,_PP))),
 1534                 debug_logicmoo(logicmoo(_)),
 1535                 fail,
 1536                 %TODO remove this failure
 1537                 must(show_call(kb_shared(M:G))),
 1538                 kb_shared(M:G),!,fail.
 1539mpred_call_1(_,G,_):- mpred_call_with_no_triggers(G).
 1540
 1541
 1542:- thread_local t_l:infBackChainPrevented/1.
 call_with_bc_triggers(+MP) is semidet
Call Using Backchaining Triggers.
 1549call_with_bc_triggers(MP) :- strip_module(MP,_,P), functor(P,F,A), \+ t_l:infBackChainPrevented(F/A), 
 1550  lookup_u(bt(P,Trigger)),
 1551  no_repeats(mpred_get_support(bt(P,Trigger),S)),
 1552  once(no_side_effects(P)),
 1553  locally_tl(infBackChainPrevented(F/A),mpred_eval_lhs(Trigger,S)).
 mpred_call_with_no_triggers(+Clause) is semidet
PFC Call Using No Triggers.
 1560mpred_call_with_no_triggers(Clause) :-  strip_module(Clause,_,F),
 1561  %= this (is_ftVar(F)) is probably not advisable due to extreme inefficiency.
 1562  (is_ftVar(F)    ->  mpred_facts_and_universe(F) ;
 1563     mpred_call_with_no_triggers_bound(F)).
 mpred_call_with_no_triggers_bound(+F) is semidet
PFC Call Using No Triggers Bound.
 1570mpred_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.
 1576mpred_call_with_no_triggers_uncaugth(Clause) :-  strip_module(Clause,_,F),
 1577  show_failure(mpred_call_with_no_triggers_bound,no_side_effects(F)),
 1578  (\+ current_predicate(_,F) -> fail;call_u(F)).
 1579  %= we check for system predicates as well.
 1580  %has_cl(F) -> (clause_u(F,Condition),(Condition==true->true;call_u(Condition)));
 1581  %call_u(F).
 mpred_bc_only(+M) is semidet
PFC Backchaining Only.
 1589%mpred_bc_only(G):- !,defaultAssertMt(W), loop_check(mpred_BC_w_cache(W,G)).
 1590%mpred_bc_only(M:G):- !, loop_check(with_umt(M,mpred_bc_only0(G))).
 1591mpred_bc_only(G):- no_repeats(loop_check(mpred_bc_only0(G))).
 mpred_bc_only(+M) is semidet
PFC Backchaining + FACTS + Inheritance.
 1597mpred_bc_and_with_pfc(G):- no_repeats(loop_check(mpred_bc_and_with_pfc_0(G))).
 1598
 1599mpred_bc_and_with_pfc_0(G):- mpred_call_only_facts(G). % was missing
 1600mpred_bc_and_with_pfc_0(G):- mpred_bc_only0(G).
 1601mpred_bc_and_with_pfc_0(G):- strip_module(G,M,P),inherit_above(M,P).
 1602
 1603
 1604
 1605
 1606% % :- '$set_source_module'(mpred_kb_ops).
 mpred_bc_only0(+G) is semidet
PFC Backchaining Only Primary Helper.
 1612mpred_bc_only0(G):- mpred_unnegate(G,Pos),!, show_call(why,\+ mpred_bc_only(Pos)).
 1613% mpred_bc_only0(G):- pfcBC_NoFacts(G).
 1614mpred_bc_only0(G):- mpred_BC_w_cache(G,G).
 1615
 1616% 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.
 1629pfcBC_NoFacts(F):- pfcBC_NoFacts_TRY(F)*-> true ; (mpred_slow_search,pfcBC_Cache(F)).
 mpred_slow_search is semidet
PFC Slow Search.
 1636mpred_slow_search.
 1637
 1638
 1639/*
 1640%% ruleBackward( +R, ?Condition) is semidet.
 1641%
 1642% Rule Backward.
 1643%
 1644ruleBackward(R,Condition):- call_u(( ruleBackward0(R,Condition),functor(Condition,F,_),
 1645  \+ consequent_arg(_,v(call_u_no_bc,call,call_u),F))).
 1646%ruleBackward0(F,Condition):-clause_u(F,Condition),\+ (is_true(Condition);mpred_is_info(Condition)).
 1647
 1648%% ruleBackward0( +F, ?Condition) is semidet.
 1649%
 1650% Rule Backward Primary Helper.
 1651%
 1652ruleBackward0(F,Condition):- call_u((  '<-'(F,Condition),\+ (is_true(Condition);mpred_is_info(Condition)) )).
 1653
 1654%{X}:-dmsg_pretty(legacy({X})),call_u(X).
 1655*/
 pfcBC_NoFacts_TRY(+F) is semidet
Prolog Forward Chaining Backtackable Class No Facts Try.
 1664pfcBC_NoFacts_TRY(F) :- no_repeats(ruleBackward(F,Condition,Support)),
 1665  % neck(F),
 1666  copy_term((Condition,Support),(CCondition,SupportC)),
 1667  no_repeats(F,call_u(Condition)),  
 1668  maybe_support_bt(F,CCondition,SupportC).
 1669
 1670maybe_support_bt(P,_,_):-mpred_ignored(P),!.
 1671maybe_support_bt(F,Condition,Support):-  
 1672  doall((no_repeats(Why,call_u(bt(F,pt(A,Why)))) *-> mpred_add_support_fast(F,(A,Why)))),
 1673  doall((no_repeats(Why,call_u(bt(F,Why))) *-> mpred_add_support_fast(F,(bt(F,Why),Support)))),
 1674  ignore((maybeSupport(F,(Condition,Support)))).
 1675
 1676:- meta_predicate mpred_why_all(*). 1677mpred_why_all(Call):- !,
 1678      call_u(Call),
 1679      doall((
 1680        lookup_u(Call),
 1681        ignore(show_failure(mpred_why(Call))),
 1682        dmsg_pretty(result=Call),nl)),
 1683   forall(Call,ignore(show_failure(mpred_why(Call)))).
 1684
 1685mpred_why_all(Call):-
 1686      doall((
 1687        call_u(Call),
 1688        ignore(show_failure(mpred_why(Call))),
 1689        dmsg_pretty(result=Call),nl)).
 pfcBC_Cache(+F) is semidet
Prolog Forward Chaining Backtackable Class Cache.
 1697pfcBC_Cache(F) :- mpred_call_only_facts(pfcBC_Cache,F),
 1698   ignore((ground(F),( (\+mpred_call_0(F)), maybeSupport(F,(g,ax))))).
 maybeSupport(+P, ?VALUE2) is semidet
Maybe Support.
 1706maybeSupport(P,_):-mpred_ignored(P),!.
 1707maybeSupport(P,S):- fail, ( \+ ground(P)-> true;
 1708  (predicate_property(P,dynamic)->mpred_post(P,S);true)).
 1709
 1710maybeSupport(P,S):- 
 1711   mpred_add_support_fast(P,S),
 1712   maybeMaybeAdd(P,S).
 1713 
 1714maybeMaybeAdd(P,_):- \+ predicate_property(P,dynamic),!.
 1715maybeMaybeAdd(P,_):- \+ \+ clause_u(P,true),!.
 1716maybeMaybeAdd(P,S):- 
 1717 locally_tl(assert_to(a),
 1718    assert_u_confirmed_was_missing(P)),
 1719   mpred_trace_op(add,P,S),
 1720   mpred_enqueue(P,S).
 mpred_ignored(:TermC) is semidet
PFC Ignored.
 1727mpred_ignored(argIsa(F, A, argIsaFn(F, A))).
 1728mpred_ignored(genls(A,A)).
 1729mpred_ignored(isa(tCol,tCol)).
 1730%mpred_ignored(isa(W,tCol)):-mreq(baseKB:hasInstance_dyn(tCol,W)).
 1731mpred_ignored(isa(W,_)):-is_ftCompound(W),call_u(isa(W,meta_argtypes)).
 1732mpred_ignored(C):-clause_safe(C,true). 
 1733mpred_ignored(isa(_,Atom)):-atom(Atom),atom_concat(ft,_,Atom),!.
 1734mpred_ignored(isa(_,argIsaFn(_, _))).
 has_cl(+H) is semidet
Has Clause.
 1742has_cl(H):-predicate_property(H,number_of_clauses(_)).
 1743
 1744% an action is undoable if there exists a method for undoing it.
 mpred_negation_w_neg(+P, ?NF) is semidet
PFC Negation W Negated.
 1751mpred_negation_w_neg(~(P),P):-is_ftNonvar(P),!.
 1752mpred_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.
 1760baseKB:hook_one_minute_timer_tick:-mpred_cleanup.
 mpred_cleanup is semidet
PFC Cleanup.
 1767mpred_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.
 1774mpred_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.
 1781mpred_cleanup_0(M,P):- findall(P-B-Ref,M:clause(P,B,Ref),L),
 1782  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)))).
 1783
 1784% :-debug.
 1785%isInstFn(A):-!,trace_or_throw_ex(isInstFn(A)).
 1786
 1787%= mpred_unnegate(N,P) is true if N is a negated term and P is the term
 1788%= with the negation operator stripped.
 1789
 1790/*
 1791%% mpred_unnegate( +P, ?P) is semidet.
 1792%
 1793% PFC Negation.
 1794%
 1795mpred_unnegate((-P),P).
 1796% mpred_unnegate((~P),P).
 1797mpred_unnegate((\+(P)),P).
 1798*/
 1799/*
 1800
 1801%% mpred_negated_literal( +P) is semidet.
 1802%
 1803% PFC Negated Literal.
 1804%
 1805mpred_negated_literal(P):-is_reprop(P),!,fail.
 1806mpred_negated_literal(P):-mpred_negated_literal(P,_).
 1807
 1808%% mpred_negated_literal( +P, ?Q) is semidet.
 1809%
 1810% PFC Negated Literal.
 1811%
 1812mpred_negated_literal(P,Q) :- is_ftNonvar(P),
 1813  mpred_unnegate(P,Q),
 1814  mpred_literal(Q).
 1815
 1816*/
 mpred_is_assertable(+X) is semidet
PFC If Is A Assertable.
 1822mpred_is_assertable(X):- mpred_literal_nv(X),\+ functor(X,{},_).
 mpred_literal_nv(+X) is semidet
PFC Literal Nv.
 1828mpred_literal_nv(X):-is_ftNonvar(X),mpred_literal(X).
 1829
 1830/*
 1831%% mpred_literal( +X) is semidet.
 1832%
 1833% PFC Literal.
 1834%
 1835mpred_literal(X) :- is_reprop(X),!,fail.
 1836mpred_literal(X) :- cyclic_term(X),!,fail.
 1837mpred_literal(X) :- atom(X),!.
 1838mpred_literal(X) :- mpred_negated_literal(X),!.
 1839mpred_literal(X) :- mpred_positive_literal(X),!.
 1840mpred_literal(X) :- is_ftVar(X),!.
 1841
 1842*/
 is_reprop(+X) is semidet
If Is A Reprop.
 1848is_reprop(X):- compound(X),is_reprop_0(X).
 is_reprop_0(+X) is semidet
If Is A reprop Primary Helper.
 1854is_reprop_0(~(X)):-!,is_reprop(X).
 1855is_reprop_0(X):-get_functor(X,repropagate,_).
 mpred_non_neg_literal(+X) is semidet
PFC Not Negated Literal.
 1862mpred_non_neg_literal(X):- is_reprop(X),!,fail.
 1863mpred_non_neg_literal(X):- atom(X),!.
 1864mpred_non_neg_literal(X):- sanity(stack_check),
 1865    mpred_positive_literal(X), X \= ~(_), X \= mpred_prop(_,_,_,_), X \= conflict(_).
 1866
 1867% ======================= mpred_file('pfcsupport').	% support maintenance
 is_relative(:TermV) is semidet
If Is A Relative.
 1874is_relative(V):- (\+is_ftCompound(V)),!,fail.
 1875is_relative(update(_)).
 1876is_relative(replace(_)).
 1877is_relative(rel(_)).
 1878is_relative(+(X)):- \+ is_ftVar(X).
 1879is_relative(-(X)):- \+ is_ftVar(X).
 1880is_relative(*(X)):- \+ is_ftVar(X).
 1881
 1882/*
 1883% TODO not called yet
 1884%= mpred_get_trigger_key(+Trigger,-Key)
 1885%=
 1886%= Arg1 is a trigger.  Key is the best term to index it on.
 1887
 1888mpred_get_trigger_key(pt(Key,_),Key).
 1889mpred_get_trigger_key(pk(Key,_,_),Key).
 1890mpred_get_trigger_key(nt(Key,_,_),Key).
 1891mpred_get_trigger_key(Key,Key).
 1892*/
 1893
 1894/*
 1895
 1896the 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. 
 1897
 1898
 1899% TODO not called yet
 1900%=^L
 1901%= Get a key from the trigger that will be used as the first argument of
 1902%= the trigger baseable clause that stores the trigger.
 1903%=
 1904mpred_trigger_key(X,X) :- is_ftVar(X), !.
 1905mpred_trigger_key(chart(word(W),_L),W) :- !.
 1906mpred_trigger_key(chart(stem([Char1|_Rest]),_L),Char1) :- !.
 1907mpred_trigger_key(chart(Concept,_L),Concept) :- !.
 1908mpred_trigger_key(X,X).
 1909*/
 1910% ======================= mpred_file('pfcdb').	% predicates to manipulate database.
 1911
 1912%   File   : pfcdb.pl
 1913%   Author : Tim Finin, finin@prc.unisys.com
 1914%   Author :  Dave Matuszek, dave@prc.unisys.com
 1915%   Author :  Dan Corpron
 1916%   Updated: 10/11/87, ...
 1917%   Purpose: predicates to manipulate a pfc database (e.g. save,
 1918%	restore, reset, etc).
 clause_or_call(+H, ?B) is semidet
Clause Or Call.
 1929clause_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).
 1930clause_or_call(isa(I,C),true):-!,call_u(isa_asserted(I,C)).
 1931clause_or_call(genls(I,C),true):-!,on_x_log_throw(call_u(genls(I,C))).
 1932clause_or_call(H,B):- clause(src_edit(_Before,H),B).
 1933clause_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)).
 1934clause_or_call(H,true):- call_u(should_call_for_facts(H)),no_repeats(on_x_log_throw(H)).
 1935
 1936
 1937% as opposed to simply using clause(H,true).
 should_call_for_facts(+H) is semidet
Should Call For Facts.
 1943should_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.
 1949should_call_for_facts(_,F,_):- a(prologSideEffects,F),!,fail.
 1950should_call_for_facts(H,_,_):- modulize_head(H,HH), \+ predicate_property(HH,number_of_clauses(_)),!.
 1951should_call_for_facts(_,F,A):- clause_b(mpred_prop(_M,F,A,pfcRHS)),!,fail.
 1952should_call_for_facts(_,F,A):- clause_b(mpred_prop(_M,F,A,pfcMustFC)),!,fail.
 1953should_call_for_facts(_,F,_):- a(prologDynamic,F),!.
 1954should_call_for_facts(_,F,_):- \+ a(pfcControlled,F),!.
 no_side_effects(+P) is semidet
No Side Effects.
 1962no_side_effects(P):-  (\+ is_side_effect_disabled->true;(get_functor(P,F,_),a(prologSideEffects,F))).
 1963
 1964
 1965:- was_dynamic(functorIsMacro/1).
 compute_resolve(+NewerP, ?OlderQ, ?SU, ?SU, ?OlderQ) is semidet
Compute Resolve.
 1972compute_resolve(NewerP,OlderQ,SU,SU,(mpred_blast(OlderQ),mpred_ain(NewerP,S),mpred_withdraw(conflict(NewerP)))):-
 1973  must(correctify_support(SU,S)),
 1974  wdmsg_pretty(compute_resolve(newer(NewerP-S)>older(OlderQ-S))).
 1975compute_resolve(NewerP,OlderQ,S1,[U],Resolve):-compute_resolve(OlderQ,NewerP,[U2],S1,Resolve),match_source_ref1(U),match_source_ref1(U2),!.
 1976compute_resolve(NewerP,OlderQ,SU,S2,(mpred_blast(OlderQ),mpred_ain(NewerP,S1),mpred_withdraw(conflict(NewerP)))):-
 1977  must(correctify_support(SU,S1)),
 1978  wdmsg_pretty(compute_resolve((NewerP-S1)>(OlderQ-S2))).
 compute_resolve(+NewerP, ?OlderQ, ?Resolve) is semidet
Compute Resolve.
 1986compute_resolve(NewerP,OlderQ,Resolve):-
 1987   supporters_list(NewerP,S1),
 1988   supporters_list(OlderQ,S2),
 1989   compute_resolve(NewerP,OlderQ,S1,S2,Resolve).
 is_resolved(+C) is semidet
If Is A Resolved.
 1997is_resolved(C):- Why= is_resolved, mpred_call_only_facts(Why,C),\+mpred_call_only_facts(Why,~(C)).
 1998is_resolved(C):- Why= is_resolved, mpred_call_only_facts(Why,~(C)),\+mpred_call_only_facts(Why,C).
 1999
 2000:- must(nop(_)).
 mpred_prove_neg(+G) is semidet
PFC Prove Negated.
 2007mpred_prove_neg(G):-  (dtrace), \+ mpred_bc_only(G), \+ mpred_fact(G).
 pred_head(:PRED1Type, ?P) is semidet
Predicate Head.
 2014pred_head(Type,P):- no_repeats(P,(call(Type,P),\+ nonfact_metawrapper(P),is_ftCompound(P))).
 pred_head_all(+P) is semidet
Predicate Head All.
 2021pred_head_all(P):- pred_head(pred_all,P).
 nonfact_metawrapper(:TermP) is semidet
Nonfact Metawrapper.
 2028nonfact_metawrapper(~(_)).
 2029nonfact_metawrapper(pt(_,_)).
 2030nonfact_metawrapper(bt(_,_,_)).
 2031nonfact_metawrapper(nt(_,_)).
 2032nonfact_metawrapper(spft(_,_,_)).
 2033nonfact_metawrapper(added(_)).
 2034% we use the arity 1 forms is why 
 2035nonfact_metawrapper(term_expansion(_,_)).
 2036nonfact_metawrapper(P):- \+ current_predicate(_,P).
 2037nonfact_metawrapper(M:P):-atom(M),!,nonfact_metawrapper(P).
 2038nonfact_metawrapper(P):- get_functor(P,F,_), 
 2039   (a(prologSideEffects,F);a(rtNotForUnboundPredicates,F)).
 2040nonfact_metawrapper(P):-rewritten_metawrapper(P).
 rewritten_metawrapper(+C) is semidet
Rewritten Metawrapper.
 2047rewritten_metawrapper(_):-!,fail.
 2048%rewritten_metawrapper(isa(_,_)).
 2049rewritten_metawrapper(C):-is_ftCompound(C),functor(C,t,_).
 2050
 2051
 2052%% meta_wrapper_rule( :TermARG1) is semidet.
 2053%
 2054% Meta Wrapper Rule.
 2055%
 2056meta_wrapper_rule((_<-_)).
 2057meta_wrapper_rule((_<==>_)).
 2058meta_wrapper_rule((_==>_)).
 2059meta_wrapper_rule((_:-_)).
 pred_all(+P) is semidet
Predicate All.
 2067pred_all(P):-pred_u0(P).
 2068pred_all(P):-pred_t0(P).
 2069pred_all(P):-pred_r0(P).
 pred_u0(+P) is semidet
Predicate For User Code Primary Helper.
 2076pred_u0(P):-pred_u1(P),has_db_clauses(P).
 2077pred_u0(P):-pred_u2(P).
 pred_u1(+VALUE1) is semidet
Predicate For User Code Secondary Helper.
 2083pred_u1(P):-a(pfcControlled,F),arity_no_bc(F,A),functor(P,F,A).
 2084pred_u1(P):-a(prologHybrid,F),arity_no_bc(F,A),functor(P,F,A).
 2085pred_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.
 2091pred_u2(P):- compound(P),functor(P,F,A),sanity(no_repeats(arity_no_bc(F,A))),!,has_db_clauses(P).
 2092pred_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.
 2100has_db_clauses(PI):-modulize_head(PI,P),
 2101   predicate_property(P,number_of_clauses(NC)),\+ predicate_property(P,number_of_rules(NC)), \+ \+ clause_u(P,true).
Predicate True Stucture Primary Helper.
 2109pred_t0(P):-mreq('==>'(P)).
 2110pred_t0(P):-mreq(pt(P,_)).
 2111pred_t0(P):-mreq(bt(P,_)).
 2112pred_t0(P):-mreq(nt(P,_,_)).
 2113pred_t0(P):-mreq(spft(P,_,_)).
 2114
 2115%pred_r0(-(P)):- call_u(-(P)).
 2116%pred_r0(~(P)):- mreq(~(P)).
 2117
 2118
 2119%% pred_r0( :TermP) is semidet.
 2120%
 2121% Predicate R Primary Helper.
 2122%
 2123pred_r0(P==>Q):- mreq(P==>Q).
 2124pred_r0(P<==>Q):- mreq(P<==>Q).
 2125pred_r0(P<-Q):- mreq(P<-Q).
 cnstrn(+X) is semidet
Cnstrn.
 2132:- module_transparent(cnstrn/1). 2133cnstrn(X):-term_variables(X,Vs),maplist(cnstrn0(X),Vs),!.
 cnstrn(+V, ?X) is semidet
Cnstrn.
 2139:- module_transparent(cnstrn/2). 2140cnstrn(V,X):-cnstrn0(X,V).
 cnstrn0(+X, ?V) is semidet
Cnstrn Primary Helper.
 2146:- module_transparent(cnstrn0/2). 2147cnstrn0(X,V):-when(is_ftNonvar(V),X).
 rescan_pfc is semidet
Rescan Prolog Forward Chaining.
 2154rescan_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.
 2161mpred_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.
 2168repropagate(_):-  check_context_module,fail.
 2169%repropagate(P):-  check_real_context_module,fail.
 2170
 2171repropagate(P):-  is_ftVar(P),!.
 2172repropagate(==>P):- !,repropagate(P).
 2173repropagate(P):-  meta_wrapper_rule(P),!,call_u(repropagate_meta_wrapper_rule(P)).
 2174repropagate(P):-  \+ predicate_property(P,_),'$find_predicate'(P,PP),PP\=[],!,forall(member(M:F/A,PP),
 2175                                                          must((functor(Q,F,A),repropagate_1(M:Q)))).
 2176repropagate(F/A):- is_ftNameArity(F,A),!,functor(P,F,A),!,repropagate(P).
 2177repropagate(F/A):- atom(F),is_ftVar(A),!,repropagate(F).
 2178
 2179repropagate(P):-  \+ predicate_property(_:P,_),dmsg_pretty(undefined_repropagate(P)),dumpST,dtrace,!,fail.
 2180repropagate(P):-  call_u(repropagate_0(P)).
 2181
 2182
 2183predicate_to_goal(P,Goal):-atom(P),get_arity(P,F,A),functor(Goal,F,A).
 2184predicate_to_goal(PF/A,Goal):-atom(PF),get_arity(PF,F,A),functor(Goal,F,A).
 2185predicate_to_goal(G,G):-compound(G),!.
 repropagate_0(+P) is semidet
repropagate Primary Helper.
 2191repropagate_0(P):- loop_check(call_u(repropagate_1(P)),true).
 2192
 2193:- thread_local t_l:is_repropagating/1.
 repropagate_1(+P) is semidet
repropagate Secondary Helper.
 2200repropagate_1(P):- is_ftVar(P),!.
 2201repropagate_1(==>P):- !,repropagate_1(P).
 2202repropagate_1(USER:P):- USER==user,!,repropagate_1(P).
 2203%repropagate_1((P/_)):-!,repropagate_1(P).
 2204
 2205repropagate_1(P):- call_u(repropagate_2(P)).
 2206
 2207:- export(repropagate_2/1). 2208:- module_transparent(repropagate_2/1).
 repropagate_2(+P) is semidet
repropagate Extended Helper.
 2214repropagate_2(P):-
 2215 call_u(doall((no_repeats((mpred_facts_and_universe(P))),
 2216    locally_tl(is_repropagating(P),ignore((once(show_failure(fwd_ok(P))),show_call(mpred_fwc(P)))))))).
 2217
 2218% repropagate_meta_wrapper_rule(P==>_):- !, repropagate(P).
 repropagate_meta_wrapper_rule(+P) is semidet
Repropagate Meta Wrapper Rule.
 2224repropagate_meta_wrapper_rule(P):-repropagate_1(P).
 fwd_ok(:TermP) is semidet
Forward Repropigated Ok.
 2231fwd_ok(_):-!.
 2232fwd_ok(P):-ground(P),!.
 2233fwd_ok(if_missing1(_,_)).
 2234fwd_ok(idForTest(_,_)).
 2235fwd_ok(clif(_)).
 2236fwd_ok(pfclog(_)).
 2237fwd_ok(X):-compound(X),get_assertion_head_arg(_,X,E),compound(E),functor(E,(:-),_),!.
 2238% fwd_ok(P):-must(ground(P)),!.
 mpred_facts_only(+P) is semidet
PFC Facts Only.
 2245mpred_facts_only(P):- (is_ftVar(P)->(pred_head_all(P),\+ meta_wrapper_rule(P));true),no_repeats(P).
 2246
 2247
 2248
 2249:- thread_local(t_l:user_abox/1). 2250
 2251% :- ((prolog_load_context(file,F),  prolog_load_context(source,F))-> throw(prolog_load_context(source,F)) ; true). :- include('mpred_header.pi').
 2252:- style_check(+singleton). 2253
 2254% TODO READD
 2255%:- foreach(arg(_,isEach(prologMultiValued,prologOrdered,prologNegByFailure,prologPTTP,prologKIF,pfcControlled,ttRelationType,
 2256%     prologHybrid,predCanHaveSingletons,prologDynamic,prologBuiltin,functorIsMacro,prologListValued,prologSingleValued),P),)
 get
 2260% =================================================
 2261% ==============  UTILS BEGIN        ==============
 2262% =================================================
 ruleBackward(+R, ?Condition) is semidet
Rule Backward.
 2268ruleBackward(R,Condition,Support):- ruleBackward0(R,Condition,Support),
 2269  functor(Condition,F,_),\+ arg(_,v(call_u,mpred_bc_only,inherit_above),F).
 ruleBackward0(+F, ?Condition) is semidet
Rule Backward Primary Helper.
 2275ruleBackward0(F,Condition,Support):- call_u('<-'(FF,Condition)),copy_term('<-'(FF,Condition),Support),FF=F.
 2276%ruleBackward0(F,Condition,(F :- Condition)):- clause_u(F,Condition),\+ (is_true(Condition);mpred_is_info(Condition)).
 2277
 2278
 2279% ======================= 
 2280% user''s program''s database
 2281% ======================= 
 assert_mu(+X) is semidet
Assert For User Code.
 2289assert_mu(MH):-  fix_mp(clause(assert,assert_u), MH,M,H),get_unnegated_functor(H,F,A),assert_mu(M,H,F,A).
 2290asserta_mu(MH):- fix_mp(clause(assert,asserta_u),MH,M,H),asserta_mu(M,H).
 2291assertz_mu(MH):- fix_mp(clause(assert,assertz_u),MH,M,H),assertz_mu(M,H).
 2292
 2293
 2294% :- kb_shared(baseKB:singleValuedInArg/2).
 2295:- thread_local(t_l:assert_to/1).
 assert_mu(+Module, +Pred, ?Functor, ?Arity) is semidet
Assert For User Code.
 2301assert_mu(M,M2:Pred,F,A):- M == M2,!, assert_mu(M,Pred,F,A).
 2302% maYBE assert_mu(M,(M2:Pred :- B),F,A):- M == M2,!, assert_mu(M,(Pred :- B),F,A).
 2303assert_mu(M,_:Pred,F,A):- dtrace,sanity(\+ is_ftVar(Pred)),!, assert_mu(M,Pred,F,A).
 2304assert_mu(M,(Pred:- (AWC,More)),_,_):- AWC == awc,!,asserta_mu(M,(Pred:- (AWC,More))).
 2305assert_mu(M,(Pred:- (ZWC,More)),_,_):- ZWC == zwc,!,assertz_mu(M,(Pred:- (ZWC,More))).
 2306%assert_mu(M,Pred,F,_):- clause_b(singleValuedInArg(F,SV)),!,must(update_single_valued_arg(M,Pred,SV)),!.
 2307%assert_mu(M,Pred,F,A):- a(prologSingleValued,F),!,must(update_single_valued_arg(M,Pred,A)),!.
 2308assert_mu(M,Pred,F,_):- a(prologOrdered,F),!,assertz_mu(M,Pred).
 2309assert_mu(M,Pred,_,_):- t_l:assert_to(Where),!, (Where = a -> asserta_mu(M,Pred); assertz_mu(M,Pred)).
 2310%assert_mu(M,Pred,_,1):- !, assertz_mu(M,Pred),!.
 2311assert_mu(M,Pred,_,_):- assertz_mu(M,Pred).
 2312
 2313
 2314:-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)))).

 2327assertz_mu(_,X):- check_never_assert(X),fail.
 2328%assertz_mu(M,M2:Pred,F,A):- M == M2,!, assertz_mu(M,Pred,F,A).
 2329%assertz_mu(M,spft(P,mfl4(VarNameZ,KB,F,L),T)):-M\==KB,!,assertz_mu(KB,spft(P,mfl4(VarNameZ,KB,F,L),T)).
 2330assertz_mu(M,X):- strip_module(X,_,P), %sanity(check_never_assert(M:P)), 
 2331    must((expire_tabled_list(M:P),show_failure(attvar_op(db_op_call(assertz,assertz_i),M:P)))).
 2332   %(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)))).

 2342asserta_mu(_,X):- check_never_assert(X),fail.
 2343asserta_mu(M,X):- strip_module(X,_,P),!, %sanity(check_never_assert(M:P)), 
 2344    must((expire_tabled_list(M:P),show_failure(attvar_op(db_op_call(asserta,asserta_i),M:P)))).
 2345   %(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)))).

 2353retract_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))).
 2354retract_mu(X):- check_never_retract(X),fail.
 2355retract_mu(~(X)):-!,show_success(why,retract_eq_quitely_f(~(X))),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).
 2356retract_mu((X)):-!,show_success(why,retract_eq_quitely_f((X))),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).
 2357retract_mu(M:(H:-B)):- atom(M),!, clause_u(H,B,R),erase(R).
 2358retract_mu((H:-B)):-!, clause_u(H,B,R),erase(R).
 2359%retract_mu(~(X)):-must(is_ftNonvar(X)),!,retract_eq_quitely_f(~(X)),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).
 2360%retract_mu(hs(X)):-!,retract_eq_quitely_f(hs(X)),must((expire_tabled_list(~(X)))),must((expire_tabled_list((X)))).
 2361
 2362
 2363
 2364
 2365
 2366:- retractall(t_l:mpred_debug_local). 2367:- thread_local(t_l:in_rescan_mpred_hook/0). 2368
 2369:- module_transparent(mpred_call_0/1). 2370
 2371 :- meta_predicate update_single_valued_arg(+,+,*). 2372 :- meta_predicate assert_mu(*,+,*,*). 2373 :- meta_predicate mpred_facts_and_universe(*). 2374 :- meta_predicate {*}. 2375 :- meta_predicate neg_in_code0(*). 2376 :- meta_predicate repropagate_2(*). 2377 :- meta_predicate mpred_get_support_via_sentence(*,*). 2378
 2379:- dynamic(infoF/1). 2380
 2381
 2382
 2383mpred_kb_ops_file.
 2384
 2385:- fixup_exports.