Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

our_ansi_format/3 errors out when allowed to backtrack. #222

Open
stassa opened this issue Dec 13, 2024 · 0 comments
Open

our_ansi_format/3 errors out when allowed to backtrack. #222

stassa opened this issue Dec 13, 2024 · 0 comments

Comments

@stassa
Copy link
Collaborator

stassa commented Dec 13, 2024

our_ansi_format/3 is a thin wrapper around SWI-Prolog's ansi_format/3 to allow passing colour arguments as atoms rather than lists of attributes. The following example is from the SWI-Prolog documentation for ansi_format/3.

% Start in the Prolog shell but load metta_interp.pl.
stassaP@VARYS:~$ cd /mnt/c/Users/me/Documents/Prolog/metta/metta-wam_win
stassaP@VARYS:/mnt/c/Users/meDocuments/Prolog/metta/metta-wam_win$ swipl $PWD/prolog/metta_lang/metta_interp.pl --prolog

% Note the ; after the first successful call.
1 ?- our_ansi_format([bold,fg(cyan)], 'Hello ~w', [world]).
Hello world
true ;
% Term-position in <clause>(0x561914d278c0) at PC=27:
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/prolog_clause.pl',set_prolog_flag(dialect_pfc,default))
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/prolog_clause.pl',set_prolog_flag(subclause_expansion,true))
% info(load_mpred_file_complete(prolog_clause:'/home/me/lib/swipl/library/prolog_clause.pl'))
% info(load_mpred_file_complete(prolog_clause:'/home/me/lib/swipl/library/prolog_clause.pl'))
% found_eof_hook((t_l:eof_hook('/home/me/lib/swipl/library/prolog_clause.pl',file_scope:set_prolog_flag(subclause_expansion,true)):-true))
% call_eof_hook(on_f_log_ignore(prolog_clause,t_l:eof_hook('/home/me/lib/swipl/library/prolog_clause.pl',file_scope:set_prolog_flag(subclause_expansion,true))))
% found_eof_hook((t_l:eof_hook('/home/me/lib/swipl/library/prolog_clause.pl',file_scope:set_prolog_flag(dialect_pfc,default)):-true))
% call_eof_hook(on_f_log_ignore(prolog_clause,t_l:eof_hook('/home/me/lib/swipl/library/prolog_clause.pl',file_scope:set_prolog_flag(dialect_pfc,default))))
% clause_info(<clause>(0x561914d278c0)) (1-st clause of rgb/4)...
% from /home/me/lib/swipl/library/ansi_term.pl:319 ...
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/prolog_source.pl',set_prolog_flag(dialect_pfc,default))
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/prolog_source.pl',set_prolog_flag(subclause_expansion,true))
% Trying with syntax prolog
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/operators.pl',set_prolog_flag(dialect_pfc,default))
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/operators.pl',set_prolog_flag(subclause_expansion,true))
% read ...
% unified ...
% got names

%       <span class="pl-list"><span class="pl-functor"> [ </span><span class="pl-args"><span class="pl-int">2</span>, <span class="pl-int">1</span></span>]</span>

% eof_hook(register,file_scope,'/home/me/lib/swipl/library/prolog_clause.pl',set_prolog_flag(dialect_pfc,default))
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/prolog_clause.pl',set_prolog_flag(subclause_expansion,true))
% Term-position in <clause>(0x561914d26fc0) at PC=44:
% clause_info(<clause>(0x561914d26fc0)) (16-th clause of sgr_code/2)...
% from /home/me/lib/swipl/library/ansi_term.pl:244 ...
% Trying with syntax prolog
% read ...
% unified ...
% got names

%       <span class="pl-list"><span class="pl-functor"> [ </span><span class="pl-args"><span class="pl-int">2</span>, <span class="pl-int">2</span>|<span class="pl-ellipsis">...</span></span>]</span>

% Term-position in <clause>(0x561914ce5b80) at PC=9:
% clause_info(<clause>(0x561914ce5b80)) (4-th clause of sgr_codes_ex/3)...
% from /home/me/lib/swipl/library/ansi_term.pl:176 ...
% Trying with syntax prolog
% read ...
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/occurs.pl',set_prolog_flag(dialect_pfc,default))
% eof_hook(register,file_scope,'/home/me/lib/swipl/library/occurs.pl',set_prolog_flag(subclause_expansion,true))
% unified ...
% got names

%       <span class="pl-list"><span class="pl-functor"> [ </span><span class="pl-args"><span class="pl-int">2</span>, <span class="pl-int">1</span>|<span class="pl-ellipsis">...</span></span>]</span>

% Term-position in <clause>(0x561914d13080) at PC=16:
% clause_info(<clause>(0x561914d13080)) (3-th clause of sgr_codes_ex/3)...
% from /home/me/lib/swipl/library/ansi_term.pl:172 ...
% Trying with syntax prolog
% read ...
% unified ...
% got names

%       <span class="pl-list"><span class="pl-functor"> [ </span><span class="pl-args"><span class="pl-int">2</span>, <span class="pl-int">2</span>|<span class="pl-ellipsis">...</span></span>]</span>

% Term-position in <clause>(0x561914beec80) at PC=60:
% clause_info(<clause>(0x561914beec80)) (1-st clause of call_dcg/3)...
% from /home/me/lib/swipl/boot/dcg.pl:352 ...
% Trying with syntax prolog
% read ...
% unified ...
% got names

