12
13
15:- if(( ( \+ ((current_prolog_flag(logicmoo_include,Call),Call))) )). 16:- module(mpred_stubs_file_module,
17 [
18agenda_rescan_mpred_prop/0,
19assert_mpred_t/1,
20call_for_literal/3,
21call_for_literal_db/3,
22call_for_literal_db0/3,
23call_for_literal_db2/3,
24call_for_literal_ideep_ilc/1,
25call_mpred_body/2,
26call_mpred_body_ilc/2,
27call_provided_mpred_storage_op/3,
28call_rule_db/3,
29call_wdmsg/2,
30call_wdmsg/4,
31constrain_args/1,
32constrain_args/2,
33create_stub_body/2,
34create_stub_body/3,
35cwdl/2,
36erase_mpred_storage_op/1,
37ensure_universal_stub_plus_mt_why/2,
38first_mpred_prop/1,
39get_cc/2,
40hybrid_tPredStubImpl/1,
41is_call_op/1,
42is_mpred_change_op/1,
43is_mpred_op/1,
44is_non_call_op/1,
45is_proc_only/1,
46is_same_clauses/2,
47is_same_clauses/3,
48is_tCol/1,
49last_arg_ground/1,
50last_arg_ground/3,
51make_builtin/1,
52mpred_prop_ordered/2,
53mpred_t_call_op/2,
54mpred_t_mpred_storage_clauses_facts/3,
55mpred_t_storage_op/2,
56mud_call_store_op/2,
57mustIsa/2,
58must_op/2,
59must_same_clauses/2,
60no_rescans/0,
61out_of_mpred_t/1,
62provide_clauses_list/2,
63really_add_mpred_storage_op/1,
64registerCycPredMtWhy/1,
65registerCycPredMtWhy_3/3,
66registerCycPredMtWhy_3/4,
67renumbervarZ/2,
68tf_result/2,
69test_call_cut/0,
70wff_check_failed/3,
71wff_check_mpred_t_throw/1,
72mpred_stubs_file/0
73 ]). 74
76
77:- endif. 78
83
84
85:- meta_predicate
86
88call_mpred_body(*,0),
90call_mpred_body_ilc(*,0),
92cwdl(0,+),
94must_op(*,0),
96tf_result(0, -),
97registerCycPredMtWhy(0),
98call_provided_mpred_storage_op(*,0,*). 99
100:- module_transparent
101agenda_rescan_mpred_prop/0,
102assert_mpred_t/1,
103call_for_literal/3,
104call_for_literal_db/3,
105call_for_literal_db0/3,
106call_for_literal_db2/3,
107call_for_literal_ideep_ilc/1,
108call_mpred_body/2,
109call_mpred_body_ilc/2,
110call_provided_mpred_storage_op/3,
111call_rule_db/3,
112call_wdmsg/2,
113call_wdmsg/4,
114constrain_args/1,
115constrain_args/2,
116create_stub_body/2,
117create_stub_body/3,
118cwdl/2,
119erase_mpred_storage_op/1,
120first_mpred_prop/1,
121get_cc/2,
122hybrid_tPredStubImpl/1,
123is_call_op/1,
124is_mpred_change_op/1,
125is_mpred_op/1,
126is_non_call_op/1,
127is_proc_only/1,
128is_same_clauses/2,
129is_same_clauses/3,
130is_tCol/1,
131last_arg_ground/1,
132last_arg_ground/3,
133make_builtin/1,
134mpred_prop_ordered/2,
135mpred_t_call_op/2,
136baseKB:mpred_provide_storage_op/2,
137mpred_t_mpred_storage_clauses_facts/3,
138mpred_t_storage_op/2,
139mud_call_store_op/2,
140mustIsa/2,
141must_op/2,
142must_same_clauses/2,
143no_rescans/0,
144out_of_mpred_t/1,
145provide_clauses_list/2,
146really_add_mpred_storage_op/1,
147registerCycPredMtWhy/1,
148registerCycPredMtWhy_3/3,
149registerCycPredMtWhy_3/4,
150renumbervarZ/2,
151test_call_cut/0,
152wff_check_failed/3,
153wff_check_mpred_t_throw/1. 154
155
156:- set_how_virtualize_file(bodies). 157
164hybrid_tPredStubImpl(prologHybrid).
165hybrid_tPredStubImpl(prologPTTP).
166hybrid_tPredStubImpl(prologDynamic).
167hybrid_tPredStubImpl(prologBuiltin).
168hybrid_tPredStubImpl(prologKIF).
169hybrid_tPredStubImpl(prologEquality).
170
171
172
179make_builtin(P):-
180 get_arity(P,F,A),
181 show_failure(why,(atom(F),integer(A))),
182 functor(B,F,A),
183 (predicate_property(B,built_in) -> true ;
184 (locally(set_prolog_flag(access_level,system),lock_predicate(F/A)),
185 check_context_module,
186 ain(mpred_prop(F,A,prologBuiltin)),ain(arity(F,A)))).
187
188
245
249
250
253
255
257
258
259
266create_stub_body(Head,Stub):-create_stub_body(call(conjecture),Head,Stub).
268
275create_stub_body(call(conjecture),Head,Stub):- Stub = (call_provided_mpred_storage_op(call(conjecture),Head,Then),
276 ((Then=(!,Whatnot))->
277 (!,Whatnot);
278 Then)).
279
280
287erase_mpred_storage_op(Head):-
288 create_stub_body(_,Head,Stub),
289 doall(retract((Head:- Stub))),
290 foreach(clause(Head,Stub,Ref),erase_safe(clause(Head,Stub,Ref),Ref)).
291
292
299really_add_mpred_storage_op(Head):-
300 create_stub_body(call(conjecture),Head,Stub),
301 asserta_if_new((Head:- Stub)).
302
303
304
311renumbervarZ((H:-B),GGG):-is_true(B),!,copy_term(H,GG),unnumbervars(GG,GGG),numbervars(GGG,0,_).
312renumbervarZ(H,GGG):-copy_term(H,GG),unnumbervars(GG,GGG),numbervars(GGG,0,_).
313
320is_same_clauses(Head,NEWHBLISTN,HBLISTN):-
321 must_maplist(renumbervarZ,HBLISTN,HBLIST),
322 must_maplist(renumbervarZ,NEWHBLISTN,NEWHBLIST),
323 sort(NEWHBLIST,NEWHBLISTS),
324 sort(HBLIST,HBLISTS),
325 length(NEWHBLIST,LN),
326 length(HBLIST,LO),
327 list_difference_eq(HBLIST,NEWHBLIST,MISSING),length(MISSING,LM),
328 list_difference_eq(NEWHBLIST,HBLIST,EXTRA),length(EXTRA,LE),
329 (NEWHBLIST=@=HBLIST -> true ;((NEWHBLISTS=@=HBLISTS;LM=0) -> wdmsg(trace_or_throw(must_same_clauses(Head,[extra(LE)|EXTRA],[missing(LM)|MISSING]))) ;
330 ((wdmsg((trace_or_throw(must_same_clauses(Head,[LO|HBLIST],[LN|NEWHBLIST],[extra(LE)|EXTRA],[missing(LM)|MISSING])))),!,fail)))).
331
332
339must_same_clauses(Head,HBLISTN):-
340 provide_clauses_list(Head,NEWHBLISTN),
341 must(is_same_clauses(Head,NEWHBLISTN,HBLISTN)).
342
343
350is_same_clauses(Head,HBLISTN):-
351 provide_clauses_list(Head,NEWHBLISTN),
352 must(is_same_clauses(Head,NEWHBLISTN,HBLISTN)).
353
368
369
371
378is_tCol(V):-is_ftVar(V),!,fail.
379is_tCol(tCol).
380is_tCol(F):- local_q_mpred_isa(F,1,tCol);a(tCol,F);a(F,_).
381
382
383local_q_mpred_isa(F,A,C):- call_u(mpred_prop(F,A,C)).
390is_proc_only(V):-is_ftVar(V),!,fail.
391is_proc_only(F):- functor(P,F,1),predicate_property(P,_), \+ tCol(F).
392
394
401is_call_op(Var):-var(Var),!,trace_or_throw(var_is_call_op(Var)).
402is_call_op(call(_)):-!.
403is_call_op(query(_,_)):-!.
404is_call_op(call).
405
406
413is_non_call_op(Op):-is_mpred_op(Op),not(is_call_op(Op)).
414
416
423is_mpred_change_op(change(_,_)).
424
426
433is_mpred_op(Op):-is_mpred_change_op(Op).
434is_mpred_op(call(_)).
435is_mpred_op(query(_,_)).
436is_mpred_op(clauses(_)).
437
439:- was_export(last_arg_ground/1). 440
447last_arg_ground(HEAD):-compound(HEAD),functor(HEAD,F,A),last_arg_ground(F, A, HEAD),!.
448
455last_arg_ground(mud_test,_,_).
456last_arg_ground(_,A,_):-A>2,!.
457last_arg_ground(_,A,HEAD):-arg(A,HEAD,Arg),!,ground(Arg).
458
459
460
467call_provided_mpred_storage_op(call(_),H,true):-was_isa(H,I,C),!,isa_asserted(I,C).
468call_provided_mpred_storage_op(Op,H,true):-!,no_repeats(loop_check(may_storage_op(Op,H),clause_u(H))).
469
470
471
478test_call_cut:- X=!,dmsg(testing_call_cut),X.
479test_call_cut:- throw(test_failed(testing_call_cut)).
481
485
486
487
494must_op(Op, H ):- (var(Op);var(H)),!,trace_or_throw(var_database_op0(Op, H )).
495must_op(Op, H ):- once(fully_expand(Op,H,HH)),H\=@=HH,!,must_op(Op, HH).
496must_op(change(assert,_),Call):-!,must(Call),!.
497must_op(change( retract,_),Call):-!,must(Call),!.
498must_op(clauses(_),Call):-!,loop_check(on_x_debug(Call)).
499must_op(call(_),Call):-!,loop_check(on_x_debug(Call)).
500must_op(_,Call):-!,loop_check(on_x_debug(Call)).
501
502
509call_wdmsg(P,DB):- t_l:noDBaseMODs(_),!,wdmsg(error(noDBaseMODs(P,DB))).
510call_wdmsg(P,DB):- get_functor(DB,F,A), call_wdmsg(P,DB,F,A).
511
512
519call_wdmsg(P,DB,t,_A):-!, append_term(P,DB,CALL),dmsg((CALL)),call_u(CALL).
520call_wdmsg(P,MP,F,A):- local_q_mpred_isa(F,A,prologHybrid),must(A>1),into_functor_form(t,MP,DB),!, append_term(P,DB,CALL),dmsg(info(CALL)),!,call_u(CALL).
521call_wdmsg(P,MP,F,A):-
522 (\+ local_q_mpred_isa(F,A,prologDynamic)),
523 (\+ local_q_mpred_isa(F,A,prologBuiltin)),
524 into_functor_form(t,MP,DB),!,
525 append_term(P,DB,CALL),dmsg(info(CALL)),!,call_u(CALL).
526call_wdmsg(P,DB,F,A):- append_term(P,DB,CALL),dmsg(info(CALL)),must(local_q_mpred_isa(F,A,prologDynamic);local_q_mpred_isa(F,A,prologBuiltin)),!,call_u(CALL).
528
529
533
535
537
544no_rescans.
545
546:- was_export(agenda_rescan_mpred_prop/0). 547
548
555agenda_rescan_mpred_prop:- loop_check(rescan_mpred_prop_ilc,true).
556
558:- use_module(prolog_statistics:library(statistics)). 564rescan_mpred_prop_ilc:-no_rescans,!.
565rescan_mpred_prop_ilc:-rescan_duplicated_facts(user,local_q_mpred_isa(_,_)),fail.
566rescan_mpred_prop_ilc:- prolog_statistics:time(forall(find_and_call(mpred_prop_ordered(Pred,Prop)),ain(local_q_mpred_isa(Pred,Prop)))),fail.
567rescan_mpred_prop_ilc.
568
569
576first_mpred_prop(meta_argtypes(_)).
577
578
585mpred_prop_ordered(Pred,Prop):-first_mpred_prop(Prop),local_q_mpred_isa(Pred,_,Prop),\+ (local_q_mpred_isa(Pred,_,prologDynamic)).
586mpred_prop_ordered(Pred,Prop):-local_q_mpred_isa(Pred,_,Prop),not(first_mpred_prop(Prop)),not(local_q_mpred_isa(Pred,_,prologDynamic)).
587
588
592
599provide_clauses_list(Head,HBLISTO):- get_pifunctor(Head,PHead),
600 findall((PHead :- B),
601 no_repeats([PHead:B],((call_no_cuts(baseKB:mpred_provide_storage_clauses(PHead,B,Proof)),is_source_proof(Proof)))),
602 HBLIST),
603 create_stub_body(PHead,Stub),
604 delete(HBLIST,Stub,HBLISTO),!.
605
606
607
614get_cc(PI,NC):-provide_clauses_list(PI,HBLISTO),length(HBLISTO,NC).
615
616
620
628baseKB:mpred_provide_setup(Op,HeadIn,StubType,OUT):- StubType \== prologDynamic,
629 sanity(var(OUT)),
630 must((StubType = prologHybrid)),
631 OUT=defined(baseKB:mpred_provide_setup(Op,HeadIn,StubType)).
637tf_result(Call,TF):-(Call->TF=true;TF=fail).
638
645assert_mpred_t((G:-B)):-is_true(B),!,must(assert_mpred_t(G)).
646assert_mpred_t(DB):-once(fully_expand(change(assert,ain),DB,MP)),DB\=@=MP,!,must(assert_mpred_t(MP)).
647assert_mpred_t((G1,G2)):-!,assert_mpred_t(G1),assert_mpred_t(G2).
648assert_mpred_t(G):-add_from_file(G).
649
653
654
662baseKB:hook_mpred_listing(Match):- fail,
663 ((
664 dif:dif(Proof,prologRef(_)),
665 no_repeats([H,B],((baseKB:mpred_provide_storage_clauses(H,B,Proof)),
666 Proof\=prologRef(_))),term_matches_hb(Match,H,B),portray_hb(Proof:H,B))),fail.
667
668
669
677:- multifile(baseKB:mpred_provide_storage_clauses/3). 678baseKB:mpred_provide_storage_clauses(H,B,Proof):-mpred_t_mpred_storage_clauses_facts(H,B,Proof).
679
680
687mpred_t_mpred_storage_clauses_facts(H,true,t(H)):-is_list(H),!,length(H,A),A>2,loop_check(call_u(t(H))).
688mpred_t_mpred_storage_clauses_facts(H,true,t(H)):-compound(H),!,current_predicate(into_plist_arities/4),functor(H,_,A),A>1,loop_check(call_u(t(H))).
690
697
698
699
707baseKB:mpred_provide_storage_op(Op,HB):-
708 must(baseKB:is_mpred_op(Op)),
709 (quietly(baseKB:remodulize(Op,HB,HeadBody)),get_functor(HeadBody,F),
710 once(F==t; baseKB:a(prologHybrid,F)),
711 locally_tl(already_in_file_term_expansion,mpred_t_storage_op(Op,HeadBody))).
712
717
724mpred_t_storage_op(Op,(Head:-Body)):- is_true(Body),!,mpred_t_storage_op(Op,Head).
725
726
727mpred_t_storage_op(Op,H):- baseKB:pfcManageHybrids,!,baseKB:mpred_provide_storage_op(Op,H).
728
729mpred_t_storage_op(Op,(:-(Body))):-!,loop_check(mpred_op(Op,(:-(Body))),true),!.
730
732mpred_t_storage_op(_,isa(_,_)):- !,fail. 733mpred_t_storage_op(Op,X):- was_isa(X,I,C),!,mpred_op(Op,isa(I,C)).
734
736mpred_t_storage_op(Op,HeadBodyI):- quietly(((expand_term(HeadBodyI,HeadBodyM)),HeadBodyI\=@=HeadBodyM)),!,mpred_t_storage_op(Op,HeadBodyM).
737mpred_t_storage_op(Op,X):- not(is_non_call_op(Op)),!,mpred_t_call_op(Op,X).
738
740mpred_t_storage_op(Op,(Head:-Body)):-
741 reduce_mpred_op(Op,Op2),
742 special_wrapper_body(Body,direct_to_prolog),!,
743 wdmsg(direct_to_prolog_special_wrapper_body(Op2,Head,Body)),
744 (mud_call_store_op(Op2,(Head:-Body))).
745
747mpred_t_storage_op(Op,(Head:-Body)):- \+ if_defined(use_kif(Head,Body)),
748 wdmsg(saved_clause_in_hybridRule(Op,Head,Body)),!,
749 (mud_call_store_op(Op,ruleBackward(Head,Body))).
750
752mpred_t_storage_op(Op,(Head:-Body)):-
753 if_defined(use_kif(Head,Body)),
754 reduce_mpred_op(Op,Op2),
755 CALL0 = (call(Op2,ruleBackward(Head,Body))), 756 must(((CALL0,if_defined(mpred_t_tell_kif(Op2,(Head:-Body)))))),!.
757
759mpred_t_storage_op(Op,RULE):- if_defined(is_kif_clause(RULE)),!,
760 reduce_mpred_op(Op,Op2),
761 if_defined(mpred_t_tell_kif(Op2,RULE)),!.
762
765
767mpred_t_storage_op(Op,HeadBody):-
768 into_functor_form(t,HeadBody,DB),
769 770 must((mud_call_store_op(Op,DB),sanity(show_call(why,DB)))),!.
771
772
779mud_call_store_op(Op,(H:-B)):- is_true(B),!,mud_call_store_op(Op,H).
780mud_call_store_op(Op,t('$si$':'$was_imported_kb_content$', _, OPRAND)):-!,loop_check(mpred_op(Op,OPRAND),true).
781mud_call_store_op(Op,OPRAND):- show_success(why,wff_check_failed(Op,OPRAND,_WHY)),!.
782mud_call_store_op(Op,OPRAND):- reduce_mpred_op(Op,Op2),show_call(why,call(Op2,OPRAND)).
783
784
791wff_check_failed(_,DB,WHY):- DB = t('$si$':'$was_imported_kb_content$', WHY, _Assert).
792
799wff_check_mpred_t_throw(DB):- wff_check_failed(_,DB,WHY),trace_or_throw(crazy_mpred_t_was_imported_kb_content(WHY,DB)).
800wff_check_mpred_t_throw(_).
801
806
813mpred_t_call_op(_,isa(_,_)):- !,fail.
814mpred_t_call_op(Op,X):- was_isa(X,I,C),!,mpred_op(Op,isa(I,C)).
815
817mpred_t_call_op(_,FACT):- get_functor(FACT, F,A), !,
818 lc_tcall(call_for_literal(F,A,FACT)),!.
819
820
821
831:- meta_predicate call_for_literal(?,1,*). 832call_for_literal(_,_,HEAD):- if_defined(use_kif(HEAD,true)),!,call_u(kif_ask(HEAD)).
833call_for_literal(_,_,HEAD):- call_u(use_ideep_swi),!, call_for_literal_ideep_ilc(HEAD),!,loop_check_term(cwdl(CALL,7),HEAD,(CALL)).
834call_for_literal(F,A,HEAD):- call_for_literal_db(F,A,HEAD).
835
836
843:- meta_predicate call_for_literal_db(?,1,*). 844call_for_literal_db(F,A,HEAD):- P=F, HEAD=..[P|ARGS],
845 ((lmcache:after_mpred_load)->kb_shared(F/A);true),
846 constrain_args(P,ARGS),call_for_literal_db0(F,A,HEAD),constrain_args(P,ARGS).
847
848
849
856cwdl(CALL,DEEP7):- call_with_depth_limit(CALL,DEEP7,Result),
857 ( Result == depth_limit_exceeded -> (!,fail) ; true).
858
859
866call_for_literal_ideep_ilc(HEAD):- get_functor(HEAD,F,A),call_for_literal_db(F,A,HEAD).
867
868
875call_for_literal_db0(F,A,HEAD):-no_repeats(HEAD,call_for_literal_db2(F,A,HEAD)).
876
877:- style_check(-singleton). 878
885call_for_literal_db2(_,_,HEAD):- clause_u(HEAD).
886call_for_literal_db2(F,_, _):- (a(completelyAssertedCollection,F);a(completeExtentAsserted,F)),!,fail.
887call_for_literal_db2(F,A,HEAD):- loop_check(call_rule_db(F,A,HEAD)).
888call_for_literal_db2(F,A,HEAD):- \+ call_u(use_kif(HEAD,true)),HEAD=..[P1,A1,A2],dif(P2,P1),loop_check_term(clause_u(genlPreds(P2,P1)),gp(P1),fail),
889 call_u(t(P2,A1,A2)).
890
891
892
899out_of_mpred_t(HEAD):-clause_safe(HEAD,true)*->true;show_success(why,call_u(fact_always_true(HEAD))).
900
901
902
909call_rule_db(F,_A,_HEAD):- a(completelyAssertedCollection,F),!,fail.
910call_rule_db(_F,_A,HEAD):- if_defined(use_kif(HEAD,_)),!,call_u(kif_ask(HEAD)).
911call_rule_db(_F,_A,HEAD):- ruleBackward(HEAD,BODY,_Why),call_mpred_body(HEAD,BODY).
912
913:- style_check(+singleton). 914:- style_check(-singleton). 915
916
923call_mpred_body(_,true):-!.
924call_mpred_body(HEAD,and(A,B)):- !,call_mpred_body(HEAD,A),!,call_mpred_body(HEAD,B).
925call_mpred_body(HEAD,(A,B)):- !,call_mpred_body(HEAD,A),call_mpred_body(HEAD,B).
926call_mpred_body(HEAD,BODY):- no_repeats(loop_check_term(call_mpred_body_ilc(HEAD,BODY),body_of(HEAD),(nop(dmsg(failure(call_mpred_body_ilc(HEAD,BODY)))),!,fail))).
927
928
935call_mpred_body_ilc(_HEAD,BODY):- on_x_debug(BODY).
936
937
938
945mustIsa(I,C):-nonvar(I),!,call_u(isa(I,C)),!.
946mustIsa(I,C):-when(nonvar(I),call_u(isa(I,C))).
947
948
955constrain_args(_):-!.
956constrain_args(HEAD):-HEAD=..[P|ARGS],constrain_args(P,ARGS).
957
958
965constrain_args(_,_):-!.
966constrain_args(_P,[AR,GS]):-!,dif(AR,GS).
967constrain_args(_,[_P,AR,GS]):-!,dif(AR,GS).
968constrain_args(A,B):- if_defined(constrain_args_pttp(A,B),fail).
969
970
972
1023
1031
1032
1034
1061
1062:- was_export(registerCycPredMtWhy/1). 1063
1064
1065
1072registerCycPredMtWhy_3(_CM,M,PI,F/A2):-
1073 registerCycPredMtWhy_3(M,PI,F/A2).
1074
1075
1082registerCycPredMtWhy_3(M,_PI,F/A2):-
1083 ignore((A2==3,assertz_if_new(is_never_type(F)))),
1084 A is A2 - 2, 1085 ain(mpred_prop(F,A,cycPlus2)).
1086
1087
1088
1095registerCycPredMtWhy(P):-!,baseKB:with_pi(P,baseKB:registerCycPredMtWhy_3).
1096
1097
1098
1105ensure_universal_stub_plus(F,AMinus2):-
1106 kb_shared(F/AMinus2).
1108
1109
1116ensure_universal_stub_plus_mt_why(F,A2):- once(( AMinus2 is A2 -2, ensure_universal_stub_plus(F,AMinus2))),fail.
1117
1119
1120ensure_universal_stub_plus_mt_why(F,A2):-
1121 export(F/A2),
1122 functor(HEAD,F,A2),
1123 HEAD=..[F|ARGS],
1124 append(ARGSMinus2,[_,_],ARGS),
1125 HEADMinus2=..[F|ARGSMinus2],
1126 AMinus2 is A2 -2,
1127 assert_if_new((HEAD:-HEADMinus2)),!,
1128 1129 defaultAssertMt(M),
1130 kb_shared(M:F/AMinus2).
1131
1132
1133:- fixup_exports. 1134
1135mpred_stubs_file