SWI Prolog provides normalize_space / 2, so you can define your predicate as follows:
normalize_space_in_atom(A1,A2) :- normalize_space(atom(A2),A1).
I tried this with SWI Prolog 5.7.5 and it seems to work. You can add additional error handling if you want.
source
share