|
95 | 95 | :- dynamic(metta_compiled_predicate/3). |
96 | 96 | :- multifile(metta_compiled_predicate/3). |
97 | 97 |
|
| 98 | +%setup_mi_me(FnName,LenArgs,_InternalTypeArgs,_InternalTypeResult) :- !. |
| 99 | +setup_mi_me(FnName,LenArgs,InternalTypeArgs,InternalTypeResult) :- |
| 100 | + debug_info(always(setup_mi_me),setup_mi_me(FnName,LenArgs,InternalTypeArgs,InternalTypeResult)), |
| 101 | + must_det_lls(( |
| 102 | + sum_list(LenArgs,LenArgsTotal), |
| 103 | + LenArgsTotalPlus1 is LenArgsTotal+1, |
| 104 | + findall(Atom0, (between(1, LenArgsTotalPlus1, I0) ,Atom0='$VAR'(I0)), AtomList0), |
| 105 | + create_prefixed_name('mc_',LenArgs,FnName,FnNameWPrefix), |
| 106 | + Hc =.. [FnNameWPrefix|AtomList0], |
| 107 | + create_prefixed_name('mi_',LenArgs,FnName,FnNameWMiPrefix), |
| 108 | + Hi =.. [FnNameWMiPrefix|AtomList0], |
| 109 | + create_prefixed_name('me_',LenArgs,FnName,FnNameWMePrefix), |
| 110 | + He =.. [FnNameWMePrefix|AtomList0], |
| 111 | + append(Eval,[RetVal],[FnName|AtomList0]), |
| 112 | + Bi = ci(true,FnName,LenArgsTotal,Eval,RetVal,true,Goal), |
| 113 | + % Bi =.. [ci,true,[],true,Goal], |
| 114 | + compiler_assertz(Hi:-((Goal=Hc),Bi)), |
| 115 | + compiler_assertz(He:-Hc))). |
| 116 | + |
| 117 | +setup_library_call(Source,FnName,LenArgs,MettaTypeArgs,MettaTypeResult,InternalTypeArgs,InternalTypeResult) :- |
| 118 | + (transpiler_predicate_store(_,FnName,LenArgs,_,_,_,_) -> true ; |
| 119 | + compiler_assertz(transpiler_predicate_store(Source,FnName,LenArgs,MettaTypeArgs,MettaTypeResult,InternalTypeArgs,InternalTypeResult))), |
| 120 | + setup_mi_me(FnName,LenArgs,InternalTypeArgs,InternalTypeResult) |
| 121 | + . |
| 122 | + |
98 | 123 |
|
99 | 124 | % ======================================= |
100 | 125 | % TODO move non flybase specific code between here and the compiler |
|
209 | 234 | % N = expr code |
210 | 235 | % C = common code tp be called before both the exec and expr cases |
211 | 236 |
|
| 237 | + |
| 238 | +as_p1_exec(X,X) :- \+ compound(X), !. |
| 239 | +% as_p1_exec(X,Y) :- as_p1_expr(X,S),eval(S,Y). |
212 | 240 | as_p1_exec(ispu(URet),URet) :- !. |
213 | 241 | as_p1_exec(ispuU(URet,UCode),URet) :- !, call(UCode). |
214 | 242 | as_p1_exec(ispeEn(ERet,ECode,_),ERet) :- !, call(ECode). |
|
217 | 245 | as_p1_exec(rtrace(T),TRet) :- !, rtrace(as_p1_exec(T,TRet)). |
218 | 246 | as_p1_exec(call(P1,T),TRet) :- !, call(P1,as_p1_exec(T,TRet)). |
219 | 247 | as_p1_exec(X,Y) :- as_p1_expr(X,S),eval(S,Y). |
| 248 | +%as_p1_exec(X,X) :- !. |
220 | 249 |
|
221 | | - |
| 250 | +as_p1_expr(X,X) :- \+ compound(X), !. |
222 | 251 | as_p1_expr(ispu(URet),URet) :- !. |
223 | 252 | as_p1_expr(ispuU(URet,UCode),URet) :- !, call(UCode). |
224 | 253 | as_p1_expr(ispeEn(_,_,NRet),NRet). |
|
283 | 312 | must_use_compiler(Fn,disable_interp(Fn)):- nb_current(disable_interp,WasDC), member(Fn,WasDC). |
284 | 313 | must_use_compiler(Fn,interp_disabled(Fn)):- use_evaluator(fa(Fn, _), interp, disabled). |
285 | 314 |
|
| 315 | +ci(_,_,_,G):- call(G). |
| 316 | + |
286 | 317 | % Compiler is Disabled for Fn |
287 | 318 | ci(PreInterp,Fn,Len,Eval,RetVal,_PreComp,_Compiled):- fail, |
288 | 319 | once(must_use_interp(Fn,Why,TF)), |
|
413 | 444 | ; |
414 | 445 | % new, insert clause |
415 | 446 | current_compiler_context(CompCtx), % where expected to be stored (builtin,user,etc) |
416 | | - compiler_assertz(transpiler_predicate_store(CompCtx,FnName,LenArgs,todo,todo,FinalLazyArgsAdj,FinalLazyRetAdj)), |
| 447 | + setup_library_call(CompCtx,FnName,LenArgs,todo,todo,FinalLazyArgsAdj,FinalLazyRetAdj), |
417 | 448 | recompile_from_depends(FnName,LenArgs) |
418 | 449 | ). |
419 | 450 |
|
|
451 | 482 | recompile_from_depends(FnName,LenArgs) :- |
452 | 483 | transpiler_debug(2,(format_e("recompile_from_depends ~w/~w\n",[FnName,LenArgs]))), |
453 | 484 | %LenArgs is LenArgsPlus1-1, |
454 | | - %create_mc_name(LenArgs,,FnName,FnNameWPrefix), |
| 485 | + %create_prefixed_name('mc_',LenArgs,,FnName,FnNameWPrefix), |
455 | 486 | %findall(Atom0, (between(1, LenArgsPlus1, I0) ,Atom0='$VAR'(I0)), AtomList0), |
456 | 487 | %H @.. [FnNameWPrefix|AtomList0], |
457 | 488 | %transpiler_debug(2,format_e("Retracting stub: ~q\n",[H]) ; true), |
|
486 | 517 | %format_e("recompile_from_depends_child ~w/~w\n",[Fn,Arity]),flush_output(user_output), |
487 | 518 | ArityP1 is Arity+1, |
488 | 519 | %retract(transpiler_predicate_store(_,Fn,Arity,_,_,_,_)), |
489 | | - create_mc_name(Arity,Fn,FnWPrefix), |
| 520 | + create_prefixed_name('mc_',Arity,Fn,FnWPrefix), |
490 | 521 | abolish(FnWPrefix/ArityP1), |
| 522 | + % create_prefixed_name('mi_',Arity,Fn,FnWMiPrefix), |
| 523 | + % abolish(FnWMiPrefix/ArityP1), |
| 524 | + % create_prefixed_name('me_',Arity,Fn,FnWMePrefix), |
| 525 | + % abolish(FnWMePrefix/ArityP1), |
491 | 526 | % retract(transpiler_stub_created(Fn,Arity)), |
492 | 527 | % create an ordered list of integers to make sure to do them in order |
493 | 528 | findall(ClauseIDt,transpiler_clause_store(Fn,Arity,ClauseIDt,_,_,_,_,_,_),ClauseIdList), |
|
525 | 560 | subst_varnames(HeadIsIn+AsBodyFnIn,HeadIs+AsBodyFn), |
526 | 561 | %leash(-all),trace, |
527 | 562 | get_curried_name_structure(HeadIs,FnName,Args,LenArgs), |
528 | | - create_mc_name(LenArgs,FnName,FnNameWPrefix), |
| 563 | + create_prefixed_name('mc_',LenArgs,FnName,FnNameWPrefix), |
529 | 564 | %ensure_callee_site(Space,FnName,LenArgs), |
530 | 565 | remove_stub(Space,FnName,LenArgs), |
531 | 566 | sum_list(LenArgs,LenArgsTotal), |
|
534 | 569 | % retract any stubs |
535 | 570 |
|
536 | 571 | (transpiler_stub_created(FnName,LenArgs) -> |
537 | | - retract(transpiler_stub_created(FnName,LenArgs)), |
| 572 | + (retract(transpiler_stub_created(FnName,LenArgs)), |
538 | 573 | findall(Atom0, (between(1, LenArgsTotalPlus1, I0) ,Atom0='$VAR'(I0)), AtomList0), |
539 | 574 | H @.. [FnNameWPrefix|AtomList0], |
540 | 575 | transpiler_debug(2,format_e("Retracting stub: ~q\n",[H]) ; true), |
541 | | - retractall(H) |
| 576 | + retractall(H), |
| 577 | + %create_prefixed_name('mi_',LenArgs,FnName,FnNameWMiPrefix), |
| 578 | + %H1 @.. [FnNameWMiPrefix|AtomList0], |
| 579 | + %retractall(H1), |
| 580 | + %create_prefixed_name('me_',LenArgs,FnName,FnNameWMePrefix), |
| 581 | + %H2 @.. [FnNameWMePrefix|AtomList0], |
| 582 | + %retractall(H2), |
| 583 | + true) |
542 | 584 | ; true), |
543 | 585 |
|
544 | 586 | %AsFunction = HeadIs, |
|
570 | 612 | %precompute_typeinfo(HResult,HeadIs,AsBodyFn,Ast,TypeInfo), |
571 | 613 |
|
572 | 614 | %get_property_lazy(FinalLazyRet,FinalLazyOnlyRet), |
573 | | - |
| 615 | + setup_mi_me(FnName,LenArgs,FinalLazyArgsAdj,FinalLazyRetAdj), |
574 | 616 | OldExpr = [defn,HeadIs,AsBodyFn], |
575 | 617 |
|
576 | 618 | combine_transform_and_collect(OldExpr, Assignments, _NewExpr, VarMappings), |
|
599 | 641 | %(var(HResult) -> (Result = HResult, HHead = Head) ; |
600 | 642 | % funct_with_result_is_nth_of_pred(HeadIs,AsFunction, Result, _Nth, Head)), |
601 | 643 |
|
602 | | - HeadAST=[assign,HResult,[fcall(FnName,LenArgs),Args2]], |
| 644 | + HeadAST=[assign,HResult,[hcall(FnName,LenArgs),Args2]], |
603 | 645 | (transpiler_trace(FnName) -> Prefix=[[native(trace)]] ; Prefix=[]), |
604 | 646 | append([Prefix|Code],CodeAppend), |
605 | 647 | append(CodeAppend,FullCode,FullCode2), |
|
1084 | 1126 | ; |
1085 | 1127 | (transpiler_enable_interpreter_calls -> |
1086 | 1128 | % create a stub to call the interpreter |
1087 | | - (create_mc_name(LenArgs,Fn,Fp), |
| 1129 | + (create_prefixed_name('mc_',LenArgs,Fn,Fp), |
1088 | 1130 | (current_predicate(Fp/LenArgs) -> true ; |
1089 | 1131 | LenArgs1 is LenArgs+1, |
1090 | 1132 | findall(Atom0, (between(1, LenArgs1, I0) ,Atom0='$VAR'(I0)), AtomList0), |
|
1171 | 1213 | maplist(lazy_impedance_match, LazyResultParts, EvalArgs, RetResultsParts, ConvertedParts, RetResultsPartsN, ConvertedNParts, RetResults, Converteds), |
1172 | 1214 | append(Converteds,Converteds2), |
1173 | 1215 | %append(RetResults,[RetResult],RetResults2), |
1174 | | - create_mc_name(LenArgs,'',Prefix), |
| 1216 | + % BEER this is where to change the call to another function |
| 1217 | + create_prefixed_name('mc_',LenArgs,'',Prefix), |
1175 | 1218 | invert_curried_structure(Fn,LenArgs,RetResults,RecurriedList), |
1176 | 1219 | append(Converteds2,[[transpiler_apply,Prefix,Fn,RecurriedList,RetResult,RetResultsParts, RetResultsPartsN, LazyResultParts,ConvertedParts, ConvertedNParts]],Converted), |
1177 | 1220 | assign_or_direct_var_only(Converteds2,RetResultN,list(RecurriedList),ConvertedN). |
|
1360 | 1403 | maybe_lazy_list(Caller,F,1,Args00,Args0), |
1361 | 1404 | %label_arg_types(F,1,Args0), |
1362 | 1405 | maplist(ast_to_prolog_aux(Caller,DontStub),Args0,Args1), |
1363 | | - create_mc_name(LenArgs,F,Fp), |
| 1406 | + create_prefixed_name('mi_',LenArgs,F,Fp), % TODO |
| 1407 | + %label_arg_types(F,0,[A|Args1]), |
| 1408 | + %LenArgs1 is LenArgs+1, |
| 1409 | + append(Args1,[A],Args2), |
| 1410 | + R ~.. [f(FIn),Fp|Args2], |
| 1411 | + (Caller=caller(CallerInt,CallerSz),(CallerInt-CallerSz)\=(F-LenArgs),\+ transpiler_depends_on(CallerInt,CallerSz,F,LenArgs) -> |
| 1412 | + compiler_assertz(transpiler_depends_on(CallerInt,CallerSz,F,LenArgs)), |
| 1413 | + transpiler_debug(2,format_e("Asserting: transpiler_depends_on(~q,~q,~q,~q)\n",[CallerInt,CallerSz,F,LenArgs])) |
| 1414 | + ; true) |
| 1415 | + %sum_list(LenArgs,LenArgsTotal), |
| 1416 | + %LenArgsTotalPlus1 is LenArgsTotal+1, |
| 1417 | + %((current_predicate(Fp/LenArgsTotalPlus1);member(F/LenArgs,DontStub)) -> |
| 1418 | + % true |
| 1419 | + %; check_supporting_predicates('&self',F/LenArgs)) |
| 1420 | + %notice_callee(Caller,F/LenArgs) |
| 1421 | + )). |
| 1422 | +ast_to_prolog_aux(Caller,DontStub,[assign,A,[hcall(FIn,LenArgs),ArgsIn]],R) :- (fullvar(A); \+ compound(A)),callable(FIn),!, |
| 1423 | + must_det_lls(( |
| 1424 | + FIn @.. [F|Pre], % allow compound natives |
| 1425 | + append(Pre,ArgsIn,Args00), |
| 1426 | + maybe_lazy_list(Caller,F,1,Args00,Args0), |
| 1427 | + %label_arg_types(F,1,Args0), |
| 1428 | + maplist(ast_to_prolog_aux(Caller,DontStub),Args0,Args1), |
| 1429 | + create_prefixed_name('mc_',LenArgs,F,Fp), |
1364 | 1430 | %label_arg_types(F,0,[A|Args1]), |
1365 | 1431 | %LenArgs1 is LenArgs+1, |
1366 | 1432 | append(Args1,[A],Args2), |
|
1380 | 1446 | must_det_lls(( |
1381 | 1447 | maybe_lazy_list(Caller,F,1,ArgsIn,Args0), |
1382 | 1448 | maplist(ast_to_prolog_aux(Caller,DontStub),Args0,Args1), |
1383 | | - create_mc_name(LenArgs,F,Fp), |
| 1449 | + create_prefixed_name('mc_',LenArgs,F,Fp), |
1384 | 1450 | append(Args1,[A],Args2), |
1385 | 1451 | R0 =..[Fp,XX], |
1386 | 1452 | R1=..[apply_fn,XX,Args2], |
|
1396 | 1462 | %label_arg_types(FIn,1,Args0), |
1397 | 1463 | maplist(ast_to_prolog_aux(Caller,DontStub),Args0,Args1), |
1398 | 1464 | append(LenArgsRest,LenArgs,LenArgsAll), |
1399 | | - create_mc_name(LenArgsAll,FIn,Fp), |
| 1465 | + create_prefixed_name('mc_',LenArgsAll,FIn,Fp), |
1400 | 1466 | %label_arg_types(FIn,0,[A|Args1]), |
1401 | 1467 | %LenArgs1 is LenArgs+1, |
1402 | 1468 | R0 ~.. [xxx(4),Fp|Args1], |
|
1443 | 1509 | % label_arg_types(F,1,Args0), |
1444 | 1510 | % maplist(ast_to_prolog_aux(Caller,DontStub),Args0,Args1), |
1445 | 1511 | % length(Args0,LenArgs), |
1446 | | -% create_mc_name(LenArgs,F,Fp), |
| 1512 | +% create_prefixed_name('mc_',LenArgs,F,Fp), |
1447 | 1513 | % label_arg_types(F,0,[A|Args1]), |
1448 | 1514 | % %LenArgs1 is LenArgs+1, |
1449 | 1515 | % append(Args1,[A],Args2), |
|
1770 | 1836 | correct_assertz(Info,Info). |
1771 | 1837 |
|
1772 | 1838 |
|
1773 | | -is_prolog_code(Info):- strip_module(Info,_,Neck), compound(Neck), compound_name_arity(Neck,F,_), |
1774 | | - (Neck == ':-' ; \+ compiler_data(F/_)),!. |
| 1839 | +is_prolog_rule(Info):- strip_module(Info,_,Neck), compound(Neck), compound_name_arity(Neck,F,_), F == ':-'. |
| 1840 | +is_compiler_data(Info):- strip_module(Info,_,Neck), compound(Neck), compound_name_arity(Neck,F,_), compiler_data(F/_),!. |
1775 | 1841 |
|
1776 | 1842 | compiler_assertz(Info):- is_list(Info),!,maplist(compiler_assertz,Info),fail. |
1777 | 1843 |
|
1778 | 1844 | compiler_assertz(Info):- |
1779 | | - (is_prolog_code(Info)-> debug_info(assertz_code, t(Info)); debug_info(compiler_assertz, Info)),fail. |
| 1845 | + (is_prolog_rule(Info)-> debug_info(assertz_code, t(Info)); |
| 1846 | + (is_compiler_data(Info)-> debug_info(assertz_compiler_data, t(Info)); |
| 1847 | + debug_info(compiler_assertz, Info))),fail. |
1780 | 1848 |
|
1781 | 1849 | compiler_assertz(Info):- (once(correct_assertz(Info,InfoC))),Info\=@=InfoC,!, |
1782 | 1850 | debug_info(compiler_assertz,correct_assertz(ca)), |
|
1809 | 1877 | maybe_write_info(Info):- string(Info),!,writeln(Info). |
1810 | 1878 | maybe_write_info(Info):- \+ compound(Info),!, ppt(Info). |
1811 | 1879 | maybe_write_info(Info):- \+ \+ (no_conflict_numbervars(Info), maybe_write_info0(Info)). |
| 1880 | +maybe_write_info0((:-B)):- compound(B),gensym(top_call_,Sym),maybe_write_info0((Sym:-B)), maybe_write_info0((:- Sym)). |
| 1881 | +maybe_write_info0((:-B)):- into_plnamed((:- time(B)),Info2), !,nl,nl, no_conflict_numbervars(Info2), portray_clause(Info2), nl,nl. |
1812 | 1882 | maybe_write_info0((H:-B)):- into_plnamed((H:-B),Info2), !,nl,nl, no_conflict_numbervars(Info2),ppt(Info2), nl,nl. |
1813 | 1883 | maybe_write_info0(Info):- into_plnamed(Info,Info2), !, writeq(Info2),writeln('.'). |
1814 | 1884 |
|
|
1835 | 1905 | writeln(":- include(library(metta_lang/metta_transpiled_header))."), |
1836 | 1906 | writeln("%:- ensure_loaded(library(metta_lang/metta_interp))."), |
1837 | 1907 | writeln(":- ensure_loaded(library(metta_rt)). % avoids starting the REPL"), |
| 1908 | + writeln(":- setup_library_calls."), |
1838 | 1909 | writeln(":- style_check(-discontiguous)."), |
1839 | 1910 | writeln(":- style_check(-singleton)."), |
1840 | 1911 | nl. |
|
1862 | 1933 |
|
1863 | 1934 | skip_redef_fa(Fn,Arity) :- integer(Arity),!,skip_redef_fa(Fn,[Arity]). |
1864 | 1935 | skip_redef_fa(Fn,LenArgs) :- |
1865 | | - create_mc_name(LenArgs,Fn,FnWPrefix), |
| 1936 | + create_prefixed_name('mc_',LenArgs,Fn,FnWPrefix), |
1866 | 1937 | sum_list(LenArgs,LenArgsTotal), |
1867 | 1938 | LenArgsTotalPlus1 is LenArgsTotal+1, |
1868 | 1939 | functor(Info,FnWPrefix,LenArgsTotalPlus1), |
|
2366 | 2437 | true))))))),!. |
2367 | 2438 |
|
2368 | 2439 | %transpile_prefix(''). |
2369 | | -transpile_impl_prefix('mi_'). |
| 2440 | +transpile_impl_prefix('mi__1_'). |
2370 | 2441 | :- dynamic(is_transpile_impl_prefix/3). |
2371 | 2442 | transpile_impl_prefix(F,Arity,Fn):- is_transpile_impl_prefix(F,Arity,Fn)*->true;(transpile_impl_prefix(Prefix),FNArity is Arity-1,atomic_list_concat([Prefix,FNArity,'__',F],Fn),asserta(is_transpile_impl_prefix(F,Arity,Fn))). |
2372 | 2443 |
|
2373 | | -transpile_call_prefix('mc_'). |
| 2444 | +transpile_call_prefix('mc__1_'). |
2374 | 2445 | :- dynamic(is_transpile_call_prefix/3). |
2375 | 2446 | transpile_call_prefix(F,Arity,Fn):- is_transpile_call_prefix(F,Arity,Fn)*->true;(transpile_call_prefix(Prefix),FNArity is Arity-1,atomic_list_concat([Prefix,FNArity,'__',F],Fn),asserta(is_transpile_call_prefix(F,Arity,Fn))). |
2376 | 2447 |
|
2377 | 2448 |
|
2378 | 2449 | prefix_impl_preds(Prefix,F,A):- prefix_impl_preds_pp(Prefix,F,A). |
2379 | | -prefix_impl_preds('mc_',F,A):- is_transpile_call_prefix(F,A,Fn),current_predicate(Fn/A), \+ prefix_impl_preds_pp(_,F,A). |
2380 | | -prefix_impl_preds('mi_',F,A):- is_transpile_impl_prefix(F,A,Fn),current_predicate(Fn/A), \+ prefix_impl_preds_pp(_,F,A). |
| 2450 | +prefix_impl_preds('mc__1_',F,A):- is_transpile_call_prefix(F,A,Fn),current_predicate(Fn/A), \+ prefix_impl_preds_pp(_,F,A). |
| 2451 | +prefix_impl_preds('mi__1_',F,A):- is_transpile_impl_prefix(F,A,Fn),current_predicate(Fn/A), \+ prefix_impl_preds_pp(_,F,A). |
2381 | 2452 |
|
2382 | | -prefix_impl_preds_pp(Prefix,F,A):- predicate_property('mc_2__:'(_,_,_),file(File)),predicate_property(Preds,file(File)),functor(Preds,Fn,A), |
2383 | | - ((transpile_impl_prefix(Prefix);transpile_call_prefix(Prefix)),atom_list_concat([Prefix,_FNArity,'__',F],Fn)). |
| 2453 | +prefix_impl_preds_pp(Prefix,F,A):- predicate_property('mc__1_2_:'(_,_,_),file(File)),predicate_property(Preds,file(File)),functor(Preds,Fn,A), |
| 2454 | + ((transpile_impl_prefix(Prefix);transpile_call_prefix(Prefix)),atom_list_concat([Prefix,_FNArity,'_',F],Fn)). |
2384 | 2455 |
|
2385 | 2456 | maplist_and_conj(_,A,B):- fullvar(A),!,B=A. |
2386 | 2457 | maplist_and_conj(_,A,B):- \+ compound(A),!,B=A. |
|
2819 | 2890 |
|
2820 | 2891 | check_supporting_predicates(Space,F/A) :- % already exists |
2821 | 2892 | %trace, |
2822 | | - create_mc_name(A,F,Fp), |
| 2893 | + create_prefixed_name('mc_',A,F,Fp), |
2823 | 2894 | with_mutex_maybe(transpiler_mutex_lock, |
2824 | 2895 | (sum_list(A,ATot),ATot1 is ATot+1, |
2825 | 2896 | (current_predicate(Fp/ATot1) -> true ; |
|
3267 | 3338 | H @.. [Fh|Args], |
3268 | 3339 | length(Args,N), |
3269 | 3340 | N1 is N-1, |
3270 | | - atomic_list_concat(['mc_',N1,'__',Fh],FPrefixed), |
| 3341 | + atomic_list_concat(['mc__1_',N1,'_',Fh],FPrefixed), |
3271 | 3342 | H2 @.. [FPrefixed|Args], |
3272 | 3343 | clause_occurs_warning(H2,B,Ref),clause(HH,BB,Ref), |
3273 | 3344 | strip_m(HH,HHH),HHH=@=H2, |
|
0 commit comments