Skip to content

Commit

Permalink
remove non-determinism in number_chars/2 and numbercodes/2 (#1473)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthom committed May 14, 2022
1 parent cea1353 commit e416377
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
39 changes: 20 additions & 19 deletions src/lib/builtins.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1378,15 +1378,17 @@
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)
Expand All @@ -1395,13 +1397,16 @@
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))
Expand All @@ -1416,15 +1421,13 @@
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) :-
Expand All @@ -1438,15 +1441,13 @@
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) :-
Expand Down
5 changes: 4 additions & 1 deletion src/machine/system_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit e416377

Please sign in to comment.