Skip to content

Commit 1978406

Browse files
committed
metta_printer.pl: fix pretty-printer to preserve variable names via copy_term; improve maybe_write_goals/1 filtering; safely handle empty or unbound attrs with \attrs\
1 parent abf1b9e commit 1978406

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

prolog/metta_lang/metta_printer.pl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
% Run the primary source-printing predicate within `run_pl_source/1`.
244244
run_pl_source(print_pl_source0(P)).
245245

246+
%pnotrace(G):- !, call(G).
246247
pnotrace(G):- notrace(G).
247248
%pnotrace(G):- quietly(G).
248249

@@ -596,7 +597,7 @@
596597
mvar_str(S,N) :- % For an unbound variable without a name, format it as `$<variable>`.
597598
attvar(S), notrace,ignore(nortrace),
598599
with_output_to(string(SS),ignore((get_attrs(S,Attrs),display(Attrs)))),
599-
trace,
600+
%trace,
600601
sformat(N,'a-~w-~w', [SS,S]). % bou
601602
mvar_str(S,N) :- % For an unbound variable without a name, format it as `$<variable>`.
602603
var(S), !, sformat(N,'~p', [S]).
@@ -764,10 +765,19 @@
764765
% display(v=V),
765766
no_type_unification((
766767
% Guess variables in V and pretty-print using `pp_sex/1`.
768+
767769
undo_bindings(pnotrace((
768-
gather_src_and_goal(V,Nat,NatGoals),
769-
once((pp_sex(Nat))),
770-
maybe_write_goals(NatGoals)))))), !.
770+
term_variables(V,Vars),
771+
copy_term(V+Vars,CV+CVars,_),
772+
maplist(transfer_varname,Vars,CVars),
773+
((CVars=@=Vars)
774+
-> pp_sex(V);
775+
(gather_src_and_goal(V,Nat,NatGoals),
776+
unify_with_occurs_check(CV,Nat),
777+
write_src_ng(CV,NatGoals)))))))),!.
778+
779+
write_src_ng(Nat,NatGoals):- pp_sex(Nat),
780+
if_t(NatGoals\==[],if_t(nb_current('$write_goals',true),maybe_write_goals(NatGoals))).
771781

772782
bou:attr_portray_hook(Val,_V):- copy_term(Val,_,Info),!,format(' bou ~w ',[Info]),!. %writeq(break_on_unify(V,Val)),write(' ').
773783
bou:attr_unify_hook(_,_):- bt,!,trace,break.
@@ -848,11 +858,10 @@
848858
with_written_goals(Call):-
849859
locally(b_setval('$write_goals',true),Call).
850860

851-
maybe_write_goals(_Goals):- \+ nb_current('$write_goals',true), !.
852-
maybe_write_goals(Goals):-
853-
exclude(is_f_nv,Goals,LGoals),
854-
if_t(LGoals\==[],format(' {<~q>} ', [LGoals])).
855-
%if_t(LGoals\==[],with_output_to(user_error,ansi_format([fg(yellow)], ' {~q} ', [LGoals]))).
861+
maybe_write_goals(Goals):- Goals==[],!.
862+
maybe_write_goals(_):- \+ nb_current('$write_goals',true), !.
863+
maybe_write_goals(Goals):- once(exclude(is_f_nv,Goals,LGoals)),if_t(LGoals\==[],format(' {<~q>} ', [LGoals])).
864+
856865
is_f_nv(P):- compound(P), functor(P,F,A,_), !, is_f_nv(F,A).
857866
is_f_nv(break_on_unify,2). is_f_nv(name_variable,2).
858867

0 commit comments

Comments
 (0)