Give new answer variables readable names (#279)

This commit is contained in:
Mark Thom
2020-03-14 03:04:11 -06:00
parent beed4e8aa8
commit 31258247b3
2 changed files with 71 additions and 17 deletions

View File

@@ -65,6 +65,49 @@
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) :-
( G = (Var = Value) ->
write(Var),
@@ -122,19 +165,20 @@
'$graphic_token_char'(Char).
'$write_eqs_and_read_input'(B, VarList) :-
sort(VarList, SortedVarList),
'$extend_var_list'(VarList, VarList, NewVarList),
sort(NewVarList, SortedVarList),
'$get_b_value'(B0),
'$gather_goals'(SortedVarList, VarList, Goals),
'$gather_goals'(SortedVarList, SortedVarList, Goals),
( B0 == B ->
( Goals == [] ->
write('true.'), nl
; thread_goals(Goals, ThreadedGoals, (',')),
'$write_eq'(ThreadedGoals, VarList),
'$write_eq'(ThreadedGoals, NewVarList),
write('.'),
nl
)
; thread_goals(Goals, ThreadedGoals, (',')),
'$write_eq'(ThreadedGoals, VarList),
'$write_eq'(ThreadedGoals, NewVarList),
'$raw_input_read_char'(C),
( C == (';'), !,
write(' ;'), nl, false