provide better variable names in write_term_to_chars/3 (#340)
This commit is contained in:
@@ -3,6 +3,51 @@
|
||||
|
||||
:- use_module(library(iso_ext)).
|
||||
|
||||
|
||||
fabricate_var_name(VarName, N) :-
|
||||
char_code('A', AC),
|
||||
LN is N mod 26 + AC,
|
||||
char_code(LC, LN),
|
||||
NN is N // 26,
|
||||
( NN =:= 0 ->
|
||||
atom_chars(VarName, ['_', LC])
|
||||
; number_chars(NN, NNChars),
|
||||
atom_chars(VarName, ['_', LC | NNChars])
|
||||
).
|
||||
|
||||
var_list_contains_name([VarName = _ | VarList], VarName0) :-
|
||||
( VarName == VarName0 -> true
|
||||
; var_list_contains_name(VarList, VarName0)
|
||||
).
|
||||
|
||||
var_list_contains_variable([_ = Var | VarList], Var0) :-
|
||||
( Var == Var0 -> true
|
||||
; var_list_contains_variable(VarList, Var0)
|
||||
).
|
||||
|
||||
make_new_var_name(V, VarName, N, N1, VarList) :-
|
||||
fabricate_var_name(VarName0, N),
|
||||
( var_list_contains_name(VarList, VarName0) ->
|
||||
N0 is N + 1,
|
||||
make_new_var_name(V, VarName, N0, N1, VarList)
|
||||
; VarName = VarName0,
|
||||
N1 is N + 1
|
||||
).
|
||||
|
||||
extend_var_list(Value, VarList, NewVarList) :-
|
||||
term_variables(Value, Vars),
|
||||
extend_var_list_(Vars, 0, VarList, NewVarList).
|
||||
|
||||
extend_var_list_([], N, VarList, VarList).
|
||||
extend_var_list_([V|Vs], N, VarList, NewVarList) :-
|
||||
( var_list_contains_variable(VarList, V) ->
|
||||
extend_var_list_(Vs, N, VarList, NewVarList)
|
||||
; make_new_var_name(V, VarName, N, N1, VarList),
|
||||
NewVarList = [VarName = V | NewVarList0],
|
||||
extend_var_list_(Vs, N1, VarList, NewVarList0)
|
||||
).
|
||||
|
||||
|
||||
read_term_from_chars(Chars, Term) :-
|
||||
( var(Chars) ->
|
||||
throw(error(instantiation_error, read_term_from_chars/2))
|
||||
@@ -36,4 +81,5 @@ write_term_to_chars(Term, Options, Chars) :-
|
||||
builtins:inst_member_or(Options, quoted(Quoted), quoted(false)),
|
||||
builtins:inst_member_or(Options, variable_names(VarNames), variable_names([])),
|
||||
builtins:inst_member_or(Options, max_depth(MaxDepth), max_depth(0)),
|
||||
'$write_term_to_chars'(Term, IgnoreOps, NumberVars, Quoted, VarNames, MaxDepth, Chars).
|
||||
extend_var_list(Term, VarNames, NewVarNames),
|
||||
'$write_term_to_chars'(Term, IgnoreOps, NumberVars, Quoted, NewVarNames, MaxDepth, Chars).
|
||||
|
||||
@@ -423,6 +423,16 @@ impl Machine {
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
compile_user_module(&mut wam,
|
||||
Stream::from(CHARSIO),
|
||||
true,
|
||||
ListingSource::from_file_and_path(
|
||||
clause_name!("si"),
|
||||
lib_path.clone(),
|
||||
)
|
||||
);
|
||||
|
||||
if wam.compile_top_level().is_err() {
|
||||
panic!("Loading '$toplevel' module failed");
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
:- use_module(library(lists)).
|
||||
:- use_module(library(si)).
|
||||
|
||||
:- module('$toplevel', ['$repl'/1, consult/1, use_module/1, use_module/2]).
|
||||
|
||||
:- use_module(library(charsio)).
|
||||
:- use_module(library(lists)).
|
||||
:- use_module(library(si)).
|
||||
|
||||
'$repl'([_|Args]) :-
|
||||
maplist('$use_list_of_modules', Args),
|
||||
false.
|
||||
@@ -67,57 +69,14 @@
|
||||
memberchk(EqSpec, [fx,xfx,yfx])
|
||||
).
|
||||
|
||||
'$fabricate_var_name'(VarName, N) :-
|
||||
char_code('A', AC),
|
||||
LN is N mod 26 + AC,
|
||||
char_code(LC, LN),
|
||||
NN is N // 26,
|
||||
( NN =:= 0 ->
|
||||
atom_chars(VarName, ['_', LC])
|
||||
; number_chars(NN, NNChars),
|
||||
atom_chars(VarName, ['_', LC | NNChars])
|
||||
).
|
||||
|
||||
'$var_list_contains_name'([VarName = _ | VarList], VarName0) :-
|
||||
( VarName == VarName0 -> true
|
||||
; '$var_list_contains_name'(VarList, VarName0)
|
||||
).
|
||||
|
||||
'$var_list_contains_variable'([_ = Var | VarList], Var0) :-
|
||||
( Var == Var0 -> true
|
||||
; '$var_list_contains_variable'(VarList, Var0)
|
||||
).
|
||||
|
||||
'$make_new_var_name'(V, VarName, N, N1, VarList) :-
|
||||
'$fabricate_var_name'(VarName0, N),
|
||||
( '$var_list_contains_name'(VarList, VarName0) ->
|
||||
N0 is N + 1,
|
||||
'$make_new_var_name'(V, VarName, N0, N1, VarList)
|
||||
; VarName = VarName0,
|
||||
N1 is N + 1
|
||||
).
|
||||
|
||||
'$extend_var_list'(Value, VarList, NewVarList) :-
|
||||
term_variables(Value, Vars),
|
||||
'$extend_var_list_'(Vars, 0, VarList, NewVarList).
|
||||
|
||||
'$extend_var_list_'([], N, VarList, VarList).
|
||||
'$extend_var_list_'([V|Vs], N, VarList, NewVarList) :-
|
||||
( '$var_list_contains_variable'(VarList, V) ->
|
||||
'$extend_var_list_'(Vs, N, VarList, NewVarList)
|
||||
; '$make_new_var_name'(V, VarName, N, N1, VarList),
|
||||
NewVarList = [VarName = V | NewVarList0],
|
||||
'$extend_var_list_'(Vs, N1, VarList, NewVarList0)
|
||||
).
|
||||
|
||||
'$write_goal'(G, VarList, MaxDepth) :-
|
||||
( G = (Var = Value) ->
|
||||
write(Var),
|
||||
write(' = '),
|
||||
( '$needs_bracketing'(Value, (=)) ->
|
||||
write('('),
|
||||
write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
||||
write(')')
|
||||
write('('),
|
||||
write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
||||
write(')')
|
||||
; write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)])
|
||||
)
|
||||
; G == [] ->
|
||||
@@ -130,14 +89,14 @@
|
||||
write(Var),
|
||||
write(' = '),
|
||||
( '$needs_bracketing'(Value, (=)) ->
|
||||
write('('),
|
||||
write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
||||
write(')')
|
||||
write('('),
|
||||
write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
||||
write(')')
|
||||
; write_term(Value, [quoted(true), variable_names(VarList), max_depth(MaxDepth)]),
|
||||
( '$trailing_period_is_ambiguous'(Value) ->
|
||||
write(' ')
|
||||
; true
|
||||
)
|
||||
( '$trailing_period_is_ambiguous'(Value) ->
|
||||
write(' ')
|
||||
; true
|
||||
)
|
||||
)
|
||||
; G == [] ->
|
||||
write('true')
|
||||
@@ -167,7 +126,7 @@
|
||||
'$graphic_token_char'(Char).
|
||||
|
||||
'$write_eqs_and_read_input'(B, VarList) :-
|
||||
'$extend_var_list'(VarList, VarList, NewVarList),
|
||||
charsio:extend_var_list(VarList, VarList, NewVarList),
|
||||
sort(NewVarList, SortedVarList),
|
||||
'$get_b_value'(B0),
|
||||
'$gather_goals'(SortedVarList, SortedVarList, Goals),
|
||||
|
||||
Reference in New Issue
Block a user