From 2f3de51e554629d2e7b4ea023c9ef181c240692c Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Fri, 13 May 2022 18:38:02 -0600 Subject: [PATCH] remove non-determinism in number_chars/2 and numbercodes/2 (#1473) --- src/lib/builtins.pl | 45 +++++++++++++++++-------------------- src/machine/system_calls.rs | 5 ++++- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/lib/builtins.pl b/src/lib/builtins.pl index bc91180c..f6d02de8 100644 --- a/src/lib/builtins.pl +++ b/src/lib/builtins.pl @@ -1375,33 +1375,32 @@ must_be_number(N, PI) :- ; throw(error(instantiation_error, PI)) ). -can_be_chars_or_vars(Cs, _) :- var(Cs), !. -can_be_chars_or_vars(Cs, PI) :- chars_or_vars(Cs, PI). - -chars_or_vars([], _). +chars_or_vars(Cs, _) :- + ( var(Cs) -> + ! + ; Cs == [] -> + ! + ). chars_or_vars([C|Cs], PI) :- ( nonvar(C) -> ( atom(C), atom_length(C, 1) -> - ( nonvar(Cs) -> - chars_or_vars(Cs, PI) - ; false - ) + chars_or_vars(Cs, PI) ; throw(error(type_error(character, C), PI)) ) ; chars_or_vars(Cs, PI) ). -can_be_codes_or_vars(Cs, _) :- var(Cs), !. -can_be_codes_or_vars(Cs, PI) :- codes_or_vars(Cs, PI). - -codes_or_vars([], _). +codes_or_vars(Cs, _) :- + ( var(Cs) -> + ! + ; Cs == [] -> + ! + ). codes_or_vars([C|Cs], PI) :- ( nonvar(C) -> ( catch(builtins:char_code(_, C), _, false) -> - ( nonvar(Cs) -> codes_or_vars(Cs, PI) - ; false - ) + codes_or_vars(Cs, PI) ; integer(C) -> throw(error(representation_error(character_code), PI)) ; throw(error(type_error(integer, C), PI)) @@ -1416,15 +1415,13 @@ number_chars(N, Chs) :- error(E, _), builtins:throw(error(E, number_chars/2)) ), - '$chars_to_number'(Chs, Nx), - Nx = N + '$chars_to_number'(Chs, N) ; must_be_number(N, number_chars/2), ( var(Chs) -> true ; can_be_list(Chs, number_chars/2), chars_or_vars(Chs, number_chars/2) ), - '$number_to_chars'(N, Chsx), - Chsx = Chs + '$number_to_chars'(N, Chs) ). list_of_ints(Ns) :- @@ -1438,15 +1435,13 @@ number_codes(N, Chs) :- error(E, _), builtins:throw(error(E, number_codes/2)) ), - '$codes_to_number'(Chs, Nx), - Nx = N + '$codes_to_number'(Chs, N) ; must_be_number(N, number_codes/2), ( var(Chs) -> true - ; can_be_list(Chs, number_codes/2) - , codes_or_vars(Chs, number_codes/2) + ; can_be_list(Chs, number_codes/2), + codes_or_vars(Chs, number_codes/2) ), - '$number_to_codes'(N, Chsx), - Chsx = Chs + '$number_to_codes'(N, Chs) ). subsumes_term(General, Specific) :- diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 5f4cc32b..61f40e0a 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -1839,7 +1839,10 @@ impl Machine { }; let chars_atom = self.machine_st.atom_tbl.build_with(&string.trim()); - self.machine_st.unify_complete_string(chars_atom, self.machine_st.store(self.machine_st.deref(chs))); + self.machine_st.unify_complete_string( + chars_atom, + self.machine_st.store(self.machine_st.deref(chs)), + ); } #[inline(always)] -- 2.54.0