%       <span class="pl-list"><span class="pl-functor"> [ </span><span class="pl-args"><span class="pl-int">2</span>, <span class="pl-int">2</span></span>]</span>

% Term-position in <clause>(0x561914cd7b00) at PC=30:
% clause_info(<clause>(0x561914cd7b00)) (1-st clause of ansi_format/4)...
% from /home/me/lib/swipl/library/ansi_term.pl:147 ...
% Trying with syntax prolog
% read ...
% unified ...
% got names

%       <span class="pl-functor">[ </span>
  <span class="pl-int">2</span>, <span class="pl-int">2</span>, <span class="pl-int">2</span>, <span class="pl-int">2</span>, <span class="pl-int">2</span>, <span class="pl-int">1</span>]

% Term-position in <clause>(0x5619168fd0a0) at PC=18:
% clause_info(<clause>(0x5619168fd0a0)) (1-st clause of our_ansi_format/3)...
% from /mnt/c/Users/YeGoblynQueenne/Documents/Prolog/metta/metta-wam_win/prolog/metta_lang/metta_testing.pl:249 ...
% Trying with syntax prolog
% read ...
% unified ...
% got names

%       <span class="pl-list"><span class="pl-functor"> [ </span><span class="pl-args"><span class="pl-int">2</span>, <span class="pl-int">2</span></span>]</span>

ERROR: Type error: `atom' expected, found `
[ bold, fg(cyan)]

                                           ' (a list)
ERROR: In:
ERROR:   [53] atom_codes(
[ bold, fg(cyan)]

                         ,
[ 35, _38212, _38218, _38224, _38230, _38236, _3270]

                          )
ERROR:   [52] ansi_term:rgb(
[ bold, fg(cyan)]

                            ,_834,_836,_838) at /home/me/lib/swipl/library/ansi_term.pl:320
ERROR:   [51] ansi_term:sgr_code(fg(
[ bold, fg(cyan)]

                                    ),_886) at /home/me/lib/swipl/library/ansi_term.pl:247
ERROR:   [50] ansi_term:sgr_codes_ex(fg(
[ bold, fg(cyan)]

                                        ),_938,_940) at /home/me/lib/swipl/library/ansi_term.pl:177
ERROR:   [49] ansi_term:sgr_codes_ex(
[ fg( [ bold

                                     ,_992,[]) at /home/me/lib/swipl/library/ansi_term.pl:174
ERROR:   [48] '$dcg':call_dcg(ansi_term:sgr_codes_ex(
[ fg( [ bold

                                                     ),_1052,[]) at /home/me/lib/swipl/boot/dcg.pl:359
ERROR:   [45] ansi_term:ansi_format(current_output,
[ fg( [ bold

                                                   ,'Hello ~w',
[ world]

                                                               ) at /home/me/lib/swipl/library/ansi_term.pl:152
ERROR:   [43] our_ansi_format(
[ fg( [ bold

                              ,'Hello ~w',
[ world]

                                          ) at /mnt/c/Users/YeGoblynQueenne/Documents/Prolog/metta/metta-wam_win/prolog/metta_lang/metta_testing.pl:252
ERROR:   [41] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
   Exception: (52) [ansi_term] ansi_term:rgb([bold, fg('cyan')], _622, _624, _626) ? creep
^  Exception: (43) [user] our_ansi_format([fg([bold, fg('cyan')])], 'Hello ~w', [world]) ? creep
^  Exception: (34) [system] setup_call_cleanup('$toplevel':notrace(call_repl_loop_hook(begin, 0)), '$toplevel':'$query_loop'(0), '$toplevel':notrace(call_repl_loop_hook(end, 0))) ? creep
^  Call: (37) [system] setup_call_cleanup('$notrace'(_43074, _43076), once('$toplevel':call_repl_loop_hook(end, 0)), '$restore_trace'(_43074, _43076)) ? creep
^  Call: (38) [system] sig_atomic(system:'$notrace'(_43074, _43076)) ? creep
^  Exit: (37) [system] setup_call_cleanup(system:'$notrace'(3, -1), system:once('$toplevel':call_repl_loop_hook(end, 0)), system:'$restore_trace'(3, -1)) ? creep
2 ?-
|    .

ERROR: Stream user_input:127:1 Syntax error: Unexpected end of clause

Inspecting the big friendly error dump the problem seems to be that the wrong type ish of data is passed to ansi_format/3 by our_ansi_format/3 at some point. Looking at the definition of our_ansi_format/3 it's obvious this is happening when execution backtracks after the first clause, to the second one, that expects an atom C in its first argument, which it wraps in a list of attributes as expected by ansi_format/3:

our_ansi_format(C, Fmt, Args) :-
    % If Color is not an atom, apply ansi_format directly.
    \+ atom(C), % set_stream(current_output,encoding(utf8)),
    ansi_format(C, Fmt, Args).
our_ansi_format(C, Fmt, Args) :-
    % If Color is atomic, set the foreground color and format the output.
    our_ansi_format([fg(C)], Fmt, Args).

The quick and dirty solution should be obvious but a fuller solution is to implement a proper wrapper for ansi_format/3 that can handle all forms of input accepted by that predicate.

stassa added a commit to stassa/metta-testsuite that referenced this issue Dec 16, 2024
* See issue trueagi-io/metta-wam#222
* Previously our_ansi_format/3 would raise an exception if allowed to
  backtrack after a first successful call. This commit fixes that and
  also extends the wrapping around SWI-Prolog's ansi_format/3 to all
  attribute formats accepted by that predicate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant