Skip to content

Commit f05169c

Browse files
committed
closer to original .. still 74_0 69_7
1 parent 97daa1b commit f05169c

File tree

10 files changed

+3826
-3043
lines changed

10 files changed

+3826
-3043
lines changed

prolog/metta_lang/metta_compiler.pl

Lines changed: 131 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@
7474
:- ensure_loaded(metta_space).
7575
:- ensure_loaded(metta_compiler_print).
7676
:- dynamic(transpiler_clause_store/9).
77+
:- multifile(transpiler_predicate_store/7).
7778
:- dynamic(transpiler_predicate_store/7).
79+
:- dynamic(transpiler_predicate_nary_store/9).
80+
:- discontiguous transpiler_predicate_nary_store/9.
7881
:- discontiguous(compile_flow_control/8).
82+
:- multifile(compile_flow_control/8).
7983
:- ensure_loaded(metta_compiler_lib).
8084
:- ensure_loaded(metta_compiler_lib_stdlib).
8185

@@ -212,6 +216,7 @@
212216
as_p1_exec(ispeEnNC(ERet,ECode,_,_,CCode),ERet) :- !, call(CCode),call(ECode).
213217
as_p1_exec(X,X) :- !.
214218

219+
215220
as_p1_expr(ispu(URet),URet) :- !.
216221
as_p1_expr(ispuU(URet,UCode),URet) :- !, call(UCode).
217222
as_p1_expr(ispeEn(_,_,NRet),NRet).
@@ -240,6 +245,92 @@
240245
partial_combine_lists(L1,L2,Lcomb,L1a,L2a).
241246
partial_combine_lists(L1,L2,[],L1,L2).
242247

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+
243334
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244335
%%%%%%%%%%%%%%%%% Evaluation (!)
245336
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -352,7 +443,7 @@
352443
invert_curried_structure(F,LenArgs,ArgsFirst,Result).
353444

354445

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)),!.
356447
recompile_from_depends(FnName,LenArgs) :-
357448
transpiler_debug(2,(format_e("recompile_from_depends ~w/~w\n",[FnName,LenArgs]))),
358449
%LenArgs is LenArgsPlus1-1,
@@ -768,6 +859,11 @@
768859
f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, T, Converted, ConvertedN) :- compound(T),T=exec(X),!,
769860
f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, [eval,X], Converted, ConvertedN).
770861

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+
771867
f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, Convert, Converted, ConvertedN) :-
772868
nb_bound(Convert,X),!, % TODO might need to look this up at evaluation time instead
773869
f2p(HeadIs, LazyVars, RetResult, RetResultN, ResultLazy, X, Converted, ConvertedN).
@@ -817,7 +913,8 @@
817913
818914
% If Convert is a number or an atomic, it is considered as already converted.
819915
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
821918
(ResultLazy=x(_,eager,_) -> C2=Convert ; C2=[ispu,Convert]),
822919
Converted=[[assign,RetResult,C2]],
823920
% For OVER-REACHING categorization of dataobjs %
@@ -1719,6 +1816,7 @@
17191816
%must_det_lls(G):- rtrace(G),!.
17201817
%user:numbervars(Term):- varnumbers:numbervars(Term).
17211818

1819+
must_det_lls(G):- is_nodebug,!,call(G). % is_mettalog_rt or is_user_repl
17221820
must_det_lls(G):- tracing,!,call(G). % already tracing
17231821
must_det_lls((A,B)):- !, must_det_lls(A),must_det_lls(B).
17241822
%must_det_lls(G):- call(G). % already tracing
@@ -1730,6 +1828,7 @@
17301828
must_det_lls(G):- ignore((notrace,nortrace,trace_break(must_det_lls(G)))),rtrace(G),!.
17311829

17321830
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=[],!.
17331832
extract_constraints(V,VS):- var(V),!,ignore(get_types_of(V,Types)),extract_constraints(V,Types,VS),!.
17341833
extract_constraints(Converted,VSS):- term_variables(Converted,Vars),
17351834
% assign_vns(0,Vars,_),
@@ -2104,15 +2203,15 @@
21042203
remove_stub(Space,Fn,Arity):- retract(transpiler_stub_created(Space,Fn,Arity)),!,
21052204
transpile_impl_prefix(Fn,Arity,IFn),abolish(IFn/Arity),!.
21062205

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):-
21102209
find_compiled_refs(S, Refs),
2111-
print_refs(Refs),!,
2210+
locally(nb_setval(focal_symbol,S),print_refs(Refs)),!,
21122211
length(Refs,RetVal).
21132212

21142213
'compiled_info'(S):-
2115-
'mc__1_1_compiled-info'(S,_RetVal).
2214+
'mc__1_1_listing!'(S,_RetVal).
21162215

21172216
print_refs(Refs):- is_list(Refs),!,maplist(print_refs,Refs).
21182217
print_refs(Refs):- atomic(Refs),clause(M:H,B,Refs),!,print_itree(((M:H):-B)).
@@ -2125,8 +2224,13 @@
21252224

21262225
nl_print_tree(PT):-
21272226
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'))).
21292230

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).
21302234

21312235
find_compiled_refs(S, Refs):-
21322236
atom_concat('_',S,Dashed),
@@ -2144,9 +2248,17 @@
21442248
\+ \+ predicate_property(M:P,_), \+ predicate_property(M:P,imported_from(_)),
21452249
clause(M:P,_,Ref)),Refs).
21462250

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),
21482252
(B==true->Info=Ref;Info=P).
21492253

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+
21502262
compiler_data(metta_compiled_predicate/3).
21512263
compiler_data(is_transpile_call_prefix/3).
21522264
compiler_data(is_transpile_impl_prefix/3).
@@ -2159,6 +2271,11 @@
21592271
compiler_data(metta_atom/2).
21602272
compiler_data(metta_type/3).
21612273
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+
21622279
%compiler_data(metta_file_buffer/7).
21632280

21642281
ensure_callee_site(Space,Fn,Arity):- check_supporting_predicates(Space,Fn/Arity),!.
@@ -2294,6 +2411,7 @@
22942411
call_fr(G,Result,FA):- current_predicate(FA),!,call(G,Result).
22952412
call_fr(G,Result,_):- Result=G.
22962413

2414+
transpile_eval(Convert,Converted) :- nb_current('eval_in_only', interp),!, eval(Convert,Converted).
22972415
transpile_eval(Convert,Converted) :-
22982416
transpile_eval(Convert,Converted,PrologCode),!,
22992417
call(PrologCode).
@@ -2305,13 +2423,15 @@
23052423
% PrologCode=PrologCode0,
23062424
% LiConverted=Converted0
23072425
%;
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,_),
23092428
lazy_impedance_match(LE,x(doeval,eager,_),Converted,Code1,Converted,Code1,LiConverted,Code),
23102429
ast_to_prolog(no_caller,[],Code,PrologCode),
23112430
compiler_assertz(transpiler_stored_eval(Convert,PrologCode,LiConverted))
23122431
%)
23132432
.
23142433

2434+
transpile_eval_nocache(Convert,Converted,true) :- nb_current('eval_in_only', interp),!, eval(Convert,Converted).
23152435
transpile_eval_nocache(Convert0,LiConverted,PrologCode) :-
23162436
%leash(-all),trace,
23172437
subst_varnames(Convert0,Convert),
@@ -4044,3 +4164,4 @@
40444164

40454165

40464166

4167+

0 commit comments

Comments
 (0)