diff --git a/prolog/metta_lang/metta_interp.pl b/prolog/metta_lang/metta_interp.pl index fe8bd6d1ee..5f04f43ee0 100755 --- a/prolog/metta_lang/metta_interp.pl +++ b/prolog/metta_lang/metta_interp.pl @@ -411,6 +411,7 @@ option_value_name_default_type_help('answer-format', 'show', ['rust', 'silent', 'detailed'], "Control how results are displayed", 'Output and Logging'). option_value_name_default_type_help('repeats', true, [true, false], "false to avoid repeated results", 'Miscellaneous'). option_value_name_default_type_help('time', true, [false, true], "Enable or disable timing for operations (in Rust compatibility mode, this is false)", 'Miscellaneous'). +option_value_name_default_type_help('vn', auto, [auto, true, false], "Enable or disable, (auto = enable but not if it breaks stuff) EXPERIMENTAL BUG-FIX where variable names are preserved (see https://github.com/trueagi-io/metta-wam/issues/221)", 'Miscellaneous'). % Testing and Validation option_value_name_default_type_help('synth-unit-tests', false, [false, true], "Synthesize unit tests", 'Testing and Validation'). diff --git a/prolog/metta_lang/metta_loader.pl b/prolog/metta_lang/metta_loader.pl index 352f5c5ff7..da2a37dabd 100755 --- a/prolog/metta_lang/metta_loader.pl +++ b/prolog/metta_lang/metta_loader.pl @@ -3347,7 +3347,20 @@ % @arg NamedVarsList The list of named variables. % subst_vars(TermWDV, NewTerm, NamedVarsList) :- - subst_vars(TermWDV, NewTerm, [], NamedVarsList). + subst_vars(TermWDV, NewTerm, [], NamedVarsList), + if_t(fast_option_value('vn', 'true'), memorize_varnames(NamedVarsList)). + + +memorize_varnames(NamedVarsList):- \+ compound(NamedVarsList),!. +memorize_varnames([NamedVar|NamedVarsList]):- !, + memorize_varname(NamedVar), + memorize_varnames(NamedVarsList). +memorize_varnames(_). +memorize_varname(NamedVar):- \+ compound(NamedVar),!. +memorize_varname(Name=Var):- var(Var),atomic(Name),put_attr(Var,vn,Name). +memorize_varname(_). + + %! subst_vars(+Term, -Term, +Acc, -NamedVarsList) is det. % diff --git a/prolog/metta_lang/metta_printer.pl b/prolog/metta_lang/metta_printer.pl index b0f8bba973..55ec7e8201 100755 --- a/prolog/metta_lang/metta_printer.pl +++ b/prolog/metta_lang/metta_printer.pl @@ -554,13 +554,13 @@ % write_dvar(S) :- % If S is an underscore, output the name directly. - S == '_', !, write_dname(S). + S == '_', !, write('$_'). write_dvar(S) :- % If S is a double underscore, write `$` to represent it. S == '__', !, write('$'). write_dvar(S) :- % For an unbound variable, get its name and write it. - var(S), get_var_name(S, N), write_dname(N), !. + var(S), get_var_name(S, N), write_dvar(N), !. write_dvar(S) :- % For an unbound variable without a name, format it as `$`. var(S), !, format('$~p', [S]). diff --git a/prolog/metta_lang/metta_repl.pl b/prolog/metta_lang/metta_repl.pl index 32da417534..574875092e 100755 --- a/prolog/metta_lang/metta_repl.pl +++ b/prolog/metta_lang/metta_repl.pl @@ -1473,7 +1473,11 @@ % write_bsrc(Var):- Var=='Empty',!,write(Var). % Special case: write 'Empty' directly. write_bsrc(Var):- ground(Var),!,write_bsrc1(Var). % If the variable is ground, write it directly. -write_bsrc(Var):- copy_term(Var,Copy,Goals),Var=Copy,write_bsrc_goal(Var,Goals). % For non-ground terms, handle goals. +write_bsrc(Var):- copy_term(Var,Copy,Goals),Var=Copy, + exclude(excluded_hidden_goal,Goals,UnhiddenGoals), + write_bsrc_goal(Var,UnhiddenGoals). % For non-ground terms, handle goals. + +excluded_hidden_goal(name_variable(_,_)). %! write_bsrc_goal(+Var, +Goals) is det. %