|
74 | 74 | :- ensure_loaded(metta_space). |
75 | 75 | :- ensure_loaded(metta_compiler_print). |
76 | 76 | :- dynamic(transpiler_clause_store/9). |
| 77 | +:- multifile(transpiler_predicate_store/7). |
77 | 78 | :- dynamic(transpiler_predicate_store/7). |
| 79 | +:- dynamic(transpiler_predicate_nary_store/9). |
| 80 | +:- discontiguous transpiler_predicate_nary_store/9. |
78 | 81 | :- discontiguous(compile_flow_control/8). |
| 82 | +:- multifile(compile_flow_control/8). |
79 | 83 | :- ensure_loaded(metta_compiler_lib). |
80 | 84 | :- ensure_loaded(metta_compiler_lib_stdlib). |
81 | 85 |
|
|
212 | 216 | as_p1_exec(ispeEnNC(ERet,ECode,_,_,CCode),ERet) :- !, call(CCode),call(ECode). |
213 | 217 | as_p1_exec(X,X) :- !. |
214 | 218 |
|
| 219 | + |
215 | 220 | as_p1_expr(ispu(URet),URet) :- !. |
216 | 221 | as_p1_expr(ispuU(URet,UCode),URet) :- !, call(UCode). |
217 | 222 | as_p1_expr(ispeEn(_,_,NRet),NRet). |
|
240 | 245 | partial_combine_lists(L1,L2,Lcomb,L1a,L2a). |
241 | 246 | partial_combine_lists(L1,L2,[],L1,L2). |
242 | 247 |
|
| 248 | +is_proper_arg(O):- compound(O),iz_conz(O), \+ is_list(O),!,bt, trace. |
| 249 | +is_proper_arg(_). |
| 250 | +% This hook is called when an attributed var is unified |
| 251 | +proper_list_attr:attr_unify_hook(_, Value) :- \+ compound(Value),!. |
| 252 | +proper_list_attr:attr_unify_hook(_, Value) :- is_list(Value),!. |
| 253 | +proper_list_attr:attr_unify_hook(_, Value) :- iz_conz(Value),!,trace. |
| 254 | +proper_list_attr:attr_unify_hook(_, _Value). |
| 255 | +% Attach the attribute if not already present and not already a proper list |
| 256 | +ensure_proper_list_var(Var) :- var(Var),!, put_attr(Var, proper_list_attr, is_proper_arg). |
| 257 | +ensure_proper_list_var(Var) :- is_proper_arg(Var),!. |
| 258 | + |
| 259 | + |
| 260 | +eval_at(_Fn,Where):- nb_current('eval_in_only',NonNil),NonNil\==[],!,Where=NonNil. |
| 261 | +eval_at( Fn,Where):- use_evaluator(fa(Fn, _), Only, only),!,Only=Where. |
| 262 | +eval_at(_Fn,Where):- option_value(compile,false),!,Where=interp. |
| 263 | +eval_at( Fn,Where):- use_evaluator(fa(Fn, _), Where, enabled),!. |
| 264 | +eval_at( Fn,Where):- nb_current(disable_compiler,WasDC),member(Fn,WasDC), Where==compiler,!,fail. |
| 265 | +eval_at( Fn,Where):- nb_current(disable_interp,WasDC),member(Fn,WasDC), Where==interp,!,fail. |
| 266 | +eval_at(_Fn,Where):- option_value(compile,full),!,Where=compiler. |
| 267 | +eval_at(_Fn, _Any):- !. |
| 268 | + |
| 269 | +must_use_interp(Fn, only_interp(Fn), true):- use_evaluator(fa(Fn, _), interp, only). |
| 270 | +must_use_interp(_ , eval_in_only(compiler), never):- nb_current('eval_in_only',compiler). |
| 271 | +must_use_interp(_ , eval_in_only(interp), true):- nb_current('eval_in_only',interp). |
| 272 | +must_use_interp(Fn, disable_compiler(Fn), true):- nb_current(disable_compiler,WasDC), member(Fn,WasDC). |
| 273 | +must_use_interp(Fn,compiler_disabled(Fn), true):- use_evaluator(fa(Fn, _), compiler, disabled). |
| 274 | +must_use_interp(Fn,unknown(Fn), unknown). |
| 275 | + |
| 276 | +must_use_compiler(_ ,eval_in_only(compiler)):- nb_current('eval_in_only',compiler). |
| 277 | +must_use_compiler(_ ,eval_in_only(interp)):- nb_current('eval_in_only',interp), fail. |
| 278 | +must_use_compiler(Fn,only_compiler(Fn)):- use_evaluator(fa(Fn, _), compiler, only). |
| 279 | +must_use_compiler(Fn,disable_interp(Fn)):- nb_current(disable_interp,WasDC), member(Fn,WasDC). |
| 280 | +must_use_compiler(Fn,interp_disabled(Fn)):- use_evaluator(fa(Fn, _), interp, disabled). |
| 281 | + |
| 282 | +% Compiler is Disabled for Fn |
| 283 | +ci(PreInterp,Fn,Len,Eval,RetVal,_PreComp,_Compiled):- fail, |
| 284 | + once(must_use_interp(Fn,Why,TF)), |
| 285 | + TF \== unknown, TF \== never, |
| 286 | + debug_info(must_use_interp,why(Why,Fn=TF)), |
| 287 | + TF == true, !, |
| 288 | + |
| 289 | + % \+ nb_current(disable_interp,WasDI),member(Fn,WasDI), |
| 290 | + call(PreInterp), |
| 291 | + maplist(lazy_eval_to_src,Eval,Src), |
| 292 | + if_t(Eval\=@=Src, |
| 293 | + debug_info(lazy_eval_to_src,ci(Fn,Len,Eval,RetVal))), |
| 294 | + %eval_fn_disable(Fn,disable_compiler,interp,((call(PreComp),call(Compiled)))), |
| 295 | + debug_info(Why,eval_args(Src,RetVal)),!, |
| 296 | + eval_args(Src,RetVal). |
| 297 | + |
| 298 | +ci(_PreInterp,Fn,Len,_Eval,_RetVal,PreComp,Compiled):- |
| 299 | + %(nb_current(disable_interp,WasDI),member(Fn,WasDI); |
| 300 | + %\+ nb_current(disable_compiler,WasDC),member(Fn,WasDC)),!, |
| 301 | + %\+ \+ (maplist(lazy_eval_to_src,Eval,Src), |
| 302 | + % if_t(Eval\=@=Src, debug_info(lazy_eval_to_src,ci(Fn,Len,Eval,RetVal)))), |
| 303 | + if_t(false,debug_info(call_in_only_compiler,ci(Fn,Len,Compiled))),!, |
| 304 | + % eval_fn_disable(Fn,disable_compiler,eval_args(EvalM,Ret)) |
| 305 | + %show_eval_into_src(PreInterp,Eval,_EvalM), |
| 306 | + (call(PreComp),call(Compiled)), |
| 307 | + %eval_fn_disable(Fn,disable_compiler,(call(PreComp),call(Compiled))), |
| 308 | + true. |
| 309 | + |
| 310 | +eval_fn_disable(Fn,DisableCompiler,Call):- |
| 311 | + (nb_current(DisableCompiler,Was)->true;Was=[]), |
| 312 | + (New = [Fn|Was]), |
| 313 | + Setup = nb_setval(DisableCompiler,New), |
| 314 | + Restore = nb_setval(DisableCompiler,Was), |
| 315 | + redo_call_cleanup(Setup,Call,Restore). |
| 316 | + |
| 317 | + |
| 318 | +lazy_eval_to_src(A,O):- nonvar(O),trace,A=O. |
| 319 | +%lazy_eval_to_src(A,O):- var(A),!,O=A,ensure_proper_list_var(A). |
| 320 | +lazy_eval_to_src(A,O):- \+ compound(A),!,O=A. |
| 321 | +%lazy_eval_to_src(A,P):- is_list(A), maplist(lazy_eval_to_src,A,P),!. |
| 322 | +lazy_eval_to_src(A,P):- [H|T] = A, lazy_eval_to_src(H,HH),lazy_eval_to_src(T,TT),!,P= [HH|TT]. |
| 323 | +lazy_eval_to_src(A,P):- as_p1_expr(A,P),!. |
| 324 | + |
| 325 | +delistify(L,D):- is_list(L),L=[D],!. |
| 326 | +delistify(L,L). |
| 327 | + |
| 328 | +create_prefixed_name(Prefix,LenArgs,FnName,String) :- |
| 329 | + %(sub_string(FnName, 0, _, _, "f") -> break ; true), |
| 330 | + length(LenArgs,L), |
| 331 | + append([Prefix,L|LenArgs],[FnName],Parts), |
| 332 | + atomic_list_concat(Parts,'_',String). |
| 333 | + |
243 | 334 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
244 | 335 | %%%%%%%%%%%%%%%%% Evaluation (!) |
245 | 336 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
352 | 443 | invert_curried_structure(F,LenArgs,ArgsFirst,Result). |
353 | 444 |
|
354 | 445 |
|
355 | | -recompile_from_depends(FnName,LenArgs) :- skip_redef_fa(FnName,LenArgs),!,debug_info(recompile_from_depends,skip_redef_fa(FnName,LenArgs)),!. |
| 446 | +recompile_from_depends(FnName,LenArgs) :- skip_redef_fa(FnName,LenArgs),!,debug_info(recompile_code_from_depends,skip_redef_fa(FnName,LenArgs)),!. |
356 | 447 | recompile_from_depends(FnName,LenArgs) :- |
357 | 448 | transpiler_debug(2,(format_e("recompile_from_depends ~w/~w\n",[FnName,LenArgs]))), |
358 | 449 | %LenArgs is LenArgsPlus1-1, |
|
768 | 859 | f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, T, Converted, ConvertedN) :- compound(T),T=exec(X),!, |
769 | 860 | f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, [eval,X], Converted, ConvertedN). |
770 | 861 |
|
| 862 | +f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, Convert, Converted, ConvertedN) :- fail, |
| 863 | + atom(Convert), nb_bound(Convert,_),!, % TODO might need to look this up at evaluation time instead |
| 864 | + atom_string(Convert,SConvert), |
| 865 | + f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, ['eval-string',SConvert], Converted, ConvertedN),!. |
| 866 | + |
771 | 867 | f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, Convert, Converted, ConvertedN) :- |
772 | 868 | nb_bound(Convert,X),!, % TODO might need to look this up at evaluation time instead |
773 | 869 | f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, X, Converted, ConvertedN). |
|
817 | 913 |
|
818 | 914 | % If Convert is a number or an atomic, it is considered as already converted. |
819 | 915 | f2p(_HeadIs, _LazyVars, RetResult, ResultLazy, Convert, Converted) :- % HeadIs\=@=Convert, |
820 | | - once(number(Convert); atom(Convert); atomic(Convert) /*; data_term(Convert)*/ ), % Check if Convert is a number or an atom |
| 916 | + once(number(Convert); atom(Convert); atomic(Convert) %; data_term(Convert) |
| 917 | + ), % Check if Convert is a number or an atom |
821 | 918 | (ResultLazy=x(_,eager,_) -> C2=Convert ; C2=[ispu,Convert]), |
822 | 919 | Converted=[[assign,RetResult,C2]], |
823 | 920 | % For OVER-REACHING categorization of dataobjs % |
|
1719 | 1816 | %must_det_lls(G):- rtrace(G),!. |
1720 | 1817 | %user:numbervars(Term):- varnumbers:numbervars(Term). |
1721 | 1818 |
|
| 1819 | +must_det_lls(G):- is_nodebug,!,call(G). % is_mettalog_rt or is_user_repl |
1722 | 1820 | must_det_lls(G):- tracing,!,call(G). % already tracing |
1723 | 1821 | must_det_lls((A,B)):- !, must_det_lls(A),must_det_lls(B). |
1724 | 1822 | %must_det_lls(G):- call(G). % already tracing |
|
1730 | 1828 | must_det_lls(G):- ignore((notrace,nortrace,trace_break(must_det_lls(G)))),rtrace(G),!. |
1731 | 1829 |
|
1732 | 1830 | extract_constraints(V,VS):- var(V),get_attr(V,cns,_Self=Set),!,extract_constraints(_Name,Set,VS),!. |
| 1831 | +extract_constraints(V,VS):- var(V),VS=[],!. |
1733 | 1832 | extract_constraints(V,VS):- var(V),!,ignore(get_types_of(V,Types)),extract_constraints(V,Types,VS),!. |
1734 | 1833 | extract_constraints(Converted,VSS):- term_variables(Converted,Vars), |
1735 | 1834 | % assign_vns(0,Vars,_), |
|
2104 | 2203 | remove_stub(Space,Fn,Arity):- retract(transpiler_stub_created(Space,Fn,Arity)),!, |
2105 | 2204 | transpile_impl_prefix(Fn,Arity,IFn),abolish(IFn/Arity),!. |
2106 | 2205 |
|
2107 | | -% !(compiled-info! cdr-atom) |
2108 | | -transpiler_predicate_store(builtin, 'compiled-info', [1], [], '', [x(doeval,eager,[])], x(doeval,eager,[])). |
2109 | | -'mc__1_1_compiled-info'(S,RetVal):- |
| 2206 | +% !(listing!! cdr-atom) |
| 2207 | +transpiler_predicate_store(builtin, 'listing!', [1], [], '', [x(doeval,eager,[])], x(doeval,eager,[])). |
| 2208 | +'mc__1_1_listing!'(S,RetVal):- |
2110 | 2209 | find_compiled_refs(S, Refs), |
2111 | | - print_refs(Refs),!, |
| 2210 | + locally(nb_setval(focal_symbol,S),print_refs(Refs)),!, |
2112 | 2211 | length(Refs,RetVal). |
2113 | 2212 |
|
2114 | 2213 | 'compiled_info'(S):- |
2115 | | - 'mc__1_1_compiled-info'(S,_RetVal). |
| 2214 | + 'mc__1_1_listing!'(S,_RetVal). |
2116 | 2215 |
|
2117 | 2216 | print_refs(Refs):- is_list(Refs),!,maplist(print_refs,Refs). |
2118 | 2217 | print_refs(Refs):- atomic(Refs),clause(M:H,B,Refs),!,print_itree(((M:H):-B)). |
|
2125 | 2224 |
|
2126 | 2225 | nl_print_tree(PT):- |
2127 | 2226 | stream_property(Err, file_no(2)), |
2128 | | - with_output_to(Err,(format('~N'),ppt(PT),format('~N'))). |
| 2227 | + mesg_color(PT, Color), |
| 2228 | + maybe_subcolor(PT,CPT), |
| 2229 | + with_output_to(Err,(format('~N'),ansicall(Color,ppt(CPT)),format('~N'))). |
2129 | 2230 |
|
| 2231 | +maybe_subcolor(PT,CPT):- fail, nb_current(focal_symbol,S), mesg_color(PT, Color), wots(Str,ansicall(Color,ppt1(S))), |
| 2232 | + subst001(PT,S,Str,CPT),!. |
| 2233 | +maybe_subcolor(PT,PT). |
2130 | 2234 |
|
2131 | 2235 | find_compiled_refs(S, Refs):- |
2132 | 2236 | atom_concat('_',S,Dashed), |
|
2144 | 2248 | \+ \+ predicate_property(M:P,_), \+ predicate_property(M:P,imported_from(_)), |
2145 | 2249 | clause(M:P,_,Ref)),Refs). |
2146 | 2250 |
|
2147 | | -compiled_refs(Symbol,F,A,Info):- functor(P,F,A),clause(P,B,Ref),call(B), \+ \+ (arg(_,P,S),S==Symbol), |
| 2251 | +compiled_refs(Symbol,F,A,Info):- functor(P,F,A),clause(P,B,Ref), (\+ compiler_data_no_call(F/A) -> call(B)), symbol_in(2,Symbol,P), |
2148 | 2252 | (B==true->Info=Ref;Info=P). |
2149 | 2253 |
|
| 2254 | + |
| 2255 | +symbol_in(_, Symbol, P):-Symbol=@=P,!. |
| 2256 | +symbol_in(N, Symbol, P):- N>0, compound(P), N2 is N-1, symbol_in_sub(N2, Symbol, P). |
| 2257 | +symbol_in_sub(N, Symbol, P):- is_list(P),P=[S1,S2,_|_],!,symbol_in_sub(N, Symbol, [S1,S2]). |
| 2258 | +symbol_in_sub(N, Symbol, P):- is_list(P),!,member(S,P),symbol_in(N, Symbol, S). |
| 2259 | +symbol_in_sub(N, Symbol, P):- arg(_,P,S),symbol_in(N, Symbol, S). |
| 2260 | + |
| 2261 | + |
2150 | 2262 | compiler_data(metta_compiled_predicate/3). |
2151 | 2263 | compiler_data(is_transpile_call_prefix/3). |
2152 | 2264 | compiler_data(is_transpile_impl_prefix/3). |
|
2159 | 2271 | compiler_data(metta_atom/2). |
2160 | 2272 | compiler_data(metta_type/3). |
2161 | 2273 | compiler_data(metta_defn/3). |
| 2274 | +compiler_data(eval_20/6). |
| 2275 | +compiler_data_no_call(eval_20/6). |
| 2276 | + |
| 2277 | +%compiler_data(metta_atom_asserted/2). |
| 2278 | + |
2162 | 2279 | %compiler_data(metta_file_buffer/7). |
2163 | 2280 |
|
2164 | 2281 | ensure_callee_site(Space,Fn,Arity):- check_supporting_predicates(Space,Fn/Arity),!. |
|
2294 | 2411 | call_fr(G,Result,FA):- current_predicate(FA),!,call(G,Result). |
2295 | 2412 | call_fr(G,Result,_):- Result=G. |
2296 | 2413 |
|
| 2414 | +transpile_eval(Convert,Converted) :- nb_current('eval_in_only', interp),!, eval(Convert,Converted). |
2297 | 2415 | transpile_eval(Convert,Converted) :- |
2298 | 2416 | transpile_eval(Convert,Converted,PrologCode),!, |
2299 | 2417 | call(PrologCode). |
|
2305 | 2423 | % PrologCode=PrologCode0, |
2306 | 2424 | % LiConverted=Converted0 |
2307 | 2425 | %; |
2308 | | - f2p(null,[],Converted,_,LE,Convert,Code1,_), |
| 2426 | + metta_to_metta_body_macro_recurse('transpile_eval',Convert,ConvertMacr), |
| 2427 | + f2p(null,[],Converted,_,LE,ConvertMacr,Code1,_), |
2309 | 2428 | lazy_impedance_match(LE,x(doeval,eager,_),Converted,Code1,Converted,Code1,LiConverted,Code), |
2310 | 2429 | ast_to_prolog(no_caller,[],Code,PrologCode), |
2311 | 2430 | compiler_assertz(transpiler_stored_eval(Convert,PrologCode,LiConverted)) |
2312 | 2431 | %) |
2313 | 2432 | . |
2314 | 2433 |
|
| 2434 | +transpile_eval_nocache(Convert,Converted,true) :- nb_current('eval_in_only', interp),!, eval(Convert,Converted). |
2315 | 2435 | transpile_eval_nocache(Convert0,LiConverted,PrologCode) :- |
2316 | 2436 | %leash(-all),trace, |
2317 | 2437 | subst_varnames(Convert0,Convert), |
|
4044 | 4164 |
|
4045 | 4165 |
|
4046 | 4166 |
|
| 4167 | + |
0 commit comments