From 3d600943309f314f3514c2718279c129f283c787 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sat, 5 Apr 2025 09:47:29 +0200 Subject: [PATCH 1/2] ENHANCED: format specifier ~w more faithfully emulates write/1 In particular, variables now start with "_". Example: ?- format("~w", [X]). %@ _A true. Found thanks to a discussion initiated by @haijinSk: https://github.com/mthom/scryer-prolog/discussions/2863 --- src/lib/format.pl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib/format.pl b/src/lib/format.pl index 62a9e93e..6f5bac92 100644 --- a/src/lib/format.pl +++ b/src/lib/format.pl @@ -90,15 +90,15 @@ format_(Fs, Args) --> format_args_cells(Fs, Args, Cells) :- must_be(chars, Fs), must_be(list, Args), - unique_variable_names(Args, VNs), + unique_variable_names(fabricated, Args, VNs), phrase(cells(Fs,Args,0,[],VNs), Cells). -unique_variable_names(Term, VNs) :- +unique_variable_names(Type, Term, VNs) :- term_variables(Term, Vs), - foldl(var_name, Vs, VNs, 0, _). + foldl(var_name(Type), Vs, VNs, 0, _). -var_name(V, Name=V, Num0, Num) :- - charsio:fabricate_var_name(numbervars, Name, Num0), +var_name(Type, V, Name=V, Num0, Num) :- + charsio:fabricate_var_name(Type, Name, Num0), Num is Num0 + 1. user:goal_expansion(format_(Fs,Args,Cs0,Cs), @@ -574,7 +574,7 @@ portray_clause(Stream, Term) :- flush_output(Stream). portray_clause_(Term) --> - { unique_variable_names(Term, VNs) }, + { unique_variable_names(numbervars, Term, VNs) }, portray_(Term, VNs), ".\n". literal(Lit, VNs) --> From 14c8fc3e345a664d02f61833a761f23fdf58f65f Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sat, 5 Apr 2025 10:01:22 +0200 Subject: [PATCH 2/2] move ~w to least prominent position, point to more suitable specifiers In particular, "~q" is a safe better choice. --- src/lib/format.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/format.pl b/src/lib/format.pl index 6f5bac92..0c6fa2db 100644 --- a/src/lib/format.pl +++ b/src/lib/format.pl @@ -42,7 +42,6 @@ % FormatString are used literally, except for the following tokens % with special meaning: % -% | `~w` | use the next available argument from Arguments here | % | `~q` | use the next argument here, formatted as by `writeq/1` | % | `~a` | use the next argument here, which must be an atom | % | `~s` | use the next argument here, which must be a string | @@ -72,6 +71,7 @@ % | `~Nn` | N newlines | % | `~i` | ignore the next argument | % | `~~` | the literal ~ | +% | `~w` | format like `write/1` would; consider using `~q`, `~d`, etc. | % % Instead of `~N`, you can write `~*` to use the next argument from % Arguments as the numeric argument.