]> Repositorios git - scryer-prolog.git/commitdiff
provide better variable names in write_term_to_chars/3 (#340)
authorMark Thom <[email protected]>
Tue, 14 Apr 2020 16:39:10 +0000 (10:39 -0600)
committerMark Thom <[email protected]>
Tue, 14 Apr 2020 16:39:10 +0000 (10:39 -0600)
src/prolog/lib/charsio.pl
src/prolog/machine/mod.rs
src/prolog/toplevel.pl

index 3ef735eea07619f80fe0f8e7fba75bfc9a5a81ff..b0ccc88d961a429e460bea79a3cac89ca5e415e8 100644 (file)
@@ -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).
index 7a68e47216d45515b1948d2cb7681ec2185d0c6a..248f35fe3907f6d534d75cda37f1c85f84376eb6 100644 (file)
@@ -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");
         }
index 3e044661485027be52a0f8a86a42f617bc1049bd..37afaf6f1e46d760da99120eb8e737d9f4cdf019 100644 (file)
@@ -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.
        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 == [] ->
        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')
     '$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),