1/*   
    2%  LogicMOO Base FOL/PFC Setup
    3% Dec 13, 2035
    4% Douglas Miles
    5%
    6%   File   : pfccompile.pl
    7%   Author : Tim Finin, finin@prc.unisys.com
    8%   Updated: 10/11/87, ...
    9%   Purpose: compile system file for Pfc
   10
   11
   12*/
   13:- if(('$current_source_module'(SM),'context_module'(M),'$current_typein_module'(CM),asserta(baseKB:'using_pfc'(M,CM,SM,pfc_lib)))).   14:- endif.   15
   16:- if((prolog_load_context(source,File),prolog_load_context(file,File))).   17:- module(pfc_lib,[]).   18:- endif.   19%:- set_prolog_flag(gc,false).
   20:- set_prolog_flag(pfc_version,2.0).   21:- set_prolog_flag(retry_undefined, kb_shared).   22
   23kb_global_w(M:F/A):- 
   24   M:multifile(M:F/A),
   25   M:module_transparent(M:F/A),
   26   M:dynamic(M:F/A),
   27   M:export(M:F/A),
   28   do_import(system,M,F,A),
   29   do_import(user,M,F,A),
   30   do_import(pfc_lib,M,F,A),
   31   do_import(header_sane,M,F,A),
   32   M:kb_global(M:F/A),
   33   system:import(M:F/A).
   34
   35
   36:- user:use_module(library(file_scope)).   37:- set_prolog_flag_until_eof(access_level,system).   38
   39:- user:use_module(library(attvar_reader)).   40:- user:use_module(library(logimcoo/each_call)).   41:- user:use_module(library(must_trace)).   42:- user:use_module(library(virtualize_source)).   43:- user:use_module(library(hook_hybrid)).   44:- user:use_module(library(no_repeats)).   45:- user:use_module(library(logicmoo_util_strings)).   46:- user:use_module(library(loop_check)).   47:- user:use_module(library(attvar_serializer)).   48
   49:- dynamic(rdf_rewrite:(~)/1).   50:- kb_global_w(rdf_rewrite:arity/2).   51:- kb_global_w(baseKB:genlMt/2).   52:- kb_global_w(baseKB:mpred_prop/4).   53:- kb_global_w(baseKB:mtHybrid/1).   54:- kb_global_w(baseKB:mtProlog/1).   55:- kb_global_w(baseKB:tCol/1).   56:- kb_global_w(baseKB:mpred_database_term/3).   57:- kb_global_w(baseKB:mtNoPrologCode/1).   58:- kb_global_w(baseKB:ftText/1).   59:- kb_global_w(baseKB:mtNotInherits/1).   60:- kb_global_w(baseKB:mtInherits/1).   61:- kb_global_w(baseKB:rtArgsVerbatum/1).   62
   63
   64
   65
   66
   67:- kb_shared(baseKB:never_assert_u/1).   68:- kb_shared(baseKB:never_assert_u/2).   69:- kb_shared(baseKB:never_retract_u/1).   70:- kb_shared(baseKB:never_retract_u/2).   71:- kb_shared(baseKB:mpred_prop/4).   72:- kb_shared(baseKB:do_and_undo/2).   73:- kb_shared(baseKB:spft/3).   74:- kb_shared(baseKB:bt/2).   75:- kb_shared(baseKB:hs/1).   76:- kb_shared(baseKB:nt/3).   77:- kb_shared(baseKB:pk/3).   78:- kb_shared(baseKB:pt/2).   79:- kb_shared(baseKB:que/2).   80:- kb_shared(baseKB:pm/1).   81:- kb_shared(baseKB:spft/3).   82:- kb_shared(baseKB:tms/1).   83
   84
   85
   86%:- listing(arity/2).
   87%:- listing(baseKB:_).
   88
   89:- set_prolog_flag_until_eof(debug,true).   90
   91:- if(\+ current_prolog_flag(lm_pfc_lean,_)).   92:- set_prolog_flag(lm_pfc_lean,true).   93:- endif.   94
   95
   96kb_shared_base(M:FA):-!,kb_shared(M:FA).
   97kb_shared_base(FA):-kb_local(baseKB:FA).
   98kb_local_base(M:FA):-!,kb_local(M:FA).
   99kb_local_base(FA):-kb_local(baseKB:FA).
  100kb_global_base(M:FA):-!,kb_global(M:FA).
  101kb_global_base(FA):- kb_local(baseKB:FA).
  102
  103
  104
  105% :- kb_global_base(baseKB:genlMt/2).
  106
  107:- kb_shared(baseKB:mpred_prop/4).  108
  109:- baseKB:forall(between(1,11,A),kb_local(t/A)).  110:- baseKB:forall(between(5,7,A),kb_local(mpred_f/A)).  111
  112% :- kb_shared_base(baseKB:admittedArgument/3).
  113%:- set_prolog_flag(runtime_speed,0). % 0 = dont care
  114:- set_prolog_flag(runtime_speed, 1). % 1 = default
  115:- set_prolog_flag(runtime_debug, 1). % 2 = important but dont sacrifice other features for it
  116:- set_prolog_flag(runtime_safety, 3).  % 3 = very important
  117:- set_prolog_flag(unsafe_speedups, false).  118:- set_prolog_flag(pfc_booted,false).  119
  120
  121:- use_module(library(prolog_pack)).  122pfc_rescan_autoload_pack_packages_part_1:- dmsg("SCAN AUTOLOADING PACKAGES..."),
  123 forall('$pack':pack(Pack, _),
  124  forall(((pack_property(Pack, directory(PackDir)),prolog_pack:pack_info_term(PackDir,autoload(true)))),
  125  (access_file(PackDir,write) -> prolog_pack:post_install_autoload(PackDir, [autoload(true)]) ; dmsg(cannot_access_file(PackDir,write))))),
  126 dmsg(".. SCAN AUTOLOADING COMPLETE"),!.
  127:- current_prolog_flag(lm_no_autoload,true) -> true; pfc_rescan_autoload_pack_packages_part_1.  128
  129
  130:- meta_predicate pack_autoload_packages(0).  131pack_autoload_packages(NeedExistingIndex):- 
  132 forall(user:expand_file_search_path(library(''),Dir),
  133  ignore(( (\+ NeedExistingIndex ; absolute_file_name('INDEX',_Absolute,[relative_to(Dir),access(read),file_type(prolog),file_errors(fail)]))->
  134   maybe_index_autoload_dir(Dir)))),
  135 reload_library_index.
  136
  137
  138maybe_index_autoload_dir(PackDir):- \+ access_file(PackDir,write),!,dmsg(cannot_write_autoload_dir(PackDir)).
  139maybe_index_autoload_dir(PackDir):- user:library_directory(PackDir), make_library_index(PackDir, ['*.pl']),dmsg(update_library_index(PackDir)).
  140maybe_index_autoload_dir(PackDir):- fail,
  141  prolog_pack:pack_info_term(PackDir,autoload(true)),
  142  prolog_pack:post_install_autoload(PackDir, [autoload(true)]) ,
  143  dmsg(post_install_autoload(PackDir,write)).  
  144maybe_index_autoload_dir(PackDir):- asserta(user:library_directory(PackDir)), make_library_index(PackDir, ['*.pl']), dmsg(created_library_index_for(PackDir)).
  145
  146pfc_rescan_autoload_pack_packages_part_2 :- pack_autoload_packages(true).
  147
  148:- current_prolog_flag(lm_no_autoload,true) -> true; pfc_rescan_autoload_pack_packages_part_2.  149
  150
  151
  152input_from_file:- prolog_load_context(stream,Stream),current_input(Stream).
  153
  154:- module_transparent(intern_predicate/1).  155:- module_transparent(intern_predicate/2).  156intern_predicate(MFA):- '$current_typein_module'(To),intern_predicate(To,MFA).
  157intern_predicate(To,F/A):- !, '$current_source_module'(M),intern_predicate(To,M:F/A).
  158intern_predicate(To,From:F/A):-!,
  159  From:module_transparent(From:F/A),
  160  From:export(From:F/A),To:export(From:F/A),To:export(From:F/A),
  161  From:compile_predicates([F/A]),system:lock_predicate(From:F/A),
  162  export(From:F/A),export(From:F/A),
  163  pfc:export(From:F/A),pfc:export(From:F/A),
  164  user:export(From:F/A),user:export(From:F/A),
  165  baseKB:export(From:F/A),baseKB:export(From:F/A),
  166  system:export(From:F/A),system:export(From:F/A),!.
  167
  168scan_missed_source:-!.
  169scan_missed_source:-
  170  prolog_load_context(file,File),scan_missed_source(File),
  171  prolog_load_context(source,SFile),!,
  172  (SFile==File-> true; scan_missed_source(SFile)).
  173
  174scan_missed_source(SFile):-prolog_load_context(module,M),
  175   forall(source_file(Pred,SFile),scan_missed_source(M,Pred,SFile)).
  176
  177scan_missed_source(M,Pred,SFile):- \+ M:clause(Pred,_,_),!,nop(dmsg(scan_missed_source(M,Pred,SFile))).
  178scan_missed_source(M,Pred,SFile):- doall((M:clause(Pred,_,Ref),
  179  (clause_property(Ref,file(SFile)) -> visit_pfc_file_ref(M,Ref) ; visit_pfc_non_file_ref(M,Ref)))).
  180
  181visit_pfc_file_ref(M,Ref):- system:clause(H,B,Ref),dmsg(visit_pfc_file_ref(M,H,B)).
  182visit_pfc_non_file_ref(M,Ref):- system:clause(H,B,Ref),dmsg(visit_pfc_non_file_ref(M,H,B)).
  183
  184
  185
  186:- intern_predicate(system,intern_predicate/1).  187
  188:- intern_predicate(system,intern_predicate/2).  189
  190'?='(ConsqIn):- fully_expand(ConsqIn,Consq),call_u(Consq),forall(mpred_why(Consq,Ante),wdmsg(Ante)).
  191'?=>'(AnteIn):- fully_expand(AnteIn,Ante),call_u(Ante),forall(mpred_why(Consq,Ante),wdmsg(Consq)).
  192
  193:- lock_predicate(pfc:'?='/1).  194:- lock_predicate(pfc:'?=>'/1).  195
  196:- thread_local(t_l:disable_px).  197
  198:- include('pfc2.0/mpred_header.pi').  199
  200/*
  201:- nop(kb_shared((
  202   bt/2, %basePFC
  203   hs/1, %basePFC
  204   nt/3, %basePFC
  205   pk/3, %basePFC
  206   pt/2, %basePFC
  207   que/1, %basePFC
  208   pm/1, %basePFC
  209   spft/3, %basePFC
  210   tms/1 %basePFC
  211   ))).
  212:- nop(kb_shared( ('~') /1)).
  213*/
  214
  215
  216:- if( \+ current_predicate(each_call_cleanup/3)).  217:- user:use_module(library(each_call_cleanup)).  218:- endif.  219
  220/*
  221% Make YALL require ">>" syntax (the problem was it autoloads when its sees PFC code containing "/" and gripes all the time)
  222
  223disable_yall:- multifile(yall:lambda_functor/1),
  224   dynamic(yall:lambda_functor/1),
  225   with_no_mpred_expansions(use_module(yall:library(yall),[])),
  226   retractall(yall:lambda_functor('/')).
  227
  228:- disable_yall.
  229*/
  230
  231:- set_prolog_flag_until_eof(access_level,system).  232
  233/*
  234% baseKB:startup_option(datalog,sanity). %  Run datalog sanity tests while starting
  235% baseKB:startup_option(clif,sanity). %  Run datalog sanity tests while starting
  236:- set_prolog_flag(fileerrors,false).
  237:- set_prolog_flag(gc,false).
  238:- set_prolog_flag(gc,true).
  239:- set_prolog_flag(optimise,false).
  240:- set_prolog_flag(last_call_optimisation,false).
  241:- set_prolog_flag(debug,true).
  242:- debug.
  243*/
  244%:- guitracer.
  245%:- set_prolog_flag(access_level,system).
  246
  247% :- set_prolog_flag(logicmoo_autoload,false).
  248% :- set_prolog_flag(logicmoo_autoload,true).
  249
  250% must be xref-ing or logicmoo_autoload or used as include file
  251:- set_prolog_flag(logicmoo_include,lmbase:skip_module_decl).  252% lmbase:skip_module_decl:- source_location(F,L),wdmsg(lmbase:skip_module_decl(F:L)),!,fail.
  253lmbase:skip_module_decl:- prolog_load_context(file,F), prolog_load_context(source,S),S\=F,!.
  254lmbase:skip_module_decl:-!,fail.
  255lmbase:skip_module_decl:-
  256   (current_prolog_flag(xref,true)-> false ;
  257    (current_prolog_flag(logicmoo_autoload,true)-> false ;
  258      ((prolog_load_context(file,F),  prolog_load_context(source,F))
  259             -> throw(error(format(":- include(~w).",[F]),reexport(F))) ; true))). 
  260
  261%%% TODO one day :- set_prolog_flag(logicmoo_include,fail).
  262
  263
  264baseKB:mpred_skipped_module(eggdrop).
  265:- forall(current_module(CM),assert(baseKB:mpred_skipped_module(CM))).  266:- retractall(baseKB:mpred_skipped_module(pfc_lib)).  267
  268% ================================================
  269% DBASE_T System
  270% ================================================    
  271
  272:- multifile(baseKB:safe_wrap/4).  273:- dynamic(baseKB:safe_wrap/4).  274
  275:- if((current_prolog_flag(runtime_debug,D),D>1)).  276:- dmsg("Ensuring PFC Loaded").  277:- endif.  278
  279:- use_module(library(subclause_expansion)).  280%:- reexport(library('pfc2.0/mpred_core.pl')).
  281%:- system:reexport(library('pfc2.0/mpred_at_box.pl')).
  282
  283:- user:use_module(library('file_scope')).  284% :- virtualize_source_file.
  285:- module_transparent(baseKB:prologBuiltin/1).  286:- multifile baseKB:prologBuiltin/1.  287:- discontiguous baseKB:prologBuiltin/1.  288:- dynamic baseKB:prologBuiltin/1.  289
  290
  291%:- break.
  292%system:ensure_abox(M):- wdmsg(was(ensure_abox(M))).
  293
  294:- multifile(lmcache:mpred_directive_value/3).  295:- volatile(lmcache:mpred_directive_value/3).  296:- dynamic(lmcache:mpred_directive_value/3).  297/*
  298:- multifile(mpred_database_term/3).
  299:- volatile(mpred_database_term/3).
  300:- dynamic(mpred_database_term/3).
  301*/
  302
  303:- reexport(('../pfc2.2/src/pfcsyntax.pl')).  304:- reexport(('../pfc2.2/src/pfccore')).  305:- reexport(('../pfc2.2/src/pfcsupport')).  306:- reexport(('../pfc2.2/src/pfcdb')).  307:- reexport(('../pfc2.2/src/pfcdebug')).  308:- reexport(('../pfc2.2/src/pfcjust')).  309:- reexport(('../pfc2.2/src/pfcwhy')).  310:- reexport(library(pfc_ex)).  311
  312%:- autoload([verbose(false)]).
  313
  314
  315
  316%baseKB:sanity_check:- findall(U,(current_module(U),default_module(U,baseKB)),L),must(L==[baseKB]).
  317baseKB:sanity_check:- doall((current_module(M),setof(U,(current_module(U),default_module(U,M),U\==M),L),
  318     wdmsg(imports_eache :- (L,[sees(M)])))).
  319baseKB:sanity_check:- doall((current_module(M),setof(U,(current_module(U),default_module(M,U),U\==M),L),wdmsg(imports(M):-L))).
  320baseKB:sanity_check:- doall((baseKB:mtProlog(M),
  321    setof(U,(current_module(U),default_module(M,U),U\==M),L),wdmsg(imports(M):-L))).
  322
  323
  324%:- rtrace((mpred_at_box:defaultAssertMt(G40331),rtrace(set_prolog_flag(G40331:unknown,warning)))).
  325%:- dbreak.
  326:- must(set_prolog_flag(abox:unknown,error)).  327%:- locally_tl(side_effect_ok,doall(call_no_cuts(module_local_init(abox,baseKB)))).
  328% :- forall(baseKB:sanity_check,true).
  329
  330
  331:- if( \+ prolog_load_context(reload,true)).  332:-module_transparent(hook_database:ain/1).  333:-module_transparent(hook_database:aina/1).  334:-module_transparent(hook_database:ainz/1).  335:-multifile(hook_database:ain/1).  336:-multifile(hook_database:aina/1).  337:-multifile(hook_database:ainz/1).  338:-dynamic(hook_database:ain/1).  339:-dynamic(hook_database:aina/1).  340:-dynamic(hook_database:ainz/1).  341:-module_transparent(mpred_core:mpred_ain/1).  342:-module_transparent(mpred_core:mpred_aina/1).  343:-module_transparent(mpred_core:mpred_ainz/1).  344:-multifile(mpred_core:mpred_ain/1).  345:-multifile(mpred_core:mpred_aina/1).  346:-multifile(mpred_core:mpred_ainz/1).  347:-dynamic(mpred_core:mpred_ain/1).  348:-dynamic(mpred_core:mpred_aina/1).  349:-dynamic(mpred_core:mpred_ainz/1).  350:-hook_database:export(mpred_core:mpred_ain/1).  351:-hook_database:export(mpred_core:mpred_aina/1).  352:-hook_database:export(mpred_core:mpred_ainz/1).  353
  354:-asserta_new((hook_database:ainz(G):- !, mpred_ainz(G))).  355:-asserta_new((hook_database:ain(M:G):- !, M:mpred_ain(M:G))).  356:-asserta_new((hook_database:aina(G):- !, mpred_aina(G))).  357:- endif.  358
  359% Load boot base file
  360user:lmbf:- 
  361 locally( set_prolog_flag(mpred_te,true),
  362  locally( set_prolog_flag(subclause_expansion,true),
  363   locally(set_prolog_flag(pfc_booted,false),
  364     with_umt(baseKB,
  365  prolog_statistics:time((reexport(baseKB:library(logicmoo/pfc/'system_base.pfc')))))))),
  366  set_prolog_flag(pfc_booted,true).
  367
  368/*
  369:- set_prolog_flag(unknown,error).
  370:- set_prolog_flag(user:unknown,error).
  371:- set_prolog_flag(lmcode:unknown,error).
  372:- set_prolog_flag(baseKB:unknown,error).
  373*/
  374:- sanity(current_prolog_flag(unknown,error)).  375:- sanity(current_prolog_flag(user:unknown,error)).  376
  377in_goal_expansion:- prolog_current_frame(F),
  378   prolog_frame_attribute(F,parent_goal,expand_goal(_,_,_,_)).
  379
  380in_clause_expand(I):-  nb_current('$goal_term',Was),same_terms(I, Was),!,fail.
  381in_clause_expand(I):-  
  382   (nb_current_or_nil('$source_term',TermWas),\+ same_terms(TermWas, I)),
  383   (nb_current_or_nil('$term',STermWas),\+ same_terms(STermWas, I)),!,
  384   fail.
  385in_clause_expand(_).
  386
  387
  388% SHOULD NOT NEED THIS 
  389%          maybe_should_rename(M,O):-current_prolog_flag(do_renames,term_expansion),if_defined(do_renames(M,O)),!.
  390maybe_should_rename(O,O).
  391
  392
  393:- multifile(baseKB:ignore_file_mpreds/1).  394:- dynamic(baseKB:ignore_file_mpreds/1).  395:- multifile(baseKB:expect_file_mpreds/1).  396:- dynamic(baseKB:expect_file_mpreds/1).  397
  398% baseKB:expect_file_mpreds(File):- prolog_load_context(file,File),t_l:current_lang(pfc).
  399% baseKB:expect_file_mpreds(File):- prolog_load_context(source,File),t_l:current_lang(pfc),source_location(SFile,_W), \+ baseKB:ignore_file_mpreds(SFile),!.
  400
  401% file late late joiners
  402:- if( \+ prolog_load_context(reload,true)).  403:- source_location(File, _)-> during_boot((asserta(baseKB:ignore_file_mpreds(File)))).  404:- doall((module_property(M,file(File)),module_property(M,class(CT)),memberchk(CT,[library,system]),asserta(baseKB:ignore_file_mpreds(File)))).  405%:- doall((source_file(File),asserta(baseKB:ignore_file_mpreds(File)))).
  406%base_kb_dynamic(F,A):- ain(mpred_prop(M,F,A,prologHybrid)),kb_shared(F/A).
  407%:- doall((virtualize_ereq(F,A),base_kb_dynamic(F,A))).
  408:- endif.  409
  410:- discontiguous(baseKB:'$pldoc'/4).  411
  412
  413baseKB:ignore_file_mpreds(File):- atom(File),check_ignore_file_mpreds(File).
  414
  415check_ignore_file_mpreds(File):- baseKB:expect_file_mpreds(File),!,fail.
  416check_ignore_file_mpreds(File):- ( atom_concat(_,'.pfc.pl',File);atom_concat(_,'.plmoo',File);atom_concat(_,'.clif',File);atom_concat(_,'.pfc',File)),!,asserta(baseKB:expect_file_mpreds(File)),fail.
  417check_ignore_file_mpreds(File):- baseKB:expect_file_mpreds(Stem),atom_concat(Stem,_,File),!,asserta(baseKB:expect_file_mpreds(File)),!,fail.
  418check_ignore_file_mpreds(File):- baseKB:ignore_file_mpreds(Stem),atom_concat(Stem,_,File),!,asserta(baseKB:ignore_file_mpreds(File)).
  419%check_ignore_file_mpreds(File):- module_property(M,file(File)),module_property(M,class(library)),asserta(baseKB:ignore_file_mpreds(File)),!.
  420% check_ignore_file_mpreds(File):- baseKB:ignore_file_mpreds(File),!.
  421% check_ignore_file_mpreds(File):- asserta(baseKB:expect_file_mpreds(File)),!,fail.
  422
  423cannot_expand_current_file:- prolog_load_context(module,M),module_property(M,class(library)),!.
  424cannot_expand_current_file:- source_location(File,_)->baseKB:ignore_file_mpreds(File),!.
  425
  426
  427in_dialect_pfc:- is_pfc_file. % \+ current_prolog_flag(dialect_pfc,cwc),!.
  428
  429%is_pfc_module(SM):- clause_b(using_pfc(SM,_, SM, pfc_toplevel)),!.
  430%is_pfc_module(SM):- clause_b(using_pfc(SM,_, SM, pfc_mod)),!,baseKB:mtCanAssert(SM).
  431is_pfc_module(SM):- clause_b(mtHybrid(SM)).
  432
  433% First checks to confirm there is nothing inhibiting
  434must_not_be_pfc_file:- is_pfc_file0, rtrace(is_pfc_file0),trace,!,fail.
  435must_not_be_pfc_file:- !.
  436
  437is_pfc_file:- current_prolog_flag(never_pfc,true),!,must_not_be_pfc_file,!,fail.
  438is_pfc_file:- quietly(is_pfc_file0),!.
  439
  440is_pfc_file0:- source_location(File,_W),!,is_pfc_file(File),!.
  441is_pfc_file0:- prolog_load_context(module, M),is_pfc_module(M),!.
  442%is_pfc_file0:- source_context_module(M),is_pfc_module(M).
  443
  444:- meta_predicate is_pfc_file(:).  445is_pfc_file(M:File):- is_pfc_file(M,File).
  446is_pfc_file(_,File):- atom_concat(_,'.pfc.pl',File);atom_concat(_,'.clif',File);atom_concat(_,'.plmoo',File);atom_concat(_,'.pfc',File),!.
  447is_pfc_file(_,File):- call(call,lmcache:mpred_directive_value(File, language, Lang)),!,(Lang==pfc;Lang==clif;Lang==fwd).
  448is_pfc_file(_,File):- baseKB:ignore_file_mpreds(File),!,fail.
  449is_pfc_file(_,File):- baseKB:expect_file_mpreds(File),!.
  450is_pfc_file(M,Other):- prolog_load_context(source, File),Other\==File,!,is_pfc_file(M,File).
  451%is_pfc_file(M,_):- prolog_load_context(module, SM), SM\==M,!, is_pfc_module(SM).
  452%is_pfc_file(M,_):- is_pfc_module(M).
  453
  454:- fixup_exports.  455
  456sub_atom(F,C):- sub_atom(F,_,_,_,C).
  457
  458only_expand(':-'(I), ':-'(M)):- !,in_dialect_pfc,fully_expand('==>'(I),M),!.
  459only_expand(I,OO):- fail, quietly(must_pfc(I,M)),  
  460  % current_why(S),!,
  461  S= mfl4(VarNameZ,Module, File, Line),source_location(File,Line),prolog_load_context(module,Module),
  462  conjuncts_to_list(M,O), !, %  [I]\=@=O,
  463  make_load_list(O,S,OO).
  464
  465make_load_list([C|O],S,[baseKB:spft(C,S,ax), :- mpred_enqueue_w_mode(S,direct,C)|OO]):- clause_asserted(C),!, make_load_list(O,S,OO).
  466make_load_list([C|O],S,[C, baseKB:spft(C,S,ax), :- mpred_enqueue_w_mode(S,direct,C)|OO]):-  is_loadin(C),!,make_load_list(O,S,OO).
  467make_load_list(_,_,[]):-!.  
  468
  469is_loadin(C):- strip_module(C,M,CC),is_loadin(M,CC).
  470is_loadin(_,CC):- must_pfc_p(CC),!.
  471is_loadin(_,(_:-_)):-!.
  472is_loadin(M,CC):- functor(CC,F,A),show_call(kb_local(M:F/A)),break.
  473
  474
  475must_pfc(IM,_):- is_never_pfc(IM),!,fail.
  476%must_pfc(IM,'==>'(IM)):- (in_dialect_pfc;must_pfc_p(IM)),!.
  477must_pfc(IM,SM:'==>'(IM)):- (in_dialect_pfc;must_pfc_p(IM)),!,source_module(SM),!.
  478
  479must_pfc_exp(IM,MO):- in_dialect_pfc,fully_expand(IM,MO),!.
  480must_pfc_exp(IM,MO):- must_pfc_p(IM),!,fully_expand(IM,MO),!.
  481
  482must_pfc_p('-->'(_,_)):-!,fail.
  483must_pfc_p(':-'(_,(CWC,_))):- atom(CWC),arg(_,v(bwc,fwc,awc,zwc),CWC),!.
  484must_pfc_p(':-'(_,(CWC,_))):- atom(CWC),arg(_,v(cwc),CWC),!,is_pfc_file.
  485must_pfc_p(':-'(Head,_)):- !, must_pfc_p(Head),!.
  486must_pfc_p('==>'(_,_)).
  487must_pfc_p('==>'(_)).
  488must_pfc_p('<==>'(_,_)).
  489must_pfc_p('<=='(_,_)).
  490must_pfc_p('<-'(_,_)).
  491must_pfc_p('<--'(_,_)).
  492must_pfc_p('->'(_,_)).
  493must_pfc_p('~'(_)).
  494must_pfc_p('--->'(_,_)).
  495% must_pfc_p('=>'(_,_)).
  496must_pfc_p(_:P):- !, must_pfc_p(P),!.
  497must_pfc_p(FAB):-functor(FAB,F,A),must_pfc_fa(F,A),!.
  498
  499must_pfc_fa(prologHybrid,_).
  500must_pfc_fa(F,A):- mpred_database_term(F,A,_),!.
  501must_pfc_fa(F,A):- clause_b(mpred_prop(M,F,A,_)),!, \+ clause_b(mpred_prop(M,F,A,prologBuiltin)).
  502must_pfc_fa(F,2):- sub_atom(F,'='),(atom_concat(_,'>',F);atom_concat('<',_,F)).
  503
  504
  505:- module_transparent(base_clause_expansion/2).  506
  507% module prefixed clauses for sure should be non pfc?
  508is_never_pfc(Var):- \+ callable(Var),!.
  509is_never_pfc(_):- prolog_load_context(file,F),\+ prolog_load_context(source,F),atom_concat(_,'.pl',F),\+ atom_concat(_,'pfc.pl',F).
  510is_never_pfc(goal_expansion(_,_,_,_)).
  511is_never_pfc(':-'(_)).
  512is_never_pfc('?-'(_)).
  513is_never_pfc('-->'(_,_)):-!.
  514is_never_pfc(attr_unify_hook(_,_)):-!.
  515
  516% TODO Maybe find a better spot?  see t/sanity_base/hard_mt_04a.pfc
  517is_never_pfc(M:C):- \+ is_never_pfc(C), \+ current_module(M),
  518   is_pfc_file,
  519   fileAssertMt(CMt),
  520   CMt:clause_b(mtHybrid(CMt)),
  521   CMt:ensure_abox(M),
  522   CMt:ain(genlMt(CMt,M)),!,fail.
  523
  524is_never_pfc(':-'(C,_)):- !,is_never_pfc(C).
  525is_never_pfc(M:P):- functor(P,F,A),clause_b(mpred_prop(M,F,A,prologBuiltin)),!.
  526is_never_pfc(_:C):- is_never_pfc(C).
  527
  528
  529% base_clause_expansion(Var,Var):- current_prolog_flag(mpred_te,false),!.
  530base_clause_expansion(Var,Var):-var(Var),!.
  531base_clause_expansion( :- module(W,List), [:- writetln(module(W,List)), :- set_fileAssertMt(W)]):- is_pfc_file,!.
  532base_clause_expansion('?=>'(I), ':-'(O)):- !, sanity(nonvar(I)), fully_expand('==>'(I),O),!. % @TODO NOT NEEDED REALY UNLESS DO mpred_expansion:reexport(library('pfc2.0/mpred_expansion.pl')),
  533base_clause_expansion(:-(I),:-(I)):- !.
  534base_clause_expansion(IM,':-'(ain(==>(IM)))):- \+ compound(IM),(sub_atom(IM,';');sub_atom(IM,'(')),!.
  535% NEXT LINE REDUNDANT base_clause_expansion(IM,IM):- \+ callable(IM),!.
  536base_clause_expansion(NeverPFC, EverPFC):- is_never_pfc(NeverPFC),!,NeverPFC=EverPFC.
  537
  538% base_clause_expansion(In,Out):- only_expand(In,Out),!.
  539base_clause_expansion(IN, ':-'(ain(ASSERT))):- must_pfc(IN,ASSERT).
  540base_clause_expansion(ASSERT, ':-'(ain(ASSERT))):- is_pfc_file.
  541
  542/*
  543
  544
  545% Checks if **should** be doing base_expansion or not      
  546:- module_transparent(base_clause_expansion_fa/4).
  547base_clause_expansion_fa(_,_,F,A):- clause_b(mpred_prop(M,F,A,prologBuiltin)),!,fail.
  548base_clause_expansion_fa(I,O,F,A):- (needs_pfc(F,A) -> true ; base_kb_dynamic(F,A)),
  549  base_clause_expansion('==>'(I),O).
  550
  551:- module_transparent(needs_pfc/2).
  552needs_pfc(F,_):- (clause_b(functorIsMacro(F));clause_b(functorDeclares(F))).
  553needs_pfc(F,A):- base_kb_dynamic(F,A).
  554needs_pfc(F,A):- clause_b(mpred_prop(M,F,_,prologHybrid)), \+ clause_b(mpred_prop(M,F,A,prologBuiltin)).
  555
  556maybe_builtin(M : _ :-_):- atom(M),!.
  557maybe_builtin(M : _ ):- atom(M),!.
  558maybe_builtin(I) :- nonvar(I),get_unnegated_functor(I,F,A),
  559   \+ (clause_b(functorIsMacro(F));clause_b(functorDeclares(F));clause_b(mpred_prop(M,F,A,prologHybrid))),
  560   ain(prologBui sltin(F/A)).
  561
  562*/
  563
  564:- sanity((clause(baseKB:ignore_file_mpreds(_),B),compound(B))).  565
  566:- if(false).  567%:- autoload([verbose(false)]).
  568:- statistics.  569:- endif.  570
  571% :- ain(arity(functorDeclares, 1)).
  572% Load boot base file
  573%:- dynamic(isa/2).
  574
  575%is_lm_mod(M):-atom_concat('logicmoo_i_',_,M).
  576%is_lm_mod(M):-atom_concat('common_logic_',_,M).
  577%is_lm_mod(M):-atom_concat('mpred_',_,M).
  578%is_lm_mod(M):-atom_concat('baseK',_,M).
  579is_lm_mod(M):-atom_concat('mud_',_,M).
  580make_exported(op(X,Y,Z),:-op(X,Y,Z)).
  581make_exported(Pred,:-export(Pred)).
  582
  583term_expansion_UNUSED(:-module(M,List),Pos,ExportList,Pos):- nonvar(Pos),
  584  ((prolog_load_context(file,File),\+ prolog_load_context(source,File));is_lm_mod(M)),
  585   maplist(make_exported,List,ExportList).
  586
  587%:- thread_local t_l:side_effect_ok/0.
  588
  589
  590
  591:- module_transparent(pfc_clause_expansion/2).  592pfc_clause_expansion(I,O):- nonvar(I),I\==end_of_file,
  593  base_clause_expansion(I,M),!,I\=@=M,
  594   ((
  595      maybe_should_rename(M,MO), 
  596      ignore(( \+ pfc_lib:same_expandsion(I,MO), dmsg(pfc_clause_expansion(I)-->MO))),
  597      maybe_directive_to_clauses(MO,O),
  598      ignore(( O\==MO , (dmsg(directive_to_clauses(I)-->O)))))),!.
  599
  600%maybe_directive_to_clauses(:- ain(A),Clauses):- loader_side_effect_capture_only(ain(A),Clauses).
  601%maybe_directive_to_clauses(:- ain(A),Clauses):- loader_side_effect_capture_only(ain(A),Clauses).
  602maybe_directive_to_clauses(O,O):-!.
  603
  604same_expandsion(I,O):-var(I),!,I==O.
  605same_expandsion(I,O):-var(O),!,I==O.
  606same_expandsion(_:I,MO):-!,same_expandsion(I,MO).
  607same_expandsion(MO,_:I):-!,same_expandsion(I,MO).
  608same_expandsion('==>'(I),MO):-!,same_expandsion(I,MO).
  609same_expandsion(I,'==>'(MO)):-!,same_expandsion(I,MO).
  610same_expandsion(I,[MO|_]):-!,same_expandsion(I,MO).
  611same_expandsion(I, (:-ain(MO))):-!,same_expandsion(I,MO).
  612same_expandsion(I, (:-mpred_ain(MO))):-!,same_expandsion(I,MO).
  613same_expandsion(I,O):-I==O.
  614
  615% prolog:message(ignored_weak_import(Into, From:PI))--> { nonvar(Into),Into \== system,dtrace(dmsg(ignored_weak_import(Into, From:PI))),fail}.
  616% prolog:message(Into)--> { nonvar(Into),functor_safe(Into,_F,A),A>1,arg(1,Into,N),\+ number(N),dtrace(wdmsg(Into)),fail}.
  617
  618/*
  619:- multifile(user:clause_expansion/2).
  620user:clause_expansion(I,O):- pfc_clause_expansion(I,O).
  621*/
  622
  623/*
  624:- multifile(clause_expansion/2).
  625clause_expansion(I,O):- pfc_clause_expansion(I,O).
  626*/
  627
  628
  629% term_expansion(I,P1,O,P2):- is_pfc_file,mpred_te(term,system,I,P1,O,P2).
  630
  631module_uses_pfc(SM):- current_predicate(SM:'$uses_pfc_toplevel'/0).
  632
  633:- multifile(pfc_goal_expansion/4).  634:- dynamic(pfc_goal_expansion/4).  635:- module_transparent(pfc_goal_expansion/4).  636pfc_goal_expansion(I,P,O,PO):- 
  637 quietly(( \+ source_location(_,_),
  638     callable(I),          
  639     var(P), % Not a file goal     
  640     \+ current_prolog_flag(xref,true), 
  641     \+ current_prolog_flag(mpred_te,false),
  642     '$current_typein_module'(CM),
  643     prolog_load_context(module,SM),
  644     ((SM \== CM) -> module_uses_pfc(SM); module_uses_pfc(CM)), 
  645     (I \= (CM : call_u(_))), (I \= call_u(_)))),
  646     fully_expand(I,M),
  647     % quietly
  648     ((
  649     O=CM:call_u(M),
  650     PO=P)).
  651
  652
  653saveBaseKB:- tell(baseKB),listing(baseKB:_),told.
  654
  655%baseKB:'==>'(Consq) :- sanity( \+ input_from_file), ain('==>'(Consq)),!.
  656%baseKB:'==>'(Ante,Consq):- sanity( \+ input_from_file), mpred_why_2(Consq,Ante).
  657
  658:- set_prolog_flag(subclause_expansion,false).  659
  660:- fixup_exports.  661
  662
  663:- if(exists_source(library(retry_undefined))).  664
  665:- use_module(library(retry_undefined)).  666:- install_retry_undefined(baseKB,kb_shared).  667
  668:- else.  669
  670:- endif.  671
  672:- multifile(system:clause_expansion/2).  673:- module_transparent(system:clause_expansion/2).  674:- system:import(pfc_clause_expansion/2).  675system:clause_expansion(I,O):- pfc_clause_expansion(I,O).
  676
  677
  678%:- set_prolog_flag(read_attvars,false).
  679:- set_prolog_flag(subclause_expansion,true).  680:- set_prolog_flag(mpred_te,true).  681
  682:- retractall(t_l:disable_px).  683
  684
  685:- multifile(system:goal_expansion/4).  686:- module_transparent(system:goal_expansion/4).  687:- system:import(pfc_goal_expansion/4).  688system:goal_expansion(I,P,O,PO):- pfc_goal_expansion(I,P,O,PO).
  689
  690
  691:- set_prolog_flag(mpred_te,true).  692:- set_prolog_flag(retry_undefined, module).